local-template.ts (2109B)
1 import { getEventHash, type NostrEvent } from 'applesauce-core/helpers'; 2 import { PUBLIC_INGRESS_BUDGETS } from '../config/budgets.ts'; 3 import { canonicalPublicKey } from '../contracts/public-key.ts'; 4 import { exactLocalFields } from '../contracts/local-records.ts'; 5 import { boundedUtf8 } from '../contracts/food-availability-v1/text.ts'; 6 import { boundedEnvelopeNumbers } from './envelope-bounds.ts'; 7 import { boundedEnvelopeTags } from './verified-envelope.ts'; 8 export type PublicSignedFields = NostrEvent; 9 export type LocalUnsignedTemplate = Readonly<{ 10 pubkey: string; 11 kind: number; 12 created_at: number; 13 tags: readonly (readonly string[])[]; 14 content: string; 15 hash: string; 16 }>; 17 // Hashing uses the qualified SDK. Admission conveys no signer/transport/store 18 // capability, supported Food profile, current-head or publishing authorization. 19 export function inspectLocalTemplate( 20 raw: unknown, 21 expectedOwner: unknown, 22 expectedKind: unknown 23 ): LocalUnsignedTemplate | undefined { 24 const owner = canonicalPublicKey(expectedOwner); 25 if ( 26 !owner || 27 ![30402, 5, 10050].some((kind) => kind === expectedKind) || 28 typeof raw !== 'string' || 29 !boundedUtf8(raw, PUBLIC_INGRESS_BUDGETS.eventBytes) 30 ) 31 return undefined; 32 try { 33 const value: unknown = JSON.parse(raw); 34 if ( 35 !exactLocalFields(value, [ 36 'pubkey', 37 'kind', 38 'created_at', 39 'tags', 40 'content' 41 ]) || 42 JSON.stringify(value) !== raw || 43 value.pubkey !== owner || 44 value.kind !== expectedKind || 45 !boundedEnvelopeTags(value.tags) || 46 typeof value.content !== 'string' || 47 !boundedUtf8(value.content, PUBLIC_INGRESS_BUDGETS.eventBytes) 48 ) 49 return undefined; 50 const numbers = boundedEnvelopeNumbers(value.kind, value.created_at); 51 if (!numbers) return undefined; 52 const template = { 53 pubkey: owner, 54 kind: numbers.kind, 55 created_at: numbers.created_at, 56 tags: value.tags, 57 content: value.content 58 }; 59 return { ...template, hash: getEventHash(template) }; 60 } catch { 61 return undefined; 62 } 63 }