@needmoretruth/nmts-cli 0.45.0 → 0.47.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.
Files changed (252) hide show
  1. package/AGENTS.md +8 -8
  2. package/CHANGELOG.md +78 -0
  3. package/README.ko.md +6 -3
  4. package/README.md +31 -11
  5. package/dist/api-advice.js +17 -2
  6. package/dist/api.d.ts +22 -0
  7. package/dist/api.js +49 -1
  8. package/dist/arg-options.d.ts +19 -1
  9. package/dist/args.js +14 -2
  10. package/dist/artifact-about.d.ts +1 -1
  11. package/dist/autonomy.js +3 -3
  12. package/dist/collision.d.ts +20 -3
  13. package/dist/collision.js +21 -3
  14. package/dist/commands/erase.js +1 -0
  15. package/dist/commands/handover.d.ts +15 -0
  16. package/dist/commands/handover.js +376 -0
  17. package/dist/commands/heavy-run.d.ts +59 -0
  18. package/dist/commands/heavy-run.js +247 -0
  19. package/dist/commands/heavy.d.ts +7 -0
  20. package/dist/commands/heavy.js +123 -0
  21. package/dist/commands/link.d.ts +12 -0
  22. package/dist/commands/link.js +121 -0
  23. package/dist/commands/ls.d.ts +5 -0
  24. package/dist/commands/ls.js +9 -1
  25. package/dist/commands/public-code-manage.d.ts +24 -0
  26. package/dist/commands/public-code-manage.js +217 -0
  27. package/dist/commands/public-code.d.ts +12 -1
  28. package/dist/commands/public-code.js +140 -45
  29. package/dist/commands/push-heavy.d.ts +6 -0
  30. package/dist/commands/push-heavy.js +128 -0
  31. package/dist/commands/push-tree.d.ts +24 -0
  32. package/dist/commands/push-tree.js +91 -0
  33. package/dist/commands/push.d.ts +4 -13
  34. package/dist/commands/push.js +7 -77
  35. package/dist/commands/put-heavy.d.ts +9 -0
  36. package/dist/commands/put-heavy.js +120 -0
  37. package/dist/commands/put-payer.d.ts +21 -0
  38. package/dist/commands/put-payer.js +24 -0
  39. package/dist/commands/put.d.ts +2 -2
  40. package/dist/commands/put.js +8 -7
  41. package/dist/commands/receive.js +14 -8
  42. package/dist/commands/s3.js +44 -10
  43. package/dist/commands/share-dispatch.js +14 -1
  44. package/dist/commands/share.d.ts +2 -0
  45. package/dist/commands/share.js +45 -35
  46. package/dist/commands/shares-sent.js +8 -3
  47. package/dist/crypto-surface.d.ts +35 -0
  48. package/dist/crypto-surface.js +13 -0
  49. package/dist/crypto.d.ts +8 -0
  50. package/dist/crypto.js +8 -0
  51. package/dist/download-part.d.ts +32 -1
  52. package/dist/download-part.js +95 -6
  53. package/dist/download-range.d.ts +19 -0
  54. package/dist/download-range.js +108 -0
  55. package/dist/download-sink.d.ts +15 -0
  56. package/dist/download.d.ts +14 -0
  57. package/dist/download.js +19 -4
  58. package/dist/handover-codes.d.ts +25 -0
  59. package/dist/handover-codes.js +106 -0
  60. package/dist/handover.d.ts +62 -0
  61. package/dist/handover.js +217 -0
  62. package/dist/heavy-api.d.ts +36 -0
  63. package/dist/heavy-api.js +107 -0
  64. package/dist/heavy-copy.d.ts +72 -0
  65. package/dist/heavy-copy.js +108 -0
  66. package/dist/heavy-evm.d.ts +20 -0
  67. package/dist/heavy-evm.js +106 -0
  68. package/dist/heavy-self-pay.d.ts +92 -0
  69. package/dist/heavy-self-pay.js +167 -0
  70. package/dist/heavy-upload.d.ts +87 -0
  71. package/dist/heavy-upload.js +154 -0
  72. package/dist/heavy-wallet-pay.d.ts +39 -0
  73. package/dist/heavy-wallet-pay.js +73 -0
  74. package/dist/help-options.js +13 -4
  75. package/dist/help.js +24 -3
  76. package/dist/links.d.ts +58 -0
  77. package/dist/links.js +206 -0
  78. package/dist/list-tier.d.ts +7 -0
  79. package/dist/list-tier.js +47 -0
  80. package/dist/main.js +13 -10
  81. package/dist/manifest-write.d.ts +4 -3
  82. package/dist/mcp-tools/reads.js +20 -6
  83. package/dist/mcp-tools/share.js +47 -1
  84. package/dist/mcp-tools/tiers.js +16 -1
  85. package/dist/platform-sign.d.ts +4 -4
  86. package/dist/platform-sign.js +3 -3
  87. package/dist/portable.d.ts +23 -0
  88. package/dist/portable.js +19 -0
  89. package/dist/product.d.ts +1 -1
  90. package/dist/product.js +1 -1
  91. package/dist/public-code-activity.d.ts +35 -0
  92. package/dist/public-code-activity.js +88 -0
  93. package/dist/public-code-refusals.d.ts +25 -0
  94. package/dist/public-code-refusals.js +87 -0
  95. package/dist/public-codes.d.ts +76 -0
  96. package/dist/public-codes.js +182 -0
  97. package/dist/recovery-build.js +16 -0
  98. package/dist/recovery-map-part.d.ts +82 -0
  99. package/dist/recovery-map-part.js +83 -0
  100. package/dist/recovery-map.d.ts +5 -40
  101. package/dist/recovery-map.js +18 -1
  102. package/dist/recovery-source.d.ts +3 -0
  103. package/dist/recovery-source.js +9 -0
  104. package/dist/risk.d.ts +55 -1
  105. package/dist/risk.js +55 -3
  106. package/dist/run-log.d.ts +1 -1
  107. package/dist/run-log.js +7 -1
  108. package/dist/s3/address.d.ts +20 -0
  109. package/dist/s3/address.js +97 -0
  110. package/dist/s3/answer.d.ts +48 -0
  111. package/dist/s3/answer.js +330 -0
  112. package/dist/s3/body-deframe.d.ts +49 -0
  113. package/dist/s3/body-deframe.js +264 -0
  114. package/dist/s3/body-measure.d.ts +13 -0
  115. package/dist/s3/body-measure.js +48 -0
  116. package/dist/s3/body-plan.d.ts +31 -0
  117. package/dist/s3/body-plan.js +159 -0
  118. package/dist/s3/body.d.ts +46 -0
  119. package/dist/s3/body.js +117 -0
  120. package/dist/s3/call.d.ts +36 -0
  121. package/dist/s3/call.js +42 -0
  122. package/dist/s3/checksum.d.ts +26 -0
  123. package/dist/s3/checksum.js +176 -0
  124. package/dist/s3/content-type.d.ts +3 -0
  125. package/dist/s3/content-type.js +49 -0
  126. package/dist/s3/contract.d.ts +91 -14
  127. package/dist/s3/drive-conditions.d.ts +47 -0
  128. package/dist/s3/drive-conditions.js +134 -0
  129. package/dist/s3/drive-key.d.ts +11 -0
  130. package/dist/s3/drive-key.js +58 -0
  131. package/dist/s3/drive-limits.d.ts +33 -0
  132. package/dist/s3/drive-limits.js +53 -0
  133. package/dist/s3/drive-lock.d.ts +11 -0
  134. package/dist/s3/drive-lock.js +47 -0
  135. package/dist/s3/drive-trashed-name.d.ts +13 -0
  136. package/dist/s3/drive-trashed-name.js +59 -0
  137. package/dist/s3/drive-write.d.ts +38 -0
  138. package/dist/s3/drive-write.js +258 -0
  139. package/dist/s3/drive.d.ts +59 -25
  140. package/dist/s3/drive.js +46 -77
  141. package/dist/s3/listing.d.ts +27 -1
  142. package/dist/s3/listing.js +62 -20
  143. package/dist/s3/long-answer.d.ts +12 -0
  144. package/dist/s3/long-answer.js +104 -0
  145. package/dist/s3/multipart-assemble.d.ts +17 -0
  146. package/dist/s3/multipart-assemble.js +68 -0
  147. package/dist/s3/multipart-sweep.d.ts +15 -0
  148. package/dist/s3/multipart-sweep.js +87 -0
  149. package/dist/s3/multipart-view.d.ts +45 -0
  150. package/dist/s3/multipart-view.js +232 -0
  151. package/dist/s3/multipart.d.ts +26 -19
  152. package/dist/s3/multipart.js +193 -78
  153. package/dist/s3/object-read.d.ts +17 -0
  154. package/dist/s3/object-read.js +127 -0
  155. package/dist/s3/object-write.d.ts +35 -0
  156. package/dist/s3/object-write.js +198 -0
  157. package/dist/s3/range.d.ts +14 -0
  158. package/dist/s3/range.js +80 -0
  159. package/dist/s3/response-sink.d.ts +12 -2
  160. package/dist/s3/response-sink.js +71 -7
  161. package/dist/s3/routes.d.ts +0 -1
  162. package/dist/s3/routes.js +242 -217
  163. package/dist/s3/same-file.d.ts +4 -1
  164. package/dist/s3/same-file.js +12 -3
  165. package/dist/s3/server.d.ts +54 -4
  166. package/dist/s3/server.js +174 -14
  167. package/dist/s3/sigv4-canonical.d.ts +91 -0
  168. package/dist/s3/sigv4-canonical.js +148 -0
  169. package/dist/s3/sigv4-presigned.d.ts +13 -0
  170. package/dist/s3/sigv4-presigned.js +82 -0
  171. package/dist/s3/sigv4.d.ts +26 -27
  172. package/dist/s3/sigv4.js +137 -102
  173. package/dist/s3/spool.d.ts +37 -0
  174. package/dist/s3/spool.js +104 -0
  175. package/dist/s3/staging.d.ts +90 -7
  176. package/dist/s3/staging.js +97 -66
  177. package/dist/s3/xml-multipart.d.ts +38 -0
  178. package/dist/s3/xml-multipart.js +47 -0
  179. package/dist/s3/xml-read.d.ts +28 -0
  180. package/dist/s3/xml-read.js +235 -0
  181. package/dist/s3/xml.d.ts +45 -2
  182. package/dist/s3/xml.js +72 -13
  183. package/dist/s3-gateway.d.ts +11 -5
  184. package/dist/s3-gateway.js +5 -2
  185. package/dist/seal.d.ts +1 -1
  186. package/dist/share-codes.d.ts +40 -0
  187. package/dist/share-codes.js +151 -0
  188. package/dist/share.d.ts +39 -0
  189. package/dist/share.js +54 -13
  190. package/dist/shared/lib/api/types-heavy.d.ts +167 -0
  191. package/dist/shared/lib/api/types-heavy.js +60 -0
  192. package/dist/shared/lib/drive/rebuild-verify.js +1 -0
  193. package/dist/shared/lib/filecoin/providers.d.ts +25 -0
  194. package/dist/shared/lib/filecoin/providers.js +58 -0
  195. package/dist/shared/lib/heavy/download.d.ts +48 -0
  196. package/dist/shared/lib/heavy/download.js +116 -0
  197. package/dist/shared/lib/heavy/order-plan.d.ts +77 -0
  198. package/dist/shared/lib/heavy/order-plan.js +83 -0
  199. package/dist/shared/lib/heavy/order-runner.d.ts +9 -0
  200. package/dist/shared/lib/heavy/order-runner.js +300 -0
  201. package/dist/shared/lib/heavy/order-types.d.ts +90 -0
  202. package/dist/shared/lib/heavy/order-types.js +6 -0
  203. package/dist/shared/lib/heavy/order-wire.d.ts +55 -0
  204. package/dist/shared/lib/heavy/order-wire.js +113 -0
  205. package/dist/shared/lib/heavy/piece-cid.d.ts +24 -0
  206. package/dist/shared/lib/heavy/piece-cid.js +55 -0
  207. package/dist/shared/lib/heavy/sp-upload.d.ts +37 -0
  208. package/dist/shared/lib/heavy/sp-upload.js +112 -0
  209. package/dist/shared/lib/heavy/synapse-piece.d.ts +1 -0
  210. package/dist/shared/lib/heavy/synapse-piece.js +8 -0
  211. package/dist/shared/lib/share/handover-format.d.ts +129 -0
  212. package/dist/shared/lib/share/handover-format.js +323 -0
  213. package/dist/shared/lib/share/public-link.d.ts +41 -0
  214. package/dist/shared/lib/share/public-link.js +107 -0
  215. package/dist/shared/lib/share/shared-file-info.js +1 -0
  216. package/dist/shared/lib/storage-network.d.ts +4 -4
  217. package/dist/shared/lib/storage-network.js +7 -7
  218. package/dist/shared/lib/storage-tier.d.ts +28 -0
  219. package/dist/shared/lib/storage-tier.js +63 -0
  220. package/dist/shared/lib/wallet/send-rules.js +1 -0
  221. package/dist/upload-file.d.ts +8 -0
  222. package/dist/upload-file.js +57 -25
  223. package/dist/upload-price-node.d.ts +3 -0
  224. package/dist/upload-price-node.js +3 -6
  225. package/dist/upload-price.d.ts +10 -0
  226. package/dist/upload-price.js +13 -1
  227. package/dist/upload-run.d.ts +80 -0
  228. package/dist/upload-run.js +173 -0
  229. package/dist/upload-steps.js +15 -4
  230. package/dist/upload-store.d.ts +15 -6
  231. package/dist/upload-store.js +6 -0
  232. package/dist/upload-wallet-put.d.ts +9 -4
  233. package/dist/upload-wallet-put.js +16 -6
  234. package/dist/upload-wallet.js +4 -0
  235. package/dist/upload-wire.d.ts +25 -0
  236. package/dist/upload-wire.js +20 -1
  237. package/dist/upload.js +28 -13
  238. package/dist/wallet-sign.js +2 -2
  239. package/docs/commands/handover.md +54 -0
  240. package/docs/commands/heavy.md +35 -0
  241. package/docs/commands/link.md +27 -0
  242. package/docs/commands/mcp.md +2 -1
  243. package/docs/commands/public-code.md +25 -6
  244. package/docs/commands/push.md +4 -1
  245. package/docs/commands/put.md +15 -1
  246. package/docs/commands/share.md +8 -3
  247. package/package.json +5 -2
  248. package/server.json +2 -2
  249. package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +106 -4
  250. package/vendor/nmts-crypto/nmts_crypto_wasm.js +286 -3
  251. package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
  252. package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +12 -1
@@ -0,0 +1,107 @@
1
+ // The public link's text shapes (NCF-3 §5.8).
2
+ //
3
+ // A public link is `<origin>/l/<token>#<secret>`: the token names the server's row, the secret `S`
4
+ // opens the file key the row holds, and the part after `#` never reaches a server. This module
5
+ // builds and reads that text, and the small document sealed beside the link that tells the reader
6
+ // the file's real length (and, when the owner chose to show it, its name).
7
+ //
8
+ // ⛔ THE DOCUMENT IS THE §5.4 SHARE DOCUMENT, not a new format. A link that shows the name seals the
9
+ // same bytes a private share does (`shared-file-info.ts`); one that hides it leaves the `name`
10
+ // key out and keeps `size`, because the size is what takes size padding back off.
11
+ //
12
+ // ⚠ IT IS COPIED VERBATIM INTO THE CLI (`deploy/gen-cli-shared.mjs`), so it depends on nothing and
13
+ // both programs read and write the same link.
14
+ /** The path segment every public link lives under. */
15
+ export const LINK_PATH = "/l/";
16
+ /** Same marker as `shared-file-info.ts` — the document is that document. */
17
+ const SHARE_FILE_FORMAT = "nmts-share-file/1";
18
+ /** 16 bytes, unpadded base64url. */
19
+ const TOKEN_RE = /^[A-Za-z0-9_-]{22}$/;
20
+ /**
21
+ * 32 bytes, unpadded CANONICAL base64url: 43 characters, and the last one carries only the four
22
+ * bits that are left, so its two low bits are zero. A second spelling of the same bytes is refused
23
+ * rather than silently accepted (§5.8 step 1).
24
+ */
25
+ const SECRET_RE = /^[A-Za-z0-9_-]{42}[AEIMQUYcgkosw048]$/;
26
+ /** Is this a link token (16 bytes, base64url)? */
27
+ export function isLinkToken(value) {
28
+ return TOKEN_RE.test(value);
29
+ }
30
+ /** Is this a link secret (32 bytes, canonical unpadded base64url)? */
31
+ export function isLinkSecret(value) {
32
+ return SECRET_RE.test(value);
33
+ }
34
+ /** The whole link. `origin` is scheme + host (no trailing slash), e.g. `https://nmts.me`. */
35
+ export function buildPublicLink(origin, token, secret) {
36
+ return `${origin.replace(/\/+$/, "")}${LINK_PATH}${token}#${secret}`;
37
+ }
38
+ /**
39
+ * Read a link: a whole URL, or anything that ends in `/l/<token>#<secret>` (a locale prefix such as
40
+ * `/ko` is allowed before it). `null` when either half is missing or not the right shape.
41
+ */
42
+ export function parsePublicLink(text) {
43
+ const trimmed = text.trim();
44
+ const hash = trimmed.indexOf("#");
45
+ if (hash < 0)
46
+ return null;
47
+ const secret = trimmed.slice(hash + 1);
48
+ const before = trimmed.slice(0, hash).replace(/[?].*$/, "").replace(/\/+$/, "");
49
+ const at = before.lastIndexOf(LINK_PATH);
50
+ if (at < 0)
51
+ return null;
52
+ const token = before.slice(at + LINK_PATH.length);
53
+ if (!isLinkToken(token) || !isLinkSecret(secret))
54
+ return null;
55
+ return { token, secret };
56
+ }
57
+ /** The string to seal as the link's `name` envelope. */
58
+ export function encodeLinkDocument(doc) {
59
+ // Key order matches `encodeSharedFileInfo`, so a link that shows the name seals the same bytes.
60
+ return doc.name === null
61
+ ? JSON.stringify({ f: SHARE_FILE_FORMAT, size: doc.size })
62
+ : JSON.stringify({ f: SHARE_FILE_FORMAT, name: doc.name, size: doc.size });
63
+ }
64
+ /**
65
+ * Read an opened `name` envelope. `null` when it is not the document or has no usable size — a
66
+ * link's reader cannot take padding off without the size, so that is a damaged link (§5.8 step 4).
67
+ */
68
+ export function decodeLinkDocument(text) {
69
+ let parsed;
70
+ try {
71
+ parsed = JSON.parse(text);
72
+ }
73
+ catch {
74
+ return null;
75
+ }
76
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
77
+ return null;
78
+ const f = Reflect.get(parsed, "f");
79
+ const name = Reflect.get(parsed, "name");
80
+ const size = Reflect.get(parsed, "size");
81
+ if (f !== SHARE_FILE_FORMAT)
82
+ return null;
83
+ if (typeof size !== "number" || !Number.isSafeInteger(size) || size < 0)
84
+ return null;
85
+ if (name !== undefined && typeof name !== "string")
86
+ return null;
87
+ return { name: typeof name === "string" ? name : null, size };
88
+ }
89
+ /** The expiry choices the share sheet and the CLI offer, in days. `null` = never. */
90
+ export const LINK_EXPIRY_PRESETS = [null, 7, 30];
91
+ /** The longest expiry a person may type, in days (ten years). */
92
+ export const LINK_EXPIRY_MAX_DAYS = 3650;
93
+ /** A typed number of days, or null when it is not a whole number in 1..LINK_EXPIRY_MAX_DAYS. */
94
+ export function linkExpiryDays(value) {
95
+ const n = typeof value === "number" ? value : Number(value.trim());
96
+ return Number.isSafeInteger(n) && n >= 1 && n <= LINK_EXPIRY_MAX_DAYS ? n : null;
97
+ }
98
+ /** `expires_at` for a link that lasts `days` from `now`, or undefined for one that never ends. */
99
+ export function linkExpiresAt(days, now) {
100
+ if (days === null)
101
+ return undefined;
102
+ return new Date(now.getTime() + days * 86_400_000).toISOString();
103
+ }
104
+ /** The name a saved link file gets. */
105
+ export function linkFileName(token) {
106
+ return `nmts-link-${token}.txt`;
107
+ }
@@ -51,6 +51,7 @@ export function decodeSharedFileInfo(sealed) {
51
51
  parsed = JSON.parse(sealed);
52
52
  }
53
53
  catch {
54
+ // See above: text that is not JSON is the name itself.
54
55
  return { name: sealed };
55
56
  }
56
57
  if (typeof parsed !== "object" || parsed === null)
@@ -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 — product tier 「NMTS Heavy」. Reserved; no upload path exists yet. */
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 has ever had an upload path, so every part,
22
- * entry and map written before the field existed is on Walrus by construction. Stated as a named
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. 「NMTS Fast」/「NMTS Heavy」 are product names for what the person buys;
22
- // these are the networks underneath. The screen keeps its own mapping (components/drive/
23
- // StorageTier.tsx) so a rename of either never silently redefines the other.
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 — product tier 「NMTS Heavy」. Reserved; no upload path exists yet. */
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 has ever had an upload path, so every part,
61
- * entry and map written before the field existed is on Walrus by construction. Stated as a named
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
  }
@@ -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 };
@@ -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 { planParts } from "./shared/lib/upload/part-plan.js";
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
- if (source.size <= 0) {
41
- throw new NmtsError("An empty file cannot be uploaded.", {
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
- // ⛔ 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.
56
- const committed = await readItemRecord(fileKey);
57
- if (committed?.itemId !== undefined) {
58
- const entry = await recordedEntry(fileKey, plan.length, source.size);
59
- if (entry !== null) {
60
- return { itemId: committed.itemId, resumed: true, ledgerIds: [], fileKey, parts: plan.length, entry };
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
- return sealPart(input.crypt, dek, padded(input.source.read(range.offset, range.length), range.length, sealFrom), {
168
- index: range.partIndex,
169
- total: parts,
170
- plaintextLen: sealFrom,
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;
@@ -12,6 +12,9 @@ import { NmtsError } from "./errors.js";
12
12
  * ⛔ IT IS NOT READ HERE. A file large enough to need several parts is a file too large to hold,
13
13
  * and the size is all that is needed to plan the upload and quote its price. The bytes are read
14
14
  * later, a slice at a time, by the part that is being sealed.
15
+ *
16
+ * ⚠ ZERO IS A SIZE. An empty file is sealed and stored like any other (`planFor` in
17
+ * `upload-price.ts`), so it is not refused here.
15
18
  */
16
19
  export function measureLocal(path) {
17
20
  let stat;
@@ -27,11 +30,5 @@ export function measureLocal(path) {
27
30
  nextStep: "This version uploads one file at a time.",
28
31
  });
29
32
  }
30
- if (stat.size === 0) {
31
- throw new NmtsError(`${path} is empty.`, {
32
- exitCode: 4,
33
- nextStep: "The storage network has nothing to store and would refuse the reservation.",
34
- });
35
- }
36
33
  return stat.size;
37
34
  }
@@ -27,6 +27,16 @@ export declare function creditsFor(sealedBytes: number, epochs?: number): number
27
27
  * that rather than doing it, and this is where the number comes from.
28
28
  */
29
29
  export declare function partSizeFor(chosen: string | number | undefined): number;
30
+ /**
31
+ * Where the cuts fall — and for an EMPTY file, one part that carries nothing.
32
+ *
33
+ * ⛔ AN EMPTY FILE IS A FILE, NOT A MISTAKE. S3 clients make them routinely, and NCF-3 seals zero
34
+ * bytes as one empty final chunk: 88 sealed bytes, which the server reserves and commits and every
35
+ * reader opens like any other part. `planParts` is a byte-for-byte copy shared with the browser
36
+ * and refuses a zero length, so the one extra case is said here, in the one place both the price
37
+ * and the sealing read the plan from.
38
+ */
39
+ export declare function planFor(size: number, partSize: number): PartRange[];
30
40
  /**
31
41
  * The plan, and what it will cost — one function so the price and the sealing cannot disagree.
32
42
  *
@@ -67,6 +67,18 @@ function parseSize(text) {
67
67
  return Number.NaN;
68
68
  return Number(match[1]) * factor;
69
69
  }
70
+ /**
71
+ * Where the cuts fall — and for an EMPTY file, one part that carries nothing.
72
+ *
73
+ * ⛔ AN EMPTY FILE IS A FILE, NOT A MISTAKE. S3 clients make them routinely, and NCF-3 seals zero
74
+ * bytes as one empty final chunk: 88 sealed bytes, which the server reserves and commits and every
75
+ * reader opens like any other part. `planParts` is a byte-for-byte copy shared with the browser
76
+ * and refuses a zero length, so the one extra case is said here, in the one place both the price
77
+ * and the sealing read the plan from.
78
+ */
79
+ export function planFor(size, partSize) {
80
+ return size === 0 ? [{ partIndex: 0, offset: 0, length: 0 }] : planParts(size, partSize);
81
+ }
70
82
  /**
71
83
  * The plan, and what it will cost — one function so the price and the sealing cannot disagree.
72
84
  *
@@ -75,7 +87,7 @@ function parseSize(text) {
75
87
  * differently from how it is actually stored.
76
88
  */
77
89
  export function planAndPrice(size, partSize, rule) {
78
- const plan = planParts(size, partSize);
90
+ const plan = planFor(size, partSize);
79
91
  const sealFor = (range) => range.partIndex === plan.length - 1
80
92
  ? paddedPlaintextLen(range.length, rule, { unitBytes: CREDIT_BYTES, shape: NCF3_SHAPE })
81
93
  : range.length;
@@ -0,0 +1,80 @@
1
+ import { type Reservation } from "./upload-store.ts";
2
+ import { type PaidPart } from "./upload-wire.ts";
3
+ /** What a refusal over another run's records is known by, for a program to branch on. */
4
+ export declare const UPLOAD_CONFLICT = "UPLOAD_CONFLICT";
5
+ /** A name for one upload of a file. Random: it says nothing about the file or the account. */
6
+ export declare function newRunId(): string;
7
+ /**
8
+ * The run this file's records belong to, or a new one when none of them names one.
9
+ *
10
+ * ⚠ A record an earlier version wrote names none. Its own key stays the one that version used
11
+ * (`runIdField`); the new id is only for the parts this run is the first to write down.
12
+ */
13
+ export declare function runIdFor(fileKey: string, parts: number): Promise<string>;
14
+ /**
15
+ * The run id a part's record is written with: the one it already has, or this upload's.
16
+ *
17
+ * ⛔ A RECORD KEEPS WHAT IT WAS WRITTEN WITH, including nothing. Its reservation is filed under the
18
+ * key it was made with; giving an earlier version's record a run id now would ask the server
19
+ * under a key it has never seen and buy the storage a second time.
20
+ */
21
+ export declare function runIdField(existing: Reservation | null, runId: string): {
22
+ runId?: string;
23
+ };
24
+ /**
25
+ * Run `body` once every earlier run of the same file in this process has finished.
26
+ *
27
+ * ⚠ WAITING, NOT REFUSING. Inside one process the first run is known to be alive, so the honest
28
+ * answer to a second one is "after it": it then finds a finished upload's records and answers
29
+ * the same file, or finds none and starts an upload of its own.
30
+ */
31
+ export declare function oneUploadAtATime<T>(fileKey: string, body: () => Promise<T>): Promise<T>;
32
+ /**
33
+ * Refuse a part record that another sealing of this file wrote.
34
+ *
35
+ * ⛔ THE WRAPPED FILE KEY SAYS WHICH SEALING A RECORD BELONGS TO. It is sealed afresh by every
36
+ * upload that starts from nothing and carried unchanged by every resume, so a record whose key
37
+ * is not this run's was written by another run — and its bytes open under a key this file's
38
+ * list entry will not hold.
39
+ */
40
+ export declare function refuseOtherSealing(existing: Reservation | null, input: {
41
+ runId: string;
42
+ entry: {
43
+ dekWrapped: string;
44
+ };
45
+ }): void;
46
+ /**
47
+ * The last look before the commit: every part is still written down as THIS run bought it.
48
+ *
49
+ * ⛔ THE READ AT THE START OF A PART IS NOT ENOUGH ACROSS PROCESSES. Another run can write its own
50
+ * record over one of these between that read and this commit; the commit names what this run
51
+ * paid for, so a record that no longer agrees means two sealings are interleaved, and a file
52
+ * made of both would not open.
53
+ */
54
+ export declare function confirmParts(fileKey: string, paid: readonly PaidPart[], entry: {
55
+ dekWrapped: string;
56
+ }): Promise<void>;
57
+ /**
58
+ * Forget a finished upload's records: the parts from the last to the first, then the file's own.
59
+ *
60
+ * ⛔ THE ORDER IS WHAT A RUN THAT STARTS IN THE MIDDLE OF IT CAN READ. A live upload's parts are
61
+ * always written down from the first, so what is left at any moment is a finished upload the
62
+ * next run recognises — a committed file with the first of its parts still named — or a file
63
+ * record alone, which names another run and is passed over. Forgetting the file's record first,
64
+ * or the parts from the front, left the tail of a finished upload looking like the start of a
65
+ * live one, which the next run would resume into keys the server has already settled.
66
+ */
67
+ export declare function forgetUpload(fileKey: string, parts: number): Promise<void>;
68
+ /**
69
+ * Forget records that cannot belong to a live upload: a part written down after one that is not,
70
+ * or a file's record with none of its parts.
71
+ *
72
+ * ⛔ WHY THOSE SHAPES MEAN "LEFT OVER". A live upload writes its parts down from the first, one at a
73
+ * time and each before its money moves; it writes the file's record only once every part is
74
+ * written down; and nothing removes any of them until the file is committed AND listed. So a gap
75
+ * in front of a part, or a file record alone, is a finished upload whose forgetting stopped half
76
+ * way (or ran in the old order). Resuming the first would seal the missing part afresh under a
77
+ * key the server has already settled for other bytes; resuming the second would answer the old
78
+ * file for new storage.
79
+ */
80
+ export declare function dropLeftovers(fileKey: string, parts: number): Promise<void>;