terms_wire_oracle.mjs (6709B)
1 // Independent synthetic fixtures. No Rust encoder output is read here. 2 import { createHash } from 'node:crypto'; 3 import { readFileSync, writeFileSync } from 'node:fs'; 4 import assert from 'node:assert/strict'; 5 6 const seller = '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798'; 7 const buyer = 'c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5'; 8 const id = (byte, length = 32) => byte.toString(16).padStart(2, '0').repeat(length); 9 const hash = value => createHash('sha256').update(value).digest('hex'); 10 const window = (start, end) => ({ start, end, timezone: 'Etc/UTC' }); 11 12 function canonical(value) { 13 if (value === null || typeof value === 'string' || typeof value === 'boolean') return JSON.stringify(value); 14 if (typeof value === 'number') { 15 assert(Number.isSafeInteger(value) && value >= 0); 16 return JSON.stringify(value); 17 } 18 if (Array.isArray(value)) return '[' + value.map(canonical).join(',') + ']'; 19 const keys = Object.keys(value).sort(); 20 assert(keys.every(key => /^[\x00-\x7f]*$/.test(key))); 21 return '{' + keys.map(key => JSON.stringify(key) + ':' + canonical(value[key])).join(',') + '}'; 22 } 23 24 function terms(config) { 25 const money = minor => ({ currency: config.currency, exponent: config.exponent, minor }); 26 const quote = config.price === 'negotiated'; 27 const variable = config.price === 'variable'; 28 const quantity = { amount: '500', unit: 'g' }; 29 const contents = config.contents === 'unspecified' ? { type: 'unspecified' } : { type: config.contents, quantity }; 30 const price = quote ? { type: 'quote_required' } : variable ? { 31 type: 'variable_weight', measurement_unit: 'g', minimum_per_sale_unit: '250', 32 maximum_per_sale_unit: '500', rate: money('17'), basis: '100', maximum_per_sale_unit_total: money('85'), 33 } : { type: 'fixed', rate: money('200'), basis: '1', discount_bps: 250, tiers: [{ from: '2', rate: money('150') }] }; 34 const option = { 35 id: id(2), label: 'Bag', unit: { type: 'package', label: 'bag', contents }, 36 increment: '1', minimum: '1', price, 37 supply: quote ? { type: 'unknown' } : { type: 'estimated', amount: '0', window: config.public_window ? window('100', '200') : null }, 38 lead_seconds: '0', 39 }; 40 const agreed = quote ? { 41 type: 'fixed', rate: money('325'), basis: '2', discount_bps: 0, selected_tier_from: null, 42 } : variable ? { 43 type: 'variable_weight', measurement_unit: 'g', minimum: '500', maximum: '1000', 44 rate: money('17'), basis: '100', maximum_total: money('170'), 45 } : { type: 'fixed', rate: money('150'), basis: '1', discount_bps: 250, selected_tier_from: '2' }; 46 const lineTotal = quote ? '325' : variable ? '170' : '439'; 47 const fee = quote ? '0' : '25'; 48 const instructions = config.instructions_by_group ?? [config.instructions]; 49 return { 50 buyer, seller, 51 lines: [{ 52 id: id(5), source: { coordinate: { kind: 30402, author: seller, d: id(1, 16) }, event_id: id(3) }, 53 option_id: id(2), product: { name: 'Carrots š„', description: 'Public crop', producer: seller, origin_summary: 'Example area' }, 54 option, amount: quote || variable ? '2' : '3', fulfillment_group: id(4), 55 source_lot_label: config.lot, price: agreed, maximum_line_total: money(lineTotal), 56 }], 57 fulfillment: instructions.map((instruction, index) => ({ id: id(4 + index), terms: { 58 mode: config.mode, window: window('300', '400'), address: config.address, 59 instructions: instruction, contact: config.contact, 60 } })), 61 fulfillment_charge: money(fee), maximum_total: money(String(BigInt(lineTotal) + BigInt(fee))), 62 supply: config.future ? { type: 'future_promise', expected_window: window('100', '200'), risk_disclosure: 'Weather change š§' } : { type: 'stock_backed' }, 63 cancellation: 'mutual_residual_v1', rounding: 'half_even_net_line_v1', 64 }; 65 } 66 const base = { 67 price: 'fixed', currency: 'CAD', exponent: 2, contents: 'estimated', public_window: true, 68 mode: 'pickup', address: 'Private pickup\nš„¬', instructions: 'Call "arrival"\\now\t', 69 contact: null, lot: 'Private lot A', future: false, 70 }; 71 const controls = Array.from({ length: 32 }, (_, index) => String.fromCharCode(index)).join(''); 72 const configs = [ 73 { name: 'fixed_cad_controls', ...base, instructions: controls + ' "\\/ Ć A\u030a \u2028 \u2029 š„¬' }, 74 { name: 'variable_jpy_delivery', ...base, price: 'variable', currency: 'JPY', exponent: 0, contents: 'fixed', mode: 'delivery', contact: '', lot: null, future: true }, 75 { name: 'negotiated_kwd_promise', ...base, price: 'negotiated', currency: 'KWD', exponent: 3, contents: 'unspecified', public_window: false, contact: 'Private contact', lot: '', future: true }, 76 { name: 'fixed_clf', ...base, currency: 'CLF', exponent: 4, public_window: false }, 77 { name: 'unicode_composed', ...base, instructions: 'Ć ' }, 78 { name: 'unicode_decomposed', ...base, instructions: 'A\u030a' }, 79 { name: 'contact_null', ...base }, 80 { name: 'contact_empty', ...base, contact: '' }, 81 ]; 82 const empty = { name: 'exact_content_limit', ...base, address: '', instructions_by_group: Array(8).fill('') }; 83 let remaining = 16384 - Buffer.byteLength(canonical(terms(empty))); 84 for (let index = 0; index < 8; index++) { 85 const count = Math.min(remaining, 2048); 86 empty.instructions_by_group[index] = 'a'.repeat(count); 87 remaining -= count; 88 } 89 assert.equal(remaining, 0); 90 assert.equal(Buffer.byteLength(canonical(terms(empty))), 16384); 91 configs.push(empty); 92 const vectors = configs.map(config => { 93 const wire = canonical(terms(config)); 94 return { name: config.name, config, wire, utf8_bytes: Buffer.byteLength(wire), 95 terms_digest: hash(Buffer.concat([Buffer.from('radroots.terms.v1\0'), Buffer.from(wire)])), 96 raw_sha256: hash(wire), record_domain_digest: hash(Buffer.concat([Buffer.from('radroots.record.v1\0'), Buffer.from(wire)])), 97 }; 98 }); 99 const overflow = structuredClone(empty); 100 overflow.name = 'content_limit_plus_one'; 101 const index = overflow.instructions_by_group.findIndex(value => value.length < 2048); 102 assert(index >= 0); 103 overflow.instructions_by_group[index] += 'a'; 104 assert.equal(Buffer.byteLength(canonical(terms(overflow))), 16385); 105 const output = JSON.stringify({ schema: 'radroots.terms.vectors.v1', producer: 'independent ECMAScript JCS subset and Node.js SHA-256; synthetic data only', vectors, 106 invalid: [{ config: overflow, utf8_bytes: 16385, expected: 'limit_exceeded' }], 107 }, null, 2) + '\n'; 108 const target = new URL('../fixtures/terms_wire.v1.json', import.meta.url); 109 if (process.argv.includes('--check')) assert.equal(readFileSync(target, 'utf8'), output, 'Terms fixture is stale'); 110 else if (process.argv.includes('--write')) writeFileSync(target, output); 111 else throw new Error('Use --write or --check');