@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,20 @@
|
|
|
1
|
+
import type { PrivateKeyAccount } from "viem/accounts";
|
|
2
|
+
/** An EVM wallet number as a person typed it. Refused, never rounded. */
|
|
3
|
+
export declare function evmIndexOf(raw: string | undefined): number;
|
|
4
|
+
/** The EIP-55 address of a 32-byte secp256k1 private key. */
|
|
5
|
+
export declare function evmAddressOf(privateKey: Uint8Array): `0x${string}`;
|
|
6
|
+
/**
|
|
7
|
+
* EVM wallet `index`'s private key, from an NMTS key.
|
|
8
|
+
*
|
|
9
|
+
* ⛔ THE CALLER WIPES WHAT COMES BACK — it moves money on Filecoin.
|
|
10
|
+
*/
|
|
11
|
+
export declare function evmKeyOf(code: string, index: number): Promise<Uint8Array>;
|
|
12
|
+
/** EVM wallet `index`'s address, from an NMTS key. Nothing is signed; the key is wiped here. */
|
|
13
|
+
export declare function evmAddressFor(code: string, index: number): Promise<`0x${string}`>;
|
|
14
|
+
/**
|
|
15
|
+
* EVM wallet `index` as a viem account that signs. The key bytes are wiped once the account holds
|
|
16
|
+
* them; the account itself keeps the key for as long as the caller keeps the account.
|
|
17
|
+
*/
|
|
18
|
+
export declare function evmAccountFor(code: string, index: number): Promise<PrivateKeyAccount>;
|
|
19
|
+
/** `0x…` for viem, from key bytes. ⚠ The string cannot be wiped; keep it only as long as the call. */
|
|
20
|
+
export declare function hexKey(key: Uint8Array): `0x${string}`;
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
// The EVM wallets an NMTS key derives (NCF-3 §1.9) — the key the engine hands out, and the address
|
|
2
|
+
// computed from it here.
|
|
3
|
+
//
|
|
4
|
+
// ⛔ THE KEY COMES FROM THE ENGINE, NEVER FROM HERE. `evm_key_for` is the same WebAssembly the
|
|
5
|
+
// browser and the recovery tool run, so one NMTS key names one Filecoin address everywhere.
|
|
6
|
+
// Only the step from a private key to its address is written in TypeScript, because it is the
|
|
7
|
+
// textbook one (secp256k1 public key → Keccak-256 → last 20 bytes, EIP-55 case) and a test holds
|
|
8
|
+
// it to the frozen vectors in `crypto/tests/vectors/ncf3-evm.json`.
|
|
9
|
+
//
|
|
10
|
+
// ⛔ EVERY KEY IS WIPED BY WHOEVER ASKED FOR IT. The derivation output holds every key in the
|
|
11
|
+
// account; it and the wallet root sliced out of it are zeroed here on every path out.
|
|
12
|
+
//
|
|
13
|
+
// ⚠ NO `t410`/`f410` FORM. Neither viem nor the Synapse packages carry a helper for Filecoin's
|
|
14
|
+
// delegated address spelling, and it is not written by hand here — the 0x address is the one the
|
|
15
|
+
// Filecoin EVM, the Synapse SDK and every EVM wallet read.
|
|
16
|
+
import { secp256k1 } from "@noble/curves/secp256k1.js";
|
|
17
|
+
import { keccak_256 } from "@noble/hashes/sha3.js";
|
|
18
|
+
import { assertUsableCode } from "./account.js";
|
|
19
|
+
import { DERIVED, loadCrypto } from "./crypto.js";
|
|
20
|
+
import { NmtsError } from "./errors.js";
|
|
21
|
+
import { BINARY_NAME } from "./product.js";
|
|
22
|
+
/** The highest EVM wallet number accepted — the same range the Sui wallets use. */
|
|
23
|
+
const EVM_INDEX_LIMIT = 2 ** 31;
|
|
24
|
+
/** An EVM wallet number as a person typed it. Refused, never rounded. */
|
|
25
|
+
export function evmIndexOf(raw) {
|
|
26
|
+
if (raw === undefined || raw === "")
|
|
27
|
+
return 0;
|
|
28
|
+
const text = raw.trim();
|
|
29
|
+
const value = Number(text);
|
|
30
|
+
if (!/^[0-9]+$/u.test(text) || !Number.isSafeInteger(value) || value >= EVM_INDEX_LIMIT) {
|
|
31
|
+
throw new NmtsError(`An EVM wallet number is a whole number from 0 to ${EVM_INDEX_LIMIT - 1}, not "${raw}".`, {
|
|
32
|
+
exitCode: 2,
|
|
33
|
+
nextStep: `Nothing was signed. \`${BINARY_NAME} heavy wallet\` shows wallet 0.`,
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
return value;
|
|
37
|
+
}
|
|
38
|
+
/** `0x` + 40 hex, in EIP-55 mixed case. */
|
|
39
|
+
function checksummed(addressHex) {
|
|
40
|
+
const lower = addressHex.toLowerCase();
|
|
41
|
+
const hash = keccak_256(new TextEncoder().encode(lower));
|
|
42
|
+
let out = "0x";
|
|
43
|
+
for (let i = 0; i < lower.length; i += 1) {
|
|
44
|
+
const char = lower.charAt(i);
|
|
45
|
+
const byte = hash[i >> 1] ?? 0;
|
|
46
|
+
const nibble = i % 2 === 0 ? byte >> 4 : byte & 0x0f;
|
|
47
|
+
out = `${out}${nibble >= 8 ? char.toUpperCase() : char}`;
|
|
48
|
+
}
|
|
49
|
+
return out;
|
|
50
|
+
}
|
|
51
|
+
/** The EIP-55 address of a 32-byte secp256k1 private key. */
|
|
52
|
+
export function evmAddressOf(privateKey) {
|
|
53
|
+
const publicKey = secp256k1.getPublicKey(privateKey, false);
|
|
54
|
+
const digest = keccak_256(publicKey.subarray(1));
|
|
55
|
+
const hex = Array.from(digest.subarray(12), (b) => b.toString(16).padStart(2, "0")).join("");
|
|
56
|
+
return checksummed(hex);
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* EVM wallet `index`'s private key, from an NMTS key.
|
|
60
|
+
*
|
|
61
|
+
* ⛔ THE CALLER WIPES WHAT COMES BACK — it moves money on Filecoin.
|
|
62
|
+
*/
|
|
63
|
+
export async function evmKeyOf(code, index) {
|
|
64
|
+
await assertUsableCode(code);
|
|
65
|
+
const glue = await loadCrypto();
|
|
66
|
+
const bytes = glue.account_code_parse(code);
|
|
67
|
+
const derived = glue.kdf_derive(bytes);
|
|
68
|
+
const [from, to] = DERIVED.walletRoot;
|
|
69
|
+
const root = derived.slice(from, to);
|
|
70
|
+
try {
|
|
71
|
+
return glue.evm_key_for(root, index);
|
|
72
|
+
}
|
|
73
|
+
finally {
|
|
74
|
+
root.fill(0);
|
|
75
|
+
derived.fill(0);
|
|
76
|
+
bytes.fill(0);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
/** EVM wallet `index`'s address, from an NMTS key. Nothing is signed; the key is wiped here. */
|
|
80
|
+
export async function evmAddressFor(code, index) {
|
|
81
|
+
const key = await evmKeyOf(code, index);
|
|
82
|
+
try {
|
|
83
|
+
return evmAddressOf(key);
|
|
84
|
+
}
|
|
85
|
+
finally {
|
|
86
|
+
key.fill(0);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* EVM wallet `index` as a viem account that signs. The key bytes are wiped once the account holds
|
|
91
|
+
* them; the account itself keeps the key for as long as the caller keeps the account.
|
|
92
|
+
*/
|
|
93
|
+
export async function evmAccountFor(code, index) {
|
|
94
|
+
const key = await evmKeyOf(code, index);
|
|
95
|
+
try {
|
|
96
|
+
const { privateKeyToAccount } = await import("viem/accounts");
|
|
97
|
+
return privateKeyToAccount(hexKey(key));
|
|
98
|
+
}
|
|
99
|
+
finally {
|
|
100
|
+
key.fill(0);
|
|
101
|
+
}
|
|
102
|
+
}
|
|
103
|
+
/** `0x…` for viem, from key bytes. ⚠ The string cannot be wiped; keep it only as long as the call. */
|
|
104
|
+
export function hexKey(key) {
|
|
105
|
+
return `0x${Array.from(key, (b) => b.toString(16).padStart(2, "0")).join("")}`;
|
|
106
|
+
}
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
import type { Account } from "viem";
|
|
2
|
+
import type { CryptoGlue } from "./crypto.ts";
|
|
3
|
+
import { commitHeavyItem } from "./heavy-api.ts";
|
|
4
|
+
import { type HeavyCommitted, type HeavyFile } from "./heavy-upload.ts";
|
|
5
|
+
import type { Network } from "./network.ts";
|
|
6
|
+
import type { PaddingRule } from "./shared/lib/crypto/size-padding.ts";
|
|
7
|
+
import { type FilecoinChain } from "./shared/lib/filecoin/providers.ts";
|
|
8
|
+
/** Copies one self-paid part may ask for, and the default. */
|
|
9
|
+
export declare const SELF_PAY_MAX_COPIES = 12;
|
|
10
|
+
export declare const SELF_PAY_DEFAULT_COPIES = 2;
|
|
11
|
+
/** `--copies` as typed. Absent = 2. Refused, never clamped. */
|
|
12
|
+
export declare function copiesOf(raw: string | number | undefined): number;
|
|
13
|
+
/** `--providers 4,9` as registry ids. Absent = the SDK chooses. */
|
|
14
|
+
export declare function providersOf(raw: string | readonly (number | bigint)[] | undefined): bigint[] | undefined;
|
|
15
|
+
/** Which Filecoin chain this NMTS network pays on — refused where no company is listed. */
|
|
16
|
+
export declare function selfPayChain(network: Network): FilecoinChain;
|
|
17
|
+
/** The Synapse client for one account on one chain. `account` may be a bare address for reads. */
|
|
18
|
+
export declare function synapseFor(account: Account | `0x${string}`, chain: FilecoinChain): Promise<import("@filoz/synapse-sdk").Synapse>;
|
|
19
|
+
/** The phases a caller can show, per part. */
|
|
20
|
+
export type SelfPayProgress = {
|
|
21
|
+
phase: "sealing" | "uploading" | "stored";
|
|
22
|
+
fileIndex: number;
|
|
23
|
+
partIndex: number;
|
|
24
|
+
parts: number;
|
|
25
|
+
} | {
|
|
26
|
+
phase: "sent";
|
|
27
|
+
fileIndex: number;
|
|
28
|
+
partIndex: number;
|
|
29
|
+
parts: number;
|
|
30
|
+
sentBytes: number;
|
|
31
|
+
totalBytes: number;
|
|
32
|
+
};
|
|
33
|
+
export interface SelfPayContext {
|
|
34
|
+
server: string;
|
|
35
|
+
bearer: string;
|
|
36
|
+
crypt: CryptoGlue;
|
|
37
|
+
/** Borrowed — the caller wipes it. */
|
|
38
|
+
dataKey: Uint8Array;
|
|
39
|
+
rule: PaddingRule;
|
|
40
|
+
network: Network;
|
|
41
|
+
/** The EVM account that pays and signs: this key's own (`evm_key_for`), or a business's. */
|
|
42
|
+
account: Account;
|
|
43
|
+
copies: number;
|
|
44
|
+
providers?: readonly bigint[] | undefined;
|
|
45
|
+
signal?: AbortSignal | undefined;
|
|
46
|
+
onProgress?: ((event: SelfPayProgress) => void) | undefined;
|
|
47
|
+
/** ⚠ Seams, not options: the storage client and the commit, for tests. */
|
|
48
|
+
synapse?: SelfPaySynapse | undefined;
|
|
49
|
+
commit?: typeof commitHeavyItem | undefined;
|
|
50
|
+
}
|
|
51
|
+
/** The two things this path asks of the Synapse SDK. */
|
|
52
|
+
export interface SelfPaySynapse {
|
|
53
|
+
upload(bytes: Uint8Array, options: {
|
|
54
|
+
copies: number;
|
|
55
|
+
providerIds?: bigint[];
|
|
56
|
+
signal?: AbortSignal;
|
|
57
|
+
onProgress?: (sent: number) => void;
|
|
58
|
+
}): Promise<{
|
|
59
|
+
pieceCid: string;
|
|
60
|
+
copies: {
|
|
61
|
+
providerId: bigint;
|
|
62
|
+
dataSetId: bigint;
|
|
63
|
+
pieceId: bigint;
|
|
64
|
+
retrievalUrl: string;
|
|
65
|
+
}[];
|
|
66
|
+
}>;
|
|
67
|
+
/** The Filecoin epoch now, and how many more the deposit lasts at its current rate. */
|
|
68
|
+
runway(): Promise<{
|
|
69
|
+
epoch: bigint;
|
|
70
|
+
runwayInEpochs: bigint;
|
|
71
|
+
}>;
|
|
72
|
+
}
|
|
73
|
+
/** The real one: the Synapse SDK, over this account on this chain. */
|
|
74
|
+
export declare function synapseSelfPay(account: Account, chain: FilecoinChain): Promise<SelfPaySynapse>;
|
|
75
|
+
/**
|
|
76
|
+
* The epoch the deposit reaches at the rate the account pays after this upload.
|
|
77
|
+
*
|
|
78
|
+
* ⚠ A RATE OF ZERO HAS NO END (the SDK answers the largest uint256). That only happens when no
|
|
79
|
+
* piece is being paid for, which after a successful upload it is; the 30-day lockup is written
|
|
80
|
+
* then, because it is what the storage company is owed whatever the deposit does.
|
|
81
|
+
*/
|
|
82
|
+
export declare function expiryFrom(runway: {
|
|
83
|
+
epoch: bigint;
|
|
84
|
+
runwayInEpochs: bigint;
|
|
85
|
+
}): number;
|
|
86
|
+
/** Upload these files from the key's own EVM wallet and commit each one. The caller writes the list. */
|
|
87
|
+
export declare function heavySelfPut(ctx: SelfPayContext, files: readonly HeavyFile[]): Promise<{
|
|
88
|
+
files: HeavyCommitted[];
|
|
89
|
+
expiryEpoch: number;
|
|
90
|
+
paidBy: string;
|
|
91
|
+
copies: number[];
|
|
92
|
+
}>;
|
|
@@ -0,0 +1,167 @@
|
|
|
1
|
+
// NMTS Heavy paid by the key's OWN EVM wallet — developer mode. Anything a developer could do with
|
|
2
|
+
// the Synapse SDK directly (how many copies, which storage companies, their own deposit) is done
|
|
3
|
+
// here through it, and the result is filed in the account like any other file.
|
|
4
|
+
//
|
|
5
|
+
// ⛔ NO ORDER AND NO TREASURY. The wallet's own Filecoin Pay deposit pays the storage companies;
|
|
6
|
+
// the server only records where the copies are (`paid_by` = the 0x address, `owner_kind` 0) and
|
|
7
|
+
// checks nothing about the payment, as it does for a Walrus wallet upload.
|
|
8
|
+
//
|
|
9
|
+
// ⛔ REFUSED BEFORE ANYTHING IS SPENT ON A CHAIN WITH NO LISTED COMPANY. Today that is mainnet:
|
|
10
|
+
// `shared/lib/filecoin/providers.ts` has no mainnet rows, so self-paid Heavy is Calibration only.
|
|
11
|
+
//
|
|
12
|
+
// ⛔ THE SDK IS LOADED ONLY WHEN THIS RUNS (it pulls in viem, seconds in Node), never at start-up.
|
|
13
|
+
//
|
|
14
|
+
// ⚠ ITS REQUESTS DO NOT GO THROUGH `reachFetch`. The Synapse SDK makes its own RPC and storage
|
|
15
|
+
// company calls; a proxy set on this package is not applied to them.
|
|
16
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
17
|
+
import { bytesToHex } from "@noble/hashes/utils.js";
|
|
18
|
+
import { NmtsError } from "./errors.js";
|
|
19
|
+
import { commitHeavyItem } from "./heavy-api.js";
|
|
20
|
+
import { HEAVY_COPY } from "./heavy-copy.js";
|
|
21
|
+
import { prepareHeavyFiles, sealHeavyPart, wipe } from "./heavy-upload.js";
|
|
22
|
+
import { FILECOIN_CHAIN_FOR_NETWORK, FILECOIN_PROVIDERS } from "./shared/lib/filecoin/providers.js";
|
|
23
|
+
/** Copies one self-paid part may ask for, and the default. */
|
|
24
|
+
export const SELF_PAY_MAX_COPIES = 12;
|
|
25
|
+
export const SELF_PAY_DEFAULT_COPIES = 2;
|
|
26
|
+
/** `--copies` as typed. Absent = 2. Refused, never clamped. */
|
|
27
|
+
export function copiesOf(raw) {
|
|
28
|
+
if (raw === undefined || raw === "")
|
|
29
|
+
return SELF_PAY_DEFAULT_COPIES;
|
|
30
|
+
const value = typeof raw === "number" ? raw : Number(raw.trim());
|
|
31
|
+
if (!Number.isSafeInteger(value) || value < 1 || value > SELF_PAY_MAX_COPIES) {
|
|
32
|
+
throw new NmtsError(HEAVY_COPY.badCopies(String(raw)), { exitCode: 2 });
|
|
33
|
+
}
|
|
34
|
+
return value;
|
|
35
|
+
}
|
|
36
|
+
/** `--providers 4,9` as registry ids. Absent = the SDK chooses. */
|
|
37
|
+
export function providersOf(raw) {
|
|
38
|
+
if (raw === undefined || raw === "")
|
|
39
|
+
return undefined;
|
|
40
|
+
const parts = typeof raw === "string" ? raw.split(",").map((p) => p.trim()) : raw.map(String);
|
|
41
|
+
if (parts.length === 0 || parts.some((p) => !/^[0-9]+$/u.test(p))) {
|
|
42
|
+
throw new NmtsError(HEAVY_COPY.badProviders(String(raw)), { exitCode: 2 });
|
|
43
|
+
}
|
|
44
|
+
return parts.map((p) => BigInt(p));
|
|
45
|
+
}
|
|
46
|
+
/** Which Filecoin chain this NMTS network pays on — refused where no company is listed. */
|
|
47
|
+
export function selfPayChain(network) {
|
|
48
|
+
const chain = FILECOIN_CHAIN_FOR_NETWORK[network];
|
|
49
|
+
if (FILECOIN_PROVIDERS[chain].length === 0) {
|
|
50
|
+
throw new NmtsError(HEAVY_COPY.evmNoProviders(chain), { exitCode: 4 });
|
|
51
|
+
}
|
|
52
|
+
return chain;
|
|
53
|
+
}
|
|
54
|
+
/** The Synapse client for one account on one chain. `account` may be a bare address for reads. */
|
|
55
|
+
export async function synapseFor(account, chain) {
|
|
56
|
+
const sdk = await import("@filoz/synapse-sdk");
|
|
57
|
+
return sdk.Synapse.create({ account, source: "nmts", chain: chain === "mainnet" ? sdk.mainnet : sdk.calibration });
|
|
58
|
+
}
|
|
59
|
+
/** The real one: the Synapse SDK, over this account on this chain. */
|
|
60
|
+
export async function synapseSelfPay(account, chain) {
|
|
61
|
+
const synapse = await synapseFor(account, chain);
|
|
62
|
+
return {
|
|
63
|
+
async upload(bytes, options) {
|
|
64
|
+
const result = await synapse.storage.upload(bytes, {
|
|
65
|
+
copies: options.copies,
|
|
66
|
+
...(options.providerIds === undefined ? {} : { providerIds: options.providerIds }),
|
|
67
|
+
...(options.signal === undefined ? {} : { signal: options.signal }),
|
|
68
|
+
...(options.onProgress === undefined ? {} : { callbacks: { onProgress: options.onProgress } }),
|
|
69
|
+
});
|
|
70
|
+
return { pieceCid: result.pieceCid.toString(), copies: result.copies };
|
|
71
|
+
},
|
|
72
|
+
async runway() {
|
|
73
|
+
const summary = await synapse.payments.accountSummary();
|
|
74
|
+
return { epoch: summary.epoch, runwayInEpochs: summary.runwayInEpochs };
|
|
75
|
+
},
|
|
76
|
+
};
|
|
77
|
+
}
|
|
78
|
+
/** Filecoin epochs in 30 days — the lockup a storage company is always owed. */
|
|
79
|
+
const EPOCHS_PER_MONTH = 86400n;
|
|
80
|
+
/**
|
|
81
|
+
* The epoch the deposit reaches at the rate the account pays after this upload.
|
|
82
|
+
*
|
|
83
|
+
* ⚠ A RATE OF ZERO HAS NO END (the SDK answers the largest uint256). That only happens when no
|
|
84
|
+
* piece is being paid for, which after a successful upload it is; the 30-day lockup is written
|
|
85
|
+
* then, because it is what the storage company is owed whatever the deposit does.
|
|
86
|
+
*/
|
|
87
|
+
export function expiryFrom(runway) {
|
|
88
|
+
const end = runway.runwayInEpochs > 2n ** 40n ? runway.epoch + EPOCHS_PER_MONTH : runway.epoch + runway.runwayInEpochs;
|
|
89
|
+
return Number(end);
|
|
90
|
+
}
|
|
91
|
+
/** Upload these files from the key's own EVM wallet and commit each one. The caller writes the list. */
|
|
92
|
+
export async function heavySelfPut(ctx, files) {
|
|
93
|
+
const chain = selfPayChain(ctx.network);
|
|
94
|
+
const synapse = ctx.synapse ?? (await synapseSelfPay(ctx.account, chain));
|
|
95
|
+
const prepared = await prepareHeavyFiles(ctx.crypt, ctx.dataKey, ctx.rule, files);
|
|
96
|
+
try {
|
|
97
|
+
const partsPerFile = [];
|
|
98
|
+
const copyCounts = [];
|
|
99
|
+
for (const [fileIndex, one] of prepared.entries()) {
|
|
100
|
+
const parts = [];
|
|
101
|
+
for (const part of one.plan) {
|
|
102
|
+
const at = { fileIndex, partIndex: part.partIndex, parts: one.plan.length };
|
|
103
|
+
ctx.onProgress?.({ phase: "sealing", ...at });
|
|
104
|
+
const bytes = await sealHeavyPart(ctx.crypt, one, part);
|
|
105
|
+
ctx.onProgress?.({ phase: "uploading", ...at });
|
|
106
|
+
const stored = await synapse.upload(bytes, {
|
|
107
|
+
copies: ctx.copies,
|
|
108
|
+
...(ctx.providers === undefined ? {} : { providerIds: [...ctx.providers] }),
|
|
109
|
+
...(ctx.signal === undefined ? {} : { signal: ctx.signal }),
|
|
110
|
+
onProgress: (sent) => ctx.onProgress?.({ phase: "sent", ...at, sentBytes: sent, totalBytes: bytes.byteLength }),
|
|
111
|
+
});
|
|
112
|
+
if (stored.copies.length === 0)
|
|
113
|
+
throw new NmtsError(HEAVY_COPY.evmNoCopy(stored.pieceCid), { exitCode: 1 });
|
|
114
|
+
copyCounts.push(stored.copies.length);
|
|
115
|
+
parts.push({
|
|
116
|
+
part_index: part.partIndex,
|
|
117
|
+
storage_kind: 0,
|
|
118
|
+
network: 1,
|
|
119
|
+
blob_id: stored.pieceCid,
|
|
120
|
+
sealed_len: part.sealedLen,
|
|
121
|
+
owner_kind: 0,
|
|
122
|
+
expiry_epoch: 0,
|
|
123
|
+
copies: stored.copies.map((c) => ({
|
|
124
|
+
provider_id: c.providerId.toString(),
|
|
125
|
+
data_set_id: c.dataSetId.toString(),
|
|
126
|
+
piece_id: c.pieceId.toString(),
|
|
127
|
+
retrieval_url: c.retrievalUrl,
|
|
128
|
+
})),
|
|
129
|
+
});
|
|
130
|
+
ctx.onProgress?.({ phase: "stored", ...at });
|
|
131
|
+
}
|
|
132
|
+
partsPerFile.push(parts);
|
|
133
|
+
}
|
|
134
|
+
// ⛔ READ AFTER EVERY PART IS IN: the new pieces raise the rate, and the rate sets the end.
|
|
135
|
+
const expiryEpoch = expiryFrom(await synapse.runway());
|
|
136
|
+
const commit = ctx.commit ?? commitHeavyItem;
|
|
137
|
+
const committed = [];
|
|
138
|
+
for (const [index, one] of prepared.entries()) {
|
|
139
|
+
const parts = (partsPerFile[index] ?? []).map((p) => ({ ...p, expiry_epoch: expiryEpoch }));
|
|
140
|
+
const itemId = await commit({
|
|
141
|
+
server: ctx.server,
|
|
142
|
+
bearer: ctx.bearer,
|
|
143
|
+
// The pieces name the file: the same pieces committed again are the same file.
|
|
144
|
+
idempotencyKey: `nmts-heavy-self-${bytesToHex(sha256(new TextEncoder().encode(parts.map((p) => p.blob_id).join(",")))).slice(0, 40)}`,
|
|
145
|
+
dekWrapped: one.secrets.dekWrapped,
|
|
146
|
+
contentHashCt: one.secrets.contentHashCt,
|
|
147
|
+
parts,
|
|
148
|
+
paidBy: ctx.account.address,
|
|
149
|
+
});
|
|
150
|
+
committed.push({
|
|
151
|
+
itemId,
|
|
152
|
+
name: one.file.name,
|
|
153
|
+
parentId: one.file.parentId,
|
|
154
|
+
destination: one.file.destination,
|
|
155
|
+
plaintextLen: one.file.source.size,
|
|
156
|
+
dekWrapped: one.secrets.dekWrapped,
|
|
157
|
+
contentHashCt: one.secrets.contentHashCt,
|
|
158
|
+
sealedBytes: one.plan.reduce((sum, p) => sum + p.sealedLen, 0),
|
|
159
|
+
parts: one.plan.length,
|
|
160
|
+
});
|
|
161
|
+
}
|
|
162
|
+
return { files: committed, expiryEpoch, paidBy: ctx.account.address, copies: copyCounts };
|
|
163
|
+
}
|
|
164
|
+
finally {
|
|
165
|
+
wipe(prepared);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import type { CryptoGlue } from "./crypto.ts";
|
|
2
|
+
import { commitHeavyItem } from "./heavy-api.ts";
|
|
3
|
+
import { type FileSecrets } from "./seal.ts";
|
|
4
|
+
import type { HeavyOrderApi } from "./shared/lib/api/types-heavy.ts";
|
|
5
|
+
import { type PaddingRule } from "./shared/lib/crypto/size-padding.ts";
|
|
6
|
+
import { type HeavyPayment, type HeavyProgress, type HeavyRunDeps, type HeavyRunResult } from "./shared/lib/heavy/order-runner.ts";
|
|
7
|
+
import { type PlaintextSource } from "./upload-file.ts";
|
|
8
|
+
/** One file of a Heavy upload, and where it goes in the drive. */
|
|
9
|
+
export interface HeavyFile {
|
|
10
|
+
source: PlaintextSource;
|
|
11
|
+
name: string;
|
|
12
|
+
parentId: string | null;
|
|
13
|
+
/** The destination as typed — carried back for the caller's own messages. */
|
|
14
|
+
destination: string;
|
|
15
|
+
}
|
|
16
|
+
/** One part of a Heavy file: where it is in the plaintext, what it seals from, what it seals to. */
|
|
17
|
+
export interface HeavyPlanPart {
|
|
18
|
+
partIndex: number;
|
|
19
|
+
offset: number;
|
|
20
|
+
length: number;
|
|
21
|
+
/** The plaintext length the stream declares: the last part padded, and never under 39 bytes. */
|
|
22
|
+
sealFrom: number;
|
|
23
|
+
sealedLen: number;
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* How a file is cut for Heavy: 512 MiB parts, the last one padded by the account's own rule —
|
|
27
|
+
* exactly as Standard pads — and raised to the 39 bytes a legal piece needs.
|
|
28
|
+
*
|
|
29
|
+
* ⛔ ONLY THE LAST PART IS PADDED, for the reason `upload-file.ts` gives: a reader recovers where the
|
|
30
|
+
* padding is from the file's size, and that answer is unique only while every earlier part is full.
|
|
31
|
+
*/
|
|
32
|
+
export declare function planHeavyFile(size: number, rule: PaddingRule): HeavyPlanPart[];
|
|
33
|
+
/** What a credit-paid Heavy upload of these sizes costs: every slot is charged on its own, as the server does —
|
|
34
|
+
* half the Standard price, rounded up, at least 1 per slot — Heavy credits are priced for people, not at cost. */
|
|
35
|
+
export declare function heavyCredits(parts: readonly HeavyPlanPart[]): number;
|
|
36
|
+
/** One file, read once for its digest, with its key made and its parts planned. */
|
|
37
|
+
export interface PreparedHeavyFile {
|
|
38
|
+
file: HeavyFile;
|
|
39
|
+
plan: HeavyPlanPart[];
|
|
40
|
+
secrets: FileSecrets;
|
|
41
|
+
}
|
|
42
|
+
/** Read each file once for its content digest, and make its key. ⛔ The caller wipes `secrets.dek`. */
|
|
43
|
+
export declare function prepareHeavyFiles(crypt: CryptoGlue, dataKey: Uint8Array, rule: PaddingRule, files: readonly HeavyFile[]): Promise<PreparedHeavyFile[]>;
|
|
44
|
+
export declare function wipe(prepared: readonly PreparedHeavyFile[]): void;
|
|
45
|
+
/** Seal one part of a prepared file. A fresh stream, so a fresh nonce, every call. */
|
|
46
|
+
export declare function sealHeavyPart(crypt: CryptoGlue, prepared: PreparedHeavyFile, part: HeavyPlanPart): Promise<Uint8Array<ArrayBuffer>>;
|
|
47
|
+
/** The PieceCID, from the Filecoin tooling's own library — loaded only now, never at start-up. */
|
|
48
|
+
export declare function pieceCidOfBytes(bytes: Blob | Uint8Array): Promise<string>;
|
|
49
|
+
export interface HeavyOrderContext {
|
|
50
|
+
server: string;
|
|
51
|
+
bearer: string;
|
|
52
|
+
crypt: CryptoGlue;
|
|
53
|
+
/** The account's data key. Borrowed — the caller wipes it. */
|
|
54
|
+
dataKey: Uint8Array;
|
|
55
|
+
rule: PaddingRule;
|
|
56
|
+
onProgress?: ((event: HeavyProgress) => void) | undefined;
|
|
57
|
+
signal?: AbortSignal | undefined;
|
|
58
|
+
/** The key an order was already opened under (to show its price first); absent = a fresh one. */
|
|
59
|
+
idempotencyKey?: string | undefined;
|
|
60
|
+
/** ⚠ Seams, not options: the order routes and the runner's edges, for tests. */
|
|
61
|
+
api?: HeavyOrderApi | undefined;
|
|
62
|
+
deps?: Partial<Omit<HeavyRunDeps, "api" | "seal">> | undefined;
|
|
63
|
+
commit?: typeof commitHeavyItem | undefined;
|
|
64
|
+
}
|
|
65
|
+
/** One committed Heavy file: what the caller writes into the sealed list. */
|
|
66
|
+
export interface HeavyCommitted {
|
|
67
|
+
itemId: string;
|
|
68
|
+
name: string;
|
|
69
|
+
parentId: string | null;
|
|
70
|
+
destination: string;
|
|
71
|
+
plaintextLen: number;
|
|
72
|
+
dekWrapped: string;
|
|
73
|
+
contentHashCt: string;
|
|
74
|
+
sealedBytes: number;
|
|
75
|
+
parts: number;
|
|
76
|
+
}
|
|
77
|
+
export interface HeavyOrderOutcome {
|
|
78
|
+
run: HeavyRunResult;
|
|
79
|
+
files: HeavyCommitted[];
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Upload these files as ONE Heavy order and commit each of them.
|
|
83
|
+
*
|
|
84
|
+
* ⛔ IT DOES NOT WRITE THE SEALED LIST — the caller does, before anything else, exactly as with
|
|
85
|
+
* `uploadFile`. A committed file the list does not name is invisible to the person.
|
|
86
|
+
*/
|
|
87
|
+
export declare function heavyOrderPut(ctx: HeavyOrderContext, files: readonly HeavyFile[], payment: HeavyPayment): Promise<HeavyOrderOutcome>;
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
// NMTS Heavy, paid through an ORDER — credits, or WAL from the wallet once everything is stored.
|
|
2
|
+
// Files in, sealed here, placed and uploaded by the shared order runner, committed as files.
|
|
3
|
+
//
|
|
4
|
+
// ⛔ THE ORDER OF THINGS IS THE SHARED RUNNER'S (`shared/lib/heavy/order-runner.ts`), byte for byte
|
|
5
|
+
// the browser's: open the order, per slot seal → PieceCID → place → upload → wait until stored,
|
|
6
|
+
// then pay when the wallet pays. This file supplies the edges — the sealer, the PieceCID, the
|
|
7
|
+
// upload through this package's `fetch`, the api — and commits each file afterwards.
|
|
8
|
+
//
|
|
9
|
+
// ⛔ ONE KEY PER FILE, MADE ONCE AND HELD FOR THE RUN. Every part of a file is sealed under it, and it
|
|
10
|
+
// is wiped on every way out. There is no resume across runs: sealing is not deterministic, so a
|
|
11
|
+
// second run is a second order (the server removes an unbound order's pieces after a day and
|
|
12
|
+
// gives back the credits for slots that were never stored).
|
|
13
|
+
//
|
|
14
|
+
// ⛔ THE BYTES NEVER PASS THROUGH THE SERVER. They go from this process to the storage company the
|
|
15
|
+
// server named, and the company checks them against the PieceCID computed here.
|
|
16
|
+
//
|
|
17
|
+
// ⛔ NO `node:` IMPORT. The SDK's browser entry reaches this file through `portable.ts`.
|
|
18
|
+
import { sha256 } from "@noble/hashes/sha2.js";
|
|
19
|
+
import { NmtsError } from "./errors.js";
|
|
20
|
+
import { commitHeavyItem, createHeavyApi } from "./heavy-api.js";
|
|
21
|
+
import { reachFetch } from "./reach.js";
|
|
22
|
+
import { fileSecrets, NCF3_SHAPE, sealedLenFor, sealPart } from "./seal.js";
|
|
23
|
+
import { paddedPlaintextLen } from "./shared/lib/crypto/size-padding.js";
|
|
24
|
+
import { HEAVY_MIN_SEAL_FROM_BYTES, HEAVY_PART_SIZE_BYTES, runHeavyOrder, } from "./shared/lib/heavy/order-runner.js";
|
|
25
|
+
import { uploadPieceToProvider } from "./shared/lib/heavy/sp-upload.js";
|
|
26
|
+
import { planParts } from "./shared/lib/upload/part-plan.js";
|
|
27
|
+
import { padded } from "./upload-file.js";
|
|
28
|
+
import { CREDIT_BYTES, creditsFor } from "./upload-price.js";
|
|
29
|
+
/**
|
|
30
|
+
* How a file is cut for Heavy: 512 MiB parts, the last one padded by the account's own rule —
|
|
31
|
+
* exactly as Standard pads — and raised to the 39 bytes a legal piece needs.
|
|
32
|
+
*
|
|
33
|
+
* ⛔ ONLY THE LAST PART IS PADDED, for the reason `upload-file.ts` gives: a reader recovers where the
|
|
34
|
+
* padding is from the file's size, and that answer is unique only while every earlier part is full.
|
|
35
|
+
*/
|
|
36
|
+
export function planHeavyFile(size, rule) {
|
|
37
|
+
if (!Number.isSafeInteger(size) || size <= 0) {
|
|
38
|
+
throw new NmtsError("An empty file cannot be uploaded.", {
|
|
39
|
+
nextStep: "The storage network has nothing to store and would refuse the reservation.",
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
const plan = planParts(size, HEAVY_PART_SIZE_BYTES);
|
|
43
|
+
return plan.map((range) => {
|
|
44
|
+
const isLast = range.partIndex === plan.length - 1;
|
|
45
|
+
const rounded = isLast
|
|
46
|
+
? paddedPlaintextLen(range.length, rule, { unitBytes: CREDIT_BYTES, shape: NCF3_SHAPE })
|
|
47
|
+
: range.length;
|
|
48
|
+
const sealFrom = Math.max(rounded, HEAVY_MIN_SEAL_FROM_BYTES);
|
|
49
|
+
return { partIndex: range.partIndex, offset: range.offset, length: range.length, sealFrom, sealedLen: sealedLenFor(sealFrom) };
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/** What a credit-paid Heavy upload of these sizes costs: every slot is charged on its own, as the server does —
|
|
53
|
+
* half the Standard price, rounded up, at least 1 per slot — Heavy credits are priced for people, not at cost. */
|
|
54
|
+
export function heavyCredits(parts) {
|
|
55
|
+
return parts.reduce((sum, part) => sum + Math.max(1, Math.ceil(creditsFor(part.sealedLen) / 2)), 0);
|
|
56
|
+
}
|
|
57
|
+
/** Read each file once for its content digest, and make its key. ⛔ The caller wipes `secrets.dek`. */
|
|
58
|
+
export async function prepareHeavyFiles(crypt, dataKey, rule, files) {
|
|
59
|
+
const out = [];
|
|
60
|
+
try {
|
|
61
|
+
for (const file of files) {
|
|
62
|
+
const plan = planHeavyFile(file.source.size, rule);
|
|
63
|
+
const digest = sha256.create();
|
|
64
|
+
for await (const chunk of file.source.read(0, file.source.size))
|
|
65
|
+
digest.update(chunk);
|
|
66
|
+
const hashed = digest.digest();
|
|
67
|
+
out.push({ file, plan, secrets: fileSecrets(crypt, dataKey, hashed) });
|
|
68
|
+
hashed.fill(0);
|
|
69
|
+
}
|
|
70
|
+
return out;
|
|
71
|
+
}
|
|
72
|
+
catch (error) {
|
|
73
|
+
wipe(out);
|
|
74
|
+
throw error;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
export function wipe(prepared) {
|
|
78
|
+
for (const one of prepared)
|
|
79
|
+
one.secrets.dek.fill(0);
|
|
80
|
+
}
|
|
81
|
+
/** Seal one part of a prepared file. A fresh stream, so a fresh nonce, every call. */
|
|
82
|
+
export function sealHeavyPart(crypt, prepared, part) {
|
|
83
|
+
return sealPart(crypt, prepared.secrets.dek, padded(prepared.file.source.read(part.offset, part.length), part.length, part.sealFrom), { index: part.partIndex, total: prepared.plan.length, plaintextLen: part.sealFrom });
|
|
84
|
+
}
|
|
85
|
+
/** The PieceCID, from the Filecoin tooling's own library — loaded only now, never at start-up. */
|
|
86
|
+
export async function pieceCidOfBytes(bytes) {
|
|
87
|
+
const { pieceCidOf } = await import("./shared/lib/heavy/piece-cid.js");
|
|
88
|
+
return pieceCidOf(bytes);
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Upload these files as ONE Heavy order and commit each of them.
|
|
92
|
+
*
|
|
93
|
+
* ⛔ IT DOES NOT WRITE THE SEALED LIST — the caller does, before anything else, exactly as with
|
|
94
|
+
* `uploadFile`. A committed file the list does not name is invisible to the person.
|
|
95
|
+
*/
|
|
96
|
+
export async function heavyOrderPut(ctx, files, payment) {
|
|
97
|
+
const prepared = await prepareHeavyFiles(ctx.crypt, ctx.dataKey, ctx.rule, files);
|
|
98
|
+
try {
|
|
99
|
+
const byKey = new Map(prepared.map((p, index) => [`f${index}`, p]));
|
|
100
|
+
const seal = async (job) => {
|
|
101
|
+
const one = byKey.get(job.fileKey);
|
|
102
|
+
const part = one?.plan[job.partIndex];
|
|
103
|
+
if (one === undefined || part === undefined)
|
|
104
|
+
throw new NmtsError(`Slot ${job.slot} names no part of this upload.`);
|
|
105
|
+
return sealHeavyPart(ctx.crypt, one, part);
|
|
106
|
+
};
|
|
107
|
+
const run = await runHeavyOrder({
|
|
108
|
+
files: prepared.map((p, index) => ({
|
|
109
|
+
key: `f${index}`,
|
|
110
|
+
parts: p.plan.map((part) => ({ partIndex: part.partIndex, sealedLen: part.sealedLen })),
|
|
111
|
+
})),
|
|
112
|
+
payment,
|
|
113
|
+
...(ctx.idempotencyKey === undefined ? {} : { idempotencyKey: ctx.idempotencyKey }),
|
|
114
|
+
...(ctx.onProgress === undefined ? {} : { onProgress: ctx.onProgress }),
|
|
115
|
+
...(ctx.signal === undefined ? {} : { signal: ctx.signal }),
|
|
116
|
+
}, {
|
|
117
|
+
api: ctx.api ?? createHeavyApi(ctx.server, ctx.bearer),
|
|
118
|
+
seal,
|
|
119
|
+
pieceCid: pieceCidOfBytes,
|
|
120
|
+
upload: (input) => uploadPieceToProvider({ ...input, fetchImpl: reachFetch }),
|
|
121
|
+
...ctx.deps,
|
|
122
|
+
});
|
|
123
|
+
const commit = ctx.commit ?? commitHeavyItem;
|
|
124
|
+
const committed = [];
|
|
125
|
+
for (const [index, one] of prepared.entries()) {
|
|
126
|
+
const parts = run.files[index]?.parts;
|
|
127
|
+
if (parts === undefined)
|
|
128
|
+
throw new NmtsError(`The order came back without file ${index + 1}.`);
|
|
129
|
+
const itemId = await commit({
|
|
130
|
+
server: ctx.server,
|
|
131
|
+
bearer: ctx.bearer,
|
|
132
|
+
idempotencyKey: `nmts-heavy-${run.orderId}-${index}`,
|
|
133
|
+
dekWrapped: one.secrets.dekWrapped,
|
|
134
|
+
contentHashCt: one.secrets.contentHashCt,
|
|
135
|
+
parts,
|
|
136
|
+
});
|
|
137
|
+
committed.push({
|
|
138
|
+
itemId,
|
|
139
|
+
name: one.file.name,
|
|
140
|
+
parentId: one.file.parentId,
|
|
141
|
+
destination: one.file.destination,
|
|
142
|
+
plaintextLen: one.file.source.size,
|
|
143
|
+
dekWrapped: one.secrets.dekWrapped,
|
|
144
|
+
contentHashCt: one.secrets.contentHashCt,
|
|
145
|
+
sealedBytes: one.plan.reduce((sum, part) => sum + part.sealedLen, 0),
|
|
146
|
+
parts: one.plan.length,
|
|
147
|
+
});
|
|
148
|
+
}
|
|
149
|
+
return { run, files: committed };
|
|
150
|
+
}
|
|
151
|
+
finally {
|
|
152
|
+
wipe(prepared);
|
|
153
|
+
}
|
|
154
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { type HeavyFile, type HeavyOrderContext, type HeavyOrderOutcome } from "./heavy-upload.ts";
|
|
2
|
+
import type { Network } from "./network.ts";
|
|
3
|
+
import type { SendReads, TransferShape } from "./wallet-send-chain.ts";
|
|
4
|
+
/** What the opened order asks, and what the paying wallet holds against it. */
|
|
5
|
+
export interface HeavyWalletQuote {
|
|
6
|
+
orderId: string;
|
|
7
|
+
/** The Sui address that sends the WAL. */
|
|
8
|
+
address: string;
|
|
9
|
+
/** Where the WAL goes. */
|
|
10
|
+
treasury: string;
|
|
11
|
+
walFrost: bigint;
|
|
12
|
+
/** The transfer's measured fee in MIST, or null when it could not be measured. */
|
|
13
|
+
feeMist: bigint | null;
|
|
14
|
+
termDays: number;
|
|
15
|
+
}
|
|
16
|
+
export interface HeavyWalletInput {
|
|
17
|
+
code: string;
|
|
18
|
+
network: Network;
|
|
19
|
+
/** Which of this key's wallets pays. */
|
|
20
|
+
wallet: number;
|
|
21
|
+
/** 1..=365. */
|
|
22
|
+
termDays: number;
|
|
23
|
+
/** Told the quote as soon as it is known — before the checks, so a refusal follows what was shown. */
|
|
24
|
+
onQuote?: ((quote: HeavyWalletQuote) => void) | undefined;
|
|
25
|
+
/** Asked once the balances cover the quote, before anything is sealed; throwing stops the run. */
|
|
26
|
+
approve?: ((quote: HeavyWalletQuote) => void) | undefined;
|
|
27
|
+
/** Told once the WAL has left. */
|
|
28
|
+
onPaid?: ((spent: {
|
|
29
|
+
walFrost: bigint;
|
|
30
|
+
suiMist: bigint;
|
|
31
|
+
}) => void) | undefined;
|
|
32
|
+
/** ⚠ Seams, not options: the chain's reads and the transfer's signature, for tests. */
|
|
33
|
+
reads?: SendReads | undefined;
|
|
34
|
+
sign?: ((shape: TransferShape) => Promise<string>) | undefined;
|
|
35
|
+
}
|
|
36
|
+
/** Store `files` in one wallet-paid Heavy order. */
|
|
37
|
+
export declare function heavyWalletPut(ctx: HeavyOrderContext, files: readonly HeavyFile[], input: HeavyWalletInput): Promise<HeavyOrderOutcome & {
|
|
38
|
+
quote: HeavyWalletQuote;
|
|
39
|
+
}>;
|