@needmoretruth/nmts-cli 0.40.0 → 0.41.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.
Files changed (47) hide show
  1. package/CHANGELOG.md +16 -0
  2. package/README.ko.md +27 -1
  3. package/README.md +29 -1
  4. package/dist/api-advice.js +6 -0
  5. package/dist/api.d.ts +10 -1
  6. package/dist/api.js +12 -2
  7. package/dist/arg-options.d.ts +21 -0
  8. package/dist/args.js +12 -1
  9. package/dist/artifact-about.d.ts +1 -1
  10. package/dist/commands/create-link.d.ts +9 -0
  11. package/dist/commands/create-link.js +31 -1
  12. package/dist/commands/create.d.ts +9 -0
  13. package/dist/commands/create.js +17 -0
  14. package/dist/commands/login-passphrase.d.ts +13 -0
  15. package/dist/commands/login-passphrase.js +64 -0
  16. package/dist/commands/login.d.ts +3 -0
  17. package/dist/commands/login.js +15 -55
  18. package/dist/commands/openers-attach.d.ts +38 -0
  19. package/dist/commands/openers-attach.js +97 -0
  20. package/dist/commands/openers.d.ts +23 -0
  21. package/dist/commands/openers.js +195 -0
  22. package/dist/crypto-surface.d.ts +28 -0
  23. package/dist/crypto-surface.js +7 -0
  24. package/dist/help.js +15 -0
  25. package/dist/main.js +11 -12
  26. package/dist/openers/doors.d.ts +65 -0
  27. package/dist/openers/doors.js +97 -0
  28. package/dist/openers/hints.d.ts +22 -0
  29. package/dist/openers/hints.js +11 -0
  30. package/dist/openers/sign.d.ts +56 -0
  31. package/dist/openers/sign.js +202 -0
  32. package/dist/openers/wallet.d.ts +71 -0
  33. package/dist/openers/wallet.js +210 -0
  34. package/dist/openers-key-file.d.ts +14 -0
  35. package/dist/openers-key-file.js +99 -0
  36. package/dist/openers.d.ts +7 -0
  37. package/dist/openers.js +21 -0
  38. package/dist/product.d.ts +1 -1
  39. package/dist/product.js +1 -1
  40. package/dist/risk.d.ts +11 -0
  41. package/dist/risk.js +15 -0
  42. package/docs/commands/openers.md +31 -0
  43. package/package.json +5 -1
  44. package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +54 -0
  45. package/vendor/nmts-crypto/nmts_crypto_wasm.js +108 -0
  46. package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
  47. package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +4 -0
@@ -0,0 +1,22 @@
1
+ export interface OpenerHints {
2
+ /**
3
+ * The `nextStep` for a wallet that has no opener on this server — the sign-in that found
4
+ * nothing. ⚠ The account NUMBER is inside the signed bytes, so nothing can search for it.
5
+ */
6
+ noOpener?: string | undefined;
7
+ /** The `nextStep` for a wallet whose two signatures over one message differed. */
8
+ notRepeatable?: string | undefined;
9
+ /** The `nextStep` for a wallet whose signing scheme cannot be used as an opener. */
10
+ wrongKind?: string | undefined;
11
+ /** The `nextStep` for a wallet that already opens THIS account — there is nothing to do. */
12
+ alreadyAttached?: string | undefined;
13
+ /**
14
+ * The `nextStep` for a wallet that already opens ANOTHER account at this account number.
15
+ *
16
+ * ⚠ The way out is another number now and the SAME number at every later sign-in, which is a
17
+ * sentence about the caller's own options — so it is one of these rather than a fixed one.
18
+ */
19
+ opensAnother?: string | undefined;
20
+ /** The `nextStep` for an account that is already holding as many openers as it may. */
21
+ full?: string | undefined;
22
+ }
@@ -0,0 +1,11 @@
1
+ // The words a caller puts in a refusal, where the neutral sentence is not the one to say.
2
+ //
3
+ // ⛔ A TERMINAL NAMES ITS OWN COMMANDS AND A LIBRARY NAMES NONE. "`nmts openers add` attaches this
4
+ // wallet" is the right next step for somebody at a prompt and the wrong one inside somebody
5
+ // else's page, where that program does not exist. So the JUDGEMENT is written once, in this
6
+ // folder, and the sentence that follows it belongs to whoever asked — the same division
7
+ // `storage-control/hints.ts` keeps, for the same reason.
8
+ //
9
+ // ⚠ EVERY FIELD IS OPTIONAL, and what is left out is answered with a sentence that names nothing
10
+ // outside the caller's own program. The SDK passes none of them.
11
+ export {};
@@ -0,0 +1,56 @@
1
+ import type { OpenerHints } from "./hints.ts";
2
+ /** What a wallet hands back: the serialized signature, and what it says it signed. */
3
+ export interface WalletSignature {
4
+ /** The serialized signature, base64 — `flag ‖ signature ‖ public key`, as every wallet sends it. */
5
+ signature: string;
6
+ /** Base64 of the bytes the wallet signed, when it says. Compared, never trusted. */
7
+ bytes?: string | undefined;
8
+ }
9
+ /** A wallet's answer in either shape: the standard's object, or the signature on its own. */
10
+ export type SignedMessage = WalletSignature | string;
11
+ /** Whatever holds the wallet's key: a browser extension, a key file, a hardware device. */
12
+ export type SignWallet = (message: Uint8Array) => Promise<SignedMessage> | SignedMessage;
13
+ /** Which account this wallet opens, and how to ask it. */
14
+ export interface WalletOpener {
15
+ /** The wallet's Sui address, `0x` and 64 LOWERCASE hex. Refused, never repaired. */
16
+ address: string;
17
+ /**
18
+ * Which of this wallet's NMTS accounts. Default 1.
19
+ *
20
+ * ⛔ IT IS INSIDE THE SIGNED BYTES, so nothing can look for the others: a sign-in that finds no
21
+ * opener cannot try 2, 3, 4 without asking the person to sign again, once per number.
22
+ */
23
+ account?: number | undefined;
24
+ /**
25
+ * A product scope. Absent means one wallet opens the same account everywhere; present means this
26
+ * wallet opens a different account for this product alone.
27
+ *
28
+ * ⚠ THE PRICE OF LEAVING IT OUT: every product that gets this signature opens the same files.
29
+ */
30
+ app?: string | undefined;
31
+ sign: SignWallet;
32
+ }
33
+ /** The exact bytes this wallet is asked to sign. Built by the engine; refused, never repaired. */
34
+ export declare function openerMessage(input: WalletOpener): Promise<Uint8Array>;
35
+ /**
36
+ * Get one signature over this account's message, judge it, and lend it to `use`.
37
+ *
38
+ * ⛔ THE SIGNATURE IS NOT THE ANSWER. Whatever `use` returns is; the bytes are dropped when this
39
+ * call ends, and there is deliberately no form of this that hands them back.
40
+ */
41
+ export declare function withSignature<T>(input: WalletOpener, use: (serialized: Uint8Array) => Promise<T>, hints?: OpenerHints): Promise<T>;
42
+ /**
43
+ * Get the signature TWICE and refuse unless both are the same 64 bytes, then lend it to `use`.
44
+ *
45
+ * ⛔ THE ONE CHECK THAT MAKES AN OPENER WORTH STORING. A slot is opened by the same signature that
46
+ * sealed it, so a wallet that signs differently the second time seals something it can never
47
+ * open — and the person would learn that at their next sign-in, with the account behind it.
48
+ * Two signatures now is a question a wallet answers in seconds; the alternative is a slot that
49
+ * looks fine forever.
50
+ *
51
+ * ⚠ IT IS RUN FOR EVERY SCHEME, including the three that sign deterministically. An Ed25519 wallet
52
+ * held by a signing service, an MPC wallet or a hardware wallet with its own nonce rule all
53
+ * present as Ed25519 and all may hedge — the flag says how a signature is checked, not how it
54
+ * was made.
55
+ */
56
+ export declare function withRepeatedSignature<T>(input: WalletOpener, use: (serialized: Uint8Array) => Promise<T>, hints?: OpenerHints): Promise<T>;
@@ -0,0 +1,202 @@
1
+ // The bytes a wallet is asked to sign, and the one place a signature it hands back is judged.
2
+ //
3
+ // ⛔ THE MESSAGE IS THE ENGINE'S AND IS NEVER RETYPED HERE. `opener_message` builds it in Rust so
4
+ // that the browser, this tool and the recovery tool ask for ONE message: a character of drift
5
+ // between them is a different wrapping key, a different locator, and a slot that no longer
6
+ // opens. Nothing in this file writes a line of it, checks an address for itself, or repairs an
7
+ // input the engine refuses — all three are inside what a person reads in the wallet popup.
8
+ //
9
+ // ⛔ THE SERIALIZED SIGNATURE NEVER LEAVES THIS FILE'S CALL STACK. `withSignature` borrows it for
10
+ // the length of one piece of work and answers whatever that work answers; it is not returned,
11
+ // not stored on anything returned, and not closed over by anything that outlives the call. That
12
+ // is review finding 2-B3 kept on this side of the boundary — the Rust crate keeps it on the
13
+ // other (`crypto/src/opener.rs`) — and `cli/test/openers.test.ts` proves the objects these
14
+ // functions hand back cannot reach it.
15
+ //
16
+ // ⛔ THE SCHEME IS READ BEFORE THE SIGNATURE IS VERIFIED, and it is read with the chain library's
17
+ // own flag table rather than a list written here. Three schemes sign the same bytes the same
18
+ // way every time and can be openers; multisig, zkLogin and passkeys cannot, and each of them
19
+ // gets its own reason code because the answer differs — a passkey holder needs to be told that
20
+ // passkeys sign differently every time, not that "something was wrong". The ENGINE refuses them
21
+ // too, and that refusal is the authority; what is here is which sentence a caller can say.
22
+ //
23
+ // ⚠ VERIFIED AGAINST OUR OWN BYTES, ALWAYS. The wallet is asked to sign a message this file built
24
+ // and the signature is checked against that same message and the address it names — never
25
+ // against whatever the wallet says it signed. A wallet that answers `bytes` has them compared
26
+ // first, so "it signed something else" is reported as itself rather than as an invalid signature.
27
+ import { SIGNATURE_FLAG_TO_SCHEME } from "@mysten/sui/cryptography";
28
+ import { verifyPersonalMessageSignature } from "@mysten/sui/verify";
29
+ import { fromBase64Url } from "../bytes.js";
30
+ import { loadCrypto } from "../crypto.js";
31
+ import { NmtsError } from "../errors.js";
32
+ /** The scheme flags that can be an opener, by the chain library's own name for each. */
33
+ const OPENS = ["ED25519", "Secp256k1", "Secp256r1"];
34
+ /**
35
+ * Why a wallet's signing scheme cannot hold an opener. ⛔ ONE CODE PER REASON: the thing to do
36
+ * about a multisig wallet and about a passkey are different, so the two cannot share an answer.
37
+ */
38
+ const REFUSED_SCHEME = {
39
+ // Copy facts — the sentences. Facts, in order: a multisig wallet's bytes depend on which signers
40
+ // took part, so the same message does not always give the same signature; zkLogin's ephemeral
41
+ // key and proof change every session; a passkey signs over a counter, so no two signatures over
42
+ // one message match. None of the three can re-open a slot tomorrow, which is why they are
43
+ // refused at the door rather than discovered later.
44
+ MultiSig: "WALLET_MULTISIG: this wallet signs differently depending on which signers take part.",
45
+ ZkLogin: "WALLET_ZKLOGIN: this wallet signs differently in every session.",
46
+ Passkey: "WALLET_PASSKEY: this wallet signs differently every time.",
47
+ };
48
+ /** The exact bytes this wallet is asked to sign. Built by the engine; refused, never repaired. */
49
+ export async function openerMessage(input) {
50
+ const glue = await loadCrypto();
51
+ const account = input.account ?? 1;
52
+ try {
53
+ return input.app === undefined
54
+ ? glue.opener_message(input.address, account)
55
+ : glue.opener_message(input.address, account, input.app);
56
+ }
57
+ catch (error) {
58
+ // ⚠ The engine's own sentence is carried: it names which of the three inputs was not canonical,
59
+ // and every one of them is a value the caller passed rather than anything secret.
60
+ throw new NmtsError(`WALLET_MESSAGE_REFUSED: ${message(error)}`, {
61
+ exitCode: 2,
62
+ // Copy facts — the next step. Facts: the address is `0x` + 64 lowercase hex, the account
63
+ // number starts at 1, an app is 1–64 of a-z0-9.- starting and ending alphanumeric — and none
64
+ // of the three is tidied up here, because all three are inside what the person signs.
65
+ nextStep: "Nothing was signed. Pass the address exactly as the wallet spells it.",
66
+ });
67
+ }
68
+ }
69
+ /**
70
+ * Get one signature over this account's message, judge it, and lend it to `use`.
71
+ *
72
+ * ⛔ THE SIGNATURE IS NOT THE ANSWER. Whatever `use` returns is; the bytes are dropped when this
73
+ * call ends, and there is deliberately no form of this that hands them back.
74
+ */
75
+ export async function withSignature(input, use, hints = {}) {
76
+ const message = await openerMessage(input);
77
+ return use(await judged(await input.sign(message), message, input.address, hints));
78
+ }
79
+ /**
80
+ * Get the signature TWICE and refuse unless both are the same 64 bytes, then lend it to `use`.
81
+ *
82
+ * ⛔ THE ONE CHECK THAT MAKES AN OPENER WORTH STORING. A slot is opened by the same signature that
83
+ * sealed it, so a wallet that signs differently the second time seals something it can never
84
+ * open — and the person would learn that at their next sign-in, with the account behind it.
85
+ * Two signatures now is a question a wallet answers in seconds; the alternative is a slot that
86
+ * looks fine forever.
87
+ *
88
+ * ⚠ IT IS RUN FOR EVERY SCHEME, including the three that sign deterministically. An Ed25519 wallet
89
+ * held by a signing service, an MPC wallet or a hardware wallet with its own nonce rule all
90
+ * present as Ed25519 and all may hedge — the flag says how a signature is checked, not how it
91
+ * was made.
92
+ */
93
+ export async function withRepeatedSignature(input, use, hints = {}) {
94
+ const message = await openerMessage(input);
95
+ const first = await judged(await input.sign(message), message, input.address, hints);
96
+ const again = await judged(await input.sign(message), message, input.address, hints);
97
+ if (!sameBytes(first, again)) {
98
+ throw new NmtsError(
99
+ // Copy facts — the sentence. Facts: the wallet was asked for the same signature twice and gave
100
+ // two different answers; a slot is opened by the signature that sealed it, so this wallet
101
+ // could not open what it is about to seal. Nothing was stored.
102
+ "WALLET_NOT_REPEATABLE: this wallet signed the same message two different ways.", {
103
+ exitCode: 4,
104
+ nextStep: hints.notRepeatable ??
105
+ "Nothing was stored. A wallet that signs differently each time cannot open what it seals.",
106
+ });
107
+ }
108
+ return use(first);
109
+ }
110
+ /**
111
+ * One wallet answer, turned into serialized bytes this layer will act on — or a named refusal.
112
+ *
113
+ * The order is deliberate: what it signed, then what it signed WITH, then whether the signature
114
+ * holds. Each question's failure has a different answer, and asking them in this order means the
115
+ * reason a caller is given is the first thing that was actually wrong.
116
+ */
117
+ async function judged(answer, message, address, hints) {
118
+ const signature = typeof answer === "string" ? answer : answer.signature;
119
+ const said = typeof answer === "string" ? undefined : answer.bytes;
120
+ if (typeof signature !== "string" || signature === "") {
121
+ throw new NmtsError("WALLET_SIGNATURE_MISSING: the wallet answered no signature.", {
122
+ exitCode: 4,
123
+ nextStep: "Nothing was stored and nothing was opened.",
124
+ });
125
+ }
126
+ // ⛔ FIRST, BECAUSE IT IS THE ONE FAILURE THAT IS NOT ABOUT CRYPTOGRAPHY. A wallet that signed a
127
+ // different message — its own wrapper, a truncation, another site's text — would otherwise be
128
+ // reported as an invalid signature, which sends whoever reads it to look at the wrong thing.
129
+ if (said !== undefined && !sameBytes(fromBase64Url(said), message)) {
130
+ throw new NmtsError("WALLET_SIGNED_OTHER_BYTES: the wallet signed something other than this message.", {
131
+ exitCode: 4,
132
+ // Copy facts — the next step. Facts: what the wallet returned as the signed bytes is not what
133
+ // it was handed; the message carries the address, the account number and the version, and
134
+ // opening depends on those exact bytes. Nothing was stored.
135
+ nextStep: "Nothing was stored. The message this asked for and the one that was signed differ.",
136
+ });
137
+ }
138
+ const serialized = decoded(signature);
139
+ refuseTheScheme(serialized, hints);
140
+ try {
141
+ await verifyPersonalMessageSignature(message, signature, { address });
142
+ }
143
+ catch {
144
+ // ⛔ THE LIBRARY'S OWN MESSAGE IS NOT CARRIED. It differs between "not valid for this message"
145
+ // and "not this address", and both mean one thing to whoever asked: what came back does not
146
+ // belong to the wallet that was named, over the bytes it was given.
147
+ throw new NmtsError("WALLET_SIGNATURE_INVALID: that signature is not this wallet's, over these bytes.", {
148
+ exitCode: 4,
149
+ nextStep: "Nothing was stored and nothing was opened.",
150
+ });
151
+ }
152
+ return serialized;
153
+ }
154
+ /** The serialized signature's bytes, or a refusal that does not quote what was handed in. */
155
+ function decoded(signature) {
156
+ let bytes;
157
+ try {
158
+ bytes = fromBase64Url(signature);
159
+ }
160
+ catch {
161
+ throw new NmtsError("WALLET_SIGNATURE_MALFORMED: the wallet's signature is not base64.", {
162
+ exitCode: 4,
163
+ nextStep: "Nothing was stored and nothing was opened.",
164
+ });
165
+ }
166
+ if (bytes.length === 0) {
167
+ throw new NmtsError("WALLET_SIGNATURE_MALFORMED: the wallet's signature is empty.", {
168
+ exitCode: 4,
169
+ nextStep: "Nothing was stored and nothing was opened.",
170
+ });
171
+ }
172
+ return bytes;
173
+ }
174
+ /**
175
+ * Refuse a scheme that cannot hold an opener, by its own name.
176
+ *
177
+ * ⚠ AN ALLOW LIST. A flag nobody here has judged is refused for being unknown rather than let
178
+ * through for being new — the same rule the engine keeps one layer down.
179
+ */
180
+ function refuseTheScheme(serialized, hints) {
181
+ const flag = serialized[0];
182
+ const scheme = flag === undefined ? undefined : Reflect.get(SIGNATURE_FLAG_TO_SCHEME, String(flag));
183
+ if (typeof scheme === "string" && OPENS.includes(scheme))
184
+ return;
185
+ const named = typeof scheme === "string" ? REFUSED_SCHEME[scheme] : undefined;
186
+ throw new NmtsError(named ??
187
+ // Copy facts — the sentence for a scheme nothing here knows. Fact: this version accepts the
188
+ // three Sui schemes that sign one message the same way every time.
189
+ `WALLET_UNKNOWN_SCHEME: this version does not know how wallet scheme 0x${(flag ?? 0).toString(16).padStart(2, "0")} signs.`, {
190
+ exitCode: 4,
191
+ nextStep: hints.wrongKind ??
192
+ "Nothing was stored and nothing was opened. An opener needs a wallet that signs one message the same way every time.",
193
+ });
194
+ }
195
+ /** Two runs of bytes, compared for being the same. Neither of them is secret. */
196
+ function sameBytes(a, b) {
197
+ return a.length === b.length && a.every((byte, at) => byte === b[at]);
198
+ }
199
+ /** Whatever an engine refusal said, as text. */
200
+ function message(error) {
201
+ return error instanceof Error ? error.message : String(error);
202
+ }
@@ -0,0 +1,71 @@
1
+ import { type OpenerAccess } from "./doors.ts";
2
+ import type { OpenerHints } from "./hints.ts";
3
+ import { type WalletOpener } from "./sign.ts";
4
+ /** What a sign-in with a wallet answers. */
5
+ export interface WalletSignIn {
6
+ /** The NMTS key this wallet opened, in the form a person reads and types. */
7
+ accountCode: string;
8
+ /** The name the slot it opened is filed under — what `remove` takes. */
9
+ locator: string;
10
+ }
11
+ /** What attaching a wallet answers. ⛔ The signature that made it is not here and cannot be. */
12
+ export interface AttachedWallet {
13
+ locator: string;
14
+ kind: "wallet";
15
+ }
16
+ /**
17
+ * Sign in with a wallet: sign once, ask for the slot that name finds, and open it.
18
+ *
19
+ * ⛔ THERE IS NO "WRONG SIGNATURE, RIGHT SLOT" CASE. The name the slot is filed under and the key
20
+ * that opens it come out of the same signature, so a signature that finds a slot opens it —
21
+ * which is why a failure to open is reported as a defect rather than as a wrong wallet.
22
+ *
23
+ * ⚠ AN ACCOUNT NUMBER IS INSIDE THE SIGNED BYTES, so nothing here can look for the others. A
24
+ * sign-in that finds nothing says so; trying 2, 3, 4 would mean asking the person to sign once
25
+ * per number, and the caller is the one that may ask.
26
+ */
27
+ export declare function signInWithWallet(server: string, input: WalletOpener, hints?: OpenerHints): Promise<WalletSignIn>;
28
+ /**
29
+ * Attach this wallet to the account `accountCode` opens.
30
+ *
31
+ * ⛔ THE SIGNATURE IS TAKEN TWICE AND THE TWO MUST MATCH — see `withRepeatedSignature`. A wallet
32
+ * that signs differently the second time would seal a slot it could never open.
33
+ *
34
+ * ⛔ WHAT THIS CREATES IS A STANDING WAY INTO THE ACCOUNT. Whoever holds this wallet can open every
35
+ * file in it, from any machine, until the slot is removed. The server is given a locked lump it
36
+ * cannot open and a name it cannot trace to a wallet.
37
+ *
38
+ * ⛔ AND THE NAME IS ASKED FOR BEFORE ANYTHING IS SEALED OR SENT. One wallet at one account number
39
+ * reaches exactly one locator, so a slot already filed under it is either this account's — the
40
+ * wallet is attached already — or another account's, and a PUT would meet the server's flat
41
+ * "not found" (its deliberate answer for "not yours"). Both are said here instead, in the words
42
+ * that tell the person what to do about it.
43
+ */
44
+ export declare function addWallet(access: OpenerAccess, accountCode: string, input: WalletOpener, hints?: OpenerHints): Promise<AttachedWallet>;
45
+ /**
46
+ * Refuse NOW if this wallet, at this account number, already opens an account on this server.
47
+ *
48
+ * ⛔ FOR THE ONE PATH WHERE THE ACCOUNT DOES NOT EXIST YET. `nmts create --wallet` would otherwise
49
+ * make an account, print its key, and only then learn that the wallet cannot be attached to it —
50
+ * leaving behind an account nobody asked for. One signature answers that before anything is
51
+ * created, and there is nothing to compare the slot against, so every slot found is another
52
+ * account's.
53
+ */
54
+ export declare function refuseIfWalletOpensAnAccount(server: string, input: WalletOpener, hints?: OpenerHints): Promise<void>;
55
+ /**
56
+ * Take one wallet off the account.
57
+ *
58
+ * ⛔ "FROM NOW ON", NOT "AS IF IT NEVER KNEW". A wallet that opened this account once has held the
59
+ * NMTS key. The only answer to that is a new account, and the screen or command that offers
60
+ * removal is where that sentence belongs.
61
+ */
62
+ export declare function removeWallet(access: OpenerAccess, locator: string): Promise<void>;
63
+ /**
64
+ * The sealed 62 bytes filed under one locator — the file the recovery tool opens with a signature.
65
+ *
66
+ * ⛔ IT NEEDS NO CREDENTIAL AND PROVES NOTHING. Whoever has the locator can fetch these bytes, and
67
+ * they are worth nothing without the wallet: the name is derived from the same secret the key
68
+ * is. What it buys is that a person can keep their own copy and open their account with their
69
+ * wallet alone, with this server gone.
70
+ */
71
+ export declare function walletSlot(server: string, locator: string): Promise<Uint8Array>;
@@ -0,0 +1,210 @@
1
+ // What a wallet actually does: open an account, be attached to one, be taken off one.
2
+ //
3
+ // ⛔ ONE IMPLEMENTATION, FOUR CALLERS — the command line, the SDK, a page and the recovery tool
4
+ // ask for the same acts with different surfaces around them. The order inside each act is the
5
+ // part that must not be written twice: which bytes are signed, what is compared against what,
6
+ // when the account's 20 key bytes exist and when they are wiped, and what the server is told.
7
+ //
8
+ // ⛔ NOTHING HERE PRINTS, PROMPTS, OR CHOOSES AN EXIT CODE. A terminal shows the address and asks;
9
+ // a page shows a button; a library caller decided by calling. Every refusal is thrown with a
10
+ // machine code at the front of it, and the sentence after that code is the caller's to replace.
11
+ //
12
+ // ⛔ THE ACCOUNT CODE IS BORROWED, NOT KEPT. `addWallet` takes it, parses it to the 20 bytes the
13
+ // engine seals, and wipes those bytes on every path out — including the failing one. What comes
14
+ // back from an attach is a locator and nothing else; what comes back from a sign-in IS the
15
+ // account, and it goes straight to whoever asked and nowhere else.
16
+ import { toBase64Url } from "../bytes.js";
17
+ import { loadCrypto } from "../crypto.js";
18
+ import { NmtsError } from "../errors.js";
19
+ import { fetchSlot, KIND_WALLET, putOpener, removeOpener, } from "./doors.js";
20
+ import { withRepeatedSignature, withSignature } from "./sign.js";
21
+ /**
22
+ * Sign in with a wallet: sign once, ask for the slot that name finds, and open it.
23
+ *
24
+ * ⛔ THERE IS NO "WRONG SIGNATURE, RIGHT SLOT" CASE. The name the slot is filed under and the key
25
+ * that opens it come out of the same signature, so a signature that finds a slot opens it —
26
+ * which is why a failure to open is reported as a defect rather than as a wrong wallet.
27
+ *
28
+ * ⚠ AN ACCOUNT NUMBER IS INSIDE THE SIGNED BYTES, so nothing here can look for the others. A
29
+ * sign-in that finds nothing says so; trying 2, 3, 4 would mean asking the person to sign once
30
+ * per number, and the caller is the one that may ask.
31
+ */
32
+ export async function signInWithWallet(server, input, hints = {}) {
33
+ const glue = await loadCrypto();
34
+ return withSignature(input, async (serialized) => {
35
+ const locator = toBase64Url(glue.opener_locator(serialized));
36
+ const slot = await fetchSlot(server, locator);
37
+ if (slot === null) {
38
+ throw new NmtsError(
39
+ // Copy facts — the sentence. Facts: this wallet, at this account number, has no opener on
40
+ // this server; nothing was created and nothing was sent but the lookup. Another number
41
+ // needs another signature, because the number is inside what was signed.
42
+ "NO_OPENER_FOR_WALLET: no NMTS account on this server opens with this wallet.", {
43
+ exitCode: 4,
44
+ nextStep: hints.noOpener ??
45
+ "Nothing was opened. A different account number is a different message, so it needs a new signature.",
46
+ });
47
+ }
48
+ const key = glue.opener_open(serialized, slot);
49
+ try {
50
+ return { accountCode: glue.account_code_display(key), locator };
51
+ }
52
+ finally {
53
+ key.fill(0);
54
+ }
55
+ }, hints);
56
+ }
57
+ /**
58
+ * Attach this wallet to the account `accountCode` opens.
59
+ *
60
+ * ⛔ THE SIGNATURE IS TAKEN TWICE AND THE TWO MUST MATCH — see `withRepeatedSignature`. A wallet
61
+ * that signs differently the second time would seal a slot it could never open.
62
+ *
63
+ * ⛔ WHAT THIS CREATES IS A STANDING WAY INTO THE ACCOUNT. Whoever holds this wallet can open every
64
+ * file in it, from any machine, until the slot is removed. The server is given a locked lump it
65
+ * cannot open and a name it cannot trace to a wallet.
66
+ *
67
+ * ⛔ AND THE NAME IS ASKED FOR BEFORE ANYTHING IS SEALED OR SENT. One wallet at one account number
68
+ * reaches exactly one locator, so a slot already filed under it is either this account's — the
69
+ * wallet is attached already — or another account's, and a PUT would meet the server's flat
70
+ * "not found" (its deliberate answer for "not yours"). Both are said here instead, in the words
71
+ * that tell the person what to do about it.
72
+ */
73
+ export async function addWallet(access, accountCode, input, hints = {}) {
74
+ const glue = await loadCrypto();
75
+ return withRepeatedSignature(input, async (serialized) => {
76
+ const locator = toBase64Url(glue.opener_locator(serialized));
77
+ const taken = await fetchSlot(access.server, locator);
78
+ if (taken !== null)
79
+ refuseTakenName(glue, serialized, taken, accountCode, input.account ?? 1, hints);
80
+ const key = keyBytes(glue.account_code_parse.bind(glue), accountCode);
81
+ let slot;
82
+ try {
83
+ slot = glue.opener_seal(serialized, key);
84
+ }
85
+ finally {
86
+ key.fill(0);
87
+ }
88
+ await putOpener(access, locator, KIND_WALLET, slot);
89
+ return { locator, kind: "wallet" };
90
+ }, hints);
91
+ }
92
+ /**
93
+ * Refuse NOW if this wallet, at this account number, already opens an account on this server.
94
+ *
95
+ * ⛔ FOR THE ONE PATH WHERE THE ACCOUNT DOES NOT EXIST YET. `nmts create --wallet` would otherwise
96
+ * make an account, print its key, and only then learn that the wallet cannot be attached to it —
97
+ * leaving behind an account nobody asked for. One signature answers that before anything is
98
+ * created, and there is nothing to compare the slot against, so every slot found is another
99
+ * account's.
100
+ */
101
+ export async function refuseIfWalletOpensAnAccount(server, input, hints = {}) {
102
+ const glue = await loadCrypto();
103
+ await withSignature(input, async (serialized) => {
104
+ const locator = toBase64Url(glue.opener_locator(serialized));
105
+ if ((await fetchSlot(server, locator)) !== null)
106
+ throw opensAnother(input.account ?? 1, hints);
107
+ }, hints);
108
+ }
109
+ /**
110
+ * What a locator that is already taken means, said as one of two refusals.
111
+ *
112
+ * ⛔ THE PROBE'S OPENED KEY IS A SECRET LIKE ANY OTHER. It is compared and wiped on every path out,
113
+ * including the throwing ones, and it is never returned, printed or put in a message.
114
+ *
115
+ * ⚠ A SLOT THAT WILL NOT OPEN COUNTS AS ANOTHER ACCOUNT'S. The name comes from the same signature
116
+ * the opening key does, so this cannot happen to a sound slot; what it must never become is a
117
+ * PUT that walks into the server's "not found".
118
+ */
119
+ function refuseTakenName(glue, serialized, slot, accountCode, account, hints) {
120
+ let opened = null;
121
+ let mine = null;
122
+ try {
123
+ try {
124
+ opened = glue.opener_open(serialized, slot);
125
+ }
126
+ catch {
127
+ opened = null;
128
+ }
129
+ if (opened !== null) {
130
+ const key = keyBytes(glue.account_code_parse.bind(glue), accountCode);
131
+ mine = key;
132
+ if (opened.length === key.length && opened.every((byte, at) => byte === key[at])) {
133
+ throw new NmtsError("WALLET_ALREADY_ATTACHED: this wallet already opens this account.", {
134
+ exitCode: 4,
135
+ nextStep: hints.alreadyAttached ?? "Nothing was stored.",
136
+ });
137
+ }
138
+ }
139
+ throw opensAnother(account, hints);
140
+ }
141
+ finally {
142
+ opened?.fill(0);
143
+ mine?.fill(0);
144
+ }
145
+ }
146
+ /** The refusal for a name that belongs to an account this key does not open. */
147
+ function opensAnother(account, hints) {
148
+ return new NmtsError(`WALLET_OPENS_ANOTHER_ACCOUNT: under account number ${account}, this wallet already opens another NMTS account.`, {
149
+ exitCode: 4,
150
+ nextStep: hints.opensAnother ??
151
+ "Nothing was stored. One wallet and one number open one account: use another account number, and the same number when signing in.",
152
+ });
153
+ }
154
+ /**
155
+ * Take one wallet off the account.
156
+ *
157
+ * ⛔ "FROM NOW ON", NOT "AS IF IT NEVER KNEW". A wallet that opened this account once has held the
158
+ * NMTS key. The only answer to that is a new account, and the screen or command that offers
159
+ * removal is where that sentence belongs.
160
+ */
161
+ export async function removeWallet(access, locator) {
162
+ await removeOpener(access, requiredLocator(locator));
163
+ }
164
+ /**
165
+ * The sealed 62 bytes filed under one locator — the file the recovery tool opens with a signature.
166
+ *
167
+ * ⛔ IT NEEDS NO CREDENTIAL AND PROVES NOTHING. Whoever has the locator can fetch these bytes, and
168
+ * they are worth nothing without the wallet: the name is derived from the same secret the key
169
+ * is. What it buys is that a person can keep their own copy and open their account with their
170
+ * wallet alone, with this server gone.
171
+ */
172
+ export async function walletSlot(server, locator) {
173
+ const slot = await fetchSlot(server, requiredLocator(locator));
174
+ if (slot === null) {
175
+ throw new NmtsError("OPENER_NOT_FOUND: this server has no slot filed under that name.", {
176
+ exitCode: 4,
177
+ // Copy facts — the next step. Fact: an unknown name, a malformed one and somebody else's are
178
+ // one answer on purpose — any difference between them is a way to learn which names exist.
179
+ nextStep: "Nothing was fetched. The list of this account's openers has the names it holds.",
180
+ });
181
+ }
182
+ return slot;
183
+ }
184
+ /** A locator that is at least present. What it NAMES is the server's to judge. */
185
+ function requiredLocator(locator) {
186
+ if (locator.trim() === "") {
187
+ throw new NmtsError("OPENER_LOCATOR_MISSING: no opener was named.", {
188
+ exitCode: 2,
189
+ nextStep: "Nothing was sent. Name the opener by the locator the list prints.",
190
+ });
191
+ }
192
+ return locator.trim();
193
+ }
194
+ /**
195
+ * The account's 20 key bytes, with the one refusal this tool words for a code that is not one.
196
+ *
197
+ * ⚠ The engine's own message is not repeated: it can contain the input, and the input here is the
198
+ * account itself.
199
+ */
200
+ function keyBytes(parse, accountCode) {
201
+ try {
202
+ return parse(accountCode);
203
+ }
204
+ catch {
205
+ throw new NmtsError("ACCOUNT_CODE_INVALID: that is not a valid NMTS key.", {
206
+ exitCode: 2,
207
+ nextStep: "Nothing was stored. The last character is a check symbol; look for a mistyped one.",
208
+ });
209
+ }
210
+ }
@@ -0,0 +1,14 @@
1
+ import type { SignWallet } from "./openers.ts";
2
+ /** A wallet this run can ask for signatures: which address it is, and how to ask. */
3
+ export interface KeyFileWallet {
4
+ address: string;
5
+ sign: SignWallet;
6
+ }
7
+ /**
8
+ * The wallet held in `path`, ready to sign the opener message.
9
+ *
10
+ * The file holds one `suiprivkey1…` line — what `sui keytool export` writes and what every Sui
11
+ * tool reads. Surrounding whitespace and a trailing newline are ignored, because writing a secret
12
+ * to a file with `echo` appends one and refusing it would be a puzzle with no clue.
13
+ */
14
+ export declare function walletFromKeyFile(path: string): KeyFileWallet;