commit d525b5530b4045eb5a8825a2156f8ab23f95a3b6
parent 8dab3390cbd7e3d43ec3f04c08d0befbf5074523
Author: triesap <tyson@radroots.org>
Date: Tue, 6 Oct 2026 12:23:45 +0000
availability: Implement owned deletion evidence projection (HCP-035)
- Admit opaque verified requests with canonical targets and advisory diagnostics
- Evaluate same-author event references and inclusive address cutoffs
- Retain withdrawal evidence without older-head resurrection or erasure claims
- Verify nineteen signed tests and actual pinned Rust interoperability
Diffstat:
3 files changed, 661 insertions(+), 0 deletions(-)
diff --git a/web/src/lib/catalog/deletions.ts b/web/src/lib/catalog/deletions.ts
@@ -0,0 +1,123 @@
+import {
+ verifiedEnvelopeSnapshot,
+ type VerifiedEnvelope
+} from '../nostr/verified-envelope.ts';
+import {
+ canonicalDeletionCoordinate,
+ deletionRequestSnapshot,
+ type DeletionRequest
+} from '../nostr/deletion-adapter.ts';
+import { publicHeadEnvelope, type PublicHead } from './heads.ts';
+
+export type DeletionDecision = Readonly<{
+ outcome: 'visible' | 'suppressed';
+ reason:
+ | 'deletion_request_immune'
+ | 'deletion_no_authorized_reference'
+ | 'deletion_request_author_mismatch'
+ | 'deletion_address_cutoff_precedes_target'
+ | 'deletion_event_id_reference'
+ | 'deletion_address_reference'
+ | 'deletion_event_id_and_address_reference';
+ eventReference: Readonly<{ requestId: string }> | null;
+ addressReference: Readonly<{
+ coordinate: string;
+ inclusiveCutoff: number;
+ requestId: string;
+ }> | null;
+}>;
+// Pure reduction over caller-retained admitted proofs. Storage admission and
+// coherent retention/eviction remain working-set duties; no implicit cache here.
+export function evaluateDeletion(
+ target: VerifiedEnvelope,
+ requests: readonly DeletionRequest[]
+): DeletionDecision {
+ const event = verifiedEnvelopeSnapshot(target);
+ if (!event) throw new Error('deletion_target_proof_invalid');
+ if (event.kind === 5)
+ return {
+ outcome: 'visible',
+ reason: 'deletion_request_immune',
+ eventReference: null,
+ addressReference: null
+ };
+ const replaceable =
+ event.kind === 0 ||
+ event.kind === 3 ||
+ (event.kind >= 10000 && event.kind <= 19999);
+ const identifier = replaceable
+ ? ''
+ : event.kind >= 30000 && event.kind <= 39999
+ ? event.tags.find((t) => t[0] === 'd')?.[1]
+ : undefined;
+ // Unlike generic head ordering, missing/malformed FIRST d is no coordinate.
+ const coordinate =
+ identifier === undefined
+ ? undefined
+ : canonicalDeletionCoordinate(
+ `${event.kind}:${event.pubkey}:${identifier}`
+ );
+ let eventReference: DeletionDecision['eventReference'] = null;
+ let addressReference: DeletionDecision['addressReference'] = null;
+ let unauthorized = false;
+ for (const token of requests) {
+ const request = deletionRequestSnapshot(token);
+ if (!request) continue;
+ const exact = request.eventTargets.some((v) => v.eventId === event.id);
+ const address =
+ coordinate !== undefined &&
+ request.addressTargets.some((v) => v.coordinate === coordinate);
+ if (!exact && !address) continue;
+ if (request.pubkey !== event.pubkey) {
+ unauthorized = true;
+ continue;
+ }
+ if (
+ exact &&
+ (eventReference === null || request.id < eventReference.requestId)
+ )
+ eventReference = { requestId: request.id };
+ if (
+ address &&
+ (addressReference === null ||
+ request.created_at > addressReference.inclusiveCutoff ||
+ (request.created_at === addressReference.inclusiveCutoff &&
+ request.id < addressReference.requestId))
+ )
+ addressReference = {
+ coordinate,
+ inclusiveCutoff: request.created_at,
+ requestId: request.id
+ };
+ }
+ const addressApplies =
+ addressReference !== null &&
+ event.created_at <= addressReference.inclusiveCutoff;
+ const reason: DeletionDecision['reason'] =
+ eventReference !== null
+ ? addressApplies
+ ? 'deletion_event_id_and_address_reference'
+ : 'deletion_event_id_reference'
+ : addressApplies
+ ? 'deletion_address_reference'
+ : addressReference !== null
+ ? 'deletion_address_cutoff_precedes_target'
+ : unauthorized
+ ? 'deletion_request_author_mismatch'
+ : 'deletion_no_authorized_reference';
+ return {
+ outcome:
+ eventReference !== null || addressApplies ? 'suppressed' : 'visible',
+ reason,
+ eventReference,
+ addressReference
+ };
+}
+// Evaluate the actual selected winner, never an older focused search match.
+// Suppression retains both the winner and request evidence; it claims no erase.
+export function evaluatePublicHeadDeletion(
+ head: PublicHead,
+ requests: readonly DeletionRequest[]
+): DeletionDecision {
+ return evaluateDeletion(publicHeadEnvelope(head), requests);
+}
diff --git a/web/src/lib/nostr/deletion-adapter.ts b/web/src/lib/nostr/deletion-adapter.ts
@@ -0,0 +1,175 @@
+import { canonicalPublicKey } from '../contracts/public-key.ts';
+import { boundedUtf8 } from '../contracts/food-availability-v1/text.ts';
+import {
+ verifiedEnvelopeSnapshot,
+ type VerifiedEnvelope
+} from './verified-envelope.ts';
+
+declare const deletionBrand: unique symbol;
+export type DeletionRequest = Readonly<{ readonly [deletionBrand]: true }>;
+type Target = Readonly<{ tagIndex: number; rawTag: readonly string[] }>;
+export type DeletionRequestSnapshot = Readonly<{
+ id: string;
+ pubkey: string;
+ created_at: number;
+ eventTargets: readonly (Target & { readonly eventId: string })[];
+ addressTargets: readonly (Target & { readonly coordinate: string })[];
+ kindAdvisories: readonly (Target & { readonly kind: number })[];
+ diagnostics: readonly (Target & { readonly code: string })[];
+ rawTags: readonly (readonly string[])[];
+}>;
+const owners = new WeakMap<
+ DeletionRequest,
+ Readonly<{
+ proof: VerifiedEnvelope;
+ projection: DeletionRequestSnapshot;
+ }>
+>();
+const encoder = new TextEncoder();
+function emptyRows<T>(): T[] {
+ return [];
+}
+// Generic Lib189 Nip01Coordinate, deliberately separate from Food/naddr.
+// Rust u32 text parsing accepts a plus and leading zeroes; identifiers stay raw.
+export function canonicalDeletionCoordinate(value: string): string | undefined {
+ if (!boundedUtf8(value, 4096)) return undefined;
+ const first = value.indexOf(':'),
+ second = value.indexOf(':', first + 1);
+ if (first < 0 || second < 0) return undefined;
+ const kindText = value.slice(0, first);
+ if (!/^\+?[0-9]+$/u.test(kindText)) return undefined;
+ const kind = Number(kindText),
+ identifier = value.slice(second + 1);
+ const replaceable =
+ kind === 0 || kind === 3 || (kind >= 10000 && kind <= 19999);
+ if (
+ (!replaceable && !(kind >= 30000 && kind <= 39999)) ||
+ (replaceable && identifier !== '')
+ )
+ return undefined;
+ const pubkey = canonicalPublicKey(
+ value.slice(first + 1, second).toLowerCase()
+ );
+ return pubkey === undefined ? undefined : `${kind}:${pubkey}:${identifier}`;
+}
+function compareUtf8(a: string, b: string): number {
+ const x = encoder.encode(a),
+ y = encoder.encode(b);
+ for (let i = 0; i < Math.min(x.length, y.length); i++)
+ if (x[i] !== y[i]) return x[i] - y[i];
+ return x.length - y.length;
+}
+// Verification already enforces the selected content/tag/element/aggregate and
+// 256KiB wire budgets. The minimal kind5 projection cannot exceed that raw wire.
+// Admission establishes references only, never author authority or deletion.
+export function admitDeletionRequest(
+ proof: VerifiedEnvelope
+):
+ | Readonly<{ ok: true; value: DeletionRequest }>
+ | Readonly<{ ok: false; code: string }> {
+ const event = verifiedEnvelopeSnapshot(proof);
+ if (!event) return { ok: false, code: 'deletion_proof_invalid' };
+ if (event.kind !== 5) return { ok: false, code: 'unsupported_kind' };
+ let eventTargets = emptyRows<Target & { eventId: string }>();
+ let addressTargets = emptyRows<Target & { coordinate: string }>();
+ for (let tagIndex = 0; tagIndex < event.tags.length; tagIndex++) {
+ const rawTag = event.tags[tagIndex],
+ name = rawTag[0],
+ value = rawTag[1];
+ if (name === 'e') {
+ if (value === undefined)
+ return { ok: false, code: 'deletion_event_target_shape' };
+ if (!/^[0-9a-fA-F]{64}$/u.test(value))
+ return { ok: false, code: 'deletion_event_target_invalid' };
+ const eventId = value.toLowerCase();
+ if (!eventTargets.some((v) => v.eventId === eventId))
+ eventTargets = eventTargets.concat({ tagIndex, rawTag, eventId });
+ } else if (name === 'a') {
+ if (value === undefined)
+ return { ok: false, code: 'deletion_address_target_shape' };
+ const coordinate = canonicalDeletionCoordinate(value);
+ if (coordinate === undefined)
+ return { ok: false, code: 'deletion_address_target_invalid' };
+ if (!addressTargets.some((v) => v.coordinate === coordinate))
+ addressTargets = addressTargets.concat({
+ tagIndex,
+ rawTag,
+ coordinate
+ });
+ }
+ }
+ if (eventTargets.length === 0 && addressTargets.length === 0)
+ return { ok: false, code: 'deletion_target_missing' };
+ let kindAdvisories = emptyRows<Target & { kind: number }>();
+ let diagnostics = emptyRows<Target & { code: string }>();
+ for (let tagIndex = 0; tagIndex < event.tags.length; tagIndex++) {
+ const rawTag = event.tags[tagIndex];
+ if (rawTag[0] !== 'k') continue;
+ const value = rawTag[1],
+ kind = Number(value);
+ const suffix =
+ value === undefined
+ ? 'shape'
+ : !/^(?:0|[1-9][0-9]*)$/u.test(value) || kind > 65535
+ ? 'invalid'
+ : kindAdvisories.some((v) => v.kind === kind)
+ ? 'duplicate'
+ : undefined;
+ if (suffix)
+ diagnostics = diagnostics.concat({
+ tagIndex,
+ rawTag,
+ code: `deletion_kind_advisory_${suffix}_ignored`
+ });
+ else kindAdvisories = kindAdvisories.concat({ tagIndex, rawTag, kind });
+ }
+ if (eventTargets.length === 0)
+ for (const advisory of kindAdvisories)
+ if (
+ !addressTargets.some(
+ (v) =>
+ Number(v.coordinate.slice(0, v.coordinate.indexOf(':'))) ===
+ advisory.kind
+ )
+ )
+ diagnostics = diagnostics.concat({
+ tagIndex: advisory.tagIndex,
+ rawTag: advisory.rawTag,
+ code: 'deletion_kind_advisory_conflict_ignored'
+ });
+ eventTargets.sort((a, b) => compareUtf8(a.eventId, b.eventId));
+ addressTargets.sort((a, b) => compareUtf8(a.coordinate, b.coordinate));
+ kindAdvisories.sort((a, b) => a.kind - b.kind);
+ diagnostics.sort((a, b) => a.tagIndex - b.tagIndex);
+ const token = Object.freeze({}) as DeletionRequest;
+ owners.set(token, {
+ proof,
+ projection: {
+ id: event.id,
+ pubkey: event.pubkey,
+ created_at: event.created_at,
+ eventTargets,
+ addressTargets,
+ kindAdvisories,
+ diagnostics,
+ rawTags: event.tags
+ }
+ });
+ return { ok: true, value: token };
+}
+// Detached public snapshots cannot alter the retained tombstone or forge it.
+export function deletionRequestSnapshot(
+ request: DeletionRequest
+): DeletionRequestSnapshot | undefined {
+ const owner = owners.get(request);
+ return owner === undefined
+ ? undefined
+ : (JSON.parse(JSON.stringify(owner.projection)) as DeletionRequestSnapshot);
+}
+// The original bounded signed public wire remains held by its genuine proof.
+// This grants no signing capability, runtime store or remote-erasure authority.
+export function deletionRequestEnvelope(
+ request: DeletionRequest
+): VerifiedEnvelope | undefined {
+ return owners.get(request)?.proof;
+}
diff --git a/web/tests/unit/deletions.test.ts b/web/tests/unit/deletions.test.ts
@@ -0,0 +1,363 @@
+import { afterAll, describe, expect, it } from 'vitest';
+import { finalizeEvent } from 'applesauce-core/helpers';
+import {
+ verifyEnvelope,
+ type VerifiedEnvelope
+} from '../../src/lib/nostr/verified-envelope.ts';
+import {
+ admitDeletionRequest,
+ deletionRequestEnvelope,
+ deletionRequestSnapshot,
+ type DeletionRequest
+} from '../../src/lib/nostr/deletion-adapter.ts';
+import {
+ evaluateDeletion,
+ evaluatePublicHeadDeletion
+} from '../../src/lib/catalog/deletions.ts';
+import {
+ createPublicHeadCandidate,
+ selectPublicHead,
+ publicHeadSnapshot
+} from '../../src/lib/catalog/heads.ts';
+
+const keys: Uint8Array[] = [];
+afterAll(() => {
+ for (const key of keys) key.fill(0);
+});
+function author() {
+ const key = crypto.getRandomValues(new Uint8Array(32));
+ keys.push(key);
+ return (time: number, tags: string[][], kind = 30402, content = '') => {
+ const event = finalizeEvent({ kind, created_at: time, tags, content }, key);
+ const result = verifyEnvelope(JSON.stringify(event));
+ if (!result.ok) throw new Error('Invalid signed test event');
+ return { event, proof: result.value };
+ };
+}
+function admitted(proof: VerifiedEnvelope) {
+ const result = admitDeletionRequest(proof);
+ if (!result.ok) throw new Error(result.code);
+ return result.value;
+}
+function fixture() {
+ const a = author(),
+ b = author(),
+ target = a(100, [['d', 'stock']]);
+ const address = `30402:${target.event.pubkey}:stock`;
+ const request = (time: number, tags = [['a', address]]) =>
+ admitted(a(time, tags, 5).proof);
+ return { a, b, target, address, request };
+}
+describe('verified owned NIP09 deletion evidence', () => {
+ it('retains the genuine original verified envelope for retained tombstone evidence', () => {
+ const f = fixture();
+ const original = f.a(100, [['a', f.address]], 5, 'withdrawal evidence');
+ const request = admitted(original.proof);
+ expect(deletionRequestEnvelope(request)).toBe(original.proof);
+ expect(deletionRequestEnvelope({} as DeletionRequest)).toBeUndefined();
+ });
+ it('retains admitted deletion before the target is evaluated', () => {
+ const f = fixture(),
+ deletion = f.request(100);
+ expect(evaluateDeletion(f.target.proof, [deletion])).toMatchObject({
+ outcome: 'suppressed',
+ reason: 'deletion_address_reference',
+ addressReference: { inclusiveCutoff: 100 }
+ });
+ });
+ it('rejects matching references by the wrong author', () => {
+ const f = fixture();
+ const wrong = admitted(
+ f.b(
+ 101,
+ [
+ ['e', f.target.event.id],
+ ['a', f.address]
+ ],
+ 5
+ ).proof
+ );
+ expect(evaluateDeletion(f.target.proof, [wrong])).toMatchObject({
+ outcome: 'visible',
+ reason: 'deletion_request_author_mismatch',
+ eventReference: null,
+ addressReference: null
+ });
+ });
+ it('uses inclusive address cutoff and allows a genuinely later revision', () => {
+ const f = fixture(),
+ request = f.request(100);
+ expect(evaluateDeletion(f.target.proof, [request]).outcome).toBe(
+ 'suppressed'
+ );
+ expect(
+ evaluateDeletion(f.a(101, [['d', 'stock']]).proof, [request])
+ ).toMatchObject({
+ outcome: 'visible',
+ reason: 'deletion_address_cutoff_precedes_target',
+ addressReference: { inclusiveCutoff: 100 }
+ });
+ expect(
+ evaluateDeletion(f.a(99, [['d', 'stock']]).proof, [request]).outcome
+ ).toBe('suppressed');
+ });
+ it('applies an exact e reference regardless of request timestamp or k mismatch', () => {
+ const f = fixture(),
+ request = f.request(0, [
+ ['e', f.target.event.id],
+ ['k', '1']
+ ]);
+ expect(evaluateDeletion(f.target.proof, [request])).toMatchObject({
+ outcome: 'suppressed',
+ reason: 'deletion_event_id_reference',
+ addressReference: null
+ });
+ });
+ it('cannot resurrect an older head after withdrawal or repeated old arrival', () => {
+ const f = fixture();
+ const old = createPublicHeadCandidate(f.a(99, [['d', 'stock']]).proof)!;
+ const head = createPublicHeadCandidate(f.target.proof)!;
+ const tombstones = [f.request(100)];
+ const selected = selectPublicHead(head, old);
+ expect(selected.decision).toBe('older');
+ expect(publicHeadSnapshot(selected.head).id).toBe(f.target.event.id);
+ expect(evaluatePublicHeadDeletion(selected.head, tombstones).outcome).toBe(
+ 'suppressed'
+ );
+ expect(evaluatePublicHeadDeletion(selected.head, tombstones).outcome).toBe(
+ 'suppressed'
+ );
+ });
+ it('never treats kind5 itself as retractable evidence', () => {
+ const f = fixture(),
+ target = f.a(100, [['e', f.target.event.id]], 5);
+ expect(
+ evaluateDeletion(target.proof, [f.request(101, [['e', target.event.id]])])
+ ).toEqual({
+ outcome: 'visible',
+ reason: 'deletion_request_immune',
+ eventReference: null,
+ addressReference: null
+ });
+ });
+ it('distinguishes unrelated requests from wrong authors', () => {
+ const f = fixture();
+ expect(
+ evaluateDeletion(f.target.proof, [
+ f.request(101, [['e', 'f'.repeat(64)]])
+ ]).reason
+ ).toBe('deletion_no_authorized_reference');
+ });
+ it('chooses lowest exact request ID and greatest cutoff/lower-ID address evidence independent of order and repeats', () => {
+ const f = fixture(),
+ exactA = f.request(0, [['e', f.target.event.id]]),
+ exactB = f.request(200, [['e', f.target.event.id]]);
+ const addressA = f.request(120),
+ addressB = f.request(120, [
+ ['a', f.address],
+ ['x', 'different']
+ ]),
+ stale = f.request(110);
+ const rows = [exactA, addressA, exactB, addressB, stale];
+ const expected = evaluateDeletion(f.target.proof, rows);
+ expect(
+ evaluateDeletion(f.target.proof, [...rows].reverse().concat(rows))
+ ).toEqual(expected);
+ expect(expected).toMatchObject({
+ reason: 'deletion_event_id_and_address_reference',
+ eventReference: {
+ requestId: [exactA, exactB]
+ .map((v) => deletionRequestSnapshot(v)!.id)
+ .sort()[0]
+ },
+ addressReference: {
+ inclusiveCutoff: 120,
+ requestId: [addressA, addressB]
+ .map((v) => deletionRequestSnapshot(v)!.id)
+ .sort()[0]
+ }
+ });
+ });
+ it('retains stale authorized address evidence in preference to wrong-author reason', () => {
+ const f = fixture(),
+ wrong = admitted(f.b(200, [['e', f.target.event.id]], 5).proof);
+ expect(
+ evaluateDeletion(f.target.proof, [wrong, f.request(99)]).reason
+ ).toBe('deletion_address_cutoff_precedes_target');
+ });
+ it('uses first d exactly, with no address for missing or malformed first d', () => {
+ const f = fixture(),
+ empty = f.request(100, [['a', `30402:${f.target.event.pubkey}:`]]);
+ for (const tags of [[], [['d']], [['d'], ['d', 'stock']]])
+ expect(
+ evaluateDeletion(f.a(100, tags).proof, [empty, f.request(100)]).outcome
+ ).toBe('visible');
+ expect(evaluateDeletion(f.a(100, [['d', '']]).proof, [empty]).outcome).toBe(
+ 'suppressed'
+ );
+ expect(
+ evaluateDeletion(
+ f.a(100, [
+ ['d', 'stock'],
+ ['d', 'other']
+ ]).proof,
+ [f.request(100)]
+ ).outcome
+ ).toBe('suppressed');
+ });
+ it('preserves opaque identifier bytes and does not normalize titles, whitespace, Unicode or colon', () => {
+ const f = fixture();
+ for (const id of ['a:b', ' stock ', 'é', 'e\u0301', '\ufeffstock']) {
+ const target = f.a(100, [['d', id]]);
+ const request = f.request(100, [
+ ['a', `30402:${target.event.pubkey}:${id}`]
+ ]);
+ expect(evaluateDeletion(target.proof, [request]).outcome).toBe(
+ 'suppressed'
+ );
+ expect(evaluateDeletion(f.target.proof, [request]).outcome).toBe(
+ 'visible'
+ );
+ }
+ });
+ it('accepts generic replaceable coordinate kinds with empty identifiers', () => {
+ const f = fixture();
+ for (const kind of [0, 3, 10000, 19999]) {
+ const target = f.a(100, [['d', 'ignored']], kind);
+ const request = f.request(100, [
+ ['a', `${kind}:${target.event.pubkey}:`]
+ ]);
+ expect(evaluateDeletion(target.proof, [request]).outcome).toBe(
+ 'suppressed'
+ );
+ }
+ });
+ it('canonicalizes mixed-case e/a and plus/leading-zero coordinate kinds while retaining first raw provenance', () => {
+ const f = fixture();
+ const tags = [
+ ['e', f.target.event.id.toUpperCase(), 'relay'],
+ ['e', f.target.event.id],
+ ['a', `+030402:${f.target.event.pubkey.toUpperCase()}:stock`, 'extra'],
+ ['a', f.address],
+ ['x', 'unknown']
+ ];
+ const request = f.request(100, tags),
+ view = deletionRequestSnapshot(request)!;
+ expect(view.rawTags).toEqual(tags);
+ expect(view.eventTargets).toEqual([
+ { tagIndex: 0, eventId: f.target.event.id, rawTag: tags[0] }
+ ]);
+ expect(view.addressTargets).toEqual([
+ { tagIndex: 2, coordinate: f.address, rawTag: tags[2] }
+ ]);
+ expect(evaluateDeletion(f.target.proof, [request]).reason).toBe(
+ 'deletion_event_id_and_address_reference'
+ );
+ });
+ it('keeps k advisory and returns ordered shape/invalid/duplicate/conflict diagnostics', () => {
+ const f = fixture(),
+ request = f.request(100, [
+ ['k'],
+ ['a', f.address],
+ ['k', '+30402'],
+ ['k', '30402'],
+ ['k', '30402', 'duplicate'],
+ ['k', '31923'],
+ ['k', '65536']
+ ]);
+ const view = deletionRequestSnapshot(request)!;
+ expect(view.kindAdvisories.map((v) => v.kind)).toEqual([30402, 31923]);
+ expect(view.diagnostics.map((v) => [v.tagIndex, v.code])).toEqual([
+ [0, 'deletion_kind_advisory_shape_ignored'],
+ [2, 'deletion_kind_advisory_invalid_ignored'],
+ [4, 'deletion_kind_advisory_duplicate_ignored'],
+ [5, 'deletion_kind_advisory_conflict_ignored'],
+ [6, 'deletion_kind_advisory_invalid_ignored']
+ ]);
+ expect(evaluateDeletion(f.target.proof, [request]).outcome).toBe(
+ 'suppressed'
+ );
+ expect(
+ deletionRequestSnapshot(
+ f.request(100, [
+ ['a', f.address],
+ ['e', f.target.event.id],
+ ['k', '31923']
+ ])
+ )!.diagnostics
+ ).toEqual([]);
+ });
+ it('rejects any malformed e/a in source order rather than partially admitting a request', () => {
+ const f = fixture();
+ for (const [tags, code] of [
+ [[['a', f.address], ['e'], ['a', 'bad']], 'deletion_event_target_shape'],
+ [
+ [
+ ['e', f.target.event.id],
+ ['a', 'bad'],
+ ['e', 'bad']
+ ],
+ 'deletion_address_target_invalid'
+ ],
+ [[['e', 'bad']], 'deletion_event_target_invalid'],
+ [[['a']], 'deletion_address_target_shape'],
+ [[['x', 'unknown'], ['k']], 'deletion_target_missing']
+ ] as [string[][], string][])
+ expect(admitDeletionRequest(f.a(100, tags, 5).proof)).toMatchObject({
+ ok: false,
+ code
+ });
+ });
+ it('rejects unsupported/invalid address kinds, nonempty replaceable identifiers, bad curve keys and whitespace', () => {
+ const f = fixture();
+ for (const value of [
+ `1:${f.target.event.pubkey}:`,
+ `0:${f.target.event.pubkey}:x`,
+ '30402:' + '0'.repeat(64) + ':stock',
+ ` 30402:${f.target.event.pubkey}:stock`,
+ `30402:${f.target.event.pubkey}:stock` +
+ 'x'.repeat(4097 - f.address.length)
+ ]) {
+ const tags = [['a', value]];
+ const wire = JSON.stringify(
+ finalizeEvent({ kind: 5, created_at: 100, tags, content: '' }, keys[0])
+ );
+ const proof = verifyEnvelope(wire);
+ if (!proof.ok) {
+ expect(value.length).toBeGreaterThanOrEqual(4096);
+ continue;
+ }
+ expect(admitDeletionRequest(proof.value).ok).toBe(false);
+ }
+ });
+ it('requires genuine proofs and admitted ownership rather than SDK flags or forged tokens', () => {
+ const f = fixture();
+ expect(admitDeletionRequest({} as VerifiedEnvelope)).toEqual({
+ ok: false,
+ code: 'deletion_proof_invalid'
+ });
+ expect(admitDeletionRequest(f.target.proof)).toEqual({
+ ok: false,
+ code: 'unsupported_kind'
+ });
+ expect(deletionRequestSnapshot({} as DeletionRequest)).toBeUndefined();
+ expect(
+ evaluateDeletion(f.target.proof, [{} as DeletionRequest]).outcome
+ ).toBe('visible');
+ expect(() => evaluateDeletion({} as VerifiedEnvelope, [])).toThrow(
+ 'deletion_target_proof_invalid'
+ );
+ });
+ it('protects retained tombstone metadata and raw tags from caller mutation', () => {
+ const f = fixture(),
+ request = f.request(100),
+ view = deletionRequestSnapshot(request)!;
+ (view.rawTags as string[][])[0][1] = 'bad';
+ (view.addressTargets as unknown as { coordinate: string }[])[0].coordinate =
+ 'bad';
+ expect(deletionRequestSnapshot(request)!.rawTags[0][1]).toBe(f.address);
+ expect(evaluateDeletion(f.target.proof, [request]).outcome).toBe(
+ 'suppressed'
+ );
+ });
+});