~ knotless Search/
↑↓ move↵ open⇧↵ all results

chadtmiller.com/pds.js

An atproto PDS in JavaScript, for Cloudflare Workers and Node

0
0
main
33 lines · 1.1 KB Raw History
// @pdsjs/core/kem - the account's `#atproto_kem` key.
//
// A P-256 key published in the DID document beside `#atproto`. A space syncer
// derives a per-request shared secret against it, and the repo host proves a
// response with a MAC under that secret. The key never signs.

import { derivePublicKey, generateKeyPair } from './crypto.js';
import { publicKeyToDidKey } from './plc.js';

export const KEM_KEY_ID = 'atproto_kem';

/**
 * A fresh KEM key.
 * @returns {Promise<{privateKey: Uint8Array, didKey: string}>} the 32-byte
 *   P-256 scalar, and its public key as a did:key
 */
export async function generateKemKey() {
  const { privateKey, publicKey } = await generateKeyPair();
  return { privateKey, didKey: publicKeyToDidKey(publicKey, 'p256') };
}

/**
 * The did:key of a stored KEM key.
 * @param {Uint8Array} privateKey - 32-byte P-256 scalar
 * @returns {Promise<string>}
 */
export async function kemDidKey(privateKey) {
  if (privateKey.length !== 32) {
    throw new Error(`A KEM key is 32 bytes, got ${privateKey.length}`);
  }
  const { publicKey } = await derivePublicKey(privateKey);
  return publicKeyToDidKey(publicKey, 'p256');
}