@@ -13,9 +13,12 @@ import {
1313 LazorkitClient ,
1414 SmartWalletActionArgs ,
1515 SmartWalletAction ,
16- } from '../../contract-integration' ;
16+ asCredentialHash ,
17+ asPasskeyPublicKey
18+ } from '../../contract' ;
1719import { getFeePayer , signAndSendTxn } from '../paymaster' ;
1820import { logger } from '../logger' ;
21+ import { sha256 } from 'js-sha256' ;
1922
2023/**
2124 * Factory that returns high-level wallet operations bound to a given
@@ -35,18 +38,28 @@ export const createWalletActions = (
3538 setLoading ( true ) ;
3639
3740 try {
38- // Debug log removed
39- let { smartWallet, walletDevice } = await lazorProgram . getSmartWalletByPasskey (
40- data . passkeyPubkey
41+ // Compute credential hash
42+ const credentialHash = asCredentialHash (
43+ Array . from (
44+ new Uint8Array (
45+ sha256 . arrayBuffer ( Buffer . from ( data . credentialId , 'base64' ) )
46+ )
47+ )
4148 ) ;
4249
43- if ( ! smartWallet || ! walletDevice ) {
44- // Debug log removed
50+ // Fetch initial wallet state
51+ let walletState = await lazorProgram . getSmartWalletByCredentialHash ( credentialHash ) ;
52+
53+ let smartWallet : anchor . web3 . PublicKey | undefined ;
54+ let walletDevice : anchor . web3 . PublicKey | undefined ;
55+
56+ if ( ! walletState ) {
57+ // === Create new smart wallet on chain ===
4558
4659 const feePayer = await getFeePayer ( config . paymasterUrl ) ;
4760
48- const result = await lazorProgram . createSmartWalletTx ( {
49- passkeyPubkey : data . passkeyPubkey ,
61+ const result = await lazorProgram . createSmartWalletTxn ( {
62+ passkeyPublicKey : asPasskeyPublicKey ( data . passkeyPubkey ) ,
5063 payer : feePayer ,
5164 credentialIdBase64 : data . credentialId ,
5265 } ) ;
@@ -67,22 +80,32 @@ export const createWalletActions = (
6780 throw new Error ( `Create wallet relayer error: ${ JSON . stringify ( sendError ) } ` ) ;
6881 }
6982
70- await lazorProgram . connection . confirmTransaction ( String ( sendResult . signature ) , 'confirmed' ) ;
83+ await lazorProgram . connection . confirmTransaction (
84+ String ( sendResult . signature ) ,
85+ 'confirmed'
86+ ) ;
7187
72- const fetched = await lazorProgram . getSmartWalletByPasskey ( data . passkeyPubkey ) ;
73- smartWallet = fetched . smartWallet ;
74- walletDevice = fetched . walletDevice ;
88+ walletState = await lazorProgram . getSmartWalletByCredentialHash ( credentialHash ) ;
7589
76- if ( ! smartWallet || ! walletDevice ) {
77- logger . error ( 'Failed to create smart wallet on chain after transaction confirmed' , {
78- signature : sendResult . signature ,
79- passkeyPubkey : data . passkeyPubkey ,
80- } ) ;
90+ if ( ! walletState ) {
91+ logger . error (
92+ 'Failed to create smart wallet on chain after transaction confirmed' ,
93+ {
94+ signature : sendResult . signature ,
95+ passkeyPubkey : data . passkeyPubkey ,
96+ }
97+ ) ;
8198 throw new Error ( 'Failed to create smart wallet on chain' ) ;
8299 }
83100 }
84101
85- // Return the enriched WalletInfo
102+ smartWallet = walletState . smartWallet ;
103+ walletDevice = walletState . walletDevice ;
104+
105+ if ( ! smartWallet || ! walletDevice ) {
106+ throw new Error ( 'Smart wallet or wallet device is undefined after processing.' ) ;
107+ }
108+
86109 return {
87110 ...data ,
88111 smartWallet : smartWallet . toString ( ) ,
0 commit comments