heads.ts (3463B)
1 import { 2 verifiedEnvelopeSnapshot, 3 type VerifiedEnvelope 4 } from '../nostr/verified-envelope.ts'; 5 6 declare const headBrand: unique symbol; 7 export type PublicHead = Readonly<{ readonly [headBrand]: true }>; 8 export type PublicHeadSnapshot = Readonly<{ 9 kind: 0 | 10050 | 30402; 10 pubkey: string; 11 identifier: string | null; 12 id: string; 13 created_at: number; 14 }>; 15 export type PublicHeadDecision = 16 'applied' | 'duplicate' | 'older' | 'higher_id' | 'coordinate_mismatch'; 17 export type PublicHeadSelection = Readonly<{ 18 decision: PublicHeadDecision; 19 head: PublicHead; 20 }>; 21 interface HeadOwner extends PublicHeadSnapshot { 22 readonly proof: VerifiedEnvelope; 23 } 24 const owners = new WeakMap<PublicHead, HeadOwner>(); 25 function ownerOf(head: PublicHead): HeadOwner { 26 const owner = owners.get(head); 27 if (!owner) throw new Error('public_head_invalid'); 28 return owner; 29 } 30 // The frozen Lib189 raw NIP-01 selector precedes product/profile admission. 31 // Missing, empty, or malformed FIRST d selects the empty address. This is 32 // deliberately separate from the focused FoodIdentifier/naddr constraints. 33 export function createPublicHeadCandidate( 34 proof: VerifiedEnvelope 35 ): PublicHead | undefined { 36 const event = verifiedEnvelopeSnapshot(proof); 37 if (!event) return undefined; 38 const kind = event.kind; 39 if (kind !== 0 && kind !== 10050 && kind !== 30402) return undefined; 40 const identifier = 41 kind === 30402 42 ? (event.tags.find((tag) => tag[0] === 'd')?.[1] ?? '') 43 : null; 44 const token = Object.freeze({}) as PublicHead; 45 owners.set(token, { 46 kind, 47 pubkey: event.pubkey, 48 identifier, 49 id: event.id, 50 created_at: event.created_at, 51 proof 52 }); 53 return token; 54 } 55 // Detached scalar metadata cannot alter the private proof or coordinate. 56 export function publicHeadSnapshot(head: PublicHead): PublicHeadSnapshot { 57 const owner = ownerOf(head); 58 return { 59 kind: owner.kind, 60 pubkey: owner.pubkey, 61 identifier: owner.identifier, 62 id: owner.id, 63 created_at: owner.created_at 64 }; 65 } 66 export function publicHeadEnvelope(head: PublicHead): VerifiedEnvelope { 67 return ownerOf(head).proof; 68 } 69 // Internal dictionary identity, never a normalized title or an on-wire a tag. 70 export function publicHeadKey(head: PublicHead): string { 71 const owner = ownerOf(head); 72 return JSON.stringify([owner.kind, owner.pubkey, owner.identifier]); 73 } 74 // Pure incremental ordering only. Display uncertainty, deletion, expiry, 75 // working-set retention and source resolution remain separate consumers. 76 export function selectPublicHead( 77 current: PublicHead | undefined, 78 candidate: PublicHead 79 ): PublicHeadSelection { 80 const next = ownerOf(candidate); 81 if (current === undefined) return { decision: 'applied', head: candidate }; 82 const previous = ownerOf(current); 83 if ( 84 next.kind !== previous.kind || 85 next.pubkey !== previous.pubkey || 86 next.identifier !== previous.identifier 87 ) 88 return { decision: 'coordinate_mismatch', head: current }; 89 if (next.id === previous.id) return { decision: 'duplicate', head: current }; 90 if (next.created_at > previous.created_at) 91 return { decision: 'applied', head: candidate }; 92 if (next.created_at < previous.created_at) 93 return { decision: 'older', head: current }; 94 // Canonical lower-case hex order equals the pinned 32-byte event-ID order. 95 return next.id < previous.id 96 ? { decision: 'applied', head: candidate } 97 : { decision: 'higher_id', head: current }; 98 }