11import * as Schema from 'effect/Schema' ;
2+ import type { SmartAccountClient } from 'permissionless' ;
23import type { Address , Chain , Hex , WalletClient } from 'viem' ;
4+ import { proveIdentityOwnership } from '../identity/prove-ownership.js' ;
35import * as Messages from '../messages/index.js' ;
46import { store } from '../store-connect.js' ;
57import { loadAccountAddress , storeAccountAddress , storeKeys } from './auth-storage.js' ;
68import { createIdentityKeys } from './create-identity-keys.js' ;
79import { decryptIdentity , encryptIdentity } from './identity-encryption.js' ;
8- import { proveIdentityOwnership } from './prove-ownership.js' ;
9- import { getSmartAccountWalletClient , smartAccountNeedsUpdate , updateLegacySmartAccount } from './smart-account.js' ;
10+ import {
11+ type SmartAccountParams ,
12+ getSmartAccountWalletClient ,
13+ isSmartAccountDeployed ,
14+ smartAccountNeedsUpdate ,
15+ updateLegacySmartAccount ,
16+ } from './smart-account.js' ;
1017import type { IdentityKeys , Signer , Storage } from './types.js' ;
1118
1219export async function identityExists ( accountAddress : string , syncServerUri : string ) {
@@ -18,14 +25,21 @@ export async function identityExists(accountAddress: string, syncServerUri: stri
1825
1926export async function signup (
2027 signer : Signer ,
28+ walletClient : WalletClient ,
29+ smartAccountClient : SmartAccountClient ,
2130 accountAddress : Address ,
2231 syncServerUri : string ,
2332 storage : Storage ,
2433 identityToken : string ,
2534) {
2635 const keys = createIdentityKeys ( ) ;
2736 const { ciphertext, nonce } = await encryptIdentity ( signer , keys ) ;
28- const { accountProof, keyProof } = await proveIdentityOwnership ( signer , accountAddress , keys ) ;
37+ const { accountProof, keyProof } = await proveIdentityOwnership (
38+ walletClient ,
39+ smartAccountClient ,
40+ accountAddress ,
41+ keys ,
42+ ) ;
2943
3044 const req : Messages . RequestConnectCreateIdentity = {
3145 keyBox : { signer : await signer . getAddress ( ) , accountAddress, ciphertext, nonce } ,
@@ -105,20 +119,42 @@ export async function login({
105119 rpcUrl : string ;
106120 chain : Chain ;
107121} ) {
108- const accountAddressFromStorage = ( loadAccountAddress ( storage ) as Hex ) ?? undefined ;
109- const smartAccountWalletClient = await getSmartAccountWalletClient ( {
122+ const accountAddressFromStorage = loadAccountAddress ( storage ) as Hex ;
123+ const smartAccountParams : SmartAccountParams = {
110124 owner : walletClient ,
111- address : accountAddressFromStorage ,
112125 rpcUrl,
113126 chain,
114- } ) ;
127+ } ;
128+ if ( accountAddressFromStorage ) {
129+ smartAccountParams . address = accountAddressFromStorage ;
130+ }
131+ const smartAccountWalletClient = await getSmartAccountWalletClient ( smartAccountParams ) ;
115132 if ( ! smartAccountWalletClient . account ) {
116133 throw new Error ( 'Smart account wallet client not found' ) ;
117134 }
135+ console . log ( 'smartAccountWalletClient' , smartAccountWalletClient ) ;
136+ console . log ( 'address' , smartAccountWalletClient . account . address ) ;
137+ console . log ( 'is deployed' , await isSmartAccountDeployed ( smartAccountWalletClient ) ) ;
118138 // This will prompt the user to sign a user operation to update the smart account
119139 if ( await smartAccountNeedsUpdate ( smartAccountWalletClient , chain , rpcUrl ) ) {
120140 await updateLegacySmartAccount ( smartAccountWalletClient , chain , rpcUrl ) ;
121141 }
142+
143+ // TODO: remove this once we manage to get counterfactual signatures working
144+ if ( ! ( await isSmartAccountDeployed ( smartAccountWalletClient ) ) ) {
145+ console . log ( 'sending dummy userOp to deploy smart account' ) ;
146+ if ( ! walletClient . account ) {
147+ throw new Error ( 'Wallet client account not found' ) ;
148+ }
149+ const tx = await smartAccountWalletClient . sendUserOperation ( {
150+ calls : [ { to : walletClient . account . address , data : '0x' } ] ,
151+ account : smartAccountWalletClient . account ,
152+ } ) ;
153+
154+ console . log ( 'tx' , tx ) ;
155+ const receipt = await smartAccountWalletClient . waitForUserOperationReceipt ( { hash : tx } ) ;
156+ console . log ( 'receipt' , receipt ) ;
157+ }
122158 const accountAddress = smartAccountWalletClient . account . address ;
123159 if ( accountAddressFromStorage === undefined ) {
124160 storeAccountAddress ( storage , accountAddress ) ;
@@ -130,7 +166,15 @@ export async function login({
130166 } ;
131167 const exists = await identityExists ( accountAddress , syncServerUri ) ;
132168 if ( ! exists ) {
133- authData = await signup ( signer , accountAddress , syncServerUri , storage , identityToken ) ;
169+ authData = await signup (
170+ signer ,
171+ walletClient ,
172+ smartAccountWalletClient ,
173+ accountAddress ,
174+ syncServerUri ,
175+ storage ,
176+ identityToken ,
177+ ) ;
134178 } else {
135179 authData = await restoreKeys ( signer , accountAddress , syncServerUri , storage , identityToken ) ;
136180 }
0 commit comments