@needmoretruth/nmts-cli 0.45.0 → 0.46.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/AGENTS.md +2 -2
- package/CHANGELOG.md +61 -0
- package/README.ko.md +6 -3
- package/README.md +15 -3
- package/dist/api-advice.js +17 -2
- package/dist/api.d.ts +22 -0
- package/dist/api.js +49 -1
- package/dist/arg-options.d.ts +8 -0
- package/dist/args.js +7 -1
- package/dist/artifact-about.d.ts +1 -1
- package/dist/collision.d.ts +20 -3
- package/dist/collision.js +21 -3
- package/dist/commands/erase.js +1 -0
- package/dist/commands/handover.d.ts +13 -0
- package/dist/commands/handover.js +328 -0
- package/dist/commands/heavy-run.d.ts +59 -0
- package/dist/commands/heavy-run.js +247 -0
- package/dist/commands/heavy.d.ts +7 -0
- package/dist/commands/heavy.js +123 -0
- package/dist/commands/ls.d.ts +5 -0
- package/dist/commands/ls.js +9 -1
- package/dist/commands/public-code.d.ts +6 -0
- package/dist/commands/public-code.js +46 -0
- package/dist/commands/push-heavy.d.ts +6 -0
- package/dist/commands/push-heavy.js +128 -0
- package/dist/commands/push-tree.d.ts +24 -0
- package/dist/commands/push-tree.js +91 -0
- package/dist/commands/push.d.ts +4 -13
- package/dist/commands/push.js +7 -77
- package/dist/commands/put-heavy.d.ts +9 -0
- package/dist/commands/put-heavy.js +120 -0
- package/dist/commands/put-payer.d.ts +21 -0
- package/dist/commands/put-payer.js +24 -0
- package/dist/commands/put.d.ts +2 -2
- package/dist/commands/put.js +8 -7
- package/dist/commands/receive.js +3 -3
- package/dist/commands/s3.js +44 -10
- package/dist/crypto-surface.d.ts +8 -0
- package/dist/crypto-surface.js +3 -0
- package/dist/crypto.d.ts +3 -0
- package/dist/crypto.js +3 -0
- package/dist/download-part.d.ts +32 -1
- package/dist/download-part.js +95 -6
- package/dist/download-range.d.ts +19 -0
- package/dist/download-range.js +108 -0
- package/dist/download-sink.d.ts +15 -0
- package/dist/download.d.ts +14 -0
- package/dist/download.js +19 -4
- package/dist/handover.d.ts +44 -0
- package/dist/handover.js +199 -0
- package/dist/heavy-api.d.ts +36 -0
- package/dist/heavy-api.js +107 -0
- package/dist/heavy-copy.d.ts +72 -0
- package/dist/heavy-copy.js +108 -0
- package/dist/heavy-evm.d.ts +20 -0
- package/dist/heavy-evm.js +106 -0
- package/dist/heavy-self-pay.d.ts +92 -0
- package/dist/heavy-self-pay.js +167 -0
- package/dist/heavy-upload.d.ts +87 -0
- package/dist/heavy-upload.js +154 -0
- package/dist/heavy-wallet-pay.d.ts +39 -0
- package/dist/heavy-wallet-pay.js +73 -0
- package/dist/help-options.js +4 -2
- package/dist/help.js +16 -3
- package/dist/list-tier.d.ts +7 -0
- package/dist/list-tier.js +47 -0
- package/dist/main.js +12 -10
- package/dist/manifest-write.d.ts +4 -3
- package/dist/platform-sign.d.ts +4 -4
- package/dist/platform-sign.js +3 -3
- package/dist/portable.d.ts +15 -0
- package/dist/portable.js +11 -0
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/recovery-build.js +16 -0
- package/dist/recovery-map-part.d.ts +82 -0
- package/dist/recovery-map-part.js +83 -0
- package/dist/recovery-map.d.ts +5 -40
- package/dist/recovery-map.js +18 -1
- package/dist/recovery-source.d.ts +3 -0
- package/dist/recovery-source.js +9 -0
- package/dist/risk.d.ts +30 -0
- package/dist/risk.js +23 -2
- package/dist/run-log.d.ts +1 -1
- package/dist/run-log.js +7 -1
- package/dist/s3/address.d.ts +20 -0
- package/dist/s3/address.js +97 -0
- package/dist/s3/answer.d.ts +48 -0
- package/dist/s3/answer.js +330 -0
- package/dist/s3/body-deframe.d.ts +49 -0
- package/dist/s3/body-deframe.js +264 -0
- package/dist/s3/body-measure.d.ts +13 -0
- package/dist/s3/body-measure.js +48 -0
- package/dist/s3/body-plan.d.ts +31 -0
- package/dist/s3/body-plan.js +159 -0
- package/dist/s3/body.d.ts +46 -0
- package/dist/s3/body.js +117 -0
- package/dist/s3/call.d.ts +36 -0
- package/dist/s3/call.js +42 -0
- package/dist/s3/checksum.d.ts +26 -0
- package/dist/s3/checksum.js +176 -0
- package/dist/s3/content-type.d.ts +3 -0
- package/dist/s3/content-type.js +49 -0
- package/dist/s3/contract.d.ts +91 -14
- package/dist/s3/drive-conditions.d.ts +47 -0
- package/dist/s3/drive-conditions.js +134 -0
- package/dist/s3/drive-key.d.ts +11 -0
- package/dist/s3/drive-key.js +58 -0
- package/dist/s3/drive-limits.d.ts +33 -0
- package/dist/s3/drive-limits.js +53 -0
- package/dist/s3/drive-lock.d.ts +11 -0
- package/dist/s3/drive-lock.js +47 -0
- package/dist/s3/drive-trashed-name.d.ts +13 -0
- package/dist/s3/drive-trashed-name.js +59 -0
- package/dist/s3/drive-write.d.ts +38 -0
- package/dist/s3/drive-write.js +258 -0
- package/dist/s3/drive.d.ts +59 -25
- package/dist/s3/drive.js +46 -77
- package/dist/s3/listing.d.ts +27 -1
- package/dist/s3/listing.js +62 -20
- package/dist/s3/long-answer.d.ts +12 -0
- package/dist/s3/long-answer.js +104 -0
- package/dist/s3/multipart-assemble.d.ts +17 -0
- package/dist/s3/multipart-assemble.js +68 -0
- package/dist/s3/multipart-sweep.d.ts +15 -0
- package/dist/s3/multipart-sweep.js +87 -0
- package/dist/s3/multipart-view.d.ts +45 -0
- package/dist/s3/multipart-view.js +232 -0
- package/dist/s3/multipart.d.ts +26 -19
- package/dist/s3/multipart.js +193 -78
- package/dist/s3/object-read.d.ts +17 -0
- package/dist/s3/object-read.js +127 -0
- package/dist/s3/object-write.d.ts +35 -0
- package/dist/s3/object-write.js +198 -0
- package/dist/s3/range.d.ts +14 -0
- package/dist/s3/range.js +80 -0
- package/dist/s3/response-sink.d.ts +12 -2
- package/dist/s3/response-sink.js +71 -7
- package/dist/s3/routes.d.ts +0 -1
- package/dist/s3/routes.js +242 -217
- package/dist/s3/same-file.d.ts +4 -1
- package/dist/s3/same-file.js +12 -3
- package/dist/s3/server.d.ts +54 -4
- package/dist/s3/server.js +174 -14
- package/dist/s3/sigv4-canonical.d.ts +91 -0
- package/dist/s3/sigv4-canonical.js +148 -0
- package/dist/s3/sigv4-presigned.d.ts +13 -0
- package/dist/s3/sigv4-presigned.js +82 -0
- package/dist/s3/sigv4.d.ts +26 -27
- package/dist/s3/sigv4.js +137 -102
- package/dist/s3/spool.d.ts +37 -0
- package/dist/s3/spool.js +104 -0
- package/dist/s3/staging.d.ts +90 -7
- package/dist/s3/staging.js +97 -66
- package/dist/s3/xml-multipart.d.ts +38 -0
- package/dist/s3/xml-multipart.js +47 -0
- package/dist/s3/xml-read.d.ts +28 -0
- package/dist/s3/xml-read.js +235 -0
- package/dist/s3/xml.d.ts +45 -2
- package/dist/s3/xml.js +72 -13
- package/dist/s3-gateway.d.ts +11 -5
- package/dist/s3-gateway.js +5 -2
- package/dist/seal.d.ts +1 -1
- package/dist/share.d.ts +5 -0
- package/dist/share.js +1 -1
- package/dist/shared/lib/api/types-heavy.d.ts +167 -0
- package/dist/shared/lib/api/types-heavy.js +60 -0
- package/dist/shared/lib/drive/rebuild-verify.js +1 -0
- package/dist/shared/lib/filecoin/providers.d.ts +25 -0
- package/dist/shared/lib/filecoin/providers.js +58 -0
- package/dist/shared/lib/heavy/download.d.ts +48 -0
- package/dist/shared/lib/heavy/download.js +116 -0
- package/dist/shared/lib/heavy/order-plan.d.ts +77 -0
- package/dist/shared/lib/heavy/order-plan.js +83 -0
- package/dist/shared/lib/heavy/order-runner.d.ts +9 -0
- package/dist/shared/lib/heavy/order-runner.js +300 -0
- package/dist/shared/lib/heavy/order-types.d.ts +90 -0
- package/dist/shared/lib/heavy/order-types.js +6 -0
- package/dist/shared/lib/heavy/order-wire.d.ts +55 -0
- package/dist/shared/lib/heavy/order-wire.js +113 -0
- package/dist/shared/lib/heavy/piece-cid.d.ts +24 -0
- package/dist/shared/lib/heavy/piece-cid.js +55 -0
- package/dist/shared/lib/heavy/sp-upload.d.ts +37 -0
- package/dist/shared/lib/heavy/sp-upload.js +112 -0
- package/dist/shared/lib/heavy/synapse-piece.d.ts +1 -0
- package/dist/shared/lib/heavy/synapse-piece.js +8 -0
- package/dist/shared/lib/share/handover-format.d.ts +129 -0
- package/dist/shared/lib/share/handover-format.js +323 -0
- package/dist/shared/lib/share/shared-file-info.js +1 -0
- package/dist/shared/lib/storage-network.d.ts +4 -4
- package/dist/shared/lib/storage-network.js +7 -7
- package/dist/shared/lib/storage-tier.d.ts +28 -0
- package/dist/shared/lib/storage-tier.js +63 -0
- package/dist/shared/lib/wallet/send-rules.js +1 -0
- package/dist/upload-file.d.ts +8 -0
- package/dist/upload-file.js +57 -25
- package/dist/upload-price-node.d.ts +3 -0
- package/dist/upload-price-node.js +3 -6
- package/dist/upload-price.d.ts +10 -0
- package/dist/upload-price.js +13 -1
- package/dist/upload-run.d.ts +80 -0
- package/dist/upload-run.js +173 -0
- package/dist/upload-steps.js +15 -4
- package/dist/upload-store.d.ts +15 -6
- package/dist/upload-store.js +6 -0
- package/dist/upload-wallet-put.d.ts +9 -4
- package/dist/upload-wallet-put.js +16 -6
- package/dist/upload-wallet.js +4 -0
- package/dist/upload-wire.d.ts +25 -0
- package/dist/upload-wire.js +20 -1
- package/dist/upload.js +28 -13
- package/dist/wallet-sign.js +2 -2
- package/docs/commands/handover.md +38 -0
- package/docs/commands/heavy.md +35 -0
- package/docs/commands/public-code.md +7 -0
- package/docs/commands/push.md +4 -1
- package/docs/commands/put.md +15 -1
- package/docs/commands/share.md +3 -1
- package/package.json +5 -2
- package/server.json +2 -2
- package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +17 -1
- package/vendor/nmts-crypto/nmts_crypto_wasm.js +28 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +2 -1
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
// The two files a person can pass around outside NMTS: the HANDOVER file, which carries one file
|
|
2
|
+
// sealed to one recipient, and the PUBLIC CODE file, which lets a sender seal to somebody without
|
|
3
|
+
// asking the server for their identity. The byte layouts are fixed by the format specification
|
|
4
|
+
// (NCF-3 §5.6 and §5.7); this module is the JSON around them, and nothing else.
|
|
5
|
+
//
|
|
6
|
+
// ⛔ NO CRYPTOGRAPHY HERE, AND NO IMPORTS. Everything that needs a key or a hash happens in the
|
|
7
|
+
// caller — the browser's crypto worker, or the command-line tool's engine. This module only
|
|
8
|
+
// builds and reads the text, so both programs refuse the same files for the same reasons. It is
|
|
9
|
+
// copied byte for byte into the command-line package.
|
|
10
|
+
//
|
|
11
|
+
// ⛔ IT REFUSES EXACTLY WHAT §5.6 AND §5.7 SAY A READER REFUSES, and nothing else. The specification
|
|
12
|
+
// lists every refusal (the size cap, the closed key sets, the `note` shape, the version rule, the
|
|
13
|
+
// network, the lengths, canonical base64url); a refusal added here and not there makes two
|
|
14
|
+
// readers of one published format disagree about one file.
|
|
15
|
+
//
|
|
16
|
+
// ⚠ THE ITEM ID IS COMPARED AS WRITTEN. §5.3 lowercases it before binding, so "ABC" and "abc" bind
|
|
17
|
+
// the same. A reader that accepted "ABC" would show text different from what was bound, so an item
|
|
18
|
+
// id that is not already lowercase is refused here.
|
|
19
|
+
/** Wire values of the handover file. Fixed forever once a file carries them. */
|
|
20
|
+
export const HANDOVER_FORMAT = "nmts-handover";
|
|
21
|
+
export const HANDOVER_VERSION = 1;
|
|
22
|
+
/** Marker inside the sealed parts document. */
|
|
23
|
+
export const HANDOVER_PARTS_FORMAT = "nmts-handover-parts/1";
|
|
24
|
+
/** Marker of the name document — the §5.4 share document, which a handover extends by two keys. */
|
|
25
|
+
export const SHARE_FILE_FORMAT = "nmts-share-file/1";
|
|
26
|
+
/** Wire values of the public code file. */
|
|
27
|
+
export const PUBLIC_CODE_FORMAT = "nmts-public-code";
|
|
28
|
+
export const PUBLIC_CODE_VERSION = 1;
|
|
29
|
+
/** File name endings. */
|
|
30
|
+
export const HANDOVER_EXTENSION = ".nmtshandover";
|
|
31
|
+
export const PUBLIC_CODE_EXTENSION = ".nmtscode";
|
|
32
|
+
/**
|
|
33
|
+
* What a person who opens the file in a text editor reads. Informational; not read back.
|
|
34
|
+
* The two place names are the labels on the screen: the side panel entry and the button.
|
|
35
|
+
*/
|
|
36
|
+
export const HANDOVER_NOTE = [
|
|
37
|
+
"An NMTS handover file. Sign in at nmts.me with the NMTS key it was made for, then open it from Shared with me → Open handover file.",
|
|
38
|
+
"NMTS 건네기 파일입니다. 이 파일을 받을 NMTS 키로 nmts.me에 로그인한 뒤 「받은 공유함」 → 「건네기 파일 열기」에서 여십시오.", // shown in Korean
|
|
39
|
+
];
|
|
40
|
+
/** Size caps on the whole text, checked before it is parsed (§5.6, §5.7). */
|
|
41
|
+
export const MAX_HANDOVER_FILE_BYTES = 1024 * 1024;
|
|
42
|
+
export const MAX_PUBLIC_CODE_FILE_BYTES = 8 * 1024;
|
|
43
|
+
/** Exact byte lengths the specification fixes. */
|
|
44
|
+
const IDENTITY_LEN = 4989;
|
|
45
|
+
const ENVELOPE_LEN = 1240;
|
|
46
|
+
const DIGEST_ENVELOPE_LEN = 104;
|
|
47
|
+
const SHA256_LEN = 32;
|
|
48
|
+
/** nonce(24) + commitment(32) + tag(16): the smallest sealed value, with an empty plaintext. */
|
|
49
|
+
const MIN_SEALED_LEN = 72;
|
|
50
|
+
/** Field bounds (§5.6). Together they keep a file under MAX_HANDOVER_FILE_BYTES. */
|
|
51
|
+
export const MAX_NAME_SEALED_LEN = 64 * 1024;
|
|
52
|
+
const MAX_PARTS_SEALED_LEN = 640 * 1024;
|
|
53
|
+
const MAX_PARTS = 4096;
|
|
54
|
+
const MAX_EPOCH = 0xffff_ffff;
|
|
55
|
+
const MAX_CODE_CHARS = 64;
|
|
56
|
+
const UUID_LOWER = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
57
|
+
const B64URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
58
|
+
const B64URL = /^[A-Za-z0-9_-]*$/;
|
|
59
|
+
/** Blob and patch ids go into an aggregator URL, so they are held to the base64url alphabet. */
|
|
60
|
+
const STORAGE_ID = /^[A-Za-z0-9_-]{1,256}$/;
|
|
61
|
+
export class HandoverFormatError extends Error {
|
|
62
|
+
problem;
|
|
63
|
+
/** The field at fault, for `bad-field`. */
|
|
64
|
+
field;
|
|
65
|
+
constructor(problem, field = null) {
|
|
66
|
+
super(field === null ? `handover file: ${problem}` : `handover file: ${problem} (${field})`);
|
|
67
|
+
this.name = "HandoverFormatError";
|
|
68
|
+
this.problem = problem;
|
|
69
|
+
this.field = field;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Bytes behind an unpadded base64url string, or null when it is not one in CANONICAL form: the
|
|
74
|
+
* bits the last character carries beyond the data must be zero, so one byte string has one text.
|
|
75
|
+
*/
|
|
76
|
+
export function base64UrlByteLength(text) {
|
|
77
|
+
if (!B64URL.test(text))
|
|
78
|
+
return null;
|
|
79
|
+
const rest = text.length % 4;
|
|
80
|
+
if (rest === 1)
|
|
81
|
+
return null;
|
|
82
|
+
if (rest !== 0) {
|
|
83
|
+
const last = B64URL_ALPHABET.indexOf(text.charAt(text.length - 1));
|
|
84
|
+
const unused = rest === 2 ? 0b1111 : 0b11;
|
|
85
|
+
if ((last & unused) !== 0)
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
return Math.floor(text.length / 4) * 3 + (rest === 0 ? 0 : rest - 1);
|
|
89
|
+
}
|
|
90
|
+
/** `nmts-handover-<YYYY-MM-DD>.nmtshandover`, the date in UTC. The file's own name is not in it. */
|
|
91
|
+
export function handoverFileName(now) {
|
|
92
|
+
return `nmts-handover-${now.toISOString().slice(0, 10)}${HANDOVER_EXTENSION}`;
|
|
93
|
+
}
|
|
94
|
+
/** `nmts-public-code-<public code>.nmtscode`. The code is public, and it tells two such files apart. */
|
|
95
|
+
export function publicCodeFileName(code) {
|
|
96
|
+
return `nmts-public-code-${code.replace(/[^0-9A-Za-z-]/g, "")}${PUBLIC_CODE_EXTENSION}`;
|
|
97
|
+
}
|
|
98
|
+
/** The handover file's text. Pretty-printed: somebody may well open it to see what it is. */
|
|
99
|
+
export function encodeHandover(file) {
|
|
100
|
+
checkHandover(file);
|
|
101
|
+
const { network, item, sender, envelope, name, hash, parts } = file;
|
|
102
|
+
const doc = { format: HANDOVER_FORMAT, version: HANDOVER_VERSION, network, item, sender, envelope, name, hash, parts, note: HANDOVER_NOTE };
|
|
103
|
+
return `${JSON.stringify(doc, null, 2)}\n`;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Read a handover file's text, in the order §5.6 gives: size, JSON, format, version, network, key
|
|
107
|
+
* set, `note`, fields. `network` is the reader's own. Throws `HandoverFormatError`; never returns a
|
|
108
|
+
* partial file.
|
|
109
|
+
*/
|
|
110
|
+
export function decodeHandover(text, network) {
|
|
111
|
+
capBytes(text, MAX_HANDOVER_FILE_BYTES);
|
|
112
|
+
const doc = objectOf(text);
|
|
113
|
+
if (doc["format"] !== HANDOVER_FORMAT)
|
|
114
|
+
throw new HandoverFormatError("wrong-format");
|
|
115
|
+
checkVersion(doc["version"], HANDOVER_VERSION);
|
|
116
|
+
const fileNetwork = doc["network"];
|
|
117
|
+
if (fileNetwork !== "mainnet" && fileNetwork !== "testnet")
|
|
118
|
+
throw new HandoverFormatError("bad-field", "network");
|
|
119
|
+
if (fileNetwork !== network)
|
|
120
|
+
throw new HandoverFormatError("other-network");
|
|
121
|
+
closedKeys(doc, ["format", "version", "network", "item", "sender", "envelope", "name", "hash", "parts", "note"]);
|
|
122
|
+
const note = doc["note"];
|
|
123
|
+
if (note !== undefined && !(Array.isArray(note) && note.every((line) => typeof line === "string"))) {
|
|
124
|
+
throw new HandoverFormatError("bad-field", "note");
|
|
125
|
+
}
|
|
126
|
+
const file = {
|
|
127
|
+
network: fileNetwork,
|
|
128
|
+
item: stringField(doc, "item"),
|
|
129
|
+
sender: stringField(doc, "sender"),
|
|
130
|
+
envelope: stringField(doc, "envelope"),
|
|
131
|
+
name: stringField(doc, "name"),
|
|
132
|
+
hash: stringField(doc, "hash"),
|
|
133
|
+
parts: stringField(doc, "parts"),
|
|
134
|
+
};
|
|
135
|
+
checkHandover(file);
|
|
136
|
+
return file;
|
|
137
|
+
}
|
|
138
|
+
/** The name document, exactly as it is sealed. */
|
|
139
|
+
export function encodeHandoverName(doc) {
|
|
140
|
+
checkName(doc);
|
|
141
|
+
return JSON.stringify({ f: SHARE_FILE_FORMAT, name: doc.name, size: doc.size, network: doc.network, parts_sha256: doc.partsSha256 });
|
|
142
|
+
}
|
|
143
|
+
/** Whether a name document, once sealed, fits the `name` field. The caller checks before sealing. */
|
|
144
|
+
export function handoverNameFits(encoded) {
|
|
145
|
+
return new TextEncoder().encode(encoded).length + MIN_SEALED_LEN <= MAX_NAME_SEALED_LEN;
|
|
146
|
+
}
|
|
147
|
+
/** Read an opened name document. Throws `HandoverFormatError` (`bad-field`, `name`). */
|
|
148
|
+
export function decodeHandoverName(text) {
|
|
149
|
+
const doc = objectOf(text);
|
|
150
|
+
const bad = new HandoverFormatError("bad-field", "name");
|
|
151
|
+
const keys = Object.keys(doc).sort().join(",");
|
|
152
|
+
if (keys !== "f,name,network,parts_sha256,size" || doc["f"] !== SHARE_FILE_FORMAT)
|
|
153
|
+
throw bad;
|
|
154
|
+
const name = doc["name"];
|
|
155
|
+
const size = doc["size"];
|
|
156
|
+
const network = doc["network"];
|
|
157
|
+
const partsSha256 = doc["parts_sha256"];
|
|
158
|
+
if (typeof name !== "string" || typeof size !== "number" || typeof partsSha256 !== "string")
|
|
159
|
+
throw bad;
|
|
160
|
+
if (network !== "mainnet" && network !== "testnet")
|
|
161
|
+
throw bad;
|
|
162
|
+
const out = { name, size, network, partsSha256 };
|
|
163
|
+
checkName(out);
|
|
164
|
+
return out;
|
|
165
|
+
}
|
|
166
|
+
/** The parts document, exactly as it is sealed. Compact: nobody reads it but the recipient's engine. */
|
|
167
|
+
export function encodePartsList(parts) {
|
|
168
|
+
checkParts(parts);
|
|
169
|
+
return JSON.stringify({
|
|
170
|
+
f: HANDOVER_PARTS_FORMAT,
|
|
171
|
+
parts: parts.map((p, i) => ({ i, blob: p.blob, patch: p.patch, len: p.len, exp: p.exp })),
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/** Read an opened parts document. Throws `HandoverFormatError` (`bad-field`, `parts`). */
|
|
175
|
+
export function decodePartsList(text) {
|
|
176
|
+
const doc = objectOf(text);
|
|
177
|
+
const bad = new HandoverFormatError("bad-field", "parts");
|
|
178
|
+
if (Object.keys(doc).sort().join(",") !== "f,parts" || doc["f"] !== HANDOVER_PARTS_FORMAT)
|
|
179
|
+
throw bad;
|
|
180
|
+
const raw = doc["parts"];
|
|
181
|
+
if (!Array.isArray(raw))
|
|
182
|
+
throw bad;
|
|
183
|
+
const parts = raw.map((value, index) => {
|
|
184
|
+
if (!isRecord(value))
|
|
185
|
+
throw bad;
|
|
186
|
+
const keys = Object.keys(value).sort().join(",");
|
|
187
|
+
if (keys !== "blob,exp,i,len,patch" || value["i"] !== index)
|
|
188
|
+
throw bad;
|
|
189
|
+
const blob = value["blob"];
|
|
190
|
+
const patch = value["patch"];
|
|
191
|
+
const len = value["len"];
|
|
192
|
+
const exp = value["exp"];
|
|
193
|
+
if ((blob !== null && typeof blob !== "string") || (patch !== null && typeof patch !== "string"))
|
|
194
|
+
throw bad;
|
|
195
|
+
if (typeof len !== "number" || typeof exp !== "number")
|
|
196
|
+
throw bad;
|
|
197
|
+
return { blob, patch, len, exp };
|
|
198
|
+
});
|
|
199
|
+
checkParts(parts);
|
|
200
|
+
return parts;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The smallest recorded end epoch, or 0 when any piece's is unknown (0). Exclusive, and recorded
|
|
204
|
+
* when the file was made — see `HandoverPart.exp`.
|
|
205
|
+
*/
|
|
206
|
+
export function earliestExpiry(parts) {
|
|
207
|
+
if (parts.some((p) => p.exp === 0))
|
|
208
|
+
return 0;
|
|
209
|
+
return parts.reduce((min, p) => Math.min(min, p.exp), MAX_EPOCH);
|
|
210
|
+
}
|
|
211
|
+
/** The public code file's text. */
|
|
212
|
+
export function encodePublicCodeFile(file) {
|
|
213
|
+
checkPublicCode(file);
|
|
214
|
+
const doc = { format: PUBLIC_CODE_FORMAT, version: PUBLIC_CODE_VERSION, code: file.code, identity: file.identity };
|
|
215
|
+
return `${JSON.stringify(doc, null, 2)}\n`;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Read a public code file's text. Throws `HandoverFormatError`.
|
|
219
|
+
*
|
|
220
|
+
* ⚠ This checks the SHAPE only. Whether `identity` really fingerprints to `code` is the engine's
|
|
221
|
+
* question, and a caller must ask it before sealing anything to the identity.
|
|
222
|
+
*/
|
|
223
|
+
export function decodePublicCodeFile(text) {
|
|
224
|
+
capBytes(text, MAX_PUBLIC_CODE_FILE_BYTES);
|
|
225
|
+
const doc = objectOf(text);
|
|
226
|
+
if (doc["format"] !== PUBLIC_CODE_FORMAT)
|
|
227
|
+
throw new HandoverFormatError("wrong-format");
|
|
228
|
+
checkVersion(doc["version"], PUBLIC_CODE_VERSION);
|
|
229
|
+
closedKeys(doc, ["format", "version", "code", "identity"]);
|
|
230
|
+
const file = { code: stringField(doc, "code"), identity: stringField(doc, "identity") };
|
|
231
|
+
checkPublicCode(file);
|
|
232
|
+
return file;
|
|
233
|
+
}
|
|
234
|
+
// ── checks ───────────────────────────────────────────────────────────────────────────────────
|
|
235
|
+
function capBytes(text, max) {
|
|
236
|
+
// UTF-16 units never outnumber UTF-8 bytes, so a long string is refused before it is encoded.
|
|
237
|
+
if (text.length > max || new TextEncoder().encode(text).length > max) {
|
|
238
|
+
throw new HandoverFormatError("too-large");
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/** An integer above the known version is "newer"; anything else that is not the version is wrong. */
|
|
242
|
+
function checkVersion(version, known) {
|
|
243
|
+
if (version === known)
|
|
244
|
+
return;
|
|
245
|
+
if (typeof version === "number" && Number.isSafeInteger(version) && version > known) {
|
|
246
|
+
throw new HandoverFormatError("unknown-version");
|
|
247
|
+
}
|
|
248
|
+
throw new HandoverFormatError("bad-field", "version");
|
|
249
|
+
}
|
|
250
|
+
function closedKeys(doc, allowed) {
|
|
251
|
+
for (const key of Object.keys(doc)) {
|
|
252
|
+
if (!allowed.includes(key))
|
|
253
|
+
throw new HandoverFormatError("bad-field", key);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function checkHandover(file) {
|
|
257
|
+
if (file.network !== "mainnet" && file.network !== "testnet")
|
|
258
|
+
throw new HandoverFormatError("bad-field", "network");
|
|
259
|
+
if (!UUID_LOWER.test(file.item))
|
|
260
|
+
throw new HandoverFormatError("bad-field", "item");
|
|
261
|
+
exactly(file.sender, IDENTITY_LEN, "sender");
|
|
262
|
+
exactly(file.envelope, ENVELOPE_LEN, "envelope");
|
|
263
|
+
exactly(file.hash, DIGEST_ENVELOPE_LEN, "hash");
|
|
264
|
+
within(file.name, MIN_SEALED_LEN + 1, MAX_NAME_SEALED_LEN, "name");
|
|
265
|
+
within(file.parts, MIN_SEALED_LEN + 1, MAX_PARTS_SEALED_LEN, "parts");
|
|
266
|
+
}
|
|
267
|
+
function checkName(doc) {
|
|
268
|
+
const network = doc.network === "mainnet" || doc.network === "testnet";
|
|
269
|
+
const ok = Number.isSafeInteger(doc.size) && doc.size >= 0 && network && base64UrlByteLength(doc.partsSha256) === SHA256_LEN;
|
|
270
|
+
if (!ok)
|
|
271
|
+
throw new HandoverFormatError("bad-field", "name");
|
|
272
|
+
}
|
|
273
|
+
function checkParts(parts) {
|
|
274
|
+
if (parts.length === 0 || parts.length > MAX_PARTS)
|
|
275
|
+
throw new HandoverFormatError("bad-field", "parts");
|
|
276
|
+
for (const p of parts) {
|
|
277
|
+
const ok = (p.blob === null) !== (p.patch === null) &&
|
|
278
|
+
(p.blob === null || STORAGE_ID.test(p.blob)) &&
|
|
279
|
+
(p.patch === null || STORAGE_ID.test(p.patch)) &&
|
|
280
|
+
Number.isSafeInteger(p.len) &&
|
|
281
|
+
p.len > 0 &&
|
|
282
|
+
Number.isSafeInteger(p.exp) &&
|
|
283
|
+
p.exp >= 0 &&
|
|
284
|
+
p.exp <= MAX_EPOCH;
|
|
285
|
+
if (!ok)
|
|
286
|
+
throw new HandoverFormatError("bad-field", "parts");
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function checkPublicCode(file) {
|
|
290
|
+
if (file.code.length === 0 || file.code.length > MAX_CODE_CHARS)
|
|
291
|
+
throw new HandoverFormatError("bad-field", "code");
|
|
292
|
+
exactly(file.identity, IDENTITY_LEN, "identity");
|
|
293
|
+
}
|
|
294
|
+
function exactly(value, bytes, field) {
|
|
295
|
+
if (base64UrlByteLength(value) !== bytes)
|
|
296
|
+
throw new HandoverFormatError("bad-field", field);
|
|
297
|
+
}
|
|
298
|
+
function within(value, min, max, field) {
|
|
299
|
+
const n = base64UrlByteLength(value);
|
|
300
|
+
if (n === null || n < min || n > max)
|
|
301
|
+
throw new HandoverFormatError("bad-field", field);
|
|
302
|
+
}
|
|
303
|
+
function isRecord(value) {
|
|
304
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
305
|
+
}
|
|
306
|
+
function objectOf(text) {
|
|
307
|
+
let parsed;
|
|
308
|
+
try {
|
|
309
|
+
parsed = JSON.parse(text);
|
|
310
|
+
}
|
|
311
|
+
catch {
|
|
312
|
+
throw new HandoverFormatError("not-json");
|
|
313
|
+
}
|
|
314
|
+
if (!isRecord(parsed))
|
|
315
|
+
throw new HandoverFormatError("not-json");
|
|
316
|
+
return parsed;
|
|
317
|
+
}
|
|
318
|
+
function stringField(doc, field) {
|
|
319
|
+
const value = doc[field];
|
|
320
|
+
if (typeof value !== "string")
|
|
321
|
+
throw new HandoverFormatError("bad-field", field);
|
|
322
|
+
return value;
|
|
323
|
+
}
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
/** Network codes, as stored. Fixed forever once a row carries one. */
|
|
2
2
|
export declare const NETWORK_WALRUS = 0;
|
|
3
|
-
/** Filecoin —
|
|
3
|
+
/** Filecoin — what the "NMTS Heavy" tier stores on (whole copies kept by storage companies). */
|
|
4
4
|
export declare const NETWORK_FILECOIN = 1;
|
|
5
5
|
/** Arweave — reserved and deliberately last: it cannot be deleted, ever (BACKLOG §2.2). */
|
|
6
6
|
export declare const NETWORK_ARWEAVE = 2;
|
|
@@ -18,8 +18,8 @@ export declare function networkCode(name: string): number | null;
|
|
|
18
18
|
/**
|
|
19
19
|
* What an ABSENT network field means: Walrus.
|
|
20
20
|
*
|
|
21
|
-
* This is a fact, not a fallback. No other network
|
|
22
|
-
* entry and map written
|
|
23
|
-
* constant so the reasoning sits next to every use of it rather than being re-derived.
|
|
21
|
+
* This is a fact, not a fallback. No other network had an upload path until well after the field
|
|
22
|
+
* existed, so every part, entry and map written without it is on Walrus by construction. Stated as
|
|
23
|
+
* a named constant so the reasoning sits next to every use of it rather than being re-derived.
|
|
24
24
|
*/
|
|
25
25
|
export declare const NETWORK_WHEN_UNRECORDED = 0;
|
|
@@ -18,16 +18,16 @@
|
|
|
18
18
|
// with none of our code beside it. A magic number in that document is a trap; a word is not.
|
|
19
19
|
// This mirrors the existing `kind` field, which is `1` in the file list and `"file"` in the map.
|
|
20
20
|
//
|
|
21
|
-
// ⚠ NOT A TIER MAPPING.
|
|
22
|
-
// these are the networks underneath. The
|
|
23
|
-
//
|
|
21
|
+
// ⚠ NOT A TIER MAPPING. "NMTS Standard" and "NMTS Heavy" are product names for what the person
|
|
22
|
+
// picks; these are the networks underneath. The one mapping between the two lives in
|
|
23
|
+
// `storage-tier.ts`, so a rename of either never silently redefines the other.
|
|
24
24
|
//
|
|
25
25
|
// FAILURE MODES: none at runtime — pure functions over constants. An UNKNOWN code answers `null`
|
|
26
26
|
// rather than falling back to Walrus: a wrong network recorded in a recovery map is unfixable
|
|
27
27
|
// years later, so callers are made to decide what to do about it.
|
|
28
28
|
/** Network codes, as stored. Fixed forever once a row carries one. */
|
|
29
29
|
export const NETWORK_WALRUS = 0;
|
|
30
|
-
/** Filecoin —
|
|
30
|
+
/** Filecoin — what the "NMTS Heavy" tier stores on (whole copies kept by storage companies). */
|
|
31
31
|
export const NETWORK_FILECOIN = 1;
|
|
32
32
|
/** Arweave — reserved and deliberately last: it cannot be deleted, ever (BACKLOG §2.2). */
|
|
33
33
|
export const NETWORK_ARWEAVE = 2;
|
|
@@ -57,8 +57,8 @@ export function networkCode(name) {
|
|
|
57
57
|
/**
|
|
58
58
|
* What an ABSENT network field means: Walrus.
|
|
59
59
|
*
|
|
60
|
-
* This is a fact, not a fallback. No other network
|
|
61
|
-
* entry and map written
|
|
62
|
-
* constant so the reasoning sits next to every use of it rather than being re-derived.
|
|
60
|
+
* This is a fact, not a fallback. No other network had an upload path until well after the field
|
|
61
|
+
* existed, so every part, entry and map written without it is on Walrus by construction. Stated as
|
|
62
|
+
* a named constant so the reasoning sits next to every use of it rather than being re-derived.
|
|
63
63
|
*/
|
|
64
64
|
export const NETWORK_WHEN_UNRECORDED = NETWORK_WALRUS;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** The tiers a person can pick, by the word every interface uses. */
|
|
2
|
+
export type StorageTier = "standard" | "heavy";
|
|
3
|
+
/** Every tier, in the order an interface lists them. */
|
|
4
|
+
export declare const STORAGE_TIERS: readonly StorageTier[];
|
|
5
|
+
/** What an upload uses when nobody chose. */
|
|
6
|
+
export declare const DEFAULT_STORAGE_TIER: StorageTier;
|
|
7
|
+
/** The storage network code a tier stores on. */
|
|
8
|
+
export declare function networkForTier(tier: StorageTier): number;
|
|
9
|
+
/**
|
|
10
|
+
* The tier a stored network code belongs to, or `null` for a code no tier uses.
|
|
11
|
+
*
|
|
12
|
+
* An absent network field means Walrus (every part written before the field existed is on Walrus),
|
|
13
|
+
* so `undefined` answers "standard". An unknown code answers `null` rather than a guess: a newer
|
|
14
|
+
* client may have stored something this one has never heard of, and calling it Standard would send
|
|
15
|
+
* a reader to the wrong network.
|
|
16
|
+
*/
|
|
17
|
+
export declare function tierForNetwork(network: number | undefined): StorageTier | null;
|
|
18
|
+
/** Whether a string is one of the tier words, exactly as written. */
|
|
19
|
+
export declare function isStorageTier(value: unknown): value is StorageTier;
|
|
20
|
+
/**
|
|
21
|
+
* Read a tier word typed by a person (a `--tier` flag, an SDK option, a config file).
|
|
22
|
+
*
|
|
23
|
+
* Surrounding space and letter case are forgiven, because nothing else about the word is ambiguous.
|
|
24
|
+
* Anything else is refused with a message naming the accepted words — never mapped to the default,
|
|
25
|
+
* because a typo that silently stores a file on the other network is the one mistake a person cannot
|
|
26
|
+
* see afterwards.
|
|
27
|
+
*/
|
|
28
|
+
export declare function parseStorageTier(raw: string): StorageTier;
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
// WHICH TIER a person picks, and which storage network that tier means — the one place the two
|
|
2
|
+
// are tied together.
|
|
3
|
+
//
|
|
4
|
+
// ⚠ THIS FILE IS PUBLISHED. It is copied byte-for-byte into the `nmts` command-line package.
|
|
5
|
+
// Keep the comments self-contained English, with no pointer only we can follow.
|
|
6
|
+
//
|
|
7
|
+
// A TIER IS A PRODUCT NAME; A NETWORK IS WHAT IS UNDERNEATH. A person chooses "NMTS Standard" or
|
|
8
|
+
// "NMTS Heavy" and never has to learn what Walrus or Filecoin is. Stored rows, sealed file lists and
|
|
9
|
+
// recovery maps record the NETWORK (`storage-network.ts`), because that is what a reader needs years
|
|
10
|
+
// later to find the bytes. Keeping the mapping here, and only here, means renaming a tier can never
|
|
11
|
+
// silently change where anybody's bytes are recorded to be.
|
|
12
|
+
//
|
|
13
|
+
// standard ↔ network 0 (Walrus) — erasure-coded across many storage nodes, paid per epoch
|
|
14
|
+
// heavy ↔ network 1 (Filecoin) — whole copies kept by two separate storage companies
|
|
15
|
+
//
|
|
16
|
+
// The same word is used everywhere: `--tier standard|heavy` on the command line, `tier:` in the SDK.
|
|
17
|
+
// The default is "standard".
|
|
18
|
+
import { NETWORK_FILECOIN, NETWORK_WALRUS } from "./storage-network.js";
|
|
19
|
+
/** Every tier, in the order an interface lists them. */
|
|
20
|
+
export const STORAGE_TIERS = ["standard", "heavy"];
|
|
21
|
+
/** What an upload uses when nobody chose. */
|
|
22
|
+
export const DEFAULT_STORAGE_TIER = "standard";
|
|
23
|
+
const NETWORK_OF = {
|
|
24
|
+
standard: NETWORK_WALRUS,
|
|
25
|
+
heavy: NETWORK_FILECOIN,
|
|
26
|
+
};
|
|
27
|
+
/** The storage network code a tier stores on. */
|
|
28
|
+
export function networkForTier(tier) {
|
|
29
|
+
return NETWORK_OF[tier];
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* The tier a stored network code belongs to, or `null` for a code no tier uses.
|
|
33
|
+
*
|
|
34
|
+
* An absent network field means Walrus (every part written before the field existed is on Walrus),
|
|
35
|
+
* so `undefined` answers "standard". An unknown code answers `null` rather than a guess: a newer
|
|
36
|
+
* client may have stored something this one has never heard of, and calling it Standard would send
|
|
37
|
+
* a reader to the wrong network.
|
|
38
|
+
*/
|
|
39
|
+
export function tierForNetwork(network) {
|
|
40
|
+
if (network === undefined || network === NETWORK_WALRUS)
|
|
41
|
+
return "standard";
|
|
42
|
+
if (network === NETWORK_FILECOIN)
|
|
43
|
+
return "heavy";
|
|
44
|
+
return null;
|
|
45
|
+
}
|
|
46
|
+
/** Whether a string is one of the tier words, exactly as written. */
|
|
47
|
+
export function isStorageTier(value) {
|
|
48
|
+
return value === "standard" || value === "heavy";
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Read a tier word typed by a person (a `--tier` flag, an SDK option, a config file).
|
|
52
|
+
*
|
|
53
|
+
* Surrounding space and letter case are forgiven, because nothing else about the word is ambiguous.
|
|
54
|
+
* Anything else is refused with a message naming the accepted words — never mapped to the default,
|
|
55
|
+
* because a typo that silently stores a file on the other network is the one mistake a person cannot
|
|
56
|
+
* see afterwards.
|
|
57
|
+
*/
|
|
58
|
+
export function parseStorageTier(raw) {
|
|
59
|
+
const value = raw.trim().toLowerCase();
|
|
60
|
+
if (isStorageTier(value))
|
|
61
|
+
return value;
|
|
62
|
+
throw new RangeError(`Unknown storage tier "${raw}". Use "standard" or "heavy".`);
|
|
63
|
+
}
|
|
@@ -45,6 +45,7 @@ export function parseTokenAmountToBaseUnits(input, decimals = TOKEN_DECIMALS) {
|
|
|
45
45
|
return BigInt(`${intPart === "" ? "0" : intPart}${frac}`);
|
|
46
46
|
}
|
|
47
47
|
catch {
|
|
48
|
+
// The pattern above admits only digits; a BigInt failure still means not an amount.
|
|
48
49
|
return null;
|
|
49
50
|
}
|
|
50
51
|
}
|
package/dist/upload-file.d.ts
CHANGED
|
@@ -94,4 +94,12 @@ 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
|
+
/**
|
|
98
|
+
* The part's bytes, followed by zeros up to the length it will declare.
|
|
99
|
+
*
|
|
100
|
+
* ⛔ ZEROS APPENDED TO THE PLAINTEXT, NOT BYTES TACKED ONTO THE STORED STREAM. A stream's header is
|
|
101
|
+
* authenticated but not encrypted, so padding added after sealing would leave the real length
|
|
102
|
+
* legible in the header of a public object — which is the exact thing this is for.
|
|
103
|
+
*/
|
|
104
|
+
export declare function padded(chunks: AsyncIterable<Uint8Array>, real: number, sealFrom: number): AsyncIterable<Uint8Array>;
|
|
97
105
|
export { entryOf };
|
package/dist/upload-file.js
CHANGED
|
@@ -18,12 +18,12 @@
|
|
|
18
18
|
import { sha256 } from "@noble/hashes/sha2.js";
|
|
19
19
|
import { fromBase64Url } from "./bytes.js";
|
|
20
20
|
import { AAD } from "./crypto.js";
|
|
21
|
-
import { NmtsError } from "./errors.js";
|
|
22
21
|
import { fileSecrets, sealPart } from "./seal.js";
|
|
23
|
-
import {
|
|
22
|
+
import { planFor } from "./upload-price.js";
|
|
24
23
|
import { paddedPlaintextLen, } from "./shared/lib/crypto/size-padding.js";
|
|
25
24
|
import { NCF3_SHAPE } from "./seal.js";
|
|
26
25
|
import { buyAndPushPart, entryOf } from "./upload.js";
|
|
26
|
+
import { confirmParts, dropLeftovers, oneUploadAtATime, runIdFor } from "./upload-run.js";
|
|
27
27
|
import { commitItem } from "./upload-steps.js";
|
|
28
28
|
import { finishReservationKey, partKey, readItemRecord, readReservationBytes, readReservationRecord, startReservationKey, } from "./upload-store.js";
|
|
29
29
|
/** How much plaintext is handed to the engine at a time. Matches the format's own chunk size. */
|
|
@@ -37,31 +37,39 @@ export const READ_CHUNK_BYTES = 4 * 2 ** 20;
|
|
|
37
37
|
*/
|
|
38
38
|
export async function uploadFile(input) {
|
|
39
39
|
const { dataKey, source, onStep } = input;
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
nextStep: "The storage network has nothing to store and would refuse the reservation.",
|
|
43
|
-
});
|
|
44
|
-
}
|
|
45
|
-
const plan = planParts(source.size, input.partSize);
|
|
40
|
+
// ⚠ An empty file is one part that carries nothing — `planFor` says why it is a file at all.
|
|
41
|
+
const plan = planFor(source.size, input.partSize);
|
|
46
42
|
onStep?.({ step: "planning", parts: plan.length, partSize: input.partSize });
|
|
47
43
|
// ── pass one: the reservation key and the content hash, from a single read ──
|
|
48
44
|
onStep?.({ step: "hashing", parts: plan.length });
|
|
49
45
|
const keyHash = startReservationKey(dataKey);
|
|
50
46
|
const contentDigest = await hashWhole(source, keyHash);
|
|
51
47
|
const fileKey = finishReservationKey(keyHash, input.name, input.destination);
|
|
52
|
-
// ⛔
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
48
|
+
// ⛔ FROM HERE TO THE COMMIT, ONE RUN OF THIS FILE AT A TIME in this process (`upload-run.ts`).
|
|
49
|
+
return oneUploadAtATime(fileKey, async () => {
|
|
50
|
+
await dropLeftovers(fileKey, plan.length);
|
|
51
|
+
const runId = await runIdFor(fileKey, plan.length);
|
|
52
|
+
// ⛔ ALREADY COMMITTED STOPS HERE, BEFORE ANY PART IS TOUCHED. A file that got as far as
|
|
53
|
+
// `POST /v1/items` exists and is paid for; all that can still be missing is the account's own
|
|
54
|
+
// list. Asking the server about every part again would be a round trip per part to learn
|
|
55
|
+
// something the record already says. ⚠ Only THIS upload's commit: a record naming another run
|
|
56
|
+
// is what an earlier upload of the same file left, and its item is not these bytes.
|
|
57
|
+
const committed = await readItemRecord(fileKey);
|
|
58
|
+
if (committed?.itemId !== undefined && (committed.runId === undefined || committed.runId === runId)) {
|
|
59
|
+
const entry = await recordedEntry(fileKey, plan.length, source.size);
|
|
60
|
+
if (entry !== null) {
|
|
61
|
+
return { itemId: committed.itemId, resumed: true, ledgerIds: [], fileKey, parts: plan.length, entry };
|
|
62
|
+
}
|
|
61
63
|
}
|
|
62
|
-
|
|
64
|
+
return uploadParts(input, { fileKey, runId, plan, contentDigest });
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
/** Seal, buy and push every part not yet bought, then commit them as one file. */
|
|
68
|
+
async function uploadParts(input, run) {
|
|
69
|
+
const { source, onStep } = input;
|
|
70
|
+
const { fileKey, runId, plan } = run;
|
|
63
71
|
// ── the file's secrets: from the record if one exists, otherwise made now ──
|
|
64
|
-
const secrets = await openSecrets(input, fileKey, plan.length, contentDigest);
|
|
72
|
+
const secrets = await openSecrets(input, fileKey, plan.length, run.contentDigest);
|
|
65
73
|
try {
|
|
66
74
|
const entry = {
|
|
67
75
|
name: input.name,
|
|
@@ -93,6 +101,7 @@ export async function uploadFile(input) {
|
|
|
93
101
|
paid.push(await (input.buy ?? buyAndPushPart)({
|
|
94
102
|
api: input.api,
|
|
95
103
|
protocol: input.protocol,
|
|
104
|
+
runId,
|
|
96
105
|
key,
|
|
97
106
|
sealed,
|
|
98
107
|
relayUrl: input.relayUrl,
|
|
@@ -106,8 +115,10 @@ export async function uploadFile(input) {
|
|
|
106
115
|
onStep: (step) => onStep?.({ ...step, partIndex: range.partIndex, parts: plan.length }),
|
|
107
116
|
}));
|
|
108
117
|
}
|
|
118
|
+
await confirmParts(fileKey, paid, entry);
|
|
109
119
|
const itemId = await commitItem({
|
|
110
120
|
api: input.api,
|
|
121
|
+
runId,
|
|
111
122
|
epochs: input.epochs,
|
|
112
123
|
currentEpoch: input.currentEpoch,
|
|
113
124
|
entry,
|
|
@@ -164,11 +175,32 @@ async function sealPartOf(input, dek, range, parts, sealFrom) {
|
|
|
164
175
|
parts,
|
|
165
176
|
bytes: sealFrom,
|
|
166
177
|
});
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
178
|
+
const placement = { index: range.partIndex, total: parts, plaintextLen: sealFrom };
|
|
179
|
+
if (sealFrom === 0)
|
|
180
|
+
return sealNothing(input.crypt, dek, placement);
|
|
181
|
+
return sealPart(input.crypt, dek, padded(input.source.read(range.offset, range.length), range.length, sealFrom), placement);
|
|
182
|
+
}
|
|
183
|
+
/**
|
|
184
|
+
* The one part of an empty file that nothing pads: a header and one empty final chunk (NCF-3 §4.1).
|
|
185
|
+
*
|
|
186
|
+
* ⚠ HERE ONLY BECAUSE `sealPart` REFUSES A ZERO LENGTH, from before an empty file could be stored.
|
|
187
|
+
* The engine seals it as it seals every stream — this opens a session, adds nothing and finishes —
|
|
188
|
+
* and once `sealPart` takes zero this can go. It is reached only when the account stores exact
|
|
189
|
+
* sizes: every other rule rounds an empty file up to the room its one credit already bought.
|
|
190
|
+
*/
|
|
191
|
+
function sealNothing(crypt, dek, placement) {
|
|
192
|
+
const sealer = new crypt.StreamEncryptor(dek, 0, placement.index, placement.total);
|
|
193
|
+
try {
|
|
194
|
+
const header = sealer.header();
|
|
195
|
+
const end = sealer.finish();
|
|
196
|
+
const out = new Uint8Array(header.length + end.length);
|
|
197
|
+
out.set(header, 0);
|
|
198
|
+
out.set(end, header.length);
|
|
199
|
+
return out;
|
|
200
|
+
}
|
|
201
|
+
finally {
|
|
202
|
+
sealer.free();
|
|
203
|
+
}
|
|
172
204
|
}
|
|
173
205
|
/**
|
|
174
206
|
* The part's bytes, followed by zeros up to the length it will declare.
|
|
@@ -177,7 +209,7 @@ async function sealPartOf(input, dek, range, parts, sealFrom) {
|
|
|
177
209
|
* authenticated but not encrypted, so padding added after sealing would leave the real length
|
|
178
210
|
* legible in the header of a public object — which is the exact thing this is for.
|
|
179
211
|
*/
|
|
180
|
-
async function* padded(chunks, real, sealFrom) {
|
|
212
|
+
export async function* padded(chunks, real, sealFrom) {
|
|
181
213
|
for await (const chunk of chunks)
|
|
182
214
|
yield chunk;
|
|
183
215
|
let left = sealFrom - real;
|
|
@@ -4,5 +4,8 @@
|
|
|
4
4
|
* ⛔ IT IS NOT READ HERE. A file large enough to need several parts is a file too large to hold,
|
|
5
5
|
* and the size is all that is needed to plan the upload and quote its price. The bytes are read
|
|
6
6
|
* later, a slice at a time, by the part that is being sealed.
|
|
7
|
+
*
|
|
8
|
+
* ⚠ ZERO IS A SIZE. An empty file is sealed and stored like any other (`planFor` in
|
|
9
|
+
* `upload-price.ts`), so it is not refused here.
|
|
7
10
|
*/
|
|
8
11
|
export declare function measureLocal(path: string): number;
|