-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
156 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
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 { cacheWalletAddressSubscription } from '../cache-wallets-address'; | ||
import { Wallet } from '@lace/cardano'; | ||
import { BehaviorSubject, of } from 'rxjs'; | ||
|
||
describe('cacheWalletAddressSubscription', () => { | ||
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>; | ||
|
||
cacheWalletAddressSubscription(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>; | ||
|
||
cacheWalletAddressSubscription(mockWalletManager, mockWalletRepository); | ||
|
||
expect(mockWalletRepository.updateWalletMetadata).toHaveBeenCalledWith({ | ||
walletId: 'walletId', | ||
metadata: { | ||
addresses: ['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: { addresses?: string[] } }[]>([ | ||
{ | ||
walletId: 'walletId1', | ||
metadata: { addresses: ['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>; | ||
|
||
cacheWalletAddressSubscription(mockWalletManager, mockWalletRepository); | ||
|
||
wallets$.next([ | ||
{ | ||
walletId: 'walletId1', | ||
metadata: { addresses: ['address1'] } | ||
}, | ||
{ | ||
walletId: 'walletId2', | ||
metadata: {} | ||
} | ||
]); | ||
activeWalletId$.next({ walletId: 'walletId2' }); | ||
activeWallet$.next({ addresses$: of([{ address: 'address2' }, { address: 'address3' }]) }); | ||
|
||
expect(mockWalletRepository.updateWalletMetadata).toHaveBeenNthCalledWith(1, { | ||
walletId: 'walletId1', | ||
metadata: { | ||
addresses: ['address1'] | ||
} | ||
}); | ||
|
||
expect(mockWalletRepository.updateWalletMetadata).toHaveBeenNthCalledWith(2, { | ||
walletId: 'walletId2', | ||
metadata: { | ||
addresses: ['address2', 'address3', 'address1'] | ||
} | ||
}); | ||
}); | ||
}); |
34 changes: 34 additions & 0 deletions
34
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,34 @@ | ||
import { WalletManager, WalletRepository } from '@cardano-sdk/web-extension'; | ||
import { Wallet } from '@lace/cardano'; | ||
import { filter, switchMap, withLatestFrom, zip } from 'rxjs'; | ||
|
||
export const cacheWalletAddressSubscription = ( | ||
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 allCachedWalletAddresses = wallets.reduce( | ||
(acc, { metadata: { addresses = [] } }) => [...acc, ...addresses], | ||
[] | ||
); | ||
|
||
const addresses = new Set([...walletAddresses.map(({ address }) => address), ...allCachedWalletAddresses]); | ||
|
||
walletRepository.updateWalletMetadata({ | ||
walletId: activeWallet.walletId, | ||
metadata: { | ||
...wallet.metadata, | ||
addresses: [...addresses] | ||
} | ||
}); | ||
}); | ||
}; |
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
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