app

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

message-qualified-matrix.test.ts (6498B)


      1 import assert from 'node:assert/strict';
      2 import { createHash } from 'node:crypto';
      3 import { readFileSync } from 'node:fs';
      4 import test from 'node:test';
      5 import {
      6   qualifiedMessages,
      7   qualifiedMessageReplay
      8 } from './message-qualified-matrix.ts';
      9 import {
     10   messageFromWireParts,
     11   messageToWireParts,
     12   type MessageV1
     13 } from '../../src/lib/contracts/message-v1/index.ts';
     14 import { verifyEnvelope } from '../../src/lib/nostr/verified-envelope.ts';
     15 import type { NostrEvent } from 'applesauce-core/helpers';
     16 const digest = (bytes: string | Uint8Array) =>
     17   createHash('sha256').update(bytes).digest('hex');
     18 const receipt = JSON.parse(
     19   readFileSync(
     20     new URL('./message-qualified-writer-rust.v1.json', import.meta.url),
     21     'utf8'
     22   )
     23 ) as {
     24   schema_version: number;
     25   revision: string;
     26   recipe_sha256: string;
     27   inputs: Record<string, string>;
     28   consumed: { id: string; message: MessageV1 }[];
     29   qualification: string;
     30 };
     31 void test('separate sixteen Message recipes replay actual pinned Rust consumption without changing original shared fixtures', () => {
     32   assert.equal(
     33     receipt.recipe_sha256,
     34     digest(JSON.stringify(qualifiedMessageReplay))
     35   );
     36   assert.equal(receipt.schema_version, 1);
     37   assert.equal(receipt.revision, '189c49b74b4bafc142b00b76b296477931139e72');
     38   assert.equal(qualifiedMessageReplay.length, 16);
     39   assert.deepEqual(
     40     receipt.consumed,
     41     qualifiedMessageReplay.map((row) => ({ id: row.id, message: row.expected }))
     42   );
     43   const inputs = {
     44     'message-qualified-matrix.ts': new URL(
     45       './message-qualified-matrix.ts',
     46       import.meta.url
     47     ),
     48     'source_profile.v1.json': new URL(
     49       '../../../contracts/interop/message/source_profile.v1.json',
     50       import.meta.url
     51     ),
     52     'corpus.v1.json': new URL(
     53       '../../../contracts/interop/message/corpus.v1.json',
     54       import.meta.url
     55     ),
     56     'oracle/src/main.rs': new URL(
     57       '../../../contracts/interop/message/oracle/src/main.rs',
     58       import.meta.url
     59     )
     60   };
     61   assert.deepEqual(
     62     Object.keys(receipt.inputs).sort(),
     63     Object.keys(inputs).sort()
     64   );
     65   for (const [name, url] of Object.entries(inputs))
     66     assert.equal(receipt.inputs[name], digest(readFileSync(url)), name);
     67   assert.match(receipt.qualification, /unsigned kind 14/);
     68   assert.match(receipt.qualification, /not independent-client receive\/reply/);
     69 });
     70 void test('pure Message parity preserves exact p/e/subject, bounded raw text and ordinary citations without orders', () => {
     71   for (const [index, row] of qualifiedMessageReplay.entries()) {
     72     assert.deepEqual(
     73       messageFromWireParts(JSON.stringify(row.wire_parts)),
     74       row.expected,
     75       row.id
     76     );
     77     assert.equal(row.wire_parts.kind, 14);
     78     assert.deepEqual(
     79       row.wire_parts.tags.map((tag) => tag[0]),
     80       [
     81         'p',
     82         ...(row.expected.reply_to ? ['e'] : []),
     83         ...(row.expected.subject !== null ? ['subject'] : [])
     84       ]
     85     );
     86     assert.equal(Object.hasOwn(row.wire_parts, 'sig'), false);
     87     assert.equal(Object.hasOwn(row.wire_parts, 'id'), false);
     88     assert.equal(Object.hasOwn(row.wire_parts, 'pubkey'), false);
     89     assert.deepEqual(row.expected, qualifiedMessages[index]);
     90     assert.equal(
     91       messageToWireParts(
     92         JSON.stringify({ ...row.expected, order: 'unauthorized' })
     93       ),
     94       undefined
     95     );
     96   }
     97   assert.equal(
     98     new TextEncoder().encode(qualifiedMessages[9].content).length,
     99     4096
    100   );
    101   assert.notEqual(qualifiedMessages[6].content, qualifiedMessages[7].content);
    102   assert.equal(
    103     qualifiedMessages[6].content.normalize('NFC'),
    104     qualifiedMessages[7].content.normalize('NFC')
    105   );
    106 });
    107 interface PublishedWires {
    108   schema_version: number;
    109   source: {
    110     url: string;
    111     revision: string;
    112     sha256: string;
    113     bytes: number;
    114     scope: string;
    115   };
    116   cases: {
    117     role: 'peer' | 'self';
    118     fence_start_line: number;
    119     event: NostrEvent;
    120   }[];
    121 }
    122 const published = JSON.parse(
    123   readFileSync(
    124     new URL(
    125       '../fixtures/independent-nip17/public-outer-wires.v1.json',
    126       import.meta.url
    127     ),
    128     'utf8'
    129   )
    130 ) as PublishedWires;
    131 void test('frozen official NIP17 peer and self outer vectors verify fresh public signatures and exact destinations without imported keys or plaintext claims', () => {
    132   assert.equal(published.schema_version, 1);
    133   assert.equal(
    134     published.source.revision,
    135     '0046368a747c5c25ae2bec28bae0e537744c8f10'
    136   );
    137   assert.equal(
    138     published.source.sha256,
    139     '7aa9c3a9c0458a9552c4be2f3bf17f7ec60a4afe88bce7977f03500a7873a64c'
    140   );
    141   assert.equal(published.source.bytes, 12439);
    142   assert.equal(
    143     published.source.url,
    144     'https://raw.githubusercontent.com/nostr-protocol/nips/0046368a747c5c25ae2bec28bae0e537744c8f10/17.md'
    145   );
    146   assert.match(published.source.scope, /outer ciphertext only/);
    147   assert.deepEqual(
    148     published.cases.map((row) => [row.role, row.fence_start_line]),
    149     [
    150       ['peer', 190],
    151       ['self', 204]
    152     ]
    153   );
    154   assert.deepEqual(
    155     published.cases.map((row) => row.event.id),
    156     [
    157       '2886780f7349afc1344047524540ee716f7bdc1b64191699855662330bf235d8',
    158       '162b0611a1911cfcb30f8a5502792b346e535a45658b3a31ae5c178465509721'
    159     ]
    160   );
    161   const destinations = [
    162     '918e2da906df4ccd12c8ac672d8335add131a4cf9d27ce42b3bb3625755f0788',
    163     '44900586091b284416a0c001f677f9c49f7639a55c3f1e2ec130a8e1a7998e1b'
    164   ];
    165   for (const [index, row] of published.cases.entries()) {
    166     assert.deepEqual(
    167       Object.keys(row.event).sort(),
    168       ['id', 'pubkey', 'created_at', 'kind', 'tags', 'content', 'sig'].sort()
    169     );
    170     assert.equal(row.event.kind, 1059);
    171     assert.deepEqual(row.event.tags, [['p', destinations[index]]]);
    172     assert.equal(verifyEnvelope(JSON.stringify(row.event)).ok, true);
    173     assert.equal(messageFromWireParts(JSON.stringify(row.event)), undefined);
    174     assert.equal(JSON.stringify(row.event).includes('nsec'), false);
    175   }
    176   assert.notEqual(
    177     published.cases[0].event.pubkey,
    178     published.cases[1].event.pubkey
    179   );
    180   assert.notEqual(published.cases[0].event.id, published.cases[1].event.id);
    181 });
    182 void test('altered official outer target, ciphertext and signature each fail fresh verification; public examples grant no decrypted rumor custody', () => {
    183   for (const row of published.cases) {
    184     for (const changed of [
    185       { ...row.event, tags: [['p', '0'.repeat(64)]] },
    186       { ...row.event, content: row.event.content + 'A' },
    187       { ...row.event, sig: '0'.repeat(128) }
    188     ])
    189       assert.equal(verifyEnvelope(JSON.stringify(changed)).ok, false);
    190   }
    191 });