@needmoretruth/nmts-cli 0.55.0 → 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 +10 -0
- package/README.ko.md +9 -1
- package/README.md +9 -1
- package/dist/artifact-about.d.ts +1 -1
- package/dist/commands/put-wallet.js +11 -11
- 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/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/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/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-steps.js +4 -1
- 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 +11 -3
- package/dist/upload-wire.d.ts +22 -0
- package/dist/upload-wire.js +2 -0
- package/dist/walrus-write.js +12 -0
- package/docs/commands/put.md +5 -1
- package/package.json +5 -1
- 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>;
|
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
|
+
}
|
package/dist/upload-file.d.ts
CHANGED
|
@@ -94,6 +94,8 @@ export type FileUploadStep = {
|
|
|
94
94
|
export declare function uploadFile(input: FileUploadInput): Promise<UploadResult>;
|
|
95
95
|
/** The file key's records, so a caller can clear them once the list is written. */
|
|
96
96
|
export declare function partKeysOf(fileKey: string, parts: number): string[];
|
|
97
|
+
/** The key `uploadFile` files this upload's records under — the same pass, for a caller pricing it first. */
|
|
98
|
+
export declare function fileKeyOf(dataKey: Uint8Array, source: PlaintextSource, name: string, destination: string): Promise<string>;
|
|
97
99
|
/**
|
|
98
100
|
* The part's bytes, followed by zeros up to the length it will declare.
|
|
99
101
|
*
|
package/dist/upload-file.js
CHANGED
|
@@ -141,6 +141,12 @@ async function uploadParts(input, run) {
|
|
|
141
141
|
export function partKeysOf(fileKey, parts) {
|
|
142
142
|
return Array.from({ length: parts }, (_, index) => partKey(fileKey, index));
|
|
143
143
|
}
|
|
144
|
+
/** The key `uploadFile` files this upload's records under — the same pass, for a caller pricing it first. */
|
|
145
|
+
export async function fileKeyOf(dataKey, source, name, destination) {
|
|
146
|
+
const keyHash = startReservationKey(dataKey);
|
|
147
|
+
await hashWhole(source, keyHash);
|
|
148
|
+
return finishReservationKey(keyHash, name, destination);
|
|
149
|
+
}
|
|
144
150
|
/** Read the whole file once, feeding the key hash and returning the content digest. */
|
|
145
151
|
async function hashWhole(source, keyHash) {
|
|
146
152
|
const content = sha256.create();
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
import { type RecoveryAccount } from "./recovery.ts";
|
|
2
|
+
import { type PlaintextSource } from "./upload-file.ts";
|
|
3
|
+
import { RIDE_CEILING_BYTES, type BlobProtocol, type PaidPart, type UploadInput } from "./upload-wire.ts";
|
|
4
|
+
export { RIDE_CEILING_BYTES };
|
|
5
|
+
export interface RideContext {
|
|
6
|
+
account: RecoveryAccount;
|
|
7
|
+
protocol: BlobProtocol;
|
|
8
|
+
/** Said once per skipped or failed copy. The upload goes on either way. */
|
|
9
|
+
onProblem?: ((message: string) => void) | undefined;
|
|
10
|
+
}
|
|
11
|
+
/** The one part a ride is planned for, before anything is sealed. */
|
|
12
|
+
export interface RideShape {
|
|
13
|
+
/** The part's record key, as `uploadFile` files it. */
|
|
14
|
+
partKey: string;
|
|
15
|
+
/** Its sealed length, as `planAndPrice` works it out. */
|
|
16
|
+
sealedLen: number;
|
|
17
|
+
name: string;
|
|
18
|
+
parentId: string | null;
|
|
19
|
+
size: number;
|
|
20
|
+
streamPlaintextLen: number;
|
|
21
|
+
}
|
|
22
|
+
export interface RecoveryRide {
|
|
23
|
+
/**
|
|
24
|
+
* Before the quote: the length the storage is bought for when the list rides — a fresh quilt, or
|
|
25
|
+
* the quilt a first run stored — or null when the file goes up alone. Never throws.
|
|
26
|
+
*/
|
|
27
|
+
plan(shape: RideShape): Promise<number | null>;
|
|
28
|
+
/** The rail, wrapped, for what `plan` decided. */
|
|
29
|
+
wrap(rail: (input: UploadInput) => Promise<PaidPart>): (input: UploadInput) => Promise<PaidPart>;
|
|
30
|
+
/**
|
|
31
|
+
* After the commit: tell the server which quilt holds the newest list. Never throws. Answers the
|
|
32
|
+
* number of the list that rode, or null when none did.
|
|
33
|
+
*/
|
|
34
|
+
record(): Promise<number | null>;
|
|
35
|
+
}
|
|
36
|
+
/** The patch id an aggregator reads one patch by: the quilt id, then version 1 and the two indices. */
|
|
37
|
+
export declare function quiltPatchId(quiltId: string, startIndex: number, endIndex: number): string;
|
|
38
|
+
/** Wrap a wallet rail so that, with the copy switched on, the recovery list rides in the same quilt. */
|
|
39
|
+
export declare function rideRecoveryList(ctx: RideContext): RecoveryRide;
|
|
40
|
+
/**
|
|
41
|
+
* The ride for one wallet upload, planned for its one part: the ride, and the length to price.
|
|
42
|
+
*
|
|
43
|
+
* ⚠ THE PROOF IS `authSecret` — what every sign-in sends and the proof header carries. The caller
|
|
44
|
+
* already holds the code for the upload itself, so building it here asks nothing new of anybody.
|
|
45
|
+
*
|
|
46
|
+
* ⚠ THE FILE IS READ ONCE MORE, to find its records (a resume prices what the first run stored). A
|
|
47
|
+
* ride is a file of one part under 64 MiB, so nothing larger is read twice.
|
|
48
|
+
*/
|
|
49
|
+
export declare function planWalletRide(ctx: {
|
|
50
|
+
server: string;
|
|
51
|
+
apiKey: string;
|
|
52
|
+
code: string;
|
|
53
|
+
accountId: string;
|
|
54
|
+
network: string;
|
|
55
|
+
}, protocol: BlobProtocol, onProblem: ((message: string) => void) | undefined, file: {
|
|
56
|
+
source: PlaintextSource;
|
|
57
|
+
name: string;
|
|
58
|
+
parentId: string | null;
|
|
59
|
+
destination: string;
|
|
60
|
+
streamPlaintextLen: number;
|
|
61
|
+
sealedLen: number;
|
|
62
|
+
}): Promise<{
|
|
63
|
+
ride: RecoveryRide;
|
|
64
|
+
quiltLen: number | null;
|
|
65
|
+
}>;
|
|
@@ -0,0 +1,250 @@
|
|
|
1
|
+
// The recovery list riding in a wallet upload — the command line's and the SDK's copy of what the
|
|
2
|
+
// browser does in `web/src/lib/recovery/network-copy.ts` (spec 2026-09-20 unit B · NCF-3 §2.5).
|
|
3
|
+
//
|
|
4
|
+
// ⛔ WHY IT RIDES IN THE SAME UPLOAD. A quilt's blob id is a hash of its contents, so a list written
|
|
5
|
+
// afterwards could not name the quilt it is in, and one written before the next upload would
|
|
6
|
+
// describe every file but the one it rode with — for somebody who uploads once, all of them.
|
|
7
|
+
// NRM-3's own-quilt placement says "my patch is called X, in the quilt you found me in"; the
|
|
8
|
+
// standalone recovery program finds the list by the patch name the NMTS key derives and the
|
|
9
|
+
// wallets it derives, with no server anywhere in the picture.
|
|
10
|
+
//
|
|
11
|
+
// ⛔ IT NEVER FAILS AN UPLOAD. Whatever goes wrong before the bytes leave — the switch unreadable,
|
|
12
|
+
// the list not reconciling, a proof refused — is reported through `onProblem` and the file goes
|
|
13
|
+
// up as its own blob, as it would have with the switch off. The person asked for a file to be
|
|
14
|
+
// stored; the copy is a passenger.
|
|
15
|
+
//
|
|
16
|
+
// ⛔ ONE PART, UNDER 64 MiB, NEW STORAGE. A file of several parts has no single quilt to ride in, and
|
|
17
|
+
// the browser draws the same line. A held storage resource was sized for the file alone, and a
|
|
18
|
+
// quilt is a little larger — so a reused resource uploads without the passenger.
|
|
19
|
+
//
|
|
20
|
+
// ⛔ PLANNED BEFORE THE PRICE. Storage is bought for the bytes the register signature names, and a
|
|
21
|
+
// quilt is larger than the file in it. So the decision to ride, and the quilt's length, come
|
|
22
|
+
// before the quote — the browser's order (`web/src/lib/upload/quilt-session.ts`: encode, quote,
|
|
23
|
+
// check the funds). `plan` lays the quilt out with the file's length in zeros and a list with
|
|
24
|
+
// the riding entry's fields at their real lengths; a quilt's layout depends on lengths alone, so
|
|
25
|
+
// the quilt laid out after agreement is the same size. If it is not — the account changed in
|
|
26
|
+
// between — the file goes up alone, which costs less than was agreed, never more.
|
|
27
|
+
//
|
|
28
|
+
// ⛔ A RESUME DOES NOT RE-QUILT. The rail keeps the bytes it was given; the record here says those
|
|
29
|
+
// bytes were a quilt and where the file sits inside it, so a second run prices that quilt and
|
|
30
|
+
// reports the same patch.
|
|
31
|
+
import { fromBase64Url, toBase64Url } from "./bytes.js";
|
|
32
|
+
import { DERIVED, loadCrypto } from "./crypto.js";
|
|
33
|
+
import { isRecord } from "./guards.js";
|
|
34
|
+
import { host } from "./host.js";
|
|
35
|
+
import { makeRecoveryList, noteOfferedSeq, readRecoveryCopy, recordRecoveryList } from "./recovery.js";
|
|
36
|
+
import { registrationProofOf } from "./registration.js";
|
|
37
|
+
import { fileSecrets } from "./seal.js";
|
|
38
|
+
import { fileKeyOf } from "./upload-file.js";
|
|
39
|
+
import { partKey, readReservationRecord } from "./upload-store.js";
|
|
40
|
+
import { RIDE_CEILING_BYTES } from "./upload-wire.js";
|
|
41
|
+
// The largest sealed file a recovery list rides with lives in `upload-wire.ts`, beside the seam.
|
|
42
|
+
export { RIDE_CEILING_BYTES };
|
|
43
|
+
const rideKey = (key) => `recovery-copy/${key}`;
|
|
44
|
+
/** The patch id an aggregator reads one patch by: the quilt id, then version 1 and the two indices. */
|
|
45
|
+
export function quiltPatchId(quiltId, startIndex, endIndex) {
|
|
46
|
+
const id = fromBase64Url(quiltId);
|
|
47
|
+
const out = new Uint8Array(id.length + 5);
|
|
48
|
+
out.set(id, 0);
|
|
49
|
+
out[id.length] = 1;
|
|
50
|
+
new DataView(out.buffer).setUint16(id.length + 1, startIndex, true);
|
|
51
|
+
new DataView(out.buffer).setUint16(id.length + 3, endIndex, true);
|
|
52
|
+
return toBase64Url(out);
|
|
53
|
+
}
|
|
54
|
+
async function readRide(key) {
|
|
55
|
+
const bytes = await host().state.read(rideKey(key));
|
|
56
|
+
if (bytes === undefined)
|
|
57
|
+
return null;
|
|
58
|
+
try {
|
|
59
|
+
const v = JSON.parse(new TextDecoder().decode(bytes));
|
|
60
|
+
if (!isRecord(v))
|
|
61
|
+
return null;
|
|
62
|
+
const { identifier, startIndex, endIndex, seq, streamLen, quiltLen } = v;
|
|
63
|
+
if (typeof identifier !== "string")
|
|
64
|
+
return null;
|
|
65
|
+
const nums = [startIndex, endIndex, seq, streamLen, quiltLen];
|
|
66
|
+
if (!nums.every((n) => typeof n === "number" && Number.isSafeInteger(n)))
|
|
67
|
+
return null;
|
|
68
|
+
return { identifier, startIndex: Number(startIndex), endIndex: Number(endIndex), seq: Number(seq), streamLen: Number(streamLen), quiltLen: Number(quiltLen) };
|
|
69
|
+
}
|
|
70
|
+
catch {
|
|
71
|
+
return null;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
/** The quilt a first run stored, when the record says this part rode: its length, else null. */
|
|
75
|
+
async function resumedQuilt(key) {
|
|
76
|
+
const reserved = await readReservationRecord(key);
|
|
77
|
+
if (reserved === null)
|
|
78
|
+
return null;
|
|
79
|
+
const known = await readRide(key);
|
|
80
|
+
return { ride: known !== null && reserved.sealedLen === known.quiltLen ? known : null, sealedLen: reserved.sealedLen };
|
|
81
|
+
}
|
|
82
|
+
/** The failure's own sentence, without its full stop — the line around it ends the sentence. */
|
|
83
|
+
function why(error) {
|
|
84
|
+
return (error instanceof Error ? error.message : String(error)).replace(/[.\s]+$/, "");
|
|
85
|
+
}
|
|
86
|
+
/** A fresh patch name for the file, from the platform's generator. */
|
|
87
|
+
function freshIdentifier() {
|
|
88
|
+
const bytes = new Uint8Array(16);
|
|
89
|
+
globalThis.crypto.getRandomValues(bytes);
|
|
90
|
+
return toBase64Url(bytes);
|
|
91
|
+
}
|
|
92
|
+
/** Wrap a wallet rail so that, with the copy switched on, the recovery list rides in the same quilt. */
|
|
93
|
+
export function rideRecoveryList(ctx) {
|
|
94
|
+
const rides = [];
|
|
95
|
+
const say = (message) => ctx.onProblem?.(message);
|
|
96
|
+
const encode = ctx.protocol.encodeQuilt?.bind(ctx.protocol);
|
|
97
|
+
let planned = null;
|
|
98
|
+
/** The list with `riding` in it, and the quilt it makes with `stream`. */
|
|
99
|
+
const lay = async (riding, stream) => {
|
|
100
|
+
if (encode === undefined)
|
|
101
|
+
throw new Error("this storage network protocol cannot lay out a quilt");
|
|
102
|
+
const made = await makeRecoveryList(ctx.account, { riding: [riding] });
|
|
103
|
+
const encoded = await encode([
|
|
104
|
+
{ contents: stream, identifier: riding.identifier },
|
|
105
|
+
{ contents: fromBase64Url(made.built.sealed), identifier: await patchNameOf(ctx.account.code) },
|
|
106
|
+
]);
|
|
107
|
+
const mine = encoded.patches.find((p) => p.identifier === riding.identifier);
|
|
108
|
+
if (mine === undefined)
|
|
109
|
+
throw new Error("the quilt has no patch for the file");
|
|
110
|
+
return { quilt: encoded.quilt, mine, seq: made.seq };
|
|
111
|
+
};
|
|
112
|
+
const plan = async (shape) => {
|
|
113
|
+
planned = null;
|
|
114
|
+
if (encode === undefined)
|
|
115
|
+
return null;
|
|
116
|
+
try {
|
|
117
|
+
const resumed = await resumedQuilt(shape.partKey);
|
|
118
|
+
if (resumed !== null)
|
|
119
|
+
return resumed.ride?.quiltLen ?? null;
|
|
120
|
+
if (shape.sealedLen > RIDE_CEILING_BYTES)
|
|
121
|
+
return null;
|
|
122
|
+
const state = await readRecoveryCopy(ctx.account);
|
|
123
|
+
if (state === null || !state.enabled)
|
|
124
|
+
return null;
|
|
125
|
+
const identifier = freshIdentifier();
|
|
126
|
+
const { quilt } = await lay({ identifier, ...shape, ...(await secretsOfLength(ctx.account.code)) }, new Uint8Array(shape.sealedLen));
|
|
127
|
+
planned = { identifier, quiltLen: quilt.length };
|
|
128
|
+
return quilt.length;
|
|
129
|
+
}
|
|
130
|
+
catch (error) {
|
|
131
|
+
say(`The recovery list was left out of this upload: ${why(error)}. The file goes up on its own.`);
|
|
132
|
+
return null;
|
|
133
|
+
}
|
|
134
|
+
};
|
|
135
|
+
const onQuilt = (key, ride, paid) => {
|
|
136
|
+
rides.push({ partKey: key, seq: ride.seq, quiltId: paid.blobId });
|
|
137
|
+
return { ...paid, sealedLen: ride.streamLen, patchId: quiltPatchId(paid.blobId, ride.startIndex, ride.endIndex) };
|
|
138
|
+
};
|
|
139
|
+
const wrap = (rail) => async (input) => {
|
|
140
|
+
if (input.part.total !== 1)
|
|
141
|
+
return rail(input);
|
|
142
|
+
// ── a resume: the bytes handed in are whatever the first run stored ──
|
|
143
|
+
const resumed = await resumedQuilt(input.key);
|
|
144
|
+
if (resumed !== null)
|
|
145
|
+
return resumed.ride === null ? rail(input) : onQuilt(input.key, resumed.ride, await rail(input));
|
|
146
|
+
const agreed = planned;
|
|
147
|
+
if (agreed === null)
|
|
148
|
+
return rail(input);
|
|
149
|
+
let ride;
|
|
150
|
+
let quilt;
|
|
151
|
+
try {
|
|
152
|
+
const { entry } = input;
|
|
153
|
+
const laid = await lay({ identifier: agreed.identifier, name: entry.name, parentId: entry.parentId, size: entry.plaintextLen, streamPlaintextLen: input.part.plaintextLen, dekWrapped: entry.dekWrapped, contentHashCt: entry.contentHashCt }, input.sealed);
|
|
154
|
+
// ⛔ THE PRICE AGREED WAS FOR THIS LENGTH. Any other quilt goes unregistered.
|
|
155
|
+
if (laid.quilt.length !== agreed.quiltLen)
|
|
156
|
+
throw new Error("the file list changed while this upload was being priced");
|
|
157
|
+
quilt = laid.quilt;
|
|
158
|
+
ride = { identifier: agreed.identifier, startIndex: laid.mine.startIndex, endIndex: laid.mine.endIndex, seq: laid.seq, streamLen: input.sealed.length, quiltLen: quilt.length };
|
|
159
|
+
// ⛔ NOTED BEFORE THE BYTES LEAVE. Skipping a number breaks nothing; reusing one is the defect.
|
|
160
|
+
await noteOfferedSeq(ctx.account.accountId, laid.seq);
|
|
161
|
+
await host().state.write(rideKey(input.key), new TextEncoder().encode(JSON.stringify(ride)));
|
|
162
|
+
}
|
|
163
|
+
catch (error) {
|
|
164
|
+
say(`The recovery list was left out of this upload: ${why(error)}. The file goes up on its own.`);
|
|
165
|
+
return rail(input);
|
|
166
|
+
}
|
|
167
|
+
return onQuilt(input.key, ride, await rail({ ...input, sealed: quilt }));
|
|
168
|
+
};
|
|
169
|
+
const record = async () => {
|
|
170
|
+
let newest = null;
|
|
171
|
+
for (const r of rides) {
|
|
172
|
+
newest = Math.max(newest ?? 0, r.seq);
|
|
173
|
+
try {
|
|
174
|
+
await recordRecoveryList(ctx.account, { seq: r.seq, blobId: r.quiltId });
|
|
175
|
+
}
|
|
176
|
+
catch (error) {
|
|
177
|
+
// ⚠ The bytes ARE on the storage network and the recovery program finds them without us.
|
|
178
|
+
// What is missing is the account screen's note, so it is said and nothing else.
|
|
179
|
+
say(`The recovery list went up with the file, and the server did not record it: ${why(error)}. The recovery program finds it without the server.`);
|
|
180
|
+
}
|
|
181
|
+
await host().state.remove(rideKey(r.partKey)).catch(() => undefined);
|
|
182
|
+
}
|
|
183
|
+
return newest;
|
|
184
|
+
};
|
|
185
|
+
return { plan, wrap, record };
|
|
186
|
+
}
|
|
187
|
+
/**
|
|
188
|
+
* The ride for one wallet upload, planned for its one part: the ride, and the length to price.
|
|
189
|
+
*
|
|
190
|
+
* ⚠ THE PROOF IS `authSecret` — what every sign-in sends and the proof header carries. The caller
|
|
191
|
+
* already holds the code for the upload itself, so building it here asks nothing new of anybody.
|
|
192
|
+
*
|
|
193
|
+
* ⚠ THE FILE IS READ ONCE MORE, to find its records (a resume prices what the first run stored). A
|
|
194
|
+
* ride is a file of one part under 64 MiB, so nothing larger is read twice.
|
|
195
|
+
*/
|
|
196
|
+
export async function planWalletRide(ctx, protocol, onProblem, file) {
|
|
197
|
+
const { authSecret } = await registrationProofOf(ctx.code);
|
|
198
|
+
const account = {
|
|
199
|
+
server: ctx.server,
|
|
200
|
+
bearer: ctx.apiKey,
|
|
201
|
+
code: ctx.code,
|
|
202
|
+
accountId: ctx.accountId,
|
|
203
|
+
network: ctx.network,
|
|
204
|
+
accountProof: authSecret,
|
|
205
|
+
};
|
|
206
|
+
const ride = rideRecoveryList({ account, protocol, onProblem });
|
|
207
|
+
if (protocol.encodeQuilt === undefined || file.sealedLen > RIDE_CEILING_BYTES)
|
|
208
|
+
return { ride, quiltLen: null };
|
|
209
|
+
const fileKey = await withDataKey(ctx.code, (dataKey) => fileKeyOf(dataKey, file.source, file.name, file.destination));
|
|
210
|
+
const quiltLen = await ride.plan({
|
|
211
|
+
partKey: partKey(fileKey, 0),
|
|
212
|
+
sealedLen: file.sealedLen,
|
|
213
|
+
name: file.name,
|
|
214
|
+
parentId: file.parentId,
|
|
215
|
+
size: file.source.size,
|
|
216
|
+
streamPlaintextLen: file.streamPlaintextLen,
|
|
217
|
+
});
|
|
218
|
+
return { ride, quiltLen };
|
|
219
|
+
}
|
|
220
|
+
/** Run `use` with the data key the NMTS key derives, and wipe it after. */
|
|
221
|
+
async function withDataKey(code, use) {
|
|
222
|
+
const crypt = await loadCrypto();
|
|
223
|
+
const derived = crypt.kdf_derive(crypt.account_code_parse(code));
|
|
224
|
+
const dataKey = derived.slice(DERIVED.dataKey[0], DERIVED.dataKey[1]);
|
|
225
|
+
derived.fill(0);
|
|
226
|
+
try {
|
|
227
|
+
return await use(dataKey);
|
|
228
|
+
}
|
|
229
|
+
finally {
|
|
230
|
+
dataKey.fill(0);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
/** The name the list's patch takes, derived from the data key (NCF-3 §2.5). */
|
|
234
|
+
async function patchNameOf(code) {
|
|
235
|
+
const crypt = await loadCrypto();
|
|
236
|
+
return withDataKey(code, (dataKey) => crypt.recovery_patch_name(dataKey));
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* A wrapped key and a sealed hash at the lengths a real upload's are, for pricing before anything
|
|
240
|
+
* is sealed. Made by the very function `uploadFile` uses, so the lengths cannot drift; the key in
|
|
241
|
+
* them is thrown away.
|
|
242
|
+
*/
|
|
243
|
+
async function secretsOfLength(code) {
|
|
244
|
+
const crypt = await loadCrypto();
|
|
245
|
+
return withDataKey(code, (dataKey) => {
|
|
246
|
+
const made = fileSecrets(crypt, dataKey, new Uint8Array(32));
|
|
247
|
+
made.dek.fill(0);
|
|
248
|
+
return { dekWrapped: made.dekWrapped, contentHashCt: made.contentHashCt };
|
|
249
|
+
});
|
|
250
|
+
}
|
package/dist/upload-steps.js
CHANGED
|
@@ -8,6 +8,8 @@ import { readItemRecord, writeItemRecord } from "./upload-store.js";
|
|
|
8
8
|
import { UploadError, } from "./upload-wire.js";
|
|
9
9
|
/** ITEM PARTS: the storage shape this tool writes — one blob per part, the file its own set. */
|
|
10
10
|
const STORAGE_KIND_DEDICATED_BLOB = 0;
|
|
11
|
+
/** A wallet upload that carried the recovery list is one patch of a quilt (`upload-recovery-patch.ts`). */
|
|
12
|
+
const STORAGE_KIND_QUILT_PATCH = 1;
|
|
11
13
|
/** Walrus. The only network with an upload path. */
|
|
12
14
|
const NETWORK_WALRUS = 0;
|
|
13
15
|
/** Only this account can read it. Sharing is a separate act, made in a browser. */
|
|
@@ -126,9 +128,10 @@ export async function commitItem(input, fileKey, parts) {
|
|
|
126
128
|
function describePart(part, expiryEpoch) {
|
|
127
129
|
const common = {
|
|
128
130
|
part_index: part.partIndex,
|
|
129
|
-
storage_kind: STORAGE_KIND_DEDICATED_BLOB,
|
|
131
|
+
storage_kind: part.patchId === undefined ? STORAGE_KIND_DEDICATED_BLOB : STORAGE_KIND_QUILT_PATCH,
|
|
130
132
|
network: NETWORK_WALRUS,
|
|
131
133
|
blob_id: part.blobId,
|
|
134
|
+
...(part.patchId === undefined ? {} : { patch_id: part.patchId }),
|
|
132
135
|
sealed_len: part.sealedLen,
|
|
133
136
|
};
|
|
134
137
|
if (part.ledgerId !== null) {
|
|
@@ -117,6 +117,8 @@ export declare function uploadShortfallNextStep(b: UploadBudget): string;
|
|
|
117
117
|
export declare function describeUploadReview(say: (line: string) => void, facts: {
|
|
118
118
|
name: string;
|
|
119
119
|
bytes: number;
|
|
120
|
+
/** What the storage network would hold, sealing and padding included. */
|
|
121
|
+
sealedBytes: number;
|
|
120
122
|
parts: number;
|
|
121
123
|
epochs: number;
|
|
122
124
|
days: string;
|
|
@@ -133,4 +135,6 @@ export declare function describeUploadReview(say: (line: string) => void, facts:
|
|
|
133
135
|
* off buys silence.
|
|
134
136
|
*/
|
|
135
137
|
networkCopy: boolean | null;
|
|
138
|
+
/** The list rides in this upload's quilt, and the WAL above is the quilt's price. */
|
|
139
|
+
carriesRecoveryList: boolean;
|
|
136
140
|
}, budget: UploadBudget): void;
|