OverviewFilesCommitsIssuesDocsReleasesPeers

atproto-identity / test / attested-anchor.test.js

43 lines · javascript ref:
// The composed device-attested anchor: one root + one device → both the atproto genesis
// (federation authority) and the keet proof (P2P authority), consistent. Plus the
// real-flow correctness point: the root builds the identical DID from only the device's
// PUBLIC key.

import test from 'brittle'
import b4a from 'b4a'
import { buildAttestedAnchor } from '../lib/attested-anchor.js'
import { buildAttestedIdentity, verifyAttestedGenesis } from '../lib/device-attestation.js'
import { deriveAtprotoKey } from '../lib/atproto-identity.js'
import { verifyAtprotoBinding, rootIdentityPublicKey } from '../lib/device-chain.js'

const ROOT = 'abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon abandon about'
const DEVICE = 'legal winner thank year wave sausage worth useful legal winner thank yellow'

test('buildAttestedAnchor: atproto genesis + keet proof agree on one root and one device', async (t) => {
  const a = await buildAttestedAnchor({ rootMnemonic: ROOT, deviceMnemonic: DEVICE, handle: 'p2p.7rnx.net', pdsUrl: 'https://pds.7rnx.net' })

  // atproto side
  const rootPk = deriveAtprotoKey(ROOT).publicKey
  t.ok(a.did.startsWith('did:plc:'), 'did:plc minted')
  t.is(a.signedOp.verificationMethods.atproto, a.deviceDidKey, 'verificationMethod = device signing key')
  t.is(a.signedOp.rotationKeys[0], a.rootDidKey, 'rotationKeys[0] = root')
  t.ok(verifyAttestedGenesis(a.signedOp, rootPk), 'genesis signed by root')
  t.ok(a.deviceSigningKey && a.deviceSigningKey.privateKey.length === 32, 'device signing key available to the PDS')

  // keet side — the proof resolves to the same root and vouches for this DID
  const keetRoot = await rootIdentityPublicKey(ROOT)
  const res = verifyAtprotoBinding({ proof: a.keetProof, did: a.did })
  t.ok(res && b4a.equals(res.identityPublicKey, keetRoot), 'keet proof resolves to the ROOT identity')
  t.ok(res && b4a.equals(res.devicePublicKey, a.deviceKeetPublicKey), 'keet proof leaf = this device')
})

test('root builds the SAME did:plc from only the device public key (device key never leaves the device)', (t) => {
  const dev = deriveAtprotoKey(DEVICE)
  const full = buildAttestedIdentity({ rootMnemonic: ROOT, deviceMnemonic: DEVICE, handle: 'p2p.7rnx.net', pdsUrl: 'https://pds.7rnx.net' })
  const pubOnly = buildAttestedIdentity({ rootMnemonic: ROOT, deviceDidKey: dev.didKey, handle: 'p2p.7rnx.net', pdsUrl: 'https://pds.7rnx.net' })

  t.is(pubOnly.did, full.did, 'pubkey-only genesis yields the identical permanent DID')
  t.is(pubOnly.deviceSigningKey, null, 'root does NOT hold the device signing key in the pubkey-only flow')
  t.is(pubOnly.deviceDidKey, dev.didKey, 'verificationMethod is the device pubkey the root was handed')
})