Skip to main content

Try it Live

Run Signature examples in the interactive playground

Constructors

Methods for creating BrandedSignature instances from various input formats.

from

Universal constructor accepting multiple input types.

Signature

Parameters

Uint8Array (64 bytes):
  • Compact ECDSA format (r + s)
  • Defaults to secp256k1 algorithm
Object with r, s:
  • r - r component (32 bytes)
  • s - s component (32 bytes)
  • v? - Optional recovery ID (27 or 28)
  • algorithm? - Optional algorithm (defaults to ‘secp256k1’)
Object with signature:
  • signature - Ed25519 signature (64 bytes)
  • algorithm - Must be ‘ed25519’
BrandedSignature:
  • Returns input as-is (identity function)

Returns

BrandedSignature with appropriate algorithm metadata.

Throws

  • InvalidSignatureFormatError - Unsupported format or invalid length
  • InvalidSignatureLengthError - Invalid component lengths

Examples

fromSecp256k1

Create secp256k1 ECDSA signature from components.

Signature

Parameters

  • r - r component (32 bytes)
  • s - s component (32 bytes)
  • v? - Optional recovery ID (27 or 28 for Ethereum)

Returns

BrandedSignature with:
  • algorithm: "secp256k1"
  • v: number | undefined
  • Length: 64 bytes (r + s)

Throws

  • InvalidSignatureLengthError - If r or s is not 32 bytes

Examples

Recovery ID Values

Standard Ethereum:
  • 27 - First recovery attempt (y is even)
  • 28 - Second recovery attempt (y is odd)
EIP-155 (chain-specific):
  • v = chainId * 2 + 35 or chainId * 2 + 36
  • Use standard 27/28 for signature primitive
  • Chain ID handled at transaction level

fromP256

Create P-256 (NIST P-256) ECDSA signature.

Signature

Parameters

  • r - r component (32 bytes)
  • s - s component (32 bytes)

Returns

BrandedSignature with:
  • algorithm: "p256"
  • v: undefined (P-256 doesn’t use recovery IDs)
  • Length: 64 bytes (r + s)

Throws

  • InvalidSignatureLengthError - If r or s is not 32 bytes

Examples

Use Cases

  • WebAuthn authentication
  • JWT signatures (ES256)
  • TLS certificates
  • FIDO2 security keys

fromEd25519

Create Ed25519 signature.

Signature

Parameters

  • signature - Ed25519 signature (64 bytes)

Returns

BrandedSignature with:
  • algorithm: "ed25519"
  • v: undefined
  • Length: 64 bytes

Throws

  • InvalidSignatureLengthError - If signature is not 64 bytes

Examples

Use Cases

  • Cryptocurrency transactions (Solana, Cardano, Polkadot)
  • SSH keys (ed25519)
  • Age encryption
  • Modern cryptographic protocols

fromCompact

Create signature from compact encoding (r + s).

Signature

Parameters

  • bytes - Compact signature bytes (64 bytes)
  • algorithm - Signature algorithm

Returns

BrandedSignature with specified algorithm.

Throws

  • InvalidSignatureLengthError - If bytes is not 64 bytes

Examples

Compact Format

fromDER

Create signature from DER-encoded ECDSA signature.

Signature

Parameters

  • der - DER-encoded signature
  • algorithm - ECDSA algorithm (‘secp256k1’ or ‘p256’)
  • v? - Optional recovery ID (secp256k1 only)

Returns

BrandedSignature with specified algorithm.

Throws

  • InvalidDERError - Invalid DER encoding
  • InvalidAlgorithmError - Algorithm not ECDSA

Examples

DER Format

Parsing Details

  • Leading zeros removed from r and s
  • Padding (0x00) added if high bit set (positive number)
  • Result padded to 32 bytes
  • Recovery ID (v) passed separately (not in DER)

Comparison

When to Use Each Constructor

from():
  • Universal, handles any format
  • Good for library APIs
  • Flexible input handling
fromSecp256k1():
  • Explicit secp256k1 creation
  • Ethereum/Bitcoin signatures
  • When you have r, s, v components
fromP256():
  • NIST P-256 signatures
  • WebAuthn, JWT, TLS
  • When algorithm is known
fromEd25519():
  • Ed25519 signatures only
  • Modern crypto protocols
  • Type-safe Ed25519 handling
fromCompact():
  • Parsing compact binary format
  • When algorithm known externally
  • Wire format deserialization
fromDER():
  • Bitcoin transactions
  • X.509 certificates
  • Legacy ECDSA formats

Performance

All constructors are O(1) with minimal overhead:

Error Handling

Type Safety

See Also