11import * as Schema from 'effect/Schema' ;
22import type { SmartAccountClient } from 'permissionless' ;
33import type { Address , Chain , Hex , WalletClient } from 'viem' ;
4+ import { privateKeyToAccount } from 'viem/accounts' ;
45import { proveIdentityOwnership } from '../identity/prove-ownership.js' ;
56import * as Messages from '../messages/index.js' ;
67import { store } from '../store-connect.js' ;
7- import { loadAccountAddress , storeAccountAddress , storeKeys , wipeAccountAddress } from './auth-storage.js' ;
8+ import { loadAccountAddress , storeAccountAddress , storeKeys } from './auth-storage.js' ;
89import { createIdentityKeys } from './create-identity-keys.js' ;
910import { decryptIdentity , encryptIdentity } from './identity-encryption.js' ;
1011import {
1112 type SmartAccountParams ,
13+ addSmartAccountOwner ,
1214 getSmartAccountWalletClient ,
1315 isSmartAccountDeployed ,
1416 smartAccountNeedsUpdate ,
@@ -31,15 +33,23 @@ export async function signup(
3133 syncServerUri : string ,
3234 storage : Storage ,
3335 identityToken : string ,
36+ chain : Chain ,
37+ rpcUrl : string ,
3438) {
3539 const keys = createIdentityKeys ( ) ;
3640 const { ciphertext, nonce } = await encryptIdentity ( signer , keys ) ;
37- const { accountProof, keyProof } = await proveIdentityOwnership (
38- walletClient ,
39- smartAccountClient ,
40- accountAddress ,
41- keys ,
42- ) ;
41+
42+ const localAccount = privateKeyToAccount ( keys . signaturePrivateKey as `0x${string } `) ;
43+ // This will deploy the smart account if it's not deployed
44+ await addSmartAccountOwner ( smartAccountClient , localAccount . address , chain , rpcUrl ) ;
45+ const localSmartAccountClient = await getSmartAccountWalletClient ( {
46+ owner : localAccount ,
47+ address : accountAddress ,
48+ rpcUrl,
49+ chain,
50+ } ) ;
51+
52+ const { accountProof, keyProof } = await proveIdentityOwnership ( localSmartAccountClient , accountAddress , keys ) ;
4353
4454 const req : Messages . RequestConnectCreateIdentity = {
4555 keyBox : { signer : await signer . getAddress ( ) , accountAddress, ciphertext, nonce } ,
@@ -103,7 +113,7 @@ export async function restoreKeys(
103113 throw new Error ( `Error fetching identity ${ res . status } ` ) ;
104114}
105115
106- const getAndDeploySmartAccount = async ( walletClient : WalletClient , rpcUrl : string , chain : Chain , storage : Storage ) => {
116+ const getAndUpdateSmartAccount = async ( walletClient : WalletClient , rpcUrl : string , chain : Chain , storage : Storage ) => {
107117 const accountAddressFromStorage = loadAccountAddress ( storage ) as Hex ;
108118 const smartAccountParams : SmartAccountParams = {
109119 owner : walletClient ,
@@ -128,21 +138,6 @@ const getAndDeploySmartAccount = async (walletClient: WalletClient, rpcUrl: stri
128138 // Create the client again to ensure we have the 7579 config now
129139 return getSmartAccountWalletClient ( smartAccountParams ) ;
130140 }
131- if ( ! ( await isSmartAccountDeployed ( smartAccountWalletClient ) ) ) {
132- // TODO: remove this once we manage to get counterfactual signatures working
133- console . log ( 'sending dummy userOp to deploy smart account' ) ;
134- if ( ! walletClient . account ) {
135- throw new Error ( 'Wallet client account not found' ) ;
136- }
137- const tx = await smartAccountWalletClient . sendUserOperation ( {
138- calls : [ { to : walletClient . account . address , data : '0x' } ] ,
139- account : smartAccountWalletClient . account ,
140- } ) ;
141-
142- console . log ( 'tx' , tx ) ;
143- const receipt = await smartAccountWalletClient . waitForUserOperationReceipt ( { hash : tx } ) ;
144- console . log ( 'receipt' , receipt ) ;
145- }
146141 return smartAccountWalletClient ;
147142} ;
148143
@@ -163,13 +158,7 @@ export async function login({
163158 rpcUrl : string ;
164159 chain : Chain ;
165160} ) {
166- let smartAccountWalletClient : SmartAccountClient ;
167- try {
168- smartAccountWalletClient = await getAndDeploySmartAccount ( walletClient , rpcUrl , chain , storage ) ;
169- } catch ( error ) {
170- wipeAccountAddress ( storage ) ;
171- smartAccountWalletClient = await getAndDeploySmartAccount ( walletClient , rpcUrl , chain , storage ) ;
172- }
161+ const smartAccountWalletClient = await getAndUpdateSmartAccount ( walletClient , rpcUrl , chain , storage ) ;
173162 if ( ! smartAccountWalletClient . account ) {
174163 throw new Error ( 'Smart account wallet client account not found' ) ;
175164 }
@@ -189,6 +178,8 @@ export async function login({
189178 syncServerUri ,
190179 storage ,
191180 identityToken ,
181+ chain ,
182+ rpcUrl ,
192183 ) ;
193184 } else {
194185 authData = await restoreKeys ( signer , accountAddress , syncServerUri , storage , identityToken ) ;
0 commit comments