did:key utilities
@atiproto/atproto-attestation
Parse and format did:key: identifiers and the multibase-encoded private-key strings the Attestation constructor accepts. Supports p256, p384, k256, and ed25519.
KeyData
interface KeyData {type: "p256" | "p384" | "k256" | "ed25519";bytes: Uint8Array; // raw key bytes (compressed point for EC; scalar for ed25519)}
The common shape consumed by every API in this package. Public keys for EC curves are stored in SEC1 compressed form (33 bytes for p256/k256, 49 bytes for p384). Ed25519 keys are 32 bytes. Private keys are the raw scalar (32 bytes for p256/k256/ed25519, 48 bytes for p384).
parseDidKey / formatDidKey
function parseDidKey(didKey: string): KeyDatafunction formatDidKey(key: KeyData): string
Round-trip a public key through the did:key: encoding (multibase base58btc + multicodec prefix).
import { parseDidKey, formatDidKey } from "@atiproto/atproto-attestation";const parsed = parseDidKey("did:key:zDnaeUKTWUXc1HDpGfKbEK4VRpw8FJVDExddoNqfJW3kJyqzn");// parsed.type === "p256"// parsed.bytes is the compressed public keyconst did = formatDidKey(parsed);// did === "did:key:zDnaeUKTWUXc1HDpGfKbEK4VRpw8FJVDExddoNqfJW3kJyqzn"
parsePrivateMultibase / formatPrivateMultibase
function parsePrivateMultibase(mb: string): KeyDatafunction formatPrivateMultibase(key: KeyData): string
Same encoding scheme but with private-key multicodec prefixes. The Attestation constructor accepts either parsePrivateMultibase output (raw KeyData) or the multibase string itself.
import {formatPrivateMultibase,parsePrivateMultibase,Attestation,} from "@atiproto/atproto-attestation";// One-time setup: generate, encode, persist somewhere safe.const priv = generateP256Key(); // { type: "p256", bytes: <32 bytes> }const stored = formatPrivateMultibase(priv); // "z3u2en7t5LRb4G…"// Later, in a worker / service:const att = new Attestation({ privateKey: stored });// equivalent to:const att2 = new Attestation({ privateKey: parsePrivateMultibase(stored) });
Multicodec table
| Key type | Public prefix | Private prefix | JWS alg |
|---|---|---|---|
| p256 | 0x8024 | 0x8626 | ES256 |
| p384 | 0x1200 | 0x1301 | ES384 |
| k256 | 0xe701 | 0x8126 | ES256K |
| ed25519 | 0xed01 | 0x8026 | EdDSA |
P-384 cannot derive its public key from the private scalar alone in this package; supply both halves explicitly to the Attestation constructor.