@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.
- package/AGENTS.md +8 -8
- package/CHANGELOG.md +78 -0
- package/README.ko.md +6 -3
- package/README.md +31 -11
- 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 +19 -1
- package/dist/args.js +14 -2
- package/dist/artifact-about.d.ts +1 -1
- package/dist/autonomy.js +3 -3
- package/dist/collision.d.ts +20 -3
- package/dist/collision.js +21 -3
- package/dist/commands/erase.js +1 -0
- package/dist/commands/handover.d.ts +15 -0
- package/dist/commands/handover.js +376 -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/link.d.ts +12 -0
- package/dist/commands/link.js +121 -0
- package/dist/commands/ls.d.ts +5 -0
- package/dist/commands/ls.js +9 -1
- package/dist/commands/public-code-manage.d.ts +24 -0
- package/dist/commands/public-code-manage.js +217 -0
- package/dist/commands/public-code.d.ts +12 -1
- package/dist/commands/public-code.js +140 -45
- package/dist/commands/push-heavy.d.ts +6 -0
- package/dist/commands/push-heavy.js +128 -0
- package/dist/commands/push-tree.d.ts +24 -0
- package/dist/commands/push-tree.js +91 -0
- package/dist/commands/push.d.ts +4 -13
- package/dist/commands/push.js +7 -77
- package/dist/commands/put-heavy.d.ts +9 -0
- package/dist/commands/put-heavy.js +120 -0
- package/dist/commands/put-payer.d.ts +21 -0
- package/dist/commands/put-payer.js +24 -0
- package/dist/commands/put.d.ts +2 -2
- package/dist/commands/put.js +8 -7
- package/dist/commands/receive.js +14 -8
- package/dist/commands/s3.js +44 -10
- package/dist/commands/share-dispatch.js +14 -1
- package/dist/commands/share.d.ts +2 -0
- package/dist/commands/share.js +45 -35
- package/dist/commands/shares-sent.js +8 -3
- package/dist/crypto-surface.d.ts +35 -0
- package/dist/crypto-surface.js +13 -0
- package/dist/crypto.d.ts +8 -0
- package/dist/crypto.js +8 -0
- package/dist/download-part.d.ts +32 -1
- package/dist/download-part.js +95 -6
- package/dist/download-range.d.ts +19 -0
- package/dist/download-range.js +108 -0
- package/dist/download-sink.d.ts +15 -0
- package/dist/download.d.ts +14 -0
- package/dist/download.js +19 -4
- package/dist/handover-codes.d.ts +25 -0
- package/dist/handover-codes.js +106 -0
- package/dist/handover.d.ts +62 -0
- package/dist/handover.js +217 -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 +13 -4
- package/dist/help.js +24 -3
- package/dist/links.d.ts +58 -0
- package/dist/links.js +206 -0
- package/dist/list-tier.d.ts +7 -0
- package/dist/list-tier.js +47 -0
- package/dist/main.js +13 -10
- package/dist/manifest-write.d.ts +4 -3
- package/dist/mcp-tools/reads.js +20 -6
- package/dist/mcp-tools/share.js +47 -1
- package/dist/mcp-tools/tiers.js +16 -1
- package/dist/platform-sign.d.ts +4 -4
- package/dist/platform-sign.js +3 -3
- package/dist/portable.d.ts +23 -0
- package/dist/portable.js +19 -0
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/public-code-activity.d.ts +35 -0
- package/dist/public-code-activity.js +88 -0
- package/dist/public-code-refusals.d.ts +25 -0
- package/dist/public-code-refusals.js +87 -0
- package/dist/public-codes.d.ts +76 -0
- package/dist/public-codes.js +182 -0
- 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 +55 -1
- package/dist/risk.js +55 -3
- 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-codes.d.ts +40 -0
- package/dist/share-codes.js +151 -0
- package/dist/share.d.ts +39 -0
- package/dist/share.js +54 -13
- package/dist/shared/lib/api/types-heavy.d.ts +167 -0
- package/dist/shared/lib/api/types-heavy.js +60 -0
- package/dist/shared/lib/drive/rebuild-verify.js +1 -0
- package/dist/shared/lib/filecoin/providers.d.ts +25 -0
- package/dist/shared/lib/filecoin/providers.js +58 -0
- package/dist/shared/lib/heavy/download.d.ts +48 -0
- package/dist/shared/lib/heavy/download.js +116 -0
- package/dist/shared/lib/heavy/order-plan.d.ts +77 -0
- package/dist/shared/lib/heavy/order-plan.js +83 -0
- package/dist/shared/lib/heavy/order-runner.d.ts +9 -0
- package/dist/shared/lib/heavy/order-runner.js +300 -0
- package/dist/shared/lib/heavy/order-types.d.ts +90 -0
- package/dist/shared/lib/heavy/order-types.js +6 -0
- package/dist/shared/lib/heavy/order-wire.d.ts +55 -0
- package/dist/shared/lib/heavy/order-wire.js +113 -0
- package/dist/shared/lib/heavy/piece-cid.d.ts +24 -0
- package/dist/shared/lib/heavy/piece-cid.js +55 -0
- package/dist/shared/lib/heavy/sp-upload.d.ts +37 -0
- package/dist/shared/lib/heavy/sp-upload.js +112 -0
- package/dist/shared/lib/heavy/synapse-piece.d.ts +1 -0
- package/dist/shared/lib/heavy/synapse-piece.js +8 -0
- package/dist/shared/lib/share/handover-format.d.ts +129 -0
- package/dist/shared/lib/share/handover-format.js +323 -0
- package/dist/shared/lib/share/public-link.d.ts +41 -0
- package/dist/shared/lib/share/public-link.js +107 -0
- package/dist/shared/lib/share/shared-file-info.js +1 -0
- package/dist/shared/lib/storage-network.d.ts +4 -4
- package/dist/shared/lib/storage-network.js +7 -7
- package/dist/shared/lib/storage-tier.d.ts +28 -0
- package/dist/shared/lib/storage-tier.js +63 -0
- package/dist/shared/lib/wallet/send-rules.js +1 -0
- package/dist/upload-file.d.ts +8 -0
- package/dist/upload-file.js +57 -25
- package/dist/upload-price-node.d.ts +3 -0
- package/dist/upload-price-node.js +3 -6
- package/dist/upload-price.d.ts +10 -0
- package/dist/upload-price.js +13 -1
- package/dist/upload-run.d.ts +80 -0
- package/dist/upload-run.js +173 -0
- package/dist/upload-steps.js +15 -4
- package/dist/upload-store.d.ts +15 -6
- package/dist/upload-store.js +6 -0
- package/dist/upload-wallet-put.d.ts +9 -4
- package/dist/upload-wallet-put.js +16 -6
- package/dist/upload-wallet.js +4 -0
- package/dist/upload-wire.d.ts +25 -0
- package/dist/upload-wire.js +20 -1
- package/dist/upload.js +28 -13
- package/dist/wallet-sign.js +2 -2
- package/docs/commands/handover.md +54 -0
- package/docs/commands/heavy.md +35 -0
- package/docs/commands/link.md +27 -0
- package/docs/commands/mcp.md +2 -1
- package/docs/commands/public-code.md +25 -6
- package/docs/commands/push.md +4 -1
- package/docs/commands/put.md +15 -1
- package/docs/commands/share.md +8 -3
- package/package.json +5 -2
- package/server.json +2 -2
- package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +106 -4
- package/vendor/nmts-crypto/nmts_crypto_wasm.js +286 -3
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +12 -1
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export interface HandoverOptions {
|
|
2
|
+
server?: string | undefined;
|
|
3
|
+
network?: string | undefined;
|
|
4
|
+
/** `make`: the recipient — a public code, or the path of their public code file. */
|
|
5
|
+
to?: string | undefined;
|
|
6
|
+
/** `make`: the handover file to write. `open`: the file to save the received file as. */
|
|
7
|
+
out?: string | undefined;
|
|
8
|
+
/** `make`: the number of the live public code to send from. Default: the lowest-numbered live one. */
|
|
9
|
+
as?: string | undefined;
|
|
10
|
+
force?: boolean;
|
|
11
|
+
yes?: boolean;
|
|
12
|
+
json?: boolean;
|
|
13
|
+
write?: (line: string) => void;
|
|
14
|
+
}
|
|
15
|
+
export declare function handover(sub: string | undefined, operand: string | undefined, options?: HandoverOptions): Promise<number>;
|
|
@@ -0,0 +1,376 @@
|
|
|
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 needs no API key and tells the NMTS server nothing about the file or who sent it: the file
|
|
9
|
+
// carries everything, the NMTS key opens it, and the pieces come from Walrus aggregators. It asks
|
|
10
|
+
// the server two things at most, neither about this file: this account's own list of codes, only
|
|
11
|
+
// when code 0 did not open it and a key on this machine can read the list (which code to try
|
|
12
|
+
// next); and the public revoked list, one sixteenth at a time, never a single code
|
|
13
|
+
// (`revokedOnServer`). Offline, both are skipped and it still opens.
|
|
14
|
+
//
|
|
15
|
+
// ⛔ MAKE SENDS FROM A LIVE CODE of this account — the default, or `--as <n>` as `share` takes it —
|
|
16
|
+
// and refuses a recipient that is any code of this account, live or revoked.
|
|
17
|
+
//
|
|
18
|
+
// ⛔ A HANDOVER CANNOT BE TAKEN BACK. There is no row to delete. The recipient can download the file
|
|
19
|
+
// until its storage ends or its stored bytes are destroyed; removing it from the drive (`rm`,
|
|
20
|
+
// `erase`) does neither. The review printed before `--yes` says so.
|
|
21
|
+
//
|
|
22
|
+
// ⚠ `--out` MEANS THE SAME FILE-PATH THING EVERYWHERE: `make` writes the handover file there, `open`
|
|
23
|
+
// writes the received file there, as `receive --out` does. Without it, `make` writes
|
|
24
|
+
// `nmts-handover-<date>.nmtshandover` and `open` writes the sender's name — its last segment only —
|
|
25
|
+
// in the current directory.
|
|
26
|
+
import { constants, accessSync, existsSync, readFileSync, statSync, writeFileSync } from "node:fs";
|
|
27
|
+
import { dirname, resolve } from "node:path";
|
|
28
|
+
import { request } from "../api.js";
|
|
29
|
+
import { requireAccountCode } from "../code-access.js";
|
|
30
|
+
import { requireConsent } from "../consent.js";
|
|
31
|
+
import { AAD, DERIVED, loadCrypto } from "../crypto.js";
|
|
32
|
+
import { fetchKnownParts } from "../download.js";
|
|
33
|
+
import { fileSink } from "../download-sink-node.js";
|
|
34
|
+
import { buildIndex, entryAt, fullPathOf, KIND_FILE, normalisePath } from "../drive-paths.js";
|
|
35
|
+
import { NmtsError } from "../errors.js";
|
|
36
|
+
import { isRecord } from "../guards.js";
|
|
37
|
+
import { checkHandoverName, finishHandover, isHandoverNetwork, makeHandoverText, readHandoverText, readPublicCodeFileText } from "../handover.js";
|
|
38
|
+
import { checkedKeys, isMine, openWithMyCodes, senderIndexOf } from "../handover-codes.js";
|
|
39
|
+
import { readFileList } from "../manifest.js";
|
|
40
|
+
import { resolveNetwork } from "../network.js";
|
|
41
|
+
import { BINARY_NAME } from "../product.js";
|
|
42
|
+
import { destinationFor } from "../safe-path.js";
|
|
43
|
+
import { resolveServer } from "../server.js";
|
|
44
|
+
import { readCredentialsFile } from "../credentials.js";
|
|
45
|
+
import { openSession } from "../session.js";
|
|
46
|
+
import { publicCodeRefusal } from "../public-code-refusals.js";
|
|
47
|
+
import { readPublicCodes, revokedOnServer } from "../public-codes.js";
|
|
48
|
+
import { addressFromTyped, identityMatches } from "../share.js";
|
|
49
|
+
import { handoverFileName, MAX_HANDOVER_FILE_BYTES, MAX_PUBLIC_CODE_FILE_BYTES, } from "../shared/lib/share/handover-format.js";
|
|
50
|
+
import { NETWORK_WALRUS } from "../shared/lib/storage-network.js";
|
|
51
|
+
const b64 = (bytes) => Buffer.from(bytes).toString("base64url");
|
|
52
|
+
export async function handover(sub, operand, options = {}) {
|
|
53
|
+
if (sub === "make")
|
|
54
|
+
return await make(operand, options);
|
|
55
|
+
if (sub === "open")
|
|
56
|
+
return await open(operand, options);
|
|
57
|
+
throw new NmtsError("Say `make` or `open`.", {
|
|
58
|
+
exitCode: 2,
|
|
59
|
+
nextStep: `\`${BINARY_NAME} handover make <path> --to <public code | public code file>\` writes a handover file; ` +
|
|
60
|
+
`\`${BINARY_NAME} handover open <file>\` saves the file it carries.`,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
function networkOf(value) {
|
|
64
|
+
if (!isHandoverNetwork(value))
|
|
65
|
+
throw new NmtsError(`Handover files exist on mainnet and testnet, not ${value}.`, { exitCode: 2 });
|
|
66
|
+
return value;
|
|
67
|
+
}
|
|
68
|
+
/** Read a small file someone passed on: refused above `max` bytes, and every failure said plainly. */
|
|
69
|
+
function readPassedFile(path, max, what) {
|
|
70
|
+
let size;
|
|
71
|
+
try {
|
|
72
|
+
const stat = statSync(path);
|
|
73
|
+
if (!stat.isFile())
|
|
74
|
+
throw new Error("not a file");
|
|
75
|
+
size = stat.size;
|
|
76
|
+
}
|
|
77
|
+
catch {
|
|
78
|
+
throw new NmtsError(`Could not read ${path} as a ${what}.`, { exitCode: 2, nextStep: "Nothing was written." });
|
|
79
|
+
}
|
|
80
|
+
if (size > max) {
|
|
81
|
+
throw new NmtsError(`${path} is too large to be a ${what}.`, { exitCode: 2, nextStep: "Nothing was written." });
|
|
82
|
+
}
|
|
83
|
+
try {
|
|
84
|
+
return readFileSync(path, "utf8");
|
|
85
|
+
}
|
|
86
|
+
catch {
|
|
87
|
+
throw new NmtsError(`Could not read ${path}.`, { exitCode: 4, nextStep: "Nothing was written." });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
/** Refuse an output path that cannot be written, before anything is asked of anybody. */
|
|
91
|
+
function checkOutput(out, force) {
|
|
92
|
+
if (!force && existsSync(out)) {
|
|
93
|
+
throw new NmtsError(`${out} already exists.`, {
|
|
94
|
+
exitCode: 4,
|
|
95
|
+
nextStep: "Nothing was written. Pass --out to choose another name, or --force to replace it.",
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
try {
|
|
99
|
+
accessSync(dirname(out), constants.W_OK);
|
|
100
|
+
}
|
|
101
|
+
catch {
|
|
102
|
+
throw new NmtsError(`Cannot write into ${dirname(out)}.`, {
|
|
103
|
+
exitCode: 4,
|
|
104
|
+
nextStep: "Nothing was written. Pass --out with a path in a directory that exists and can be written.",
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
async function make(target, options) {
|
|
109
|
+
const say = options.write ?? ((line) => process.stdout.write(`${line}\n`));
|
|
110
|
+
if (target === undefined || target === "" || options.to === undefined || options.to === "") {
|
|
111
|
+
throw new NmtsError("Say which file, and whom to hand it to.", {
|
|
112
|
+
exitCode: 2,
|
|
113
|
+
nextStep: `\`${BINARY_NAME} handover make <path> --to <public code | public code file>\`.`,
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
const force = options.force === true;
|
|
117
|
+
const out = resolve(options.out ?? handoverFileName(new Date()));
|
|
118
|
+
checkOutput(out, force);
|
|
119
|
+
const crypt = await loadCrypto();
|
|
120
|
+
// ⛔ A file that exists is read as a public code file; anything else must be a public code, whose
|
|
121
|
+
// check symbol is tested here before the network is asked anything.
|
|
122
|
+
const codeFile = existsSync(options.to)
|
|
123
|
+
? readPublicCodeFileText(crypt, readPassedFile(options.to, MAX_PUBLIC_CODE_FILE_BYTES, "public code file"))
|
|
124
|
+
: null;
|
|
125
|
+
const typedAddress = codeFile === null ? addressFromTyped(crypt, options.to) : null;
|
|
126
|
+
const session = await openSession({ server: options.server, network: options.network });
|
|
127
|
+
const network = networkOf(resolveNetwork(session.server, session.network));
|
|
128
|
+
const list = await readFileList(session.server, session.apiKey, session.code, session.accountId);
|
|
129
|
+
if (list.manifest === null) {
|
|
130
|
+
throw new NmtsError("This account has no file list, so there is nothing to hand over.", { exitCode: 4 });
|
|
131
|
+
}
|
|
132
|
+
const index = buildIndex(list.manifest.entries);
|
|
133
|
+
const entry = entryAt(list.manifest.entries, normalisePath(target), { nothingHappened: "Nothing was written." });
|
|
134
|
+
if (entry.kind !== KIND_FILE) {
|
|
135
|
+
throw new NmtsError(`No file at "${fullPathOf(index, entry)}".`, {
|
|
136
|
+
exitCode: 4,
|
|
137
|
+
nextStep: "That is a folder. Nothing was written — a handover carries one file.",
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
if (entry.dekWrapped === undefined || entry.contentHashCt === undefined) {
|
|
141
|
+
throw new NmtsError(`The file list holds no ${entry.dekWrapped === undefined ? "key" : "hash"} for "${entry.name}".`, {
|
|
142
|
+
exitCode: 4,
|
|
143
|
+
nextStep: "A handover carries both — the key that opens the file and the hash the recipient checks. Nothing was written.",
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
checkHandoverName(entry.name, entry.size, network);
|
|
147
|
+
const recipientShown = codeFile?.display ?? options.to;
|
|
148
|
+
const codes = await readPublicCodes(session.server, session.apiKey);
|
|
149
|
+
const senderIndex = senderIndexOf(codes, options.as);
|
|
150
|
+
// ⚠ A WARNING, NOT A REFUSAL: a revoked code still opens what is sealed to it. Only a file can
|
|
151
|
+
// reach here with one — a typed revoked code has no identity to look up (410, below).
|
|
152
|
+
const recipientRevoked = codeFile === null ? false : await revokedOnServer(session.server, codeFile.address);
|
|
153
|
+
if (recipientRevoked === true && options.json !== true) {
|
|
154
|
+
say(`Its owner has revoked this public code. Ask them for the code they use now.`);
|
|
155
|
+
}
|
|
156
|
+
// ⛔ The same unlock as `share` — it gives another account this file — and asked every time.
|
|
157
|
+
requireConsent("share");
|
|
158
|
+
if (options.yes !== true) {
|
|
159
|
+
const from = options.as === undefined ? "" : `, from public code #${senderIndex}`;
|
|
160
|
+
say(`Would write a handover file for "${fullPathOf(index, entry)}", sealed to ${recipientShown}${from}, to ${out}.`);
|
|
161
|
+
say(``);
|
|
162
|
+
say(`⛔ A handover cannot be taken back. The recipient can download the file until its storage ends or its stored bytes are destroyed.`);
|
|
163
|
+
say(` Removing the file from your drive does not stop it.`);
|
|
164
|
+
say(`Nothing was written. To go ahead: ${goAhead(target, options)}`);
|
|
165
|
+
say(`⛔ If a program is reading this on somebody's behalf: show it to them and let them decide.`);
|
|
166
|
+
return 5;
|
|
167
|
+
}
|
|
168
|
+
const keys = checkedKeys(crypt, session.code, codes, senderIndex);
|
|
169
|
+
const derived = crypt.kdf_derive(crypt.account_code_parse(session.code));
|
|
170
|
+
const dataKey = derived.slice(DERIVED.dataKey[0], DERIVED.dataKey[1]);
|
|
171
|
+
derived.fill(0);
|
|
172
|
+
let dek = null;
|
|
173
|
+
let digest = null;
|
|
174
|
+
try {
|
|
175
|
+
dek = crypt.envelope_open(dataKey, new TextEncoder().encode(AAD.dekWrap), new Uint8Array(Buffer.from(entry.dekWrapped, "base64url")));
|
|
176
|
+
digest = crypt.envelope_open(dataKey, new TextEncoder().encode(AAD.contentHash), new Uint8Array(Buffer.from(entry.contentHashCt, "base64url")));
|
|
177
|
+
dataKey.fill(0);
|
|
178
|
+
let recipientIdentity;
|
|
179
|
+
let recipientAddress;
|
|
180
|
+
if (codeFile !== null) {
|
|
181
|
+
recipientIdentity = codeFile.identity;
|
|
182
|
+
recipientAddress = codeFile.address;
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
if (typedAddress === null)
|
|
186
|
+
throw new NmtsError("No recipient was given.", { exitCode: 2 });
|
|
187
|
+
recipientAddress = typedAddress;
|
|
188
|
+
let answer;
|
|
189
|
+
try {
|
|
190
|
+
answer = await request(session.server, `/v1/share-recipients/${encodeURIComponent(b64(recipientAddress))}`, {
|
|
191
|
+
token: session.apiKey,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
catch (error) {
|
|
195
|
+
throw publicCodeRefusal(error);
|
|
196
|
+
}
|
|
197
|
+
const identityB64 = isRecord(answer) ? answer["share_public_key"] : null;
|
|
198
|
+
if (typeof identityB64 !== "string") {
|
|
199
|
+
throw new NmtsError("That public code has never published an identity to seal to.", {
|
|
200
|
+
exitCode: 4,
|
|
201
|
+
nextStep: "Nothing was written. Ask the recipient for their public code file instead.",
|
|
202
|
+
});
|
|
203
|
+
}
|
|
204
|
+
recipientIdentity = new Uint8Array(Buffer.from(identityB64, "base64url"));
|
|
205
|
+
if (!identityMatches(crypt, recipientIdentity, recipientAddress)) {
|
|
206
|
+
throw new NmtsError("The identity the server returned is not the one that public code names.", {
|
|
207
|
+
exitCode: 1,
|
|
208
|
+
nextStep: "Nothing was written. Sealing to it would hand the file to somebody else.",
|
|
209
|
+
});
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
if (isMine(codes, keys, recipientAddress)) {
|
|
213
|
+
throw new NmtsError("That is your own public code.", { exitCode: 2, nextStep: "Nothing was written." });
|
|
214
|
+
}
|
|
215
|
+
const parts = partsOf(await request(session.server, `/v1/items/${encodeURIComponent(entry.id)}/parts`, { token: session.apiKey }));
|
|
216
|
+
const text = makeHandoverText(crypt, {
|
|
217
|
+
keys,
|
|
218
|
+
recipientIdentity,
|
|
219
|
+
recipientAddress,
|
|
220
|
+
dek,
|
|
221
|
+
itemId: entry.id,
|
|
222
|
+
name: entry.name,
|
|
223
|
+
size: entry.size,
|
|
224
|
+
digest,
|
|
225
|
+
parts,
|
|
226
|
+
network,
|
|
227
|
+
});
|
|
228
|
+
try {
|
|
229
|
+
// ⚠ 0600: nothing in it is readable without the recipient's key, but whom it is from is.
|
|
230
|
+
writeFileSync(out, text, { flag: force ? "w" : "wx", mode: 0o600 });
|
|
231
|
+
}
|
|
232
|
+
catch (error) {
|
|
233
|
+
const code = isRecord(error) && typeof error["code"] === "string" ? ` (${error["code"]})` : "";
|
|
234
|
+
throw new NmtsError(`Could not write ${out}${code}.`, {
|
|
235
|
+
exitCode: 4,
|
|
236
|
+
nextStep: "If it already exists, name another file with --out or replace it with --force.",
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
const shown = codeFile?.display ?? crypt.share_address_display(recipientAddress);
|
|
240
|
+
if (options.json === true) {
|
|
241
|
+
say(JSON.stringify({ out, name: entry.name, recipient: shown, fromIndex: keys.index, recipientRevoked }));
|
|
242
|
+
return 0;
|
|
243
|
+
}
|
|
244
|
+
say(`${entry.name} → ${shown}`);
|
|
245
|
+
say(` handover file ${out}`);
|
|
246
|
+
say(``);
|
|
247
|
+
say(` Pass it on yourself. Only their NMTS key decrypts the file inside it.`);
|
|
248
|
+
if (codeFile === null)
|
|
249
|
+
say(` Looking up a public code tells NMTS whom you looked up. A public code file does not.`);
|
|
250
|
+
return 0;
|
|
251
|
+
}
|
|
252
|
+
finally {
|
|
253
|
+
dataKey.fill(0);
|
|
254
|
+
dek?.fill(0);
|
|
255
|
+
digest?.fill(0);
|
|
256
|
+
keys.wipe();
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
/** The exact command that goes ahead: every option that was given, and `--yes`. */
|
|
260
|
+
function goAhead(target, options) {
|
|
261
|
+
const words = [BINARY_NAME, "handover", "make", JSON.stringify(target), "--to", JSON.stringify(options.to ?? "")];
|
|
262
|
+
if (options.out !== undefined)
|
|
263
|
+
words.push("--out", JSON.stringify(options.out));
|
|
264
|
+
if (options.force === true)
|
|
265
|
+
words.push("--force");
|
|
266
|
+
// ⚠ Already read as a number before the review is printed, so it is digits.
|
|
267
|
+
if (options.as !== undefined)
|
|
268
|
+
words.push("--as", options.as.trim());
|
|
269
|
+
if (options.server !== undefined)
|
|
270
|
+
words.push("--server", JSON.stringify(options.server));
|
|
271
|
+
if (options.network !== undefined)
|
|
272
|
+
words.push("--network", JSON.stringify(options.network));
|
|
273
|
+
words.push("--yes");
|
|
274
|
+
return words.join(" ");
|
|
275
|
+
}
|
|
276
|
+
/** The stored pieces, as the server lists them for the file's owner. */
|
|
277
|
+
function partsOf(value) {
|
|
278
|
+
const rows = isRecord(value) ? value["parts"] : null;
|
|
279
|
+
if (!Array.isArray(rows) || rows.length === 0) {
|
|
280
|
+
throw new NmtsError("The server lists no stored pieces for this file.", { nextStep: "Nothing was written." });
|
|
281
|
+
}
|
|
282
|
+
const parsed = rows.map((row) => {
|
|
283
|
+
if (!isRecord(row))
|
|
284
|
+
throw new NmtsError("A stored piece was not described in a shape this version reads.");
|
|
285
|
+
const index = row["part_index"];
|
|
286
|
+
const kind = row["storage_kind"];
|
|
287
|
+
const where = row["network"] ?? NETWORK_WALRUS;
|
|
288
|
+
const blob = row["blob_id"];
|
|
289
|
+
const patch = row["patch_id"];
|
|
290
|
+
const len = row["sealed_len"];
|
|
291
|
+
const exp = row["expiry_epoch"];
|
|
292
|
+
if (typeof index !== "number" || typeof kind !== "number" || typeof blob !== "string" || typeof len !== "number" || typeof exp !== "number") {
|
|
293
|
+
throw new NmtsError("A stored piece was not described in a shape this version reads.");
|
|
294
|
+
}
|
|
295
|
+
// ⛔ The recipient fetches from Walrus aggregators and nothing else.
|
|
296
|
+
if (where !== NETWORK_WALRUS || (kind !== 0 && kind !== 1)) {
|
|
297
|
+
throw new NmtsError("A piece of this file is not stored on Walrus in a way a handover can carry.", { nextStep: "Nothing was written." });
|
|
298
|
+
}
|
|
299
|
+
const isPatch = kind === 1;
|
|
300
|
+
if (isPatch && typeof patch !== "string")
|
|
301
|
+
throw new NmtsError("A quilt piece came without its patch id.");
|
|
302
|
+
return { index, part: { blob: isPatch ? null : blob, patch: isPatch && typeof patch === "string" ? patch : null, len, exp } };
|
|
303
|
+
});
|
|
304
|
+
parsed.sort((a, b) => a.index - b.index);
|
|
305
|
+
if (parsed.some((p, i) => p.index !== i))
|
|
306
|
+
throw new NmtsError("The stored pieces are not numbered 0, 1, … in order.");
|
|
307
|
+
return parsed.map((p) => p.part);
|
|
308
|
+
}
|
|
309
|
+
async function open(file, options) {
|
|
310
|
+
const say = options.write ?? ((line) => process.stdout.write(`${line}\n`));
|
|
311
|
+
if (file === undefined || file === "") {
|
|
312
|
+
throw new NmtsError("Say which handover file to open.", { exitCode: 2, nextStep: `\`${BINARY_NAME} handover open <file>\`.` });
|
|
313
|
+
}
|
|
314
|
+
const text = readPassedFile(file, MAX_HANDOVER_FILE_BYTES, "handover file");
|
|
315
|
+
// ⛔ No session: that would demand an API key, and this path talks to no NMTS server. Only the
|
|
316
|
+
// NMTS key (to open the file) and the network (which aggregators to ask) are needed.
|
|
317
|
+
const code = await requireAccountCode();
|
|
318
|
+
const stored = readCredentialsFile();
|
|
319
|
+
const server = resolveServer(options.server ?? stored?.server);
|
|
320
|
+
const chain = resolveNetwork(server, options.network ?? stored?.network);
|
|
321
|
+
const crypt = await loadCrypto();
|
|
322
|
+
const sealed = readHandoverText(crypt, text, networkOf(chain));
|
|
323
|
+
const found = await openWithMyCodes(crypt, code.code, sealed.sealed, server);
|
|
324
|
+
let opened = null;
|
|
325
|
+
try {
|
|
326
|
+
opened = finishHandover(crypt, sealed, found.dek);
|
|
327
|
+
// ⛔ The name is the SENDER'S choice: only its last segment, in the current directory.
|
|
328
|
+
const destination = options.out !== undefined ? resolve(options.out) : destinationFor(".", opened.name);
|
|
329
|
+
const parts = opened.parts.map((p, i) => ({
|
|
330
|
+
part_index: i,
|
|
331
|
+
storage_kind: p.patch !== null ? 1 : 0,
|
|
332
|
+
network: NETWORK_WALRUS,
|
|
333
|
+
blob_id: p.blob ?? p.patch ?? "",
|
|
334
|
+
...(p.patch !== null ? { patch_id: p.patch } : {}),
|
|
335
|
+
}));
|
|
336
|
+
const fetched = await fetchKnownParts({
|
|
337
|
+
parts,
|
|
338
|
+
size: opened.size,
|
|
339
|
+
dek: opened.dek,
|
|
340
|
+
expected: opened.digest,
|
|
341
|
+
chain,
|
|
342
|
+
sink: fileSink(destination, { force: options.force === true }),
|
|
343
|
+
});
|
|
344
|
+
// ⛔ Both by bucket, never by code; null (offline, refused) says nothing.
|
|
345
|
+
const buckets = new Map();
|
|
346
|
+
const toRevoked = found.revoked ?? (await revokedOnServer(server, found.address, buckets));
|
|
347
|
+
const senderRevoked = await revokedOnServer(server, opened.senderAddress, buckets);
|
|
348
|
+
if (options.json === true) {
|
|
349
|
+
say(JSON.stringify({
|
|
350
|
+
name: opened.name,
|
|
351
|
+
bytes: fetched.byteCount,
|
|
352
|
+
from: opened.sender,
|
|
353
|
+
out: destination,
|
|
354
|
+
expiryEpoch: opened.expiryEpoch,
|
|
355
|
+
toIndex: found.index,
|
|
356
|
+
toRevoked,
|
|
357
|
+
senderRevoked,
|
|
358
|
+
}));
|
|
359
|
+
return 0;
|
|
360
|
+
}
|
|
361
|
+
say(`${opened.name} ${fetched.byteCount} bytes`);
|
|
362
|
+
say(` from ${opened.sender}`);
|
|
363
|
+
if (senderRevoked === true)
|
|
364
|
+
say(` The sender has since revoked this code.`);
|
|
365
|
+
if (toRevoked === true)
|
|
366
|
+
say(` received with your revoked public code #${found.index} ${found.display}`);
|
|
367
|
+
else if (found.index !== 0)
|
|
368
|
+
say(` to #${found.index} ${found.display}`);
|
|
369
|
+
say(` saved to ${destination}, checked against the hash the sender sealed with it`);
|
|
370
|
+
return 0;
|
|
371
|
+
}
|
|
372
|
+
finally {
|
|
373
|
+
found.dek.fill(0);
|
|
374
|
+
opened?.digest.fill(0);
|
|
375
|
+
}
|
|
376
|
+
}
|
|
@@ -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
|
+
}>;
|