Something went wrong. Try again.
A Social Deduction / Deception Game
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102// Badge.blue-style INLINE attestation (https://badge.blue/ — CID-First Attestation Spec).//// The signing payload is a CID computed over the record itself, so it is stable regardless of how// many signatures are attached, and the repository DID is folded in so the same record in a// different repo yields a different CID — a signature can't be replayed into another repo.//// Procedure (inline form):// 1. take the record, drop any existing `signatures` array;// 2. build the `$sig` metadata { $type, key, issuer, issuedAt, repository: <repoDid> };// 3. insert it into the record as the `$sig` field;// 4. DAG-CBOR encode → SHA-256 → CIDv1(dag-cbor) == the signing payload;// 5. ECDSA-sign the CID bytes (secp256k1, low-S — @atproto/crypto normalizes);// 6. persist an entry in `signatures`: { key, issuer, issuedAt, cid, signature }.// `repository` is NOT persisted in the entry — a verifier re-derives it from the repo the record// actually lives in, which is what binds the signature to that repo. The `$sig` metadata `$type`// is a fixed protocol constant (SIG_TYPE) rather than a per-entry field, so the stored entry stays// a plain object (no nested `$type` to trip PDS record validation); we have a single signature kind.
import * as cbor from '@ipld/dag-cbor'import { CID } from 'multiformats/cid'import { sha256 } from 'multiformats/hashes/sha2'import { Secp256k1Keypair, verifySignature } from '@atproto/crypto'
export const SIG_TYPE = 'town.deception.signature'
/** A persisted inline signature entry (one element of a record's `signatures` array). */export interface SignatureEntry { /** did:key of the signing key — verifiers resolve the public key from this */ key: string /** identity that vouches for the signature (here, the game server's signing did:key) */ issuer: string issuedAt: string /** the signing-payload CID (string form), recomputable by a verifier */ cid: string /** the ECDSA signature over the CID bytes, atproto `bytes` representation */ signature: { $bytes: string }}
interface SigMeta { $type: string key: string issuer: string issuedAt: string repository: string}
/** Compute the signing-payload CID for a record bound to `repository` (steps 1–4). */async function payloadCid(record: Record<string, unknown>, meta: SigMeta): Promise<CID> { const clean: Record<string, unknown> = { ...record } delete clean.signatures delete clean.$sig const payload = { ...clean, $sig: meta } const bytes = cbor.encode(payload) const hash = await sha256.digest(bytes) return CID.createV1(cbor.code, hash)}
/** * Sign `record` for the repo it will live in, returning the inline `signatures` entry to embed. * `record` should be the final record MINUS its `signatures` array (which this produces). */export async function inlineSign( record: Record<string, unknown>, repository: string, signer: Secp256k1Keypair, issuedAt: string,): Promise<SignatureEntry> { const key = signer.did() const meta: SigMeta = { $type: SIG_TYPE, key, issuer: key, issuedAt, repository } const cid = await payloadCid(record, meta) const sig = await signer.sign(cid.bytes) return { key, issuer: key, issuedAt, cid: cid.toString(), signature: { $bytes: Buffer.from(sig).toString('base64') }, }}
/** * Verify an inline signature entry against the record and the repo DID it lives in. Recomputes the * CID (re-injecting `repository`), checks it matches the entry, then verifies the ECDSA signature. */export async function verifyInline( record: Record<string, unknown>, repository: string, entry: SignatureEntry,): Promise<boolean> { const meta: SigMeta = { $type: SIG_TYPE, key: entry.key, issuer: entry.issuer, issuedAt: entry.issuedAt, repository, } const cid = await payloadCid(record, meta) if (cid.toString() !== entry.cid) return false const sig = Buffer.from(entry.signature.$bytes, 'base64') return verifySignature(entry.key, cid.bytes, new Uint8Array(sig))}