@needmoretruth/nmts-cli 0.44.0 → 0.46.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/AGENTS.md +2 -2
- package/CHANGELOG.md +73 -0
- package/README.ko.md +6 -3
- package/README.md +15 -3
- package/dist/api-advice.js +17 -2
- package/dist/api.d.ts +22 -0
- package/dist/api.js +49 -1
- package/dist/arg-options.d.ts +14 -0
- package/dist/args.js +11 -2
- package/dist/artifact-about.d.ts +1 -1
- package/dist/collision.d.ts +20 -3
- package/dist/collision.js +21 -3
- package/dist/commands/erase.js +1 -0
- package/dist/commands/get.d.ts +2 -0
- package/dist/commands/get.js +13 -1
- package/dist/commands/handover.d.ts +13 -0
- package/dist/commands/handover.js +328 -0
- package/dist/commands/heavy-run.d.ts +59 -0
- package/dist/commands/heavy-run.js +247 -0
- package/dist/commands/heavy.d.ts +7 -0
- package/dist/commands/heavy.js +123 -0
- package/dist/commands/ls.d.ts +7 -0
- package/dist/commands/ls.js +27 -6
- package/dist/commands/public-code.d.ts +6 -0
- package/dist/commands/public-code.js +46 -0
- package/dist/commands/push-heavy.d.ts +6 -0
- package/dist/commands/push-heavy.js +128 -0
- package/dist/commands/push-tree.d.ts +24 -0
- package/dist/commands/push-tree.js +91 -0
- package/dist/commands/push.d.ts +4 -13
- package/dist/commands/push.js +7 -77
- package/dist/commands/put-heavy.d.ts +9 -0
- package/dist/commands/put-heavy.js +120 -0
- package/dist/commands/put-payer.d.ts +21 -0
- package/dist/commands/put-payer.js +24 -0
- package/dist/commands/put-thumbnail.d.ts +4 -0
- package/dist/commands/put-thumbnail.js +119 -0
- package/dist/commands/put-wallet.d.ts +2 -0
- package/dist/commands/put-wallet.js +3 -2
- package/dist/commands/put.d.ts +10 -2
- package/dist/commands/put.js +13 -7
- package/dist/commands/receive.js +3 -3
- package/dist/commands/s3.js +44 -10
- package/dist/crypto-surface.d.ts +8 -0
- package/dist/crypto-surface.js +3 -0
- package/dist/crypto.d.ts +3 -0
- package/dist/crypto.js +3 -0
- package/dist/download-part.d.ts +32 -1
- package/dist/download-part.js +95 -6
- package/dist/download-range.d.ts +19 -0
- package/dist/download-range.js +108 -0
- package/dist/download-sink.d.ts +15 -0
- package/dist/download.d.ts +14 -0
- package/dist/download.js +19 -4
- package/dist/drive-edit/trash.js +5 -3
- package/dist/drive-edit/tree.d.ts +6 -0
- package/dist/drive-edit/tree.js +10 -0
- package/dist/drive-erase.js +2 -2
- package/dist/drive-paths.d.ts +2 -2
- package/dist/drive-paths.js +2 -2
- package/dist/handover.d.ts +44 -0
- package/dist/handover.js +199 -0
- package/dist/heavy-api.d.ts +36 -0
- package/dist/heavy-api.js +107 -0
- package/dist/heavy-copy.d.ts +72 -0
- package/dist/heavy-copy.js +108 -0
- package/dist/heavy-evm.d.ts +20 -0
- package/dist/heavy-evm.js +106 -0
- package/dist/heavy-self-pay.d.ts +92 -0
- package/dist/heavy-self-pay.js +167 -0
- package/dist/heavy-upload.d.ts +87 -0
- package/dist/heavy-upload.js +154 -0
- package/dist/heavy-wallet-pay.d.ts +39 -0
- package/dist/heavy-wallet-pay.js +73 -0
- package/dist/help-options.js +8 -2
- package/dist/help.js +16 -3
- package/dist/list-tier.d.ts +7 -0
- package/dist/list-tier.js +47 -0
- package/dist/main.js +14 -9
- package/dist/manifest-write.d.ts +4 -3
- package/dist/mcp-tools/files.js +21 -2
- package/dist/mcp-tools/reads.js +9 -1
- package/dist/platform-sign.d.ts +4 -4
- package/dist/platform-sign.js +3 -3
- package/dist/portable.d.ts +17 -1
- package/dist/portable.js +13 -1
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/recovery-build.js +16 -0
- package/dist/recovery-map-part.d.ts +82 -0
- package/dist/recovery-map-part.js +83 -0
- package/dist/recovery-map.d.ts +5 -40
- package/dist/recovery-map.js +18 -1
- package/dist/recovery-source.d.ts +3 -0
- package/dist/recovery-source.js +9 -0
- package/dist/risk.d.ts +30 -0
- package/dist/risk.js +23 -2
- package/dist/run-log.d.ts +1 -1
- package/dist/run-log.js +7 -1
- package/dist/s3/address.d.ts +20 -0
- package/dist/s3/address.js +97 -0
- package/dist/s3/answer.d.ts +48 -0
- package/dist/s3/answer.js +330 -0
- package/dist/s3/body-deframe.d.ts +49 -0
- package/dist/s3/body-deframe.js +264 -0
- package/dist/s3/body-measure.d.ts +13 -0
- package/dist/s3/body-measure.js +48 -0
- package/dist/s3/body-plan.d.ts +31 -0
- package/dist/s3/body-plan.js +159 -0
- package/dist/s3/body.d.ts +46 -0
- package/dist/s3/body.js +117 -0
- package/dist/s3/call.d.ts +36 -0
- package/dist/s3/call.js +42 -0
- package/dist/s3/checksum.d.ts +26 -0
- package/dist/s3/checksum.js +176 -0
- package/dist/s3/content-type.d.ts +3 -0
- package/dist/s3/content-type.js +49 -0
- package/dist/s3/contract.d.ts +91 -14
- package/dist/s3/drive-conditions.d.ts +47 -0
- package/dist/s3/drive-conditions.js +134 -0
- package/dist/s3/drive-key.d.ts +11 -0
- package/dist/s3/drive-key.js +58 -0
- package/dist/s3/drive-limits.d.ts +33 -0
- package/dist/s3/drive-limits.js +53 -0
- package/dist/s3/drive-lock.d.ts +11 -0
- package/dist/s3/drive-lock.js +47 -0
- package/dist/s3/drive-trashed-name.d.ts +13 -0
- package/dist/s3/drive-trashed-name.js +59 -0
- package/dist/s3/drive-write.d.ts +38 -0
- package/dist/s3/drive-write.js +258 -0
- package/dist/s3/drive.d.ts +59 -25
- package/dist/s3/drive.js +46 -77
- package/dist/s3/listing.d.ts +27 -1
- package/dist/s3/listing.js +62 -20
- package/dist/s3/long-answer.d.ts +12 -0
- package/dist/s3/long-answer.js +104 -0
- package/dist/s3/multipart-assemble.d.ts +17 -0
- package/dist/s3/multipart-assemble.js +68 -0
- package/dist/s3/multipart-sweep.d.ts +15 -0
- package/dist/s3/multipart-sweep.js +87 -0
- package/dist/s3/multipart-view.d.ts +45 -0
- package/dist/s3/multipart-view.js +232 -0
- package/dist/s3/multipart.d.ts +26 -19
- package/dist/s3/multipart.js +193 -78
- package/dist/s3/object-read.d.ts +17 -0
- package/dist/s3/object-read.js +127 -0
- package/dist/s3/object-write.d.ts +35 -0
- package/dist/s3/object-write.js +198 -0
- package/dist/s3/range.d.ts +14 -0
- package/dist/s3/range.js +80 -0
- package/dist/s3/response-sink.d.ts +12 -2
- package/dist/s3/response-sink.js +71 -7
- package/dist/s3/routes.d.ts +0 -1
- package/dist/s3/routes.js +242 -217
- package/dist/s3/same-file.d.ts +4 -1
- package/dist/s3/same-file.js +12 -3
- package/dist/s3/server.d.ts +54 -4
- package/dist/s3/server.js +174 -14
- package/dist/s3/sigv4-canonical.d.ts +91 -0
- package/dist/s3/sigv4-canonical.js +148 -0
- package/dist/s3/sigv4-presigned.d.ts +13 -0
- package/dist/s3/sigv4-presigned.js +82 -0
- package/dist/s3/sigv4.d.ts +26 -27
- package/dist/s3/sigv4.js +137 -102
- package/dist/s3/spool.d.ts +37 -0
- package/dist/s3/spool.js +104 -0
- package/dist/s3/staging.d.ts +90 -7
- package/dist/s3/staging.js +97 -66
- package/dist/s3/xml-multipart.d.ts +38 -0
- package/dist/s3/xml-multipart.js +47 -0
- package/dist/s3/xml-read.d.ts +28 -0
- package/dist/s3/xml-read.js +235 -0
- package/dist/s3/xml.d.ts +45 -2
- package/dist/s3/xml.js +72 -13
- package/dist/s3-gateway.d.ts +11 -5
- package/dist/s3-gateway.js +5 -2
- package/dist/seal.d.ts +1 -1
- package/dist/share.d.ts +5 -0
- package/dist/share.js +1 -1
- package/dist/shared/lib/api/types-heavy.d.ts +167 -0
- package/dist/shared/lib/api/types-heavy.js +60 -0
- package/dist/shared/lib/drive/manifest-codec.d.ts +11 -31
- package/dist/shared/lib/drive/manifest-codec.js +15 -23
- package/dist/shared/lib/drive/manifest-index.d.ts +10 -1
- package/dist/shared/lib/drive/manifest-index.js +30 -8
- package/dist/shared/lib/drive/manifest-wire-extras.d.ts +50 -0
- package/dist/shared/lib/drive/manifest-wire-extras.js +65 -0
- package/dist/shared/lib/drive/preview-classify.d.ts +17 -0
- package/dist/shared/lib/drive/preview-classify.js +96 -0
- package/dist/shared/lib/drive/rebuild-verify.js +1 -0
- package/dist/shared/lib/filecoin/providers.d.ts +25 -0
- package/dist/shared/lib/filecoin/providers.js +58 -0
- package/dist/shared/lib/heavy/download.d.ts +48 -0
- package/dist/shared/lib/heavy/download.js +116 -0
- package/dist/shared/lib/heavy/order-plan.d.ts +77 -0
- package/dist/shared/lib/heavy/order-plan.js +83 -0
- package/dist/shared/lib/heavy/order-runner.d.ts +9 -0
- package/dist/shared/lib/heavy/order-runner.js +300 -0
- package/dist/shared/lib/heavy/order-types.d.ts +90 -0
- package/dist/shared/lib/heavy/order-types.js +6 -0
- package/dist/shared/lib/heavy/order-wire.d.ts +55 -0
- package/dist/shared/lib/heavy/order-wire.js +113 -0
- package/dist/shared/lib/heavy/piece-cid.d.ts +24 -0
- package/dist/shared/lib/heavy/piece-cid.js +55 -0
- package/dist/shared/lib/heavy/sp-upload.d.ts +37 -0
- package/dist/shared/lib/heavy/sp-upload.js +112 -0
- package/dist/shared/lib/heavy/synapse-piece.d.ts +1 -0
- package/dist/shared/lib/heavy/synapse-piece.js +8 -0
- package/dist/shared/lib/share/handover-format.d.ts +129 -0
- package/dist/shared/lib/share/handover-format.js +323 -0
- package/dist/shared/lib/share/shared-file-info.js +1 -0
- package/dist/shared/lib/storage-network.d.ts +4 -4
- package/dist/shared/lib/storage-network.js +7 -7
- package/dist/shared/lib/storage-tier.d.ts +28 -0
- package/dist/shared/lib/storage-tier.js +63 -0
- package/dist/shared/lib/wallet/send-rules.js +1 -0
- package/dist/upload-file.d.ts +8 -0
- package/dist/upload-file.js +57 -25
- package/dist/upload-price-node.d.ts +3 -0
- package/dist/upload-price-node.js +3 -6
- package/dist/upload-price.d.ts +10 -0
- package/dist/upload-price.js +13 -1
- package/dist/upload-run.d.ts +80 -0
- package/dist/upload-run.js +173 -0
- package/dist/upload-steps.js +15 -4
- package/dist/upload-store.d.ts +15 -6
- package/dist/upload-store.js +6 -0
- package/dist/upload-wallet-put.d.ts +11 -4
- package/dist/upload-wallet-put.js +17 -6
- package/dist/upload-wallet.js +4 -0
- package/dist/upload-wire.d.ts +25 -0
- package/dist/upload-wire.js +20 -1
- package/dist/upload.js +28 -13
- package/dist/wallet-sign.js +2 -2
- package/docs/commands/get.md +3 -0
- package/docs/commands/handover.md +38 -0
- package/docs/commands/heavy.md +35 -0
- package/docs/commands/ls.md +5 -0
- package/docs/commands/public-code.md +7 -0
- package/docs/commands/push.md +4 -1
- package/docs/commands/put.md +23 -1
- package/docs/commands/share.md +3 -1
- package/package.json +5 -2
- package/server.json +2 -2
- package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +17 -1
- package/vendor/nmts-crypto/nmts_crypto_wasm.js +28 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +2 -1
|
@@ -0,0 +1,328 @@
|
|
|
1
|
+
// `nmts handover make` and `nmts handover open` — handing one file to one account outside NMTS.
|
|
2
|
+
//
|
|
3
|
+
// ⛔ MAKE records nothing on the server. It reads this account's own file list and the stored
|
|
4
|
+
// pieces of the file, and — only when the recipient is given as a public code rather than a
|
|
5
|
+
// public code file — asks the server for the recipient's published identity, exactly as `share`
|
|
6
|
+
// does. No share row is written: the handover is a file on this disk, passed on by the person.
|
|
7
|
+
//
|
|
8
|
+
// ⛔ OPEN asks the NMTS server nothing, and needs no API key: the file carries everything, the
|
|
9
|
+
// NMTS key opens it, and the pieces come from Walrus aggregators.
|
|
10
|
+
//
|
|
11
|
+
// ⛔ A HANDOVER CANNOT BE TAKEN BACK. There is no row to delete. The recipient can download the file
|
|
12
|
+
// until its storage ends or its stored bytes are destroyed; removing it from the drive (`rm`,
|
|
13
|
+
// `erase`) does neither. The review printed before `--yes` says so.
|
|
14
|
+
//
|
|
15
|
+
// ⚠ `--out` MEANS THE SAME FILE-PATH THING EVERYWHERE: `make` writes the handover file there, `open`
|
|
16
|
+
// writes the received file there, as `receive --out` does. Without it, `make` writes
|
|
17
|
+
// `nmts-handover-<date>.nmtshandover` and `open` writes the sender's name — its last segment only —
|
|
18
|
+
// in the current directory.
|
|
19
|
+
import { constants, accessSync, existsSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
20
|
+
import { dirname, resolve } from "node:path";
|
|
21
|
+
import { request } from "../api.js";
|
|
22
|
+
import { requireAccountCode } from "../code-access.js";
|
|
23
|
+
import { requireConsent } from "../consent.js";
|
|
24
|
+
import { AAD, DERIVED, loadCrypto } from "../crypto.js";
|
|
25
|
+
import { fetchKnownParts } from "../download.js";
|
|
26
|
+
import { fileSink } from "../download-sink-node.js";
|
|
27
|
+
import { buildIndex, entryAt, fullPathOf, KIND_FILE, normalisePath } from "../drive-paths.js";
|
|
28
|
+
import { NmtsError } from "../errors.js";
|
|
29
|
+
import { isRecord } from "../guards.js";
|
|
30
|
+
import { checkHandoverName, isHandoverNetwork, makeHandoverText, openHandoverText, readPublicCodeFileText } from "../handover.js";
|
|
31
|
+
import { readFileList } from "../manifest.js";
|
|
32
|
+
import { resolveNetwork } from "../network.js";
|
|
33
|
+
import { BINARY_NAME } from "../product.js";
|
|
34
|
+
import { destinationFor } from "../safe-path.js";
|
|
35
|
+
import { resolveServer } from "../server.js";
|
|
36
|
+
import { readCredentialsFile } from "../credentials.js";
|
|
37
|
+
import { openSession } from "../session.js";
|
|
38
|
+
import { addressFromTyped, identityMatches, shareKeysOf } from "../share.js";
|
|
39
|
+
import { handoverFileName, MAX_HANDOVER_FILE_BYTES, MAX_PUBLIC_CODE_FILE_BYTES, } from "../shared/lib/share/handover-format.js";
|
|
40
|
+
import { NETWORK_WALRUS } from "../shared/lib/storage-network.js";
|
|
41
|
+
const b64 = (bytes) => Buffer.from(bytes).toString("base64url");
|
|
42
|
+
export async function handover(sub, operand, options = {}) {
|
|
43
|
+
if (sub === "make")
|
|
44
|
+
return await make(operand, options);
|
|
45
|
+
if (sub === "open")
|
|
46
|
+
return await open(operand, options);
|
|
47
|
+
throw new NmtsError("Say `make` or `open`.", {
|
|
48
|
+
exitCode: 2,
|
|
49
|
+
nextStep: `\`${BINARY_NAME} handover make <path> --to <public code | public code file>\` writes a handover file; ` +
|
|
50
|
+
`\`${BINARY_NAME} handover open <file>\` saves the file it carries.`,
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
function networkOf(value) {
|
|
54
|
+
if (!isHandoverNetwork(value))
|
|
55
|
+
throw new NmtsError(`Handover files exist on mainnet and testnet, not ${value}.`, { exitCode: 2 });
|
|
56
|
+
return value;
|
|
57
|
+
}
|
|
58
|
+
/** Read a small file someone passed on: refused above `max` bytes, and every failure said plainly. */
|
|
59
|
+
function readPassedFile(path, max, what) {
|
|
60
|
+
let size;
|
|
61
|
+
try {
|
|
62
|
+
const stat = statSync(path);
|
|
63
|
+
if (!stat.isFile())
|
|
64
|
+
throw new Error("not a file");
|
|
65
|
+
size = stat.size;
|
|
66
|
+
}
|
|
67
|
+
catch {
|
|
68
|
+
throw new NmtsError(`Could not read ${path} as a ${what}.`, { exitCode: 2, nextStep: "Nothing was written." });
|
|
69
|
+
}
|
|
70
|
+
if (size > max) {
|
|
71
|
+
throw new NmtsError(`${path} is too large to be a ${what}.`, { exitCode: 2, nextStep: "Nothing was written." });
|
|
72
|
+
}
|
|
73
|
+
try {
|
|
74
|
+
return readFileSync(path, "utf8");
|
|
75
|
+
}
|
|
76
|
+
catch {
|
|
77
|
+
throw new NmtsError(`Could not read ${path}.`, { exitCode: 4, nextStep: "Nothing was written." });
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
/** Refuse an output path that cannot be written, before anything is asked of anybody. */
|
|
81
|
+
function checkOutput(out, force) {
|
|
82
|
+
if (!force && existsSync(out)) {
|
|
83
|
+
throw new NmtsError(`${out} already exists.`, {
|
|
84
|
+
exitCode: 4,
|
|
85
|
+
nextStep: "Nothing was written. Pass --out to choose another name, or --force to replace it.",
|
|
86
|
+
});
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
accessSync(dirname(out), constants.W_OK);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
throw new NmtsError(`Cannot write into ${dirname(out)}.`, {
|
|
93
|
+
exitCode: 4,
|
|
94
|
+
nextStep: "Nothing was written. Pass --out with a path in a directory that exists and can be written.",
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
async function make(target, options) {
|
|
99
|
+
const say = options.write ?? ((line) => process.stdout.write(`${line}\n`));
|
|
100
|
+
if (target === undefined || target === "" || options.to === undefined || options.to === "") {
|
|
101
|
+
throw new NmtsError("Say which file, and whom to hand it to.", {
|
|
102
|
+
exitCode: 2,
|
|
103
|
+
nextStep: `\`${BINARY_NAME} handover make <path> --to <public code | public code file>\`.`,
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
const force = options.force === true;
|
|
107
|
+
const out = resolve(options.out ?? handoverFileName(new Date()));
|
|
108
|
+
checkOutput(out, force);
|
|
109
|
+
const crypt = await loadCrypto();
|
|
110
|
+
// ⛔ A file that exists is read as a public code file; anything else must be a public code, whose
|
|
111
|
+
// check symbol is tested here before the network is asked anything.
|
|
112
|
+
const codeFile = existsSync(options.to)
|
|
113
|
+
? readPublicCodeFileText(crypt, readPassedFile(options.to, MAX_PUBLIC_CODE_FILE_BYTES, "public code file"))
|
|
114
|
+
: null;
|
|
115
|
+
const typedAddress = codeFile === null ? addressFromTyped(crypt, options.to) : null;
|
|
116
|
+
const session = await openSession({ server: options.server, network: options.network });
|
|
117
|
+
const network = networkOf(resolveNetwork(session.server, session.network));
|
|
118
|
+
const list = await readFileList(session.server, session.apiKey, session.code, session.accountId);
|
|
119
|
+
if (list.manifest === null) {
|
|
120
|
+
throw new NmtsError("This account has no file list, so there is nothing to hand over.", { exitCode: 4 });
|
|
121
|
+
}
|
|
122
|
+
const index = buildIndex(list.manifest.entries);
|
|
123
|
+
const entry = entryAt(list.manifest.entries, normalisePath(target), { nothingHappened: "Nothing was written." });
|
|
124
|
+
if (entry.kind !== KIND_FILE) {
|
|
125
|
+
throw new NmtsError(`No file at "${fullPathOf(index, entry)}".`, {
|
|
126
|
+
exitCode: 4,
|
|
127
|
+
nextStep: "That is a folder. Nothing was written — a handover carries one file.",
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
if (entry.dekWrapped === undefined || entry.contentHashCt === undefined) {
|
|
131
|
+
throw new NmtsError(`The file list holds no ${entry.dekWrapped === undefined ? "key" : "hash"} for "${entry.name}".`, {
|
|
132
|
+
exitCode: 4,
|
|
133
|
+
nextStep: "A handover carries both — the key that opens the file and the hash the recipient checks. Nothing was written.",
|
|
134
|
+
});
|
|
135
|
+
}
|
|
136
|
+
checkHandoverName(entry.name, entry.size, network);
|
|
137
|
+
const recipientShown = codeFile?.display ?? options.to;
|
|
138
|
+
// ⛔ The same unlock as `share` — it gives another account this file — and asked every time.
|
|
139
|
+
requireConsent("share");
|
|
140
|
+
if (options.yes !== true) {
|
|
141
|
+
say(`Would write a handover file for "${fullPathOf(index, entry)}", sealed to ${recipientShown}, to ${out}.`);
|
|
142
|
+
say(``);
|
|
143
|
+
say(`⛔ A handover cannot be taken back. The recipient can download the file until its storage ends or its stored bytes are destroyed.`);
|
|
144
|
+
say(` Removing the file from your drive does not stop it.`);
|
|
145
|
+
say(`Nothing was written. To go ahead: ${goAhead(target, options)}`);
|
|
146
|
+
say(`⛔ If a program is reading this on somebody's behalf: show it to them and let them decide.`);
|
|
147
|
+
return 5;
|
|
148
|
+
}
|
|
149
|
+
const keys = shareKeysOf(crypt, session.code);
|
|
150
|
+
const derived = crypt.kdf_derive(crypt.account_code_parse(session.code));
|
|
151
|
+
const dataKey = derived.slice(DERIVED.dataKey[0], DERIVED.dataKey[1]);
|
|
152
|
+
derived.fill(0);
|
|
153
|
+
let dek = null;
|
|
154
|
+
let digest = null;
|
|
155
|
+
try {
|
|
156
|
+
dek = crypt.envelope_open(dataKey, new TextEncoder().encode(AAD.dekWrap), new Uint8Array(Buffer.from(entry.dekWrapped, "base64url")));
|
|
157
|
+
digest = crypt.envelope_open(dataKey, new TextEncoder().encode(AAD.contentHash), new Uint8Array(Buffer.from(entry.contentHashCt, "base64url")));
|
|
158
|
+
dataKey.fill(0);
|
|
159
|
+
let recipientIdentity;
|
|
160
|
+
let recipientAddress;
|
|
161
|
+
if (codeFile !== null) {
|
|
162
|
+
recipientIdentity = codeFile.identity;
|
|
163
|
+
recipientAddress = codeFile.address;
|
|
164
|
+
}
|
|
165
|
+
else {
|
|
166
|
+
if (typedAddress === null)
|
|
167
|
+
throw new NmtsError("No recipient was given.", { exitCode: 2 });
|
|
168
|
+
recipientAddress = typedAddress;
|
|
169
|
+
const answer = await request(session.server, `/v1/share-recipients/${encodeURIComponent(b64(recipientAddress))}`, {
|
|
170
|
+
token: session.apiKey,
|
|
171
|
+
});
|
|
172
|
+
const identityB64 = isRecord(answer) ? answer["share_public_key"] : null;
|
|
173
|
+
if (typeof identityB64 !== "string") {
|
|
174
|
+
throw new NmtsError("That public code has never published an identity to seal to.", {
|
|
175
|
+
exitCode: 4,
|
|
176
|
+
nextStep: "Nothing was written. Ask the recipient for their public code file instead.",
|
|
177
|
+
});
|
|
178
|
+
}
|
|
179
|
+
recipientIdentity = new Uint8Array(Buffer.from(identityB64, "base64url"));
|
|
180
|
+
if (!identityMatches(crypt, recipientIdentity, recipientAddress)) {
|
|
181
|
+
throw new NmtsError("The identity the server returned is not the one that public code names.", {
|
|
182
|
+
exitCode: 1,
|
|
183
|
+
nextStep: "Nothing was written. Sealing to it would hand the file to somebody else.",
|
|
184
|
+
});
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (Buffer.from(recipientAddress).equals(Buffer.from(keys.address))) {
|
|
188
|
+
throw new NmtsError("That is your own public code.", { exitCode: 2, nextStep: "Nothing was written." });
|
|
189
|
+
}
|
|
190
|
+
const parts = partsOf(await request(session.server, `/v1/items/${encodeURIComponent(entry.id)}/parts`, { token: session.apiKey }));
|
|
191
|
+
const text = makeHandoverText(crypt, {
|
|
192
|
+
keys,
|
|
193
|
+
recipientIdentity,
|
|
194
|
+
recipientAddress,
|
|
195
|
+
dek,
|
|
196
|
+
itemId: entry.id,
|
|
197
|
+
name: entry.name,
|
|
198
|
+
size: entry.size,
|
|
199
|
+
digest,
|
|
200
|
+
parts,
|
|
201
|
+
network,
|
|
202
|
+
});
|
|
203
|
+
try {
|
|
204
|
+
// ⚠ 0600: nothing in it is readable without the recipient's key, but whom it is from is.
|
|
205
|
+
writeFileSync(out, text, { flag: force ? "w" : "wx", mode: 0o600 });
|
|
206
|
+
}
|
|
207
|
+
catch (error) {
|
|
208
|
+
const code = isRecord(error) && typeof error["code"] === "string" ? ` (${error["code"]})` : "";
|
|
209
|
+
throw new NmtsError(`Could not write ${out}${code}.`, {
|
|
210
|
+
exitCode: 4,
|
|
211
|
+
nextStep: "If it already exists, name another file with --out or replace it with --force.",
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
const shown = codeFile?.display ?? crypt.share_address_display(recipientAddress);
|
|
215
|
+
if (options.json === true) {
|
|
216
|
+
say(JSON.stringify({ out, name: entry.name, recipient: shown }));
|
|
217
|
+
return 0;
|
|
218
|
+
}
|
|
219
|
+
say(`${entry.name} → ${shown}`);
|
|
220
|
+
say(` handover file ${out}`);
|
|
221
|
+
say(``);
|
|
222
|
+
say(` Pass it on yourself. Only their NMTS key decrypts the file inside it.`);
|
|
223
|
+
if (codeFile === null)
|
|
224
|
+
say(` Looking up a public code tells NMTS whom you looked up. A public code file does not.`);
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
finally {
|
|
228
|
+
dataKey.fill(0);
|
|
229
|
+
dek?.fill(0);
|
|
230
|
+
digest?.fill(0);
|
|
231
|
+
keys.wipe();
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
/** The exact command that goes ahead: every option that was given, and `--yes`. */
|
|
235
|
+
function goAhead(target, options) {
|
|
236
|
+
const words = [BINARY_NAME, "handover", "make", JSON.stringify(target), "--to", JSON.stringify(options.to ?? "")];
|
|
237
|
+
if (options.out !== undefined)
|
|
238
|
+
words.push("--out", JSON.stringify(options.out));
|
|
239
|
+
if (options.force === true)
|
|
240
|
+
words.push("--force");
|
|
241
|
+
if (options.server !== undefined)
|
|
242
|
+
words.push("--server", JSON.stringify(options.server));
|
|
243
|
+
if (options.network !== undefined)
|
|
244
|
+
words.push("--network", JSON.stringify(options.network));
|
|
245
|
+
words.push("--yes");
|
|
246
|
+
return words.join(" ");
|
|
247
|
+
}
|
|
248
|
+
/** The stored pieces, as the server lists them for the file's owner. */
|
|
249
|
+
function partsOf(value) {
|
|
250
|
+
const rows = isRecord(value) ? value["parts"] : null;
|
|
251
|
+
if (!Array.isArray(rows) || rows.length === 0) {
|
|
252
|
+
throw new NmtsError("The server lists no stored pieces for this file.", { nextStep: "Nothing was written." });
|
|
253
|
+
}
|
|
254
|
+
const parsed = rows.map((row) => {
|
|
255
|
+
if (!isRecord(row))
|
|
256
|
+
throw new NmtsError("A stored piece was not described in a shape this version reads.");
|
|
257
|
+
const index = row["part_index"];
|
|
258
|
+
const kind = row["storage_kind"];
|
|
259
|
+
const where = row["network"] ?? NETWORK_WALRUS;
|
|
260
|
+
const blob = row["blob_id"];
|
|
261
|
+
const patch = row["patch_id"];
|
|
262
|
+
const len = row["sealed_len"];
|
|
263
|
+
const exp = row["expiry_epoch"];
|
|
264
|
+
if (typeof index !== "number" || typeof kind !== "number" || typeof blob !== "string" || typeof len !== "number" || typeof exp !== "number") {
|
|
265
|
+
throw new NmtsError("A stored piece was not described in a shape this version reads.");
|
|
266
|
+
}
|
|
267
|
+
// ⛔ The recipient fetches from Walrus aggregators and nothing else.
|
|
268
|
+
if (where !== NETWORK_WALRUS || (kind !== 0 && kind !== 1)) {
|
|
269
|
+
throw new NmtsError("A piece of this file is not stored on Walrus in a way a handover can carry.", { nextStep: "Nothing was written." });
|
|
270
|
+
}
|
|
271
|
+
const isPatch = kind === 1;
|
|
272
|
+
if (isPatch && typeof patch !== "string")
|
|
273
|
+
throw new NmtsError("A quilt piece came without its patch id.");
|
|
274
|
+
return { index, part: { blob: isPatch ? null : blob, patch: isPatch && typeof patch === "string" ? patch : null, len, exp } };
|
|
275
|
+
});
|
|
276
|
+
parsed.sort((a, b) => a.index - b.index);
|
|
277
|
+
if (parsed.some((p, i) => p.index !== i))
|
|
278
|
+
throw new NmtsError("The stored pieces are not numbered 0, 1, … in order.");
|
|
279
|
+
return parsed.map((p) => p.part);
|
|
280
|
+
}
|
|
281
|
+
async function open(file, options) {
|
|
282
|
+
const say = options.write ?? ((line) => process.stdout.write(`${line}\n`));
|
|
283
|
+
if (file === undefined || file === "") {
|
|
284
|
+
throw new NmtsError("Say which handover file to open.", { exitCode: 2, nextStep: `\`${BINARY_NAME} handover open <file>\`.` });
|
|
285
|
+
}
|
|
286
|
+
const text = readPassedFile(file, MAX_HANDOVER_FILE_BYTES, "handover file");
|
|
287
|
+
// ⛔ No session: that would demand an API key, and this path talks to no NMTS server. Only the
|
|
288
|
+
// NMTS key (to open the file) and the network (which aggregators to ask) are needed.
|
|
289
|
+
const code = await requireAccountCode();
|
|
290
|
+
const stored = readCredentialsFile();
|
|
291
|
+
const chain = resolveNetwork(resolveServer(options.server ?? stored?.server), options.network ?? stored?.network);
|
|
292
|
+
const crypt = await loadCrypto();
|
|
293
|
+
const keys = shareKeysOf(crypt, code.code);
|
|
294
|
+
let opened = null;
|
|
295
|
+
try {
|
|
296
|
+
opened = openHandoverText(crypt, keys, text, networkOf(chain));
|
|
297
|
+
// ⛔ The name is the SENDER'S choice: only its last segment, in the current directory.
|
|
298
|
+
const destination = options.out !== undefined ? resolve(options.out) : destinationFor(".", opened.name);
|
|
299
|
+
const parts = opened.parts.map((p, i) => ({
|
|
300
|
+
part_index: i,
|
|
301
|
+
storage_kind: p.patch !== null ? 1 : 0,
|
|
302
|
+
network: NETWORK_WALRUS,
|
|
303
|
+
blob_id: p.blob ?? p.patch ?? "",
|
|
304
|
+
...(p.patch !== null ? { patch_id: p.patch } : {}),
|
|
305
|
+
}));
|
|
306
|
+
const fetched = await fetchKnownParts({
|
|
307
|
+
parts,
|
|
308
|
+
size: opened.size,
|
|
309
|
+
dek: opened.dek,
|
|
310
|
+
expected: opened.digest,
|
|
311
|
+
chain,
|
|
312
|
+
sink: fileSink(destination, { force: options.force === true }),
|
|
313
|
+
});
|
|
314
|
+
if (options.json === true) {
|
|
315
|
+
say(JSON.stringify({ name: opened.name, bytes: fetched.byteCount, from: opened.sender, out: destination, expiryEpoch: opened.expiryEpoch }));
|
|
316
|
+
return 0;
|
|
317
|
+
}
|
|
318
|
+
say(`${opened.name} ${fetched.byteCount} bytes`);
|
|
319
|
+
say(` from ${opened.sender}`);
|
|
320
|
+
say(` saved to ${destination}, checked against the hash the sender sealed with it`);
|
|
321
|
+
return 0;
|
|
322
|
+
}
|
|
323
|
+
finally {
|
|
324
|
+
opened?.dek.fill(0);
|
|
325
|
+
opened?.digest.fill(0);
|
|
326
|
+
keys.wipe();
|
|
327
|
+
}
|
|
328
|
+
}
|
|
@@ -0,0 +1,59 @@
|
|
|
1
|
+
import { type CryptoGlue } from "../crypto.ts";
|
|
2
|
+
import { type HeavyCommitted, type HeavyFile } from "../heavy-upload.ts";
|
|
3
|
+
import type { Network } from "../network.ts";
|
|
4
|
+
import type { Progress } from "../progress.ts";
|
|
5
|
+
import type { Session } from "../session.ts";
|
|
6
|
+
import type { OnCollision } from "../collision.ts";
|
|
7
|
+
import type { PaddingRule } from "../shared/lib/crypto/size-padding.ts";
|
|
8
|
+
import { type AccountSettings } from "../shared/lib/drive/manifest-settings.ts";
|
|
9
|
+
import { type HeavyFlags } from "./put-payer.ts";
|
|
10
|
+
export type HeavyPayer = "credits" | "wallet" | "evm";
|
|
11
|
+
/** What a Heavy command line may carry. */
|
|
12
|
+
export interface HeavyCommandOptions extends HeavyFlags {
|
|
13
|
+
pay?: string | undefined;
|
|
14
|
+
epochs?: string | number | undefined;
|
|
15
|
+
storage?: string | undefined;
|
|
16
|
+
from?: string | undefined;
|
|
17
|
+
deposit?: string | number | undefined;
|
|
18
|
+
partSize?: string | number | undefined;
|
|
19
|
+
wallet?: string | undefined;
|
|
20
|
+
trustServerTipAddress?: boolean;
|
|
21
|
+
thumbnail?: boolean;
|
|
22
|
+
thumbnailFile?: string | undefined;
|
|
23
|
+
dryRun?: boolean;
|
|
24
|
+
json?: boolean;
|
|
25
|
+
/** The instant the wallet agreement is measured against. */
|
|
26
|
+
now?: number;
|
|
27
|
+
}
|
|
28
|
+
export declare function heavyPayerOf(pay: string | undefined): HeavyPayer;
|
|
29
|
+
/** Every option that means nothing on this payer, refused before a file is measured. */
|
|
30
|
+
export declare function refuseHeavyClashes(options: HeavyCommandOptions, payer: HeavyPayer): void;
|
|
31
|
+
/** `--days`: 1..365, default 28 — the term a credit buys. */
|
|
32
|
+
export declare function termDaysOf(raw: string | undefined): number;
|
|
33
|
+
export interface HeavyRunContext {
|
|
34
|
+
session: Session;
|
|
35
|
+
network: Network;
|
|
36
|
+
crypt: CryptoGlue;
|
|
37
|
+
rule: PaddingRule;
|
|
38
|
+
settings: AccountSettings | undefined;
|
|
39
|
+
say: (line: string) => void;
|
|
40
|
+
json: boolean;
|
|
41
|
+
progress: Progress;
|
|
42
|
+
}
|
|
43
|
+
/** What the run did, for the caller's printing and `--json`. */
|
|
44
|
+
export interface HeavyRunReport {
|
|
45
|
+
files: HeavyCommitted[];
|
|
46
|
+
facts: Record<string, unknown>;
|
|
47
|
+
}
|
|
48
|
+
/** The dry run: what is known without opening an order or touching a chain. */
|
|
49
|
+
export declare function heavyDryRun(files: readonly HeavyFile[], rule: PaddingRule, payer: HeavyPayer): Record<string, unknown>;
|
|
50
|
+
/** Upload these files to NMTS Heavy on this payer, and commit them. The caller writes the list. */
|
|
51
|
+
export declare function runHeavy(ctx: HeavyRunContext, files: readonly HeavyFile[], payer: HeavyPayer, options: HeavyCommandOptions): Promise<HeavyRunReport>;
|
|
52
|
+
/** A server refusal as itself; any other stop of the runner in the Heavy words. */
|
|
53
|
+
export declare function heavyFailure(error: unknown): unknown;
|
|
54
|
+
/** Write each committed file into the sealed list; a displaced one goes to the server's trash last. */
|
|
55
|
+
export declare function recordHeavy(session: Session, done: HeavyCommitted, onCollision: OnCollision | undefined): Promise<{
|
|
56
|
+
name: string;
|
|
57
|
+
replaced: string | null;
|
|
58
|
+
seq: number;
|
|
59
|
+
}>;
|
|
@@ -0,0 +1,247 @@
|
|
|
1
|
+
// The terminal around an NMTS Heavy upload — shared by `put --tier heavy` and `push --tier heavy`:
|
|
2
|
+
// which money pays, the review a person reads before anything is spent, the wallet agreement, the
|
|
3
|
+
// progress lines, and what a failure says.
|
|
4
|
+
//
|
|
5
|
+
// ⛔ THE THREE PAYERS AND THEIR STOPS.
|
|
6
|
+
// credits one order, credits taken when it opens — the same tier as a Standard credit upload.
|
|
7
|
+
// wallet the order opens first so its WAL price can be printed; the balances are read and a
|
|
8
|
+
// known shortfall stops the run BEFORE anything is sealed; the wallet agreement is held
|
|
9
|
+
// against the price; the WAL is sent only once every part is stored — the same act,
|
|
10
|
+
// lock and review as `put --pay wallet`.
|
|
11
|
+
// evm developer mode: the key's own EVM wallet pays Filecoin through the Synapse SDK. The
|
|
12
|
+
// same wallet lock; refused before anything is spent where no company is listed.
|
|
13
|
+
//
|
|
14
|
+
// ⛔ A SERVER REFUSAL IS PRINTED AS ONE. The shared runner wraps whatever stopped it; when that was
|
|
15
|
+
// the server explaining itself, the explanation is what goes out, with the same advice table
|
|
16
|
+
// (`api-advice.ts`) every other refusal uses.
|
|
17
|
+
import { ServerError } from "../api.js";
|
|
18
|
+
import { DERIVED } from "../crypto.js";
|
|
19
|
+
import { NmtsError } from "../errors.js";
|
|
20
|
+
import { HEAVY_COPY } from "../heavy-copy.js";
|
|
21
|
+
import { evmAccountFor, evmIndexOf } from "../heavy-evm.js";
|
|
22
|
+
import { copiesOf, heavySelfPut, providersOf, selfPayChain } from "../heavy-self-pay.js";
|
|
23
|
+
import { heavyCredits, heavyOrderPut, planHeavyFile } from "../heavy-upload.js";
|
|
24
|
+
import { heavyWalletPut } from "../heavy-wallet-pay.js";
|
|
25
|
+
import { addEntry } from "../manifest-write.js";
|
|
26
|
+
import { setTrashed } from "../item-trash.js";
|
|
27
|
+
import { activeWalletOf } from "../shared/lib/drive/manifest-settings.js";
|
|
28
|
+
import { HeavyOrderError } from "../shared/lib/heavy/order-runner.js";
|
|
29
|
+
import { coinAmount } from "../wallet.js";
|
|
30
|
+
import { recordWalletSpend, requireWalletGrant } from "../wallet-grant.js";
|
|
31
|
+
import { payingWalletIndex } from "../wallet-pay-index.js";
|
|
32
|
+
import { payerOf, refuseWalletOnlyOptions } from "./put-payer.js";
|
|
33
|
+
export function heavyPayerOf(pay) {
|
|
34
|
+
return pay === "evm" ? "evm" : payerOf(pay);
|
|
35
|
+
}
|
|
36
|
+
/** Every option that means nothing on this payer, refused before a file is measured. */
|
|
37
|
+
export function refuseHeavyClashes(options, payer) {
|
|
38
|
+
const standardOnly = [
|
|
39
|
+
["--epochs", options.epochs],
|
|
40
|
+
["--storage", options.storage],
|
|
41
|
+
["--from", options.from],
|
|
42
|
+
["--deposit", options.deposit],
|
|
43
|
+
["--part-size", options.partSize],
|
|
44
|
+
["--trust-server-tip-address", options.trustServerTipAddress === true ? true : undefined],
|
|
45
|
+
["--thumbnail", options.thumbnail === true ? true : undefined],
|
|
46
|
+
["--thumbnail-file", options.thumbnailFile],
|
|
47
|
+
];
|
|
48
|
+
for (const [flag, value] of standardOnly) {
|
|
49
|
+
if (value !== undefined)
|
|
50
|
+
throw new NmtsError(HEAVY_COPY.standardOnlyFlag(flag), { exitCode: 2 });
|
|
51
|
+
}
|
|
52
|
+
if (payer !== "evm") {
|
|
53
|
+
for (const [flag, value] of [["--copies", options.copies], ["--providers", options.providers], ["--evm-wallet", options.evmWallet]]) {
|
|
54
|
+
if (value !== undefined)
|
|
55
|
+
throw new NmtsError(HEAVY_COPY.evmOnlyFlag(flag), { exitCode: 2 });
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
if (payer !== "wallet") {
|
|
59
|
+
if (options.wallet !== undefined && options.wallet !== "")
|
|
60
|
+
refuseWalletOnlyOptions({ wallet: options.wallet });
|
|
61
|
+
if (options.days !== undefined)
|
|
62
|
+
throw new NmtsError(HEAVY_COPY.walletOnlyDays, { exitCode: 2 });
|
|
63
|
+
}
|
|
64
|
+
// Judged now, not after the files are read: a typo must not surface after the money.
|
|
65
|
+
if (payer === "evm") {
|
|
66
|
+
copiesOf(options.copies);
|
|
67
|
+
providersOf(options.providers);
|
|
68
|
+
evmIndexOf(options.evmWallet);
|
|
69
|
+
}
|
|
70
|
+
if (payer === "wallet")
|
|
71
|
+
termDaysOf(options.days);
|
|
72
|
+
}
|
|
73
|
+
/** `--days`: 1..365, default 28 — the term a credit buys. */
|
|
74
|
+
export function termDaysOf(raw) {
|
|
75
|
+
if (raw === undefined || raw === "")
|
|
76
|
+
return 28;
|
|
77
|
+
const value = Number(raw.trim());
|
|
78
|
+
if (!/^[0-9]+$/u.test(raw.trim()) || value < 1 || value > 365)
|
|
79
|
+
throw new NmtsError(HEAVY_COPY.badDays(raw), { exitCode: 2 });
|
|
80
|
+
return value;
|
|
81
|
+
}
|
|
82
|
+
/** The dry run: what is known without opening an order or touching a chain. */
|
|
83
|
+
export function heavyDryRun(files, rule, payer) {
|
|
84
|
+
const plans = files.map((f) => planHeavyFile(f.source.size, rule));
|
|
85
|
+
return {
|
|
86
|
+
dryRun: true,
|
|
87
|
+
tier: "heavy",
|
|
88
|
+
paidFrom: payer,
|
|
89
|
+
bytes: files.reduce((sum, f) => sum + f.source.size, 0),
|
|
90
|
+
sealedBytes: plans.flat().reduce((sum, p) => sum + p.sealedLen, 0),
|
|
91
|
+
parts: plans.reduce((sum, p) => sum + p.length, 0),
|
|
92
|
+
...(payer === "credits" ? { credits: plans.reduce((sum, p) => sum + heavyCredits(p), 0) } : {}),
|
|
93
|
+
};
|
|
94
|
+
}
|
|
95
|
+
function label(files) {
|
|
96
|
+
return files.length === 1 ? (files[0]?.name ?? "") : `${files.length} files`;
|
|
97
|
+
}
|
|
98
|
+
/** Upload these files to NMTS Heavy on this payer, and commit them. The caller writes the list. */
|
|
99
|
+
export async function runHeavy(ctx, files, payer, options) {
|
|
100
|
+
const { session, crypt } = ctx;
|
|
101
|
+
const bytes = files.reduce((sum, f) => sum + f.source.size, 0);
|
|
102
|
+
const now = new Date(options.now ?? Date.now());
|
|
103
|
+
const derived = crypt.kdf_derive(crypt.account_code_parse(session.code));
|
|
104
|
+
const dataKey = derived.slice(DERIVED.dataKey[0], DERIVED.dataKey[1]);
|
|
105
|
+
derived.fill(0);
|
|
106
|
+
try {
|
|
107
|
+
if (payer === "evm")
|
|
108
|
+
return await runSelfPaid(ctx, files, options, dataKey, now, bytes);
|
|
109
|
+
const onProgress = (event) => tellOrder(ctx, files, event);
|
|
110
|
+
const base = { server: session.server, bearer: session.apiKey, crypt, dataKey, rule: ctx.rule, onProgress };
|
|
111
|
+
if (payer === "credits") {
|
|
112
|
+
const credits = files.reduce((sum, f) => sum + heavyCredits(planHeavyFile(f.source.size, ctx.rule)), 0);
|
|
113
|
+
if (!ctx.json)
|
|
114
|
+
ctx.say(HEAVY_COPY.creditsPlan(label(files), bytes, credits));
|
|
115
|
+
const { run, files: done } = await heavyOrderPut(base, files, { pay: "credits" });
|
|
116
|
+
return { files: done, facts: { tier: "heavy", paidFrom: "credits", orderId: run.orderId, credits: run.credits ?? credits, expiryEpoch: run.expiryEpoch } };
|
|
117
|
+
}
|
|
118
|
+
return await runWalletPaid(ctx, files, options, base, now, bytes);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
throw heavyFailure(error);
|
|
122
|
+
}
|
|
123
|
+
finally {
|
|
124
|
+
dataKey.fill(0);
|
|
125
|
+
ctx.progress.done();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
/** The wallet payer: open, review, check, agree — then run the same order. */
|
|
129
|
+
async function runWalletPaid(ctx, files, options, base, now, bytes) {
|
|
130
|
+
const { session, network } = ctx;
|
|
131
|
+
const wallet = await payingWalletIndex({ wallet: options.wallet, readActiveWallet: async () => activeWalletOf(ctx.settings) });
|
|
132
|
+
// ⛔ THE AGREEMENT IS ASKED BEFORE AN ORDER OPENS — an unpaid order counts against the day's three.
|
|
133
|
+
requireWalletGrant("seal", { walFrost: 0n, suiMist: 0n }, now);
|
|
134
|
+
const termDays = termDaysOf(options.days);
|
|
135
|
+
const { run, files: done, quote } = await heavyWalletPut(base, files, {
|
|
136
|
+
code: session.code,
|
|
137
|
+
network,
|
|
138
|
+
wallet,
|
|
139
|
+
termDays,
|
|
140
|
+
onQuote: (q) => {
|
|
141
|
+
if (!ctx.json)
|
|
142
|
+
ctx.say(HEAVY_COPY.walletPlan(label(files), bytes, coinAmount(q.walFrost), termDays, q.address, q.treasury));
|
|
143
|
+
},
|
|
144
|
+
approve: (q) => requireWalletGrant("seal", { walFrost: q.walFrost, suiMist: q.feeMist ?? 0n }, now),
|
|
145
|
+
onPaid: recordWalletSpend,
|
|
146
|
+
});
|
|
147
|
+
return {
|
|
148
|
+
files: done,
|
|
149
|
+
facts: {
|
|
150
|
+
tier: "heavy",
|
|
151
|
+
paidFrom: "wallet",
|
|
152
|
+
orderId: run.orderId,
|
|
153
|
+
priceFrost: quote.walFrost.toString(),
|
|
154
|
+
priceWal: coinAmount(quote.walFrost),
|
|
155
|
+
termDays,
|
|
156
|
+
wallet: quote.address,
|
|
157
|
+
txDigest: run.txDigest ?? null,
|
|
158
|
+
expiryEpoch: run.expiryEpoch,
|
|
159
|
+
},
|
|
160
|
+
};
|
|
161
|
+
}
|
|
162
|
+
/** The evm payer: this key's own EVM wallet, through the Synapse SDK. */
|
|
163
|
+
async function runSelfPaid(ctx, files, options, dataKey, now, bytes) {
|
|
164
|
+
selfPayChain(ctx.network);
|
|
165
|
+
const copies = copiesOf(options.copies);
|
|
166
|
+
const providers = providersOf(options.providers);
|
|
167
|
+
requireWalletGrant("seal", { walFrost: 0n, suiMist: 0n }, now);
|
|
168
|
+
const account = await evmAccountFor(ctx.session.code, evmIndexOf(options.evmWallet));
|
|
169
|
+
if (!ctx.json)
|
|
170
|
+
ctx.say(HEAVY_COPY.evmPlan(label(files), bytes, copies, account.address));
|
|
171
|
+
const done = await heavySelfPut({
|
|
172
|
+
server: ctx.session.server,
|
|
173
|
+
bearer: ctx.session.apiKey,
|
|
174
|
+
crypt: ctx.crypt,
|
|
175
|
+
dataKey,
|
|
176
|
+
rule: ctx.rule,
|
|
177
|
+
network: ctx.network,
|
|
178
|
+
account,
|
|
179
|
+
copies,
|
|
180
|
+
providers,
|
|
181
|
+
onProgress: (event) => tellSelf(ctx, files, event),
|
|
182
|
+
}, files);
|
|
183
|
+
return {
|
|
184
|
+
files: done.files,
|
|
185
|
+
facts: { tier: "heavy", paidFrom: "evm", paidBy: done.paidBy, copies: done.copies, expiryEpoch: done.expiryEpoch, ...(providers === undefined ? {} : { providers: providers.map(String) }) },
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
function where(files, fileIndex, partIndex, parts) {
|
|
189
|
+
const name = files.length > 1 ? ` ${files[fileIndex]?.name ?? ""}` : "";
|
|
190
|
+
return `${name}${parts > 1 ? ` [${partIndex + 1}/${parts}]` : ""}`;
|
|
191
|
+
}
|
|
192
|
+
function tellOrder(ctx, files, event) {
|
|
193
|
+
if (ctx.json)
|
|
194
|
+
return;
|
|
195
|
+
if (event.phase === "uploading")
|
|
196
|
+
return ctx.progress.update(event.sentBytes, event.totalBytes);
|
|
197
|
+
if ("fileKey" in event) {
|
|
198
|
+
const fileIndex = Number(event.fileKey.slice(1));
|
|
199
|
+
const parts = planHeavyFile(files[fileIndex]?.source.size ?? 1, ctx.rule).length;
|
|
200
|
+
if (event.phase === "committing")
|
|
201
|
+
ctx.progress.done();
|
|
202
|
+
return ctx.say(HEAVY_COPY.progress(event.phase, where(files, fileIndex, event.partIndex, parts)));
|
|
203
|
+
}
|
|
204
|
+
ctx.say(HEAVY_COPY.progress(event.phase, ""));
|
|
205
|
+
}
|
|
206
|
+
function tellSelf(ctx, files, event) {
|
|
207
|
+
if (ctx.json)
|
|
208
|
+
return;
|
|
209
|
+
if (event.phase === "sent")
|
|
210
|
+
return ctx.progress.update(event.sentBytes, event.totalBytes);
|
|
211
|
+
if (event.phase === "stored")
|
|
212
|
+
ctx.progress.done();
|
|
213
|
+
ctx.say(HEAVY_COPY.progress(event.phase, where(files, event.fileIndex, event.partIndex, event.parts)));
|
|
214
|
+
}
|
|
215
|
+
/** A server refusal as itself; any other stop of the runner in the Heavy words. */
|
|
216
|
+
export function heavyFailure(error) {
|
|
217
|
+
if (!(error instanceof HeavyOrderError))
|
|
218
|
+
return error;
|
|
219
|
+
if (error.cause instanceof ServerError)
|
|
220
|
+
return error.cause;
|
|
221
|
+
return new NmtsError(HEAVY_COPY.failed(error.reason, error.message), { exitCode: 1, nextStep: HEAVY_COPY.failedNext(error.reason) });
|
|
222
|
+
}
|
|
223
|
+
/** Write each committed file into the sealed list; a displaced one goes to the server's trash last. */
|
|
224
|
+
export async function recordHeavy(session, done, onCollision) {
|
|
225
|
+
const now = Date.now();
|
|
226
|
+
const added = await addEntry({
|
|
227
|
+
server: session.server,
|
|
228
|
+
apiKey: session.apiKey,
|
|
229
|
+
code: session.code,
|
|
230
|
+
accountId: session.accountId,
|
|
231
|
+
...(onCollision !== undefined ? { onCollision } : {}),
|
|
232
|
+
entry: {
|
|
233
|
+
id: done.itemId,
|
|
234
|
+
parentId: done.parentId,
|
|
235
|
+
kind: 1,
|
|
236
|
+
name: done.name,
|
|
237
|
+
size: done.plaintextLen,
|
|
238
|
+
createdAt: now,
|
|
239
|
+
updatedAt: now,
|
|
240
|
+
dekWrapped: done.dekWrapped,
|
|
241
|
+
contentHashCt: done.contentHashCt,
|
|
242
|
+
},
|
|
243
|
+
});
|
|
244
|
+
if (added.replaced)
|
|
245
|
+
await setTrashed(session.server, session.apiKey, added.replaced.id, true);
|
|
246
|
+
return { name: added.name, replaced: added.replaced?.id ?? null, seq: added.seq };
|
|
247
|
+
}
|