app

Local-first trade for farms and co-ops
git clone https://radroots.dev/git/app.git
Log | Files | Refs | README | LICENSE

nested-envelope-sizes.test.ts (6003B)


      1 import test from 'node:test';
      2 import assert from 'node:assert/strict';
      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 const bytes = (value: string) => new TextEncoder().encode(value).length;
     12 function input(peer: string, content: string) {
     13   return JSON.stringify({
     14     recipients: [{ public_key: peer, relay_url: null }],
     15     content,
     16     reply_to: null,
     17     subject: null
     18   });
     19 }
     20 await test('4096 UTF8 body boundaries and serialization overhead reject unchanged originals, never truncate', () => {
     21   const secret = generateSecretKey(),
     22     peerSecret = generateSecretKey();
     23   try {
     24     const owner = getPublicKey(secret),
     25       peer = getPublicKey(peerSecret);
     26     const cases = [
     27       ['ASCII', 'x'.repeat(4096)],
     28       ['BMP', 'é'.repeat(2048)],
     29       ['astral', '😀'.repeat(1024)]
     30     ];
     31     for (const [label, body] of cases) {
     32       const raw = input(peer, body),
     33         rumor = buildCanonicalPairRumor(raw, owner, 1700000000);
     34       assert.equal(bytes(body), 4096);
     35       assert.ok(rumor, label);
     36       assert.equal(rumor.content, body);
     37       assert.ok(bytes(JSON.stringify(rumor)) <= 8192);
     38       assert.equal(
     39         buildCanonicalPairRumor(input(peer, body + 'x'), owner, 1700000000),
     40         undefined
     41       );
     42       assert.equal(raw, input(peer, body));
     43     }
     44     for (const body of [
     45       '\ud800',
     46       '\udc00',
     47       '"'.repeat(4096),
     48       '\\'.repeat(4096),
     49       '\n'.repeat(4096),
     50       '\u0001'.repeat(4096)
     51     ]) {
     52       const raw = input(peer, body);
     53       assert.equal(buildCanonicalPairRumor(raw, owner, 1700000000), undefined);
     54       assert.equal(raw, input(peer, body));
     55     }
     56   } finally {
     57     secret.fill(0);
     58     peerSecret.fill(0);
     59   }
     60 });
     61 await test('stock SDK padding steps are measured from actual standard payloads and roundtrip without extended mode', () => {
     62   const secret = generateSecretKey(),
     63     peerSecret = generateSecretKey();
     64   let conversation: Uint8Array | undefined;
     65   try {
     66     conversation = nip44.v2.utils.getConversationKey(
     67       secret,
     68       getPublicKey(peerSecret)
     69     );
     70     const rows = [];
     71     for (const [size, expected] of [
     72       [32, 132],
     73       [33, 176],
     74       [256, 432],
     75       [257, 516],
     76       [4096, 5552],
     77       [4097, 6916],
     78       [8192, 11012],
     79       [8193, 13744],
     80       [16384, 21936],
     81       [16385, 27396]
     82     ]) {
     83       const plain = 'x'.repeat(size),
     84         cipher = nip44.v2.encrypt(plain, conversation);
     85       assert.equal(bytes(cipher), expected);
     86       assert.equal(nip44.v2.decrypt(cipher, conversation), plain);
     87       rows.push({
     88         plaintext_utf8: size,
     89         ciphertext_utf8: bytes(cipher),
     90         mode: 'stock_standard_v2_primitive_only'
     91       });
     92     }
     93     console.log(
     94       JSON.stringify({
     95         fixture: 'HCP079_ACTUAL_STOCK_PADDING_NOT_PRODUCT_ADMISSION',
     96         rows
     97       })
     98     );
     99   } finally {
    100     conversation?.fill(0);
    101     secret.fill(0);
    102     peerSecret.fill(0);
    103   }
    104 });
    105 await test('valid signed detached serialized seal and outer exact caps accept, plus one rejects before admission', () => {
    106   const secret = generateSecretKey(),
    107     peerSecret = generateSecretKey(),
    108     outerSecret = generateSecretKey();
    109   let conversation: Uint8Array | undefined,
    110     outerConversation: Uint8Array | undefined;
    111   try {
    112     const owner = getPublicKey(secret),
    113       peer = getPublicKey(peerSecret),
    114       rumor = buildCanonicalPairRumor(
    115         input(peer, 'bounded fixture'),
    116         owner,
    117         1700000000
    118       );
    119     assert.ok(rumor);
    120     const original = JSON.stringify(rumor);
    121     conversation = nip44.v2.utils.getConversationKey(secret, peer);
    122     const seal = finalizeEvent(
    123         {
    124           kind: 13,
    125           created_at: 1699999000,
    126           tags: [],
    127           content: nip44.v2.encrypt(original, conversation)
    128         },
    129         secret
    130       ),
    131       sealWire = JSON.stringify(seal);
    132     outerConversation = nip44.v2.utils.getConversationKey(outerSecret, peer);
    133     const outer = finalizeEvent(
    134         {
    135           kind: 1059,
    136           created_at: 1699998000,
    137           tags: [['p', peer]],
    138           content: nip44.v2.encrypt(sealWire, outerConversation)
    139         },
    140         outerSecret
    141       ),
    142       outerWire = JSON.stringify(outer);
    143     assert.equal(nip44.v2.decrypt(seal.content, conversation), original);
    144     assert.equal(nip44.v2.decrypt(outer.content, outerConversation), sealWire);
    145     // Valid JSON whitespace changes serialized admission bytes, not signed data.
    146     // These detached fixtures do NOT claim factory custody or genuine maxima.
    147     const atSeal = sealWire + ' '.repeat(16384 - bytes(sealWire)),
    148       atOuter = outerWire + ' '.repeat(32768 - bytes(outerWire));
    149     assert.equal(bytes(atSeal), 16384);
    150     assert.equal(bytes(atOuter), 32768);
    151     assert.equal(
    152       verifyOutboundLayerData(
    153         original,
    154         original,
    155         atSeal,
    156         atOuter,
    157         owner,
    158         peer,
    159         'peer'
    160       ),
    161       true
    162     );
    163     assert.equal(
    164       verifyOutboundLayerData(
    165         original,
    166         original,
    167         atSeal + ' ',
    168         atOuter,
    169         owner,
    170         peer,
    171         'peer'
    172       ),
    173       false
    174     );
    175     assert.equal(
    176       verifyOutboundLayerData(
    177         original,
    178         original,
    179         atSeal,
    180         atOuter + ' ',
    181         owner,
    182         peer,
    183         'peer'
    184       ),
    185       false
    186     );
    187     console.log(
    188       JSON.stringify({
    189         fixture:
    190           'HCP079_DETACHED_SIGNED_RAW_JSON_BOUNDARIES_NOT_FACTORY_MAXIMA',
    191         seal_utf8: bytes(atSeal),
    192         outer_utf8: bytes(atOuter),
    193         seal_plus_one_rejected: true,
    194         outer_plus_one_rejected: true
    195       })
    196     );
    197   } finally {
    198     conversation?.fill(0);
    199     outerConversation?.fill(0);
    200     secret.fill(0);
    201     peerSecret.fill(0);
    202     outerSecret.fill(0);
    203   }
    204 });