99import type { Chain } from "../../chains/types.js" ;
1010import { getCachedChain } from "../../chains/utils.js" ;
1111import type { ThirdwebClient } from "../../client/client.js" ;
12+ import { ZERO_ADDRESS } from "../../constants/addresses.js" ;
1213import { type ThirdwebContract , getContract } from "../../contract/contract.js" ;
1314import { allowance } from "../../extensions/erc20/__generated__/IERC20/read/allowance.js" ;
1415import { approve } from "../../extensions/erc20/write/approve.js" ;
@@ -285,13 +286,13 @@ async function createSmartAccount(
285286 { readContract } ,
286287 { encodeAbiParameters } ,
287288 { hashMessage } ,
288- { checkContractWalletSignature } ,
289+ { verifyContractWalletSignature } ,
289290 ] = await Promise . all ( [
290291 import ( "../../utils/bytecode/is-contract-deployed.js" ) ,
291292 import ( "../../transaction/read-contract.js" ) ,
292293 import ( "../../utils/abi/encodeAbiParameters.js" ) ,
293294 import ( "../../utils/hashing/hashMessage.js" ) ,
294- import ( "../../extensions/erc1271/checkContractWalletSignature .js" ) ,
295+ import ( "../../auth/verify-signature .js" ) ,
295296 ] ) ;
296297 const isDeployed = await isContractDeployed ( accountContract ) ;
297298 if ( ! isDeployed ) {
@@ -307,31 +308,56 @@ async function createSmartAccount(
307308 } ) ;
308309 }
309310
311+ console . log ( "contract deployed" ) ;
312+
310313 const originalMsgHash = hashMessage ( message ) ;
311- // check if the account contract supports EIP721 domain separator based signing
312- let factorySupports712 = false ;
313- try {
314- // this will throw if the contract does not support it (old factories)
315- await readContract ( {
314+ // check if the account contract supports EIP721 domain separator or modular based signing
315+ const [ messageHash , isModularFactory ] = await Promise . all ( [
316+ readContract ( {
316317 contract : accountContract ,
317318 method :
318319 "function getMessageHash(bytes32 _hash) public view returns (bytes32)" ,
319320 params : [ originalMsgHash ] ,
320- } ) ;
321- factorySupports712 = true ;
322- } catch {
323- // ignore
324- }
321+ } ) . catch ( ( err ) => {
322+ console . error ( err ) ;
323+ return undefined ;
324+ } ) ,
325+ readContract ( {
326+ contract : accountContract ,
327+ method : "function canInstall(address) public view returns (bool)" ,
328+ params : [ ZERO_ADDRESS ] ,
329+ } )
330+ . catch ( ( ) => false )
331+ . then ( ( ) => true ) ,
332+ ] ) ;
333+
334+ console . log ( "factoryType" , messageHash , isModularFactory ) ;
325335
326336 let sig : `0x${string } `;
327- if ( factorySupports712 ) {
337+ if ( messageHash ) {
328338 const wrappedMessageHash = encodeAbiParameters (
329339 [ { type : "bytes32" } ] ,
330340 [ originalMsgHash ] ,
331341 ) ;
342+
343+ const hasheTypedData = hashTypedData ( {
344+ domain : {
345+ name : isModularFactory ? "DefaultValidator" : "Account" ,
346+ version : "1" ,
347+ chainId : options . chain . id ,
348+ verifyingContract : accountContract . address ,
349+ } ,
350+ primaryType : "AccountMessage" ,
351+ types : { AccountMessage : [ { name : "message" , type : "bytes" } ] } ,
352+ message : { message : wrappedMessageHash } ,
353+ } ) ;
354+
355+ console . log ( "hasheTypedData" , hasheTypedData ) ;
356+ console . log ( "equal" , messageHash === hasheTypedData ) ;
357+
332358 sig = await options . personalAccount . signTypedData ( {
333359 domain : {
334- name : "Account" ,
360+ name : isModularFactory ? "DefaultValidator" : "Account" ,
335361 version : "1" ,
336362 chainId : options . chain . id ,
337363 verifyingContract : accountContract . address ,
@@ -340,14 +366,25 @@ async function createSmartAccount(
340366 types : { AccountMessage : [ { name : "message" , type : "bytes" } ] } ,
341367 message : { message : wrappedMessageHash } ,
342368 } ) ;
369+ if ( isModularFactory ) {
370+ // add validator address
371+ sig = concatHex ( [ ZERO_ADDRESS , sig ] ) ;
372+ }
343373 } else {
344374 sig = await options . personalAccount . signMessage ( { message } ) ;
345375 }
346376
347- const isValid = await checkContractWalletSignature ( {
348- contract : accountContract ,
377+ console . log ( sig ) ;
378+
379+ const isValid = await verifyContractWalletSignature ( {
380+ address : accountContract . address ,
381+ chain : accountContract . chain ,
382+ client : accountContract . client ,
349383 message,
350384 signature : sig ,
385+ } ) . catch ( ( err ) => {
386+ console . error ( "Error checking contract wallet signature" , err ) ;
387+ return false ;
351388 } ) ;
352389
353390 if ( isValid ) {
0 commit comments