deletion-adapter.ts (6714B)
1 import { canonicalPublicKey } from '../contracts/public-key.ts'; 2 import { boundedUtf8 } from '../contracts/food-availability-v1/text.ts'; 3 import { 4 verifiedEnvelopeSnapshot, 5 type VerifiedEnvelope 6 } from './verified-envelope.ts'; 7 8 declare const deletionBrand: unique symbol; 9 export type DeletionRequest = Readonly<{ readonly [deletionBrand]: true }>; 10 type Target = Readonly<{ tagIndex: number; rawTag: readonly string[] }>; 11 export type DeletionRequestSnapshot = Readonly<{ 12 id: string; 13 pubkey: string; 14 created_at: number; 15 eventTargets: readonly (Target & { readonly eventId: string })[]; 16 addressTargets: readonly (Target & { readonly coordinate: string })[]; 17 kindAdvisories: readonly (Target & { readonly kind: number })[]; 18 diagnostics: readonly (Target & { readonly code: string })[]; 19 rawTags: readonly (readonly string[])[]; 20 }>; 21 const owners = new WeakMap< 22 DeletionRequest, 23 Readonly<{ 24 proof: VerifiedEnvelope; 25 projection: DeletionRequestSnapshot; 26 }> 27 >(); 28 const encoder = new TextEncoder(); 29 function emptyRows<T>(): T[] { 30 return []; 31 } 32 // Generic Lib189 Nip01Coordinate, deliberately separate from Food/naddr. 33 // Rust u32 text parsing accepts a plus and leading zeroes; identifiers stay raw. 34 export function canonicalDeletionCoordinate(value: string): string | undefined { 35 if (!boundedUtf8(value, 4096)) return undefined; 36 const first = value.indexOf(':'), 37 second = value.indexOf(':', first + 1); 38 if (first < 0 || second < 0) return undefined; 39 const kindText = value.slice(0, first); 40 if (!/^\+?[0-9]+$/u.test(kindText)) return undefined; 41 const kind = Number(kindText), 42 identifier = value.slice(second + 1); 43 const replaceable = 44 kind === 0 || kind === 3 || (kind >= 10000 && kind <= 19999); 45 if ( 46 (!replaceable && !(kind >= 30000 && kind <= 39999)) || 47 (replaceable && identifier !== '') 48 ) 49 return undefined; 50 const pubkey = canonicalPublicKey( 51 value.slice(first + 1, second).toLowerCase() 52 ); 53 return pubkey === undefined ? undefined : `${kind}:${pubkey}:${identifier}`; 54 } 55 function compareUtf8(a: string, b: string): number { 56 const x = encoder.encode(a), 57 y = encoder.encode(b); 58 for (let i = 0; i < Math.min(x.length, y.length); i++) 59 if (x[i] !== y[i]) return x[i] - y[i]; 60 return x.length - y.length; 61 } 62 // Verification already enforces the selected content/tag/element/aggregate and 63 // 256KiB wire budgets. The minimal kind5 projection cannot exceed that raw wire. 64 // Admission establishes references only, never author authority or deletion. 65 export function admitDeletionRequest( 66 proof: VerifiedEnvelope 67 ): 68 | Readonly<{ ok: true; value: DeletionRequest }> 69 | Readonly<{ ok: false; code: string }> { 70 const event = verifiedEnvelopeSnapshot(proof); 71 if (!event) return { ok: false, code: 'deletion_proof_invalid' }; 72 if (event.kind !== 5) return { ok: false, code: 'unsupported_kind' }; 73 let eventTargets = emptyRows<Target & { eventId: string }>(); 74 let addressTargets = emptyRows<Target & { coordinate: string }>(); 75 for (let tagIndex = 0; tagIndex < event.tags.length; tagIndex++) { 76 const rawTag = event.tags[tagIndex], 77 name = rawTag[0], 78 value = rawTag[1]; 79 if (name === 'e') { 80 if (value === undefined) 81 return { ok: false, code: 'deletion_event_target_shape' }; 82 if (!/^[0-9a-fA-F]{64}$/u.test(value)) 83 return { ok: false, code: 'deletion_event_target_invalid' }; 84 const eventId = value.toLowerCase(); 85 if (!eventTargets.some((v) => v.eventId === eventId)) 86 eventTargets = eventTargets.concat({ tagIndex, rawTag, eventId }); 87 } else if (name === 'a') { 88 if (value === undefined) 89 return { ok: false, code: 'deletion_address_target_shape' }; 90 const coordinate = canonicalDeletionCoordinate(value); 91 if (coordinate === undefined) 92 return { ok: false, code: 'deletion_address_target_invalid' }; 93 if (!addressTargets.some((v) => v.coordinate === coordinate)) 94 addressTargets = addressTargets.concat({ 95 tagIndex, 96 rawTag, 97 coordinate 98 }); 99 } 100 } 101 if (eventTargets.length === 0 && addressTargets.length === 0) 102 return { ok: false, code: 'deletion_target_missing' }; 103 let kindAdvisories = emptyRows<Target & { kind: number }>(); 104 let diagnostics = emptyRows<Target & { code: string }>(); 105 for (let tagIndex = 0; tagIndex < event.tags.length; tagIndex++) { 106 const rawTag = event.tags[tagIndex]; 107 if (rawTag[0] !== 'k') continue; 108 const value = rawTag[1], 109 kind = Number(value); 110 const suffix = 111 value === undefined 112 ? 'shape' 113 : !/^(?:0|[1-9][0-9]*)$/u.test(value) || kind > 65535 114 ? 'invalid' 115 : kindAdvisories.some((v) => v.kind === kind) 116 ? 'duplicate' 117 : undefined; 118 if (suffix) 119 diagnostics = diagnostics.concat({ 120 tagIndex, 121 rawTag, 122 code: `deletion_kind_advisory_${suffix}_ignored` 123 }); 124 else kindAdvisories = kindAdvisories.concat({ tagIndex, rawTag, kind }); 125 } 126 if (eventTargets.length === 0) 127 for (const advisory of kindAdvisories) 128 if ( 129 !addressTargets.some( 130 (v) => 131 Number(v.coordinate.slice(0, v.coordinate.indexOf(':'))) === 132 advisory.kind 133 ) 134 ) 135 diagnostics = diagnostics.concat({ 136 tagIndex: advisory.tagIndex, 137 rawTag: advisory.rawTag, 138 code: 'deletion_kind_advisory_conflict_ignored' 139 }); 140 eventTargets.sort((a, b) => compareUtf8(a.eventId, b.eventId)); 141 addressTargets.sort((a, b) => compareUtf8(a.coordinate, b.coordinate)); 142 kindAdvisories.sort((a, b) => a.kind - b.kind); 143 diagnostics.sort((a, b) => a.tagIndex - b.tagIndex); 144 const token = Object.freeze({}) as DeletionRequest; 145 owners.set(token, { 146 proof, 147 projection: { 148 id: event.id, 149 pubkey: event.pubkey, 150 created_at: event.created_at, 151 eventTargets, 152 addressTargets, 153 kindAdvisories, 154 diagnostics, 155 rawTags: event.tags 156 } 157 }); 158 return { ok: true, value: token }; 159 } 160 // Detached public snapshots cannot alter the retained tombstone or forge it. 161 export function deletionRequestSnapshot( 162 request: DeletionRequest 163 ): DeletionRequestSnapshot | undefined { 164 const owner = owners.get(request); 165 return owner === undefined 166 ? undefined 167 : (JSON.parse(JSON.stringify(owner.projection)) as DeletionRequestSnapshot); 168 } 169 // The original bounded signed public wire remains held by its genuine proof. 170 // This grants no signing capability, runtime store or remote-erasure authority. 171 export function deletionRequestEnvelope( 172 request: DeletionRequest 173 ): VerifiedEnvelope | undefined { 174 return owners.get(request)?.proof; 175 }