Verifying Account Abstracted Instant Actions (ERC-4337)
Example Code Snippet
import json
from eth_typing import HexStr
from eth_utils import keccak, to_bytes, to_hex
from siwe import SiweMessage
from siwe import siwe
from web3 import Web3
RPC = ''
web3 = Web3(Web3.HTTPProvider(RPC))
def _encode_erc4337(message: SiweMessage) -> HexStr:
data_str = message.prepare_message()
print(data_str)
hex_message = data_str.encode('utf-8').hex()
presign_message_prefix = f'\x19Ethereum Signed Message:\n{len(data_str)}'
prefix = presign_message_prefix.encode('utf-8').hex()
combined_message = f'0x{prefix}{hex_message}'
return to_hex(keccak(to_bytes(hexstr=combined_message)))
message_dict = {
"account_address": "0x94b1e92397bC7Bc0964e1d6C736277AF27E5a76a",
"expiration_time": "2024-12-19T08:09:21.231Z",
"issued_at": "2024-12-16T08:09:21.231Z",
"signature": "0xf8582d00465b648c7d3f0f06d1ca41553af52bf70e1f21b115c7a18a0f548dd032000319a7947d17575ffba4a358a9e5be4f9302216a41e22ba1f4aa7f5108ac20",
"nonce": "1ESHS1N2VcQ"
}
domain = "vibe-ui-git-hedger-whitelist-link-vibe-tradings-projects.vercel.app"
uri = "https://base-hedger82.rasa.capital/login"
signature = message_dict['signature']
owner = "0x3Da94BF626065EF31dab46529ea0A69761a01a1e"
fields = {
"domain": domain,
"address": owner,
"uri": uri,
"version": siwe.VersionEnum.one,
"chain_id": "8453",
"nonce": message_dict["nonce"],
"issued_at": message_dict["issued_at"],
"statement": f'msg: {message_dict["account_address"]}',
"expiration_time": message_dict["expiration_time"]
}
siwe_message = SiweMessage(**fields)
hash_message = _encode_erc4337(siwe_message)
with open('SignerValidatorAbi.json') as f:
abi = json.load(f)
contract = web3.eth.contract(address=owner, abi=abi)
try:
magic_value = contract.functions.isValidSignature(hash_message, signature).call()
print("Magic Value Returned:", magic_value.hex())
print(magic_value == Web3.to_bytes(hexstr=HexStr('0x1626ba7e')))
except Exception as e:
print("Error calling isValidSignature:", e)
Constructing the SIWE Message
Encoding the Message for EIP-1271 Verification
On-Chain Signature Verification
Account Abstracted Flow– Verifying Instant Actions:
Last updated
