viem-goviem-go

recoverAddress

Recovers the Ethereum address from a hash and signature

recoverAddress

Recovers the Ethereum address from a hash and signature. This is a lower-level function that works with raw hashes (not messages).

Import

import "github.com/ChefBingbong/viem-go/utils/signature"

Usage

import "github.com/ChefBingbong/viem-go/utils/signature"
// Recover address from hash and signature
address, err := signature.RecoverAddress(
"0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68",
"0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c",
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Signer: %s
", address)
// Signer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

Returns

  • Type: (string, error)

The Ethereum address that signed the hash, as a hex string with 0x prefix.

Parameters

hash (required)

  • Type: string

The 32-byte hash that was signed. Must be a hex string with 0x prefix.

address, _ := signature.RecoverAddress(
"0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68",
sig,
)

signature (required)

  • Type: any (supports string, []byte, or *signature.Signature)

The signature that was generated by signing the hash.

// Hex string signature
address, _ := signature.RecoverAddress(
hash,
"0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c",
)
// Signature struct
sig, _ := signature.ParseSignature("0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c")
address, _ = signature.RecoverAddress(hash, sig)

Notes

  • This function works with raw hashes, not messages
  • For message recovery, use RecoverMessageAddress which handles EIP-191 hashing
  • For typed data recovery, use RecoverTypedDataAddress which handles EIP-712 hashing
  • The hash must be exactly 32 bytes (64 hex characters)