@needmoretruth/nmts-cli 0.54.2 → 0.56.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.
- package/CHANGELOG.md +22 -0
- package/README.ko.md +9 -1
- package/README.md +9 -1
- package/dist/artifact-about.d.ts +1 -1
- package/dist/commands/push.js +1 -1
- package/dist/commands/put-wallet.js +12 -12
- package/dist/commands/put.js +1 -1
- package/dist/commands/recovery-list.js +3 -2
- package/dist/crypto-surface.d.ts +2 -0
- package/dist/crypto-surface.js +4 -7
- package/dist/manifest.js +3 -0
- package/dist/node-read.d.ts +2 -0
- package/dist/node-read.js +24 -0
- package/dist/portable.d.ts +2 -0
- package/dist/portable.js +3 -0
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/read-race.d.ts +35 -0
- package/dist/read-race.js +91 -0
- package/dist/recovery-assemble.d.ts +4 -11
- package/dist/recovery-assemble.js +25 -106
- package/dist/recovery-build.d.ts +7 -1
- package/dist/recovery-build.js +15 -11
- package/dist/recovery-seq.d.ts +2 -2
- package/dist/recovery-seq.js +30 -23
- package/dist/recovery.d.ts +92 -0
- package/dist/recovery.js +202 -0
- package/dist/road-hosts.generated.d.ts +4 -0
- package/dist/road-hosts.generated.js +284 -0
- package/dist/shared/lib/upload/wallet-storage-estimate.d.ts +16 -0
- package/dist/shared/lib/upload/wallet-storage-estimate.js +44 -0
- package/dist/shared/lib/wallet/graphql-history.d.ts +7 -2
- package/dist/shared/lib/wallet/graphql-history.js +7 -2
- package/dist/shared/lib/walrus/credit-tip.d.ts +24 -0
- package/dist/shared/lib/walrus/credit-tip.js +61 -0
- package/dist/shared/lib/walrus/node-certificate.d.ts +18 -0
- package/dist/shared/lib/walrus/node-certificate.js +77 -0
- package/dist/shared/lib/walrus/node-quorum.d.ts +17 -0
- package/dist/shared/lib/walrus/node-quorum.js +103 -0
- package/dist/shared/lib/walrus/tip-config.d.ts +33 -0
- package/dist/shared/lib/walrus/tip-config.js +68 -0
- package/dist/upload-file.d.ts +2 -0
- package/dist/upload-file.js +6 -0
- package/dist/upload-recovery-patch.d.ts +65 -0
- package/dist/upload-recovery-patch.js +250 -0
- package/dist/upload-road.d.ts +26 -4
- package/dist/upload-road.js +79 -17
- package/dist/upload-steps.js +5 -1
- package/dist/upload-wallet-chain.d.ts +1 -1
- package/dist/upload-wallet-chain.js +11 -4
- package/dist/upload-wallet-plan.d.ts +4 -0
- package/dist/upload-wallet-plan.js +18 -8
- package/dist/upload-wallet-put.d.ts +6 -2
- package/dist/upload-wallet-put.js +30 -17
- package/dist/upload-wallet.d.ts +4 -1
- package/dist/upload-wallet.js +12 -3
- package/dist/upload-wire.d.ts +27 -0
- package/dist/upload-wire.js +2 -0
- package/dist/upload.js +3 -3
- package/dist/walrus-write.js +80 -3
- package/dist/walrus.d.ts +20 -4
- package/dist/walrus.js +115 -36
- package/docs/commands/put.md +5 -1
- package/package.json +5 -1
- package/server.json +2 -2
package/dist/recovery-build.d.ts
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
import { type CryptoGlue } from "./crypto.ts";
|
|
2
|
-
import { type RecoveryDocMetaDraft, type RecoveryListDoc } from "./recovery-map.ts";
|
|
2
|
+
import { type ManifestItemInput, type RecoveryDocMetaDraft, type RecoveryListDoc } from "./recovery-map.ts";
|
|
3
3
|
import { type SourceItem } from "./recovery-source.ts";
|
|
4
4
|
import type { ManifestEntry } from "./shared/lib/drive/manifest-codec.ts";
|
|
5
5
|
/** Path of the account root. Every file sits at this or below it. */
|
|
6
6
|
export declare const ROOT_PATH = "/";
|
|
7
7
|
/** Folder names, root-down, as the `/a/b` string the format stores. Empty means the root. */
|
|
8
8
|
export declare function pathString(names: readonly string[]): string;
|
|
9
|
+
/** base64url of the RAW bytes inside one envelope sealed under the account's data key. */
|
|
10
|
+
export declare function openRaw(crypt: CryptoGlue, dataKey: Uint8Array, envelope: string, aad: string, what: string): string;
|
|
9
11
|
export interface BuildRecoveryListInput {
|
|
10
12
|
crypt: CryptoGlue;
|
|
11
13
|
/** ⛔ BORROWED, NOT KEPT. The caller derived it and the caller wipes it. */
|
|
@@ -19,6 +21,10 @@ export interface BuildRecoveryListInput {
|
|
|
19
21
|
seq: number;
|
|
20
22
|
/** RFC3339, stamped by the caller before the walk began. */
|
|
21
23
|
generatedAt: string;
|
|
24
|
+
/** Files riding in the quilt this list goes into (NRM-3 own-quilt form), appended after the rest. */
|
|
25
|
+
pending?: readonly ManifestItemInput[] | undefined;
|
|
26
|
+
/** The storage-network copy this one follows, when it is itself such a copy. */
|
|
27
|
+
prevBlobId?: string | null | undefined;
|
|
22
28
|
meta: RecoveryDocMetaDraft;
|
|
23
29
|
}
|
|
24
30
|
export interface BuiltRecoveryList {
|
package/dist/recovery-build.js
CHANGED
|
@@ -21,6 +21,7 @@
|
|
|
21
21
|
// ⛔ TWO SOURCES, JOINED ON ID. The server is asked only for what it still owns — a file's storage
|
|
22
22
|
// parts. Its NAME, its folder, its size and its key envelope come from the account's sealed file
|
|
23
23
|
// list, because that is the only place the tree exists.
|
|
24
|
+
import { fromBase64Url, fromUtf8, toBase64Url } from "./bytes.js";
|
|
24
25
|
import { AAD, DERIVED } from "./crypto.js";
|
|
25
26
|
import { NmtsError } from "./errors.js";
|
|
26
27
|
import { buildRecoveryListDoc, RecoveryListProblem, withTotals, } from "./recovery-map.js";
|
|
@@ -96,10 +97,10 @@ function describeAll(entries) {
|
|
|
96
97
|
return out;
|
|
97
98
|
}
|
|
98
99
|
/** base64url of the RAW bytes inside one envelope sealed under the account's data key. */
|
|
99
|
-
function openRaw(crypt, dataKey, envelope, aad, what) {
|
|
100
|
+
export function openRaw(crypt, dataKey, envelope, aad, what) {
|
|
100
101
|
let opened;
|
|
101
102
|
try {
|
|
102
|
-
opened = crypt.envelope_open(dataKey, utf8.encode(aad),
|
|
103
|
+
opened = crypt.envelope_open(dataKey, utf8.encode(aad), fromBase64Url(envelope));
|
|
103
104
|
}
|
|
104
105
|
catch {
|
|
105
106
|
// ⛔ The engine's own message is not repeated: it can quote what it was given.
|
|
@@ -107,7 +108,7 @@ function openRaw(crypt, dataKey, envelope, aad, what) {
|
|
|
107
108
|
`The recovery list was not written.`);
|
|
108
109
|
}
|
|
109
110
|
try {
|
|
110
|
-
return
|
|
111
|
+
return toBase64Url(opened);
|
|
111
112
|
}
|
|
112
113
|
finally {
|
|
113
114
|
opened.fill(0);
|
|
@@ -213,13 +214,16 @@ export function buildRecoveryList(input) {
|
|
|
213
214
|
items.push(item);
|
|
214
215
|
totalBytes += about.size;
|
|
215
216
|
}
|
|
217
|
+
for (const riding of input.pending ?? []) {
|
|
218
|
+
items.push(riding);
|
|
219
|
+
totalBytes += riding.size;
|
|
220
|
+
}
|
|
216
221
|
const doc = buildRecoveryListDoc({
|
|
217
222
|
seq: input.seq,
|
|
218
|
-
// ⛔
|
|
219
|
-
// address, and a list
|
|
220
|
-
//
|
|
221
|
-
|
|
222
|
-
prevBlobId: null,
|
|
223
|
+
// ⛔ NULL FOR A LIST ON SOMEBODY'S DISK. `prev_manifest_blob_id` chains MIRRORS: it is a
|
|
224
|
+
// storage-network address, and a local list has none for a successor to point at. `seq`
|
|
225
|
+
// still increases across every list of either kind, so "which is newest" stays answerable.
|
|
226
|
+
prevBlobId: input.prevBlobId ?? null,
|
|
223
227
|
generatedAt: input.generatedAt,
|
|
224
228
|
accountId: input.accountId,
|
|
225
229
|
// Finished here because here is where they are first true: everything before this point could
|
|
@@ -229,13 +233,13 @@ export function buildRecoveryList(input) {
|
|
|
229
233
|
items,
|
|
230
234
|
});
|
|
231
235
|
const json = JSON.stringify(doc);
|
|
232
|
-
const sealed =
|
|
236
|
+
const sealed = toBase64Url(input.crypt.envelope_seal(input.dataKey, utf8.encode(AAD.recoveryMap), utf8.encode(json)));
|
|
233
237
|
// ⛔ READ IT BACK BEFORE ANYBODY IS TOLD IT EXISTS. Catches a truncated seal, a wrong key, and
|
|
234
238
|
// any future change that returns something other than what it just sealed. Without it, "your
|
|
235
239
|
// list covers 412 files" is an assumption rather than a measurement.
|
|
236
240
|
let reopened;
|
|
237
241
|
try {
|
|
238
|
-
reopened = input.crypt.envelope_open(input.dataKey, utf8.encode(AAD.recoveryMap),
|
|
242
|
+
reopened = input.crypt.envelope_open(input.dataKey, utf8.encode(AAD.recoveryMap), fromBase64Url(sealed));
|
|
239
243
|
}
|
|
240
244
|
catch {
|
|
241
245
|
throw new NmtsError("The sealed recovery list did not open again on this machine.", {
|
|
@@ -243,7 +247,7 @@ export function buildRecoveryList(input) {
|
|
|
243
247
|
nextStep: "Nothing was written. This is a fault in the tool rather than in the account.",
|
|
244
248
|
});
|
|
245
249
|
}
|
|
246
|
-
if (
|
|
250
|
+
if (fromUtf8(reopened) !== json) {
|
|
247
251
|
throw new NmtsError("The sealed recovery list did not come back as what was sealed.", {
|
|
248
252
|
exitCode: 1,
|
|
249
253
|
nextStep: "Nothing was written. This is a fault in the tool rather than in the account.",
|
package/dist/recovery-seq.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
/** The highest sequence this machine has offered for an account, or 0 when it has offered none. */
|
|
2
|
-
export declare function lastOfferedSeq(accountId: string): number
|
|
2
|
+
export declare function lastOfferedSeq(accountId: string): Promise<number>;
|
|
3
3
|
/**
|
|
4
4
|
* Write down a sequence this machine OFFERED — whether or not the server took it.
|
|
5
5
|
*
|
|
@@ -7,4 +7,4 @@ export declare function lastOfferedSeq(accountId: string): number;
|
|
|
7
7
|
* refused number the number the next run offers again, and every run after it: the same
|
|
8
8
|
* refusal for ever. Recording the attempt is what makes a second run get past it.
|
|
9
9
|
*/
|
|
10
|
-
export declare function rememberOfferedSeq(accountId: string, seq: number): void
|
|
10
|
+
export declare function rememberOfferedSeq(accountId: string, seq: number): Promise<void>;
|
package/dist/recovery-seq.js
CHANGED
|
@@ -1,43 +1,51 @@
|
|
|
1
1
|
// Which recovery list this machine wrote last, so the next one gets a higher number.
|
|
2
2
|
//
|
|
3
|
-
// ⛔ WHY IT IS REMEMBERED HERE
|
|
4
|
-
// `GET /v1/account
|
|
5
|
-
//
|
|
6
|
-
//
|
|
3
|
+
// ⛔ WHY IT IS STILL REMEMBERED HERE. The account's recorded number is said by
|
|
4
|
+
// `GET /v1/account/recovery-network-copy` (spec 2026-09-20 unit B), and `recovery.ts` asks it
|
|
5
|
+
// first. A server older than that door answers 404 or 405, and a record the server refused or
|
|
6
|
+
// never received still has to push the next number past it. So the two are put together: the
|
|
7
|
+
// next list carries the higher of the two, plus one.
|
|
7
8
|
//
|
|
8
|
-
// ⛔ WHAT
|
|
9
|
-
//
|
|
10
|
-
// server has already passed, the server refuses it — that is what its `seq`
|
|
11
|
-
// the refusal is reported rather than worked around. The attempt is recorded
|
|
12
|
-
// second run offers a higher number and the two converge instead of looping.
|
|
9
|
+
// ⛔ WHAT A MISSING SERVER NUMBER COSTS, SAID PLAINLY. A list written from a browser moves the
|
|
10
|
+
// server's number without moving this one. Against an older server the next run here then
|
|
11
|
+
// offers a number the server has already passed, the server refuses it — that is what its `seq`
|
|
12
|
+
// guard is for — and the refusal is reported rather than worked around. The attempt is recorded
|
|
13
|
+
// either way, so a second run offers a higher number and the two converge instead of looping.
|
|
14
|
+
//
|
|
15
|
+
// ⛔ IT GOES THROUGH THE HOST'S STATE, so the same counter runs in a page. On Node a key without a
|
|
16
|
+
// slash is one file directly under the config directory — the same `recovery-seq-<id>.json`,
|
|
17
|
+
// with the same text, that this module wrote with `node:fs` before (`host-node.ts::placeOf`).
|
|
13
18
|
//
|
|
14
19
|
// ⛔ IT IS NOT A SECRET AND IT IS STILL WRITTEN 0600. It holds an account id and a counter. The
|
|
15
20
|
// mode matches every other file this tool keeps beside it: one mode is easier to keep right
|
|
16
21
|
// than two.
|
|
17
|
-
import {
|
|
18
|
-
import { join } from "node:path";
|
|
19
|
-
import { configDir } from "./credentials.js";
|
|
22
|
+
import { fromUtf8, utf8 } from "./bytes.js";
|
|
20
23
|
import { NmtsError } from "./errors.js";
|
|
21
24
|
import { isRecord } from "./guards.js";
|
|
25
|
+
import { host } from "./host.js";
|
|
22
26
|
/**
|
|
23
|
-
*
|
|
27
|
+
* The state key one account's counter lives under.
|
|
24
28
|
*
|
|
25
|
-
* ⛔ AN ACCOUNT ID BECOMES PART OF A
|
|
26
|
-
* tool has comes from its own derivation and is base64url, but a value that reaches a path
|
|
29
|
+
* ⛔ AN ACCOUNT ID BECOMES PART OF A FILE NAME HERE, so it is CHECKED rather than trusted. Every id
|
|
30
|
+
* this tool has comes from its own derivation and is base64url, but a value that reaches a path
|
|
27
31
|
* unchecked is how `..` becomes a write somewhere else, and the check costs one line.
|
|
28
32
|
*/
|
|
29
|
-
function
|
|
33
|
+
function seqKey(accountId) {
|
|
30
34
|
if (!/^[A-Za-z0-9_-]{1,64}$/.test(accountId)) {
|
|
31
35
|
throw new NmtsError("That is not an account id this tool derived.", {
|
|
32
36
|
nextStep: "Nothing was written. This is a fault in the tool rather than in the account.",
|
|
33
37
|
});
|
|
34
38
|
}
|
|
35
|
-
return
|
|
39
|
+
return `recovery-seq-${accountId}.json`;
|
|
36
40
|
}
|
|
37
41
|
/** The highest sequence this machine has offered for an account, or 0 when it has offered none. */
|
|
38
|
-
export function lastOfferedSeq(accountId) {
|
|
42
|
+
export async function lastOfferedSeq(accountId) {
|
|
43
|
+
const key = seqKey(accountId);
|
|
39
44
|
try {
|
|
40
|
-
const
|
|
45
|
+
const bytes = await host().state.read(key);
|
|
46
|
+
if (bytes === undefined)
|
|
47
|
+
return 0;
|
|
48
|
+
const parsed = JSON.parse(fromUtf8(bytes));
|
|
41
49
|
if (!isRecord(parsed))
|
|
42
50
|
return 0;
|
|
43
51
|
const seq = parsed["seq"];
|
|
@@ -55,9 +63,8 @@ export function lastOfferedSeq(accountId) {
|
|
|
55
63
|
* refused number the number the next run offers again, and every run after it: the same
|
|
56
64
|
* refusal for ever. Recording the attempt is what makes a second run get past it.
|
|
57
65
|
*/
|
|
58
|
-
export function rememberOfferedSeq(accountId, seq) {
|
|
59
|
-
if (seq <= lastOfferedSeq(accountId))
|
|
66
|
+
export async function rememberOfferedSeq(accountId, seq) {
|
|
67
|
+
if (seq <= (await lastOfferedSeq(accountId)))
|
|
60
68
|
return;
|
|
61
|
-
|
|
62
|
-
writeFileSync(seqPath(accountId), `${JSON.stringify({ seq }, null, 2)}\n`, { mode: 0o600 });
|
|
69
|
+
await host().state.write(seqKey(accountId), utf8(`${JSON.stringify({ seq }, null, 2)}\n`));
|
|
63
70
|
}
|
|
@@ -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>;
|
package/dist/recovery.js
ADDED
|
@@ -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,4 @@
|
|
|
1
|
+
/** Aggregators from Walrus's operator registry that served the probe blob to https://nmts.me, pinned first rows first, then fastest first. */
|
|
2
|
+
export declare const REGISTRY_AGGREGATORS: Readonly<Record<"testnet" | "mainnet", readonly string[]>>;
|
|
3
|
+
/** The live Walrus committee's storage nodes a https page can call — the last road for uploads and downloads. */
|
|
4
|
+
export declare const STORAGE_NODE_ORIGINS: Readonly<Record<"testnet" | "mainnet", readonly string[]>>;
|