@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,37 @@
|
|
|
1
|
+
/** The request function this module needs: `fetch`, or anything shaped like it. */
|
|
2
|
+
export type FetchLike = (input: string, init?: RequestInit) => Promise<Response>;
|
|
3
|
+
/**
|
|
4
|
+
* How the part's bytes are sent in step 2. The default is `fetch`, which cannot report how much of
|
|
5
|
+
* a request body has gone out; a browser passes one built on XMLHttpRequest, which can.
|
|
6
|
+
*/
|
|
7
|
+
export type PutBody = (input: {
|
|
8
|
+
url: string;
|
|
9
|
+
body: Blob | Uint8Array<ArrayBuffer>;
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
onProgress?: (sentBytes: number, totalBytes: number) => void;
|
|
12
|
+
}) => Promise<Response>;
|
|
13
|
+
/** Why an upload did not complete, as a machine-readable word. */
|
|
14
|
+
export type PieceUploadFailure = "not_https" | "size_out_of_range" | "session_refused" | "location_missing" | "upload_refused" | "finalize_refused";
|
|
15
|
+
export declare class PieceUploadError extends Error {
|
|
16
|
+
readonly reason: PieceUploadFailure;
|
|
17
|
+
/** The HTTP status that ended it, when a response was the reason. */
|
|
18
|
+
readonly status: number | null;
|
|
19
|
+
constructor(reason: PieceUploadFailure, message: string, status?: number | null);
|
|
20
|
+
}
|
|
21
|
+
export interface UploadPieceInput {
|
|
22
|
+
/** The company's service origin, as the order's `target` answered it (`https://…`). */
|
|
23
|
+
serviceUrl: string;
|
|
24
|
+
/** The sealed part, exactly the bytes its PieceCID was computed over. */
|
|
25
|
+
bytes: Blob | Uint8Array<ArrayBuffer>;
|
|
26
|
+
/** The part's PieceCID (`bafkzcib…`). The company refuses the upload if the bytes do not match. */
|
|
27
|
+
pieceCid: string;
|
|
28
|
+
signal?: AbortSignal;
|
|
29
|
+
/** Bytes of the part sent so far, and the part's size. */
|
|
30
|
+
onProgress?: (sentBytes: number, totalBytes: number) => void;
|
|
31
|
+
/** Requests for steps 1 and 3 (and step 2 when `put` is absent). Default: the global `fetch`. */
|
|
32
|
+
fetchImpl?: FetchLike;
|
|
33
|
+
/** Step 2's transport. Default: `fetchImpl`, with progress reported only at the end. */
|
|
34
|
+
put?: PutBody;
|
|
35
|
+
}
|
|
36
|
+
/** Upload one part and have the company accept it under `pieceCid`. Resolves once step 3 answers 2xx. */
|
|
37
|
+
export declare function uploadPieceToProvider(input: UploadPieceInput): Promise<void>;
|
|
@@ -0,0 +1,112 @@
|
|
|
1
|
+
// Upload one sealed part to a Filecoin storage company, straight from this device.
|
|
2
|
+
//
|
|
3
|
+
// ⚠ THIS FILE IS PUBLISHED. It is copied byte-for-byte into the `nmts` command-line package.
|
|
4
|
+
// Keep the comments self-contained English, with no pointer only we can follow.
|
|
5
|
+
//
|
|
6
|
+
// THE PROTOCOL — three plain HTTP calls to the company's storage service, no credentials, open CORS:
|
|
7
|
+
// 1. POST <service>/pdp/piece/uploads → 201, `Location: /pdp/piece/uploads/<uuid>`
|
|
8
|
+
// 2. PUT <service>/pdp/piece/uploads/<uuid> (the bytes) → 204
|
|
9
|
+
// 3. POST <service>/pdp/piece/uploads/<uuid> {"pieceCid": "<bafkzcib…>"} → 200
|
|
10
|
+
// This is the same exchange `uploadPieceStreaming` in `@filoz/synapse-core/sp` performs. We write the
|
|
11
|
+
// three calls ourselves because importing that function also loads the library's error decoder,
|
|
12
|
+
// which carries every contract ABI the tooling knows — measured 2026-09-23 with esbuild: about
|
|
13
|
+
// 160 KB minified, of which 98 KB is that ABI table, for three requests that need none of it.
|
|
14
|
+
//
|
|
15
|
+
// ⛔ THE COMPANY IS UNTRUSTED, AND THAT IS FINE. The bytes are sealed (end-to-end encrypted) before
|
|
16
|
+
// they get here, and the company checks them against the PieceCID we name in step 3. Nothing a
|
|
17
|
+
// company does with this exchange can reveal a file or make the wrong bytes open as the right one.
|
|
18
|
+
//
|
|
19
|
+
// ⛔ NO RETRY IN HERE. Each attempt is a fresh upload session; the caller decides whether a failed
|
|
20
|
+
// attempt is worth another (`order-runner.ts` does). A half-finished session is simply abandoned.
|
|
21
|
+
export class PieceUploadError extends Error {
|
|
22
|
+
reason;
|
|
23
|
+
/** The HTTP status that ended it, when a response was the reason. */
|
|
24
|
+
status;
|
|
25
|
+
constructor(reason, message, status = null) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.name = "PieceUploadError";
|
|
28
|
+
this.reason = reason;
|
|
29
|
+
this.status = status;
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
/** The bytes one piece may hold, at least and at most — the storage companies' own limits. */
|
|
33
|
+
const MIN_BYTES = 127;
|
|
34
|
+
const MAX_BYTES = 1_065_353_216;
|
|
35
|
+
/** `<service>/<path>`, whether or not the service origin was given with a trailing slash or a path. */
|
|
36
|
+
function endpoint(serviceUrl, path) {
|
|
37
|
+
const base = serviceUrl.endsWith("/") ? serviceUrl : `${serviceUrl}/`;
|
|
38
|
+
return new URL(path, base).toString();
|
|
39
|
+
}
|
|
40
|
+
function sizeOf(bytes) {
|
|
41
|
+
return bytes instanceof Uint8Array ? bytes.byteLength : bytes.size;
|
|
42
|
+
}
|
|
43
|
+
/** Upload one part and have the company accept it under `pieceCid`. Resolves once step 3 answers 2xx. */
|
|
44
|
+
export async function uploadPieceToProvider(input) {
|
|
45
|
+
const { serviceUrl, bytes, pieceCid, signal, onProgress } = input;
|
|
46
|
+
const fetchImpl = input.fetchImpl ?? ((url, init) => fetch(url, init));
|
|
47
|
+
let service;
|
|
48
|
+
try {
|
|
49
|
+
service = new URL(serviceUrl);
|
|
50
|
+
}
|
|
51
|
+
catch {
|
|
52
|
+
// An unparsable address is the same refusal as a non-https one: nothing is sent to it.
|
|
53
|
+
throw new PieceUploadError("not_https", `The storage service address "${serviceUrl}" is not a URL.`);
|
|
54
|
+
}
|
|
55
|
+
if (service.protocol !== "https:") {
|
|
56
|
+
throw new PieceUploadError("not_https", `The storage service must be https, got ${service.protocol}`);
|
|
57
|
+
}
|
|
58
|
+
const total = sizeOf(bytes);
|
|
59
|
+
if (total < MIN_BYTES || total > MAX_BYTES) {
|
|
60
|
+
throw new PieceUploadError("size_out_of_range", `A piece must be ${MIN_BYTES}..${MAX_BYTES} bytes; this part is ${total}.`);
|
|
61
|
+
}
|
|
62
|
+
const withSignal = signal === undefined ? {} : { signal };
|
|
63
|
+
// 1. Open an upload session.
|
|
64
|
+
const created = await fetchImpl(endpoint(serviceUrl, "pdp/piece/uploads"), {
|
|
65
|
+
method: "POST",
|
|
66
|
+
...withSignal,
|
|
67
|
+
});
|
|
68
|
+
if (created.status !== 201) {
|
|
69
|
+
throw new PieceUploadError("session_refused", `The storage service did not open an upload (HTTP ${created.status}).`, created.status);
|
|
70
|
+
}
|
|
71
|
+
// ⚠ Readable across origins only when the company exposes it (`Access-Control-Expose-Headers`).
|
|
72
|
+
const location = created.headers.get("Location");
|
|
73
|
+
const uuid = location === null ? null : /\/pdp\/piece\/uploads\/([A-Fa-f0-9-]+)/.exec(location)?.[1];
|
|
74
|
+
if (uuid === null || uuid === undefined) {
|
|
75
|
+
throw new PieceUploadError("location_missing", location === null
|
|
76
|
+
? "The storage service opened an upload but did not say where (no Location header)."
|
|
77
|
+
: `The storage service answered an upload address this client cannot read: ${location}`);
|
|
78
|
+
}
|
|
79
|
+
const sessionUrl = endpoint(serviceUrl, `pdp/piece/uploads/${uuid}`);
|
|
80
|
+
// 2. Send the bytes.
|
|
81
|
+
const put = input.put ??
|
|
82
|
+
(async (req) => {
|
|
83
|
+
const res = await fetchImpl(req.url, {
|
|
84
|
+
method: "PUT",
|
|
85
|
+
headers: { "Content-Type": "application/octet-stream" },
|
|
86
|
+
body: req.body,
|
|
87
|
+
...(req.signal === undefined ? {} : { signal: req.signal }),
|
|
88
|
+
});
|
|
89
|
+
if (res.ok)
|
|
90
|
+
req.onProgress?.(total, total);
|
|
91
|
+
return res;
|
|
92
|
+
});
|
|
93
|
+
const sent = await put({
|
|
94
|
+
url: sessionUrl,
|
|
95
|
+
body: bytes,
|
|
96
|
+
...withSignal,
|
|
97
|
+
...(onProgress === undefined ? {} : { onProgress }),
|
|
98
|
+
});
|
|
99
|
+
if (!sent.ok) {
|
|
100
|
+
throw new PieceUploadError("upload_refused", `The storage service refused the part's bytes (HTTP ${sent.status}).`, sent.status);
|
|
101
|
+
}
|
|
102
|
+
// 3. Name the bytes. The company recomputes the id from what it received and refuses a mismatch.
|
|
103
|
+
const finalized = await fetchImpl(sessionUrl, {
|
|
104
|
+
method: "POST",
|
|
105
|
+
headers: { "Content-Type": "application/json" },
|
|
106
|
+
body: JSON.stringify({ pieceCid }),
|
|
107
|
+
...withSignal,
|
|
108
|
+
});
|
|
109
|
+
if (!finalized.ok) {
|
|
110
|
+
throw new PieceUploadError("finalize_refused", `The storage service did not accept the part as ${pieceCid} (HTTP ${finalized.status}).`, finalized.status);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { calculate, transformStream, tryFrom } from "@filoz/synapse-core/piece";
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
// The three piece functions NMTS Heavy uses from `@filoz/synapse-core/piece`, named one by one.
|
|
2
|
+
//
|
|
3
|
+
// WHY A FILE OF ITS OWN. `piece-cid.ts` loads the library lazily, and a lazy `import()` of the
|
|
4
|
+
// library's own entry keeps EVERY export of it alive in the bundle — measured 2026-09-23 with
|
|
5
|
+
// esbuild: about 500 KB minified, because the entry also carries the download and URL helpers and
|
|
6
|
+
// what they import. Lazily importing THIS file instead lets the bundler keep only these three
|
|
7
|
+
// (about 27 KB minified), and the library still loads only when a part is actually hashed.
|
|
8
|
+
export { calculate, transformStream, tryFrom } from "@filoz/synapse-core/piece";
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
/** Wire values of the handover file. Fixed forever once a file carries them. */
|
|
2
|
+
export declare const HANDOVER_FORMAT = "nmts-handover";
|
|
3
|
+
export declare const HANDOVER_VERSION = 1;
|
|
4
|
+
/** Marker inside the sealed parts document. */
|
|
5
|
+
export declare const HANDOVER_PARTS_FORMAT = "nmts-handover-parts/1";
|
|
6
|
+
/** Marker of the name document — the §5.4 share document, which a handover extends by two keys. */
|
|
7
|
+
export declare const SHARE_FILE_FORMAT = "nmts-share-file/1";
|
|
8
|
+
/** Wire values of the public code file. */
|
|
9
|
+
export declare const PUBLIC_CODE_FORMAT = "nmts-public-code";
|
|
10
|
+
export declare const PUBLIC_CODE_VERSION = 1;
|
|
11
|
+
/** File name endings. */
|
|
12
|
+
export declare const HANDOVER_EXTENSION = ".nmtshandover";
|
|
13
|
+
export declare const PUBLIC_CODE_EXTENSION = ".nmtscode";
|
|
14
|
+
/**
|
|
15
|
+
* What a person who opens the file in a text editor reads. Informational; not read back.
|
|
16
|
+
* The two place names are the labels on the screen: the side panel entry and the button.
|
|
17
|
+
*/
|
|
18
|
+
export declare const HANDOVER_NOTE: readonly [string, string];
|
|
19
|
+
/** Size caps on the whole text, checked before it is parsed (§5.6, §5.7). */
|
|
20
|
+
export declare const MAX_HANDOVER_FILE_BYTES: number;
|
|
21
|
+
export declare const MAX_PUBLIC_CODE_FILE_BYTES: number;
|
|
22
|
+
/** Field bounds (§5.6). Together they keep a file under MAX_HANDOVER_FILE_BYTES. */
|
|
23
|
+
export declare const MAX_NAME_SEALED_LEN: number;
|
|
24
|
+
export type HandoverNetwork = "mainnet" | "testnet";
|
|
25
|
+
/** One handover file, every binary value still in its base64url text form. */
|
|
26
|
+
export interface HandoverFile {
|
|
27
|
+
network: HandoverNetwork;
|
|
28
|
+
/** The item id, lowercase, exactly as bound into the envelope. */
|
|
29
|
+
item: string;
|
|
30
|
+
/** The sender's published identity (4989 bytes). */
|
|
31
|
+
sender: string;
|
|
32
|
+
/** The share envelope (1240 bytes); its first 16 bytes are the sender's address. */
|
|
33
|
+
envelope: string;
|
|
34
|
+
/** The name document sealed under the file key. */
|
|
35
|
+
name: string;
|
|
36
|
+
/** The whole-file digest sealed under the file key (104 bytes). */
|
|
37
|
+
hash: string;
|
|
38
|
+
/** The parts document sealed under the file key. */
|
|
39
|
+
parts: string;
|
|
40
|
+
}
|
|
41
|
+
/**
|
|
42
|
+
* The name document a handover seals: the §5.4 share document plus the two values that bind the
|
|
43
|
+
* rest of the file to the envelope. `payload_cmt` covers the sealed name, so these two are covered.
|
|
44
|
+
*/
|
|
45
|
+
export interface HandoverName {
|
|
46
|
+
name: string;
|
|
47
|
+
/** The file's real length. Required here, unlike a built-in share's document. */
|
|
48
|
+
size: number;
|
|
49
|
+
network: HandoverNetwork;
|
|
50
|
+
/** SHA-256 of the sealed parts value (its bytes, not its text), unpadded base64url. */
|
|
51
|
+
partsSha256: string;
|
|
52
|
+
}
|
|
53
|
+
/** One stored piece of the file, in order. A quilt patch names `patch`; a whole blob names `blob`. */
|
|
54
|
+
export interface HandoverPart {
|
|
55
|
+
blob: string | null;
|
|
56
|
+
patch: string | null;
|
|
57
|
+
/** Bytes the sealed stream occupies. */
|
|
58
|
+
len: number;
|
|
59
|
+
/**
|
|
60
|
+
* The piece's end epoch as the sender's drive recorded it when the file was made — exclusive:
|
|
61
|
+
* the storage has ended once the current epoch reaches it. A hint, not a promise (the sender may
|
|
62
|
+
* extend or delete the storage later); 0 means the drive did not know it.
|
|
63
|
+
*/
|
|
64
|
+
exp: number;
|
|
65
|
+
}
|
|
66
|
+
/** The public code file. */
|
|
67
|
+
export interface PublicCodeFile {
|
|
68
|
+
/** The public code in the form a person reads. */
|
|
69
|
+
code: string;
|
|
70
|
+
/** The published identity (4989 bytes), base64url. */
|
|
71
|
+
identity: string;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Why a file was refused:
|
|
75
|
+
* too-large — over the size cap; not parsed at all
|
|
76
|
+
* not-json — it is not a JSON object
|
|
77
|
+
* wrong-format — it is JSON, but not this kind of file
|
|
78
|
+
* unknown-version — this kind of file, with a version number above the one this reader knows
|
|
79
|
+
* other-network — a handover file made on the other network
|
|
80
|
+
* bad-field — the right kind and version, but a field (or `version` itself) is wrong
|
|
81
|
+
*/
|
|
82
|
+
export type HandoverFormatProblem = "too-large" | "not-json" | "wrong-format" | "unknown-version" | "other-network" | "bad-field";
|
|
83
|
+
export declare class HandoverFormatError extends Error {
|
|
84
|
+
readonly problem: HandoverFormatProblem;
|
|
85
|
+
/** The field at fault, for `bad-field`. */
|
|
86
|
+
readonly field: string | null;
|
|
87
|
+
constructor(problem: HandoverFormatProblem, field?: string | null);
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Bytes behind an unpadded base64url string, or null when it is not one in CANONICAL form: the
|
|
91
|
+
* bits the last character carries beyond the data must be zero, so one byte string has one text.
|
|
92
|
+
*/
|
|
93
|
+
export declare function base64UrlByteLength(text: string): number | null;
|
|
94
|
+
/** `nmts-handover-<YYYY-MM-DD>.nmtshandover`, the date in UTC. The file's own name is not in it. */
|
|
95
|
+
export declare function handoverFileName(now: Date): string;
|
|
96
|
+
/** `nmts-public-code-<public code>.nmtscode`. The code is public, and it tells two such files apart. */
|
|
97
|
+
export declare function publicCodeFileName(code: string): string;
|
|
98
|
+
/** The handover file's text. Pretty-printed: somebody may well open it to see what it is. */
|
|
99
|
+
export declare function encodeHandover(file: HandoverFile): string;
|
|
100
|
+
/**
|
|
101
|
+
* Read a handover file's text, in the order §5.6 gives: size, JSON, format, version, network, key
|
|
102
|
+
* set, `note`, fields. `network` is the reader's own. Throws `HandoverFormatError`; never returns a
|
|
103
|
+
* partial file.
|
|
104
|
+
*/
|
|
105
|
+
export declare function decodeHandover(text: string, network: HandoverNetwork): HandoverFile;
|
|
106
|
+
/** The name document, exactly as it is sealed. */
|
|
107
|
+
export declare function encodeHandoverName(doc: HandoverName): string;
|
|
108
|
+
/** Whether a name document, once sealed, fits the `name` field. The caller checks before sealing. */
|
|
109
|
+
export declare function handoverNameFits(encoded: string): boolean;
|
|
110
|
+
/** Read an opened name document. Throws `HandoverFormatError` (`bad-field`, `name`). */
|
|
111
|
+
export declare function decodeHandoverName(text: string): HandoverName;
|
|
112
|
+
/** The parts document, exactly as it is sealed. Compact: nobody reads it but the recipient's engine. */
|
|
113
|
+
export declare function encodePartsList(parts: readonly HandoverPart[]): string;
|
|
114
|
+
/** Read an opened parts document. Throws `HandoverFormatError` (`bad-field`, `parts`). */
|
|
115
|
+
export declare function decodePartsList(text: string): HandoverPart[];
|
|
116
|
+
/**
|
|
117
|
+
* The smallest recorded end epoch, or 0 when any piece's is unknown (0). Exclusive, and recorded
|
|
118
|
+
* when the file was made — see `HandoverPart.exp`.
|
|
119
|
+
*/
|
|
120
|
+
export declare function earliestExpiry(parts: readonly HandoverPart[]): number;
|
|
121
|
+
/** The public code file's text. */
|
|
122
|
+
export declare function encodePublicCodeFile(file: PublicCodeFile): string;
|
|
123
|
+
/**
|
|
124
|
+
* Read a public code file's text. Throws `HandoverFormatError`.
|
|
125
|
+
*
|
|
126
|
+
* ⚠ This checks the SHAPE only. Whether `identity` really fingerprints to `code` is the engine's
|
|
127
|
+
* question, and a caller must ask it before sealing anything to the identity.
|
|
128
|
+
*/
|
|
129
|
+
export declare function decodePublicCodeFile(text: string): PublicCodeFile;
|
|
@@ -0,0 +1,323 @@
|
|
|
1
|
+
// The two files a person can pass around outside NMTS: the HANDOVER file, which carries one file
|
|
2
|
+
// sealed to one recipient, and the PUBLIC CODE file, which lets a sender seal to somebody without
|
|
3
|
+
// asking the server for their identity. The byte layouts are fixed by the format specification
|
|
4
|
+
// (NCF-3 §5.6 and §5.7); this module is the JSON around them, and nothing else.
|
|
5
|
+
//
|
|
6
|
+
// ⛔ NO CRYPTOGRAPHY HERE, AND NO IMPORTS. Everything that needs a key or a hash happens in the
|
|
7
|
+
// caller — the browser's crypto worker, or the command-line tool's engine. This module only
|
|
8
|
+
// builds and reads the text, so both programs refuse the same files for the same reasons. It is
|
|
9
|
+
// copied byte for byte into the command-line package.
|
|
10
|
+
//
|
|
11
|
+
// ⛔ IT REFUSES EXACTLY WHAT §5.6 AND §5.7 SAY A READER REFUSES, and nothing else. The specification
|
|
12
|
+
// lists every refusal (the size cap, the closed key sets, the `note` shape, the version rule, the
|
|
13
|
+
// network, the lengths, canonical base64url); a refusal added here and not there makes two
|
|
14
|
+
// readers of one published format disagree about one file.
|
|
15
|
+
//
|
|
16
|
+
// ⚠ THE ITEM ID IS COMPARED AS WRITTEN. §5.3 lowercases it before binding, so "ABC" and "abc" bind
|
|
17
|
+
// the same. A reader that accepted "ABC" would show text different from what was bound, so an item
|
|
18
|
+
// id that is not already lowercase is refused here.
|
|
19
|
+
/** Wire values of the handover file. Fixed forever once a file carries them. */
|
|
20
|
+
export const HANDOVER_FORMAT = "nmts-handover";
|
|
21
|
+
export const HANDOVER_VERSION = 1;
|
|
22
|
+
/** Marker inside the sealed parts document. */
|
|
23
|
+
export const HANDOVER_PARTS_FORMAT = "nmts-handover-parts/1";
|
|
24
|
+
/** Marker of the name document — the §5.4 share document, which a handover extends by two keys. */
|
|
25
|
+
export const SHARE_FILE_FORMAT = "nmts-share-file/1";
|
|
26
|
+
/** Wire values of the public code file. */
|
|
27
|
+
export const PUBLIC_CODE_FORMAT = "nmts-public-code";
|
|
28
|
+
export const PUBLIC_CODE_VERSION = 1;
|
|
29
|
+
/** File name endings. */
|
|
30
|
+
export const HANDOVER_EXTENSION = ".nmtshandover";
|
|
31
|
+
export const PUBLIC_CODE_EXTENSION = ".nmtscode";
|
|
32
|
+
/**
|
|
33
|
+
* What a person who opens the file in a text editor reads. Informational; not read back.
|
|
34
|
+
* The two place names are the labels on the screen: the side panel entry and the button.
|
|
35
|
+
*/
|
|
36
|
+
export const HANDOVER_NOTE = [
|
|
37
|
+
"An NMTS handover file. Sign in at nmts.me with the NMTS key it was made for, then open it from Shared with me → Open handover file.",
|
|
38
|
+
"NMTS 건네기 파일입니다. 이 파일을 받을 NMTS 키로 nmts.me에 로그인한 뒤 「받은 공유함」 → 「건네기 파일 열기」에서 여십시오.", // shown in Korean
|
|
39
|
+
];
|
|
40
|
+
/** Size caps on the whole text, checked before it is parsed (§5.6, §5.7). */
|
|
41
|
+
export const MAX_HANDOVER_FILE_BYTES = 1024 * 1024;
|
|
42
|
+
export const MAX_PUBLIC_CODE_FILE_BYTES = 8 * 1024;
|
|
43
|
+
/** Exact byte lengths the specification fixes. */
|
|
44
|
+
const IDENTITY_LEN = 4989;
|
|
45
|
+
const ENVELOPE_LEN = 1240;
|
|
46
|
+
const DIGEST_ENVELOPE_LEN = 104;
|
|
47
|
+
const SHA256_LEN = 32;
|
|
48
|
+
/** nonce(24) + commitment(32) + tag(16): the smallest sealed value, with an empty plaintext. */
|
|
49
|
+
const MIN_SEALED_LEN = 72;
|
|
50
|
+
/** Field bounds (§5.6). Together they keep a file under MAX_HANDOVER_FILE_BYTES. */
|
|
51
|
+
export const MAX_NAME_SEALED_LEN = 64 * 1024;
|
|
52
|
+
const MAX_PARTS_SEALED_LEN = 640 * 1024;
|
|
53
|
+
const MAX_PARTS = 4096;
|
|
54
|
+
const MAX_EPOCH = 0xffff_ffff;
|
|
55
|
+
const MAX_CODE_CHARS = 64;
|
|
56
|
+
const UUID_LOWER = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/;
|
|
57
|
+
const B64URL_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";
|
|
58
|
+
const B64URL = /^[A-Za-z0-9_-]*$/;
|
|
59
|
+
/** Blob and patch ids go into an aggregator URL, so they are held to the base64url alphabet. */
|
|
60
|
+
const STORAGE_ID = /^[A-Za-z0-9_-]{1,256}$/;
|
|
61
|
+
export class HandoverFormatError extends Error {
|
|
62
|
+
problem;
|
|
63
|
+
/** The field at fault, for `bad-field`. */
|
|
64
|
+
field;
|
|
65
|
+
constructor(problem, field = null) {
|
|
66
|
+
super(field === null ? `handover file: ${problem}` : `handover file: ${problem} (${field})`);
|
|
67
|
+
this.name = "HandoverFormatError";
|
|
68
|
+
this.problem = problem;
|
|
69
|
+
this.field = field;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
/**
|
|
73
|
+
* Bytes behind an unpadded base64url string, or null when it is not one in CANONICAL form: the
|
|
74
|
+
* bits the last character carries beyond the data must be zero, so one byte string has one text.
|
|
75
|
+
*/
|
|
76
|
+
export function base64UrlByteLength(text) {
|
|
77
|
+
if (!B64URL.test(text))
|
|
78
|
+
return null;
|
|
79
|
+
const rest = text.length % 4;
|
|
80
|
+
if (rest === 1)
|
|
81
|
+
return null;
|
|
82
|
+
if (rest !== 0) {
|
|
83
|
+
const last = B64URL_ALPHABET.indexOf(text.charAt(text.length - 1));
|
|
84
|
+
const unused = rest === 2 ? 0b1111 : 0b11;
|
|
85
|
+
if ((last & unused) !== 0)
|
|
86
|
+
return null;
|
|
87
|
+
}
|
|
88
|
+
return Math.floor(text.length / 4) * 3 + (rest === 0 ? 0 : rest - 1);
|
|
89
|
+
}
|
|
90
|
+
/** `nmts-handover-<YYYY-MM-DD>.nmtshandover`, the date in UTC. The file's own name is not in it. */
|
|
91
|
+
export function handoverFileName(now) {
|
|
92
|
+
return `nmts-handover-${now.toISOString().slice(0, 10)}${HANDOVER_EXTENSION}`;
|
|
93
|
+
}
|
|
94
|
+
/** `nmts-public-code-<public code>.nmtscode`. The code is public, and it tells two such files apart. */
|
|
95
|
+
export function publicCodeFileName(code) {
|
|
96
|
+
return `nmts-public-code-${code.replace(/[^0-9A-Za-z-]/g, "")}${PUBLIC_CODE_EXTENSION}`;
|
|
97
|
+
}
|
|
98
|
+
/** The handover file's text. Pretty-printed: somebody may well open it to see what it is. */
|
|
99
|
+
export function encodeHandover(file) {
|
|
100
|
+
checkHandover(file);
|
|
101
|
+
const { network, item, sender, envelope, name, hash, parts } = file;
|
|
102
|
+
const doc = { format: HANDOVER_FORMAT, version: HANDOVER_VERSION, network, item, sender, envelope, name, hash, parts, note: HANDOVER_NOTE };
|
|
103
|
+
return `${JSON.stringify(doc, null, 2)}\n`;
|
|
104
|
+
}
|
|
105
|
+
/**
|
|
106
|
+
* Read a handover file's text, in the order §5.6 gives: size, JSON, format, version, network, key
|
|
107
|
+
* set, `note`, fields. `network` is the reader's own. Throws `HandoverFormatError`; never returns a
|
|
108
|
+
* partial file.
|
|
109
|
+
*/
|
|
110
|
+
export function decodeHandover(text, network) {
|
|
111
|
+
capBytes(text, MAX_HANDOVER_FILE_BYTES);
|
|
112
|
+
const doc = objectOf(text);
|
|
113
|
+
if (doc["format"] !== HANDOVER_FORMAT)
|
|
114
|
+
throw new HandoverFormatError("wrong-format");
|
|
115
|
+
checkVersion(doc["version"], HANDOVER_VERSION);
|
|
116
|
+
const fileNetwork = doc["network"];
|
|
117
|
+
if (fileNetwork !== "mainnet" && fileNetwork !== "testnet")
|
|
118
|
+
throw new HandoverFormatError("bad-field", "network");
|
|
119
|
+
if (fileNetwork !== network)
|
|
120
|
+
throw new HandoverFormatError("other-network");
|
|
121
|
+
closedKeys(doc, ["format", "version", "network", "item", "sender", "envelope", "name", "hash", "parts", "note"]);
|
|
122
|
+
const note = doc["note"];
|
|
123
|
+
if (note !== undefined && !(Array.isArray(note) && note.every((line) => typeof line === "string"))) {
|
|
124
|
+
throw new HandoverFormatError("bad-field", "note");
|
|
125
|
+
}
|
|
126
|
+
const file = {
|
|
127
|
+
network: fileNetwork,
|
|
128
|
+
item: stringField(doc, "item"),
|
|
129
|
+
sender: stringField(doc, "sender"),
|
|
130
|
+
envelope: stringField(doc, "envelope"),
|
|
131
|
+
name: stringField(doc, "name"),
|
|
132
|
+
hash: stringField(doc, "hash"),
|
|
133
|
+
parts: stringField(doc, "parts"),
|
|
134
|
+
};
|
|
135
|
+
checkHandover(file);
|
|
136
|
+
return file;
|
|
137
|
+
}
|
|
138
|
+
/** The name document, exactly as it is sealed. */
|
|
139
|
+
export function encodeHandoverName(doc) {
|
|
140
|
+
checkName(doc);
|
|
141
|
+
return JSON.stringify({ f: SHARE_FILE_FORMAT, name: doc.name, size: doc.size, network: doc.network, parts_sha256: doc.partsSha256 });
|
|
142
|
+
}
|
|
143
|
+
/** Whether a name document, once sealed, fits the `name` field. The caller checks before sealing. */
|
|
144
|
+
export function handoverNameFits(encoded) {
|
|
145
|
+
return new TextEncoder().encode(encoded).length + MIN_SEALED_LEN <= MAX_NAME_SEALED_LEN;
|
|
146
|
+
}
|
|
147
|
+
/** Read an opened name document. Throws `HandoverFormatError` (`bad-field`, `name`). */
|
|
148
|
+
export function decodeHandoverName(text) {
|
|
149
|
+
const doc = objectOf(text);
|
|
150
|
+
const bad = new HandoverFormatError("bad-field", "name");
|
|
151
|
+
const keys = Object.keys(doc).sort().join(",");
|
|
152
|
+
if (keys !== "f,name,network,parts_sha256,size" || doc["f"] !== SHARE_FILE_FORMAT)
|
|
153
|
+
throw bad;
|
|
154
|
+
const name = doc["name"];
|
|
155
|
+
const size = doc["size"];
|
|
156
|
+
const network = doc["network"];
|
|
157
|
+
const partsSha256 = doc["parts_sha256"];
|
|
158
|
+
if (typeof name !== "string" || typeof size !== "number" || typeof partsSha256 !== "string")
|
|
159
|
+
throw bad;
|
|
160
|
+
if (network !== "mainnet" && network !== "testnet")
|
|
161
|
+
throw bad;
|
|
162
|
+
const out = { name, size, network, partsSha256 };
|
|
163
|
+
checkName(out);
|
|
164
|
+
return out;
|
|
165
|
+
}
|
|
166
|
+
/** The parts document, exactly as it is sealed. Compact: nobody reads it but the recipient's engine. */
|
|
167
|
+
export function encodePartsList(parts) {
|
|
168
|
+
checkParts(parts);
|
|
169
|
+
return JSON.stringify({
|
|
170
|
+
f: HANDOVER_PARTS_FORMAT,
|
|
171
|
+
parts: parts.map((p, i) => ({ i, blob: p.blob, patch: p.patch, len: p.len, exp: p.exp })),
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
/** Read an opened parts document. Throws `HandoverFormatError` (`bad-field`, `parts`). */
|
|
175
|
+
export function decodePartsList(text) {
|
|
176
|
+
const doc = objectOf(text);
|
|
177
|
+
const bad = new HandoverFormatError("bad-field", "parts");
|
|
178
|
+
if (Object.keys(doc).sort().join(",") !== "f,parts" || doc["f"] !== HANDOVER_PARTS_FORMAT)
|
|
179
|
+
throw bad;
|
|
180
|
+
const raw = doc["parts"];
|
|
181
|
+
if (!Array.isArray(raw))
|
|
182
|
+
throw bad;
|
|
183
|
+
const parts = raw.map((value, index) => {
|
|
184
|
+
if (!isRecord(value))
|
|
185
|
+
throw bad;
|
|
186
|
+
const keys = Object.keys(value).sort().join(",");
|
|
187
|
+
if (keys !== "blob,exp,i,len,patch" || value["i"] !== index)
|
|
188
|
+
throw bad;
|
|
189
|
+
const blob = value["blob"];
|
|
190
|
+
const patch = value["patch"];
|
|
191
|
+
const len = value["len"];
|
|
192
|
+
const exp = value["exp"];
|
|
193
|
+
if ((blob !== null && typeof blob !== "string") || (patch !== null && typeof patch !== "string"))
|
|
194
|
+
throw bad;
|
|
195
|
+
if (typeof len !== "number" || typeof exp !== "number")
|
|
196
|
+
throw bad;
|
|
197
|
+
return { blob, patch, len, exp };
|
|
198
|
+
});
|
|
199
|
+
checkParts(parts);
|
|
200
|
+
return parts;
|
|
201
|
+
}
|
|
202
|
+
/**
|
|
203
|
+
* The smallest recorded end epoch, or 0 when any piece's is unknown (0). Exclusive, and recorded
|
|
204
|
+
* when the file was made — see `HandoverPart.exp`.
|
|
205
|
+
*/
|
|
206
|
+
export function earliestExpiry(parts) {
|
|
207
|
+
if (parts.some((p) => p.exp === 0))
|
|
208
|
+
return 0;
|
|
209
|
+
return parts.reduce((min, p) => Math.min(min, p.exp), MAX_EPOCH);
|
|
210
|
+
}
|
|
211
|
+
/** The public code file's text. */
|
|
212
|
+
export function encodePublicCodeFile(file) {
|
|
213
|
+
checkPublicCode(file);
|
|
214
|
+
const doc = { format: PUBLIC_CODE_FORMAT, version: PUBLIC_CODE_VERSION, code: file.code, identity: file.identity };
|
|
215
|
+
return `${JSON.stringify(doc, null, 2)}\n`;
|
|
216
|
+
}
|
|
217
|
+
/**
|
|
218
|
+
* Read a public code file's text. Throws `HandoverFormatError`.
|
|
219
|
+
*
|
|
220
|
+
* ⚠ This checks the SHAPE only. Whether `identity` really fingerprints to `code` is the engine's
|
|
221
|
+
* question, and a caller must ask it before sealing anything to the identity.
|
|
222
|
+
*/
|
|
223
|
+
export function decodePublicCodeFile(text) {
|
|
224
|
+
capBytes(text, MAX_PUBLIC_CODE_FILE_BYTES);
|
|
225
|
+
const doc = objectOf(text);
|
|
226
|
+
if (doc["format"] !== PUBLIC_CODE_FORMAT)
|
|
227
|
+
throw new HandoverFormatError("wrong-format");
|
|
228
|
+
checkVersion(doc["version"], PUBLIC_CODE_VERSION);
|
|
229
|
+
closedKeys(doc, ["format", "version", "code", "identity"]);
|
|
230
|
+
const file = { code: stringField(doc, "code"), identity: stringField(doc, "identity") };
|
|
231
|
+
checkPublicCode(file);
|
|
232
|
+
return file;
|
|
233
|
+
}
|
|
234
|
+
// ── checks ───────────────────────────────────────────────────────────────────────────────────
|
|
235
|
+
function capBytes(text, max) {
|
|
236
|
+
// UTF-16 units never outnumber UTF-8 bytes, so a long string is refused before it is encoded.
|
|
237
|
+
if (text.length > max || new TextEncoder().encode(text).length > max) {
|
|
238
|
+
throw new HandoverFormatError("too-large");
|
|
239
|
+
}
|
|
240
|
+
}
|
|
241
|
+
/** An integer above the known version is "newer"; anything else that is not the version is wrong. */
|
|
242
|
+
function checkVersion(version, known) {
|
|
243
|
+
if (version === known)
|
|
244
|
+
return;
|
|
245
|
+
if (typeof version === "number" && Number.isSafeInteger(version) && version > known) {
|
|
246
|
+
throw new HandoverFormatError("unknown-version");
|
|
247
|
+
}
|
|
248
|
+
throw new HandoverFormatError("bad-field", "version");
|
|
249
|
+
}
|
|
250
|
+
function closedKeys(doc, allowed) {
|
|
251
|
+
for (const key of Object.keys(doc)) {
|
|
252
|
+
if (!allowed.includes(key))
|
|
253
|
+
throw new HandoverFormatError("bad-field", key);
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
function checkHandover(file) {
|
|
257
|
+
if (file.network !== "mainnet" && file.network !== "testnet")
|
|
258
|
+
throw new HandoverFormatError("bad-field", "network");
|
|
259
|
+
if (!UUID_LOWER.test(file.item))
|
|
260
|
+
throw new HandoverFormatError("bad-field", "item");
|
|
261
|
+
exactly(file.sender, IDENTITY_LEN, "sender");
|
|
262
|
+
exactly(file.envelope, ENVELOPE_LEN, "envelope");
|
|
263
|
+
exactly(file.hash, DIGEST_ENVELOPE_LEN, "hash");
|
|
264
|
+
within(file.name, MIN_SEALED_LEN + 1, MAX_NAME_SEALED_LEN, "name");
|
|
265
|
+
within(file.parts, MIN_SEALED_LEN + 1, MAX_PARTS_SEALED_LEN, "parts");
|
|
266
|
+
}
|
|
267
|
+
function checkName(doc) {
|
|
268
|
+
const network = doc.network === "mainnet" || doc.network === "testnet";
|
|
269
|
+
const ok = Number.isSafeInteger(doc.size) && doc.size >= 0 && network && base64UrlByteLength(doc.partsSha256) === SHA256_LEN;
|
|
270
|
+
if (!ok)
|
|
271
|
+
throw new HandoverFormatError("bad-field", "name");
|
|
272
|
+
}
|
|
273
|
+
function checkParts(parts) {
|
|
274
|
+
if (parts.length === 0 || parts.length > MAX_PARTS)
|
|
275
|
+
throw new HandoverFormatError("bad-field", "parts");
|
|
276
|
+
for (const p of parts) {
|
|
277
|
+
const ok = (p.blob === null) !== (p.patch === null) &&
|
|
278
|
+
(p.blob === null || STORAGE_ID.test(p.blob)) &&
|
|
279
|
+
(p.patch === null || STORAGE_ID.test(p.patch)) &&
|
|
280
|
+
Number.isSafeInteger(p.len) &&
|
|
281
|
+
p.len > 0 &&
|
|
282
|
+
Number.isSafeInteger(p.exp) &&
|
|
283
|
+
p.exp >= 0 &&
|
|
284
|
+
p.exp <= MAX_EPOCH;
|
|
285
|
+
if (!ok)
|
|
286
|
+
throw new HandoverFormatError("bad-field", "parts");
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
function checkPublicCode(file) {
|
|
290
|
+
if (file.code.length === 0 || file.code.length > MAX_CODE_CHARS)
|
|
291
|
+
throw new HandoverFormatError("bad-field", "code");
|
|
292
|
+
exactly(file.identity, IDENTITY_LEN, "identity");
|
|
293
|
+
}
|
|
294
|
+
function exactly(value, bytes, field) {
|
|
295
|
+
if (base64UrlByteLength(value) !== bytes)
|
|
296
|
+
throw new HandoverFormatError("bad-field", field);
|
|
297
|
+
}
|
|
298
|
+
function within(value, min, max, field) {
|
|
299
|
+
const n = base64UrlByteLength(value);
|
|
300
|
+
if (n === null || n < min || n > max)
|
|
301
|
+
throw new HandoverFormatError("bad-field", field);
|
|
302
|
+
}
|
|
303
|
+
function isRecord(value) {
|
|
304
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
305
|
+
}
|
|
306
|
+
function objectOf(text) {
|
|
307
|
+
let parsed;
|
|
308
|
+
try {
|
|
309
|
+
parsed = JSON.parse(text);
|
|
310
|
+
}
|
|
311
|
+
catch {
|
|
312
|
+
throw new HandoverFormatError("not-json");
|
|
313
|
+
}
|
|
314
|
+
if (!isRecord(parsed))
|
|
315
|
+
throw new HandoverFormatError("not-json");
|
|
316
|
+
return parsed;
|
|
317
|
+
}
|
|
318
|
+
function stringField(doc, field) {
|
|
319
|
+
const value = doc[field];
|
|
320
|
+
if (typeof value !== "string")
|
|
321
|
+
throw new HandoverFormatError("bad-field", field);
|
|
322
|
+
return value;
|
|
323
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/** The path segment every public link lives under. */
|
|
2
|
+
export declare const LINK_PATH = "/l/";
|
|
3
|
+
/** The two halves of a link. Neither is checked against a server here. */
|
|
4
|
+
export interface PublicLinkParts {
|
|
5
|
+
token: string;
|
|
6
|
+
secret: string;
|
|
7
|
+
}
|
|
8
|
+
/** Is this a link token (16 bytes, base64url)? */
|
|
9
|
+
export declare function isLinkToken(value: string): boolean;
|
|
10
|
+
/** Is this a link secret (32 bytes, canonical unpadded base64url)? */
|
|
11
|
+
export declare function isLinkSecret(value: string): boolean;
|
|
12
|
+
/** The whole link. `origin` is scheme + host (no trailing slash), e.g. `https://nmts.me`. */
|
|
13
|
+
export declare function buildPublicLink(origin: string, token: string, secret: string): string;
|
|
14
|
+
/**
|
|
15
|
+
* Read a link: a whole URL, or anything that ends in `/l/<token>#<secret>` (a locale prefix such as
|
|
16
|
+
* `/ko` is allowed before it). `null` when either half is missing or not the right shape.
|
|
17
|
+
*/
|
|
18
|
+
export declare function parsePublicLink(text: string): PublicLinkParts | null;
|
|
19
|
+
/** What a link's sealed document says. `name` is null when the owner hid it. */
|
|
20
|
+
export interface LinkDocument {
|
|
21
|
+
name: string | null;
|
|
22
|
+
/** The file's real plaintext length. */
|
|
23
|
+
size: number;
|
|
24
|
+
}
|
|
25
|
+
/** The string to seal as the link's `name` envelope. */
|
|
26
|
+
export declare function encodeLinkDocument(doc: LinkDocument): string;
|
|
27
|
+
/**
|
|
28
|
+
* Read an opened `name` envelope. `null` when it is not the document or has no usable size — a
|
|
29
|
+
* link's reader cannot take padding off without the size, so that is a damaged link (§5.8 step 4).
|
|
30
|
+
*/
|
|
31
|
+
export declare function decodeLinkDocument(text: string): LinkDocument | null;
|
|
32
|
+
/** The expiry choices the share sheet and the CLI offer, in days. `null` = never. */
|
|
33
|
+
export declare const LINK_EXPIRY_PRESETS: readonly (number | null)[];
|
|
34
|
+
/** The longest expiry a person may type, in days (ten years). */
|
|
35
|
+
export declare const LINK_EXPIRY_MAX_DAYS = 3650;
|
|
36
|
+
/** A typed number of days, or null when it is not a whole number in 1..LINK_EXPIRY_MAX_DAYS. */
|
|
37
|
+
export declare function linkExpiryDays(value: string | number): number | null;
|
|
38
|
+
/** `expires_at` for a link that lasts `days` from `now`, or undefined for one that never ends. */
|
|
39
|
+
export declare function linkExpiresAt(days: number | null, now: Date): string | undefined;
|
|
40
|
+
/** The name a saved link file gets. */
|
|
41
|
+
export declare function linkFileName(token: string): string;
|