@@ -444,46 +444,43 @@ public async Task<string> Sign(string message)
444444 }
445445 if ( sw . SmartWallet . IsDeployed )
446446 {
447+ byte [ ] originalMsgHash = System . Text . Encoding . UTF8 . GetBytes ( message ) . HashPrefixedMessage ( ) ;
448+ string swAddress = await GetAddress ( ) ;
449+ bool factorySupports712 ;
450+ string signature = null ;
447451 try
448452 {
449- byte [ ] originalMsgHash = System . Text . Encoding . UTF8 . GetBytes ( message ) . HashPrefixedMessage ( ) ;
450-
451453 // if this fails it's a pre 712 factory
452454 await TransactionManager . ThirdwebRead < Contracts . Account . ContractDefinition . GetMessageHashFunction , Contracts . Account . ContractDefinition . GetMessageHashOutputDTO > (
453- await GetAddress ( ) ,
455+ swAddress ,
454456 new Contracts . Account . ContractDefinition . GetMessageHashFunction ( ) { Hash = originalMsgHash }
455457 ) ;
456-
457- string sig = await EIP712 . GenerateSignature_SmartAccount_AccountMessage ( "Account" , "1" , await GetChainId ( ) , await GetAddress ( ) , originalMsgHash ) ;
458-
459- bool isValid = await RecoverAddress ( message , sig ) == await GetAddress ( ) ;
460- if ( isValid )
461- {
462- return sig ;
463- }
464- else
465- {
466- throw new Exception ( "Unable to verify signature on smart account, please make sure the smart account is deployed and the signature is valid." ) ;
467- }
458+ factorySupports712 = true ;
468459 }
469- catch ( Exception e )
460+ catch
470461 {
471- ThirdwebDebug . LogWarning ( "Error signing message with smart account typed data, falling back to personal sign: " + e . Message ) ;
462+ ThirdwebDebug . Log ( "Account does not support 712 signing, using personal_sign" ) ;
463+ factorySupports712 = false ;
472464 }
465+
466+ if ( factorySupports712 )
467+ signature = await EIP712 . GenerateSignature_SmartAccount_AccountMessage ( "Account" , "1" , await GetChainId ( ) , swAddress , originalMsgHash ) ;
468+ else
469+ signature = await ThirdwebManager . Instance . SDK . session . Request < string > ( "personal_sign" , message , await GetSignerAddress ( ) ) ;
470+
471+ bool isValid = await RecoverAddress ( message , signature ) == swAddress ;
472+ if ( isValid )
473+ return signature ;
474+ else
475+ throw new Exception ( "Unable to verify signature on smart account, please make sure the smart account is deployed and the signature is valid." ) ;
473476 }
474477 else
475478 {
476479 throw new Exception ( "Smart account could not be deployed, unable to sign message." ) ;
477480 }
478481 }
479482
480- var sig2 = await ThirdwebManager . Instance . SDK . session . Request < string > ( "personal_sign" , message , await GetSignerAddress ( ) ) ;
481- var verify = await RecoverAddress ( message , sig2 ) == await GetAddress ( ) ;
482- if ( ! verify )
483- {
484- throw new Exception ( "Unable to verify signature, please make sure the wallet is unlocked and the signature is valid." ) ;
485- }
486- return sig2 ;
483+ return await ThirdwebManager . Instance . SDK . session . Request < string > ( "personal_sign" , message , await GetSignerAddress ( ) ) ;
487484 }
488485 }
489486
0 commit comments