Skip to content

Signature

Utility functions for working with ECDSA signatures.

Examples

Below are some examples demonstrating common usages of the Signature module:

Serializing a Signature

Signatures can be serialized to Hex or Bytes using Signature.toHex:

import { Signature } from 'ox'
 
const signature = Signature.toHex({
  r: 49782753348462494199823712700004552394425719014458918871452329774910450607807n,
  s: 33726695977844476214676913201140481102225469284307016937915595756355928419768n,
  yParity: 1
})
'0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c'

Deserializing a Signature

Signatures can be deserialized from Hex or Bytes using Signature.fromHex:

import { Signature } from 'ox'
 
Signature.fromHex('0x6e100a352ec6ad1b70802290e18aeed190704973570f3b8ed42cb9808e2ea6bf4a90a229a244495b41890987806fcbd2d5d23fc0dbe5f5256c2613c039d76db81c')
{ r: 5231...n, s: 3522...n, yParity: 0 }

Functions

NameDescription
Signature.assertAsserts that a Signature is valid.
Signature.extractExtracts a Signature.Signature from an arbitrary object that may include signature properties.
Signature.fromInstantiates a typed Signature.Signature object from a Signature.Signature, Signature.Legacy, Bytes.Bytes, or Hex.Hex.
Signature.fromBytesDeserializes a Bytes.Bytes signature into a structured Signature.Signature.
Signature.fromDerBytesConverts a DER-encoded signature to a Signature.Signature.
Signature.fromDerHexConverts a DER-encoded signature to a Signature.Signature.
Signature.fromHexDeserializes a Hex.Hex signature into a structured Signature.Signature.
Signature.fromLegacyConverts a Signature.Legacy into a Signature.Signature.
Signature.fromRpcConverts a Signature.Rpc into a Signature.Signature.
Signature.fromTupleConverts a Signature.Tuple to a Signature.Signature.
Signature.toBytesSerializes a Signature.Signature to Bytes.Bytes.
Signature.toDerBytesConverts a Signature.Signature to DER-encoded format.
Signature.toDerHexConverts a Signature.Signature to DER-encoded format.
Signature.toHexSerializes a Signature.Signature to Hex.Hex.
Signature.toLegacyConverts a Signature.Signature into a Signature.Legacy.
Signature.toRpcConverts a Signature.Signature into a Signature.Rpc.
Signature.toTupleConverts a Signature.Signature to a serialized Signature.Tuple to be used for signatures in Transaction Envelopes, EIP-7702 Authorization Lists, etc.
Signature.validateValidates a Signature. Returns true if the signature is valid, false otherwise.
Signature.vToYParityConverts a ECDSA v value to a yParity value.
Signature.yParityToVConverts a ECDSA v value to a yParity value.

Errors

NameDescription
Signature.InvalidRErrorThrown when the signature has an invalid r value.
Signature.InvalidSerializedSizeErrorThrown when the serialized signature is of an invalid size.
Signature.InvalidSErrorThrown when the signature has an invalid s value.
Signature.InvalidVErrorThrown when the signature has an invalid v value.
Signature.InvalidYParityErrorThrown when the signature has an invalid yParity value.
Signature.MissingPropertiesErrorThrown when the signature is missing either an r, s, or yParity property.

Types

NameDescription
Signature.Legacy(Legacy) ECDSA signature.
Signature.LegacyRpcRPC-formatted (Legacy) ECDSA signature.
Signature.RpcRPC-formatted ECDSA signature.
Signature.SignatureRoot type for an ECDSA signature.
Signature.Tuple