Skip to content

Commit 93ae53b

Browse files
authored
Merge pull request #1456 from input-output-hk/feat/wallet-manager-active-wallet
feat(web-extension)!: emit wallet and id from activeWallet$
2 parents 4466bed + 332eb02 commit 93ae53b

File tree

5 files changed

+21
-9
lines changed

5 files changed

+21
-9
lines changed

packages/e2e/test/web-extension/extension/background/cip30.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { cip30 } from '@cardano-sdk/web-extension';
22
import { runtime } from 'webextension-polyfill';
33
import { cip30 as walletCip30 } from '@cardano-sdk/wallet';
44

5-
import { NEVER } from 'rxjs';
5+
import { NEVER, map } from 'rxjs';
66
import { authenticator } from './authenticator';
77
import { logger } from '../util';
88
import { wallet$ } from './walletManager';
@@ -23,5 +23,9 @@ const confirmationCallback: walletCip30.CallbackConfirmation = {
2323
submitTx: async () => true
2424
};
2525

26-
const walletApi = walletCip30.createWalletApi(wallet$, confirmationCallback, { logger });
26+
const walletApi = walletCip30.createWalletApi(
27+
wallet$.pipe(map((activeWallet) => activeWallet?.observableWallet || null)),
28+
confirmationCallback,
29+
{ logger }
30+
);
2731
cip30.initializeBackgroundScript({ walletName }, { authenticator, logger, runtime, walletApi });

packages/e2e/test/web-extension/extension/background/supplyDistributionTracker.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { walletName } from '../const';
88

99
export const supplyDistributionTrackerReady = (async () =>
1010
createSupplyDistributionTracker(
11-
{ trigger$: wallet$.pipe(switchMap((wallet) => wallet.currentEpoch$)) },
11+
{ trigger$: wallet$.pipe(switchMap((wallet) => wallet.observableWallet.currentEpoch$)) },
1212
{
1313
logger,
1414
networkInfoProvider: await networkInfoProviderFactory.create(

packages/e2e/test/web-extension/extension/background/walletManager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import { InvalidArgumentError, isNotNil } from '@cardano-sdk/util';
2020
import { Metadata, env, logger } from '../util';
2121
import { storage as WebExtensionStorage, runtime } from 'webextension-polyfill';
2222
import { Witnesser } from '@cardano-sdk/key-management';
23-
import { filter, from, merge, of } from 'rxjs';
23+
import { filter, from, map, merge, of } from 'rxjs';
2424
import { getWallet } from '../../../../src';
2525
import { storage } from '@cardano-sdk/wallet';
2626
import { toEmpty } from '@cardano-sdk/util-rxjs';
@@ -129,7 +129,7 @@ exposeApi(
129129

130130
exposeApi(
131131
{
132-
api$: walletManager.activeWallet$.asObservable(),
132+
api$: walletManager.activeWallet$.pipe(map((activeWallet) => activeWallet?.observableWallet || null)),
133133
baseChannel: walletChannel(walletName),
134134
properties: observableWalletProperties
135135
},

packages/web-extension/src/walletManager/walletManager.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,11 @@ export interface WalletManagerDependencies<
4747
signingCoordinatorApi: SigningCoordinatorSignApi<WalletMetadata, AccountMetadata>;
4848
}
4949

50+
export interface ActiveWallet {
51+
observableWallet: ObservableWallet;
52+
props: WalletManagerActivateProps;
53+
}
54+
5055
/**
5156
* Helper class for background scripts using wallet manager.
5257
* Uses wallet and store factories to create wallets.
@@ -56,7 +61,7 @@ export class WalletManager<WalletMetadata extends { name: string }, AccountMetad
5661
implements WalletManagerApi
5762
{
5863
activeWalletId$ = new ReplaySubject<WalletManagerActivateProps | null>(1);
59-
activeWallet$ = new BehaviorSubject<ObservableWallet | null>(null);
64+
activeWallet$ = new BehaviorSubject<ActiveWallet | null>(null);
6065

6166
#activeWalletProps: WalletManagerActivateProps | null = null;
6267
#walletStores = new Map<string, storage.WalletStores>();
@@ -153,7 +158,7 @@ export class WalletManager<WalletMetadata extends { name: string }, AccountMetad
153158
})
154159
]);
155160

156-
this.activeWallet$.next(wallet);
161+
this.activeWallet$.next({ observableWallet: wallet, props });
157162

158163
this.activeWalletId$.next(props);
159164
}
@@ -254,7 +259,7 @@ export class WalletManager<WalletMetadata extends { name: string }, AccountMetad
254259
// Do not shutdown the active wallet while these subscriptions are still coupled with the observed wallet.
255260
// Instead, first decouple the active wallet from the observed wallet.
256261
this.#stopEmittingFromActiveWallet();
257-
wallet?.shutdown();
262+
wallet?.observableWallet.shutdown();
258263
this.#activeWalletProps = null;
259264
}
260265

packages/web-extension/test/walletManager/walletManager.test.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,10 @@ describe('WalletManager', () => {
231231

232232
it('sets active wallet to wallet created by factory', async () => {
233233
const activeWallet = await firstValueFrom(walletManager.activeWallet$);
234-
expect(activeWallet).toEqual(mockWallet);
234+
expect(activeWallet).toEqual({
235+
observableWallet: mockWallet,
236+
props: expect.objectContaining({ walletId: expect.stringContaining('') })
237+
});
235238
});
236239

237240
it('does not activate same wallet twice', async () => {

0 commit comments

Comments
 (0)