recoverTypedDataAddress
Recovers the Ethereum address that signed typed data
recoverTypedDataAddress
Recovers the Ethereum address that signed typed data from the typed data and signature.
Import
import "github.com/ChefBingbong/viem-go/utils/signature"import "math/big"Usage
import "github.com/ChefBingbong/viem-go/utils/signature"import "math/big"
typedData := signature.TypedDataDefinition{ Domain: signature.TypedDataDomain{ Name: "My App", Version: "1", ChainId: big.NewInt(1), }, Types: map[string][]signature.TypedDataField{ "Message": { {Name: "content", Type: "string"}, }, }, PrimaryType: "Message", Message: map[string]any{ "content": "Hello!", },}
address, err := signature.RecoverTypedDataAddress( typedData, "0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c",)if err != nil { log.Fatal(err)}fmt.Printf("Signer: %s", address)Returns
- Type:
(string, error)
The Ethereum address that signed the typed data, as a hex string with 0x prefix.
Parameters
typedData (required)
- Type:
signature.TypedDataDefinition
The typed data definition that was signed. Must match exactly what was signed.
typedData := signature.TypedDataDefinition{ Domain: signature.TypedDataDomain{ Name: "My App", Version: "1", ChainId: big.NewInt(1), }, Types: map[string][]signature.TypedDataField{ "Message": { {Name: "content", Type: "string"}, }, }, PrimaryType: "Message", Message: map[string]any{ "content": "Hello!", },}
address, _ := signature.RecoverTypedDataAddress(typedData, sig)signature (required)
- Type:
any(supportsstring,[]byte, or*signature.Signature)
The signature that was generated by signing the typed data.
address, _ := signature.RecoverTypedDataAddress( typedData, "0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c",)Implementation
This function:
- Hashes the typed data using
HashTypedData(EIP-712 format) - Recovers the public key from the hash and signature
- Derives the Ethereum address from the public key
- Returns the address as a hex string