chat_wire_oracle.mjs (4438B)
1 // Independent ECMAScript/Node oracle. Zero-filled payload frames below prove 2 // structure and byte counts only; they deliberately contain no valid MAC. 3 import { createHash } from "node:crypto"; 4 import { readFileSync } from "node:fs"; 5 6 const inputs = JSON.parse(readFileSync(new URL("../fixtures/chat_commands.v1.json", import.meta.url), "utf8")); 7 const hash = (value) => createHash("sha256").update(value, "utf8").digest("hex"); 8 const bytes = (value) => Buffer.byteLength(value, "utf8"); 9 const padded = (length) => { 10 if (length <= 32) return 32; 11 const next = 2 ** Math.ceil(Math.log2(length)); 12 const chunk = next <= 256 ? 32 : next / 8; 13 return Math.ceil(length / chunk) * chunk; 14 }; 15 const frame = (length) => { 16 const buffer = Buffer.alloc(padded(length) + 67); 17 buffer[0] = 2; 18 return buffer.toString("base64"); 19 }; 20 const encodedLength = (length) => Math.ceil((padded(length) + 67) / 3) * 4; 21 const preimage = (event) => JSON.stringify([0, event.pubkey, event.created_at, event.kind, event.tags, event.content]); 22 const unsigned = (event) => ({ id: hash(preimage(event)), ...event }); 23 const signedLength = (event) => bytes(JSON.stringify({ ...unsigned(event), sig: "0".repeat(128) })); 24 const commands = inputs.vectors.map(({ name, wire }) => ({ name, command: JSON.parse(wire) })); 25 const self = structuredClone(commands[0]); 26 self.name = "self_chat"; 27 self.command.input.peer = self.command.context.actor.participant; 28 commands.push(self); 29 const vectors = commands.map(({ name, command }) => { 30 const input = command.input; 31 const author = command.context.actor.participant; 32 const tags = [["p", input.peer], ["radroots", "thread", input.thread_id], ["radroots", "operation", command.context.operation_id]]; 33 if (input.source) tags.push(["radroots", "source", "30402", input.source.coordinate.author, input.source.coordinate.d, input.source.event_id]); 34 if (input.parent) tags.push(["e", input.parent]); 35 const event = { pubkey: author, created_at: 1700000000, kind: 14, tags, content: input.text }; 36 const rumor = JSON.stringify(unsigned(event)); 37 const ciphertext = frame(bytes(rumor)); 38 const seal = { pubkey: author, created_at: 1699999990, kind: 13, tags: [], content: ciphertext }; 39 return { name, wire: JSON.stringify(command), preimage: preimage(event), rumor, message_id: hash(preimage(event)), recipients: [...new Set([author, input.peer])].sort(), structural_ciphertext: ciphertext, seal_preimage: preimage(seal), seal_id: hash(preimage(seal)), signed_seal_bytes: signedLength(seal) }; 40 }); 41 const author = commands[0].command.context.actor.participant; 42 const budgets = [1, 32, 33, 256, 257, 1024, 16384, 24576].map((length) => { 43 const ciphertext = frame(length); 44 const seal = { pubkey: author, created_at: 253402300799, kind: 13, tags: [], content: ciphertext }; 45 const sealBytes = signedLength(seal); 46 const outer = { pubkey: author, created_at: 253402300799, kind: 1059, tags: [["p", author]], content: frame(sealBytes) }; 47 return { rumor_bytes: length, rumor_ciphertext_bytes: bytes(ciphertext), signed_seal_max_bytes: sealBytes, envelope_ciphertext_max_bytes: encodedLength(sealBytes), signed_envelope_max_bytes: signedLength(outer) }; 48 }); 49 const padding = [1, 2, 31, 32, 33, 64, 65, 255, 256, 257, 65535].map((length) => ({ plaintext_bytes: length, encoded_bytes: encodedLength(length) })); 50 const allControls = Array.from({ length: 32 }, (_, index) => String.fromCharCode(index)).join(""); 51 const canonical = [ 52 { created_at: "0", kind: 0, tags: [], content: "" }, 53 { created_at: "9", kind: 9, tags: [[], ["p", author]], content: "🥬 Å A\u030a \u2028 \u2029" }, 54 { created_at: "10", kind: 10, tags: [["quoted\"", "slash\\", allControls]], content: allControls }, 55 { created_at: "18446744073709551615", kind: 4294967295, tags: [], content: "largest legacy integers" }, 56 ].map((value) => { 57 // Decimal integer text preserves the legacy u64 case beyond JS Number. 58 const exact = `[0,${JSON.stringify(author)},${value.created_at},${value.kind},${JSON.stringify(value.tags)},${JSON.stringify(value.content)}]`; 59 return { ...value, author, preimage: exact, event_id: hash(exact), signed_json_bytes: bytes(exact) + 253 }; 60 }); 61 process.stdout.write(JSON.stringify({ schema: "radroots.chat_wire_vectors.v1", producer: `Node.js ${process.versions.node}; independent ECMAScript JSON/SHA-256/base64`, structural_payloads_are_unauthenticated: true, vectors, budgets, padding, canonical }, null, 2) + "\n");