app

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

rumor-identity.test.ts (5367B)


      1 import assert from 'node:assert/strict';
      2 import { createHash } from 'node:crypto';
      3 import test from 'node:test';
      4 import { getEventHash } from 'applesauce-core/helpers';
      5 import { buildCanonicalPairRumor } from '../../src/lib/nostr/rumor-template.ts';
      6 import { messageToWireParts } from '../../src/lib/contracts/message-v1/index.ts';
      7 import { messageCorpus } from './message-cases.ts';
      8 const author =
      9     '79be667ef9dcbbac55a06295ce870b07029bfcdb2dce28d959f2815b16f81798',
     10   peer = 'c6047f9441ed7d6d3045406e95c07cd85c778e4b8cef3ca7abac09b95c709ee5',
     11   time = 1700000100;
     12 const message = (content = 'A plain enquiry') => ({
     13   recipients: [{ public_key: peer, relay_url: null }],
     14   content,
     15   reply_to: null,
     16   subject: null
     17 });
     18 void test('fresh unsigned rumor uses the qualified hash and canonical NIP01 serialization', () => {
     19   const value = buildCanonicalPairRumor(
     20     JSON.stringify(message()),
     21     author,
     22     time
     23   )!;
     24   assert.equal(value.id, getEventHash(value));
     25   assert.equal(
     26     value.id,
     27     createHash('sha256')
     28       .update(
     29         JSON.stringify([0, author, time, 14, [['p', peer]], 'A plain enquiry'])
     30       )
     31       .digest('hex')
     32   );
     33   assert.equal(value.pubkey, author);
     34   assert.equal(value.created_at, time);
     35   assert.equal(value.kind, 14);
     36   assert.equal('sig' in value, false);
     37   assert.deepEqual(Object.keys(value).sort(), [
     38     'content',
     39     'created_at',
     40     'id',
     41     'kind',
     42     'pubkey',
     43     'tags'
     44   ]);
     45 });
     46 void test('body Unicode and generic Message reply/subject semantics survive canonical hashing', () => {
     47   const input = {
     48     ...message('Carrots 🥕\nnostr:untrusted-text'),
     49     reply_to: { id: 'f'.repeat(64), relays: null },
     50     subject: 'Food question'
     51   };
     52   const value = buildCanonicalPairRumor(JSON.stringify(input), author, time)!;
     53   assert.equal(value.content, input.content);
     54   assert.deepEqual(value.tags, [
     55     ['p', peer],
     56     ['e', input.reply_to.id],
     57     ['subject', input.subject]
     58   ]);
     59   assert.equal(value.id, getEventHash(value));
     60 });
     61 void test('same captured fields have the same ID and changed observed timestamp/body changes it', () => {
     62   const raw = JSON.stringify(message()),
     63     first = buildCanonicalPairRumor(raw, author, time)!;
     64   assert.equal(buildCanonicalPairRumor(raw, author, time)!.id, first.id);
     65   assert.notEqual(buildCanonicalPairRumor(raw, author, time + 1)!.id, first.id);
     66   assert.notEqual(
     67     buildCanonicalPairRumor(
     68       JSON.stringify(message('A different intention')),
     69       author,
     70       time
     71     )!.id,
     72     first.id
     73   );
     74   // Intentional same-second duplicate admission/clock progression belongs to074.
     75 });
     76 void test('groups, duplicate recipients and self messages are unsupported', () => {
     77   for (const recipients of [
     78     [],
     79     [{ public_key: author }],
     80     [{ public_key: peer }, { public_key: author }],
     81     [{ public_key: peer }, { public_key: peer }]
     82   ])
     83     assert.equal(
     84       buildCanonicalPairRumor(
     85         JSON.stringify({ ...message(), recipients }),
     86         author,
     87         time
     88       ),
     89       undefined
     90     );
     91 });
     92 void test('keys and observed numeric timestamps cannot be coerced or rounded', () => {
     93   for (const owner of [
     94     author.toUpperCase(),
     95     '0'.repeat(64),
     96     '',
     97     null,
     98     { toString: () => author }
     99   ])
    100     assert.equal(
    101       buildCanonicalPairRumor(JSON.stringify(message()), owner, time),
    102       undefined
    103     );
    104   for (const timestamp of [
    105     -1,
    106     -0,
    107     1.5,
    108     Infinity,
    109     NaN,
    110     Number.MAX_SAFE_INTEGER + 1,
    111     String(time),
    112     null
    113   ])
    114     assert.equal(
    115       buildCanonicalPairRumor(JSON.stringify(message()), author, timestamp),
    116       undefined
    117     );
    118   assert.equal(
    119     buildCanonicalPairRumor(JSON.stringify(message()), author, 0)!.created_at,
    120     0
    121   );
    122 });
    123 void test('body and complete rumor JSON limits are separately enforced without truncation', () => {
    124   assert.equal(
    125     buildCanonicalPairRumor(
    126       JSON.stringify(message('a'.repeat(4096))),
    127       author,
    128       time
    129     )!.content.length,
    130     4096
    131   );
    132   assert.equal(
    133     buildCanonicalPairRumor(
    134       JSON.stringify(message('é'.repeat(2048))),
    135       author,
    136       time
    137     )!.content.length,
    138     2048
    139   );
    140   for (const content of ['a'.repeat(4097), 'é'.repeat(2049), '\ud800', ' \n\t'])
    141     assert.equal(
    142       buildCanonicalPairRumor(JSON.stringify(message(content)), author, time),
    143       undefined
    144     );
    145   const escaped = JSON.stringify(message('"'.repeat(4000)));
    146   assert.ok(messageToWireParts(escaped), 'detached parts still fit');
    147   assert.equal(
    148     buildCanonicalPairRumor(escaped, author, time),
    149     undefined,
    150     'full rumor overhead exceeds8KiB'
    151   );
    152 });
    153 for (const vector of messageCorpus().vectors.filter((v) => v.mode === 'write'))
    154   void test(
    155     'pinned Message writer projection into pair rumor: ' + vector.id,
    156     () => {
    157       const raw = JSON.stringify(vector.input),
    158         parts = messageToWireParts(raw);
    159       const recipients = parts?.tags.filter((row) => row[0] === 'p');
    160       const value = buildCanonicalPairRumor(raw, author, time);
    161       if (!parts || recipients?.length !== 1 || recipients[0]?.[1] === author)
    162         assert.equal(value, undefined);
    163       else {
    164         assert.ok(value);
    165         assert.deepEqual(value.tags, parts.tags);
    166         assert.equal(value.content, parts.content);
    167         assert.equal(value.id, getEventHash(value));
    168         assert.equal(value.pubkey, author);
    169       }
    170     }
    171   );