OverviewFilesCommitsIssuesDocsReleasesPeers

atproto-room / test / label-signer.test.mjs

48 lines · javascript ref:
// LabelSigner — pure materialize (intent -> signed com.atproto.label, verifies against the labeler
// key) + the emit DIFF (only new/changed intents are re-signed). Node. Run: node test/label-signer.test.mjs
import { keyFromPrivate } from 'atproto-identity/lib/atproto-identity.js'
import { verifyLabel } from 'atproto-identity/lib/labels.js'
import { materializeLabel, LabelSigner } from '../lib/label-signer.js'

const assert = (c, m) => { if (!c) { console.error('FAIL:', m); process.exit(1) } console.log('  ok -', m) }

const labeler = keyFromPrivate(new Uint8Array(32).fill(21)) // { privateKey, publicKey, didKey }
const SRC = 'did:plc:communitylabeler'
const U1 = 'at://did:plc:a/app.bsky.feed.post/1'
const U2 = 'at://did:plc:a/app.bsky.feed.post/2'

// --- pure materialize ---
const l = materializeLabel({ uri: U1, val: 'spam', neg: false, cts: '2026-07-01T00:00:00.000Z' }, { src: SRC, privateKey: labeler.privateKey })
assert(l.src === SRC && l.uri === U1 && l.val === 'spam' && l.sig.length === 64, 'materializeLabel -> signed label under the labeler DID')
assert(verifyLabel(l, labeler.didKey) === true, 'materialized label verifies against the labeler key')

// --- emit diff ---
const s = new LabelSigner({ src: SRC, privateKey: labeler.privateKey })
let out = s.emit([
  { uri: U1, val: 'spam', neg: false, cts: '2026-07-01T00:00:00.000Z' },
  { uri: U2, val: 'nsfw', neg: false, cts: '2026-07-02T00:00:00.000Z' }
])
assert(out.length === 2, 'first emit signs both intents')
assert(out.every((x) => verifyLabel(x, labeler.didKey)), 'all emitted labels verify')

// re-emit unchanged -> nothing
out = s.emit([
  { uri: U1, val: 'spam', neg: false, cts: '2026-07-01T00:00:00.000Z' },
  { uri: U2, val: 'nsfw', neg: false, cts: '2026-07-02T00:00:00.000Z' }
])
assert(out.length === 0, 're-emitting unchanged intents signs nothing (idempotent)')

// a mod negates U1/spam (newer cts, neg:true) -> re-signed once
out = s.emit([
  { uri: U1, val: 'spam', neg: true, cts: '2026-07-05T00:00:00.000Z' },
  { uri: U2, val: 'nsfw', neg: false, cts: '2026-07-02T00:00:00.000Z' }
])
assert(out.length === 1 && out[0].uri === U1 && out[0].neg === true, 'a changed intent (negation) is re-signed exactly once')
assert(verifyLabel(out[0], labeler.didKey), 'the negation label verifies')

// guards
let threw = false; try { new LabelSigner({ src: SRC }) } catch { threw = true }
assert(threw, 'LabelSigner requires src + privateKey')

console.log('\nLABEL-SIGNER OK: intent -> signed label (verifies) + emit-diff (new/changed only)')