viem-goviem-go

recoverMessageAddress

Recovers the Ethereum address that signed a message

recoverMessageAddress

Recovers the Ethereum address that signed a message from the message and signature.

Import

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

Usage

import "github.com/ChefBingbong/viem-go/utils/signature"
address, err := signature.RecoverMessageAddress(
signature.NewSignableMessage("hello world"),
"0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c",
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Signer: %s
", address)
// Signer: 0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266

Returns

  • Type: (string, error)

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

Parameters

message (required)

  • Type: signature.SignableMessage

The message that was signed. Use signature.NewSignableMessage() for string messages or signature.NewSignableMessageHex() for hex data.

// Recover from string message
address, _ := signature.RecoverMessageAddress(
signature.NewSignableMessage("hello world"),
sig,
)
// Recover from hex data
address, _ = signature.RecoverMessageAddress(
signature.NewSignableMessageHex("0x68656c6c6f20776f726c64"),
sig,
)

signature (required)

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

The signature that was generated by signing the message.

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

Implementation

This function:

  1. Hashes the message using HashMessage (EIP-191 format)
  2. Recovers the public key from the hash and signature
  3. Derives the Ethereum address from the public key
  4. Returns the address as a hex string