outbound-envelope-layers.ts (3297B)
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 { 5 messageFromWireParts, 6 messageToWireParts 7 } from '../contracts/message-v1/index.ts'; 8 import { safeUnsignedInteger } from './envelope-bounds.ts'; 9 import { verifyEnvelope } from './verified-envelope.ts'; 10 11 function fields( 12 value: unknown, 13 names: string[] 14 ): value is Record<string, unknown> { 15 if (typeof value !== 'object' || value === null || Array.isArray(value)) 16 return false; 17 let count = 0; 18 for (const name in value) { 19 if (!names.includes(name)) return false; 20 count++; 21 } 22 return count === names.length && names.every((name) => name in value); 23 } 24 // Detached structural/signature inspection only. Ciphertext correspondence and 25 // genuine original reservation custody belong to the factory-owned proof. 26 export function verifyOutboundLayerData( 27 original: unknown, 28 rumorWire: unknown, 29 sealWire: unknown, 30 outerWire: unknown, 31 author: unknown, 32 recipient: unknown, 33 role: unknown 34 ): boolean { 35 const owner = canonicalPublicKey(author), 36 peer = canonicalPublicKey(recipient); 37 if ( 38 !owner || 39 !peer || 40 owner === peer || 41 (role !== 'peer' && role !== 'self') || 42 typeof original !== 'string' || 43 typeof rumorWire !== 'string' || 44 original !== rumorWire || 45 !boundedUtf8(rumorWire, 8192) || 46 typeof sealWire !== 'string' || 47 !boundedUtf8(sealWire, 16384) || 48 typeof outerWire !== 'string' || 49 !boundedUtf8(outerWire, 32768) 50 ) 51 return false; 52 try { 53 const rumor: unknown = JSON.parse(rumorWire), 54 seal: unknown = JSON.parse(sealWire), 55 outer: unknown = JSON.parse(outerWire); 56 const unsigned = ['id', 'pubkey', 'created_at', 'kind', 'tags', 'content'], 57 signed = [...unsigned, 'sig']; 58 if ( 59 !fields(rumor, unsigned) || 60 !fields(seal, signed) || 61 !fields(outer, signed) || 62 rumor.pubkey !== owner || 63 rumor.kind !== 14 || 64 safeUnsignedInteger(rumor.created_at) === undefined || 65 seal.pubkey !== owner || 66 seal.kind !== 13 || 67 !Array.isArray(seal.tags) || 68 seal.tags.length !== 0 || 69 outer.kind !== 1059 || 70 !Array.isArray(outer.tags) || 71 JSON.stringify(outer.tags) !== 72 JSON.stringify([['p', role === 'self' ? owner : peer]]) || 73 !verifyEnvelope(sealWire).ok || 74 !verifyEnvelope(outerWire).ok 75 ) 76 return false; 77 const parts = { kind: 14, tags: rumor.tags, content: rumor.content }; 78 const decoded = messageFromWireParts(JSON.stringify(parts)); 79 if ( 80 !decoded || 81 decoded.recipients.length !== 1 || 82 decoded.recipients[0].public_key !== peer 83 ) 84 return false; 85 const canonical = messageToWireParts(JSON.stringify(decoded)); 86 if (!canonical || JSON.stringify(canonical) !== JSON.stringify(parts)) 87 return false; 88 const template: UnsignedEvent = { 89 pubkey: owner, 90 created_at: rumor.created_at as number, 91 kind: 14, 92 tags: canonical.tags.map((row) => [...row]), 93 content: canonical.content 94 }; 95 return typeof rumor.id === 'string' && rumor.id === getEventHash(template); 96 } catch { 97 return false; 98 } 99 }