viem-goviem-go

parseSignature

Parses a hex formatted signature into a structured Signature

parseSignature

Parses a hex formatted signature into a structured Signature with R, S, V, and YParity components.

Import

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

Usage

import "github.com/ChefBingbong/viem-go/utils/signature"
sig, err := signature.ParseSignature(
"0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c",
)
if err != nil {
log.Fatal(err)
}
fmt.Printf("R: %s
", sig.R)
// R: 0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf
fmt.Printf("S: %s
", sig.S)
// S: 0x4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db8
fmt.Printf("YParity: %d
", sig.YParity)
// YParity: 1
fmt.Printf("V: %s
", sig.V.String())
// V: 28

Returns

  • Type: (*signature.Signature, error)

A structured signature with R, S, V, and YParity fields.

Parameters

signatureHex (required)

  • Type: string

The hex formatted signature string. Must be 65 bytes (130 hex characters with 0x prefix).

sig, _ := signature.ParseSignature(
"0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c",
)

Signature Structure

The parsed signature contains:

  • R: The R component (32 bytes as hex string)
  • S: The S component (32 bytes as hex string)
  • V: The V value (27 or 28 for legacy, optional for EIP-2930/1559)
  • YParity: The parity of the y-coordinate (0 or 1)

Functions

ParseSignature

Parses a hex signature string:

sig, _ := signature.ParseSignature("0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c")

ParseSignatureBytes

Parses a 65-byte signature:

sig, _ := signature.ParseSignatureBytes([]byte{0x6e, 0x10, ...})

Signature Format

The signature format is:

  • First 32 bytes: R component
  • Next 32 bytes: S component
  • Last byte: Recovery byte (0/1 for yParity, or 27/28 for legacy v)