@peer2peer/cli 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (105) hide show
  1. package/LICENSE +201 -0
  2. package/README.md +366 -2
  3. package/dist/ancestry.d.ts +13 -0
  4. package/dist/ancestry.js +71 -0
  5. package/dist/app.d.ts +363 -0
  6. package/dist/app.js +1177 -0
  7. package/dist/bin.d.ts +2 -0
  8. package/dist/bin.js +56 -0
  9. package/dist/circle-ops.d.ts +402 -0
  10. package/dist/circle-ops.js +1725 -0
  11. package/dist/cli.d.ts +611 -0
  12. package/dist/cli.js +1650 -0
  13. package/dist/codes.d.ts +231 -0
  14. package/dist/codes.js +998 -0
  15. package/dist/config.d.ts +112 -0
  16. package/dist/config.js +243 -0
  17. package/dist/doctor.d.ts +99 -0
  18. package/dist/doctor.js +817 -0
  19. package/dist/errors.d.ts +8 -0
  20. package/dist/errors.js +29 -0
  21. package/dist/ext-host.d.ts +32 -0
  22. package/dist/ext-host.js +259 -0
  23. package/dist/extensions.d.ts +386 -0
  24. package/dist/extensions.js +395 -0
  25. package/dist/features.d.ts +91 -0
  26. package/dist/features.js +164 -0
  27. package/dist/format.d.ts +57 -0
  28. package/dist/format.js +248 -0
  29. package/dist/identity.d.ts +236 -0
  30. package/dist/identity.js +751 -0
  31. package/dist/inbound.d.ts +32 -0
  32. package/dist/inbound.js +258 -0
  33. package/dist/index.d.ts +8 -0
  34. package/dist/index.js +14 -0
  35. package/dist/join-answers.d.ts +49 -0
  36. package/dist/join-answers.js +149 -0
  37. package/dist/outbox.d.ts +109 -0
  38. package/dist/outbox.js +337 -0
  39. package/dist/pretty.d.ts +100 -0
  40. package/dist/pretty.js +256 -0
  41. package/dist/procstart.d.ts +1 -0
  42. package/dist/procstart.js +66 -0
  43. package/dist/progress.d.ts +30 -0
  44. package/dist/progress.js +78 -0
  45. package/dist/projects.d.ts +240 -0
  46. package/dist/projects.js +699 -0
  47. package/dist/prompt/picker.d.ts +64 -0
  48. package/dist/prompt/picker.js +152 -0
  49. package/dist/prompt/tty.d.ts +29 -0
  50. package/dist/prompt/tty.js +122 -0
  51. package/dist/readonly.d.ts +20 -0
  52. package/dist/readonly.js +50 -0
  53. package/dist/receipts.d.ts +99 -0
  54. package/dist/receipts.js +180 -0
  55. package/dist/relay-client.d.ts +277 -0
  56. package/dist/relay-client.js +873 -0
  57. package/dist/relay-info.d.ts +24 -0
  58. package/dist/relay-info.js +53 -0
  59. package/dist/rotation.d.ts +402 -0
  60. package/dist/rotation.js +2054 -0
  61. package/dist/running.d.ts +5 -0
  62. package/dist/running.js +74 -0
  63. package/dist/sessions.d.ts +340 -0
  64. package/dist/sessions.js +1152 -0
  65. package/dist/shortcode.d.ts +149 -0
  66. package/dist/shortcode.js +465 -0
  67. package/dist/social.d.ts +139 -0
  68. package/dist/social.js +485 -0
  69. package/dist/state.d.ts +483 -0
  70. package/dist/state.js +217 -0
  71. package/dist/stdin.d.ts +17 -0
  72. package/dist/stdin.js +46 -0
  73. package/dist/sync.d.ts +77 -0
  74. package/dist/sync.js +375 -0
  75. package/dist/transient.d.ts +1 -0
  76. package/dist/transient.js +28 -0
  77. package/dist/trust.d.ts +37 -0
  78. package/dist/trust.js +134 -0
  79. package/dist/tty-confirm.d.ts +2 -0
  80. package/dist/tty-confirm.js +53 -0
  81. package/dist/tui/command.d.ts +20 -0
  82. package/dist/tui/command.js +70 -0
  83. package/dist/tui/frame.d.ts +43 -0
  84. package/dist/tui/frame.js +111 -0
  85. package/dist/tui/model.d.ts +215 -0
  86. package/dist/tui/model.js +255 -0
  87. package/dist/tui/sanitize.d.ts +13 -0
  88. package/dist/tui/sanitize.js +151 -0
  89. package/dist/tui/terminal.d.ts +88 -0
  90. package/dist/tui/terminal.js +354 -0
  91. package/dist/tui/tui.d.ts +71 -0
  92. package/dist/tui/tui.js +571 -0
  93. package/dist/tui/view.d.ts +4 -0
  94. package/dist/tui/view.js +356 -0
  95. package/dist/unknown-kinds.d.ts +12 -0
  96. package/dist/unknown-kinds.js +66 -0
  97. package/dist/version.d.ts +1 -0
  98. package/dist/version.js +5 -0
  99. package/dist/want-pow.d.ts +12 -0
  100. package/dist/want-pow.js +46 -0
  101. package/dist/watch.d.ts +35 -0
  102. package/dist/watch.js +325 -0
  103. package/dist/xstate.d.ts +125 -0
  104. package/dist/xstate.js +422 -0
  105. package/package.json +60 -4
package/dist/app.d.ts ADDED
@@ -0,0 +1,363 @@
1
+ import { type IdentityFetcher, IdentityChain, type CreateIdentityDocParams, type DeviceKeysV3, type DeviceSignerV3, type EnvelopeKindV3, type IdentityKey, type IdentityView, type RotationStatement, type SignerContext, type StoredCapV3, type TrustStoreV3 } from "@peer2peer/core/v3";
2
+ import { type Env, type FileConfig, type TransportResolver, type Tunables } from "./config.js";
3
+ import { RelayPool, type EnvelopeHandler, type RelayConnection } from "./relay-client.js";
4
+ import type { SelectSpec } from "./prompt/picker.js";
5
+ import type { Pretty } from "./pretty.js";
6
+ import { CliState, type KeysDoc, type SelfDoc } from "./state.js";
7
+ import { type Progress } from "./progress.js";
8
+ import type { ExtensionRegistry } from "./extensions.js";
9
+ export interface Io {
10
+ /** Human output (stdout). */
11
+ out(s: string): void;
12
+ /** Diagnostics (stderr). */
13
+ err(s: string): void;
14
+ /** Asks the user; undefined when there is no terminal. */
15
+ prompt?: (question: string) => Promise<string>;
16
+ /** Asks without echoing what is typed (the backup phrase, review N-L3). */
17
+ promptSecret?: (question: string) => Promise<string>;
18
+ /**
19
+ * Reads all of stdin. `implicit`: the user didn't ask for stdin (e.g. `send` with no text), so
20
+ * an inherited stdin that never ends (PowerShell, an IDE task) must not hang the command: give
21
+ * up with "" when nothing arrives within a few seconds.
22
+ */
23
+ readStdin?: (opts?: {
24
+ implicit?: boolean;
25
+ }) => Promise<string>;
26
+ /**
27
+ * An interactive picker (prompt/tty.ts): the values chosen, or undefined when cancelled. Only set
28
+ * when stdin and stdout are a terminal, CI and --no-input are unset and no agent runs the command.
29
+ */
30
+ select?: (spec: SelectSpec) => Promise<string[] | undefined>;
31
+ /** stdout is a terminal: human output may use pretty.ts (colour only when `color`). */
32
+ pretty?: Pretty;
33
+ }
34
+ export interface AppOptions {
35
+ env: Env;
36
+ io: Io;
37
+ /**
38
+ * `p2p doctor`: the stores read the home but every write stays in memory (readonly.ts), so
39
+ * nothing in P2P_HOME changes, not even after a bounded step times out and finishes later.
40
+ */
41
+ readOnly?: boolean;
42
+ }
43
+ /** Days before a certificate's or document's expiry at which it is renewed. */
44
+ export declare const RENEW_CERT_WITHIN_SEC: number;
45
+ /** How long closing waits for envelope handlers still running. */
46
+ export declare const CLOSE_GRACE_MS = 3000;
47
+ export declare const REPUBLISH_DOC_WITHIN_SEC: number;
48
+ export interface Delivery {
49
+ userId: string;
50
+ /** Devices an envelope was sealed to. */
51
+ devices: number;
52
+ /** Distinct relays that accepted at least one copy. */
53
+ relays: string[];
54
+ /** `written`: it went out on the wire before failing (see SendResult). */
55
+ errors?: {
56
+ did: string;
57
+ relay: string;
58
+ code: string;
59
+ written?: boolean;
60
+ }[];
61
+ /**
62
+ * Copies sealed but accepted by no relay, as sealed (outbox.ts can send these same envelopes
63
+ * later: the recipient's replay window drops a second copy), with the relays and caps they
64
+ * were for and why each relay failed.
65
+ */
66
+ unsent?: {
67
+ did: string;
68
+ env: string;
69
+ targets: {
70
+ relay: string;
71
+ cap?: string;
72
+ }[];
73
+ codes: string[];
74
+ }[];
75
+ }
76
+ export declare class App {
77
+ /** The person's front end vouched for this command (P2P_PERSON_TOKEN, checked once and removed: projects.ts). */
78
+ front: boolean;
79
+ /** The person's "yes" on their controlling terminal (tty-confirm.ts); cli.ts may stand another in (tests). */
80
+ confirmTty: (question: string) => Promise<boolean>;
81
+ readonly env: Env;
82
+ readonly io: Io;
83
+ readonly home: string;
84
+ readonly state: CliState;
85
+ readonly store: TrustStoreV3 & {
86
+ readonly dir: string;
87
+ };
88
+ readonly readOnly: boolean;
89
+ readonly file: FileConfig;
90
+ readonly transport: TransportResolver;
91
+ readonly tun: Tunables;
92
+ keys?: KeysDoc;
93
+ self?: SelfDoc;
94
+ device?: DeviceKeysV3;
95
+ ik?: IdentityKey;
96
+ pool?: RelayPool;
97
+ /** Slow-step notices and P2P_DEBUG step logs (stderr). */
98
+ readonly progress: Progress;
99
+ /** Aborted by the command deadline (cli.ts): stops new network steps (progress.step). */
100
+ readonly abort: AbortController;
101
+ /**
102
+ * Set once an identity publish may have been stored by a relay: one accepted, or one went out
103
+ * and its answer never came (timeout, dropped connection). A restore must not wipe after that
104
+ * (review L1). Definite refusals (a relay's error code) and relays never reached don't set it.
105
+ */
106
+ publishMaybeStored: boolean;
107
+ /** Handles envelopes delivered while connected (set by inbound.ts). */
108
+ onEnvelope?: EnvelopeHandler;
109
+ /** Runs after connectAndDrain (set by inbound.ts: finishes short-code circle joins). */
110
+ afterDrain?: () => Promise<unknown>;
111
+ /** Runs when every relay refused this device and our own document turned out rotated (set by inbound.ts: the alert). */
112
+ onIdentityRotated?: () => Promise<unknown>;
113
+ /** The session this process is the `p2p watch` of (running.ts). */
114
+ watching?: string;
115
+ /** close() is giving up on envelope handlers still running (set by inbound.ts: abandonInFlight). */
116
+ onAbandon?: () => Promise<unknown>;
117
+ /** close() is waiting for the pool's handlers. */
118
+ private closing;
119
+ private discovered;
120
+ private readonly refusedVerdicts;
121
+ /** The relays as of the last connect (for `ls --json`). */
122
+ relayStatus?: {
123
+ listed: string[];
124
+ connected: string[];
125
+ failed: {
126
+ url: string;
127
+ reason: string;
128
+ }[];
129
+ };
130
+ warnings: string[];
131
+ /** Accept restricted relay connections (only `backup restore`, before its new device is listed). */
132
+ allowRestricted: boolean;
133
+ /** Installed extensions (extensions.ts): render inbound `x:<ns>.*` kinds. Absent: fallbacks only. */
134
+ extensions?: ExtensionRegistry;
135
+ constructor(opts: AppOptions);
136
+ /** Loads this device's keys. With `require`, a missing identity is an error. */
137
+ load(require?: boolean): Promise<boolean>;
138
+ setKeys(keys: KeysDoc, self: SelfDoc): void;
139
+ get userId(): string;
140
+ get did(): string;
141
+ get relays(): string[];
142
+ get isKeyholder(): boolean;
143
+ requireKeyholder(what: string): IdentityKey;
144
+ signer(): DeviceSignerV3;
145
+ warn(msg: string): void;
146
+ /** Connects to every own relay (receiving whatever is queued). At least one must answer. */
147
+ connect(opts?: {
148
+ reconnect?: boolean;
149
+ onFatal?: (r: string) => void;
150
+ allowNone?: boolean;
151
+ }): Promise<RelayPool>;
152
+ /** circle-puts started when a members-only relay connection authenticated: close waits for them (bounded). */
153
+ private readonly circlePushes;
154
+ /** Admission keys from redeemed codes not used yet (by relay), or undefined when there are none. */
155
+ private pendingAdmits;
156
+ /** Keeps a redeemed code's admission key for `relay` until that relay admits us with it. */
157
+ saveAdmit(relay: string, key: string): Promise<void>;
158
+ private forgetAdmit;
159
+ /**
160
+ * A members-only relay that follows a circle said which version it holds: when we hold that
161
+ * circle further along, push the rest (`circle-put`), so additions and removals take effect
162
+ * there at once. The relay verifies every version itself; its refusals change nothing here.
163
+ *
164
+ * A chain names every member: it goes only to one of our OWN relays (listed in our identity
165
+ * document), and only for a circle we are a member of. Any other relay (a recipient's, or one
166
+ * that names a circle id it shouldn't know) gets nothing, whatever cid it asks for.
167
+ */
168
+ syncRelayCircle(conn: RelayConnection): Promise<void>;
169
+ /** After this owner published a version of `cid`: every open members-only relay connection that follows it. */
170
+ pushCircleToRelays(cid: string): Promise<void>;
171
+ private refreshingDeviceInfo;
172
+ /** Set while `init` publishes the first document: nothing to bring current until it is on the relays. */
173
+ deviceInfoDeferred: boolean;
174
+ /**
175
+ * docs/compatibility.md: keeps this device's entry in the identity document (`dinfo`) current
176
+ * after an upgrade, or removes it when the user opted out: a key holder publishes the next version
177
+ * when the entry differs from what this client is. At most once an hour, and not when every relay
178
+ * wouldn't store it anyway (an entry is only ever published to relays that list identity.lenient).
179
+ */
180
+ refreshDeviceInfo(): Promise<boolean>;
181
+ /**
182
+ * §2.8: every relay refused this device. When our own document's final
183
+ * version carries a rotation (relays serve documents publicly), the
184
+ * identity moved without this device: say so, and how to re-link.
185
+ */
186
+ throwIfRotated(): Promise<void>;
187
+ /** Connects and handles everything queued for this device. */
188
+ connectAndDrain(): Promise<RelayPool>;
189
+ close(): Promise<void>;
190
+ ownView(): Promise<IdentityView>;
191
+ view(userId: string): Promise<IdentityView | undefined>;
192
+ /**
193
+ * Network I/O for core's refreshIdentity (§5.6), per relay so core can cap
194
+ * each relay's answer on its own: `tips` and the `chain` walk
195
+ * (`/v3/identity/<id>/chain?to=&n=`) on every listed relay (at most 5).
196
+ */
197
+ fetcher(): IdentityFetcher;
198
+ /**
199
+ * Refreshes `userId` from the relays (core refreshIdentity: chain rules,
200
+ * gap filling, automatic jumps only over unanchored history). This is the
201
+ * only way an identity enters the store: bundled documents never do.
202
+ * Failures leave the held view (or none) and are reported as a warning.
203
+ */
204
+ refreshIdentity(userId: string, relays: readonly string[], opts?: {
205
+ force?: boolean;
206
+ quiet?: boolean;
207
+ strict?: boolean;
208
+ }): Promise<IdentityView | undefined>;
209
+ /** Relays a (bundled, unverified) identity document lists: only a hint for where to refresh from. */
210
+ static relaysOf(docJws: string | undefined): string[];
211
+ /** The view of `userId`, refreshed from the relays its document lists (or `hint`, then ours, when unknown). */
212
+ viewFor(userId: string, opts?: {
213
+ refresh?: boolean;
214
+ hint?: readonly string[];
215
+ }): Promise<IdentityView | undefined>;
216
+ /** Relays someone else named (a code's, a pasted link's): their names are checked where they resolve, on every WebSocket and HTTP connection. */
217
+ private guarded;
218
+ /** From now on in this process, connections to these relays (documents over HTTP, the pool's WebSockets) go through privateGuardLookup. */
219
+ guardRelays(relays: readonly string[]): void;
220
+ private guardedLookup;
221
+ /** Signer resolution for core verifiers, finding unknown identities on the relays (once per process). */
222
+ signerCtx(hint?: readonly string[]): SignerContext;
223
+ discover(userId: string, hint?: readonly string[]): Promise<IdentityView | undefined>;
224
+ /** History of our own chain up to (and including) `hash`, oldest first, for back-filling relays. */
225
+ ownHistory(hash: string): Promise<string[]>;
226
+ /** Publishes `doc` (already in our chain) to `relays`; remembers the ones that didn't take it. */
227
+ publishDoc(doc: string, relays: readonly string[]): Promise<{
228
+ ok: string[];
229
+ failed: {
230
+ relay: string;
231
+ code: string;
232
+ }[];
233
+ }>;
234
+ /** On connect: relays that failed transiently, and outright refusals whose backoff has passed. */
235
+ retryUnpublished(): Promise<void>;
236
+ /** Publishes our current head again to every relay it lists (R4-M2: `p2p identity republish`). */
237
+ republish(): Promise<{
238
+ ver: number;
239
+ ok: string[];
240
+ failed: {
241
+ relay: string;
242
+ code: string;
243
+ }[];
244
+ }>;
245
+ /** Takes a newer certificate for this device from disk (another process renewed it) or our own document. */
246
+ private reloadCert;
247
+ /**
248
+ * R4-L2: a relay refused this device. Refresh our own document: if some branch still lists
249
+ * this device (e.g. it was linked during a fork, so it isn't in the intersection yet), retry
250
+ * later with backoff; if none does, it was revoked.
251
+ */
252
+ authRefusedVerdict(relay: string): Promise<"retry" | "fatal">;
253
+ updateSelf(fn: (s: SelfDoc) => SelfDoc): Promise<SelfDoc>;
254
+ /**
255
+ * Builds, signs and publishes the next version of our identity document
256
+ * (§2.5): fetches the head from every listed relay first and builds on the
257
+ * newest, keeps the device certificates current, and records what this
258
+ * device added (compromise detection).
259
+ */
260
+ publishNext(change: (p: CreateIdentityDocParams) => void, opts?: {
261
+ restoring?: boolean;
262
+ /** §2.8 step 6: the old identity's final version: no devices, no key holders, carrying the statement (P3). */
263
+ rotation?: RotationStatement;
264
+ }): Promise<{
265
+ doc: string;
266
+ ver: number;
267
+ ok: string[];
268
+ failed: {
269
+ relay: string;
270
+ code: string;
271
+ }[];
272
+ merged: number;
273
+ }>;
274
+ /** One certificate per listed device: renewed when within 30 days of expiry (key holders renew, §2.3). */
275
+ private currentCerts;
276
+ /** Switches this device to a newer certificate for itself (renewal keeps did, spk and kpk). */
277
+ adoptCert(cert: string): Promise<void>;
278
+ /** A non-key-holding device takes a renewed certificate from its own document (§2.3 renewal by sync or document). */
279
+ adoptCertFromDoc(): Promise<void>;
280
+ nameOf(userId: string): Promise<string>;
281
+ /** `<petname or name>@<fp>` (§2.1: always with the fingerprint). */
282
+ addressOf(userId: string): Promise<string>;
283
+ /**
284
+ * A userId from what the user typed: a userId, a petname, a display
285
+ * address `name@xxxx-xxxx-xxxx-xxxx`, or a name when it is unique among
286
+ * known identities. Decisions always use the full userId (§2.1).
287
+ */
288
+ resolveUser(input: string): Promise<string>;
289
+ /** A cap minted by this device for our own mailboxes (any of our devices may issue one for itself). */
290
+ selfCap(): string;
291
+ /** Caps we hold that let us reach `to` with `kind`, by relay (best first). */
292
+ capsFor(to: string, kind: string, extra?: readonly string[]): Promise<Map<string, string>>;
293
+ /** Records a cap this device issued, so it can be revoked later (§5.3). */
294
+ recordIssued(capJws: string): Promise<StoredCapV3>;
295
+ /** Sends `revoke` for each cap to every relay it names. */
296
+ revokeCaps(caps: readonly StoredCapV3[]): Promise<number>;
297
+ /**
298
+ * Seals `plaintext` to every current device of `to` and sends each copy to
299
+ * up to 3 of their relays. Our own devices are reached with a cap this
300
+ * device mints; others with a held cap, or PoW for a cap-less `contact`.
301
+ */
302
+ deliver(to: string, kind: EnvelopeKindV3, session: {
303
+ own: string;
304
+ sid: string;
305
+ }, plaintext: object, opts?: {
306
+ refresh?: boolean;
307
+ extraCaps?: readonly string[];
308
+ hint?: readonly string[];
309
+ onlyDevices?: readonly string[];
310
+ }): Promise<Delivery>;
311
+ /** Delivers to our other devices, when there are any (sync, §4.3). */
312
+ syncOwn(body: Record<string, unknown>): Promise<Delivery | undefined>;
313
+ }
314
+ /**
315
+ * Does the held history cover every branch back to their common ancestor?
316
+ * (Each version above the common ancestry has its parents held.) Only then
317
+ * is revokedAlong complete for those branches (R3-M3).
318
+ */
319
+ export declare function branchesCovered(chain: IdentityChain, tips: readonly string[]): boolean;
320
+ /**
321
+ * After merging `tips` into `head`: the devices to re-add (listed by some
322
+ * pre-merge tip, dropped by the merge, not revoked along the head's
323
+ * ancestry, not denied after a jump), but only when the held history covers
324
+ * every branch; otherwise they are `skipped` (fail closed, R3-M3).
325
+ */
326
+ export declare function planReadd(chain: IdentityChain, tips: readonly string[], unionDevices: readonly string[], mergedDevices: readonly string[], head: string, denied: readonly string[]): {
327
+ readd: string[];
328
+ skipped: string[];
329
+ };
330
+ /** Publish failures worth retrying on every connect; anything else is an outright refusal. */
331
+ export declare const TRANSIENT_PUBLISH_CODES: readonly string[];
332
+ /**
333
+ * After a publish: transient failures go to `unpublished` (retried on every connect); outright
334
+ * refusals (e.g. `forbidden`) are timestamped in `refusedPublish` (retried at most hourly, or by an
335
+ * explicit `p2p identity republish`); relays that took it are cleared from both.
336
+ */
337
+ export declare function recordPublishOutcome(s: SelfDoc, ok: readonly string[], failed: readonly {
338
+ relay: string;
339
+ code: string;
340
+ }[], now: number): SelfDoc;
341
+ /** The relays to retry on connect: transient failures, and refusals older than `backoffMs`. */
342
+ export declare function relaysToRetry(s: SelfDoc, now: number, backoffMs: number): string[];
343
+ /** The relays a document lists, head's first, then any other tip's. */
344
+ export declare function orderRelays(head: readonly string[], all: readonly string[]): string[];
345
+ export declare function certDid(jws: string): string | undefined;
346
+ export declare function certExp(jws: string): number;
347
+ export interface CapPeek {
348
+ iss: string;
349
+ sub: string;
350
+ capid: string;
351
+ relays: string[];
352
+ kinds: string[];
353
+ exp: number;
354
+ }
355
+ /** A cap's payload, unverified (for routing our own sends; the relay and the issuer verify). */
356
+ export declare function capPeek(jws: string): CapPeek | undefined;
357
+ /** The co-member cap a join statement carries (§3.3, review M3). */
358
+ export declare function joinCapOf(statementJws: string): string | undefined;
359
+ /** A default display name from the OS user: `[a-z0-9._-]{1,32}`, else "me". */
360
+ export declare function defaultName(): string;
361
+ /** A device label: the host name, sanitized and capped at 64. */
362
+ export declare function defaultLabel(): string;
363
+ export declare function newKeysDoc(userId: string, device: DeviceKeysV3, cert: string, ik?: IdentityKey): KeysDoc;