Skip to main content

SEA.sign()

Create a digital signature for data using ECDSA (Elliptic Curve Digital Signature Algorithm). Signatures prove data authenticity and integrity.

Syntax

Parameters

  • data (any): Data to sign (string, object, array, number)
  • pair (object): Key pair with pub and priv properties
  • callback (function, optional): Called with signed result
  • options (object, optional):
    • encode: Encoding for signature (default: ‘base64’)
    • raw: Return raw object instead of SEA-prefixed string (default: false)
    • check: Skip signing if already signed (internal use)

Returns

A Promise that resolves to a signed message:
Or if raw: true, returns an object:

Basic Usage

Sign with Key Pair

Sign String Data

With Callback

How It Works

Reference: ~/workspace/source/sea/sign.js:9-42
  1. Parse Data: Converts data to JSON if needed
  2. Hash: Computes SHA-256 hash of the data
  3. Sign: Uses ECDSA with P-256 curve to sign the hash
  4. Format: Returns message + signature in structured format

Signing Process

Signed Message Structure

Default Format

Fields

  • m: The message/data being signed (any JSON-serializable value)
  • s: ECDSA signature (base64-encoded by default)

Signature Format

ECDSA signatures consist of two values (r, s):
  • Algorithm: ECDSA with P-256 curve
  • Hash: SHA-256
  • Encoding: Base64 (default) or custom
  • Size: ~64 bytes (512 bits) for P-256

Use Cases

Sign User Data

Prove Message Authorship

Timestamped Signatures

Sign Transactions

Options

Raw Output

Return object instead of SEA-prefixed string:

Custom Encoding

Skip Re-signing

Internal optimization - if data is already signed, return it:

Security Considerations

Private Key Protection

Never share your private key! Only use pair.priv for signing. Anyone with access to priv can create signatures as you.

Message Integrity

Signing provides:
  • Authenticity: Proves who created the signature
  • Integrity: Detects any modification to the data
  • Non-repudiation: Signer cannot deny signing

Signature Verification

Always verify signatures before trusting data:

ECDSA Details

Algorithm Parameters

  • Curve: P-256 (secp256r1, prime256v1)
  • Key Size: 256 bits
  • Hash Function: SHA-256
  • Signature Size: ~64 bytes
  • Security Level: 128-bit
Reference: ~/workspace/source/sea/sign.js:29-30

Deterministic vs Random

SEA uses the Web Crypto API’s ECDSA implementation, which typically uses random nonces (RFC 6979 deterministic signatures may vary by implementation).

Error Handling

Missing Private Key

Undefined Data

With Callbacks

Performance

  • Speed: ~1-5ms per signature (varies by device)
  • Async: Always asynchronous (uses Web Crypto API)
  • Batch: Can sign multiple messages in parallel

Common Patterns

Sign and Store

Multi-signature

Signature Chain

Integration with GUN

Automatic Signing

GUN automatically signs data when a user is authenticated:

Manual Signing for Public Data

Verification

See SEA.verify() for verifying signatures.