@@ -93,6 +93,8 @@ export const useRecoveryGuardian = () => {
9393 if ( ! contracts . guardianExecutor ) throw new Error ( "GuardianExecutor contract address not configured" ) ;
9494 const client = getPublicClient ( { chainId : defaultChain . id } ) ;
9595
96+ console . log ( `[getGuardians] Fetching guardians for account: ${ guardedAccount } ` ) ;
97+
9698 // Get list of guardian addresses
9799 const guardians = await client . readContract ( {
98100 address : contracts . guardianExecutor ,
@@ -101,6 +103,8 @@ export const useRecoveryGuardian = () => {
101103 args : [ guardedAccount ] ,
102104 } ) ;
103105
106+ console . log ( `[getGuardians] Found ${ guardians . length } guardian(s):` , guardians ) ;
107+
104108 // For each guardian, get their status to determine if active
105109 const guardiansWithStatus = await Promise . all (
106110 guardians . map ( async ( addr ) => {
@@ -110,13 +114,16 @@ export const useRecoveryGuardian = () => {
110114 functionName : "guardianStatusFor" ,
111115 args : [ guardedAccount , addr ] ,
112116 } ) ;
117+ console . log ( `[getGuardians] Guardian ${ addr } : present=${ isPresent } , active=${ isActive } ` ) ;
113118 return { addr, isReady : isPresent && isActive } ;
114119 } ) ,
115120 ) ;
116121
117122 getGuardiansData . value = guardiansWithStatus ;
123+ console . log ( "[getGuardians] Guardians with status:" , guardiansWithStatus ) ;
118124 return guardiansWithStatus ;
119125 } catch ( err ) {
126+ console . error ( "[getGuardians] Error fetching guardians:" , err ) ;
120127 getGuardiansError . value = err as Error ;
121128 return [ ] ;
122129 } finally {
@@ -166,7 +173,62 @@ export const useRecoveryGuardian = () => {
166173 const { inProgress : proposeGuardianInProgress , error : proposeGuardianError , execute : proposeGuardian } = useAsync ( async ( address : Address ) => {
167174 if ( ! contracts . guardianExecutor ) throw new Error ( "GuardianExecutor contract address not configured" ) ;
168175
169- const client = getClient ( { chainId : defaultChain . id } ) ;
176+ const client = getClient ( { chainId : defaultChain . id , usePaymaster : true } ) ;
177+ const accountAddress = client . account . address ;
178+
179+ console . log ( `[proposeGuardian] Account: ${ accountAddress } , Proposing guardian: ${ address } ` ) ;
180+
181+ // Check if GuardianExecutor module is installed
182+ const publicClient = getPublicClient ( { chainId : defaultChain . id } ) ;
183+ const isModuleInstalled = await publicClient . readContract ( {
184+ address : accountAddress ,
185+ abi : [ {
186+ type : "function" ,
187+ name : "isModuleInstalled" ,
188+ inputs : [
189+ { name : "moduleTypeId" , type : "uint256" } ,
190+ { name : "module" , type : "address" } ,
191+ { name : "additionalContext" , type : "bytes" } ,
192+ ] ,
193+ outputs : [ { type : "bool" } ] ,
194+ stateMutability : "view" ,
195+ } ] ,
196+ functionName : "isModuleInstalled" ,
197+ args : [ 1n , contracts . guardianExecutor , "0x" ] , // 1 = MODULE_TYPE_EXECUTOR
198+ } ) ;
199+
200+ console . log ( `[proposeGuardian] GuardianExecutor module installed: ${ isModuleInstalled } ` ) ;
201+
202+ // Install module if not already installed
203+ if ( ! isModuleInstalled ) {
204+ console . log ( "[proposeGuardian] Installing GuardianExecutor module..." ) ;
205+ const installTx = await client . writeContract ( {
206+ address : accountAddress ,
207+ abi : [ {
208+ type : "function" ,
209+ name : "installModule" ,
210+ inputs : [
211+ { name : "moduleTypeId" , type : "uint256" } ,
212+ { name : "module" , type : "address" } ,
213+ { name : "initData" , type : "bytes" } ,
214+ ] ,
215+ outputs : [ ] ,
216+ stateMutability : "nonpayable" ,
217+ } ] ,
218+ functionName : "installModule" ,
219+ args : [ 1n , contracts . guardianExecutor , "0x" ] , // 1 = MODULE_TYPE_EXECUTOR, empty initData
220+ } ) ;
221+
222+ console . log ( `[proposeGuardian] Module installation transaction sent: ${ installTx } ` ) ;
223+ const installReceipt = await client . waitForTransactionReceipt ( { hash : installTx } ) ;
224+
225+ if ( installReceipt . status === "reverted" ) {
226+ console . error ( "[proposeGuardian] Module installation reverted. Receipt:" , installReceipt ) ;
227+ throw new Error ( `Failed to install GuardianExecutor module for account ${ accountAddress } ` ) ;
228+ }
229+
230+ console . log ( "[proposeGuardian] GuardianExecutor module installed successfully" ) ;
231+ }
170232
171233 // Call proposeGuardian on the GuardianExecutor module through the smart account
172234 const tx = await client . writeContract ( {
@@ -176,8 +238,19 @@ export const useRecoveryGuardian = () => {
176238 args : [ address ] ,
177239 } ) ;
178240
241+ console . log ( `[proposeGuardian] Transaction sent: ${ tx } ` ) ;
242+
179243 // Wait for transaction receipt
180244 const receipt = await client . waitForTransactionReceipt ( { hash : tx } ) ;
245+
246+ console . log ( `[proposeGuardian] Transaction confirmed. Status: ${ receipt . status } ` ) ;
247+
248+ if ( receipt . status === "reverted" ) {
249+ console . error ( "[proposeGuardian] Transaction reverted. Receipt:" , receipt ) ;
250+ throw new Error ( `Failed to propose guardian ${ address } for account ${ accountAddress } ` ) ;
251+ }
252+
253+ console . log ( `[proposeGuardian] Guardian ${ address } proposed successfully for account ${ accountAddress } ` ) ;
181254 return receipt ;
182255 } ) ;
183256
@@ -188,7 +261,7 @@ export const useRecoveryGuardian = () => {
188261 const { inProgress : removeGuardianInProgress , error : removeGuardianError , execute : removeGuardian } = useAsync ( async ( address : Address ) => {
189262 if ( ! contracts . guardianExecutor ) throw new Error ( "GuardianExecutor contract address not configured" ) ;
190263
191- const client = getClient ( { chainId : defaultChain . id } ) ;
264+ const client = getClient ( { chainId : defaultChain . id , usePaymaster : true } ) ;
192265
193266 const tx = await client . writeContract ( {
194267 address : contracts . guardianExecutor ,
@@ -206,28 +279,63 @@ export const useRecoveryGuardian = () => {
206279
207280 /**
208281 * Accept/confirm a guardian proposal for a given account
209- * This is called by the guardian (from their EOA) to accept the guardian role
282+ * This is called by the guardian (from their EOA or smart account ) to accept the guardian role
210283 */
211284 const { inProgress : confirmGuardianInProgress , error : confirmGuardianError , execute : confirmGuardian } = useAsync ( async < transport extends Transport , chain extends Chain , account extends Account > ( { client, accountToGuard } : { client : WalletClient < transport , chain , account > ; accountToGuard : Address } ) => {
212285 if ( ! contracts . guardianExecutor ) throw new Error ( "GuardianExecutor contract address not configured" ) ;
213286
214- // Call acceptGuardian directly from the guardian's wallet
215- const tx = await client . writeContract ( {
216- address : contracts . guardianExecutor ,
217- abi : GuardianExecutorAbi ,
218- functionName : "acceptGuardian" ,
219- args : [ accountToGuard ] ,
220- chain : null ,
221- } ) ;
287+ const guardianAddress = client . account . address ;
288+ console . log ( `[confirmGuardian] Guardian address: ${ guardianAddress } , Account to guard: ${ accountToGuard } ` ) ;
289+
290+ // First, verify the guardian was actually proposed
291+ console . log ( "[confirmGuardian] Checking if guardian was proposed..." ) ;
292+ const guardians = await getGuardians ( accountToGuard ) ;
293+ const guardianStatus = guardians . find ( ( g ) => g . addr . toLowerCase ( ) === guardianAddress . toLowerCase ( ) ) ;
222294
223- const transactionReceipt = await waitForTransactionReceipt ( client , { hash : tx , confirmations : 1 } ) ;
295+ if ( ! guardianStatus ) {
296+ throw new Error ( `Guardian ${ guardianAddress } was never proposed for account ${ accountToGuard } . The account owner must propose this guardian first before you can accept.` ) ;
297+ }
224298
225- // Check if transaction was successful
226- if ( transactionReceipt . status === "reverted" ) {
227- throw new Error ( "Transaction reverted: Guardian confirmation failed" ) ;
299+ if ( guardianStatus . isReady ) {
300+ console . log ( `[confirmGuardian] Guardian ${ guardianAddress } is already active for account ${ accountToGuard } ` ) ;
301+ return { alreadyActive : true } ;
228302 }
229303
230- return { transactionReceipt } ;
304+ console . log ( "[confirmGuardian] Guardian found in pending state, proceeding with acceptance..." ) ;
305+
306+ try {
307+ // Call acceptGuardian from the guardian's wallet
308+ const tx = await client . writeContract ( {
309+ address : contracts . guardianExecutor ,
310+ abi : GuardianExecutorAbi ,
311+ functionName : "acceptGuardian" ,
312+ args : [ accountToGuard ] ,
313+ chain : null ,
314+ } ) ;
315+
316+ console . log ( `[confirmGuardian] Transaction sent: ${ tx } ` ) ;
317+
318+ const transactionReceipt = await waitForTransactionReceipt ( client , { hash : tx , confirmations : 1 } ) ;
319+
320+ // Check if transaction was successful
321+ if ( transactionReceipt . status === "reverted" ) {
322+ console . error ( "[confirmGuardian] Transaction reverted. Receipt:" , transactionReceipt ) ;
323+ throw new Error ( `Transaction reverted: Guardian confirmation failed. Guardian: ${ guardianAddress } , Account: ${ accountToGuard } . This usually means the guardian was not properly proposed or the GuardianExecutor module is not installed.` ) ;
324+ }
325+
326+ console . log ( "[confirmGuardian] Guardian confirmed successfully" ) ;
327+ return { transactionReceipt } ;
328+ } catch ( error : unknown ) {
329+ console . error ( "[confirmGuardian] Error details:" , error ) ;
330+ // Try to provide more specific error message
331+ if ( ( error as Error ) . message ?. includes ( "GuardianNotFound" ) ) {
332+ throw new Error ( `Guardian ${ guardianAddress } was not found in pending guardians for account ${ accountToGuard } . Make sure the guardian was proposed first by the account owner.` ) ;
333+ }
334+ if ( error . message ?. includes ( "NotInitialized" ) ) {
335+ throw new Error ( `GuardianExecutor module is not initialized for account ${ accountToGuard } . The account owner needs to install the GuardianExecutor module first.` ) ;
336+ }
337+ throw error ;
338+ }
231339 } ) ;
232340
233341 /**
0 commit comments