viem-goviem-go

verifyMessage

Verifies if a signed message was generated by the provided address

verifyMessage

Verifies that a message was signed by the provided address.

:::warning[Warning] This utility can only verify a message that was signed by an Externally Owned Account (EOA). To verify messages from Contract Accounts (& EOA), use the Public Client's VerifyMessage action instead. :::

Import

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

Usage

import "github.com/ChefBingbong/viem-go/utils/signature"
// Verify message signature
valid, err := signature.VerifyMessage(
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
signature.NewSignableMessage("hello world"),
"0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c",
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Valid: %v
", valid)
// Valid: true

Returns

  • Type: (bool, error)

true if the provided address generated the signature, false otherwise.

Parameters

address (required)

  • Type: string

The Ethereum address that signed the original message.

valid, _ := signature.VerifyMessage(
"0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266",
signature.NewSignableMessage("hello world"),
sig,
)

message (required)

  • Type: signature.SignableMessage

The message to be verified. Use signature.NewSignableMessage() for string messages or signature.NewSignableMessageHex() for hex data.

// Verify string message
valid, _ := signature.VerifyMessage(
address,
signature.NewSignableMessage("hello world"),
sig,
)
// Verify hex data
valid, _ = signature.VerifyMessage(
address,
signature.NewSignableMessageHex("0x68656c6c6f20776f726c64"),
sig,
)

signature (required)

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

The signature that was generated by signing the message with the address's private key.

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

Implementation

This function:

  1. Recovers the address from the message and signature using RecoverMessageAddress
  2. Compares the recovered address with the provided address (case-insensitive)
  3. Returns true if they match, false otherwise