Skip to main content

Overview

The @atproto/crypto package provides cryptographic primitives for the AT Protocol, including key generation, signing, verification, and encoding utilities. It supports both P-256 (secp256r1) and K-256 (secp256k1) elliptic curve cryptography.

Installation

Supported Cryptographic Systems

The package implements two elliptic curve systems:
  • P-256 (ES256): NIST P-256, aka secp256r1, aka prime256v1
  • K-256 (ES256K): NIST K-256, aka secp256k1
Both use SHA-256 hashing and produce “low-S” signatures as specified in the AT Protocol cryptography specification.

Keypair Classes

P256Keypair

P-256 elliptic curve keypair for ES256 signatures.
Promise<P256Keypair>
Creates a new random P-256 keypair.
boolean
default:"false"
Whether the private key can be exported. Set to true if you need to call export().
Promise<P256Keypair>
Imports a keypair from an existing private key.
Uint8Array | string
required
Private key bytes or hex string
boolean
default:"false"
Whether the imported key can be re-exported
Instance Methods:
string
Returns the public key formatted as a did:key identifier.
Promise<Uint8Array>
Signs a message. The message is hashed with SHA-256 before signing. Returns a 64-byte compact signature (not DER-encoded).
Uint8Array
required
Message bytes to sign
Uint8Array
Returns the raw public key bytes (compressed format).
string
Returns the public key as an encoded string.
SupportedEncodings
default:"base64pad"
Encoding format: ‘base64pad’, ‘base64’, ‘hex’, etc.
Promise<Uint8Array>
Exports the private key bytes. Throws if the keypair was not created with exportable: true.
Properties:
  • jwtAlg: string - Always 'ES256' for P-256 keypairs

Secp256k1Keypair

K-256 elliptic curve keypair for ES256K signatures.
The API is identical to P256Keypair, but uses K-256 cryptography.
Promise<Secp256k1Keypair>
Creates a new random K-256 keypair.
Promise<Secp256k1Keypair>
Imports a keypair from an existing private key.
Properties:
  • jwtAlg: string - Always 'ES256K' for K-256 keypairs

Signature Verification

verifySignature

Verifies a signature using a public key in did:key format.
Promise<boolean>
Verifies a signature against a public key.
string
required
Public key in did:key format
Uint8Array
required
Message bytes that were signed
Uint8Array
required
Signature bytes to verify
string
Expected JWT algorithm (‘ES256’ or ‘ES256K’). If provided, verification fails if the key type doesn’t match.
boolean
default:"false"
Whether to allow non-canonical (high-S) signatures

verifySignatureUtf8

Convenience function for verifying UTF-8 string signatures.
Promise<boolean>
Verifies a signature with UTF-8 string message and base64url-encoded signature.

DID Key Utilities

formatDidKey

Formats a public key as a did:key identifier.
string
string
required
JWT algorithm: ‘ES256’ or ‘ES256K’
Uint8Array
required
Uncompressed public key bytes

parseDidKey

Parses a did:key identifier to extract key type and bytes.
ParsedMultikey
Extracts the algorithm and key bytes from a did:key identifier.

Multibase Utilities

multibaseToBytes

Decodes a multibase-encoded string to bytes.
Uint8Array
Decodes a multibase string (base58btc with ‘z’ prefix) to bytes.

bytesToMultibase

Encodes bytes as a multibase string.
string
Encodes bytes as multibase (base58btc with ‘z’ prefix).

Hashing Utilities

sha256

Computes SHA-256 hash.
Promise<Uint8Array>
Computes SHA-256 hash of the input data.

sha256Hex

Computes SHA-256 hash as hex string.

Random Utilities

randomBytes

Generates cryptographically secure random bytes.
Uint8Array
Generates secure random bytes of the specified length.

Constants


TypeScript Interfaces

Keypair

ExportableKeypair

VerifyOptions


Complete Example

Additional Resources