@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,217 @@
1
+ // `nmts public-code list ยท new ยท revoke` โ€” the account's numbered public codes.
2
+ //
3
+ // โ›” WHAT IS PRINTED IS WHAT THIS KEY DERIVES, CHECKED AGAINST WHAT THE SERVER HOLDS. Each code is
4
+ // derived here at its number and compared with the server's address; a difference stops the
5
+ // command with the bigger fact (`differentCode`), because a person gives these out and a code the
6
+ // key does not make is somebody else's.
7
+ //
8
+ // โ›” REVOKING NEVER HAPPENS WITHOUT A YES. `revoke` and `new --replace` are the one high act here
9
+ // (`risk.ts` "public-code.revoke"); the question names the code, and without a terminal it is
10
+ // `--yes` or nothing.
11
+ //
12
+ // โš  THE CEILINGS ARE THE SERVER'S. Three live codes, one for a Platform user, and a daily number
13
+ // that is smaller for an agent: this prints what the server answered and does not keep a copy.
14
+ import { toBase64Url } from "../bytes.js";
15
+ import { loadCrypto } from "../crypto.js";
16
+ import { NmtsError } from "../errors.js";
17
+ import { BINARY_NAME } from "../product.js";
18
+ import { promptLine, stdinIsATerminal } from "../prompt.js";
19
+ import { codeActivity } from "../public-code-activity.js";
20
+ import { codeNumber, differentCode, lastLiveCode, notLiveCode, publicCodeRefusal } from "../public-code-refusals.js";
21
+ import { defaultCode, liveCodes, nextCodeIndex, publishCode, readPublicCodes, revokeCode, } from "../public-codes.js";
22
+ import { openSession } from "../session.js";
23
+ import { shareKeyRing, shareKeysAt } from "../share-codes.js";
24
+ /** The code this key derives at `row`'s number, as a person reads it โ€” or `differentCode` when the server's differs. */
25
+ export function checkedDisplay(crypt, code, row) {
26
+ const keys = shareKeysAt(crypt, code, row.index);
27
+ try {
28
+ if (toBase64Url(keys.address) !== row.address)
29
+ throw differentCode();
30
+ return keys.display;
31
+ }
32
+ finally {
33
+ keys.wipe();
34
+ }
35
+ }
36
+ /** `checkedDisplay` for every row, from one ring โ€” the key's derivation runs once, not once per row. */
37
+ function checkedDisplays(ring, rows) {
38
+ return new Map(rows.map((row) => {
39
+ const keys = ring.at(row.index);
40
+ if (toBase64Url(keys.address) !== row.address)
41
+ throw differentCode();
42
+ return [row.index, keys.display];
43
+ }));
44
+ }
45
+ /** The UTC day of an instant, `YYYY-MM-DD`; the text as sent when it is not one. */
46
+ function day(instant) {
47
+ const at = new Date(instant);
48
+ return Number.isNaN(at.getTime()) ? instant : at.toISOString().slice(0, 10);
49
+ }
50
+ function rowLine(row, display, isDefault) {
51
+ const counts = `sent ${row.sent} ยท received ${row.received} ยท messages ${row.support}`;
52
+ if (row.revokedAt !== null)
53
+ return `#${row.index} ${display} revoked ${day(row.revokedAt)} ${counts}`;
54
+ return `#${row.index} ${display}${isDefault ? " default" : ""} made ${day(row.createdAt)} ${counts}`;
55
+ }
56
+ function activityLines(activity) {
57
+ const out = [];
58
+ for (const s of activity?.sent ?? []) {
59
+ const what = s.path ?? `${s.itemId} (no longer in the file list)`;
60
+ const gone = s.recipientRevoked ? " ยท the recipient has revoked this code" : "";
61
+ out.push(` sent ${what} to ${s.recipient} ${day(s.createdAt)} share ${s.id}${gone}`);
62
+ }
63
+ for (const r of activity?.received ?? []) {
64
+ if (r.problem !== null) {
65
+ out.push(` received share ${r.id} (will not open: ${r.problem})`);
66
+ continue;
67
+ }
68
+ const gone = r.senderRevoked ? " ยท the sender has revoked this code" : "";
69
+ out.push(` received ${r.name ?? ""} from ${r.sender ?? ""} ${day(r.createdAt)} share ${r.id}${gone}`);
70
+ }
71
+ return out;
72
+ }
73
+ function sayer(options) {
74
+ return options.write ?? ((line) => process.stdout.write(`${line}\n`));
75
+ }
76
+ /** `nmts public-code list [--activity]` โ€” every code, live ones first, and the two ceilings. */
77
+ export async function listCodes(options = {}) {
78
+ const say = sayer(options);
79
+ const session = await openSession({ server: options.server, network: options.network });
80
+ const crypt = await loadCrypto();
81
+ const door = { server: session.server, token: session.apiKey };
82
+ const list = await readPublicCodes(door.server, door.token);
83
+ const live = liveCodes(list);
84
+ const ordered = [...live, ...list.codes.filter((c) => c.revokedAt !== null)];
85
+ const ring = shareKeyRing(crypt, session.code);
86
+ let shown;
87
+ try {
88
+ shown = checkedDisplays(ring, ordered);
89
+ }
90
+ finally {
91
+ ring.wipe();
92
+ }
93
+ const def = defaultCode(list)?.index ?? null;
94
+ const activity = options.activity === true
95
+ ? await codeActivity(crypt, session.code, { ...door, accountId: session.accountId }, list)
96
+ : null;
97
+ if (options.json === true) {
98
+ const codes = ordered.map((r) => ({
99
+ index: r.index,
100
+ code: shown.get(r.index) ?? "",
101
+ raw: r.address,
102
+ default: r.index === def,
103
+ createdAt: r.createdAt,
104
+ revokedAt: r.revokedAt,
105
+ sent: r.sent,
106
+ received: r.received,
107
+ messages: r.support,
108
+ ...(activity === null ? {} : { activity: activity.get(r.index) ?? { sent: [], received: [] } }),
109
+ }));
110
+ say(JSON.stringify({ codes, live: live.length, liveMax: list.liveMax, madeToday: list.madeToday, dayCap: list.dayCap }));
111
+ return 0;
112
+ }
113
+ if (ordered.length === 0) {
114
+ say(`This account has no published public code yet.`);
115
+ say(``);
116
+ say(` ${BINARY_NAME} public-code --publish`);
117
+ return 0;
118
+ }
119
+ for (const r of ordered) {
120
+ say(rowLine(r, shown.get(r.index) ?? "", r.index === def));
121
+ if (activity !== null)
122
+ for (const line of activityLines(activity.get(r.index)))
123
+ say(line);
124
+ }
125
+ say(``);
126
+ say(`${live.length} of ${list.liveMax} live ยท ${list.madeToday} of ${list.dayCap} made today (UTC)`);
127
+ return 0;
128
+ }
129
+ /**
130
+ * The revoke question, put to a person โ€” or, with nobody to put it to, the refusal that carries it.
131
+ * True when the answer is yes.
132
+ */
133
+ async function confirmRevoke(index, display, options, say) {
134
+ if (options.yes === true)
135
+ return true;
136
+ const question = `Revoke public code #${index} (${display})? Nobody can send to it again, and nobody can bring it ` +
137
+ `back. Files already received with it stay.`;
138
+ if (options.readLine === undefined && !stdinIsATerminal()) {
139
+ throw new NmtsError(question, {
140
+ exitCode: 5,
141
+ nextStep: "This needs the person's yes. If they say yes, run the same command with --yes. Nothing was done.",
142
+ });
143
+ }
144
+ const answer = (await (options.readLine ?? promptLine)(`${question} [y/N] `)).trim();
145
+ if (answer === "y" || answer === "Y")
146
+ return true;
147
+ say(`Nothing was revoked.`);
148
+ return false;
149
+ }
150
+ /** `nmts public-code new [--replace <n>]` โ€” the next number, published; with --replace, that code revoked in the same request. */
151
+ export async function newCode(options = {}) {
152
+ const say = sayer(options);
153
+ const replace = options.replace === undefined ? undefined : codeNumber(options.replace, "--replace");
154
+ const session = await openSession({ server: options.server, network: options.network });
155
+ const crypt = await loadCrypto();
156
+ const door = { server: session.server, token: session.apiKey };
157
+ const list = await readPublicCodes(door.server, door.token);
158
+ let replaced = null;
159
+ if (replace !== undefined) {
160
+ const row = liveCodes(list).find((c) => c.index === replace);
161
+ if (row === undefined)
162
+ throw notLiveCode(replace);
163
+ replaced = checkedDisplay(crypt, session.code, row);
164
+ if (!(await confirmRevoke(replace, replaced, options, say)))
165
+ return 1;
166
+ }
167
+ let made;
168
+ try {
169
+ made = await publishCode(crypt, session.code, door, nextCodeIndex(list), replace);
170
+ }
171
+ catch (error) {
172
+ throw publicCodeRefusal(error, { liveMax: list.liveMax, dayCap: list.dayCap, replace: defaultCode(list)?.index });
173
+ }
174
+ if (options.json === true) {
175
+ say(JSON.stringify({ index: made.index, code: made.code, raw: made.address, revoked: replace ?? null }));
176
+ return 0;
177
+ }
178
+ say(`made public code #${made.index} ${made.code}`);
179
+ if (replace !== undefined)
180
+ say(`revoked #${replace} ${replaced ?? ""}`);
181
+ return 0;
182
+ }
183
+ /** `nmts public-code revoke <n>` โ€” one way, and never the last live code. */
184
+ export async function revokeCodeCommand(operand, options = {}) {
185
+ const say = sayer(options);
186
+ const index = codeNumber(operand, "`public-code revoke`");
187
+ const session = await openSession({ server: options.server, network: options.network });
188
+ const crypt = await loadCrypto();
189
+ const door = { server: session.server, token: session.apiKey };
190
+ const list = await readPublicCodes(door.server, door.token);
191
+ const row = list.codes.find((c) => c.index === index);
192
+ if (row === undefined) {
193
+ throw new NmtsError(`This account has no public code #${index}.`, {
194
+ exitCode: 4,
195
+ nextStep: `\`${BINARY_NAME} public-code list\` shows its codes.`,
196
+ });
197
+ }
198
+ const display = checkedDisplay(crypt, session.code, row);
199
+ if (row.revokedAt === null) {
200
+ if (liveCodes(list).length <= 1)
201
+ throw lastLiveCode(index);
202
+ if (!(await confirmRevoke(index, display, options, say)))
203
+ return 1;
204
+ try {
205
+ await revokeCode(door, index);
206
+ }
207
+ catch (error) {
208
+ throw publicCodeRefusal(error, { replace: index });
209
+ }
210
+ }
211
+ if (options.json === true) {
212
+ say(JSON.stringify({ index, code: display, raw: row.address, revoked: true }));
213
+ return 0;
214
+ }
215
+ say(row.revokedAt === null ? `revoked public code #${index} ${display}` : `public code #${index} was already revoked on ${day(row.revokedAt)}`);
216
+ return 0;
217
+ }
@@ -1,9 +1,20 @@
1
+ import type { ParsedArgs } from "../args.ts";
1
2
  export interface PublicCodeOptions {
2
3
  server?: string | undefined;
3
4
  network?: string | undefined;
4
- /** Publish it, so other accounts can send to it. Permanent. */
5
+ /** Publish it, so other accounts can send to it. */
5
6
  publish?: boolean;
7
+ /** `--save [file]`: write this account's public code file (NCF-3 ยง5.7) โ€” the default code's. */
8
+ save?: boolean;
9
+ /** `--save --as <n>`: the file for code number n instead, made on this machine alone. */
10
+ as?: string | undefined;
11
+ /** The file `--save` writes; by default `nmts-public-code-<code>.nmtscode` here. */
12
+ file?: string | undefined;
13
+ /** Replace that file if it exists. */
14
+ force?: boolean;
6
15
  json?: boolean;
7
16
  write?: (line: string) => void;
8
17
  }
18
+ /** `nmts public-code [list|new|revoke]` and `nmts public-code --save [file]`, from the parsed command line. */
19
+ export declare function runPublicCode(args: ParsedArgs): Promise<number>;
9
20
  export declare function publicCode(options?: PublicCodeOptions): Promise<number>;
@@ -7,83 +7,178 @@
7
7
  // pasting into the other had two ways to be wrong about one value. Both are fixed here: one
8
8
  // name, and the same grouped form a person sees on the screen.
9
9
  //
10
+ // โ›” ONE KEY, SEVERAL CODES. An account holds one to three live public codes,
11
+ // numbered from the one NMTS key; the bare command shows the DEFAULT โ€” the lowest-numbered live
12
+ // one โ€” and `list ยท new ยท revoke` (`public-code-manage.ts`) handle the rest. A published code
13
+ // never changes; what changes is which codes are live, and revoking one is one way.
14
+ //
10
15
  // โ›” WHY PUBLISHING IS A SEPARATE STEP AND NOT SOMETHING THIS COMMAND JUST DOES. Sending a file
11
16
  // already publishes the sender's code as a side effect, because a share cannot exist without
12
17
  // one and the person has already decided to hand something over. RECEIVING is the other way
13
- // round: nothing has been decided yet, and the record is permanent. So the plain command reads,
14
- // says whether it can be sent to, and names the flag; `--publish` is the deliberate act.
18
+ // round: nothing has been decided yet. So the plain command reads, says whether it can be sent
19
+ // to, and names the flag; `--publish` is the deliberate act.
15
20
  //
16
- // โ›” WHAT "PERMANENT" DOES AND DOES NOT MEAN HERE. The record cannot be withdrawn or replaced. It
17
- // is also not a choice: the code and the identity behind it are derived from the NMTS key,
18
- // so the same NMTS key produces the same bytes on any device, and the server refuses a
19
- // bundle whose claimed value is not the fingerprint of its own root. The only way to publish a
20
- // wrong one is to be holding a different NMTS key. That is worth saying plainly rather than
21
- // warning vaguely โ€” a warning that cannot be acted on just teaches people to click through.
21
+ // โ›” IT IS NOT A CHOICE. The code and the identity behind it are derived from the NMTS key, so the
22
+ // same key produces the same bytes at the same number on any device, and the server refuses a
23
+ // bundle whose claimed value is not the fingerprint of its own root.
22
24
  //
23
25
  // โš  IT IS NOT THE NMTS KEY. That one opens every file in the account and must never be given
24
26
  // to anybody; this one is meant to be given away, and on its own it opens nothing.
25
- import { request } from "../api.js";
26
- import { NmtsError } from "../errors.js";
27
- import { isRecord } from "../guards.js";
27
+ import { writeFileSync } from "node:fs";
28
+ import { resolve } from "node:path";
29
+ import { requireAccountCode } from "../code-access.js";
30
+ import { readCredentialsFile, resolveApiKey } from "../credentials.js";
31
+ import { publicCodeFileText } from "../handover.js";
32
+ import { checkedKeys } from "../handover-codes.js";
33
+ import { publicCodeFileName } from "../shared/lib/share/handover-format.js";
28
34
  import { loadCrypto } from "../crypto.js";
35
+ import { NmtsError } from "../errors.js";
29
36
  import { BINARY_NAME } from "../product.js";
37
+ import { codeNumber, publicCodeRefusal } from "../public-code-refusals.js";
38
+ import { defaultCode, liveCodes, nextCodeIndex, publishCode, readPublicCodes } from "../public-codes.js";
39
+ import { resolveServer } from "../server.js";
30
40
  import { openSession } from "../session.js";
31
- import { shareKeysOf } from "../share.js";
32
- function b64(bytes) {
33
- return Buffer.from(bytes).toString("base64url");
41
+ import { shareKeysAt } from "../share-codes.js";
42
+ import { checkedDisplay } from "./public-code-manage.js";
43
+ /** `nmts public-code [list|new|revoke]` and `nmts public-code --save [file]`, from the parsed command line. */
44
+ export async function runPublicCode(args) {
45
+ const sub = args.operands[0] ?? "";
46
+ const common = { server: args.server, network: args.network, json: args.json };
47
+ // โš  WITH --save THE OPERAND IS THE FILE, not a verb โ€” the same reading `risk.ts` gives it.
48
+ if (sub === "" || args.save) {
49
+ return await publicCode({ ...common, publish: args.publish, save: args.save, as: args.as, file: args.operands[0], force: args.force });
50
+ }
51
+ const manage = await import("./public-code-manage.js");
52
+ switch (sub) {
53
+ case "list":
54
+ return await manage.listCodes({ ...common, activity: args.activity });
55
+ case "new":
56
+ return await manage.newCode({ ...common, replace: args.replace, yes: args.yes });
57
+ case "revoke":
58
+ return await manage.revokeCodeCommand(args.operands[1], { ...common, yes: args.yes });
59
+ default:
60
+ throw new NmtsError(`"${sub}" is not a public-code command.`, {
61
+ exitCode: 2,
62
+ nextStep: `\`${BINARY_NAME} public-code\`, \`public-code list\`, \`public-code new\` or \`public-code revoke <number>\`.`,
63
+ });
64
+ }
34
65
  }
35
66
  export async function publicCode(options = {}) {
36
67
  const say = options.write ?? ((line) => process.stdout.write(`${line}\n`));
68
+ if (options.save === true) {
69
+ if (options.publish === true) {
70
+ throw new NmtsError("--save and --publish are two different things; run them one at a time.", { exitCode: 2 });
71
+ }
72
+ return await savePublicCodeFile(options, say);
73
+ }
37
74
  const session = await openSession({ server: options.server, network: options.network });
38
75
  const crypt = await loadCrypto();
39
- const keys = shareKeysOf(crypt, session.code);
40
- const mine = b64(keys.address);
41
- const shown = keys.display;
42
- const seen = await request(session.server, "/v1/account/share-identity", {
43
- token: session.apiKey,
44
- });
45
- let published = isRecord(seen) && seen["published"] === true;
46
- // โ›” IF THE SERVER ALREADY HOLDS A DIFFERENT ONE, STOP. Publishing is first-writer-wins and the
47
- // server would refuse the write anyway, but the useful thing to report is not "the write
48
- // failed" โ€” it is that the NMTS key this machine is holding is not the one this account
49
- // was made with, which is a much bigger fact than a failed request.
50
- const held = isRecord(seen) ? seen["share_address"] : null;
51
- if (typeof held === "string" && held !== mine) {
52
- throw new NmtsError("This account already publishes a different public code.", {
53
- exitCode: 4,
54
- nextStep: "The public code is derived from the NMTS key, so a different one means this machine " +
55
- "is holding a different account's code than the key beside it. Check which account you meant.",
56
- });
76
+ const door = { server: session.server, token: session.apiKey };
77
+ const list = await readPublicCodes(door.server, door.token);
78
+ const held = defaultCode(list);
79
+ let shown;
80
+ let live = liveCodes(list).length;
81
+ if (held !== null) {
82
+ // โ›” IF THE SERVER HOLDS A CODE THIS KEY DOES NOT MAKE AT THAT NUMBER, STOP (`differentCode`).
83
+ shown = { index: held.index, code: checkedDisplay(crypt, session.code, held), raw: held.address };
57
84
  }
58
- if (options.publish === true && !published) {
59
- await request(session.server, "/v1/account/share-identity", {
60
- token: session.apiKey,
61
- method: "PUT",
62
- body: { share_public_key: b64(keys.identity), share_address: mine },
63
- });
64
- published = true;
85
+ else if (options.publish === true) {
86
+ try {
87
+ const made = await publishCode(crypt, session.code, door, nextCodeIndex(list));
88
+ shown = { index: made.index, code: made.code, raw: made.address };
89
+ live = 1;
90
+ }
91
+ catch (error) {
92
+ throw publicCodeRefusal(error, { liveMax: list.liveMax, dayCap: list.dayCap });
93
+ }
65
94
  }
95
+ else {
96
+ const keys = shareKeysAt(crypt, session.code, nextCodeIndex(list));
97
+ shown = { index: keys.index, code: keys.display, raw: Buffer.from(keys.address).toString("base64url") };
98
+ keys.wipe();
99
+ }
100
+ const published = live > 0;
66
101
  if (options.json === true) {
67
102
  // โš  BOTH FORMS. `code` is what a person reads and types; `raw` is what the wire carries.
68
103
  // A reader that has only one of them ends up converting, and that is a second place to be wrong.
69
- say(JSON.stringify({ code: shown, raw: mine, published }));
104
+ say(JSON.stringify({ code: shown.code, raw: shown.raw, published, index: shown.index, live }));
70
105
  return 0;
71
106
  }
72
- say(`public code ${shown}`);
73
107
  if (published) {
74
- say(` published โ€” another account can send files to it`);
108
+ say(`public code ${shown.code} (default ยท ${live} of ${list.liveMax} live)`);
75
109
  say(``);
76
110
  say(`Give it to whoever is sending. โ›” It is NOT your NMTS key โ€” that one opens`);
77
111
  say(`every file you have and is never given to anybody. This one opens nothing.`);
78
112
  return 0;
79
113
  }
114
+ say(`public code ${shown.code}`);
80
115
  say(` NOT published โ€” nobody can send to it yet`);
81
116
  say(``);
82
117
  say(`Publishing writes it on the server so a sender can find the key to seal to.`);
83
- say(`It is permanent: it cannot be withdrawn or changed afterwards. It is also not a`);
84
- say(`choice โ€” it comes from your NMTS key, so the same NMTS key always gives`);
85
- say(`the same public code, on this machine or any other.`);
118
+ say(`A published code cannot be changed. You can revoke it and make a new one with`);
119
+ say(` ${BINARY_NAME} public-code new`);
120
+ say(`The code comes from your NMTS key, so the same key always gives the same code.`);
86
121
  say(``);
87
122
  say(` ${BINARY_NAME} public-code --publish`);
88
123
  return 0;
89
124
  }
125
+ /**
126
+ * `nmts public-code --save [file]` โ€” this account's public code file (NCF-3 ยง5.7).
127
+ *
128
+ * โ›” NOTHING IN IT IS SECRET: the identity is derived from the NMTS key on this machine, exactly the
129
+ * bytes `--publish` would put on the server. Whoever holds the file can seal a handover to this
130
+ * account without looking the code up โ€” which is the point: that lookup is the one thing that
131
+ * tells NMTS who is sending to whom.
132
+ *
133
+ * โš  WHICH CODE: `--as <n>` names it and nothing is asked of the server. Without it, the default โ€”
134
+ * read off the account's list when a key on this machine can read it, and code 0, the code every
135
+ * key starts with, when none can.
136
+ */
137
+ async function savePublicCodeFile(options, say) {
138
+ const { code } = await requireAccountCode();
139
+ const crypt = await loadCrypto();
140
+ const keys = await savedCodeKeys(crypt, code, options);
141
+ const force = options.force === true;
142
+ const file = options.file;
143
+ const out = resolve(file === undefined || file === "" ? publicCodeFileName(keys.display) : file);
144
+ try {
145
+ writeFileSync(out, publicCodeFileText(keys), { flag: force ? "w" : "wx" });
146
+ }
147
+ catch {
148
+ throw new NmtsError(`Could not write ${out}.`, {
149
+ exitCode: 4,
150
+ nextStep: "If it already exists, name another file or replace it with --force.",
151
+ });
152
+ }
153
+ finally {
154
+ keys.wipe();
155
+ }
156
+ if (options.json === true) {
157
+ say(JSON.stringify({ code: keys.display, index: keys.index, out }));
158
+ return 0;
159
+ }
160
+ say(`public code file ${out}`);
161
+ say(` for public code #${keys.index} ${keys.display}`);
162
+ say(``);
163
+ say(`Whoever has this file can hand files over to you without asking NMTS.`);
164
+ return 0;
165
+ }
166
+ /** The keys the file is written from: `--as`, else the default code, else code 0. */
167
+ async function savedCodeKeys(crypt, code, options) {
168
+ if (options.as !== undefined)
169
+ return shareKeysAt(crypt, code, codeNumber(options.as, "--as"));
170
+ const key = resolveApiKey();
171
+ if (key === null)
172
+ return shareKeysAt(crypt, code, 0);
173
+ let list;
174
+ try {
175
+ list = await readPublicCodes(resolveServer(options.server ?? readCredentialsFile()?.server), key.key);
176
+ }
177
+ catch {
178
+ throw new NmtsError("Could not read which public code is your default.", {
179
+ exitCode: 4,
180
+ nextStep: "Nothing was written. Pass --as <number> to write the file for that code without asking the server.",
181
+ });
182
+ }
183
+ return checkedKeys(crypt, code, list, defaultCode(list)?.index ?? 0);
184
+ }
@@ -0,0 +1,6 @@
1
+ import { type PushOptions } from "./push.ts";
2
+ /** Cut the files into runs of at most one order's parts, in order. */
3
+ export declare function batchesOf<T extends {
4
+ size: number;
5
+ }>(files: readonly T[], partsOf: (one: T) => number): T[][];
6
+ export declare function pushHeavy(target: string, options: PushOptions): Promise<number>;
@@ -0,0 +1,128 @@
1
+ // `nmts push <directory> --tier heavy` โ€” a whole directory into NMTS Heavy, keeping its shape.
2
+ //
3
+ // โ›” ONE ORDER FOR THE DIRECTORY, not one per file: fifty small files are one order and โ€” when the
4
+ // wallet pays โ€” one signature. An order holds at most 256 parts, so a larger tree is sent as
5
+ // several orders one after another, each committed before the next begins.
6
+ //
7
+ // โ›” WHAT IS ALREADY THERE IS SKIPPED, as on Standard, which is what makes running it again safe.
8
+ // And it stops at the first failure, saying what is already uploaded โ€” the rule `push.ts` gives.
9
+ import { parseAsked } from "../collision.js";
10
+ import { loadCrypto } from "../crypto.js";
11
+ import { NmtsError } from "../errors.js";
12
+ import { planHeavyFile } from "../heavy-upload.js";
13
+ import { paddingRuleOf, readFileList } from "../manifest.js";
14
+ import { resolveNetwork } from "../network.js";
15
+ import { Progress, silentSink } from "../progress.js";
16
+ import { stderrSink } from "../progress-node.js";
17
+ import { openSession } from "../session.js";
18
+ import { HEAVY_MAX_SLOTS } from "../shared/lib/heavy/order-runner.js";
19
+ import { fileSource } from "../upload-file-node.js";
20
+ import { heavyDryRun, heavyPayerOf, recordHeavy, refuseHeavyClashes, runHeavy } from "./heavy-run.js";
21
+ import { payerAddressOf, sayDryRun } from "./put-heavy.js";
22
+ import { folderFor } from "./push.js";
23
+ import { localTree, splitAlready } from "./push-tree.js";
24
+ /** Cut the files into runs of at most one order's parts, in order. */
25
+ export function batchesOf(files, partsOf) {
26
+ const out = [];
27
+ let current = [];
28
+ let slots = 0;
29
+ for (const one of files) {
30
+ const parts = partsOf(one);
31
+ if (current.length > 0 && slots + parts > HEAVY_MAX_SLOTS) {
32
+ out.push(current);
33
+ current = [];
34
+ slots = 0;
35
+ }
36
+ current.push(one);
37
+ slots += parts;
38
+ }
39
+ if (current.length > 0)
40
+ out.push(current);
41
+ return out;
42
+ }
43
+ export async function pushHeavy(target, options) {
44
+ const say = options.write ?? ((line) => process.stdout.write(`${line}\n`));
45
+ const payer = heavyPayerOf(options.pay);
46
+ refuseHeavyClashes(options, payer);
47
+ const { root, found } = localTree(target, options.to, options.hidden === true);
48
+ if (found.length === 0) {
49
+ if (options.json) {
50
+ say(JSON.stringify({ files: 0, uploaded: 0, skipped: 0 }));
51
+ return 0;
52
+ }
53
+ say(`${root} holds no files to send.`);
54
+ return 0;
55
+ }
56
+ const session = await openSession({ server: options.server, network: options.network });
57
+ const network = resolveNetwork(session.server, session.network);
58
+ const asked = parseAsked(options.onCollision);
59
+ const list = await readFileList(session.server, session.apiKey, session.code, session.accountId);
60
+ const rule = paddingRuleOf(list.manifest?.settings);
61
+ const { folderIds, already, todo } = splitAlready(list.manifest?.entries ?? [], found);
62
+ const heavyFile = (one, parentId) => ({
63
+ source: fileSource(one.local, one.size),
64
+ name: one.name,
65
+ parentId,
66
+ destination: one.folder,
67
+ });
68
+ if (options.dryRun === true) {
69
+ const facts = {
70
+ files: found.length,
71
+ toSend: todo.length,
72
+ skipped: already.length,
73
+ ...(todo.length === 0 ? {} : heavyDryRun(todo.map((one) => heavyFile(one, null)), rule, payer)),
74
+ ...(await payerAddressOf(session, payer, options, list.manifest?.settings)),
75
+ };
76
+ if (options.json)
77
+ say(JSON.stringify(facts));
78
+ else {
79
+ sayDryRun(say, `${todo.length} file${todo.length === 1 ? "" : "s"}`, facts);
80
+ if (already.length > 0)
81
+ say(` ${already.length} already in the drive, which this would not send again`);
82
+ }
83
+ return 0;
84
+ }
85
+ const json = options.json === true;
86
+ const progress = new Progress(json ? silentSink() : stderrSink(), "uploading");
87
+ const crypt = await loadCrypto();
88
+ const uploaded = [];
89
+ const reports = [];
90
+ try {
91
+ for (const batch of batchesOf(todo, (one) => planHeavyFile(one.size, rule).length)) {
92
+ const files = [];
93
+ for (const one of batch)
94
+ files.push(heavyFile(one, await folderFor(session, folderIds, one.folder)));
95
+ const ctx = { session, network, crypt, rule, settings: list.manifest?.settings, say, json, progress };
96
+ const report = await runHeavy(ctx, files, payer, options);
97
+ reports.push(report.facts);
98
+ for (const done of report.files) {
99
+ const added = await recordHeavy(session, done, asked);
100
+ uploaded.push(`${done.destination}/${added.name}`);
101
+ }
102
+ }
103
+ }
104
+ catch (error) {
105
+ progress.done();
106
+ // โ›” WHAT IS UPLOADED IS REAL AND PAID FOR โ€” the same words `push.ts` uses, for the same reason.
107
+ const because = error instanceof Error ? error.message : String(error);
108
+ throw new NmtsError(because, {
109
+ exitCode: error instanceof NmtsError ? error.exitCode : 1,
110
+ nextStep: uploaded.length === 0
111
+ ? error instanceof NmtsError && error.nextStep !== null
112
+ ? error.nextStep
113
+ : "Nothing was uploaded."
114
+ : `${uploaded.length} file${uploaded.length === 1 ? " is" : "s are"} uploaded and paid ` +
115
+ `for. Running the same command again sends only what is missing.`,
116
+ });
117
+ }
118
+ finally {
119
+ progress.done();
120
+ }
121
+ if (json) {
122
+ say(JSON.stringify({ files: found.length, uploaded: uploaded.length, skipped: already.length, tier: "heavy", orders: reports }));
123
+ return 0;
124
+ }
125
+ say(``);
126
+ say(`${uploaded.length} sent ยท ${already.length} already there`);
127
+ return 0;
128
+ }
@@ -0,0 +1,24 @@
1
+ import type { ManifestEntry } from "../shared/lib/drive/manifest-codec.ts";
2
+ /** One local file, and where it goes in the drive. */
3
+ export interface PlannedFile {
4
+ /** Absolute path on this machine. */
5
+ local: string;
6
+ /** Folder path inside the drive. */
7
+ folder: string;
8
+ name: string;
9
+ size: number;
10
+ }
11
+ /** The directory as typed: refused when it is missing or a file, then walked. */
12
+ export declare function localTree(target: string, to: string | undefined, hidden: boolean): {
13
+ root: string;
14
+ found: PlannedFile[];
15
+ };
16
+ /** Which found files the drive already holds, which to send, and the folders already known. */
17
+ export declare function splitAlready(entries: readonly ManifestEntry[], found: readonly PlannedFile[]): {
18
+ folderIds: Map<string, string | null>;
19
+ already: PlannedFile[];
20
+ todo: PlannedFile[];
21
+ };
22
+ export { walk as filesUnderDirectory };
23
+ /** Every file under a local directory, with the drive folder each one belongs in. */
24
+ declare function walk(dir: string, driveFolder: string, hidden: boolean): PlannedFile[];