@@ -687,26 +687,42 @@ export class Wallets implements WalletsInterface {
687
687
// If there is an existing wallet callback, we check if any wallet already exist for this login signer
688
688
if ( this . walletSelectionUiHandler ) {
689
689
const existingWallets = await State . getWalletsFor ( this . shared . sequence . stateProvider , loginSigner . signer )
690
+
690
691
if ( existingWallets . length > 0 ) {
692
+ for ( const wallet of existingWallets ) {
693
+ const preliminaryEntry : Wallet = {
694
+ address : wallet . wallet ,
695
+ status : 'logging-in' ,
696
+ loginEmail : loginSigner . loginEmail ,
697
+ loginType : loginSigner . extra . signerKind ,
698
+ loginDate : new Date ( ) . toISOString ( ) ,
699
+ device : '' as `0x${string } `,
700
+ useGuard : false ,
701
+ }
702
+ await this . shared . databases . manager . set ( preliminaryEntry )
703
+ }
704
+
691
705
const result = await this . walletSelectionUiHandler ( {
692
706
existingWallets : existingWallets . map ( ( w ) => w . wallet ) ,
693
707
signerAddress : await loginSigner . signer . address ,
694
- context : isAuthCodeArgs ( args )
695
- ? {
696
- isRedirect : args . isRedirect ,
697
- target : args . target ,
698
- }
699
- : {
700
- isRedirect : false ,
701
- } ,
708
+ context : isAuthCodeArgs ( args ) ? { isRedirect : args . isRedirect , target : args . target } : { isRedirect : false } ,
702
709
} )
703
710
704
711
if ( result === 'abort-signup' ) {
712
+ for ( const wallet of existingWallets ) {
713
+ const finalEntry = await this . shared . databases . manager . get ( wallet . wallet )
714
+ if ( finalEntry && ! finalEntry . device ) {
715
+ await this . shared . databases . manager . del ( wallet . wallet )
716
+ }
717
+ }
705
718
// Abort the signup process
706
719
return undefined
707
720
}
708
721
709
722
if ( result === 'create-new' ) {
723
+ for ( const wallet of existingWallets ) {
724
+ await this . shared . databases . manager . del ( wallet . wallet )
725
+ }
710
726
// Continue with the signup process
711
727
} else {
712
728
throw new Error ( 'invalid-result-from-wallet-selector' )
@@ -867,8 +883,9 @@ export class Wallets implements WalletsInterface {
867
883
868
884
async login ( args : LoginArgs ) : Promise < string > {
869
885
if ( isLoginToWalletArgs ( args ) ) {
870
- const prevWallet = await this . has ( args . wallet )
871
- if ( prevWallet ) {
886
+ const existingWallet = await this . get ( args . wallet )
887
+
888
+ if ( existingWallet ?. status === 'ready' ) {
872
889
throw new Error ( 'wallet-already-logged-in' )
873
890
}
874
891
@@ -898,21 +915,20 @@ export class Wallets implements WalletsInterface {
898
915
await this . shared . modules . recovery . addRecoverySignerToModules ( modules , device . address )
899
916
}
900
917
901
- const existingEntry = await this . shared . databases . manager . get ( args . wallet )
902
-
903
- const walletEntryToUpdate = {
904
- ...( existingEntry ?? { } ) ,
918
+ const walletEntryToUpdate : Wallet = {
919
+ ...( existingWallet as Wallet ) ,
905
920
address : args . wallet ,
906
921
status : 'logging-in' as const ,
907
922
loginDate : new Date ( ) . toISOString ( ) ,
908
923
device : device . address ,
909
- loginType : 'wallet' as const ,
924
+ loginType : existingWallet ?. loginType || 'wallet' ,
925
+ loginEmail : existingWallet ?. loginEmail ,
910
926
useGuard : guardTopology !== undefined ,
911
927
}
912
928
913
929
await this . shared . databases . manager . set ( walletEntryToUpdate )
914
930
915
- return this . requestConfigurationUpdate (
931
+ const requestId = await this . requestConfigurationUpdate (
916
932
args . wallet ,
917
933
{
918
934
devicesTopology : nextDevicesTopology ,
@@ -921,6 +937,7 @@ export class Wallets implements WalletsInterface {
921
937
'login' ,
922
938
'wallet-webapp' ,
923
939
)
940
+ return requestId
924
941
}
925
942
926
943
if ( isLoginToMnemonicArgs ( args ) ) {
0 commit comments