Created
February 11, 2025 22:13
-
-
Save ajhodges/e3657697e2f2b2a74957cf64087cae52 to your computer and use it in GitHub Desktop.
ERC-6492 Validator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"context" | |
"errors" | |
"fmt" | |
"github.com/ethereum/go-ethereum/accounts/abi" | |
"github.com/ethereum/go-ethereum/common" | |
"github.com/ethereum/go-ethereum/common/hexutil" | |
"github.com/ethereum/go-ethereum/rpc" | |
) | |
var ( | |
addressType, _ = abi.NewType("address", "", nil) | |
bytes32Type, _ = abi.NewType("bytes32", "", nil) | |
bytesType, _ = abi.NewType("bytes", "", nil) | |
universalSigValidatorConstructorArgs = abi.Arguments{ | |
{Name: "signer", Type: addressType}, | |
{Name: "hash", Type: bytes32Type}, | |
{Name: "signature", Type: bytesType}, | |
} | |
validateSigOffchainBytecode = hexutil.MustDecode("0x608060405234801561001057600080fd5b50604051610dfe380380610dfe83398101604081905261002f91610124565b600060405161003d906100dd565b604051809103906000f080158015610059573d6000803e3d6000fd5b5090506000816001600160a01b0316638f0684308686866040518463ffffffff1660e01b815260040161008e939291906101fb565b6020604051808303816000875af11580156100ad573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906100d19190610244565b9050806000526001601ff35b610b908061026e83390190565b634e487b7160e01b600052604160045260246000fd5b60005b8381101561011b578181015183820152602001610103565b50506000910152565b60008060006060848603121561013957600080fd5b83516001600160a01b038116811461015057600080fd5b6020850151604086015191945092506001600160401b038082111561017457600080fd5b818601915086601f83011261018857600080fd5b81518181111561019a5761019a6100ea565b604051601f8201601f19908116603f011681019083821181831017156101c2576101c26100ea565b816040528281528960208487010111156101db57600080fd5b6101ec836020830160208801610100565b80955050505050509250925092565b60018060a01b0384168152826020820152606060408201526000825180606084015261022e816080850160208701610100565b601f01601f191691909101608001949350505050565b60006020828403121561025657600080fd5b8151801515811461026657600080fd5b939250505056fe608060405234801561001057600080fd5b50610b70806100206000396000f3fe608060405234801561001057600080fd5b50600436106100415760003560e01c806376be4cea146100465780638f0684301461006d57806398ef1ed814610080575b600080fd5b61005961005436600461077c565b610093565b604051901515815260200160405180910390f35b61005961007b366004610801565b610540565b61005961008e366004610801565b6105bf565b60006001600160a01b0387163b6060827f649264926492649264926492649264926492649264926492649264926492649288886100d160208261085d565b6100dd928b9290610884565b6100e6916108ae565b14905080156101c6576000606089828a61010160208261085d565b9261010e93929190610884565b81019061011b919061096f565b9550909250905084158061012c5750865b156101bf57600080836001600160a01b03168360405161014c9190610a09565b6000604051808303816000865af19150503d8060008114610189576040519150601f19603f3d011682016040523d82523d6000602084013e61018e565b606091505b5091509150816101bc5780604051639d0d6e2d60e01b81526004016101b39190610a51565b60405180910390fd5b50505b5050610200565b87878080601f0160208091040260200160405190810160405280939291908181526020018383808284376000920191909152509294505050505b808061020c5750600083115b1561036f57604051630b135d3f60e11b81526001600160a01b038b1690631626ba7e9061023f908c908690600401610a6b565b602060405180830381865afa925050508015610278575060408051601f3d908101601f1916820190925261027591810190610a84565b60015b6102f4573d8080156102a6576040519150601f19603f3d011682016040523d82523d6000602084013e6102ab565b606091505b50851580156102ba5750600084115b156102d9576102ce8b8b8b8b8b6001610093565b945050505050610536565b80604051636f2a959960e01b81526004016101b39190610a51565b6001600160e01b03198116630b135d3f60e11b14801581610313575086155b801561031f5750600085115b1561033f576103338c8c8c8c8c6001610093565b95505050505050610536565b8415801561034a5750825b8015610354575087155b1561036357806000526001601ffd5b94506105369350505050565b610378876106a7565b604187146103ee5760405162461bcd60e51b815260206004820152603a60248201527f5369676e617475726556616c696461746f72237265636f7665725369676e657260448201527f3a20696e76616c6964207369676e6174757265206c656e67746800000000000060648201526084016101b3565b60006103fd6020828a8c610884565b610406916108ae565b90506000610418604060208b8d610884565b610421916108ae565b905060008a8a604081811061043857610438610aae565b919091013560f81c915050601b811480159061045857508060ff16601c14155b156104bb5760405162461bcd60e51b815260206004820152602d60248201527f5369676e617475726556616c696461746f723a20696e76616c6964207369676e60448201526c617475726520762076616c756560981b60648201526084016101b3565b6040805160008152602081018083528e905260ff83169181019190915260608101849052608081018390526001600160a01b038e169060019060a0016020604051602081039080840390855afa158015610519573d6000803e3d6000fd5b505050602060405103516001600160a01b03161496505050505050505b9695505050505050565b604051633b5f267560e11b815260009030906376be4cea906105719088908890889088906001908990600401610ac4565b6020604051808303816000875af1158015610590573d6000803e3d6000fd5b505050506040513d601f19601f820116820180604052508101906105b49190610b1d565b90505b949350505050565b604051633b5f267560e11b815260009030906376be4cea906105ef90889088908890889088908190600401610ac4565b6020604051808303816000875af192505050801561062a575060408051601f3d908101601f1916820190925261062791810190610b1d565b60015b6106a0573d808015610658576040519150601f19603f3d011682016040523d82523d6000602084013e61065d565b606091505b508051600181900361069c578160008151811061067c5761067c610aae565b6020910101516001600160f81b031916600160f81b1492506105b7915050565b8082fd5b90506105b7565b6106ec816040516024016106bd91815260200190565b60408051601f198184030181529190526020810180516001600160e01b031663f5b1bba960e01b1790526106ef565b50565b80516a636f6e736f6c652e6c6f67602083016000808483855afa5050505050565b6001600160a01b03811681146106ec57600080fd5b60008083601f84011261073757600080fd5b50813567ffffffffffffffff81111561074f57600080fd5b60208301915083602082850101111561076757600080fd5b9250929050565b80151581146106ec57600080fd5b60008060008060008060a0878903121561079557600080fd5b86356107a081610710565b955060208701359450604087013567ffffffffffffffff8111156107c357600080fd5b6107cf89828a01610725565b90955093505060608701356107e38161076e565b915060808701356107f38161076e565b809150509295509295509295565b6000806000806060858703121561081757600080fd5b843561082281610710565b935060208501359250604085013567ffffffffffffffff81111561084557600080fd5b61085187828801610725565b95989497509550505050565b8181038181111561087e57634e487b7160e01b600052601160045260246000fd5b92915050565b6000808585111561089457600080fd5b838611156108a157600080fd5b5050820193919092039150565b8035602083101561087e57600019602084900360031b1b1692915050565b634e487b7160e01b600052604160045260246000fd5b600082601f8301126108f357600080fd5b813567ffffffffffffffff8082111561090e5761090e6108cc565b604051601f8301601f19908116603f01168101908282118183101715610936576109366108cc565b8160405283815286602085880101111561094f57600080fd5b836020870160208301376000602085830101528094505050505092915050565b60008060006060848603121561098457600080fd5b833561098f81610710565b9250602084013567ffffffffffffffff808211156109ac57600080fd5b6109b8878388016108e2565b935060408601359150808211156109ce57600080fd5b506109db868287016108e2565b9150509250925092565b60005b83811015610a005781810151838201526020016109e8565b50506000910152565b60008251610a1b8184602087016109e5565b9190910192915050565b60008151808452610a3d8160208601602086016109e5565b601f01601f19169290920160200192915050565b602081526000610a646020830184610a25565b9392505050565b8281526040602082015260006105b76040830184610a25565b600060208284031215610a9657600080fd5b81516001600160e01b031981168114610a6457600080fd5b634e487b7160e01b600052603260045260246000fd5b6001600160a01b03871681526020810186905260a0604082018190528101849052838560c0830137600060c085830181019190915292151560608201529015156080820152601f909201601f1916909101019392505050565b600060208284031215610b2f57600080fd5b8151610a648161076e56fea2646970667358221220ac245daa282eaf48233bfd53b956a8056c4f0db72e2ad7265683a46b7e17f50764736f6c63430008180033") | |
) | |
// VerifyERC6492Signature verifies a signature using ERC6492 offchain signature verification. | |
// WARNING: rpcClient should point to an INTERNAL trusted node. | |
// **Do not use the responses from a third party node to gate authentication into your backend.** | |
func VerifyERC6492Signature(ctx context.Context, rpcClient *rpc.Client, signer common.Address, hash common.Hash, signature []byte) error { | |
encodedData, err := universalSigValidatorConstructorArgs.Pack(signer, hash, signature) | |
if err != nil { | |
return err | |
} | |
packedData := append(validateSigOffchainBytecode, encodedData...) | |
var result hexutil.Bytes | |
err = rpcClient.CallContext(ctx, &result, "eth_call", map[string]interface{}{"data": hexutil.Bytes(packedData)}, "latest") | |
if err != nil { | |
return err | |
} | |
if bytes.Equal(result, []byte{0x01}) { | |
return nil | |
} | |
return errors.New("signature verification failed") | |
} | |
func main() { | |
rpcURL := "https://mainnet.base.org" | |
rpcClient, err := rpc.Dial(rpcURL) | |
if err != nil { | |
panic(err) | |
} | |
address := "0x1f1199a7de04178791be0d07f4e4f936efba5e9e" | |
hash := "0x83c8658674c43f0aa4a2f9e9fceb9f6e66d29fdd01bef435e4f7cf43ba5eb768" | |
// The signature should be wrapped according to ERC-6492 | |
// https://eips.ethereum.org/EIPS/eip-6492#signer-side | |
// For this example, we are using a basic 1271 signature from an already-deployed account | |
signature := "0x0000000000000000000000000000000000000000000000000000000000000020000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000400000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000002000000000000000000000000000000000000000000000000000000000000000c00000000000000000000000000000000000000000000000000000000000000120000000000000000000000000000000000000000000000000000000000000001700000000000000000000000000000000000000000000000000000000000000016b64f9fffa7ac675dcc03d745293a1e366934520c17ce3e6f7577c8b7aec08ea0061f011b6d97ac15601576ba500327cad499641acddeef2c0432c62859dc4c30000000000000000000000000000000000000000000000000000000000000025f198086b2db17256731bc456673b96bcef23f51d1fbacdd7c4379ef65465572f0100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008a7b2274797065223a22776562617574686e2e676574222c226368616c6c656e6765223a226738686c686e54455077716b6f766e705f4f7566626d62536e393042767651313550665051377065743267222c226f726967696e223a2268747470733a2f2f6b6579732e636f696e626173652e636f6d222c2263726f73734f726967696e223a66616c73657d00000000000000000000000000000000000000000000" | |
err = VerifyERC6492Signature( | |
context.Background(), | |
rpcClient, | |
common.HexToAddress(address), | |
common.HexToHash(hash), | |
hexutil.MustDecode(signature), | |
) | |
if err != nil { | |
panic(err) | |
} | |
fmt.Println("Signature verified successfully!") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not 100% tested but should be pretty close