UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

98 lines (97 loc) • 4.44 kB
/** * Transferable coin notes — A issues, B holds, C redeems against A. * * A grant's coins are bound to the pair that agreed them. A note is not: it is a * bearer credit against the issuing node, redeemable by whoever presents it and * holds a grant with that node. That is what lets capacity move through a mesh * rather than only along the edge it was granted on. * * **Replay protection is the whole design.** A note is worth exactly one * redemption, so the issuer keeps a record of every note it minted and marks it * spent under the same lock that credits the balance. Two nodes racing the same * note produce one credit and one `spent`. * * **What a holder can and cannot check.** Notes are signed with an HMAC keyed by * a secret only the issuer has, so an intermediate holder cannot verify one * offline — it can only ask the issuer, which is what `POST /peer/note` is for. * That check is safe to expose because holding the note is itself the * credential: the request must carry a note whose signature the issuer accepts, * so it tells a stranger nothing they did not already have. * * An asymmetric signature would let a holder verify without asking. It is not * available here — the package's browser bundle stubs `node:crypto` down to a * subset with no Ed25519 — and the check-with-the-issuer step has to exist * regardless, because a valid signature says nothing about whether the note has * already been spent. * * @module proxy/shareNotes */ import type { ProxyShareNote, ProxyShareNoteRecord, ProxyShareNoteStatus } from "../types/index.js"; /** Default life of a note. Long enough to hand over, short enough to expire. */ export declare const DEFAULT_NOTE_TTL_MS = 2592000000; /** Wire prefix, so a note is never mistaken for a share token. */ export declare const NOTE_PREFIX = "nln"; /** * How long a record outlives the note it tracks. * * Every mint appends a record and nothing ever removed one, so the file grew * for the life of the node. A record past its `notAfter` cannot authorize * anything — the note is refused as `expired` — so the only thing the extra * retention buys is that a holder presenting a just-lapsed note is told * `expired` rather than the more alarming `unknown`. A month of that is plenty. */ export declare const NOTE_RECORD_RETENTION_MS = 2592000000; export declare function initShareNotes(filePath: string): void; /** Render a note as one line that survives a chat window. */ export declare function encodeShareNote(note: ProxyShareNote): string; /** Read a note back. Returns undefined for anything that is not one of ours. */ export declare function decodeShareNote(encoded: string): ProxyShareNote | undefined; /** * Mint a note against this node. * * The record is written before the note is returned, so a note can never exist * that the issuer has no memory of — the case where a crash between the two * would leave an unredeemable credit in someone's hands. */ export declare function issueShareNote(args: { issuer: string; coins: number; ttlMs?: number; memo?: string; now?: number; }): Promise<ProxyShareNote>; /** * What the issuer makes of a note it is shown. * * `forged` and `unknown` are kept apart deliberately: the first means the * signature does not check out, the second means it does but this node has no * record of minting it — which is what a note from a *different* issuer looks * like, and is a different thing to tell the holder. */ export declare function inspectShareNote(note: ProxyShareNote, secret: string | undefined, now?: number): Promise<{ status: ProxyShareNoteStatus; coins: number; }>; /** * Redeem a note into a grant's balance. * * Marking spent and crediting happen under one lock, so two holders racing the * same note produce exactly one credit. The record is marked **before** the * credit: a crash between them costs the redeemer the note, which is the safe * direction — the alternative is a note that can be redeemed twice. */ export declare function redeemShareNote(args: { note: ProxyShareNote; grantId: string; secret: string | undefined; now?: number; }): Promise<{ ok: true; coins: number; balance: number | undefined; } | { ok: false; status: ProxyShareNoteStatus; }>; /** Every note this node has minted, newest first. */ export declare function listShareNotes(): Promise<ProxyShareNoteRecord[]>;