nested-qualified-sizes.test.ts (5003B)
1 import assert from 'node:assert/strict'; 2 import { test } from 'node:test'; 3 import { 4 generateSecretKey, 5 getPublicKey, 6 finalizeEvent 7 } from 'applesauce-core/helpers'; 8 import { nip44 } from 'applesauce-core/helpers/encryption'; 9 import { buildCanonicalPairRumor } from '../../src/lib/nostr/rumor-template.ts'; 10 import { verifyOutboundLayerData } from '../../src/lib/nostr/outbound-envelope-layers.ts'; 11 import { PRIVATE_TRANSPORT_BUDGETS } from '../../src/lib/config/budgets.ts'; 12 const bytes = (value: string) => new TextEncoder().encode(value).length; 13 await test('actual stock NIP44 self and peer expansion preserves maximal Unicode body, subject, parent and exact escaped rumor under all nested byte caps', () => { 14 const keys = [ 15 generateSecretKey(), 16 generateSecretKey(), 17 generateSecretKey(), 18 generateSecretKey() 19 ]; 20 try { 21 const owner = getPublicKey(keys[0]), 22 peer = getPublicKey(keys[1]); 23 const input = (content: string) => 24 JSON.stringify({ 25 recipients: [{ public_key: peer, relay_url: null }], 26 content, 27 reply_to: { id: '1'.repeat(64), relays: null }, 28 subject: '🥕'.repeat(160) 29 }); 30 const plan = (content: string) => 31 buildCanonicalPairRumor(input(content), owner, 1700000000); 32 const recipes: [string, string][] = [ 33 ['ASCII', 'x'.repeat(4096)], 34 ['BMP', 'é'.repeat(2048)], 35 ['astral', '😀'.repeat(1024)], 36 ['mixed_escaped', '😀é"\n'.repeat(512)] 37 ]; 38 let low = 0, 39 high = 4096; 40 while (low < high) { 41 const middle = Math.ceil((low + high) / 2); 42 if (plan('\u0001'.repeat(middle))) low = middle; 43 else high = middle - 1; 44 } 45 let maximal = '\u0001'.repeat(low); 46 for (let count = 0; count < 6 && plan(maximal + 'x'); count++) 47 maximal += 'x'; 48 recipes.push(['maximum_escaped_rumor_with_subject_parent', maximal]); 49 const rows = []; 50 for (const [shape, body] of recipes) { 51 const originalInput = input(body), 52 rumor = plan(body); 53 assert.ok(rumor, shape); 54 assert.equal(rumor.content, body); 55 assert.equal(input(body), originalInput); 56 const rumorWire = JSON.stringify(rumor); 57 assert.equal( 58 plan(body + 'x'), 59 undefined, 60 `${shape}: inclusive original boundary` 61 ); 62 if (shape.startsWith('maximum')) 63 assert.equal(bytes(rumorWire), PRIVATE_TRANSPORT_BUDGETS.rumorBytes); 64 else assert.equal(bytes(body), PRIVATE_TRANSPORT_BUDGETS.bodyBytes); 65 for (const [index, role] of ['self', 'peer'].entries()) { 66 const target = role === 'self' ? owner : peer; 67 const sealKey = nip44.v2.utils.getConversationKey(keys[0], target); 68 const wrapKey = nip44.v2.utils.getConversationKey( 69 keys[index + 2], 70 target 71 ); 72 try { 73 const seal = finalizeEvent( 74 { 75 kind: 13, 76 created_at: 1699999900, 77 tags: [], 78 content: nip44.v2.encrypt(rumorWire, sealKey) 79 }, 80 keys[0] 81 ); 82 const sealWire = JSON.stringify(seal); 83 const outer = finalizeEvent( 84 { 85 kind: 1059, 86 created_at: 1699999800, 87 tags: [['p', target]], 88 content: nip44.v2.encrypt(sealWire, wrapKey) 89 }, 90 keys[index + 2] 91 ); 92 const outerWire = JSON.stringify(outer); 93 assert.equal(nip44.v2.decrypt(outer.content, wrapKey), sealWire); 94 assert.equal(nip44.v2.decrypt(seal.content, sealKey), rumorWire); 95 assert.equal( 96 verifyOutboundLayerData( 97 rumorWire, 98 rumorWire, 99 sealWire, 100 outerWire, 101 owner, 102 peer, 103 role 104 ), 105 true 106 ); 107 assert.ok(bytes(body) <= PRIVATE_TRANSPORT_BUDGETS.bodyBytes); 108 assert.ok(bytes(rumorWire) <= PRIVATE_TRANSPORT_BUDGETS.rumorBytes); 109 assert.ok(bytes(sealWire) <= PRIVATE_TRANSPORT_BUDGETS.sealBytes); 110 assert.ok( 111 bytes(outerWire) <= PRIVATE_TRANSPORT_BUDGETS.envelopeBytes 112 ); 113 assert.equal(outer.tags.length, 1); 114 assert.equal(outer.tags[0][1], target); 115 rows.push({ 116 shape, 117 role, 118 body_utf8: bytes(body), 119 rumor_utf8: bytes(rumorWire), 120 seal_ciphertext_utf8: bytes(seal.content), 121 signed_seal_utf8: bytes(sealWire), 122 outer_ciphertext_utf8: bytes(outer.content), 123 signed_outer_utf8: bytes(outerWire) 124 }); 125 } finally { 126 sealKey.fill(0); 127 wrapKey.fill(0); 128 } 129 } 130 } 131 assert.equal(rows.length, 10); 132 console.log( 133 JSON.stringify({ 134 fixture: 135 'HCP132_ACTUAL_STOCK_CRYPTO_DETACHED_NO_FACTORY_CUSTODY_OR_CLIENT_CLAIM', 136 rows 137 }) 138 ); 139 } finally { 140 keys.forEach((key) => key.fill(0)); 141 } 142 });