@@ -254,6 +254,7 @@ const connectHardwareWalletRevamped = async (usbDevice: USBDevice): Promise<Wall
254254// eslint-disable-next-line max-statements
255255export 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 *
0 commit comments