@needmoretruth/nmts-cli 0.34.4 → 0.36.1

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 (156) hide show
  1. package/AGENTS.md +11 -12
  2. package/CHANGELOG.md +43 -0
  3. package/README.ko.md +33 -35
  4. package/README.md +21 -22
  5. package/dist/account.js +2 -1
  6. package/dist/arg-options.d.ts +169 -0
  7. package/dist/arg-options.js +10 -0
  8. package/dist/args.d.ts +2 -157
  9. package/dist/args.js +2 -0
  10. package/dist/artifact-about.d.ts +1 -1
  11. package/dist/autonomy.d.ts +3 -10
  12. package/dist/autonomy.js +15 -17
  13. package/dist/bytes.d.ts +15 -0
  14. package/dist/bytes.js +63 -0
  15. package/dist/collision.d.ts +5 -5
  16. package/dist/collision.js +19 -22
  17. package/dist/commands/delete-account.js +1 -1
  18. package/dist/commands/erase.js +1 -1
  19. package/dist/commands/extend.d.ts +2 -0
  20. package/dist/commands/extend.js +8 -1
  21. package/dist/commands/get.js +1 -1
  22. package/dist/commands/listfile.js +1 -1
  23. package/dist/commands/mcp.js +5 -1
  24. package/dist/commands/mode.js +2 -2
  25. package/dist/commands/on-collision.d.ts +1 -1
  26. package/dist/commands/on-collision.js +3 -3
  27. package/dist/commands/platform.d.ts +9 -0
  28. package/dist/commands/platform.js +124 -0
  29. package/dist/commands/pull.js +1 -1
  30. package/dist/commands/push-wallet.js +8 -1
  31. package/dist/commands/push.d.ts +2 -0
  32. package/dist/commands/push.js +6 -4
  33. package/dist/commands/put-payer.d.ts +8 -0
  34. package/dist/commands/put-payer.js +41 -0
  35. package/dist/commands/put-wallet.d.ts +9 -3
  36. package/dist/commands/put-wallet.js +108 -186
  37. package/dist/commands/put.d.ts +5 -7
  38. package/dist/commands/put.js +11 -31
  39. package/dist/commands/receive.js +1 -1
  40. package/dist/commands/settings.js +1 -1
  41. package/dist/commands/support.js +3 -3
  42. package/dist/commands/unlock.js +2 -2
  43. package/dist/commands/wallet-donate.d.ts +3 -0
  44. package/dist/commands/wallet-donate.js +6 -2
  45. package/dist/commands/wallet-hall.d.ts +4 -1
  46. package/dist/commands/wallet-hall.js +16 -8
  47. package/dist/commands/wallet-list.d.ts +18 -0
  48. package/dist/commands/wallet-list.js +116 -0
  49. package/dist/commands/wallet-send.d.ts +4 -0
  50. package/dist/commands/wallet-send.js +6 -2
  51. package/dist/commands/wallet-storage-ops.d.ts +4 -1
  52. package/dist/commands/wallet-storage-ops.js +7 -2
  53. package/dist/commands/wallet-swap.d.ts +3 -0
  54. package/dist/commands/wallet-swap.js +8 -4
  55. package/dist/commands/wallet-use.d.ts +7 -0
  56. package/dist/commands/wallet-use.js +55 -0
  57. package/dist/commands/wallet.d.ts +4 -0
  58. package/dist/commands/wallet.js +20 -8
  59. package/dist/crypto.d.ts +8 -5
  60. package/dist/crypto.js +15 -61
  61. package/dist/download-sink-node.d.ts +49 -0
  62. package/dist/download-sink-node.js +204 -0
  63. package/dist/download-sink.d.ts +0 -48
  64. package/dist/download-sink.js +6 -199
  65. package/dist/download.js +5 -4
  66. package/dist/engine-node.d.ts +19 -0
  67. package/dist/engine-node.js +77 -0
  68. package/dist/env-vars.d.ts +20 -0
  69. package/dist/env-vars.js +27 -0
  70. package/dist/extend-plan.d.ts +2 -0
  71. package/dist/gate.js +1 -1
  72. package/dist/help.js +12 -5
  73. package/dist/host-contract.d.ts +7 -0
  74. package/dist/host-contract.js +95 -0
  75. package/dist/host-node.d.ts +27 -0
  76. package/dist/host-node.js +192 -0
  77. package/dist/host.d.ts +99 -0
  78. package/dist/host.js +59 -0
  79. package/dist/index.d.ts +6 -40
  80. package/dist/index.js +20 -32
  81. package/dist/main.js +11 -10
  82. package/dist/manifest-chunk-cache.d.ts +3 -3
  83. package/dist/manifest-chunk-cache.js +39 -44
  84. package/dist/manifest-chunk-flow.js +15 -14
  85. package/dist/manifest-write.js +1 -1
  86. package/dist/manifest.d.ts +2 -2
  87. package/dist/manifest.js +37 -34
  88. package/dist/mcp-tools/tiers.js +1 -1
  89. package/dist/network.d.ts +1 -1
  90. package/dist/network.js +6 -2
  91. package/dist/platform-sign.d.ts +143 -0
  92. package/dist/platform-sign.js +220 -0
  93. package/dist/portable.d.ts +59 -0
  94. package/dist/portable.js +82 -0
  95. package/dist/product.d.ts +1 -1
  96. package/dist/product.js +1 -1
  97. package/dist/progress-node.d.ts +2 -0
  98. package/dist/progress-node.js +14 -0
  99. package/dist/progress.d.ts +0 -1
  100. package/dist/progress.js +4 -8
  101. package/dist/redact.js +3 -2
  102. package/dist/registration.js +5 -2
  103. package/dist/risk.d.ts +11 -0
  104. package/dist/risk.js +16 -0
  105. package/dist/run-log.d.ts +4 -4
  106. package/dist/run-log.js +29 -36
  107. package/dist/seal.js +5 -4
  108. package/dist/server.d.ts +1 -1
  109. package/dist/server.js +6 -2
  110. package/dist/setup-questions.js +2 -2
  111. package/dist/shared/lib/drive/manifest-ops.d.ts +8 -30
  112. package/dist/shared/lib/drive/manifest-ops.js +7 -39
  113. package/dist/shared/lib/drive/manifest-settings-patch.d.ts +52 -0
  114. package/dist/shared/lib/drive/manifest-settings-patch.js +110 -0
  115. package/dist/shared/lib/drive/manifest-settings.d.ts +55 -4
  116. package/dist/shared/lib/drive/manifest-settings.js +66 -28
  117. package/dist/shared/lib/wallet/discover.d.ts +54 -0
  118. package/dist/shared/lib/wallet/discover.js +66 -0
  119. package/dist/standing-tip.d.ts +3 -0
  120. package/dist/standing-tip.js +1 -0
  121. package/dist/upload-file-node.d.ts +3 -0
  122. package/dist/upload-file-node.js +38 -0
  123. package/dist/upload-file.d.ts +2 -2
  124. package/dist/upload-file.js +15 -45
  125. package/dist/upload-price-node.d.ts +8 -0
  126. package/dist/upload-price-node.js +37 -0
  127. package/dist/upload-price.d.ts +0 -8
  128. package/dist/upload-price.js +4 -33
  129. package/dist/upload-steps.js +3 -3
  130. package/dist/upload-store.d.ts +14 -14
  131. package/dist/upload-store.js +68 -75
  132. package/dist/upload-wallet-chain.js +3 -2
  133. package/dist/upload-wallet-put.d.ts +118 -0
  134. package/dist/upload-wallet-put.js +221 -0
  135. package/dist/upload-wallet.d.ts +3 -0
  136. package/dist/upload-wallet.js +18 -15
  137. package/dist/upload.js +16 -15
  138. package/dist/wallet-list-chain.d.ts +4 -0
  139. package/dist/wallet-list-chain.js +21 -0
  140. package/dist/wallet-pay-index.d.ts +21 -0
  141. package/dist/wallet-pay-index.js +68 -0
  142. package/dist/wallet-sign-seams.d.ts +69 -0
  143. package/dist/wallet-sign-seams.js +16 -0
  144. package/dist/wallet-sign.d.ts +3 -53
  145. package/dist/wallet-sign.js +22 -18
  146. package/dist/wallet.d.ts +13 -13
  147. package/dist/wallet.js +15 -15
  148. package/dist/walrus-write.js +3 -2
  149. package/dist/walrus.d.ts +1 -16
  150. package/dist/walrus.js +11 -22
  151. package/docs/commands/credits.md +1 -1
  152. package/docs/commands/platform.md +14 -0
  153. package/docs/commands/support.md +1 -1
  154. package/docs/commands/trash.md +1 -1
  155. package/docs/commands/wallet.md +26 -8
  156. package/package.json +13 -2
@@ -0,0 +1,204 @@
1
+ // The two places this tool can put a downloaded file: a real file, and its own stdout.
2
+ //
3
+ // ⛔ THE SHAPE IS `download-sink.ts`'S AND THE PROMISE IS KEPT HERE. A sink is the thing that
4
+ // decides what "delivered" means, and these two decide it with a rename and with a hold — both
5
+ // of which need Node. A browser's sink is the caller's, written against the same interface.
6
+ import { randomBytes } from "node:crypto";
7
+ import { createWriteStream, existsSync } from "node:fs";
8
+ import { rename, rm } from "node:fs/promises";
9
+ import { basename, dirname, join } from "node:path";
10
+ import { NmtsError } from "./errors.js";
11
+ import { handOver } from "./stdout.js";
12
+ /** The one refusal text for a destination that is already occupied. Shared by every command. */
13
+ function alreadyThere(destination) {
14
+ return new NmtsError(`${destination} already exists.`, {
15
+ exitCode: 4,
16
+ nextStep: "Nothing was written. Pass --out to choose another name, or --force to replace it.",
17
+ });
18
+ }
19
+ /**
20
+ * Write to a file, through a temporary name beside it, renamed into place only when the whole
21
+ * file has been proved.
22
+ *
23
+ * ⛔ THE TEMPORARY FILE IS IN THE SAME DIRECTORY AS THE DESTINATION, not in a system temporary
24
+ * directory, and that is the whole mechanism: `rename` is atomic only within one filesystem,
25
+ * and only an atomic rename guarantees that a reader either sees nothing under the real name or
26
+ * sees the finished file. A temporary directory can be — and on the machines this runs on
27
+ * usually is — a different filesystem, where the same call becomes copy-then-delete and a
28
+ * reader can catch the copy half done.
29
+ *
30
+ * ⛔ IT IS CREATED 0600 AND THE RENAME KEEPS THAT. The file is somebody's plaintext; it must not
31
+ * be readable by other accounts on the machine for the seconds it takes to download, any more
32
+ * than it may be afterwards.
33
+ */
34
+ export function fileSink(destination, options) {
35
+ // ⛔ Asked BEFORE the download rather than at the end, which is where the old code found out.
36
+ // Fetching a file to discover it had nowhere to go spent somebody's time and the storage
37
+ // network's bandwidth for a refusal that was knowable up front.
38
+ if (!options.force && existsSync(destination))
39
+ throw alreadyThere(destination);
40
+ const temporary = join(dirname(destination), `.${basename(destination)}.nmts-${randomBytes(6).toString("hex")}.part`);
41
+ let stream = null;
42
+ let failure = null;
43
+ let renamed = false;
44
+ // ⚠ Created on the first write rather than up front: a download that fails before it produces a
45
+ // byte — a wrong key, a part on a network this build cannot read — then leaves no file at all,
46
+ // not even one that appears and disappears.
47
+ const opened = () => {
48
+ if (stream !== null)
49
+ return stream;
50
+ const fresh = createWriteStream(temporary, { flags: "wx", mode: 0o600 });
51
+ // ⛔ An 'error' with no listener is thrown out of the event loop and kills the process, which
52
+ // would turn "the disk is full" into a crash with no next step. Kept, and reported by the
53
+ // next call that can report it.
54
+ fresh.on("error", (error) => {
55
+ failure = error;
56
+ });
57
+ stream = fresh;
58
+ return fresh;
59
+ };
60
+ const closed = async () => {
61
+ const open = stream;
62
+ if (open === null)
63
+ return;
64
+ stream = null;
65
+ // ⚠ Waits for 'close', not 'finish': a stream that failed to open never finishes, and waiting
66
+ // for the wrong event there is a hang rather than an error.
67
+ await new Promise((resolve) => {
68
+ if (open.closed) {
69
+ resolve();
70
+ return;
71
+ }
72
+ open.once("close", () => resolve());
73
+ open.end();
74
+ });
75
+ };
76
+ const abandon = async () => {
77
+ if (renamed)
78
+ return;
79
+ try {
80
+ await closed();
81
+ }
82
+ catch {
83
+ // Nothing to do about it: the file below is going away either way.
84
+ }
85
+ await rm(temporary, { force: true }).catch(() => undefined);
86
+ };
87
+ return {
88
+ // Nothing to refuse: what a disk cannot take, the write itself reports, and the temporary
89
+ // file means a disk that fills up leaves no file under the name somebody asked for.
90
+ expect: () => undefined,
91
+ write: async (bytes) => {
92
+ const target = opened();
93
+ await new Promise((resolve, reject) => {
94
+ const known = failure;
95
+ if (known !== null) {
96
+ reject(known);
97
+ return;
98
+ }
99
+ target.write(bytes, (error) => {
100
+ if (error === undefined || error === null)
101
+ resolve();
102
+ else
103
+ reject(error);
104
+ });
105
+ });
106
+ },
107
+ commit: async () => {
108
+ // A file of zero bytes is still a file, so the stream is opened even if nothing was written.
109
+ opened();
110
+ await closed();
111
+ const known = failure;
112
+ if (known !== null) {
113
+ await abandon();
114
+ throw new NmtsError(`${destination} could not be written: ${known.message}`, {
115
+ exitCode: 1,
116
+ nextStep: "Nothing was left under that name. The download itself was fine — this is the disk.",
117
+ });
118
+ }
119
+ // ⛔ Asked a second time, as late as possible. The first check was before the download; a
120
+ // file that appeared in the meantime is somebody else's, and rename would silently
121
+ // replace it. The window is now the width of one system call rather than a download.
122
+ if (!options.force && existsSync(destination)) {
123
+ await abandon();
124
+ throw alreadyThere(destination);
125
+ }
126
+ await rename(temporary, destination);
127
+ renamed = true;
128
+ return true;
129
+ },
130
+ abandon,
131
+ };
132
+ }
133
+ /**
134
+ * How much plaintext `--out -` will hold before it hands anything over. One part's worth.
135
+ *
136
+ * ⛔ NOT AN ARBITRARY NUMBER: it is the upload path's default part size, which is the most memory
137
+ * this tool has ever asked for. Keeping the stdout ceiling there means the whole tool's bound
138
+ * is one part plus one chunk whichever direction the bytes are going.
139
+ */
140
+ export const STDOUT_HOLD_LIMIT = 64 * 2 ** 20;
141
+ /**
142
+ * Hand the whole file to whatever is reading stdout — after it has been proved, not during.
143
+ *
144
+ * ⛔ THERE IS NO RENAME ON A PIPE, so the trick the file path uses is not available: a byte handed
145
+ * to a reader cannot be taken back, and a reader that has already consumed half a file cannot
146
+ * be told afterwards that the half was wrong. An exit code does not fix that — `nmts get x
147
+ * --out - > y` leaves the truncated bytes in `y` whatever the exit code says, and an agent
148
+ * reading a pipe usually acts on what it read. So this branch keeps the ORIGINAL guarantee
149
+ * rather than the original size: the file is held, checked, and only then handed over, exactly
150
+ * as before streaming existed.
151
+ *
152
+ * ⛔ WHICH MEANS IT HAS TO REFUSE SOMEWHERE, AND IT REFUSES OUT LOUD AND EARLY. Above the ceiling
153
+ * the answer is a refusal naming `--out <name>`, which streams and is checked before the file
154
+ * appears — not a silent trade of the guarantee for the size, and not the out-of-memory crash
155
+ * that this used to be. `put` already refuses this way rather than discovering a limit halfway.
156
+ *
157
+ * ⚠ Every protection this branch already had is still here and still applies to the whole file:
158
+ * bytes a terminal would act on are refused (`readableOnATerminal`), a reader that closed the
159
+ * pipe first is an ordinary end and not a failure, and any other write failure throws.
160
+ */
161
+ export function stdoutSink(to, limit = STDOUT_HOLD_LIMIT) {
162
+ const held = [];
163
+ let total = 0;
164
+ const tooLarge = (size) => new NmtsError(`This file is ${size} bytes, and \`--out -\` hands over at most ${limit}.`, {
165
+ exitCode: 4,
166
+ nextStep: "Nothing was written and nothing was sent. A pipe cannot be taken back, so this mode " +
167
+ "proves the whole file before it sends a byte, which means holding it. Use `--out <name>` " +
168
+ "— that writes any size, and the file only appears under that name once it has been checked.",
169
+ });
170
+ const forget = () => {
171
+ for (const run of held)
172
+ run.fill(0);
173
+ held.length = 0;
174
+ };
175
+ return {
176
+ expect: (size) => {
177
+ if (size > limit)
178
+ throw tooLarge(size);
179
+ },
180
+ write: async (bytes) => {
181
+ // ⚠ Copied, because the caller zeroes what it handed over as soon as this resolves.
182
+ total += bytes.length;
183
+ if (total > limit) {
184
+ forget();
185
+ throw tooLarge(total);
186
+ }
187
+ held.push(new Uint8Array(bytes));
188
+ },
189
+ commit: async () => {
190
+ const whole = new Uint8Array(total);
191
+ let at = 0;
192
+ for (const run of held) {
193
+ whole.set(run, at);
194
+ at += run.length;
195
+ }
196
+ forget();
197
+ // ⚠ NOT zeroed afterwards, deliberately: `ByteDestination` promises only that the bytes have
198
+ // been handed over, and a destination is entitled to keep the array it was given — the
199
+ // tests use one that does. Wiping it here would hand a reader a buffer of zeroes.
200
+ return await handOver(whole, to);
201
+ },
202
+ abandon: async () => forget(),
203
+ };
204
+ }
@@ -1,4 +1,3 @@
1
- import { type ByteDestination } from "./stdout.ts";
2
1
  /**
3
2
  * Somewhere a file's plaintext is delivered in order, and made visible only once.
4
3
  *
@@ -27,50 +26,3 @@ export interface PlaintextSink {
27
26
  /** Something did not check out: leave nothing behind. Never throws. */
28
27
  abandon(): Promise<void>;
29
28
  }
30
- /**
31
- * Write to a file, through a temporary name beside it, renamed into place only when the whole
32
- * file has been proved.
33
- *
34
- * ⛔ THE TEMPORARY FILE IS IN THE SAME DIRECTORY AS THE DESTINATION, not in a system temporary
35
- * directory, and that is the whole mechanism: `rename` is atomic only within one filesystem,
36
- * and only an atomic rename guarantees that a reader either sees nothing under the real name or
37
- * sees the finished file. A temporary directory can be — and on the machines this runs on
38
- * usually is — a different filesystem, where the same call becomes copy-then-delete and a
39
- * reader can catch the copy half done.
40
- *
41
- * ⛔ IT IS CREATED 0600 AND THE RENAME KEEPS THAT. The file is somebody's plaintext; it must not
42
- * be readable by other accounts on the machine for the seconds it takes to download, any more
43
- * than it may be afterwards.
44
- */
45
- export declare function fileSink(destination: string, options: {
46
- force: boolean;
47
- }): PlaintextSink;
48
- /**
49
- * How much plaintext `--out -` will hold before it hands anything over. One part's worth.
50
- *
51
- * ⛔ NOT AN ARBITRARY NUMBER: it is the upload path's default part size, which is the most memory
52
- * this tool has ever asked for. Keeping the stdout ceiling there means the whole tool's bound
53
- * is one part plus one chunk whichever direction the bytes are going.
54
- */
55
- export declare const STDOUT_HOLD_LIMIT: number;
56
- /**
57
- * Hand the whole file to whatever is reading stdout — after it has been proved, not during.
58
- *
59
- * ⛔ THERE IS NO RENAME ON A PIPE, so the trick the file path uses is not available: a byte handed
60
- * to a reader cannot be taken back, and a reader that has already consumed half a file cannot
61
- * be told afterwards that the half was wrong. An exit code does not fix that — `nmts get x
62
- * --out - > y` leaves the truncated bytes in `y` whatever the exit code says, and an agent
63
- * reading a pipe usually acts on what it read. So this branch keeps the ORIGINAL guarantee
64
- * rather than the original size: the file is held, checked, and only then handed over, exactly
65
- * as before streaming existed.
66
- *
67
- * ⛔ WHICH MEANS IT HAS TO REFUSE SOMEWHERE, AND IT REFUSES OUT LOUD AND EARLY. Above the ceiling
68
- * the answer is a refusal naming `--out <name>`, which streams and is checked before the file
69
- * appears — not a silent trade of the guarantee for the size, and not the out-of-memory crash
70
- * that this used to be. `put` already refuses this way rather than discovering a limit halfway.
71
- *
72
- * ⚠ Every protection this branch already had is still here and still applies to the whole file:
73
- * bytes a terminal would act on are refused (`readableOnATerminal`), a reader that closed the
74
- * pipe first is an ordinary end and not a failure, and any other write failure throws.
75
- */
76
- export declare function stdoutSink(to: ByteDestination, limit?: number): PlaintextSink;
@@ -12,202 +12,9 @@
12
12
  // ⛔ A SINK MUST NOT KEEP THE ARRAY IT IS HANDED. The caller zeroes each run of plaintext as soon
13
13
  // as `write` resolves, so a sink that stored the reference would hold a buffer full of zeroes
14
14
  // and write them out. Copy, or finish with the bytes before resolving.
15
- import { randomBytes } from "node:crypto";
16
- import { createWriteStream, existsSync } from "node:fs";
17
- import { rename, rm } from "node:fs/promises";
18
- import { basename, dirname, join } from "node:path";
19
- import { NmtsError } from "./errors.js";
20
- import { handOver } from "./stdout.js";
21
- /** The one refusal text for a destination that is already occupied. Shared by every command. */
22
- function alreadyThere(destination) {
23
- return new NmtsError(`${destination} already exists.`, {
24
- exitCode: 4,
25
- nextStep: "Nothing was written. Pass --out to choose another name, or --force to replace it.",
26
- });
27
- }
28
- /**
29
- * Write to a file, through a temporary name beside it, renamed into place only when the whole
30
- * file has been proved.
31
- *
32
- * ⛔ THE TEMPORARY FILE IS IN THE SAME DIRECTORY AS THE DESTINATION, not in a system temporary
33
- * directory, and that is the whole mechanism: `rename` is atomic only within one filesystem,
34
- * and only an atomic rename guarantees that a reader either sees nothing under the real name or
35
- * sees the finished file. A temporary directory can be — and on the machines this runs on
36
- * usually is — a different filesystem, where the same call becomes copy-then-delete and a
37
- * reader can catch the copy half done.
38
- *
39
- * ⛔ IT IS CREATED 0600 AND THE RENAME KEEPS THAT. The file is somebody's plaintext; it must not
40
- * be readable by other accounts on the machine for the seconds it takes to download, any more
41
- * than it may be afterwards.
42
- */
43
- export function fileSink(destination, options) {
44
- // ⛔ Asked BEFORE the download rather than at the end, which is where the old code found out.
45
- // Fetching a file to discover it had nowhere to go spent somebody's time and the storage
46
- // network's bandwidth for a refusal that was knowable up front.
47
- if (!options.force && existsSync(destination))
48
- throw alreadyThere(destination);
49
- const temporary = join(dirname(destination), `.${basename(destination)}.nmts-${randomBytes(6).toString("hex")}.part`);
50
- let stream = null;
51
- let failure = null;
52
- let renamed = false;
53
- // ⚠ Created on the first write rather than up front: a download that fails before it produces a
54
- // byte — a wrong key, a part on a network this build cannot read — then leaves no file at all,
55
- // not even one that appears and disappears.
56
- const opened = () => {
57
- if (stream !== null)
58
- return stream;
59
- const fresh = createWriteStream(temporary, { flags: "wx", mode: 0o600 });
60
- // ⛔ An 'error' with no listener is thrown out of the event loop and kills the process, which
61
- // would turn "the disk is full" into a crash with no next step. Kept, and reported by the
62
- // next call that can report it.
63
- fresh.on("error", (error) => {
64
- failure = error;
65
- });
66
- stream = fresh;
67
- return fresh;
68
- };
69
- const closed = async () => {
70
- const open = stream;
71
- if (open === null)
72
- return;
73
- stream = null;
74
- // ⚠ Waits for 'close', not 'finish': a stream that failed to open never finishes, and waiting
75
- // for the wrong event there is a hang rather than an error.
76
- await new Promise((resolve) => {
77
- if (open.closed) {
78
- resolve();
79
- return;
80
- }
81
- open.once("close", () => resolve());
82
- open.end();
83
- });
84
- };
85
- const abandon = async () => {
86
- if (renamed)
87
- return;
88
- try {
89
- await closed();
90
- }
91
- catch {
92
- // Nothing to do about it: the file below is going away either way.
93
- }
94
- await rm(temporary, { force: true }).catch(() => undefined);
95
- };
96
- return {
97
- // Nothing to refuse: what a disk cannot take, the write itself reports, and the temporary
98
- // file means a disk that fills up leaves no file under the name somebody asked for.
99
- expect: () => undefined,
100
- write: async (bytes) => {
101
- const target = opened();
102
- await new Promise((resolve, reject) => {
103
- const known = failure;
104
- if (known !== null) {
105
- reject(known);
106
- return;
107
- }
108
- target.write(bytes, (error) => {
109
- if (error === undefined || error === null)
110
- resolve();
111
- else
112
- reject(error);
113
- });
114
- });
115
- },
116
- commit: async () => {
117
- // A file of zero bytes is still a file, so the stream is opened even if nothing was written.
118
- opened();
119
- await closed();
120
- const known = failure;
121
- if (known !== null) {
122
- await abandon();
123
- throw new NmtsError(`${destination} could not be written: ${known.message}`, {
124
- exitCode: 1,
125
- nextStep: "Nothing was left under that name. The download itself was fine — this is the disk.",
126
- });
127
- }
128
- // ⛔ Asked a second time, as late as possible. The first check was before the download; a
129
- // file that appeared in the meantime is somebody else's, and rename would silently
130
- // replace it. The window is now the width of one system call rather than a download.
131
- if (!options.force && existsSync(destination)) {
132
- await abandon();
133
- throw alreadyThere(destination);
134
- }
135
- await rename(temporary, destination);
136
- renamed = true;
137
- return true;
138
- },
139
- abandon,
140
- };
141
- }
142
- /**
143
- * How much plaintext `--out -` will hold before it hands anything over. One part's worth.
144
- *
145
- * ⛔ NOT AN ARBITRARY NUMBER: it is the upload path's default part size, which is the most memory
146
- * this tool has ever asked for. Keeping the stdout ceiling there means the whole tool's bound
147
- * is one part plus one chunk whichever direction the bytes are going.
148
- */
149
- export const STDOUT_HOLD_LIMIT = 64 * 2 ** 20;
150
- /**
151
- * Hand the whole file to whatever is reading stdout — after it has been proved, not during.
152
- *
153
- * ⛔ THERE IS NO RENAME ON A PIPE, so the trick the file path uses is not available: a byte handed
154
- * to a reader cannot be taken back, and a reader that has already consumed half a file cannot
155
- * be told afterwards that the half was wrong. An exit code does not fix that — `nmts get x
156
- * --out - > y` leaves the truncated bytes in `y` whatever the exit code says, and an agent
157
- * reading a pipe usually acts on what it read. So this branch keeps the ORIGINAL guarantee
158
- * rather than the original size: the file is held, checked, and only then handed over, exactly
159
- * as before streaming existed.
160
- *
161
- * ⛔ WHICH MEANS IT HAS TO REFUSE SOMEWHERE, AND IT REFUSES OUT LOUD AND EARLY. Above the ceiling
162
- * the answer is a refusal naming `--out <name>`, which streams and is checked before the file
163
- * appears — not a silent trade of the guarantee for the size, and not the out-of-memory crash
164
- * that this used to be. `put` already refuses this way rather than discovering a limit halfway.
165
- *
166
- * ⚠ Every protection this branch already had is still here and still applies to the whole file:
167
- * bytes a terminal would act on are refused (`readableOnATerminal`), a reader that closed the
168
- * pipe first is an ordinary end and not a failure, and any other write failure throws.
169
- */
170
- export function stdoutSink(to, limit = STDOUT_HOLD_LIMIT) {
171
- const held = [];
172
- let total = 0;
173
- const tooLarge = (size) => new NmtsError(`This file is ${size} bytes, and \`--out -\` hands over at most ${limit}.`, {
174
- exitCode: 4,
175
- nextStep: "Nothing was written and nothing was sent. A pipe cannot be taken back, so this mode " +
176
- "proves the whole file before it sends a byte, which means holding it. Use `--out <name>` " +
177
- "— that writes any size, and the file only appears under that name once it has been checked.",
178
- });
179
- const forget = () => {
180
- for (const run of held)
181
- run.fill(0);
182
- held.length = 0;
183
- };
184
- return {
185
- expect: (size) => {
186
- if (size > limit)
187
- throw tooLarge(size);
188
- },
189
- write: async (bytes) => {
190
- // ⚠ Copied, because the caller zeroes what it handed over as soon as this resolves.
191
- total += bytes.length;
192
- if (total > limit) {
193
- forget();
194
- throw tooLarge(total);
195
- }
196
- held.push(new Uint8Array(bytes));
197
- },
198
- commit: async () => {
199
- const whole = new Uint8Array(total);
200
- let at = 0;
201
- for (const run of held) {
202
- whole.set(run, at);
203
- at += run.length;
204
- }
205
- forget();
206
- // ⚠ NOT zeroed afterwards, deliberately: `ByteDestination` promises only that the bytes have
207
- // been handed over, and a destination is entitled to keep the array it was given — the
208
- // tests use one that does. Wiping it here would hand a reader a buffer of zeroes.
209
- return await handOver(whole, to);
210
- },
211
- abandon: async () => forget(),
212
- };
213
- }
15
+ //
16
+ // ONLY THE SHAPE IS HERE. The two destinations this tool ships — a file on a disk, and whatever
17
+ // is reading its stdout — are both Node's, and they live in `download-sink-node.ts`. What is
18
+ // left is the contract, which is what a caller in a browser implements to hand a download to a
19
+ // `Blob`, an element or a stream of its own.
20
+ export {};
package/dist/download.js CHANGED
@@ -26,7 +26,8 @@
26
26
  // the engine's own buffer for the chunk it is assembling, and the one chunk of plaintext it
27
27
  // hands back — 4 MiB each in NCF-3. A hundred-gigabyte file costs the same as a hundred-megabyte
28
28
  // one. The exception is `--out -`, which is bounded by `STDOUT_HOLD_LIMIT` and refuses above it.
29
- import { createHash } from "node:crypto";
29
+ import { sha256 } from "@noble/hashes/sha2.js";
30
+ import { fromBase64Url, utf8 } from "./bytes.js";
30
31
  import { request } from "./api.js";
31
32
  import { AAD, DERIVED, loadCrypto } from "./crypto.js";
32
33
  import { asParts, fetchPart, openPart } from "./download-part.js";
@@ -69,7 +70,7 @@ export async function fetchFile(input) {
69
70
  derived.fill(0);
70
71
  let dek;
71
72
  try {
72
- dek = crypt.envelope_open(dataKey, new TextEncoder().encode(AAD.dekWrap), Buffer.from(input.dekWrapped, "base64url"));
73
+ dek = crypt.envelope_open(dataKey, utf8(AAD.dekWrap), fromBase64Url(input.dekWrapped));
73
74
  }
74
75
  catch {
75
76
  dataKey.fill(0);
@@ -80,7 +81,7 @@ export async function fetchFile(input) {
80
81
  let expected = null;
81
82
  if (input.contentHashCt !== undefined && input.contentHashCt !== "") {
82
83
  try {
83
- expected = crypt.envelope_open(dataKey, new TextEncoder().encode(AAD.contentHash), Buffer.from(input.contentHashCt, "base64url"));
84
+ expected = crypt.envelope_open(dataKey, utf8(AAD.contentHash), fromBase64Url(input.contentHashCt));
84
85
  }
85
86
  catch {
86
87
  dataKey.fill(0);
@@ -115,7 +116,7 @@ export async function fetchFile(input) {
115
116
  * sees the finished file or sees nothing under that name.
116
117
  */
117
118
  async function collect(crypt, ordered, dek, expected, size, chain, read, sink) {
118
- const hasher = createHash("sha256");
119
+ const hasher = sha256.create();
119
120
  let remaining = size;
120
121
  try {
121
122
  // ⛔ Asked before a byte is fetched. A destination that cannot take a file this size says so
@@ -0,0 +1,19 @@
1
+ import { type CryptoGlue } from "./crypto-surface.ts";
2
+ /**
3
+ * Where the engine's WebAssembly sits, in the three places an installation can put it.
4
+ *
5
+ * ⛔ FOUND RATHER THAN CONFIGURED. The package carries it in `vendor/`, a checkout runs from
6
+ * `src/` with the same folder one level further up, and the repository's own web tree holds a
7
+ * build of the same crate. A path in a setting would be a fourth answer nobody keeps right.
8
+ */
9
+ export declare function engineDir(): string;
10
+ /**
11
+ * Load the engine once per process.
12
+ *
13
+ * ⛔ NO TYPE ASSERTION. A dynamically imported module is `unknown`, and staying honest about that
14
+ * matters here more than anywhere: a rebuild that renamed an export would otherwise become
15
+ * "undefined is not a function" deep inside a derivation.
16
+ */
17
+ export declare function loadEngine(): Promise<CryptoGlue>;
18
+ /** For tests that need a fresh load. */
19
+ export declare function forgetEngine(): void;
@@ -0,0 +1,77 @@
1
+ // Finding and loading the engine on a machine with a disk.
2
+ //
3
+ // ⛔ NOTHING IS RE-IMPLEMENTED HERE. The bytes that derive an account's keys are the same
4
+ // WebAssembly the browser runs, built from the same Rust crate — this file only finds the build
5
+ // and hands it to the shape check every load passes.
6
+ //
7
+ // ⛔ SEPARATE FROM `host-node.ts` SO THAT REGISTERING A HOST COSTS NOTHING. `nmts --help` puts the
8
+ // host in place and never derives anything; loading this file, and the four `node:` modules it
9
+ // needs, on every run would be a fixed cost an agent pays thousands of times.
10
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
11
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
12
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
13
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
14
+ });
15
+ }
16
+ return path;
17
+ };
18
+ import { existsSync } from "node:fs";
19
+ import { readFile } from "node:fs/promises";
20
+ import { dirname, join } from "node:path";
21
+ import { fileURLToPath, pathToFileURL } from "node:url";
22
+ import { isCryptoGlue, missingExports } from "./crypto-surface.js";
23
+ import { NmtsError } from "./errors.js";
24
+ /**
25
+ * Where the engine's WebAssembly sits, in the three places an installation can put it.
26
+ *
27
+ * ⛔ FOUND RATHER THAN CONFIGURED. The package carries it in `vendor/`, a checkout runs from
28
+ * `src/` with the same folder one level further up, and the repository's own web tree holds a
29
+ * build of the same crate. A path in a setting would be a fourth answer nobody keeps right.
30
+ */
31
+ export function engineDir() {
32
+ const here = dirname(fileURLToPath(import.meta.url));
33
+ const candidates = [
34
+ join(here, "..", "vendor", "nmts-crypto"),
35
+ join(here, "..", "..", "vendor", "nmts-crypto"),
36
+ join(here, "..", "..", "web", "vendor", "nmts-crypto"),
37
+ ];
38
+ for (const dir of candidates) {
39
+ if (existsSync(join(dir, "nmts_crypto_wasm_bg.wasm")))
40
+ return dir;
41
+ }
42
+ throw new NmtsError("The NMTS crypto engine is missing from this installation.", {
43
+ exitCode: 1,
44
+ nextStep: "Reinstall the package. Nothing can be encrypted or decrypted without it.",
45
+ });
46
+ }
47
+ let engine = null;
48
+ /**
49
+ * Load the engine once per process.
50
+ *
51
+ * ⛔ NO TYPE ASSERTION. A dynamically imported module is `unknown`, and staying honest about that
52
+ * matters here more than anywhere: a rebuild that renamed an export would otherwise become
53
+ * "undefined is not a function" deep inside a derivation.
54
+ */
55
+ export async function loadEngine() {
56
+ if (engine !== null)
57
+ return engine;
58
+ const dir = engineDir();
59
+ const module = await import(__rewriteRelativeImportExtension(pathToFileURL(join(dir, "nmts_crypto_wasm.js")).href));
60
+ if (typeof module !== "object" || module === null || !("default" in module)) {
61
+ throw new NmtsError("The NMTS crypto engine did not load (no initialiser).", { exitCode: 1 });
62
+ }
63
+ const init = Reflect.get(module, "default");
64
+ if (typeof init !== "function") {
65
+ throw new NmtsError("The NMTS crypto engine did not load (initialiser is not callable).", { exitCode: 1 });
66
+ }
67
+ await init({ module_or_path: await readFile(join(dir, "nmts_crypto_wasm_bg.wasm")) });
68
+ if (!isCryptoGlue(module)) {
69
+ throw new NmtsError(`The NMTS crypto engine is missing: ${missingExports(module).join(", ")}. This build does not match this tool.`, { exitCode: 1, nextStep: "Reinstall the package." });
70
+ }
71
+ engine = module;
72
+ return module;
73
+ }
74
+ /** For tests that need a fresh load. */
75
+ export function forgetEngine() {
76
+ engine = null;
77
+ }
@@ -0,0 +1,20 @@
1
+ /** Which NMTS server this run talks to. */
2
+ export declare const SERVER_ENV_VAR = "NMTS_SERVER";
3
+ /** Which storage network that server uses. */
4
+ export declare const NETWORK_ENV_VAR = "NMTS_NETWORK";
5
+ /**
6
+ * Point reads at somebody else's aggregator, or at a development stack.
7
+ *
8
+ * ⚠ It replaces the list rather than adding to it, and that is deliberate: a run should read from
9
+ * where it was told to read, not from there AND the public hosts. Comma-separated for more
10
+ * than one, tried in the order given.
11
+ */
12
+ export declare const AGGREGATOR_ENV_VAR = "NMTS_AGGREGATOR";
13
+ /**
14
+ * Push writes through somebody else's relay, or through a development stack.
15
+ *
16
+ * ⚠ ONE host, not a list. Unlike reads there is nothing to fail over to — see `RELAY_HOSTS`.
17
+ */
18
+ export declare const RELAY_ENV_VAR = "NMTS_RELAY";
19
+ /** Ask a different Sui JSON-RPC node the shard-count question. */
20
+ export declare const SUI_RPC_ENV_VAR = "NMTS_SUI_RPC";
@@ -0,0 +1,27 @@
1
+ // The names of the environment variables this tool reads.
2
+ //
3
+ // ⛔ NAMES ONLY, AND NO IMPORTS. `nmts --help` prints several of these, and a help text that had to
4
+ // load the module that USES a variable would drag that module's whole chain into every run —
5
+ // `check:cli-startup` measures exactly that, and it is the leak it has caught most often. The
6
+ // modules that read the variables re-export their own name from here, so every caller still
7
+ // finds it where it belongs and there is still only one spelling of each.
8
+ /** Which NMTS server this run talks to. */
9
+ export const SERVER_ENV_VAR = "NMTS_SERVER";
10
+ /** Which storage network that server uses. */
11
+ export const NETWORK_ENV_VAR = "NMTS_NETWORK";
12
+ /**
13
+ * Point reads at somebody else's aggregator, or at a development stack.
14
+ *
15
+ * ⚠ It replaces the list rather than adding to it, and that is deliberate: a run should read from
16
+ * where it was told to read, not from there AND the public hosts. Comma-separated for more
17
+ * than one, tried in the order given.
18
+ */
19
+ export const AGGREGATOR_ENV_VAR = "NMTS_AGGREGATOR";
20
+ /**
21
+ * Push writes through somebody else's relay, or through a development stack.
22
+ *
23
+ * ⚠ ONE host, not a list. Unlike reads there is nothing to fail over to — see `RELAY_HOSTS`.
24
+ */
25
+ export const RELAY_ENV_VAR = "NMTS_RELAY";
26
+ /** Ask a different Sui JSON-RPC node the shard-count question. */
27
+ export const SUI_RPC_ENV_VAR = "NMTS_SUI_RPC";
@@ -143,6 +143,8 @@ export type SignExtension = (input: {
143
143
  network: string;
144
144
  /** ⛔ The NMTS key. It never leaves this machine: it derives the wallet and nothing else. */
145
145
  code: string;
146
+ /** Which of this key's wallets pays — the account's own number (`wallet-pay-index.ts`). */
147
+ wallet: number;
146
148
  objectIds: readonly string[];
147
149
  epochs: number;
148
150
  }) => Promise<string>;