@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
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
import { chainQuorum } from "./node-certificate.js";
|
|
2
|
+
export const QUORUM_GRACE_MIN_MS = 2_000;
|
|
3
|
+
export const QUORUM_GRACE_MAX_MS = 20_000;
|
|
4
|
+
export const STALL_FLOOR_MS = 90_000;
|
|
5
|
+
export const RETRY_WAITS_MS = [3_000, 6_000, 12_000, 24_000];
|
|
6
|
+
/** So many shards lost that the rest can never reach the chain's quorum. */
|
|
7
|
+
const beyondHope = (lostShards, nShards) => !chainQuorum(nShards - lostShards, nShards);
|
|
8
|
+
/** Wait `ms`, or less when the signal aborts. */
|
|
9
|
+
const pause = (ms, signal) => new Promise((resolve) => {
|
|
10
|
+
const timer = setTimeout(resolve, ms);
|
|
11
|
+
signal.addEventListener("abort", () => (clearTimeout(timer), resolve()), { once: true });
|
|
12
|
+
});
|
|
13
|
+
/** Replace the client's all-nodes wait with quorum-then-grace. Returns the same client. */
|
|
14
|
+
export function stopAtQuorum(walrus, timing = {}) {
|
|
15
|
+
const now = timing.now ?? Date.now;
|
|
16
|
+
const stallFloorMs = timing.stallFloorMs ?? STALL_FLOOR_MS;
|
|
17
|
+
const waits = timing.retryWaitsMs ?? RETRY_WAITS_MS;
|
|
18
|
+
/** One node's write, asked again after a failure while the write is open. */
|
|
19
|
+
const writeOne = async (input, signal) => {
|
|
20
|
+
for (let attempt = 0;; attempt++) {
|
|
21
|
+
try {
|
|
22
|
+
return await walrus.writeEncodedBlobToNode(input);
|
|
23
|
+
}
|
|
24
|
+
catch (error) {
|
|
25
|
+
const wait = waits[attempt];
|
|
26
|
+
if (wait === undefined || signal.aborted)
|
|
27
|
+
throw error;
|
|
28
|
+
await pause(wait, signal);
|
|
29
|
+
if (signal.aborted)
|
|
30
|
+
throw error;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
const writeAll = async ({ blobId, metadata, sliversByNode, signal, ...options }) => {
|
|
35
|
+
const { committee } = await walrus.systemState();
|
|
36
|
+
const nShards = committee.n_shards;
|
|
37
|
+
const nNodes = sliversByNode.length;
|
|
38
|
+
const controller = new AbortController();
|
|
39
|
+
const linked = signal ? AbortSignal.any([controller.signal, signal]) : controller.signal;
|
|
40
|
+
const results = sliversByNode.map(() => null);
|
|
41
|
+
const started = now();
|
|
42
|
+
let okShards = 0;
|
|
43
|
+
let lostShards = 0;
|
|
44
|
+
let settled = 0;
|
|
45
|
+
return await new Promise((resolve, reject) => {
|
|
46
|
+
let graceTimer = null;
|
|
47
|
+
let stallTimer = null;
|
|
48
|
+
let stallMs = null;
|
|
49
|
+
let done = false;
|
|
50
|
+
const tooMany = (why) => new Error(`Too many failures while writing blob ${blobId} to nodes (${why})`);
|
|
51
|
+
const quorum = () => chainQuorum(okShards, nShards);
|
|
52
|
+
const finish = (error) => {
|
|
53
|
+
if (done)
|
|
54
|
+
return;
|
|
55
|
+
done = true;
|
|
56
|
+
if (graceTimer !== null)
|
|
57
|
+
clearTimeout(graceTimer);
|
|
58
|
+
if (stallTimer !== null)
|
|
59
|
+
clearTimeout(stallTimer);
|
|
60
|
+
controller.abort(error ?? new Error("quorum reached; the remaining node writes were stopped"));
|
|
61
|
+
if (error)
|
|
62
|
+
reject(error);
|
|
63
|
+
else
|
|
64
|
+
resolve([...results]);
|
|
65
|
+
};
|
|
66
|
+
const check = () => {
|
|
67
|
+
if (settled === nNodes)
|
|
68
|
+
return finish(quorum() ? undefined : tooMany("no quorum"));
|
|
69
|
+
if (graceTimer === null && quorum()) {
|
|
70
|
+
const grace = Math.min(Math.max(now() - started, QUORUM_GRACE_MIN_MS), QUORUM_GRACE_MAX_MS);
|
|
71
|
+
graceTimer = setTimeout(() => finish(), grace);
|
|
72
|
+
}
|
|
73
|
+
if (stallMs === null && okShards * 2 > nShards)
|
|
74
|
+
stallMs = Math.max((now() - started) * 4, stallFloorMs);
|
|
75
|
+
if (stallMs !== null) {
|
|
76
|
+
if (stallTimer !== null)
|
|
77
|
+
clearTimeout(stallTimer);
|
|
78
|
+
stallTimer = setTimeout(() => finish(quorum() ? undefined : tooMany(`${nNodes - settled} nodes did not answer`)), stallMs);
|
|
79
|
+
}
|
|
80
|
+
};
|
|
81
|
+
sliversByNode.forEach((slivers, nodeIndex) => {
|
|
82
|
+
const shards = slivers.primary.length;
|
|
83
|
+
writeOne({ blobId, nodeIndex, metadata, slivers, signal: linked, ...options }, linked)
|
|
84
|
+
.then((confirmation) => {
|
|
85
|
+
results[nodeIndex] = confirmation;
|
|
86
|
+
okShards += shards;
|
|
87
|
+
}, () => {
|
|
88
|
+
lostShards += shards;
|
|
89
|
+
})
|
|
90
|
+
.finally(() => {
|
|
91
|
+
settled += 1;
|
|
92
|
+
if (done)
|
|
93
|
+
return;
|
|
94
|
+
if (beyondHope(lostShards, nShards))
|
|
95
|
+
return finish(tooMany("refused"));
|
|
96
|
+
check();
|
|
97
|
+
});
|
|
98
|
+
});
|
|
99
|
+
});
|
|
100
|
+
};
|
|
101
|
+
walrus.writeEncodedBlobToNodes = writeAll;
|
|
102
|
+
return walrus;
|
|
103
|
+
}
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The tip a relay charges, as parsed from its `GET /v1/tip-config`. A relay is free (`none`),
|
|
3
|
+
* flat (`const`), or size-linear (`linear`). `unknown` means the endpoint answered but we could
|
|
4
|
+
* not understand its tip shape — REACHABLE BUT UNUSABLE (scoring gates it out, never guesses a
|
|
5
|
+
* price). All MIST amounts are bigint.
|
|
6
|
+
*/
|
|
7
|
+
export type RelayTip = {
|
|
8
|
+
readonly kind: "none";
|
|
9
|
+
} | {
|
|
10
|
+
readonly kind: "const";
|
|
11
|
+
readonly address: string;
|
|
12
|
+
readonly baseMist: bigint;
|
|
13
|
+
} | {
|
|
14
|
+
readonly kind: "linear";
|
|
15
|
+
readonly address: string;
|
|
16
|
+
readonly baseMist: bigint;
|
|
17
|
+
/** MIST added per KiB of ENCODED size (Walrus config `encoded_size_mul_per_kib`). */
|
|
18
|
+
readonly mulPerKibMist: bigint;
|
|
19
|
+
} | {
|
|
20
|
+
readonly kind: "unknown";
|
|
21
|
+
readonly detail: string;
|
|
22
|
+
};
|
|
23
|
+
/**
|
|
24
|
+
* Parse a relay's `/v1/tip-config` JSON into a RelayTip, DEFENSIVELY (an unrecognized/garbled shape
|
|
25
|
+
* becomes `unknown`, never a guessed price). Recognized shapes:
|
|
26
|
+
* no-tip: { "no_tip": ... } → none
|
|
27
|
+
* const: { "send_tip": { "address", "kind": { "const": N } } } → const
|
|
28
|
+
* linear: { "send_tip": { "address",
|
|
29
|
+
* "kind": { "linear": { "base": N,
|
|
30
|
+
* "encoded_size_mul_per_kib": M } } } } → linear
|
|
31
|
+
* Tolerates a missing `send_tip` wrapper (reads `kind` off the root) and camelCase `sendTip`.
|
|
32
|
+
*/
|
|
33
|
+
export declare function parseTipConfig(raw: unknown): RelayTip;
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// A relay's tip, read from its `GET /v1/tip-config`. Kept apart from the relay pool so the
|
|
2
|
+
// command-line tool reads a tip with the same parser the browser uses (it copies this file).
|
|
3
|
+
/** Coerce a JSON tip amount (number | numeric string | bigint) to a non-negative bigint, or null. */
|
|
4
|
+
function toMist(value) {
|
|
5
|
+
if (typeof value === "number")
|
|
6
|
+
return Number.isInteger(value) && value >= 0 ? BigInt(value) : null;
|
|
7
|
+
if (typeof value === "bigint")
|
|
8
|
+
return value >= 0n ? value : null;
|
|
9
|
+
if (typeof value === "string")
|
|
10
|
+
return /^\d+$/.test(value) ? BigInt(value) : null;
|
|
11
|
+
return null;
|
|
12
|
+
}
|
|
13
|
+
/** The value under `key` when `value` is a JSON object that has it; `undefined` otherwise. */
|
|
14
|
+
function field(value, key) {
|
|
15
|
+
return typeof value === "object" && value !== null && key in value ? Reflect.get(value, key) : undefined;
|
|
16
|
+
}
|
|
17
|
+
function isObject(value) {
|
|
18
|
+
return typeof value === "object" && value !== null;
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* Parse a relay's `/v1/tip-config` JSON into a RelayTip, DEFENSIVELY (an unrecognized/garbled shape
|
|
22
|
+
* becomes `unknown`, never a guessed price). Recognized shapes:
|
|
23
|
+
* no-tip: { "no_tip": ... } → none
|
|
24
|
+
* const: { "send_tip": { "address", "kind": { "const": N } } } → const
|
|
25
|
+
* linear: { "send_tip": { "address",
|
|
26
|
+
* "kind": { "linear": { "base": N,
|
|
27
|
+
* "encoded_size_mul_per_kib": M } } } } → linear
|
|
28
|
+
* Tolerates a missing `send_tip` wrapper (reads `kind` off the root) and camelCase `sendTip`.
|
|
29
|
+
*/
|
|
30
|
+
export function parseTipConfig(raw) {
|
|
31
|
+
// A no-tip relay answers the bare JSON string "no_tip" (measured on Mysten's relay image v1.60.1,
|
|
32
|
+
// 2026-11-09; the SDK reads any string as "no tip"). Read as an object, a free relay would be
|
|
33
|
+
// "unknown" and could never be chosen.
|
|
34
|
+
if (raw === "no_tip")
|
|
35
|
+
return { kind: "none" };
|
|
36
|
+
if (!isObject(raw)) {
|
|
37
|
+
return { kind: "unknown", detail: "tip-config was not a JSON object" };
|
|
38
|
+
}
|
|
39
|
+
if ("no_tip" in raw || field(raw, "tip") === "no_tip")
|
|
40
|
+
return { kind: "none" };
|
|
41
|
+
const sendTip = field(raw, "send_tip") ?? field(raw, "sendTip");
|
|
42
|
+
const node = isObject(sendTip) ? sendTip : raw;
|
|
43
|
+
const addressValue = field(node, "address");
|
|
44
|
+
const address = typeof addressValue === "string" ? addressValue : "";
|
|
45
|
+
const kind = field(node, "kind");
|
|
46
|
+
if (!isObject(kind)) {
|
|
47
|
+
return { kind: "unknown", detail: "tip-config had no recognizable send_tip.kind" };
|
|
48
|
+
}
|
|
49
|
+
if ("const" in kind) {
|
|
50
|
+
const baseMist = toMist(field(kind, "const"));
|
|
51
|
+
if (baseMist === null)
|
|
52
|
+
return { kind: "unknown", detail: "const tip amount was not a non-negative integer" };
|
|
53
|
+
return { kind: "const", address, baseMist };
|
|
54
|
+
}
|
|
55
|
+
if ("linear" in kind) {
|
|
56
|
+
const linear = field(kind, "linear");
|
|
57
|
+
if (!isObject(linear)) {
|
|
58
|
+
return { kind: "unknown", detail: "linear tip had no body" };
|
|
59
|
+
}
|
|
60
|
+
const baseMist = toMist(field(linear, "base") ?? 0);
|
|
61
|
+
const mulPerKibMist = toMist(field(linear, "encoded_size_mul_per_kib"));
|
|
62
|
+
if (baseMist === null || mulPerKibMist === null) {
|
|
63
|
+
return { kind: "unknown", detail: "linear tip base/encoded_size_mul_per_kib were not non-negative integers" };
|
|
64
|
+
}
|
|
65
|
+
return { kind: "linear", address, baseMist, mulPerKibMist };
|
|
66
|
+
}
|
|
67
|
+
return { kind: "unknown", detail: "unrecognized tip kind (expected const | linear | no_tip)" };
|
|
68
|
+
}
|
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-road.d.ts
CHANGED
|
@@ -4,12 +4,24 @@ export declare const OWN_RELAY_PATH = "/v1/relay";
|
|
|
4
4
|
export declare const OWN_RELAY_PART_BYTES: number;
|
|
5
5
|
/** How long Mysten's relay gets to answer before this run takes the own road. */
|
|
6
6
|
export declare const ROAD_PROBE_TIMEOUT_MS = 10000;
|
|
7
|
+
/**
|
|
8
|
+
* The "relay address" of the direct road: no host, the storage nodes themselves (D51). The same
|
|
9
|
+
* value as the browser's (`web/src/lib/walrus/node-road.ts`), and it is what a resumable upload's
|
|
10
|
+
* record keeps, so a resumed part goes back to the nodes.
|
|
11
|
+
*/
|
|
12
|
+
export declare const NODE_ROAD = "walrus:storage-nodes";
|
|
13
|
+
/** True for the direct road. */
|
|
14
|
+
export declare function isNodeRoad(relayUrl: string): boolean;
|
|
15
|
+
/** True for a road that takes no tip: the own relay and the storage nodes. */
|
|
16
|
+
export declare function isTipFree(relayUrl: string): boolean;
|
|
7
17
|
/** True for the own relay's address on any NMTS server. */
|
|
8
18
|
export declare function isOwnRelay(relayUrl: string): boolean;
|
|
9
19
|
/**
|
|
10
|
-
* Decide this run's road, once, before anything is priced. True when
|
|
20
|
+
* Decide this run's road, once, before anything is priced. True when a small road (the storage
|
|
21
|
+
* nodes, or the own relay) was taken.
|
|
11
22
|
*
|
|
12
|
-
* `probe` is the request that asks Mysten's relay (tests pass a fake one).
|
|
23
|
+
* `probe` is the request that asks Mysten's relay (tests pass a fake one). `nodeRoad: false` is a
|
|
24
|
+
* machine that cannot write to the storage nodes itself; it goes to the own relay, as before D51.
|
|
13
25
|
*/
|
|
14
26
|
export declare function chooseUploadRoad(input: {
|
|
15
27
|
network: string;
|
|
@@ -17,12 +29,22 @@ export declare function chooseUploadRoad(input: {
|
|
|
17
29
|
apiKey: string;
|
|
18
30
|
probe?: typeof fetch;
|
|
19
31
|
timeoutMs?: number;
|
|
32
|
+
nodeRoad?: boolean;
|
|
33
|
+
/** A wallet upload: the person pays the tip, so the other company relays may carry it. */
|
|
34
|
+
otherCompanies?: boolean;
|
|
35
|
+
/** A credit upload: the other company relays may carry it while their tip fits the credit ceiling. */
|
|
36
|
+
credit?: boolean;
|
|
20
37
|
}): Promise<boolean>;
|
|
38
|
+
/**
|
|
39
|
+
* The own relay to try when a direct write failed, with the fetch that carries this account's key,
|
|
40
|
+
* or null when this run did not choose the small roads (a named relay, or Mysten's).
|
|
41
|
+
*/
|
|
42
|
+
export declare function ownRelayFallback(): string | null;
|
|
21
43
|
/** Forget the choice (tests; a library caller between runs). */
|
|
22
44
|
export declare function resetUploadRoad(): void;
|
|
23
|
-
/** The
|
|
45
|
+
/** The road this run writes through: the storage nodes or the own relay when chosen, else the usual relay. */
|
|
24
46
|
export declare function uploadRelay(network: string): string;
|
|
25
|
-
/** The part size a fresh upload uses: unchanged on Mysten's road, at most 64 MiB on
|
|
47
|
+
/** The part size a fresh upload uses: unchanged on Mysten's road, at most 64 MiB on the small roads. */
|
|
26
48
|
export declare function partSizeForRoad(partSize: number): number;
|
|
27
49
|
/**
|
|
28
50
|
* The fetch the relay client uses. On the own road it carries this account's key, and while another
|