@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.
- package/CHANGELOG.md +20 -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/receive.js +30 -87
- package/dist/commands/recovery-list.js +3 -2
- package/dist/commands/share.d.ts +7 -0
- package/dist/commands/share.js +58 -180
- package/dist/commands/shares-sent.js +4 -29
- package/dist/crypto-surface.d.ts +2 -0
- package/dist/crypto-surface.js +4 -7
- package/dist/links.d.ts +5 -0
- package/dist/links.js +6 -1
- package/dist/platform-sign.d.ts +7 -1
- package/dist/platform-sign.js +12 -1
- package/dist/portable.d.ts +4 -0
- package/dist/portable.js +5 -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/sharing-inbox.d.ts +52 -0
- package/dist/sharing-inbox.js +146 -0
- package/dist/sharing.d.ts +52 -0
- package/dist/sharing.js +218 -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 +9 -1
- package/server.json +2 -2
package/dist/sharing.js
ADDED
|
@@ -0,0 +1,218 @@
|
|
|
1
|
+
// Sharing as a library: send one file to another account, list what came in and what went out,
|
|
2
|
+
// receive one, and take one back — for the command line, the SDK and a page, with nothing in it
|
|
3
|
+
// that needs Node (spec 2026-09-20 unit C).
|
|
4
|
+
//
|
|
5
|
+
// ⛔ WHY IT IS ITS OWN ENTRY. An app built on NMTS could not share a person's file: the steps lived
|
|
6
|
+
// inside `nmts share` and `nmts receive`, with the terminal wound through them. The commands are
|
|
7
|
+
// now shells over these functions, so what an app sends is what the command sends, byte for
|
|
8
|
+
// byte — the sealing itself is `share.ts`, untouched, and its order is load-bearing.
|
|
9
|
+
//
|
|
10
|
+
// ⛔ THE SAME FOR EVERY KEY HOLDER. `ShareAccount.bearer` is an API key or a delegation token. A
|
|
11
|
+
// token reaches the two sending doors only with its `share` bit AND the code's proof
|
|
12
|
+
// (`accountProof`), so the proof is sent on those two requests and on no other. An API key is
|
|
13
|
+
// asked for a live human check there instead, and the proof is simply not needed.
|
|
14
|
+
//
|
|
15
|
+
// ⛔ RECEIVING IS `sharing-inbox.ts`, re-exported here so `./share` is one entry.
|
|
16
|
+
//
|
|
17
|
+
// ⛔ NO `node:`, NO `Buffer`, NO `process.` — `test/portable-closure.test.ts` walks this file.
|
|
18
|
+
import { request, ServerError } from "./api.js";
|
|
19
|
+
import { fromBase64Url, toBase64Url, utf8 } from "./bytes.js";
|
|
20
|
+
import { AAD, DERIVED, loadCrypto } from "./crypto.js";
|
|
21
|
+
import { buildIndex, entryAt, fullPathOf, KIND_FILE, normalisePath } from "./drive-paths.js";
|
|
22
|
+
import { NmtsError } from "./errors.js";
|
|
23
|
+
import { readFileList } from "./manifest.js";
|
|
24
|
+
import { BINARY_NAME } from "./product.js";
|
|
25
|
+
import { differentCode, notLiveCode, publicCodeRefusal } from "./public-code-refusals.js";
|
|
26
|
+
import { ensureLiveCode, liveCodes, PUBLISH_TRIES } from "./public-codes.js";
|
|
27
|
+
import { identityMatches, sealShare } from "./share.js";
|
|
28
|
+
import { shareKeyRing, shareKeysAt } from "./share-codes.js";
|
|
29
|
+
export { addressFromTyped } from "./share.js";
|
|
30
|
+
export { receivedShares, receiveShare } from "./sharing-inbox.js";
|
|
31
|
+
/** The file at `target`, checked for the key and the hash a share carries. */
|
|
32
|
+
export async function shareableFileAt(account, target) {
|
|
33
|
+
const list = await readFileList(account.server, account.bearer, account.code, account.accountId);
|
|
34
|
+
if (list.manifest === null) {
|
|
35
|
+
throw new NmtsError("This account has no file list, so there is nothing to share.", { exitCode: 4 });
|
|
36
|
+
}
|
|
37
|
+
const index = buildIndex(list.manifest.entries);
|
|
38
|
+
const entry = entryAt(list.manifest.entries, normalisePath(target), {
|
|
39
|
+
nothingHappened: "Nothing was shared.",
|
|
40
|
+
});
|
|
41
|
+
if (entry.kind !== KIND_FILE) {
|
|
42
|
+
throw new NmtsError(`No file at "${fullPathOf(index, entry)}".`, {
|
|
43
|
+
exitCode: 4,
|
|
44
|
+
nextStep: "That is a folder. Nothing was shared — this version shares one file at a time.",
|
|
45
|
+
});
|
|
46
|
+
}
|
|
47
|
+
const { dekWrapped, contentHashCt } = entry;
|
|
48
|
+
if (dekWrapped === undefined || contentHashCt === undefined) {
|
|
49
|
+
throw new NmtsError(`The file list holds no ${dekWrapped === undefined ? "key" : "hash"} for "${entry.name}".`, {
|
|
50
|
+
exitCode: 4,
|
|
51
|
+
nextStep: "A share carries both — the key that opens the file and the hash the recipient checks the " +
|
|
52
|
+
"bytes against. Nothing was shared.",
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return { entry: { ...entry, dekWrapped, contentHashCt }, path: fullPathOf(index, entry) };
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* Which live code sends: `as` when named, else the lowest-numbered live one — published first when
|
|
59
|
+
* the account has none, as a first share does.
|
|
60
|
+
*
|
|
61
|
+
* ⚠ A DELEGATION TOKEN CANNOT READ THE CODE LIST (its business replaces a user's one code through
|
|
62
|
+
* its own signed batch, and that list stays closed to it). So for a token the codes are asked
|
|
63
|
+
* one number at a time at the public lookup, from 0: a revoked one answers 410, the live one 200,
|
|
64
|
+
* and the first number never published 404 — there is nothing after it.
|
|
65
|
+
*/
|
|
66
|
+
async function senderKeys(crypt, account, as, onFirstCode) {
|
|
67
|
+
const door = { server: account.server, token: account.bearer };
|
|
68
|
+
let codes;
|
|
69
|
+
try {
|
|
70
|
+
({ list: codes } = await ensureLiveCode(crypt, account.code, door, onFirstCode));
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
if (!(error instanceof ServerError && error.code === "DELEGATION_SCOPE"))
|
|
74
|
+
throw publicCodeRefusal(error);
|
|
75
|
+
return as === undefined ? await walkToLiveCode(crypt, account) : shareKeysAt(crypt, account.code, as);
|
|
76
|
+
}
|
|
77
|
+
const live = liveCodes(codes);
|
|
78
|
+
const sender = as === undefined ? live[0] : live.find((c) => c.index === as);
|
|
79
|
+
if (sender === undefined)
|
|
80
|
+
throw notLiveCode(as ?? 0);
|
|
81
|
+
const keys = shareKeysAt(crypt, account.code, sender.index);
|
|
82
|
+
if (toBase64Url(keys.address) !== sender.address) {
|
|
83
|
+
keys.wipe();
|
|
84
|
+
throw differentCode();
|
|
85
|
+
}
|
|
86
|
+
return keys;
|
|
87
|
+
}
|
|
88
|
+
async function walkToLiveCode(crypt, account) {
|
|
89
|
+
const ring = shareKeyRing(crypt, account.code);
|
|
90
|
+
try {
|
|
91
|
+
for (let index = 0; index < PUBLISH_TRIES; index += 1) {
|
|
92
|
+
const address = toBase64Url(ring.at(index).address);
|
|
93
|
+
try {
|
|
94
|
+
await request(account.server, `/v1/share-recipients/${encodeURIComponent(address)}`, { token: account.bearer });
|
|
95
|
+
return shareKeysAt(crypt, account.code, index);
|
|
96
|
+
}
|
|
97
|
+
catch (error) {
|
|
98
|
+
if (error instanceof ServerError && error.status === 410)
|
|
99
|
+
continue;
|
|
100
|
+
if (error instanceof ServerError && error.status === 404)
|
|
101
|
+
break;
|
|
102
|
+
throw error;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
finally {
|
|
107
|
+
ring.wipe();
|
|
108
|
+
}
|
|
109
|
+
throw new NmtsError("This account has no public code yet, so nothing can be shared from it.", {
|
|
110
|
+
exitCode: 4,
|
|
111
|
+
nextStep: "Nothing was shared. The business that issued this token publishes one with setPublicCodes; then share again.",
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
/**
|
|
115
|
+
* Seal one file for one public code and send it.
|
|
116
|
+
*
|
|
117
|
+
* ⛔ THE CALLER HAS ALREADY DECIDED. The command asks its person between `shareableFileAt` and this
|
|
118
|
+
* call; a program decides by calling. Nothing here asks anybody anything.
|
|
119
|
+
*/
|
|
120
|
+
export async function sendShare(account, file, recipientAddress, options = {}) {
|
|
121
|
+
const crypt = await loadCrypto();
|
|
122
|
+
const { entry } = file;
|
|
123
|
+
let keys = null;
|
|
124
|
+
const derived = crypt.kdf_derive(crypt.account_code_parse(account.code));
|
|
125
|
+
const dataKey = derived.slice(DERIVED.dataKey[0], DERIVED.dataKey[1]);
|
|
126
|
+
derived.fill(0);
|
|
127
|
+
let dek = null;
|
|
128
|
+
let digest = null;
|
|
129
|
+
try {
|
|
130
|
+
dek = crypt.envelope_open(dataKey, utf8(AAD.dekWrap), fromBase64Url(entry.dekWrapped));
|
|
131
|
+
digest = crypt.envelope_open(dataKey, utf8(AAD.contentHash), fromBase64Url(entry.contentHashCt));
|
|
132
|
+
dataKey.fill(0);
|
|
133
|
+
keys = await senderKeys(crypt, account, options.as, options.onFirstCode);
|
|
134
|
+
const answer = await request(account.server, `/v1/share-recipients/${encodeURIComponent(toBase64Url(recipientAddress))}`, { token: account.bearer });
|
|
135
|
+
const identityB64 = typeof answer === "object" && answer !== null ? Reflect.get(answer, "share_public_key") : null;
|
|
136
|
+
if (typeof identityB64 !== "string") {
|
|
137
|
+
throw new NmtsError("That public code has never published an identity to share to.", {
|
|
138
|
+
exitCode: 4,
|
|
139
|
+
nextStep: "Nothing was shared. The code may be right and unused — somebody has to open " +
|
|
140
|
+
"their account once before anything can be encrypted to them.",
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
const recipientIdentity = fromBase64Url(identityB64);
|
|
144
|
+
// ⛔ CHECKED BEFORE ANYTHING IS ENCRYPTED TO IT. The engine checks it again inside the wrap;
|
|
145
|
+
// this exists so the refusal names WHICH thing was wrong.
|
|
146
|
+
if (!identityMatches(crypt, recipientIdentity, recipientAddress)) {
|
|
147
|
+
throw new NmtsError("The identity the server returned is not the one that public code names.", {
|
|
148
|
+
exitCode: 1,
|
|
149
|
+
nextStep: "Nothing was shared. Encrypting to it would hand the file to somebody else.",
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
const payload = sealShare(crypt, {
|
|
153
|
+
keys,
|
|
154
|
+
recipientIdentity,
|
|
155
|
+
recipientAddress,
|
|
156
|
+
dek,
|
|
157
|
+
itemId: entry.id,
|
|
158
|
+
name: entry.name,
|
|
159
|
+
size: entry.size,
|
|
160
|
+
digest,
|
|
161
|
+
});
|
|
162
|
+
const created = await request(account.server, "/v1/shares", {
|
|
163
|
+
token: account.bearer,
|
|
164
|
+
method: "POST",
|
|
165
|
+
body: { item_id: entry.id, recipient_address: toBase64Url(recipientAddress), ...payload },
|
|
166
|
+
...(account.accountProof === undefined ? {} : { accountProof: account.accountProof }),
|
|
167
|
+
});
|
|
168
|
+
// ⛔ READ BACK, NOT ECHOED. The server returns the address it actually STORED the share under.
|
|
169
|
+
const stored = typeof created === "object" && created !== null ? Reflect.get(created, "recipient_address") : null;
|
|
170
|
+
if (typeof stored !== "string" || stored !== toBase64Url(recipientAddress)) {
|
|
171
|
+
throw new NmtsError("The server filed this share against a different public code.", {
|
|
172
|
+
exitCode: 1,
|
|
173
|
+
nextStep: `Withdraw it now with \`${BINARY_NAME} unshare\` and share it again. The file's key was ` +
|
|
174
|
+
`wrapped for the address you typed, so it is not readable by whoever it was filed under.`,
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
const id = typeof created === "object" && created !== null ? Reflect.get(created, "id") : null;
|
|
178
|
+
return {
|
|
179
|
+
id: typeof id === "string" ? id : null,
|
|
180
|
+
name: entry.name,
|
|
181
|
+
recipient: crypt.share_address_display(recipientAddress),
|
|
182
|
+
fromIndex: keys.index,
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
finally {
|
|
186
|
+
dataKey.fill(0);
|
|
187
|
+
dek?.fill(0);
|
|
188
|
+
digest?.fill(0);
|
|
189
|
+
keys?.wipe();
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/** Who one file was shared with: the server's rows, unchanged, beside the file's path and id. */
|
|
193
|
+
export async function sentShares(account, target) {
|
|
194
|
+
const list = await readFileList(account.server, account.bearer, account.code, account.accountId);
|
|
195
|
+
if (list.manifest === null) {
|
|
196
|
+
throw new NmtsError("This account has no file list, so there is nothing to look up.", { exitCode: 4 });
|
|
197
|
+
}
|
|
198
|
+
const index = buildIndex(list.manifest.entries);
|
|
199
|
+
const entry = entryAt(list.manifest.entries, normalisePath(target), { nothingHappened: "Nothing was changed." });
|
|
200
|
+
if (entry.kind !== KIND_FILE) {
|
|
201
|
+
throw new NmtsError(`No file at "${fullPathOf(index, entry)}".`, {
|
|
202
|
+
exitCode: 4,
|
|
203
|
+
nextStep: "That is a folder, and a share is of one file.",
|
|
204
|
+
});
|
|
205
|
+
}
|
|
206
|
+
const answer = await request(account.server, `/v1/shares/sent?item_id=${encodeURIComponent(entry.id)}`, { token: account.bearer });
|
|
207
|
+
const rows = typeof answer === "object" && answer !== null ? Reflect.get(answer, "shares") : null;
|
|
208
|
+
if (!Array.isArray(rows)) {
|
|
209
|
+
throw new NmtsError("The server's answer did not carry a list of shares.", {
|
|
210
|
+
nextStep: "Nothing was changed. Report it rather than retrying — the shape, not the network, is wrong.",
|
|
211
|
+
});
|
|
212
|
+
}
|
|
213
|
+
return { path: fullPathOf(index, entry), itemId: entry.id, rows };
|
|
214
|
+
}
|
|
215
|
+
/** Withdraw a share this account sent, or remove one it was sent. A copy already taken stays taken. */
|
|
216
|
+
export async function removeShare(account, id) {
|
|
217
|
+
await request(account.server, `/v1/shares/${encodeURIComponent(id)}`, { token: account.bearer, method: "DELETE" });
|
|
218
|
+
}
|
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;
|
|
@@ -13,6 +13,7 @@
|
|
|
13
13
|
// costs the most: a zero would refuse an upload the wallet can afford.
|
|
14
14
|
import { NmtsError } from "./errors.js";
|
|
15
15
|
import { BINARY_NAME } from "./product.js";
|
|
16
|
+
import { RIDE_CEILING_BYTES } from "./upload-wire.js";
|
|
16
17
|
import { fits, leftoverBytes, statusOf, usableFirst } from "./shared/lib/storage-control/plan.js";
|
|
17
18
|
import { coinAmount } from "./wallet.js";
|
|
18
19
|
/** The browser's default rung, and the term one credit buys. A number of EPOCHS, not days. */
|
|
@@ -179,15 +180,24 @@ export function describeUploadReview(say, facts, budget) {
|
|
|
179
180
|
say(` This is paid in WAL and SUI from the wallet this NMTS key derives — not from credits, which`);
|
|
180
181
|
say(` is what \`${BINARY_NAME} put\` spends without --pay wallet. Nobody, NMTS included, can reverse it.`);
|
|
181
182
|
say(` The storage is the wallet's own: NMTS records the file and does not hold it.`);
|
|
182
|
-
// ⛔ SAID WHERE IT CHANGES WHAT SOMEBODY GETS, AND NOWHERE ELSE.
|
|
183
|
-
// the recovery list's copy
|
|
184
|
-
//
|
|
185
|
-
//
|
|
186
|
-
|
|
187
|
-
|
|
183
|
+
// ⛔ SAID WHERE IT CHANGES WHAT SOMEBODY GETS, AND NOWHERE ELSE. An account that never asked for
|
|
184
|
+
// the recovery list's copy has nothing to be told (`GET /v1/account/summary` `network_copy`).
|
|
185
|
+
// Since spec 2026-09-20 unit B a one-part upload onto new storage carries it, as the browser's
|
|
186
|
+
// small uploads do (`upload-recovery-patch.ts`); anything else still does not, and says so.
|
|
187
|
+
// Whether it rides was decided before the price, so the sentence says what happens, not "if".
|
|
188
|
+
const on = facts.carriesRecoveryList || facts.networkCopy === true;
|
|
189
|
+
if (on || facts.networkCopy === null) {
|
|
190
|
+
say(on
|
|
188
191
|
? ` This account asks for the recovery list to be copied to the storage network with its uploads.`
|
|
189
192
|
: ` Whether this account asks for the recovery list's storage-network copy could not be read.`);
|
|
190
|
-
|
|
191
|
-
|
|
193
|
+
const fits = facts.parts === 1 && facts.storage.kind === "buy" && facts.sealedBytes <= RIDE_CEILING_BYTES;
|
|
194
|
+
if (facts.carriesRecoveryList)
|
|
195
|
+
say(` The newest recovery list goes up with this file, and the WAL above includes it.`);
|
|
196
|
+
else if (fits)
|
|
197
|
+
say(` This upload does not include it. \`${BINARY_NAME} recovery-list\` saves the list as a file.`);
|
|
198
|
+
else {
|
|
199
|
+
say(` Only a one-part upload under 64 MiB onto new storage includes it, so this one does not.`);
|
|
200
|
+
say(` \`${BINARY_NAME} recovery-list\` saves the list as a file.`);
|
|
201
|
+
}
|
|
192
202
|
}
|
|
193
203
|
}
|