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" ;
@@ -262,13 +263,13 @@ async function createSmartAccount(
262263 { readContract } ,
263264 { encodeAbiParameters } ,
264265 { hashMessage } ,
265- { checkContractWalletSignature } ,
266+ { verifyContractWalletSignature } ,
266267 ] = await Promise . all ( [
267268 import ( "../../utils/bytecode/is-contract-deployed.js" ) ,
268269 import ( "../../transaction/read-contract.js" ) ,
269270 import ( "../../utils/abi/encodeAbiParameters.js" ) ,
270271 import ( "../../utils/hashing/hashMessage.js" ) ,
271- import ( "../../extensions/erc1271/checkContractWalletSignature .js" ) ,
272+ import ( "../../auth/verify-signature .js" ) ,
272273 ] ) ;
273274 const isDeployed = await isContractDeployed ( accountContract ) ;
274275 if ( ! isDeployed ) {
@@ -284,31 +285,56 @@ async function createSmartAccount(
284285 } ) ;
285286 }
286287
288+ console . log ( "contract deployed" ) ;
289+
287290 const originalMsgHash = hashMessage ( message ) ;
288- // check if the account contract supports EIP721 domain separator based signing
289- let factorySupports712 = false ;
290- try {
291- // this will throw if the contract does not support it (old factories)
292- await readContract ( {
291+ // check if the account contract supports EIP721 domain separator or modular based signing
292+ const [ messageHash , isModularFactory ] = await Promise . all ( [
293+ readContract ( {
293294 contract : accountContract ,
294295 method :
295296 "function getMessageHash(bytes32 _hash) public view returns (bytes32)" ,
296297 params : [ originalMsgHash ] ,
297- } ) ;
298- factorySupports712 = true ;
299- } catch {
300- // ignore
301- }
298+ } ) . catch ( ( err ) => {
299+ console . error ( err ) ;
300+ return undefined ;
301+ } ) ,
302+ readContract ( {
303+ contract : accountContract ,
304+ method : "function canInstall(address) public view returns (bool)" ,
305+ params : [ ZERO_ADDRESS ] ,
306+ } )
307+ . catch ( ( ) => false )
308+ . then ( ( ) => true ) ,
309+ ] ) ;
310+
311+ console . log ( "factoryType" , messageHash , isModularFactory ) ;
302312
303313 let sig : `0x${string } `;
304- if ( factorySupports712 ) {
314+ if ( messageHash ) {
305315 const wrappedMessageHash = encodeAbiParameters (
306316 [ { type : "bytes32" } ] ,
307317 [ originalMsgHash ] ,
308318 ) ;
319+
320+ const hasheTypedData = hashTypedData ( {
321+ domain : {
322+ name : isModularFactory ? "DefaultValidator" : "Account" ,
323+ version : "1" ,
324+ chainId : options . chain . id ,
325+ verifyingContract : accountContract . address ,
326+ } ,
327+ primaryType : "AccountMessage" ,
328+ types : { AccountMessage : [ { name : "message" , type : "bytes" } ] } ,
329+ message : { message : wrappedMessageHash } ,
330+ } ) ;
331+
332+ console . log ( "hasheTypedData" , hasheTypedData ) ;
333+ console . log ( "equal" , messageHash === hasheTypedData ) ;
334+
309335 sig = await options . personalAccount . signTypedData ( {
310336 domain : {
311- name : "Account" ,
337+ name : isModularFactory ? "DefaultValidator" : "Account" ,
312338 version : "1" ,
313339 chainId : options . chain . id ,
314340 verifyingContract : accountContract . address ,
@@ -317,14 +343,25 @@ async function createSmartAccount(
317343 types : { AccountMessage : [ { name : "message" , type : "bytes" } ] } ,
318344 message : { message : wrappedMessageHash } ,
319345 } ) ;
346+ if ( isModularFactory ) {
347+ // add validator address
348+ sig = concatHex ( [ ZERO_ADDRESS , sig ] ) ;
349+ }
320350 } else {
321351 sig = await options . personalAccount . signMessage ( { message } ) ;
322352 }
323353
324- const isValid = await checkContractWalletSignature ( {
325- contract : accountContract ,
354+ console . log ( sig ) ;
355+
356+ const isValid = await verifyContractWalletSignature ( {
357+ address : accountContract . address ,
358+ chain : accountContract . chain ,
359+ client : accountContract . client ,
326360 message,
327361 signature : sig ,
362+ } ) . catch ( ( err ) => {
363+ console . error ( "Error checking contract wallet signature" , err ) ;
364+ return false ;
328365 } ) ;
329366
330367 if ( isValid ) {
0 commit comments