app

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

inbox-preferences.ts (3474B)


      1 import { canonicalPublicKey } from '../contracts/public-key.ts';
      2 import { canonicalRelayOrigin } from '../config/relays.ts';
      3 import {
      4   verifyEnvelope,
      5   verifiedEnvelopeSnapshot
      6 } from './verified-envelope.ts';
      7 
      8 declare const preferenceBrand: unique symbol;
      9 export type InboxPreference = Readonly<{ [preferenceBrand]: true }>;
     10 type Status = 'supported' | 'unsupported';
     11 type Saved = Readonly<{
     12   wire: string;
     13   author: string;
     14   id: string;
     15   createdAt: number;
     16   status: Status;
     17   relays: readonly string[];
     18 }>;
     19 export type InboxPreferenceResult =
     20   | Readonly<{
     21       status: 'rejected';
     22       reason: 'invalid_owner' | 'invalid_envelope' | 'wrong_author';
     23     }>
     24   | Readonly<{ status: 'unrelated' }>
     25   | Readonly<{ status: Status; value: InboxPreference }>;
     26 const preferences = new WeakMap<InboxPreference, Saved>();
     27 
     28 // Parse only. A compatible advertised list proves neither current discovery
     29 // completeness nor allowlist intersection, access, readiness or consent to write.
     30 // Generic ingress bounds and SDK hashing/signature checks remain authoritative.
     31 export function readInboxPreference(
     32   raw: unknown,
     33   intendedAuthor: unknown
     34 ): InboxPreferenceResult {
     35   const author = canonicalPublicKey(intendedAuthor);
     36   if (!author) return { status: 'rejected', reason: 'invalid_owner' };
     37   if (typeof raw !== 'string')
     38     return { status: 'rejected', reason: 'invalid_envelope' };
     39   const proof = verifyEnvelope(raw);
     40   if (!proof.ok) return { status: 'rejected', reason: 'invalid_envelope' };
     41   const event = verifiedEnvelopeSnapshot(proof.value);
     42   if (!event) return { status: 'rejected', reason: 'invalid_envelope' };
     43   if (event.pubkey !== author)
     44     return { status: 'rejected', reason: 'wrong_author' };
     45   if (event.kind !== 10050) return { status: 'unrelated' };
     46   const relays: string[] = [];
     47   function appendRelay(origin: string) {
     48     relays.push(origin);
     49   }
     50   let compatible = true;
     51   for (const tag of event.tags) {
     52     if (tag[0] !== 'relay') continue;
     53     const origin = tag.length === 2 ? canonicalRelayOrigin(tag[1]) : undefined;
     54     if (!origin) compatible = false;
     55     else if (!relays.includes(origin)) appendRelay(origin);
     56   }
     57   const status =
     58     compatible && relays.length !== 0 ? 'supported' : 'unsupported';
     59   const token = Object.freeze({}) as InboxPreference;
     60   preferences.set(token, {
     61     wire: raw,
     62     author,
     63     id: event.id,
     64     createdAt: event.created_at,
     65     status,
     66     relays: status === 'supported' ? relays : []
     67   });
     68   // A verified unsupported head survives for the later latest-head resolver.
     69   // Never turn malformed/newer data into absence or silently use a partial list.
     70   return { status, value: token };
     71 }
     72 export function inboxPreferenceSnapshot(token: InboxPreference):
     73   | Readonly<{
     74       author: string;
     75       id: string;
     76       createdAt: number;
     77       status: Status;
     78       relays: string[];
     79     }>
     80   | undefined {
     81   const saved = preferences.get(token);
     82   return (
     83     saved && {
     84       author: saved.author,
     85       id: saved.id,
     86       createdAt: saved.createdAt,
     87       status: saved.status,
     88       relays: saved.relays.slice()
     89     }
     90   );
     91 }
     92 // Exact original, including whitespace, duplicate/unknown tags, content and
     93 // bounded extra fields. Extra root fields are preserved data, not signed claims.
     94 // Later explicit review owns any update/removal; this adapter never rewrites it.
     95 export function inboxPreferenceWire(
     96   token: InboxPreference
     97 ): string | undefined {
     98   return preferences.get(token)?.wire;
     99 }