app

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

seal-builder.test.ts (4476B)


      1 import { afterAll, expect, it } from 'vitest';
      2 import {
      3   finalizeEvent,
      4   generateSecretKey,
      5   getPublicKey,
      6   verifiedSymbol,
      7   type UnsignedEvent
      8 } from 'applesauce-core/helpers';
      9 import {
     10   sealCiphertextV2,
     11   captureSealTemplate,
     12   bindSealResponse
     13 } from '../../src/lib/nostr/seal-template.ts';
     14 import { isPublicEnvelopeKind } from '../../src/lib/nostr/public-kinds.ts';
     15 const key = generateSecretKey();
     16 const owner = getPublicKey(key);
     17 afterAll(() => key.fill(0));
     18 const cipher = btoa(String.fromCharCode(2) + 'x'.repeat(98));
     19 function fixture() {
     20   const wire = captureSealTemplate(owner, cipher, 1700000000);
     21   if (!wire) throw Error('expected source seal template');
     22   return { wire, template: JSON.parse(wire) as UnsignedEvent };
     23 }
     24 it('captures only encrypted kind13 with exact sender/time and empty tags; public lane excludes it', () => {
     25   const { template } = fixture();
     26   expect(template).toEqual({
     27     pubkey: owner,
     28     kind: 13,
     29     created_at: 1700000000,
     30     tags: [],
     31     content: cipher
     32   });
     33   expect(isPublicEnvelopeKind(13)).toBe(false);
     34   expect(isPublicEnvelopeKind(14)).toBe(false);
     35 });
     36 it('requires bounded canonical v2 encoded payload and exact numeric/owner inputs', () => {
     37   expect(sealCiphertextV2(cipher)).toBe(true);
     38   for (const bad of [
     39     null,
     40     '',
     41     'plain text',
     42     '#version',
     43     cipher + '\n',
     44     btoa(String.fromCharCode(1) + 'x'.repeat(98)),
     45     'A'.repeat(16385)
     46   ])
     47     expect(sealCiphertextV2(bad)).toBe(false);
     48   for (const bad of [-0, -1, 1.5, Number.MAX_SAFE_INTEGER + 1, '100'])
     49     expect(captureSealTemplate(owner, cipher, bad)).toBeUndefined();
     50   expect(captureSealTemplate(owner.toUpperCase(), cipher, 1)).toBeUndefined();
     51 });
     52 it('accepts genuine signatures while reconstructing only seven allowed signed fields', () => {
     53   const { wire, template } = fixture();
     54   const signed = finalizeEvent(template, key);
     55   const result = bindSealResponse(wire, {
     56     ...signed,
     57     plaintext: 'private sentinel',
     58     toJSON: () => {
     59       throw Error('must not execute');
     60     }
     61   });
     62   expect(result).toBeDefined();
     63   expect(
     64     Object.keys(JSON.parse(result!) as Record<string, unknown>).sort()
     65   ).toEqual(['content', 'created_at', 'id', 'kind', 'pubkey', 'sig', 'tags']);
     66   expect(result).not.toContain('sentinel');
     67 });
     68 it('rejects valid signatures for substituted ciphertext, kind, tags, time or sender', () => {
     69   const { wire, template } = fixture();
     70   for (const changed of [
     71     { ...template, content: btoa(String.fromCharCode(2) + 'y'.repeat(98)) },
     72     { ...template, kind: 1059 },
     73     { ...template, tags: [['p', owner]] },
     74     { ...template, created_at: 1700000001 }
     75   ])
     76     expect(bindSealResponse(wire, finalizeEvent(changed, key))).toBeUndefined();
     77   const other = generateSecretKey();
     78   try {
     79     expect(
     80       bindSealResponse(wire, finalizeEvent(template, other))
     81     ).toBeUndefined();
     82   } finally {
     83     other.fill(0);
     84   }
     85 });
     86 it('freshly verifies hash and signature despite provider cache symbols', () => {
     87   const { wire, template } = fixture();
     88   const signed = finalizeEvent(template, key);
     89   expect(
     90     bindSealResponse(wire, {
     91       ...signed,
     92       id: '0'.repeat(64),
     93       [verifiedSymbol]: true
     94     })
     95   ).toBeUndefined();
     96   expect(
     97     bindSealResponse(wire, {
     98       ...signed,
     99       sig: '0'.repeat(128),
    100       [verifiedSymbol]: true
    101     })
    102   ).toBeUndefined();
    103 });
    104 it('own data fields only: getters/toJSON/iterators cannot supply accepted fields', () => {
    105   const { wire, template } = fixture();
    106   const signed = finalizeEvent(template, key);
    107   let reads = 0;
    108   const response = { ...signed };
    109   Object.defineProperty(response, 'content', {
    110     get() {
    111       reads++;
    112       return cipher;
    113     }
    114   });
    115   expect(bindSealResponse(wire, response)).toBeUndefined();
    116   expect(reads).toBe(0);
    117   const tags: string[] = [];
    118   Object.defineProperty(tags, Symbol.iterator, {
    119     value: () => {
    120       throw Error('must not enumerate');
    121     }
    122   });
    123   expect(bindSealResponse(wire, { ...signed, tags })).toBeDefined();
    124 });
    125 it('malformed templates/objects and nonempty provider tags never become private seals', () => {
    126   const { wire, template } = fixture();
    127   for (const bad of [null, [], {}, '{', ' '.repeat(16385)])
    128     expect(bindSealResponse(wire, bad)).toBeUndefined();
    129   expect(bindSealResponse('{}', finalizeEvent(template, key))).toBeUndefined();
    130   expect(
    131     bindSealResponse(wire, {
    132       ...finalizeEvent(template, key),
    133       tags: [['client', 'leak']]
    134     })
    135   ).toBeUndefined();
    136 });