@@ -4,7 +4,7 @@ import type { Address, Chain, Hex, WalletClient } from 'viem';
44import { proveIdentityOwnership } from '../identity/prove-ownership.js' ;
55import * as Messages from '../messages/index.js' ;
66import { store } from '../store-connect.js' ;
7- import { loadAccountAddress , storeAccountAddress , storeKeys } from './auth-storage.js' ;
7+ import { loadAccountAddress , storeAccountAddress , storeKeys , wipeAccountAddress } from './auth-storage.js' ;
88import { createIdentityKeys } from './create-identity-keys.js' ;
99import { decryptIdentity , encryptIdentity } from './identity-encryption.js' ;
1010import {
@@ -103,23 +103,7 @@ export async function restoreKeys(
103103 throw new Error ( `Error fetching identity ${ res . status } ` ) ;
104104}
105105
106- export async function login ( {
107- walletClient,
108- signer,
109- syncServerUri,
110- storage,
111- identityToken,
112- rpcUrl,
113- chain,
114- } : {
115- walletClient : WalletClient ;
116- signer : Signer ;
117- syncServerUri : string ;
118- storage : Storage ;
119- identityToken : string ;
120- rpcUrl : string ;
121- chain : Chain ;
122- } ) {
106+ const getAndDeploySmartAccount = async ( walletClient : WalletClient , rpcUrl : string , chain : Chain , storage : Storage ) => {
123107 const accountAddressFromStorage = loadAccountAddress ( storage ) as Hex ;
124108 const smartAccountParams : SmartAccountParams = {
125109 owner : walletClient ,
@@ -140,10 +124,8 @@ export async function login({
140124 if ( await smartAccountNeedsUpdate ( smartAccountWalletClient , chain , rpcUrl ) ) {
141125 console . log ( 'updating smart account' ) ;
142126 await updateLegacySmartAccount ( smartAccountWalletClient , chain , rpcUrl ) ;
143- }
144-
145- // TODO: remove this once we manage to get counterfactual signatures working
146- if ( ! ( await isSmartAccountDeployed ( smartAccountWalletClient ) ) ) {
127+ } else if ( ! ( await isSmartAccountDeployed ( smartAccountWalletClient ) ) ) {
128+ // TODO: remove this once we manage to get counterfactual signatures working
147129 console . log ( 'sending dummy userOp to deploy smart account' ) ;
148130 if ( ! walletClient . account ) {
149131 throw new Error ( 'Wallet client account not found' ) ;
@@ -157,6 +139,36 @@ export async function login({
157139 const receipt = await smartAccountWalletClient . waitForUserOperationReceipt ( { hash : tx } ) ;
158140 console . log ( 'receipt' , receipt ) ;
159141 }
142+ return smartAccountWalletClient ;
143+ } ;
144+
145+ export async function login ( {
146+ walletClient,
147+ signer,
148+ syncServerUri,
149+ storage,
150+ identityToken,
151+ rpcUrl,
152+ chain,
153+ } : {
154+ walletClient : WalletClient ;
155+ signer : Signer ;
156+ syncServerUri : string ;
157+ storage : Storage ;
158+ identityToken : string ;
159+ rpcUrl : string ;
160+ chain : Chain ;
161+ } ) {
162+ let smartAccountWalletClient : SmartAccountClient ;
163+ try {
164+ smartAccountWalletClient = await getAndDeploySmartAccount ( walletClient , rpcUrl , chain , storage ) ;
165+ } catch ( error ) {
166+ wipeAccountAddress ( storage ) ;
167+ smartAccountWalletClient = await getAndDeploySmartAccount ( walletClient , rpcUrl , chain , storage ) ;
168+ }
169+ if ( ! smartAccountWalletClient . account ) {
170+ throw new Error ( 'Smart account wallet client account not found' ) ;
171+ }
160172 const accountAddress = smartAccountWalletClient . account . address ;
161173 // const keys = loadKeys(storage, accountAddress);
162174 let authData : {
0 commit comments