@@ -254,6 +254,7 @@ const connectHardwareWalletRevamped = async (usbDevice: USBDevice): Promise<Wall
254
254
// eslint-disable-next-line max-statements
255
255
export const useWalletManager = ( ) : UseWalletManager => {
256
256
const {
257
+ deletingWallet,
257
258
walletLock,
258
259
setWalletLock,
259
260
cardanoWallet,
@@ -406,6 +407,42 @@ export const useWalletManager = (): UseWalletManager => {
406
407
return firstValueFrom ( walletRepository . wallets$ ) ;
407
408
} , [ resetWalletLock , backgroundService , setCardanoWallet , setWalletLock , getCurrentChainId ] ) ;
408
409
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
+
409
446
/**
410
447
* Loads wallet from storage.
411
448
* @returns resolves with wallet information or null when no wallet is found
@@ -427,6 +464,9 @@ export const useWalletManager = (): UseWalletManager => {
427
464
// If there is no active wallet, activate the 1st one
428
465
if ( ! activeWalletProps ) {
429
466
// deleting a wallet calls deactivateWallet(): do nothing, wallet will also be deleted from repository
467
+ if ( ! deletingWallet ) {
468
+ await activateAnyWallet ( wallets ) ;
469
+ }
430
470
return ;
431
471
}
432
472
@@ -484,6 +524,8 @@ export const useWalletManager = (): UseWalletManager => {
484
524
cardanoWallet ,
485
525
currentChain ,
486
526
manageAccountsWallet ,
527
+ activateAnyWallet ,
528
+ deletingWallet ,
487
529
setCardanoCoin ,
488
530
setManageAccountsWallet
489
531
]
@@ -612,31 +654,6 @@ export const useWalletManager = (): UseWalletManager => {
612
654
return createWallet ( { name, passphrase, metadata, encryptedSecrets, extendedAccountPublicKey } ) ;
613
655
} ;
614
656
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
-
640
657
/**
641
658
* Deletes Wallet from memory, all info from browser storage and destroys all stores
642
659
*
0 commit comments