@needmoretruth/nmts-cli 0.46.0 → 0.48.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (68) hide show
  1. package/AGENTS.md +8 -8
  2. package/CHANGELOG.md +32 -1
  3. package/README.ko.md +1 -1
  4. package/README.md +19 -11
  5. package/dist/arg-options.d.ts +11 -1
  6. package/dist/args.js +7 -1
  7. package/dist/artifact-about.d.ts +1 -1
  8. package/dist/autonomy.js +3 -3
  9. package/dist/commands/handover.d.ts +2 -0
  10. package/dist/commands/handover.js +65 -17
  11. package/dist/commands/link.d.ts +12 -0
  12. package/dist/commands/link.js +121 -0
  13. package/dist/commands/links.d.ts +7 -0
  14. package/dist/commands/links.js +71 -0
  15. package/dist/commands/public-code-manage.d.ts +24 -0
  16. package/dist/commands/public-code-manage.js +217 -0
  17. package/dist/commands/public-code.d.ts +7 -2
  18. package/dist/commands/public-code.js +104 -55
  19. package/dist/commands/receive.js +11 -5
  20. package/dist/commands/share-dispatch.js +18 -1
  21. package/dist/commands/share.d.ts +2 -0
  22. package/dist/commands/share.js +45 -35
  23. package/dist/commands/shares-sent.js +8 -3
  24. package/dist/crypto-surface.d.ts +27 -0
  25. package/dist/crypto-surface.js +10 -0
  26. package/dist/crypto.d.ts +5 -0
  27. package/dist/crypto.js +5 -0
  28. package/dist/handover-codes.d.ts +25 -0
  29. package/dist/handover-codes.js +106 -0
  30. package/dist/handover.d.ts +21 -3
  31. package/dist/handover.js +47 -29
  32. package/dist/help-options.js +9 -2
  33. package/dist/help.js +11 -1
  34. package/dist/links.d.ts +67 -0
  35. package/dist/links.js +226 -0
  36. package/dist/main.js +6 -6
  37. package/dist/mcp-tools/reads.js +20 -6
  38. package/dist/mcp-tools/share.js +47 -1
  39. package/dist/mcp-tools/tiers.js +16 -1
  40. package/dist/portable.d.ts +8 -0
  41. package/dist/portable.js +8 -0
  42. package/dist/product.d.ts +1 -1
  43. package/dist/product.js +1 -1
  44. package/dist/public-code-activity.d.ts +35 -0
  45. package/dist/public-code-activity.js +88 -0
  46. package/dist/public-code-refusals.d.ts +25 -0
  47. package/dist/public-code-refusals.js +87 -0
  48. package/dist/public-codes.d.ts +76 -0
  49. package/dist/public-codes.js +182 -0
  50. package/dist/risk.d.ts +36 -1
  51. package/dist/risk.js +38 -1
  52. package/dist/share-codes.d.ts +40 -0
  53. package/dist/share-codes.js +151 -0
  54. package/dist/share.d.ts +34 -0
  55. package/dist/share.js +53 -12
  56. package/dist/shared/lib/share/public-link.d.ts +41 -0
  57. package/dist/shared/lib/share/public-link.js +107 -0
  58. package/docs/commands/handover.md +17 -1
  59. package/docs/commands/link.md +34 -0
  60. package/docs/commands/mcp.md +2 -1
  61. package/docs/commands/public-code.md +21 -9
  62. package/docs/commands/share.md +6 -3
  63. package/package.json +1 -1
  64. package/server.json +2 -2
  65. package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +91 -5
  66. package/vendor/nmts-crypto/nmts_crypto_wasm.js +259 -4
  67. package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
  68. package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +11 -1
@@ -0,0 +1,151 @@
1
+ // The numbered public codes one NMTS key derives (NCF-3 §5.9).
2
+ //
3
+ // ⛔ EVERY NUMBER IS THE SAME KEY'S. Code 0 is the one every account has had from the start and comes
4
+ // straight out of the key's derivation (`shareKeysOf`); code N ≥ 1 is expanded from the 32-byte
5
+ // `shareIdRoot` slice of that derivation. Nothing is stored: the same key gives the same code at
6
+ // the same number on any machine, which is why a revoked code's shares still open here.
7
+ //
8
+ // ⛔ A SHARE NAMES NO RECIPIENT NUMBER. The received listing says which code the server filed a share
9
+ // under, and a handover file does not say at all, so `openShareAnyCode` tries the numbers it is
10
+ // given in order. A wrong number fails exactly as any other refusal does, so trying several
11
+ // weakens nothing.
12
+ //
13
+ // ⚠ THE KEY'S DERIVATION IS THE SLOW STEP — seconds, on purpose — and every number grows from its
14
+ // output. So anything that touches more than one number goes through ONE ring (`shareKeyRing`),
15
+ // which runs it once; `shareKeysAt` runs it for a single number.
16
+ import { DERIVED } from "./crypto.js";
17
+ import { NmtsError } from "./errors.js";
18
+ import { shareKeysFromDerived, shareKeysOf, unwrapWith } from "./share.js";
19
+ const IDENTITY_LEN = 4989;
20
+ const SEED_LEN = 32;
21
+ /** The largest number a code may carry — the server keeps it in a signed 32-bit column. */
22
+ export const CODE_INDEX_MAX = 2 ** 31 - 2;
23
+ /** A code number as a caller wrote it. Refused, never rounded: a neighbouring code is a different one. */
24
+ export function requireCodeIndex(value) {
25
+ if (!Number.isSafeInteger(value) || value < 0 || value > CODE_INDEX_MAX) {
26
+ throw new NmtsError(`A public code's number is a whole number from 0 to ${CODE_INDEX_MAX}.`, { exitCode: 2 });
27
+ }
28
+ return value;
29
+ }
30
+ /** Keys at N ≥ 1 from an already-sliced `shareIdRoot`. The caller wipes the root. */
31
+ function keysFromRoot(crypt, root, index) {
32
+ const seeds = crypt.share_id_seeds(root, index);
33
+ const kemSeed = seeds.slice(0, SEED_LEN);
34
+ const authSecret = seeds.slice(SEED_LEN, 2 * SEED_LEN);
35
+ const sigSeed = seeds.slice(2 * SEED_LEN, 3 * SEED_LEN);
36
+ seeds.fill(0);
37
+ const wipe = () => {
38
+ kemSeed.fill(0);
39
+ authSecret.fill(0);
40
+ sigSeed.fill(0);
41
+ };
42
+ try {
43
+ const identity = crypt.share_public_key_at(kemSeed, authSecret, sigSeed, index);
44
+ if (identity.length !== IDENTITY_LEN) {
45
+ throw new NmtsError(`Public code #${index}'s identity came out ${identity.length} bytes.`, {
46
+ nextStep: "Nothing was sent. The crypto engine and this tool disagree about the format.",
47
+ });
48
+ }
49
+ const address = crypt.share_address_at(sigSeed, index);
50
+ return { index, kemSeed, authSecret, sigSeed, address, identity, display: crypt.share_address_display(address), wipe };
51
+ }
52
+ catch (error) {
53
+ wipe();
54
+ throw error;
55
+ }
56
+ }
57
+ /** Run `body` with this key's `shareIdRoot`, wiped after. */
58
+ function withShareIdRoot(crypt, code, body) {
59
+ const derived = crypt.kdf_derive(crypt.account_code_parse(code));
60
+ const root = derived.slice(DERIVED.shareIdRoot[0], DERIVED.shareIdRoot[1]);
61
+ derived.fill(0);
62
+ try {
63
+ return body(root);
64
+ }
65
+ finally {
66
+ root.fill(0);
67
+ }
68
+ }
69
+ /**
70
+ * Everything sharing needs for public code number `index` — the same shape `shareKeysOf` answers,
71
+ * which is exactly what it answers for 0. ⛔ The caller wipes it.
72
+ */
73
+ export function shareKeysAt(crypt, code, index) {
74
+ requireCodeIndex(index);
75
+ if (index === 0)
76
+ return shareKeysOf(crypt, code);
77
+ return withShareIdRoot(crypt, code, (root) => keysFromRoot(crypt, root, index));
78
+ }
79
+ /**
80
+ * The keys of several codes, from ONE run of the key's derivation, on first use. What stays between
81
+ * uses is code 0's keys and the 32-byte root the other numbers grow from — never the rest of the
82
+ * derivation. ⛔ `wipe()` wipes all of it.
83
+ */
84
+ export function shareKeyRing(crypt, code) {
85
+ const held = new Map();
86
+ let root = null;
87
+ const rootOf = () => {
88
+ if (root !== null)
89
+ return root;
90
+ const derived = crypt.kdf_derive(crypt.account_code_parse(code));
91
+ try {
92
+ held.set(0, shareKeysFromDerived(crypt, derived));
93
+ const made = derived.slice(DERIVED.shareIdRoot[0], DERIVED.shareIdRoot[1]);
94
+ root = made;
95
+ return made;
96
+ }
97
+ finally {
98
+ derived.fill(0);
99
+ }
100
+ };
101
+ return {
102
+ at(index) {
103
+ requireCodeIndex(index);
104
+ const from = rootOf();
105
+ const had = held.get(index);
106
+ if (had !== undefined)
107
+ return had;
108
+ const made = keysFromRoot(crypt, from, index);
109
+ held.set(index, made);
110
+ return made;
111
+ },
112
+ wipe() {
113
+ for (const keys of held.values())
114
+ keys.wipe();
115
+ held.clear();
116
+ root?.fill(0);
117
+ root = null;
118
+ },
119
+ };
120
+ }
121
+ /**
122
+ * Open one envelope with whichever of `indices` it was sealed to, trying them in the order given.
123
+ *
124
+ * ⛔ ONE REFUSAL FOR EVERY FAILURE. Which number was tried and how it failed is not something to
125
+ * report: a wrong code, a tampered envelope and a sender who is not who it claims all fail the
126
+ * same way, and saying which would be telling somebody holding the file more than the file says.
127
+ */
128
+ export function openShareAnyCode(crypt, code, indices, sealed) {
129
+ const ring = shareKeyRing(crypt, code);
130
+ try {
131
+ return openShareWithRing(crypt, ring, indices, sealed);
132
+ }
133
+ finally {
134
+ ring.wipe();
135
+ }
136
+ }
137
+ /** `openShareAnyCode` with a ring the caller holds, and wipes. */
138
+ export function openShareWithRing(crypt, ring, indices, sealed) {
139
+ for (const index of indices) {
140
+ try {
141
+ return { dek: unwrapWith(crypt, ring.at(index), sealed), index };
142
+ }
143
+ catch {
144
+ // The next number, or the one refusal below.
145
+ }
146
+ }
147
+ throw new NmtsError("It did not open with any of this account's public codes.", {
148
+ exitCode: 1,
149
+ nextStep: "Nothing was opened. The sender can seal it again to a public code this account holds.",
150
+ });
151
+ }
package/dist/share.d.ts CHANGED
@@ -7,6 +7,11 @@ import { type CryptoGlue } from "./crypto.ts";
7
7
  * failures.
8
8
  */
9
9
  export interface ShareKeys {
10
+ /**
11
+ * Which of this key's numbered public codes these are (NCF-3 §5.9). 0 is the code every account
12
+ * has had from the start; `share-codes.ts` derives the others.
13
+ */
14
+ index: number;
10
15
  /** Key-agreement seed. */
11
16
  kemSeed: Uint8Array;
12
17
  /** Proves this account SENT a share. */
@@ -23,6 +28,8 @@ export interface ShareKeys {
23
28
  }
24
29
  /** Derive everything sharing needs from an NMTS key. */
25
30
  export declare function shareKeysOf(crypt: CryptoGlue, code: string): ShareKeys;
31
+ /** Code 0's keys out of one run of the key's derivation. The caller wipes `derived`. */
32
+ export declare function shareKeysFromDerived(crypt: CryptoGlue, derived: Uint8Array): ShareKeys;
26
33
  /** The three sealed fields a share carries, base64url, exactly as they go to the server. */
27
34
  export interface SharePayload {
28
35
  dek_share_ct: string;
@@ -64,11 +71,20 @@ export interface ReceivedRow {
64
71
  /** Bytes on the storage network — NOT the file's length. The sealed document has that. */
65
72
  size: number;
66
73
  sender_public_key?: string;
74
+ /** Which of this account's public codes it was sent to. Absent from an older server: code 0. */
75
+ to_index?: number;
76
+ /** Whether the sender has revoked the code it was sent from. */
77
+ sender_code_revoked?: boolean;
67
78
  dek_share_ct: string;
68
79
  name_share_ct: string;
69
80
  content_hash_share_ct: string;
70
81
  created_at: string;
71
82
  }
83
+ /**
84
+ * Which of this account's codes a received row came to. A number the server should never send —
85
+ * negative, fractional, past a code's range — reads as 0, where the open then fails and says so.
86
+ */
87
+ export declare function receivedIndex(row: ReceivedRow): number;
72
88
  /** What a received share turns into once opened, or why it could not be. */
73
89
  export interface OpenedShare {
74
90
  id: string;
@@ -80,6 +96,9 @@ export interface OpenedShare {
80
96
  size: number | null;
81
97
  /** The sender's address, in readable form — only ever set when the open SUCCEEDED. */
82
98
  sender: string | null;
99
+ /** Which of this account's public codes it came to, and whether the sender revoked theirs. */
100
+ toIndex: number;
101
+ senderRevoked: boolean;
83
102
  /** The file's own key. Present only when it opened; the caller wipes it. */
84
103
  dek: Uint8Array | null;
85
104
  /** The sealed digest, carried through so a download can check the bytes. */
@@ -95,6 +114,21 @@ export interface OpenedShare {
95
114
  * line — not to leave a gap somebody has no way to notice.
96
115
  */
97
116
  export declare function openReceived(crypt: CryptoGlue, keys: ShareKeys, row: ReceivedRow): OpenedShare;
117
+ /** The sealed parts of one share that its recipient opens with. */
118
+ export interface ShareSealed {
119
+ senderPublic: Uint8Array;
120
+ envelope: Uint8Array;
121
+ itemId: string;
122
+ nameCt: Uint8Array;
123
+ digestCt: Uint8Array;
124
+ }
125
+ /**
126
+ * Open one envelope with one of this account's codes. Throws when it will not open.
127
+ *
128
+ * ⚠ THE RECIPIENT'S NUMBER IS PART OF WHAT OPENS IT, so the keys must be the code it was sent to:
129
+ * code 0 through the engine's first form, any other through its numbered one.
130
+ */
131
+ export declare function unwrapWith(crypt: CryptoGlue, keys: ShareKeys, s: ShareSealed): Uint8Array;
98
132
  /** The digest a recipient checks the downloaded bytes against. */
99
133
  export declare function openSharedDigest(crypt: CryptoGlue, dek: Uint8Array, digestCt: string): Uint8Array | null;
100
134
  /**
package/dist/share.js CHANGED
@@ -16,6 +16,7 @@
16
16
  // made by the account it names. The claimed sender is therefore printed only AFTER the open
17
17
  // succeeds — before that it is a claim, and printing a claim as a fact is how somebody trusts
18
18
  // a file that was not sent by who it says.
19
+ import { fromBase64Url, toBase64Url } from "./bytes.js";
19
20
  import { AAD, DERIVED } from "./crypto.js";
20
21
  import { NmtsError } from "./errors.js";
21
22
  import { decodeSharedFileInfo, encodeSharedFileInfo, } from "./shared/lib/share/shared-file-info.js";
@@ -27,12 +28,20 @@ const IDENTITY_LEN = 4989;
27
28
  /** Derive everything sharing needs from an NMTS key. */
28
29
  export function shareKeysOf(crypt, code) {
29
30
  const derived = crypt.kdf_derive(crypt.account_code_parse(code));
31
+ try {
32
+ return shareKeysFromDerived(crypt, derived);
33
+ }
34
+ finally {
35
+ derived.fill(0);
36
+ }
37
+ }
38
+ /** Code 0's keys out of one run of the key's derivation. The caller wipes `derived`. */
39
+ export function shareKeysFromDerived(crypt, derived) {
30
40
  const slice = (range) => derived.slice(range[0], range[1]);
31
41
  const kemSeed = slice(DERIVED.shareKemSeed);
32
42
  const authSecret = slice(DERIVED.shareAuthSecret);
33
43
  const sigSeed = slice(DERIVED.shareSigSeed);
34
44
  const address = slice(DERIVED.shareAddress);
35
- derived.fill(0);
36
45
  const identity = crypt.share_public_key(kemSeed, authSecret, sigSeed);
37
46
  if (identity.length !== IDENTITY_LEN) {
38
47
  throw new NmtsError(`This account's sharing identity came out ${identity.length} bytes.`, {
@@ -40,6 +49,7 @@ export function shareKeysOf(crypt, code) {
40
49
  });
41
50
  }
42
51
  return {
52
+ index: 0,
43
53
  kemSeed,
44
54
  authSecret,
45
55
  sigSeed,
@@ -75,16 +85,29 @@ export function sealShare(crypt, input) {
75
85
  if (digestCt.length !== DIGEST_ENVELOPE_LEN) {
76
86
  throw new NmtsError(`A sealed content hash came out ${digestCt.length} bytes.`);
77
87
  }
78
- const envelope = crypt.share_wrap_dek(input.keys.authSecret, input.keys.sigSeed, input.recipientIdentity, input.recipientAddress, input.dek, input.itemId, nameCt, digestCt);
88
+ // ⚠ THE SENDER'S NUMBER IS INSIDE THE ENVELOPE: it names which of this key's codes sent it, so a
89
+ // code other than 0 wraps through the engine's numbered form.
90
+ const k = input.keys;
91
+ const envelope = k.index === 0
92
+ ? crypt.share_wrap_dek(k.authSecret, k.sigSeed, input.recipientIdentity, input.recipientAddress, input.dek, input.itemId, nameCt, digestCt)
93
+ : crypt.share_wrap_dek_as(k.authSecret, k.sigSeed, k.index, input.recipientIdentity, input.recipientAddress, input.dek, input.itemId, nameCt, digestCt);
79
94
  if (envelope.length !== ENVELOPE_LEN) {
80
95
  throw new NmtsError(`A share envelope came out ${envelope.length} bytes.`);
81
96
  }
82
97
  return {
83
- dek_share_ct: Buffer.from(envelope).toString("base64url"),
84
- name_share_ct: Buffer.from(nameCt).toString("base64url"),
85
- content_hash_share_ct: Buffer.from(digestCt).toString("base64url"),
98
+ dek_share_ct: toBase64Url(envelope),
99
+ name_share_ct: toBase64Url(nameCt),
100
+ content_hash_share_ct: toBase64Url(digestCt),
86
101
  };
87
102
  }
103
+ /**
104
+ * Which of this account's codes a received row came to. A number the server should never send —
105
+ * negative, fractional, past a code's range — reads as 0, where the open then fails and says so.
106
+ */
107
+ export function receivedIndex(row) {
108
+ const n = row.to_index;
109
+ return typeof n === "number" && Number.isSafeInteger(n) && n >= 0 && n <= 2 ** 31 - 2 ? n : 0;
110
+ }
88
111
  /**
89
112
  * Open one received share.
90
113
  *
@@ -98,6 +121,8 @@ export function openReceived(crypt, keys, row) {
98
121
  itemId: row.item_id,
99
122
  createdAt: row.created_at,
100
123
  digestCt: row.content_hash_share_ct,
124
+ toIndex: receivedIndex(row),
125
+ senderRevoked: row.sender_code_revoked === true,
101
126
  };
102
127
  const unopened = (problem) => ({
103
128
  ...base,
@@ -112,13 +137,17 @@ export function openReceived(crypt, keys, row) {
112
137
  // authenticate against, and an unauthenticated open is not one worth doing.
113
138
  return unopened("the sender's published identity is not available");
114
139
  }
115
- const envelope = new Uint8Array(Buffer.from(row.dek_share_ct, "base64url"));
116
- const nameCt = new Uint8Array(Buffer.from(row.name_share_ct, "base64url"));
117
- const digestCt = new Uint8Array(Buffer.from(row.content_hash_share_ct, "base64url"));
118
- const senderPublic = new Uint8Array(Buffer.from(row.sender_public_key, "base64url"));
140
+ // ⚠ Decoded inside the try: a row whose fields are not base64 is a row that will not open, and it
141
+ // is still listed.
142
+ let envelope = new Uint8Array(0);
143
+ let nameCt = new Uint8Array(0);
119
144
  let dek;
120
145
  try {
121
- dek = crypt.share_unwrap_dek(keys.kemSeed, keys.authSecret, keys.sigSeed, senderPublic, envelope, row.item_id, nameCt, digestCt);
146
+ envelope = fromBase64Url(row.dek_share_ct);
147
+ nameCt = fromBase64Url(row.name_share_ct);
148
+ const digestCt = fromBase64Url(row.content_hash_share_ct);
149
+ const senderPublic = fromBase64Url(row.sender_public_key);
150
+ dek = unwrapWith(crypt, keys, { senderPublic, envelope, itemId: row.item_id, nameCt, digestCt });
122
151
  }
123
152
  catch {
124
153
  return unopened("it did not open with this account's keys");
@@ -150,10 +179,21 @@ export function openReceived(crypt, keys, row) {
150
179
  problem: null,
151
180
  };
152
181
  }
182
+ /**
183
+ * Open one envelope with one of this account's codes. Throws when it will not open.
184
+ *
185
+ * ⚠ THE RECIPIENT'S NUMBER IS PART OF WHAT OPENS IT, so the keys must be the code it was sent to:
186
+ * code 0 through the engine's first form, any other through its numbered one.
187
+ */
188
+ export function unwrapWith(crypt, keys, s) {
189
+ return keys.index === 0
190
+ ? crypt.share_unwrap_dek(keys.kemSeed, keys.authSecret, keys.sigSeed, s.senderPublic, s.envelope, s.itemId, s.nameCt, s.digestCt)
191
+ : crypt.share_unwrap_dek_as(keys.kemSeed, keys.authSecret, keys.sigSeed, keys.index, s.senderPublic, s.envelope, s.itemId, s.nameCt, s.digestCt);
192
+ }
153
193
  /** The digest a recipient checks the downloaded bytes against. */
154
194
  export function openSharedDigest(crypt, dek, digestCt) {
155
195
  try {
156
- return crypt.envelope_open(dek, encoder.encode(AAD.shareContentHash), new Uint8Array(Buffer.from(digestCt, "base64url")));
196
+ return crypt.envelope_open(dek, encoder.encode(AAD.shareContentHash), fromBase64Url(digestCt));
157
197
  }
158
198
  catch {
159
199
  return null;
@@ -187,7 +227,8 @@ export function identityMatches(crypt, identity, address) {
187
227
  if (identity.length !== IDENTITY_LEN)
188
228
  return false;
189
229
  try {
190
- return Buffer.from(crypt.share_address_of(identity)).equals(Buffer.from(address));
230
+ const fingerprint = crypt.share_address_of(identity);
231
+ return fingerprint.length === address.length && fingerprint.every((byte, at) => byte === address[at]);
191
232
  }
192
233
  catch {
193
234
  return false;
@@ -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;
@@ -0,0 +1,107 @@
1
+ // The public link's text shapes (NCF-3 §5.8).
2
+ //
3
+ // A public link is `<origin>/l/<token>#<secret>`: the token names the server's row, the secret `S`
4
+ // opens the file key the row holds, and the part after `#` never reaches a server. This module
5
+ // builds and reads that text, and the small document sealed beside the link that tells the reader
6
+ // the file's real length (and, when the owner chose to show it, its name).
7
+ //
8
+ // ⛔ THE DOCUMENT IS THE §5.4 SHARE DOCUMENT, not a new format. A link that shows the name seals the
9
+ // same bytes a private share does (`shared-file-info.ts`); one that hides it leaves the `name`
10
+ // key out and keeps `size`, because the size is what takes size padding back off.
11
+ //
12
+ // ⚠ IT IS COPIED VERBATIM INTO THE CLI (`deploy/gen-cli-shared.mjs`), so it depends on nothing and
13
+ // both programs read and write the same link.
14
+ /** The path segment every public link lives under. */
15
+ export const LINK_PATH = "/l/";
16
+ /** Same marker as `shared-file-info.ts` — the document is that document. */
17
+ const SHARE_FILE_FORMAT = "nmts-share-file/1";
18
+ /** 16 bytes, unpadded base64url. */
19
+ const TOKEN_RE = /^[A-Za-z0-9_-]{22}$/;
20
+ /**
21
+ * 32 bytes, unpadded CANONICAL base64url: 43 characters, and the last one carries only the four
22
+ * bits that are left, so its two low bits are zero. A second spelling of the same bytes is refused
23
+ * rather than silently accepted (§5.8 step 1).
24
+ */
25
+ const SECRET_RE = /^[A-Za-z0-9_-]{42}[AEIMQUYcgkosw048]$/;
26
+ /** Is this a link token (16 bytes, base64url)? */
27
+ export function isLinkToken(value) {
28
+ return TOKEN_RE.test(value);
29
+ }
30
+ /** Is this a link secret (32 bytes, canonical unpadded base64url)? */
31
+ export function isLinkSecret(value) {
32
+ return SECRET_RE.test(value);
33
+ }
34
+ /** The whole link. `origin` is scheme + host (no trailing slash), e.g. `https://nmts.me`. */
35
+ export function buildPublicLink(origin, token, secret) {
36
+ return `${origin.replace(/\/+$/, "")}${LINK_PATH}${token}#${secret}`;
37
+ }
38
+ /**
39
+ * Read a link: a whole URL, or anything that ends in `/l/<token>#<secret>` (a locale prefix such as
40
+ * `/ko` is allowed before it). `null` when either half is missing or not the right shape.
41
+ */
42
+ export function parsePublicLink(text) {
43
+ const trimmed = text.trim();
44
+ const hash = trimmed.indexOf("#");
45
+ if (hash < 0)
46
+ return null;
47
+ const secret = trimmed.slice(hash + 1);
48
+ const before = trimmed.slice(0, hash).replace(/[?].*$/, "").replace(/\/+$/, "");
49
+ const at = before.lastIndexOf(LINK_PATH);
50
+ if (at < 0)
51
+ return null;
52
+ const token = before.slice(at + LINK_PATH.length);
53
+ if (!isLinkToken(token) || !isLinkSecret(secret))
54
+ return null;
55
+ return { token, secret };
56
+ }
57
+ /** The string to seal as the link's `name` envelope. */
58
+ export function encodeLinkDocument(doc) {
59
+ // Key order matches `encodeSharedFileInfo`, so a link that shows the name seals the same bytes.
60
+ return doc.name === null
61
+ ? JSON.stringify({ f: SHARE_FILE_FORMAT, size: doc.size })
62
+ : JSON.stringify({ f: SHARE_FILE_FORMAT, name: doc.name, size: doc.size });
63
+ }
64
+ /**
65
+ * Read an opened `name` envelope. `null` when it is not the document or has no usable size — a
66
+ * link's reader cannot take padding off without the size, so that is a damaged link (§5.8 step 4).
67
+ */
68
+ export function decodeLinkDocument(text) {
69
+ let parsed;
70
+ try {
71
+ parsed = JSON.parse(text);
72
+ }
73
+ catch {
74
+ return null;
75
+ }
76
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
77
+ return null;
78
+ const f = Reflect.get(parsed, "f");
79
+ const name = Reflect.get(parsed, "name");
80
+ const size = Reflect.get(parsed, "size");
81
+ if (f !== SHARE_FILE_FORMAT)
82
+ return null;
83
+ if (typeof size !== "number" || !Number.isSafeInteger(size) || size < 0)
84
+ return null;
85
+ if (name !== undefined && typeof name !== "string")
86
+ return null;
87
+ return { name: typeof name === "string" ? name : null, size };
88
+ }
89
+ /** The expiry choices the share sheet and the CLI offer, in days. `null` = never. */
90
+ export const LINK_EXPIRY_PRESETS = [null, 7, 30];
91
+ /** The longest expiry a person may type, in days (ten years). */
92
+ export const LINK_EXPIRY_MAX_DAYS = 3650;
93
+ /** A typed number of days, or null when it is not a whole number in 1..LINK_EXPIRY_MAX_DAYS. */
94
+ export function linkExpiryDays(value) {
95
+ const n = typeof value === "number" ? value : Number(value.trim());
96
+ return Number.isSafeInteger(n) && n >= 1 && n <= LINK_EXPIRY_MAX_DAYS ? n : null;
97
+ }
98
+ /** `expires_at` for a link that lasts `days` from `now`, or undefined for one that never ends. */
99
+ export function linkExpiresAt(days, now) {
100
+ if (days === null)
101
+ return undefined;
102
+ return new Date(now.getTime() + days * 86_400_000).toISOString();
103
+ }
104
+ /** The name a saved link file gets. */
105
+ export function linkFileName(token) {
106
+ return `nmts-link-${token}.txt`;
107
+ }
@@ -6,7 +6,7 @@ Tiers: handover=none · handover.make=high(share)
6
6
  `handover make <path> --to <public code | public code file>` writes a handover file: one file of
7
7
  your drive, sealed so that only the recipient's NMTS key decrypts the file inside it. You pass the
8
8
  handover file on yourself — mail, chat, a USB stick. No share is registered, and the recipient's
9
- side asks the NMTS server nothing. `--out <file>` names the handover file (default
9
+ side tells the NMTS server nothing about it. `--out <file>` names the handover file (default
10
10
  `nmts-handover-<YYYY-MM-DD>.nmtshandover` in the current directory, the date in UTC; it never
11
11
  carries the file's own name); `--force` replaces a file that is already there. Both are checked
12
12
  before anything else is done. The handover file is written readable by you only (mode 0600).
@@ -18,6 +18,13 @@ believed only when the identity inside fingerprints to the code it names; that p
18
18
  consistent, not that it came from the recipient, so compare the code it shows with the one they
19
19
  gave you.
20
20
 
21
+ It is sent from your default public code, the lowest-numbered live one, or from the live code
22
+ `--as <n>` names, as for `share`; the recipient sees that code. None of your own codes, live or
23
+ revoked, can be the recipient. When the recipient's public code file names a code its owner has
24
+ revoked, `make` prints `Its owner has revoked this public code. Ask them for the code they use now.`
25
+ and still writes the file, because a revoked code still opens what was sealed to it. A typed code
26
+ that was revoked has no identity left to look up, so it is refused.
27
+
21
28
  It gives another account this file, so it sits behind the same unlock as `share` and asks on every
22
29
  run: it prints the file, the recipient and the output path and stops until the same command is run
23
30
  with `--yes`, in every mode but skip-permissions. The command it prints keeps every option you gave.
@@ -34,5 +41,14 @@ sealed; a file that does not match is not written. It refuses a handover file ov
34
41
  made for another NMTS key, a file whose fields do not hold together, a file from a newer version,
35
42
  and a file made on the other network (`--network`).
36
43
 
44
+ A handover file does not say which of your public codes it was sealed to, so `open` tries them:
45
+ code 0 first; then, when an API key on this machine can read your list of codes, your live codes
46
+ lowest first and your revoked ones newest first; with no key, codes 1, 2, … up to 64. When the code
47
+ that opened it is not code 0 it prints `to #n` and the code, or `received with your revoked public
48
+ code #n` when you have revoked it. When the sender's code is on the revoked list it prints `The
49
+ sender has since revoked this code.` Both revocation checks fetch one sixteenth of the public
50
+ revoked list and compare on this machine, so the server is never asked about one code. Offline,
51
+ they are skipped and the file still opens.
52
+
37
53
  Whoever holds a handover file sees the sender's public code in it. The aggregator the recipient
38
54
  fetches from sees the recipient's network address, as for any download.
@@ -0,0 +1,34 @@
1
+ # nmts link — public links to one file
2
+
3
+ Commands: link, links
4
+ Tiers: link=none · link.make=high(share) · link.revoke=low · links=none · links.revoke=low · links.revoke-all=low
5
+
6
+ `link make <path>` makes a public link to one file of your drive and prints it:
7
+ `https://nmts.me/l/<token>#<secret>`. Anyone with the link can open this file. Cutting the link stops
8
+ new downloads, but copies already downloaded stay with whoever has them. `--hide-name` leaves the
9
+ file's name out; its size stays, because the size is what takes size padding back off.
10
+ `--expires <n>d` stops the link after that many days (1d to 3650d); without it the link lasts until
11
+ it is cut. The secret after `#` is made here and is in no request: the server stores the file key
12
+ wrapped under it, the name document and the content hash sealed under the file key, and a copy of
13
+ the secret sealed under this account's key so the link can be printed again. It gives the file to
14
+ whoever holds the link, so it sits behind the same unlock as `share` and asks on every run.
15
+
16
+ `link list <path>` prints every link made to that file, newest first, with when it was made and how
17
+ many times it was downloaded; a live link is printed whole again, a cut one says when and by whom
18
+ (`owner`, `operator`, `report`).
19
+
20
+ `link revoke <id>` cuts one link. The server stops handing out the file's pieces for it at once.
21
+
22
+ `link open <link>` downloads and decrypts a file from a public link, without logging in. It asks
23
+ the server for the token only, opens the file key with the secret, fetches the pieces from Walrus
24
+ and checks the whole file against the hash the owner sealed; a file that does not match is not
25
+ written. Without `--out` the file is saved in the current directory under the name the owner
26
+ showed, reduced to its last segment, or `nmts-link-<token>` when the name was hidden; `--force`
27
+ replaces a file that is already there. A cut, expired or unknown link is refused.
28
+
29
+ `links` prints every live public link this account holds, across all its files, newest first: the
30
+ id, the file's path (read from this account's own file list; the server has file ids, not names),
31
+ when it was made, how many times it was downloaded, when it expires if it does, and the whole link.
32
+ `links revoke <id>` cuts one, as `link revoke` does. `links revoke-all` cuts every live link in one
33
+ request — all or none — and prints how many were cut; asking again cuts none. Copies already
34
+ downloaded stay with whoever has them.
@@ -13,7 +13,8 @@ MCP elicitation — or refused, naming the way round it, when the client cannot
13
13
  The tools: `nmts_whoami` `nmts_list` `nmts_usage` `nmts_expiring` `nmts_balance` `nmts_shares`
14
14
  `nmts_shares_sent` · `nmts_wallet_activity` `nmts_wallet_storage` · `nmts_devices` · `nmts_notices`
15
15
  `nmts_notice` `nmts_terms` `nmts_privacy` · `nmts_losses` `nmts_loss_recheck` · `nmts_get`
16
- `nmts_pull` `nmts_receive` · `nmts_put` `nmts_push` `nmts_padding` `nmts_deposit` · `nmts_public_code` ·
16
+ `nmts_pull` `nmts_receive` · `nmts_put` `nmts_push` `nmts_padding` `nmts_deposit` · `nmts_public_code`
17
+ `nmts_public_code_new` `nmts_public_code_revoke` ·
17
18
  `nmts_mkdir` `nmts_move` `nmts_rename` `nmts_mark` `nmts_label_rename` `nmts_unlabel_all`
18
19
  `nmts_trash` `nmts_restore` · `nmts_credits_transfer` · `nmts_share` `nmts_unshare` · `nmts_support_send`
19
20
  `nmts_support_list` `nmts_support_show` `nmts_support_reply`.
@@ -1,16 +1,28 @@
1
- # nmts public-code — the code other accounts send files to
1
+ # nmts public-code — the codes other accounts send files to
2
2
 
3
3
  Commands: public-code
4
- Tiers: public-code=none · public-code.publish=medium
4
+ Tiers: public-code=none · public-code.list=none · public-code.publish=medium · public-code.new=medium · public-code.revoke=high
5
5
 
6
- Prints the account's public code and whether it has been published. It is not the NMTS key.
7
- An unpublished code cannot receive anything. Publishing is permanent, so it is `--publish` and not
8
- automatic: in the default mode it asks first (or takes `--yes`); in an auto mode it is your
9
- judgement, and a permanent, public act is one to put to the person.
6
+ `public-code` prints the account's default public code — its lowest-numbered live one — and whether
7
+ it has been published. It is not the NMTS key. An unpublished code cannot receive anything, and
8
+ publishing is a public act, so it is `--publish` and not automatic: in the default mode it asks
9
+ first (or takes `--yes`); in an auto mode it is your judgement, and one to put to the person.
10
10
 
11
- `--save [file]` writes this account's public code file: the public code and the identity behind
12
- it, nothing secret. Without a file name it writes `nmts-public-code-<public code>.nmtscode` in the
13
- current directory. It needs the NMTS key and asks the server nothing. Whoever has the file can make
11
+ Every code comes from the one NMTS key, at a number: 0, 1, 2, … An account holds one to three live
12
+ codes (a Platform user exactly one). `public-code list` shows every code, live and revoked, with how
13
+ many shares and messages went through it; `--activity` lists the shares. `public-code new` publishes
14
+ the next number; `--replace <n>` revokes that code in the same request. `public-code revoke <n>`
15
+ revokes one: nobody can send to it again, nobody can bring it back, and files already received with
16
+ it stay. The last live code cannot be revoked. Revoking, by either command, asks every time and
17
+ needs `--yes` where nobody can answer. The server caps how many codes an account makes per UTC day;
18
+ `list` prints the cap and how many were made today.
19
+
20
+ `--save [file]` writes the public code file of your default code: the public code and the
21
+ identity behind it, nothing secret. When an API key on this machine can read your list of codes,
22
+ the default is the lowest-numbered live one; with no key it is code 0, the code every NMTS key
23
+ starts with. `--save --as <n>` writes the file of code n instead and asks the server nothing.
24
+ Without a file name it writes `nmts-public-code-<public code>.nmtscode` in the current directory.
25
+ It needs the NMTS key. Whoever has the file can make
14
26
  a handover file for you (`nmts handover make … --to <file>`) without looking your code up, so NMTS
15
27
  does not learn who is sending to you. `--force` replaces a file that is already there. It is not
16
28
  combined with `--publish`.
@@ -8,10 +8,13 @@ download it; withdrawing later stops further downloads and cannot recall a copy
8
8
  and the code is not checked against a person — the wrong code sends the file to whoever holds it.
9
9
  So it is locked until a person runs `nmts unlock share` once, and then asks on every run: it prints
10
10
  the file and the code and stops until the same command is run with `--yes`, in every mode but
11
- skip-permissions. Sharing is also on the short list the periodic human check gates.
11
+ skip-permissions. Sharing is also on the short list the periodic human check gates. It sends from
12
+ the account's lowest-numbered live public code, or from the one `--as <n>` names; an account with
13
+ no live code publishes one first.
12
14
 
13
- `shares` lists what was shared with this account; `shares --sent <path>` says who one file was
14
- shared with. `receive <id>` downloads one shared file (`--out`, `--force` as for `get`); without
15
+ `shares` lists what was shared with this account, with the code each share came to once more than
16
+ one code has received, and a mark where the sender has revoked theirs; `shares --sent <path>` says
17
+ who one file was shared with. `receive <id>` downloads one shared file (`--out`, `--force` as for `get`); without
15
18
  `--out` it is saved in the current directory under the name the sender gave it, reduced to its last
16
19
  segment, so a name such as `../x` or an absolute path cannot place it anywhere else. `unshare
17
20
  <id>` withdraws a share you sent, or removes one you were sent; in the default mode it asks once.