Skip to content

Commit 66a958b

Browse files
committed
fix: activate any wallet when none is active
in order to recover from wallet activation bugs for example, a crash after wallet deactivation but before activating another wallet fix: do not explicitly deactivate wallet when switching to another explicitly deactivating will trigger a recovery mechanism that will activate any wallet
1 parent ed01c97 commit 66a958b

File tree

2 files changed

+45
-27
lines changed

2 files changed

+45
-27
lines changed

apps/browser-extension-wallet/src/hooks/useWalletManager.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ const connectHardwareWalletRevamped = async (usbDevice: USBDevice): Promise<Wall
254254
// eslint-disable-next-line max-statements
255255
export const useWalletManager = (): UseWalletManager => {
256256
const {
257+
deletingWallet,
257258
walletLock,
258259
setWalletLock,
259260
cardanoWallet,
@@ -406,6 +407,42 @@ export const useWalletManager = (): UseWalletManager => {
406407
return firstValueFrom(walletRepository.wallets$);
407408
}, [resetWalletLock, backgroundService, setCardanoWallet, setWalletLock, getCurrentChainId]);
408409

410+
const activateWallet = useCallback(
411+
async (props: ActivateWalletProps): Promise<void> => {
412+
const [wallets, activeWallet] = await firstValueFrom(
413+
combineLatest([walletRepository.wallets$, walletManager.activeWalletId$])
414+
);
415+
if (activeWallet?.walletId === props.walletId && activeWallet?.accountIndex === props.accountIndex) {
416+
logger.debug('Wallet is already active');
417+
return;
418+
}
419+
const updateWalletMetadataProps = {
420+
walletId: props.walletId,
421+
metadata: {
422+
...wallets.find(({ walletId }) => walletId === props.walletId).metadata,
423+
lastActiveAccountIndex: props.accountIndex
424+
}
425+
};
426+
await walletRepository.updateWalletMetadata(updateWalletMetadataProps);
427+
await walletManager.activate({
428+
...props,
429+
chainId: getCurrentChainId()
430+
});
431+
},
432+
[getCurrentChainId]
433+
);
434+
435+
const activateAnyWallet = useCallback(
436+
async (wallets: AnyWallet<Wallet.WalletMetadata, Wallet.AccountMetadata>[]) => {
437+
const anyWallet: ActivateWalletProps = {
438+
walletId: wallets[0].walletId,
439+
accountIndex: wallets[0].type === WalletType.Script ? undefined : wallets[0].accounts[0]?.accountIndex
440+
};
441+
await activateWallet(anyWallet);
442+
},
443+
[activateWallet]
444+
);
445+
409446
/**
410447
* Loads wallet from storage.
411448
* @returns resolves with wallet information or null when no wallet is found
@@ -427,6 +464,9 @@ export const useWalletManager = (): UseWalletManager => {
427464
// If there is no active wallet, activate the 1st one
428465
if (!activeWalletProps) {
429466
// deleting a wallet calls deactivateWallet(): do nothing, wallet will also be deleted from repository
467+
if (!deletingWallet) {
468+
await activateAnyWallet(wallets);
469+
}
430470
return;
431471
}
432472

@@ -484,6 +524,8 @@ export const useWalletManager = (): UseWalletManager => {
484524
cardanoWallet,
485525
currentChain,
486526
manageAccountsWallet,
527+
activateAnyWallet,
528+
deletingWallet,
487529
setCardanoCoin,
488530
setManageAccountsWallet
489531
]
@@ -612,31 +654,6 @@ export const useWalletManager = (): UseWalletManager => {
612654
return createWallet({ name, passphrase, metadata, encryptedSecrets, extendedAccountPublicKey });
613655
};
614656

615-
const activateWallet = useCallback(
616-
async (props: ActivateWalletProps): Promise<void> => {
617-
const [wallets, activeWallet] = await firstValueFrom(
618-
combineLatest([walletRepository.wallets$, walletManager.activeWalletId$])
619-
);
620-
if (activeWallet?.walletId === props.walletId && activeWallet?.accountIndex === props.accountIndex) {
621-
logger.debug('Wallet is already active');
622-
return;
623-
}
624-
await walletManager.deactivate();
625-
await walletRepository.updateWalletMetadata({
626-
walletId: props.walletId,
627-
metadata: {
628-
...wallets.find(({ walletId }) => walletId === props.walletId).metadata,
629-
lastActiveAccountIndex: props.accountIndex
630-
}
631-
});
632-
await walletManager.activate({
633-
...props,
634-
chainId: getCurrentChainId()
635-
});
636-
},
637-
[getCurrentChainId]
638-
);
639-
640657
/**
641658
* Deletes Wallet from memory, all info from browser storage and destroys all stores
642659
*

packages/e2e-tests/src/fixture/walletRepositoryInitializer.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,12 @@ export const clearWalletRepository = async (): Promise<void> => {
1616
await switchToWindowWithLace(0);
1717
await browser.execute(`
1818
return (async () => {
19-
await window.walletManager.deactivate();
2019
const wallets = await window.firstValueFrom(window.walletRepository.wallets$);
21-
for (const wallet of wallets) {
20+
// reversing in order to delete shared wallets before dependent wallets
21+
for (const wallet of wallets.reverse()) {
2222
await window.walletRepository.removeWallet(wallet.walletId);
2323
}
24+
await window.walletManager.deactivate();
2425
return JSON.stringify(wallets);
2526
})()
2627
`);

0 commit comments

Comments
 (0)