@needmoretruth/nmts-cli 0.55.0 → 0.57.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 (49) hide show
  1. package/CHANGELOG.md +20 -0
  2. package/README.ko.md +9 -1
  3. package/README.md +9 -1
  4. package/dist/artifact-about.d.ts +1 -1
  5. package/dist/commands/put-wallet.js +11 -11
  6. package/dist/commands/receive.js +30 -87
  7. package/dist/commands/recovery-list.js +3 -2
  8. package/dist/commands/share.d.ts +7 -0
  9. package/dist/commands/share.js +58 -180
  10. package/dist/commands/shares-sent.js +4 -29
  11. package/dist/crypto-surface.d.ts +2 -0
  12. package/dist/crypto-surface.js +4 -7
  13. package/dist/links.d.ts +5 -0
  14. package/dist/links.js +6 -1
  15. package/dist/platform-sign.d.ts +7 -1
  16. package/dist/platform-sign.js +12 -1
  17. package/dist/portable.d.ts +4 -0
  18. package/dist/portable.js +5 -0
  19. package/dist/product.d.ts +1 -1
  20. package/dist/product.js +1 -1
  21. package/dist/recovery-assemble.d.ts +4 -11
  22. package/dist/recovery-assemble.js +25 -106
  23. package/dist/recovery-build.d.ts +7 -1
  24. package/dist/recovery-build.js +15 -11
  25. package/dist/recovery-seq.d.ts +2 -2
  26. package/dist/recovery-seq.js +30 -23
  27. package/dist/recovery.d.ts +92 -0
  28. package/dist/recovery.js +202 -0
  29. package/dist/sharing-inbox.d.ts +52 -0
  30. package/dist/sharing-inbox.js +146 -0
  31. package/dist/sharing.d.ts +52 -0
  32. package/dist/sharing.js +218 -0
  33. package/dist/upload-file.d.ts +2 -0
  34. package/dist/upload-file.js +6 -0
  35. package/dist/upload-recovery-patch.d.ts +65 -0
  36. package/dist/upload-recovery-patch.js +250 -0
  37. package/dist/upload-steps.js +4 -1
  38. package/dist/upload-wallet-plan.d.ts +4 -0
  39. package/dist/upload-wallet-plan.js +18 -8
  40. package/dist/upload-wallet-put.d.ts +6 -2
  41. package/dist/upload-wallet-put.js +30 -17
  42. package/dist/upload-wallet.d.ts +4 -1
  43. package/dist/upload-wallet.js +11 -3
  44. package/dist/upload-wire.d.ts +22 -0
  45. package/dist/upload-wire.js +2 -0
  46. package/dist/walrus-write.js +12 -0
  47. package/docs/commands/put.md +5 -1
  48. package/package.json +9 -1
  49. package/server.json +2 -2
@@ -0,0 +1,92 @@
1
+ import { type BuiltRecoveryList } from "./recovery-build.ts";
2
+ /** An account as the recovery doors need it. */
3
+ export interface RecoveryAccount {
4
+ readonly server: string;
5
+ /** What the one auth header carries: an API key or a delegation token. */
6
+ readonly bearer: string;
7
+ readonly code: string;
8
+ readonly accountId: string;
9
+ readonly network: string;
10
+ /** ⛔ base64url of the code's proof. Held for this call's requests and nothing else. */
11
+ readonly accountProof: string;
12
+ }
13
+ /** The storage-network copy's switch, whether a copy exists, and the list number to beat. */
14
+ export interface RecoveryCopyState {
15
+ enabled: boolean;
16
+ written: boolean;
17
+ seq: number;
18
+ }
19
+ /**
20
+ * The switch as the server holds it, or null from a server older than the narrow read.
21
+ *
22
+ * ⚠ NULL ONLY FOR 404 AND 405 — "this server has no such door". Any other refusal is thrown: a
23
+ * copy switched on and a read that failed are opposite facts, and folding one into the other
24
+ * would quietly stop uploads carrying the list.
25
+ */
26
+ export declare function readRecoveryCopy(account: Pick<RecoveryAccount, "server" | "bearer">): Promise<RecoveryCopyState | null>;
27
+ /** Turn the storage-network copy on or off. The code's proof travels with it. */
28
+ export declare function setRecoveryCopy(account: RecoveryAccount, on: boolean): Promise<void>;
29
+ /**
30
+ * The number the next list carries: one past the higher of the server's and this machine's own.
31
+ *
32
+ * ⛔ BOTH, NOT EITHER. The server's alone repeats a number whose record was swallowed; this
33
+ * machine's alone repeats one a browser already wrote (`recovery-seq.ts`).
34
+ */
35
+ export declare function nextRecoverySeq(account: Pick<RecoveryAccount, "server" | "bearer" | "accountId">): Promise<number>;
36
+ /** A file going up in the very quilt this list rides in, before the server has heard of it. */
37
+ export interface RidingFile {
38
+ /** Its patch's name in the quilt. The list's own-quilt placement points at it. */
39
+ identifier: string;
40
+ name: string;
41
+ parentId: string | null;
42
+ /** The file's real size. */
43
+ size: number;
44
+ /** What its sealed stream declares, which is more than `size` when the stream was padded. */
45
+ streamPlaintextLen: number;
46
+ dekWrapped: string;
47
+ contentHashCt: string;
48
+ }
49
+ export interface MakeOptions {
50
+ /** Ticks while the account is being read, so a large one does not look frozen. */
51
+ onProgress?: ((loaded: number) => void) | undefined;
52
+ /** Files riding in the quilt this list goes into. */
53
+ riding?: readonly RidingFile[] | undefined;
54
+ }
55
+ export interface MadeRecoveryList {
56
+ /** The `.nmtsmap` file: the name to offer it under, and its bytes. */
57
+ file: {
58
+ filename: string;
59
+ content: Uint8Array;
60
+ };
61
+ built: BuiltRecoveryList;
62
+ seq: number;
63
+ /** RFC3339, stamped before the account was read. What the server is told. */
64
+ capturedAt: string;
65
+ }
66
+ /**
67
+ * Read the account, build its recovery list, seal it, and wrap it as the `.nmtsmap` file.
68
+ *
69
+ * ⛔ THE CAPTURE TIME IS STAMPED BEFORE ANYTHING IS READ. The server keeps it to decide which files
70
+ * count as "not in your list yet"; stamped after the walk, a file made during the walk would be
71
+ * in neither the list nor the count.
72
+ *
73
+ * ⛔ THE DATA KEY IS DERIVED HERE AND WIPED HERE, on every path out, including the failing one.
74
+ */
75
+ export declare function makeRecoveryList(account: RecoveryAccount, options?: MakeOptions): Promise<MadeRecoveryList>;
76
+ /**
77
+ * Tell the server a list was written, and what number it carries.
78
+ *
79
+ * ⛔ THE ATTEMPT IS REMEMBERED WHETHER OR NOT IT LANDS (`recovery-seq.ts`): a number the server
80
+ * refused must not be the number the next run offers again.
81
+ *
82
+ * ⛔ A BLOB ID ONLY FOR A STORAGE-NETWORK COPY. Without one the record says `local`, and the server
83
+ * refuses an address beside `local` — a recorded address nothing was written to would let a
84
+ * screen advertise a copy that does not exist.
85
+ */
86
+ export declare function recordRecoveryList(account: RecoveryAccount, record: {
87
+ seq: number;
88
+ capturedAt?: string | undefined;
89
+ blobId?: string | undefined;
90
+ }): Promise<void>;
91
+ /** Note a number as offered before the bytes carrying it leave — skipping one is harmless, reusing one is not. */
92
+ export declare function noteOfferedSeq(accountId: string, seq: number): Promise<void>;
@@ -0,0 +1,202 @@
1
+ // The recovery list as a library: build it, record it, and read or flip its storage-network copy —
2
+ // for the command line, the SDK, and a page, with nothing in it that needs Node.
3
+ //
4
+ // ⛔ WHY IT IS ITS OWN ENTRY (spec 2026-09-20 unit B · owner 09-20 D1). An app built on NMTS had no
5
+ // way to hand its user the one file that outlives this service, or to say whether that file
6
+ // rides along on the storage network. `nmts recovery-list` could, and only from a terminal. The
7
+ // command is now a shell over `makeRecoveryList`, so the file an app gets is the file the
8
+ // command writes, byte for byte.
9
+ //
10
+ // ⛔ THE SAME FOR EVERY KEY HOLDER. `RecoveryAccount.bearer` is an API key or a delegation token,
11
+ // and `accountProof` is the code's proof (`authSecret`, NCF-3 §1). The server asks for both on
12
+ // the three recovery doors whichever credential arrives — a business holding a token cannot
13
+ // rebuild its user's whole index by itself, and nobody holding the code is refused.
14
+ //
15
+ // ⛔ NO `node:`, NO `Buffer`, NO `process.` — `test/portable-closure.test.ts` walks this file.
16
+ // State goes through `host().state` (`recovery-seq.ts`), bytes through `bytes.ts`.
17
+ import { request, ServerError } from "./api.js";
18
+ import { artifactAbout } from "./artifact-about.js";
19
+ import { utf8 } from "./bytes.js";
20
+ import { AAD, DERIVED, loadCrypto } from "./crypto.js";
21
+ import { NmtsError } from "./errors.js";
22
+ import { isRecord } from "./guards.js";
23
+ import { readFileList } from "./manifest.js";
24
+ import { BINARY_NAME } from "./product.js";
25
+ import { buildRecoveryList, openRaw, pathString } from "./recovery-build.js";
26
+ import { buildRecoveryMapFile } from "./recovery-map-file.js";
27
+ import { recoveryDocMeta } from "./recovery-map.js";
28
+ import { lastOfferedSeq, rememberOfferedSeq } from "./recovery-seq.js";
29
+ import { readAllRecoverySource } from "./recovery-source.js";
30
+ import { buildIndex, pathOf } from "./shared/lib/drive/manifest-index.js";
31
+ import { AGGREGATOR_HOSTS, suiRpcHost } from "./walrus.js";
32
+ const NETWORK_COPY = "/v1/account/recovery-network-copy";
33
+ /**
34
+ * The switch as the server holds it, or null from a server older than the narrow read.
35
+ *
36
+ * ⚠ NULL ONLY FOR 404 AND 405 — "this server has no such door". Any other refusal is thrown: a
37
+ * copy switched on and a read that failed are opposite facts, and folding one into the other
38
+ * would quietly stop uploads carrying the list.
39
+ */
40
+ export async function readRecoveryCopy(account) {
41
+ let reply;
42
+ try {
43
+ reply = await request(account.server, NETWORK_COPY, { token: account.bearer });
44
+ }
45
+ catch (error) {
46
+ if (error instanceof ServerError && (error.status === 404 || error.status === 405))
47
+ return null;
48
+ throw error;
49
+ }
50
+ if (!isRecord(reply) || typeof reply["enabled"] !== "boolean" || typeof reply["written"] !== "boolean") {
51
+ throw new NmtsError("This version cannot read the server's answer about the recovery list's storage copy.");
52
+ }
53
+ const seq = reply["seq"];
54
+ return {
55
+ enabled: reply["enabled"],
56
+ written: reply["written"],
57
+ seq: typeof seq === "number" && Number.isSafeInteger(seq) && seq > 0 ? seq : 0,
58
+ };
59
+ }
60
+ /** Turn the storage-network copy on or off. The code's proof travels with it. */
61
+ export async function setRecoveryCopy(account, on) {
62
+ await request(account.server, NETWORK_COPY, {
63
+ method: "PUT",
64
+ token: account.bearer,
65
+ accountProof: account.accountProof,
66
+ body: { enabled: on },
67
+ });
68
+ }
69
+ /**
70
+ * The number the next list carries: one past the higher of the server's and this machine's own.
71
+ *
72
+ * ⛔ BOTH, NOT EITHER. The server's alone repeats a number whose record was swallowed; this
73
+ * machine's alone repeats one a browser already wrote (`recovery-seq.ts`).
74
+ */
75
+ export async function nextRecoverySeq(account) {
76
+ const server = await readRecoveryCopy(account);
77
+ return Math.max(server?.seq ?? 0, await lastOfferedSeq(account.accountId)) + 1;
78
+ }
79
+ /**
80
+ * Where the bytes this list points at live, for the sealed self-description.
81
+ *
82
+ * ⚠ `aggregators` AND `chain_rpc` ARE HINTS — the first thing in the file to go stale, read by a
83
+ * reader after its own defaults. `chain` is the one that changes behaviour: a blob id from
84
+ * testnet and one from mainnet are the same kind of string.
85
+ */
86
+ function storageFor(network) {
87
+ return {
88
+ network: "walrus",
89
+ chain: network,
90
+ aggregators: [...(AGGREGATOR_HOSTS[network] ?? [])],
91
+ chain_rpc: suiRpcHost(network),
92
+ };
93
+ }
94
+ /**
95
+ * Read the account, build its recovery list, seal it, and wrap it as the `.nmtsmap` file.
96
+ *
97
+ * ⛔ THE CAPTURE TIME IS STAMPED BEFORE ANYTHING IS READ. The server keeps it to decide which files
98
+ * count as "not in your list yet"; stamped after the walk, a file made during the walk would be
99
+ * in neither the list nor the count.
100
+ *
101
+ * ⛔ THE DATA KEY IS DERIVED HERE AND WIPED HERE, on every path out, including the failing one.
102
+ */
103
+ export async function makeRecoveryList(account, options = {}) {
104
+ const capturedAt = new Date().toISOString();
105
+ const list = await readFileList(account.server, account.bearer, account.code, account.accountId);
106
+ // ⛔ A FIRST UPLOAD HAS NO LIST YET, and the file riding with it is then the whole account — the
107
+ // browser's first upload copies the same one-file list (found 2026-11-10 on testnet).
108
+ const riding = options.riding ?? [];
109
+ if (list.manifest === null && riding.length === 0) {
110
+ throw new NmtsError("This account has no file list, so nothing can be described.", {
111
+ exitCode: 4,
112
+ nextStep: `Nothing was written. A recovery list says what each stored file is CALLED and which key ` +
113
+ `opens it, and both come from the account's own file list — the storage network holds ` +
114
+ `neither. Run \`${BINARY_NAME} rebuild\` if this account has stored files but no list.`,
115
+ });
116
+ }
117
+ const entries = list.manifest?.entries ?? [];
118
+ const source = await readAllRecoverySource({
119
+ server: account.server,
120
+ apiKey: account.bearer,
121
+ accountProof: account.accountProof,
122
+ onProgress: options.onProgress,
123
+ });
124
+ const generatedAt = new Date().toISOString();
125
+ const seq = await nextRecoverySeq(account);
126
+ const crypt = await loadCrypto();
127
+ const derived = crypt.kdf_derive(crypt.account_code_parse(account.code));
128
+ const dataKey = derived.slice(DERIVED.dataKey[0], DERIVED.dataKey[1]);
129
+ derived.fill(0);
130
+ let built;
131
+ try {
132
+ const index = buildIndex(entries);
133
+ const pending = riding.map((file) => {
134
+ const parent = file.parentId === null ? undefined : index.byId.get(file.parentId);
135
+ const part = { part_index: 0, plaintext_len: file.size, network: "walrus" };
136
+ return {
137
+ id: file.identifier,
138
+ name: file.name,
139
+ path: pathString(parent === undefined ? [] : [...pathOf(index, parent), parent.name]),
140
+ size: file.size,
141
+ dek: openRaw(crypt, dataKey, file.dekWrapped, AAD.dekWrap, `the key of ${file.name}`),
142
+ contentHash: openRaw(crypt, dataKey, file.contentHashCt, AAD.contentHash, `the content hash of ${file.name}`),
143
+ // This file is being written now, so "now" is its real date; the next list rewrites it.
144
+ createdAt: generatedAt,
145
+ updatedAt: generatedAt,
146
+ parts: [file.streamPlaintextLen > file.size ? { ...part, padded_len: file.streamPlaintextLen } : part],
147
+ quilt: { identifier: file.identifier },
148
+ };
149
+ });
150
+ built = buildRecoveryList({
151
+ crypt,
152
+ dataKey,
153
+ accountId: account.accountId,
154
+ entries,
155
+ source,
156
+ seq,
157
+ generatedAt,
158
+ pending,
159
+ meta: recoveryDocMeta(artifactAbout("recovery-list"), storageFor(account.network)),
160
+ });
161
+ }
162
+ finally {
163
+ dataKey.fill(0);
164
+ }
165
+ const file = buildRecoveryMapFile({
166
+ accountId: account.accountId,
167
+ seq,
168
+ generatedAt,
169
+ // ⛔ READ BACK OUT OF THE DOCUMENT: from NRM-3 on the number depends on what is in it.
170
+ nrm: built.doc.v,
171
+ sealed: built.sealed,
172
+ });
173
+ return { file: { filename: file.filename, content: utf8(file.content) }, built, seq, capturedAt };
174
+ }
175
+ /**
176
+ * Tell the server a list was written, and what number it carries.
177
+ *
178
+ * ⛔ THE ATTEMPT IS REMEMBERED WHETHER OR NOT IT LANDS (`recovery-seq.ts`): a number the server
179
+ * refused must not be the number the next run offers again.
180
+ *
181
+ * ⛔ A BLOB ID ONLY FOR A STORAGE-NETWORK COPY. Without one the record says `local`, and the server
182
+ * refuses an address beside `local` — a recorded address nothing was written to would let a
183
+ * screen advertise a copy that does not exist.
184
+ */
185
+ export async function recordRecoveryList(account, record) {
186
+ await rememberOfferedSeq(account.accountId, record.seq);
187
+ const body = record.blobId === undefined
188
+ ? { kind: "local", seq: record.seq }
189
+ : { kind: "walrus", seq: record.seq, blob_id: record.blobId };
190
+ if (record.capturedAt !== undefined)
191
+ body["captured_at"] = record.capturedAt;
192
+ await request(account.server, "/v1/account/recovery-map", {
193
+ method: "PUT",
194
+ token: account.bearer,
195
+ accountProof: account.accountProof,
196
+ body,
197
+ });
198
+ }
199
+ /** Note a number as offered before the bytes carrying it leave — skipping one is harmless, reusing one is not. */
200
+ export async function noteOfferedSeq(accountId, seq) {
201
+ await rememberOfferedSeq(accountId, seq);
202
+ }
@@ -0,0 +1,52 @@
1
+ import type { PlaintextSink } from "./download-sink.ts";
2
+ import type { ShareAccount } from "./sharing.ts";
3
+ import type { ReadOptions } from "./walrus.ts";
4
+ /** One received share, opened as far as it would open. Its key never leaves this module. */
5
+ export interface ReceivedShare {
6
+ id: string;
7
+ itemId: string;
8
+ /** The file's name and real length as the sender sealed them; null when the row would not open. */
9
+ name: string | null;
10
+ size: number | null;
11
+ /** The sender's public code — only ever set when the open SUCCEEDED. */
12
+ sender: string | null;
13
+ senderRevoked: boolean;
14
+ /** Which of this account's codes it came to, and that code readable. */
15
+ toIndex: number;
16
+ toCode: string;
17
+ createdAt: string;
18
+ /** Why it did not open, for a row that is still listed. */
19
+ problem: string | null;
20
+ }
21
+ /** A received file, written to the sink and checked against the sender's hash. */
22
+ export interface ReceivedFile {
23
+ id: string;
24
+ name: string;
25
+ bytes: number;
26
+ parts: number;
27
+ sender: string | null;
28
+ senderRevoked: boolean;
29
+ toIndex: number;
30
+ toCode: string;
31
+ /** False when the sink took the bytes but did not deliver them (it decides what that means). */
32
+ delivered: boolean;
33
+ }
34
+ /**
35
+ * Everything shared with this account, each row opened with the code it came to.
36
+ *
37
+ * ⛔ A ROW THAT WILL NOT OPEN IS STILL LISTED, with its reason and no sender. `total` can be larger
38
+ * than the list: the server's listing is bounded, and a caller that is not told cannot know.
39
+ */
40
+ export declare function receivedShares(account: ShareAccount): Promise<{
41
+ shares: ReceivedShare[];
42
+ total: number;
43
+ }>;
44
+ /**
45
+ * Receive one share into a sink: find its row in the list, open it, check what it says, then fetch.
46
+ * `sinkFor` is asked once the name and real length are known — the command picks a path from the
47
+ * sender's name there, and a program its own destination.
48
+ */
49
+ export declare function receiveShare(account: ShareAccount, id: string, sinkFor: (file: {
50
+ name: string;
51
+ size: number;
52
+ }) => PlaintextSink, read?: ReadOptions): Promise<ReceivedFile>;
@@ -0,0 +1,146 @@
1
+ // The inbox half of `sharing.ts`: what came in, and taking one of them. Its own file because
2
+ // `check:size` measures the sending half; `./share` re-exports both, so a caller sees one entry.
3
+ //
4
+ // ⛔ RECEIVING FINDS THE ROW IN THE LIST, not by id: the list is what carries the sender's published
5
+ // identity, and without it the envelope cannot be authenticated. The length written is the one
6
+ // the sender sealed, never the server's stored size (`commands/receive.ts` says why).
7
+ //
8
+ // ⛔ NO `node:`, NO `Buffer`, NO `process.` — `test/portable-closure.test.ts` walks this file.
9
+ import { request } from "./api.js";
10
+ import { loadCrypto } from "./crypto.js";
11
+ import { fetchWithKey } from "./download.js";
12
+ import { NmtsError } from "./errors.js";
13
+ import { BINARY_NAME } from "./product.js";
14
+ import { openReceived, openSharedDigest, receivedIndex } from "./share.js";
15
+ import { shareKeyRing } from "./share-codes.js";
16
+ /** The raw rows of `GET /v1/shares/received`, read field by field. */
17
+ async function receivedRows(account) {
18
+ const value = await request(account.server, "/v1/shares/received", { token: account.bearer });
19
+ if (typeof value !== "object" || value === null)
20
+ throw new NmtsError("The server's answer was not an object.");
21
+ const rows = Reflect.get(value, "shares");
22
+ if (!Array.isArray(rows))
23
+ throw new NmtsError("The server did not list any shares.");
24
+ const total = Reflect.get(value, "total");
25
+ return { rows: rows.filter(isRow), total: typeof total === "number" ? total : rows.length };
26
+ }
27
+ function isRow(value) {
28
+ if (typeof value !== "object" || value === null)
29
+ return false;
30
+ for (const name of ["id", "item_id", "dek_share_ct", "name_share_ct", "content_hash_share_ct"]) {
31
+ if (typeof Reflect.get(value, name) !== "string")
32
+ return false;
33
+ }
34
+ return true;
35
+ }
36
+ /**
37
+ * Everything shared with this account, each row opened with the code it came to.
38
+ *
39
+ * ⛔ A ROW THAT WILL NOT OPEN IS STILL LISTED, with its reason and no sender. `total` can be larger
40
+ * than the list: the server's listing is bounded, and a caller that is not told cannot know.
41
+ */
42
+ export async function receivedShares(account) {
43
+ const crypt = await loadCrypto();
44
+ const ring = shareKeyRing(crypt, account.code);
45
+ try {
46
+ const received = await receivedRows(account);
47
+ const shares = received.rows.map((row) => {
48
+ const o = openReceived(crypt, ring.at(receivedIndex(row)), row);
49
+ o.dek?.fill(0);
50
+ return {
51
+ id: o.id,
52
+ itemId: o.itemId,
53
+ name: o.name,
54
+ size: o.size,
55
+ sender: o.sender,
56
+ senderRevoked: o.senderRevoked,
57
+ toIndex: o.toIndex,
58
+ toCode: ring.at(o.toIndex).display,
59
+ createdAt: o.createdAt,
60
+ problem: o.problem,
61
+ };
62
+ });
63
+ return { shares, total: received.total };
64
+ }
65
+ finally {
66
+ ring.wipe();
67
+ }
68
+ }
69
+ /**
70
+ * Receive one share into a sink: find its row in the list, open it, check what it says, then fetch.
71
+ * `sinkFor` is asked once the name and real length are known — the command picks a path from the
72
+ * sender's name there, and a program its own destination.
73
+ */
74
+ export async function receiveShare(account, id, sinkFor, read) {
75
+ const crypt = await loadCrypto();
76
+ const ring = shareKeyRing(crypt, account.code);
77
+ try {
78
+ const answer = await request(account.server, "/v1/shares/received", { token: account.bearer });
79
+ const rows = typeof answer === "object" && answer !== null ? Reflect.get(answer, "shares") : null;
80
+ if (!Array.isArray(rows))
81
+ throw new NmtsError("The server did not list any shares.");
82
+ const row = rows.find((candidate) => typeof candidate === "object" && candidate !== null && Reflect.get(candidate, "id") === id);
83
+ if (row === undefined) {
84
+ throw new NmtsError(`Nothing shared with this account has the id "${id}".`, {
85
+ exitCode: 4,
86
+ nextStep: `Nothing was written. \`${BINARY_NAME} shares\` lists what is there.`,
87
+ });
88
+ }
89
+ const opened = openReceived(crypt, ring.at(receivedIndex(row)), row);
90
+ if (opened.dek === null || opened.name === null) {
91
+ throw new NmtsError(`That share ${opened.problem ?? "did not open"}.`, {
92
+ exitCode: 1,
93
+ nextStep: "Nothing was written. The sender can withdraw it and share the file again.",
94
+ });
95
+ }
96
+ if (opened.size === null) {
97
+ opened.dek.fill(0);
98
+ throw new NmtsError("The sender did not record this file's real length.", {
99
+ exitCode: 1,
100
+ nextStep: "Nothing was written. It was shared by an older client; ask them to share it again from " +
101
+ "a current one, which seals the length beside the name.",
102
+ });
103
+ }
104
+ const expected = openSharedDigest(crypt, opened.dek, opened.digestCt);
105
+ if (expected === null) {
106
+ opened.dek.fill(0);
107
+ throw new NmtsError("This share's content hash did not open with the file's own key.", {
108
+ exitCode: 1,
109
+ nextStep: "Nothing was written. There would be nothing to check the bytes against.",
110
+ });
111
+ }
112
+ let sink;
113
+ try {
114
+ sink = sinkFor({ name: opened.name, size: opened.size });
115
+ }
116
+ catch (error) {
117
+ opened.dek.fill(0);
118
+ throw error;
119
+ }
120
+ const fetched = await fetchWithKey({
121
+ base: account.server,
122
+ apiKey: account.bearer,
123
+ descriptorPath: `/v1/shares/${encodeURIComponent(id)}/parts?for=download`,
124
+ size: opened.size,
125
+ dek: opened.dek,
126
+ expected,
127
+ chain: account.network,
128
+ ...(read === undefined ? {} : { read }),
129
+ sink,
130
+ });
131
+ return {
132
+ id,
133
+ name: opened.name,
134
+ bytes: fetched.byteCount,
135
+ parts: fetched.partCount,
136
+ sender: opened.sender,
137
+ senderRevoked: opened.senderRevoked,
138
+ toIndex: opened.toIndex,
139
+ toCode: ring.at(opened.toIndex).display,
140
+ delivered: fetched.delivered,
141
+ };
142
+ }
143
+ finally {
144
+ ring.wipe();
145
+ }
146
+ }
@@ -0,0 +1,52 @@
1
+ import type { ManifestEntry } from "./shared/lib/drive/manifest-codec.ts";
2
+ export { addressFromTyped } from "./share.ts";
3
+ export { receivedShares, receiveShare } from "./sharing-inbox.ts";
4
+ export type { ReceivedFile, ReceivedShare } from "./sharing-inbox.ts";
5
+ /** An account as the share doors take it. */
6
+ export interface ShareAccount {
7
+ readonly server: string;
8
+ /** What the one auth header carries: an API key or a delegation token. */
9
+ readonly bearer: string;
10
+ readonly code: string;
11
+ readonly accountId: string;
12
+ /** The chain the stored parts are on, as `resolveNetwork` answers it. */
13
+ readonly network: string;
14
+ /** ⛔ base64url of the code's proof, for the two sending doors only. Absent: not sent. */
15
+ readonly accountProof?: string | undefined;
16
+ }
17
+ /** One file of the account's list that a share can carry, and its full path. */
18
+ export interface ShareableFile {
19
+ readonly entry: ManifestEntry & {
20
+ dekWrapped: string;
21
+ contentHashCt: string;
22
+ };
23
+ readonly path: string;
24
+ }
25
+ /** What a sent share is: its id, the file's name, who it went to and from which code. */
26
+ export interface SentShare {
27
+ id: string | null;
28
+ name: string;
29
+ /** The recipient's public code, readable form. */
30
+ recipient: string;
31
+ fromIndex: number;
32
+ }
33
+ /** The file at `target`, checked for the key and the hash a share carries. */
34
+ export declare function shareableFileAt(account: ShareAccount, target: string): Promise<ShareableFile>;
35
+ /**
36
+ * Seal one file for one public code and send it.
37
+ *
38
+ * ⛔ THE CALLER HAS ALREADY DECIDED. The command asks its person between `shareableFileAt` and this
39
+ * call; a program decides by calling. Nothing here asks anybody anything.
40
+ */
41
+ export declare function sendShare(account: ShareAccount, file: ShareableFile, recipientAddress: Uint8Array, options?: {
42
+ as?: number | undefined;
43
+ onFirstCode?: () => void;
44
+ }): Promise<SentShare>;
45
+ /** Who one file was shared with: the server's rows, unchanged, beside the file's path and id. */
46
+ export declare function sentShares(account: ShareAccount, target: string): Promise<{
47
+ path: string;
48
+ itemId: string;
49
+ rows: unknown[];
50
+ }>;
51
+ /** Withdraw a share this account sent, or remove one it was sent. A copy already taken stays taken. */
52
+ export declare function removeShare(account: ShareAccount, id: string): Promise<void>;