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

typescript
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

typescript
function parseDidKey(didKey: string): KeyData
function formatDidKey(key: KeyData): string

Round-trip a public key through the did:key: encoding (multibase base58btc + multicodec prefix).

typescript
import { parseDidKey, formatDidKey } from "@atiproto/atproto-attestation";
const parsed = parseDidKey("did:key:zDnaeUKTWUXc1HDpGfKbEK4VRpw8FJVDExddoNqfJW3kJyqzn");
// parsed.type === "p256"
// parsed.bytes is the compressed public key
const did = formatDidKey(parsed);
// did === "did:key:zDnaeUKTWUXc1HDpGfKbEK4VRpw8FJVDExddoNqfJW3kJyqzn"

parsePrivateMultibase / formatPrivateMultibase

typescript
function parsePrivateMultibase(mb: string): KeyData
function 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.

typescript
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 typePublic prefixPrivate prefixJWS alg
p2560x80240x8626ES256
p3840x12000x1301ES384
k2560xe7010x8126ES256K
ed255190xed010x8026EdDSA

P-384 cannot derive its public key from the private scalar alone in this package; supply both halves explicitly to the Attestation constructor.