Skip to main content

Try it Live

Run Signature examples in the interactive playground

BrandedSignature

Branded Uint8Array type for cryptographic signatures with algorithm metadata.

Type Definition

Properties

brand

Type brand for runtime type checking using symbol branding. Visibility: Non-enumerable Writable: false Configurable: false

algorithm

Signature algorithm identifier. Values:
  • "secp256k1" - Bitcoin/Ethereum ECDSA
  • "p256" - NIST P-256 ECDSA
  • "ed25519" - EdDSA on Curve25519
Visibility: Enumerable Writable: false Configurable: false

v

Recovery ID for secp256k1 signatures (optional). Values:
  • 27 - First recovery attempt (standard Ethereum)
  • 28 - Second recovery attempt
  • undefined - Not secp256k1 or no recovery ID
Visibility: Enumerable only if defined Writable: false Configurable: false

Byte Structure

ECDSA (secp256k1, p256)

Ed25519

Metadata Storage

Properties are defined using Object.defineProperties():

Type Guards

is

Runtime type guard.
Checks:
  • Is Uint8Array
  • Has [brand] === "Signature"
  • Has algorithm property

Algorithm-Specific Checks

Examples

Creating Branded Signatures

Accessing Metadata

Preserving Metadata

Type Safety

Comparison with Plain Uint8Array

BrandedSignature

Plain Uint8Array

Serialization

JSON

Binary

Performance

Memory Overhead

BrandedSignature has minimal overhead:
  • Base: 64 bytes (signature data)
  • Metadata: 3 property descriptors (~48 bytes)
  • Total: ~112 bytes

Runtime Cost

  • Property access: O(1) (native object properties)
  • Type checking: O(1) (simple property checks)
  • Creation: Minimal overhead vs plain Uint8Array

Optimization

Immutability

All properties are readonly:

Compatibility

Uint8Array Methods

BrandedSignature supports all Uint8Array methods:

Type Narrowing

Design Rationale

Why Branded Types?

  1. Type Safety: Compile-time guarantees about signature format
  2. Self-Describing: Algorithm embedded in data
  3. API Simplicity: No need to pass algorithm separately
  4. Runtime Validation: Type guards enable safe operations

Why Not Classes?

Branded types maintain Uint8Array compatibility while adding type safety.

See Also