seal-template.ts (3227B)
1 import { getEventHash, type UnsignedEvent } from 'applesauce-core/helpers'; 2 import { canonicalPublicKey } from '../contracts/public-key.ts'; 3 import { boundedUtf8 } from '../contracts/food-availability-v1/text.ts'; 4 import { safeUnsignedInteger } from './envelope-bounds.ts'; 5 import { verifyEnvelope } from './verified-envelope.ts'; 6 // Encoded version/size preflight only. Authentication/decryption is owned by 7 // the SDK and later nested validation, never inferred from base64 structure. 8 export function sealCiphertextV2(value: unknown): value is string { 9 if ( 10 typeof value !== 'string' || 11 value.length < 132 || 12 !boundedUtf8(value, 16384) || 13 !/^[A-Za-z0-9+/]+={0,2}$/.test(value) 14 ) 15 return false; 16 try { 17 const decoded = atob(value); 18 return ( 19 decoded.length >= 99 && 20 decoded.charCodeAt(0) === 2 && 21 btoa(decoded) === value 22 ); 23 } catch { 24 return false; 25 } 26 } 27 // Detached pure data, not an operation/signing/publication capability. 28 export function captureSealTemplate( 29 author: unknown, 30 ciphertext: unknown, 31 timestamp: unknown 32 ): string | undefined { 33 const owner = canonicalPublicKey(author), 34 time = safeUnsignedInteger(timestamp); 35 if (!owner || time === undefined || !sealCiphertextV2(ciphertext)) 36 return undefined; 37 const wire = JSON.stringify({ 38 pubkey: owner, 39 kind: 13, 40 created_at: time, 41 tags: [], 42 content: ciphertext 43 }); 44 return boundedUtf8(wire, 16384) ? wire : undefined; 45 } 46 function own(value: object, name: string): unknown { 47 const property = Object.getOwnPropertyDescriptor(value, name); 48 return property && 'value' in property ? property.value : undefined; 49 } 50 // Reconstruct only fixed own data fields. No raw spread, getters, toJSON, 51 // iterators, extra fields or SDK cache symbols enter the retained private wire. 52 export function bindSealResponse( 53 captured: unknown, 54 response: unknown 55 ): string | undefined { 56 try { 57 if ( 58 typeof captured !== 'string' || 59 !boundedUtf8(captured, 16384) || 60 typeof response !== 'object' || 61 response === null || 62 Array.isArray(response) 63 ) 64 return undefined; 65 const template = JSON.parse(captured) as UnsignedEvent; 66 if ( 67 captureSealTemplate( 68 template.pubkey, 69 template.content, 70 template.created_at 71 ) !== captured 72 ) 73 return undefined; 74 for (const name of ['pubkey', 'kind', 'created_at', 'content'] as const) 75 if (own(response, name) !== template[name]) return undefined; 76 const tags = own(response, 'tags'), 77 id = own(response, 'id'), 78 sig = own(response, 'sig'); 79 if ( 80 !Array.isArray(tags) || 81 own(tags, 'length') !== 0 || 82 typeof id !== 'string' || 83 !/^[0-9a-f]{64}$/.test(id) || 84 typeof sig !== 'string' || 85 !/^[0-9a-f]{128}$/.test(sig) || 86 id !== getEventHash(template) 87 ) 88 return undefined; 89 const wire = JSON.stringify({ 90 pubkey: template.pubkey, 91 kind: 13, 92 created_at: template.created_at, 93 tags: [], 94 content: template.content, 95 id, 96 sig 97 }); 98 return boundedUtf8(wire, 16384) && verifyEnvelope(wire).ok 99 ? wire 100 : undefined; 101 } catch { 102 return undefined; 103 } 104 }