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
Keypair Classes
P256Keypair
P-256 elliptic curve keypair for ES256 signatures.Promise<P256Keypair>
boolean
default:"false"
Whether the private key can be exported. Set to
true if you need to call export().Promise<P256Keypair>
string
Promise<Uint8Array>
Uint8Array
required
Message bytes to sign
Uint8Array
string
SupportedEncodings
default:"base64pad"
Encoding format: ‘base64pad’, ‘base64’, ‘hex’, etc.
Promise<Uint8Array>
exportable: true.jwtAlg: string- Always'ES256'for P-256 keypairs
Secp256k1Keypair
K-256 elliptic curve keypair for ES256K signatures.Promise<Secp256k1Keypair>
Promise<Secp256k1Keypair>
jwtAlg: string- Always'ES256K'for K-256 keypairs
Signature Verification
verifySignature
Verifies a signature using a public key in did:key format.Promise<boolean>
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>
DID Key Utilities
formatDidKey
Formats a public key as a did:key identifier.string
parseDidKey
Parses a did:key identifier to extract key type and bytes.ParsedMultikey
Multibase Utilities
multibaseToBytes
Decodes a multibase-encoded string to bytes.Uint8Array
bytesToMultibase
Encodes bytes as a multibase string.string
Hashing Utilities
sha256
Computes SHA-256 hash.Promise<Uint8Array>
sha256Hex
Computes SHA-256 hash as hex string.Random Utilities
randomBytes
Generates cryptographically secure random bytes.Uint8Array
Constants
TypeScript Interfaces
Keypair
ExportableKeypair
VerifyOptions
Complete Example
Related Packages
- @atproto/identity - DID and handle resolution
- @atproto/syntax - Identifier validation
- @atproto/common - Common utilities