viem-goviem-go

recoverPublicKey

Recovers the public key from a hash and signature

recoverPublicKey

Recovers the public key from a hash and signature. Returns the uncompressed public key (65 bytes with 04 prefix).

Import

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

Usage

import "github.com/ChefBingbong/viem-go/utils/signature"
// Recover public key from hash and signature
pubKey, err := signature.RecoverPublicKey(
"0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68",
"0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c",
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("Public Key: %s
", pubKey)
// Public Key: 0x04bfcab88f42f8d0a...
// Get public key as bytes
pubKeyBytes, _ := signature.RecoverPublicKeyBytes(
hash,
sig,
)
// []byte{0x04, 0xbf, 0xca, ...}

Returns

  • Type: (string, error) for RecoverPublicKey, ([]byte, error) for RecoverPublicKeyBytes

The recovered public key. RecoverPublicKey returns a hex string with 0x prefix (65 bytes uncompressed format with 04 prefix), RecoverPublicKeyBytes returns raw bytes.

Parameters

hash (required)

  • Type: string

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

pubKey, _ := signature.RecoverPublicKey(
"0xd9eba16ed0ecae432b71fe008c98cc872bb4cc214d3220a36f365326cf807d68",
sig,
)

signature (required)

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

The signature that was generated by signing the hash.

pubKey, _ := signature.RecoverPublicKey(hash, "0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c")

Functions

RecoverPublicKey

Recovers the public key and returns it as a hex string:

pubKey, _ := signature.RecoverPublicKey(hash, sig)
// "0x04bfcab88f42f8d0a..." (65 bytes)

RecoverPublicKeyBytes

Recovers the public key and returns it as raw bytes:

pubKeyBytes, _ := signature.RecoverPublicKeyBytes(hash, sig)
// []byte{0x04, 0xbf, 0xca, ...} (65 bytes)

Notes

  • Returns the uncompressed public key format (65 bytes with 04 prefix)
  • The public key can be used to derive the Ethereum address using PublicKeyToAddress
  • This is a lower-level function; for address recovery, use RecoverAddress or RecoverMessageAddress