Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: cache wallets addresses in metadata
- Loading branch information
1 parent
1f939b9
commit e28823f
Showing
10 changed files
with
199 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
107 changes: 107 additions & 0 deletions
107
...owser-extension-wallet/src/lib/scripts/background/__tests__/cache-wallets-address.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* eslint-disable no-magic-numbers */ | ||
/* eslint-disable unicorn/no-useless-undefined */ | ||
import { WalletManager, WalletRepository } from '@cardano-sdk/web-extension'; | ||
import { cacheActivatedWalletAddressSubscription } from '../cache-wallets-address'; | ||
import { Wallet } from '@lace/cardano'; | ||
import { BehaviorSubject, of } from 'rxjs'; | ||
|
||
describe('cacheActivatedWalletAddressSubscription', () => { | ||
afterEach(() => { | ||
jest.clearAllMocks(); | ||
}); | ||
|
||
it('should not trigger subscription for no active wallet', () => { | ||
const mockWalletManager = { | ||
activeWallet$: of(undefined), | ||
activeWalletId$: of(undefined) | ||
} as unknown as WalletManager<Wallet.WalletMetadata, Wallet.AccountMetadata>; | ||
|
||
const mockWalletRepository = { | ||
wallets$: of([]), | ||
updateWalletMetadata: jest.fn() | ||
} as unknown as WalletRepository<Wallet.WalletMetadata, Wallet.AccountMetadata>; | ||
|
||
cacheActivatedWalletAddressSubscription(mockWalletManager, mockWalletRepository); | ||
|
||
expect(mockWalletRepository.updateWalletMetadata).not.toHaveBeenCalled(); | ||
}); | ||
|
||
it('should subscribe and update metadata', () => { | ||
const mockWalletManager = { | ||
activeWallet$: of({ addresses$: of([{ address: 'address1' }]) }), | ||
activeWalletId$: of({ walletId: 'walletId' }) | ||
} as unknown as WalletManager<Wallet.WalletMetadata, Wallet.AccountMetadata>; | ||
|
||
const mockWalletRepository = { | ||
wallets$: of([ | ||
{ | ||
walletId: 'walletId', | ||
metadata: {} | ||
} | ||
]), | ||
updateWalletMetadata: jest.fn() | ||
} as unknown as WalletRepository<Wallet.WalletMetadata, Wallet.AccountMetadata>; | ||
|
||
cacheActivatedWalletAddressSubscription(mockWalletManager, mockWalletRepository); | ||
|
||
expect(mockWalletRepository.updateWalletMetadata).toHaveBeenCalledWith({ | ||
walletId: 'walletId', | ||
metadata: { | ||
walletAddresses: ['address1'] | ||
} | ||
}); | ||
}); | ||
|
||
it('should subscribe and update metadata when a new wallet is added and activated', () => { | ||
const activeWallet$ = new BehaviorSubject({ | ||
addresses$: of([{ address: 'address1' }]) | ||
}); | ||
const activeWalletId$ = new BehaviorSubject({ | ||
walletId: 'walletId1' | ||
}); | ||
const wallets$ = new BehaviorSubject<{ walletId: string; metadata: { walletAddresses?: string[] } }[]>([ | ||
{ | ||
walletId: 'walletId1', | ||
metadata: { walletAddresses: ['address1'] } | ||
} | ||
]); | ||
const mockWalletManager = { | ||
activeWallet$, | ||
activeWalletId$ | ||
} as unknown as WalletManager<Wallet.WalletMetadata, Wallet.AccountMetadata>; | ||
|
||
const mockWalletRepository = { | ||
wallets$, | ||
updateWalletMetadata: jest.fn() | ||
} as unknown as WalletRepository<Wallet.WalletMetadata, Wallet.AccountMetadata>; | ||
|
||
cacheActivatedWalletAddressSubscription(mockWalletManager, mockWalletRepository); | ||
|
||
wallets$.next([ | ||
{ | ||
walletId: 'walletId1', | ||
metadata: { walletAddresses: ['address1'] } | ||
}, | ||
{ | ||
walletId: 'walletId2', | ||
metadata: {} | ||
} | ||
]); | ||
activeWalletId$.next({ walletId: 'walletId2' }); | ||
activeWallet$.next({ addresses$: of([{ address: 'address2' }, { address: 'address3' }]) }); | ||
|
||
expect(mockWalletRepository.updateWalletMetadata).toHaveBeenNthCalledWith(1, { | ||
walletId: 'walletId1', | ||
metadata: { | ||
walletAddresses: ['address1'] | ||
} | ||
}); | ||
|
||
expect(mockWalletRepository.updateWalletMetadata).toHaveBeenNthCalledWith(2, { | ||
walletId: 'walletId2', | ||
metadata: { | ||
walletAddresses: ['address2', 'address3'] | ||
} | ||
}); | ||
}); | ||
}); |
31 changes: 31 additions & 0 deletions
31
apps/browser-extension-wallet/src/lib/scripts/background/cache-wallets-address.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { WalletManager, WalletRepository } from '@cardano-sdk/web-extension'; | ||
import { Wallet } from '@lace/cardano'; | ||
import { filter, switchMap, withLatestFrom, zip } from 'rxjs'; | ||
|
||
export const cacheActivatedWalletAddressSubscription = ( | ||
walletManager: WalletManager<Wallet.WalletMetadata, Wallet.AccountMetadata>, | ||
walletRepository: WalletRepository<Wallet.WalletMetadata, Wallet.AccountMetadata> | ||
): void => { | ||
zip([ | ||
walletManager.activeWalletId$.pipe(filter((activeWalletId) => Boolean(activeWalletId))), | ||
walletManager.activeWallet$.pipe( | ||
filter((wallet) => Boolean(wallet)), | ||
switchMap((wallet) => wallet.addresses$) | ||
) | ||
]) | ||
.pipe(withLatestFrom(walletRepository.wallets$)) | ||
.subscribe(([[activeWallet, walletAddresses], wallets]) => { | ||
const wallet = wallets.find(({ walletId }) => walletId === activeWallet.walletId); | ||
const uniqueAddresses = [ | ||
...new Set([...(wallet.metadata.walletAddresses || []), ...walletAddresses.map(({ address }) => address)]) | ||
]; | ||
|
||
walletRepository.updateWalletMetadata({ | ||
walletId: activeWallet.walletId, | ||
metadata: { | ||
...wallet.metadata, | ||
walletAddresses: uniqueAddresses | ||
} | ||
}); | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
apps/browser-extension-wallet/src/utils/__tests__/get-all-wallets-addresses.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
import { AnyWallet } from '@cardano-sdk/web-extension'; | ||
import { getAllWalletsAddresses } from '../get-all-wallets-addresses'; | ||
import { Wallet } from '@lace/cardano'; | ||
|
||
describe('getAllWalletsAddresses', () => { | ||
it('should return an empty array if undefined is provided', () => { | ||
const addresses = getAllWalletsAddresses(); | ||
|
||
expect(addresses).toEqual([]); | ||
}); | ||
|
||
it('should return an empty array if no wallets are provided', () => { | ||
const addresses = getAllWalletsAddresses([]); | ||
|
||
expect(addresses).toEqual([]); | ||
}); | ||
|
||
it('should return an array of payment addresses', () => { | ||
const mockWallets = [ | ||
{ | ||
metadata: { | ||
walletAddresses: ['addr1', 'addr2'] | ||
} | ||
}, | ||
{ | ||
metadata: { | ||
walletAddresses: ['addr2', 'addr3'] | ||
} | ||
} | ||
] as AnyWallet<Wallet.WalletMetadata, Wallet.AccountMetadata>[]; | ||
|
||
const addresses = getAllWalletsAddresses(mockWallets); | ||
|
||
expect(addresses).toEqual(['addr1', 'addr2', 'addr3']); | ||
}); | ||
}); |
9 changes: 9 additions & 0 deletions
9
apps/browser-extension-wallet/src/utils/get-all-wallets-addresses.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
import { AnyWallet } from '@cardano-sdk/web-extension'; | ||
import { Wallet } from '@lace/cardano'; | ||
import flatMap from 'lodash/flatMap'; | ||
|
||
export const getAllWalletsAddresses = ( | ||
wallets: AnyWallet<Wallet.WalletMetadata, Wallet.AccountMetadata>[] = [] | ||
): Wallet.Cardano.PaymentAddress[] => [ | ||
...new Set(flatMap(wallets.map(({ metadata: { walletAddresses = [] } }) => walletAddresses))) | ||
]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
4 changes: 2 additions & 2 deletions
4
packages/core/src/ui/components/DappAddressSections/DappAddressSections.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters