~ knotless Search/
↑↓ move↵ open⇧↵ all results
0
0
30 lines · 1.4 KB Raw History
// Each account's KEM key (spec/SPACES.md, "The KEM key"), which the server
// publishes and the `latest` reference does not: what these runs hold to the
// spec, and take out before comparing the sides (spec/PDS.md).

import { ECDH } from 'node:crypto'
import { base58btc } from 'multiformats/bases/base58'

/// Whether `s` is a P-256 did:key of a point on the curve, compressed.
export function isP256DidKey(s) {
  if (typeof s !== 'string' || !s.startsWith('did:key:z')) return false
  try {
    const b = Buffer.from(base58btc.decode(s.slice('did:key:'.length)))
    if (b.length !== 35 || b[0] !== 0x80 || b[1] !== 0x24 || (b[2] !== 2 && b[2] !== 3)) return false
    ECDH.convertKey(b.subarray(2), 'prime256v1', undefined, undefined, 'uncompressed')
    return true
  } catch {
    return false
  }
}

/// The `@context` entry the PLC directory adds for a P-256 verification method.
const P256_CONTEXT = 'https://w3id.org/security/suites/ecdsa-2019/v1'

/// A DID document without its `#atproto_kem` verification method, and
/// without the `@context` entry the directory adds for it.
export function withoutKem(doc) {
  const out = { ...doc, verificationMethod: doc.verificationMethod.filter((m) => !(typeof m?.id === 'string' && m.id.endsWith('#atproto_kem'))) }
  if (Array.isArray(doc['@context'])) out['@context'] = doc['@context'].filter((c) => c !== P256_CONTEXT)
  return out
}