@needmoretruth/nmts-cli 0.39.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.
- package/CHANGELOG.md +23 -0
- package/README.ko.md +27 -1
- package/README.md +29 -1
- package/dist/api-advice.js +6 -0
- package/dist/api.d.ts +10 -1
- package/dist/api.js +12 -2
- package/dist/arg-options.d.ts +21 -0
- package/dist/args.js +12 -1
- package/dist/artifact-about.d.ts +1 -1
- package/dist/commands/create-link.d.ts +9 -0
- package/dist/commands/create-link.js +31 -1
- package/dist/commands/create.d.ts +9 -0
- package/dist/commands/create.js +17 -0
- package/dist/commands/extend.d.ts +3 -3
- package/dist/commands/extend.js +52 -161
- package/dist/commands/login-passphrase.d.ts +13 -0
- package/dist/commands/login-passphrase.js +64 -0
- package/dist/commands/login.d.ts +3 -0
- package/dist/commands/login.js +15 -55
- package/dist/commands/openers-attach.d.ts +38 -0
- package/dist/commands/openers-attach.js +97 -0
- package/dist/commands/openers.d.ts +23 -0
- package/dist/commands/openers.js +195 -0
- package/dist/commands/wallet-storage-ops.d.ts +2 -2
- package/dist/commands/wallet-storage-ops.js +108 -134
- package/dist/commands/wallet-storage.d.ts +3 -8
- package/dist/commands/wallet-storage.js +16 -37
- package/dist/crypto-surface.d.ts +28 -0
- package/dist/crypto-surface.js +7 -0
- package/dist/help.js +15 -0
- package/dist/main.js +11 -12
- package/dist/openers/doors.d.ts +65 -0
- package/dist/openers/doors.js +97 -0
- package/dist/openers/hints.d.ts +22 -0
- package/dist/openers/hints.js +11 -0
- package/dist/openers/sign.d.ts +56 -0
- package/dist/openers/sign.js +202 -0
- package/dist/openers/wallet.d.ts +71 -0
- package/dist/openers/wallet.js +210 -0
- package/dist/openers-key-file.d.ts +14 -0
- package/dist/openers-key-file.js +99 -0
- package/dist/openers.d.ts +7 -0
- package/dist/openers.js +21 -0
- package/dist/product.d.ts +1 -1
- package/dist/product.js +1 -1
- package/dist/risk.d.ts +11 -0
- package/dist/risk.js +15 -0
- package/dist/storage-control/extend.d.ts +124 -0
- package/dist/storage-control/extend.js +189 -0
- package/dist/storage-control/hints.d.ts +20 -0
- package/dist/storage-control/hints.js +11 -0
- package/dist/storage-control/list.d.ts +55 -0
- package/dist/storage-control/list.js +71 -0
- package/dist/storage-control/reshape.d.ts +120 -0
- package/dist/storage-control/reshape.js +188 -0
- package/dist/storage-control-chain.d.ts +1 -1
- package/dist/storage-control.d.ts +13 -0
- package/dist/storage-control.js +24 -0
- package/dist/wallet-storage-chain.d.ts +1 -1
- package/docs/commands/openers.md +31 -0
- package/package.json +9 -1
- package/vendor/nmts-crypto/nmts_crypto_wasm.d.ts +54 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm.js +108 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm +0 -0
- package/vendor/nmts-crypto/nmts_crypto_wasm_bg.wasm.d.ts +4 -0
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { ParsedArgs } from "../args.ts";
|
|
2
|
+
import { type OpenerHints, type WalletOpener } from "../openers.ts";
|
|
3
|
+
import { type KeyFileWallet } from "../openers-key-file.ts";
|
|
4
|
+
export interface OpenersCommandOptions {
|
|
5
|
+
write?: ((line: string) => void) | undefined;
|
|
6
|
+
/** Injected in tests: a wallet that is not read from a file on a disk. */
|
|
7
|
+
wallet?: KeyFileWallet | undefined;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* The sentences a TERMINAL puts in the library's refusals. A library names no commands, so these
|
|
11
|
+
* are the caller's half of each judgement (`openers/hints.ts`).
|
|
12
|
+
*/
|
|
13
|
+
export declare const OPENER_HINTS: OpenerHints;
|
|
14
|
+
/** `nmts openers [add|remove <locator>]`. */
|
|
15
|
+
export declare function openers(verb: string | undefined, args: ParsedArgs, options?: OpenersCommandOptions): Promise<number>;
|
|
16
|
+
/**
|
|
17
|
+
* The wallet this command line names, and which account of it to open.
|
|
18
|
+
*
|
|
19
|
+
* ⚠ THE ADDRESS IS THE KEY FILE'S OWN. It is not an option: an address typed beside a key file is
|
|
20
|
+
* two answers to one question, and the message a person signs carries whichever of them is
|
|
21
|
+
* right only by luck.
|
|
22
|
+
*/
|
|
23
|
+
export declare function walletOpenerFrom(args: ParsedArgs, options?: OpenersCommandOptions): WalletOpener;
|
|
@@ -0,0 +1,195 @@
|
|
|
1
|
+
// `nmts openers` — the wallets that open this account: what they are, adding one, taking one off.
|
|
2
|
+
//
|
|
3
|
+
// ⛔ THE LIST IS AN INVENTORY OF WAYS INTO THE ACCOUNT, which is why all three verbs present the
|
|
4
|
+
// NMTS key's own proof beside the API key. Adding one makes a standing road in that outlives
|
|
5
|
+
// whatever made it; removing one takes a road away; and seeing the list is seeing every road
|
|
6
|
+
// somebody's files can be reached by. All three are acts of whoever holds the key.
|
|
7
|
+
//
|
|
8
|
+
// ⛔ WHAT IS DECIDED HERE IS ONLY WHAT A TERMINAL DECIDES: which file the wallet is read from, what
|
|
9
|
+
// is printed, and the exit code. The acts themselves are `openers.ts`, which the SDK calls with
|
|
10
|
+
// none of those — so "which bytes are signed and when the key bytes are wiped" is written once.
|
|
11
|
+
//
|
|
12
|
+
// ⛔ THE WALLET'S SECRET IS NEVER AN ARGUMENT. `--sui-key-file` names a FILE; the key is read from
|
|
13
|
+
// it, used to sign one message, and never printed — see `openers-key-file.ts`.
|
|
14
|
+
//
|
|
15
|
+
// ⚠ Copy facts — every sentence below that a person reads. The facts each one has to carry are in
|
|
16
|
+
// the comment above it, and the machine codes in the refusals are not copy — they are the
|
|
17
|
+
// contract a program branches on.
|
|
18
|
+
import { accountProofFor } from "../account-proof.js";
|
|
19
|
+
import { requireAccountCode } from "../code-access.js";
|
|
20
|
+
import { readCredentialsFile } from "../credentials.js";
|
|
21
|
+
import { NmtsError } from "../errors.js";
|
|
22
|
+
import { addWallet, listOpeners, removeWallet } from "../openers.js";
|
|
23
|
+
import { walletFromKeyFile } from "../openers-key-file.js";
|
|
24
|
+
import { BINARY_NAME } from "../product.js";
|
|
25
|
+
import { resolveServer } from "../server.js";
|
|
26
|
+
import { requireApiKey } from "../session.js";
|
|
27
|
+
/**
|
|
28
|
+
* The sentences a TERMINAL puts in the library's refusals. A library names no commands, so these
|
|
29
|
+
* are the caller's half of each judgement (`openers/hints.ts`).
|
|
30
|
+
*/
|
|
31
|
+
export const OPENER_HINTS = {
|
|
32
|
+
// Copy facts — Facts: the wallet signed, and this server has no slot under the name that
|
|
33
|
+
// signature makes; the account number is inside the signed bytes, so another number means
|
|
34
|
+
// another signature; `--account n` is how to ask for one.
|
|
35
|
+
noOpener: `Nothing was opened. \`${BINARY_NAME} login --wallet --sui-key-file <file> --account 2\` asks a different number.`,
|
|
36
|
+
// Copy facts — Facts: the wallet was asked for the same signature twice and answered differently,
|
|
37
|
+
// so it could not open what it was about to seal; nothing was stored.
|
|
38
|
+
notRepeatable: `Nothing was stored. This wallet cannot be used for wallet login; \`${BINARY_NAME} login\` takes the NMTS key itself.`,
|
|
39
|
+
// Copy facts — Facts: multisig, zkLogin and passkey wallets sign differently every time; an
|
|
40
|
+
// opener needs a wallet whose signature over one message never changes.
|
|
41
|
+
wrongKind: `Nothing was stored. \`${BINARY_NAME} openers\` lists the wallets this account already has.`,
|
|
42
|
+
// ⛔ FINAL COPY, both of these. They are the two answers to a locator that is already taken, and
|
|
43
|
+
// the second one has to say the rule as well as the way out: the number used to attach is the
|
|
44
|
+
// number that must be given at every later sign-in.
|
|
45
|
+
alreadyAttached: `Nothing was stored. \`${BINARY_NAME} openers\` lists it.`,
|
|
46
|
+
opensAnother: `Nothing was stored. One wallet and one number open one account: pass another number with --account, and use the same number with \`${BINARY_NAME} login --wallet\`.`,
|
|
47
|
+
};
|
|
48
|
+
/** `nmts openers [add|remove <locator>]`. */
|
|
49
|
+
export async function openers(verb, args, options = {}) {
|
|
50
|
+
const say = options.write ?? ((line) => process.stdout.write(`${line}\n`));
|
|
51
|
+
const asked = (verb ?? "").trim();
|
|
52
|
+
if (asked === "" || asked === "list")
|
|
53
|
+
return await list(args, say);
|
|
54
|
+
if (asked === "add")
|
|
55
|
+
return await add(args, say, options);
|
|
56
|
+
if (asked === "remove")
|
|
57
|
+
return await remove(args, say);
|
|
58
|
+
throw new NmtsError(`\`${BINARY_NAME} openers\` has no verb "${asked}".`, {
|
|
59
|
+
exitCode: 2,
|
|
60
|
+
nextStep: `The verbs are: (none) to list, \`add\`, and \`remove <locator>\`.`,
|
|
61
|
+
});
|
|
62
|
+
}
|
|
63
|
+
/** What this account holds, and how many it may. */
|
|
64
|
+
async function list(args, say) {
|
|
65
|
+
const { access } = await reach(args);
|
|
66
|
+
const listing = await listOpeners(access);
|
|
67
|
+
if (args.json) {
|
|
68
|
+
say(JSON.stringify({ openers: listing.openers, cap: listing.cap }));
|
|
69
|
+
return 0;
|
|
70
|
+
}
|
|
71
|
+
// Copy facts — Facts: each row is a way into this account; `kind` says what holds it; the date is
|
|
72
|
+
// when it was added; the count against the cap is what the account may hold.
|
|
73
|
+
say(`${listing.openers.length} of ${listing.cap} openers on this account. An opener is a wallet that opens it without the NMTS key typed.`);
|
|
74
|
+
for (const one of listing.openers)
|
|
75
|
+
say(` ${one.locator} ${one.kind} ${one.createdAt}`);
|
|
76
|
+
if (listing.openers.length === 0) {
|
|
77
|
+
// Copy facts — Facts: nothing but the NMTS key opens this account today; adding a wallet is one
|
|
78
|
+
// command; whoever holds an added wallet can open every file in the account.
|
|
79
|
+
say(``);
|
|
80
|
+
say(` Only the NMTS key opens this account. \`${BINARY_NAME} openers add --sui-key-file <file>\` adds a wallet.`);
|
|
81
|
+
}
|
|
82
|
+
return 0;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* A locator as it must be TYPED, which is not always as it reads.
|
|
86
|
+
*
|
|
87
|
+
* ⛔ ABOUT ONE LOCATOR IN 64 BEGINS WITH `-`: it is 16 bytes written in base64url and that alphabet
|
|
88
|
+
* has a dash in it. Pasted onto a command line as it stands, such a name is an option to the
|
|
89
|
+
* shell and to this tool, so a line printed here for a person to copy carries the `--` that ends
|
|
90
|
+
* the options — the same way this tool already takes a file named `-h`.
|
|
91
|
+
*/
|
|
92
|
+
function asTyped(locator) {
|
|
93
|
+
return locator.startsWith("-") ? `-- ${locator}` : locator;
|
|
94
|
+
}
|
|
95
|
+
/** Attach one wallet to this account. */
|
|
96
|
+
async function add(args, say, options) {
|
|
97
|
+
const { access, code } = await reach(args);
|
|
98
|
+
const attached = await addWallet(access, code, walletOpenerFrom(args, options), OPENER_HINTS);
|
|
99
|
+
if (args.json) {
|
|
100
|
+
say(JSON.stringify({ locator: attached.locator, kind: attached.kind }));
|
|
101
|
+
return 0;
|
|
102
|
+
}
|
|
103
|
+
// Copy facts — Facts: this wallet now opens this account from any machine; the server holds a
|
|
104
|
+
// locked slot it cannot open and a name it cannot trace to a wallet; removing it later stops it
|
|
105
|
+
// opening the account from then on and cannot unknow what it has already opened.
|
|
106
|
+
say(`This wallet now opens this account.`);
|
|
107
|
+
say(``);
|
|
108
|
+
say(` ${attached.locator}`);
|
|
109
|
+
say(``);
|
|
110
|
+
say(` Whoever holds this wallet can open every file in the account until it is removed.`);
|
|
111
|
+
say(` \`${BINARY_NAME} openers remove ${asTyped(attached.locator)}\` takes it off.`);
|
|
112
|
+
return 0;
|
|
113
|
+
}
|
|
114
|
+
/** Take one wallet off this account. */
|
|
115
|
+
async function remove(args, say) {
|
|
116
|
+
const locator = (args.operands[1] ?? "").trim();
|
|
117
|
+
if (locator === "") {
|
|
118
|
+
throw new NmtsError(`\`${BINARY_NAME} openers remove\` needs the locator of the opener to take off.`, {
|
|
119
|
+
exitCode: 2,
|
|
120
|
+
nextStep: `\`${BINARY_NAME} openers\` prints the locators this account holds.`,
|
|
121
|
+
});
|
|
122
|
+
}
|
|
123
|
+
const { access } = await reach(args);
|
|
124
|
+
await removeWallet(access, locator);
|
|
125
|
+
if (args.json) {
|
|
126
|
+
say(JSON.stringify({ removed: locator }));
|
|
127
|
+
return 0;
|
|
128
|
+
}
|
|
129
|
+
// Copy facts — Facts: from now on that wallet cannot open this account; it is not "as if it never
|
|
130
|
+
// knew" — a wallet that opened the account once has held the NMTS key, and the only answer to
|
|
131
|
+
// that is a new account.
|
|
132
|
+
say(`Removed ${locator}. That wallet cannot open this account from now on.`);
|
|
133
|
+
say(` Removing does not undo the past: a wallet that opened this account once has held the NMTS key.`);
|
|
134
|
+
return 0;
|
|
135
|
+
}
|
|
136
|
+
/**
|
|
137
|
+
* The account this run acts on, and the proof the three doors ask for.
|
|
138
|
+
*
|
|
139
|
+
* ⚠ NO NETWORK IS RESOLVED, and that is deliberate — the same reason `key new` does not resolve
|
|
140
|
+
* one. Nothing here touches a chain or the storage network, so demanding to be told which
|
|
141
|
+
* network a development server uses would refuse a run that was never going to use the answer.
|
|
142
|
+
*/
|
|
143
|
+
async function reach(args) {
|
|
144
|
+
const held = await requireAccountCode();
|
|
145
|
+
const apiKey = requireApiKey();
|
|
146
|
+
const stored = readCredentialsFile();
|
|
147
|
+
// ⛔ THE PROOF IS BUILT FOR THIS ONE RUN AND NOTHING KEEPS IT — `account-proof.ts`.
|
|
148
|
+
const accountProof = await accountProofFor(held);
|
|
149
|
+
return {
|
|
150
|
+
access: { server: resolveServer(args.server ?? stored?.server), apiKey, accountProof },
|
|
151
|
+
code: held.code,
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
/**
|
|
155
|
+
* The wallet this command line names, and which account of it to open.
|
|
156
|
+
*
|
|
157
|
+
* ⚠ THE ADDRESS IS THE KEY FILE'S OWN. It is not an option: an address typed beside a key file is
|
|
158
|
+
* two answers to one question, and the message a person signs carries whichever of them is
|
|
159
|
+
* right only by luck.
|
|
160
|
+
*/
|
|
161
|
+
export function walletOpenerFrom(args, options = {}) {
|
|
162
|
+
const path = (args.suiKeyFile ?? "").trim();
|
|
163
|
+
if (options.wallet === undefined && path === "") {
|
|
164
|
+
throw new NmtsError(`--sui-key-file names the file holding the wallet's \`suiprivkey1…\` line.`, {
|
|
165
|
+
exitCode: 2,
|
|
166
|
+
// Copy facts — Facts: the secret is never an option value because a command line is readable
|
|
167
|
+
// by other processes and recorded by the shell; the file is read and never copied.
|
|
168
|
+
nextStep: `Nothing was signed. Write the wallet's key to a file and name it with --sui-key-file.`,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
const wallet = options.wallet ?? walletFromKeyFile(path);
|
|
172
|
+
const app = (args.app ?? "").trim();
|
|
173
|
+
return {
|
|
174
|
+
address: wallet.address,
|
|
175
|
+
sign: wallet.sign,
|
|
176
|
+
account: accountNumber(args.account),
|
|
177
|
+
...(app === "" ? {} : { app }),
|
|
178
|
+
};
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Which of this wallet's accounts. ⛔ Whole and from 1: the engine refuses 0, and the number is
|
|
182
|
+
* inside what the person signs, so it is never tidied up here.
|
|
183
|
+
*/
|
|
184
|
+
function accountNumber(given) {
|
|
185
|
+
const text = (given ?? "").trim();
|
|
186
|
+
if (text === "")
|
|
187
|
+
return 1;
|
|
188
|
+
if (!/^[1-9][0-9]{0,9}$/u.test(text)) {
|
|
189
|
+
throw new NmtsError(`--account takes a whole number from 1, not "${text}".`, {
|
|
190
|
+
exitCode: 2,
|
|
191
|
+
nextStep: `Nothing was signed. One wallet opens a different account per number, and 1 is the default.`,
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
return Number(text);
|
|
195
|
+
}
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { type Network } from "../network.ts";
|
|
2
|
-
import { type StorageOpsReads } from "../storage-control
|
|
2
|
+
import { type StorageOp, type StorageOpsReads } from "../storage-control.ts";
|
|
3
3
|
import type { SignStorageOp } from "../wallet-sign.ts";
|
|
4
|
-
export type StorageOp
|
|
4
|
+
export type { StorageOp } from "../storage-control.ts";
|
|
5
5
|
export interface StorageOpsOptions {
|
|
6
6
|
server?: string | undefined;
|
|
7
7
|
network?: string | undefined;
|
|
@@ -1,13 +1,14 @@
|
|
|
1
1
|
// `nmts wallet storage split|merge|transfer` — reshaping the storage resources the wallet holds,
|
|
2
|
-
// and handing one over. ⛔ EACH SIGNS.
|
|
3
|
-
//
|
|
4
|
-
//
|
|
2
|
+
// and handing one over. ⛔ EACH SIGNS. What the contract allows, what a change would leave behind,
|
|
3
|
+
// the dry run that prices it and the signature are `storage-control.ts`, which the SDK calls as
|
|
4
|
+
// well; this file is the terminal over it: the words typed, the review read, and `--yes`.
|
|
5
5
|
//
|
|
6
|
-
// ⛔ THE ORDER IS THE SAFETY,
|
|
7
|
-
// the named ones must be this wallet's ② the rules judge the request and say why not ③ the
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
6
|
+
// ⛔ THE ORDER IS THE SAFETY, and it is kept over there: ① the resources are READ from the chain and
|
|
7
|
+
// the named ones must be this wallet's ② the rules judge the request and say why not ③ the exact
|
|
8
|
+
// transaction is dry-run for its fee, and a refusal there ends the run ④ the review ⑤ without
|
|
9
|
+
// `--yes` that is the end ⑥ the wallet unlock is held against the fee — scope `storage` for
|
|
10
|
+
// cutting and joining, scope `all` for handing over ⑦ only then the signature. Steps ⑤ and ⑥ are
|
|
11
|
+
// this file's, handed in as the gate.
|
|
11
12
|
//
|
|
12
13
|
// ⛔ HANDING OVER MOVES NO FILE. The owner's sentence for the browser holds here: what goes is size
|
|
13
14
|
// and remaining time; a file's bytes are sealed with keys the NMTS key derives and stay
|
|
@@ -18,20 +19,12 @@ import { NmtsError } from "../errors.js";
|
|
|
18
19
|
import { resolveNetwork } from "../network.js";
|
|
19
20
|
import { BINARY_NAME } from "../product.js";
|
|
20
21
|
import { resolveServer } from "../server.js";
|
|
21
|
-
import {
|
|
22
|
+
import { reshapeStorage, } from "../storage-control.js";
|
|
22
23
|
import { explorerTxUrl } from "../shared/lib/wallet/activity.js";
|
|
23
|
-
import { isValidSuiAddress } from "../shared/lib/wallet/send-rules.js";
|
|
24
|
-
import { storageOpsReads } from "../storage-control-chain.js";
|
|
25
24
|
import { coinAmount, walletAddress } from "../wallet.js";
|
|
26
25
|
import { payingWalletIndex } from "../wallet-pay-index.js";
|
|
27
26
|
import { recordWalletSpend, requireWalletGrant } from "../wallet-grant.js";
|
|
28
27
|
import { formatBytes } from "./wallet-storage.js";
|
|
29
|
-
const FUSE_WHY = {
|
|
30
|
-
same: "those are the same resource.",
|
|
31
|
-
differentSize: "their periods differ and so do their sizes — the contract joins sizes only over an identical period, and periods only at an identical size.",
|
|
32
|
-
differentPeriod: "their periods differ.",
|
|
33
|
-
notAdjacent: "their sizes are equal but their periods do not touch — one has to end where the other starts.",
|
|
34
|
-
};
|
|
35
28
|
export async function walletStorageOps(op, rest, options = {}) {
|
|
36
29
|
const say = options.write ?? ((line) => process.stdout.write(`${line}\n`));
|
|
37
30
|
const resolved = await requireAccountCode();
|
|
@@ -42,57 +35,43 @@ export async function walletStorageOps(op, rest, options = {}) {
|
|
|
42
35
|
const stored = resolved.source === "file" || resolved.source === "file-locked" ? readCredentialsFile() : null;
|
|
43
36
|
const server = resolveServer(options.server ?? stored?.server);
|
|
44
37
|
const network = resolveNetwork(server, options.network ?? stored?.network);
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
exitCode:
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
38
|
+
// What was typed, as values. Everything past this is judged against the chain's own answer.
|
|
39
|
+
const { ask, command } = asked(op, rest, options);
|
|
40
|
+
// ⛔ WITHOUT `--yes` THE RUN STOPS AT THE REVIEW, exactly as `--dry-run` does — the two differ
|
|
41
|
+
// only in what is said afterwards and in the exit code, which is this file's business.
|
|
42
|
+
const stopAtReview = options.dryRun === true || options.yes !== true;
|
|
43
|
+
const outcome = await reshapeStorage({ network, code: resolved.code, wallet, address }, ask, {
|
|
44
|
+
reads: options.storageReads,
|
|
45
|
+
sign: options.signStorage,
|
|
46
|
+
dryRun: stopAtReview,
|
|
47
|
+
// ⚠ THE SENTENCES A PERSON READS ARE THIS FILE'S. The library judges; what a refusal calls
|
|
48
|
+
// the thing that was wrong — a flag, another command — belongs where somebody typed it.
|
|
49
|
+
hints: {
|
|
50
|
+
cannotRead: "Nothing was signed.",
|
|
51
|
+
notHeld: `\`${BINARY_NAME} wallet storage\` lists the ones it holds. A resource bound inside a file is not free.`,
|
|
52
|
+
cut: (what, limit) => new NmtsError(what === "size"
|
|
53
|
+
? `--size must be above 0 and below the resource's ${limit}.`
|
|
54
|
+
: `--epochs must be a whole number above 0 and below the resource's ${limit} epochs.`, { exitCode: 2 }),
|
|
55
|
+
},
|
|
56
|
+
onReview: (review) => {
|
|
57
|
+
if (!options.json)
|
|
58
|
+
describe(say, review, wallet);
|
|
59
|
+
},
|
|
60
|
+
// ⑥ The unlock, held against the fee this review measured. ⛔ Throwing here signs nothing.
|
|
61
|
+
agree: (review) => {
|
|
62
|
+
requireWalletGrant(review.plan.action, spendOf(review), new Date(options.now ?? Date.now()));
|
|
63
|
+
},
|
|
64
|
+
});
|
|
65
|
+
const { review } = outcome;
|
|
66
|
+
const facts = {
|
|
67
|
+
op,
|
|
68
|
+
network,
|
|
69
|
+
address,
|
|
70
|
+
shape: review.plan.shape,
|
|
71
|
+
feeSui: review.feeMist === null ? null : coinAmount(review.feeMist),
|
|
72
|
+
currentEpoch: review.currentEpoch,
|
|
66
73
|
};
|
|
67
|
-
|
|
68
|
-
const { shape, action, lines, command } = plan(op, rest, options, held);
|
|
69
|
-
const walrusPackageId = await reads.walrusPackageId();
|
|
70
|
-
// ③ The dry run: the fee, or the contract's own refusal.
|
|
71
|
-
const verdict = await reads.dryRun(shape, address);
|
|
72
|
-
if (verdict.refusal !== null) {
|
|
73
|
-
throw new NmtsError(`The chain would refuse this: ${verdict.refusal}`, {
|
|
74
|
-
exitCode: 4,
|
|
75
|
-
nextStep: "Nothing was signed and no fee was spent.",
|
|
76
|
-
});
|
|
77
|
-
}
|
|
78
|
-
const feeMist = verdict.feeMist;
|
|
79
|
-
const facts = { op, network, address, shape, feeSui: feeMist === null ? null : coinAmount(feeMist), currentEpoch: read.currentEpoch };
|
|
80
|
-
// ④ The review.
|
|
81
|
-
if (!options.json) {
|
|
82
|
-
say(`Would ${lines.what}`);
|
|
83
|
-
say(` from ${address} (wallet ${wallet})`);
|
|
84
|
-
for (const l of lines.detail)
|
|
85
|
-
say(` ${l}`);
|
|
86
|
-
say(feeMist === null
|
|
87
|
-
? ` Chain fee (SUI): could not be measured just now; it is charged with the signature.`
|
|
88
|
-
: ` Chain fee about ${facts.feeSui} SUI, measured by a dry run just now.`);
|
|
89
|
-
if (op === "transfer") {
|
|
90
|
-
say(` ⛔ No file goes with it. What goes is size and remaining time; the files stay sealed with this`);
|
|
91
|
-
say(` account's keys. It cannot be undone, and NMTS cannot recall it. Check the address once more.`);
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
// ⑤ Without --yes, that is the end.
|
|
95
|
-
if (options.dryRun === true || options.yes !== true) {
|
|
74
|
+
if (outcome.kind === "review") {
|
|
96
75
|
if (options.json) {
|
|
97
76
|
say(JSON.stringify({ ...facts, signed: false, dryRun: options.dryRun === true }));
|
|
98
77
|
return options.dryRun === true ? 0 : 4;
|
|
@@ -107,61 +86,86 @@ export async function walletStorageOps(op, rest, options = {}) {
|
|
|
107
86
|
nextStep: `Read the review above, then: \`${command} --yes\``,
|
|
108
87
|
});
|
|
109
88
|
}
|
|
110
|
-
//
|
|
111
|
-
|
|
112
|
-
requireWalletGrant(action, spend, new Date(options.now ?? Date.now()));
|
|
113
|
-
const sign = options.signStorage ?? (await import("../wallet-sign.js")).signStorageOp;
|
|
114
|
-
const digest = await sign({ network, code: resolved.code, wallet, shape, walrusPackageId });
|
|
115
|
-
recordWalletSpend(spend);
|
|
89
|
+
// What left the wallet is added to this machine's ledger as the dry run measured it.
|
|
90
|
+
recordWalletSpend(spendOf(review));
|
|
116
91
|
if (options.json) {
|
|
117
|
-
say(JSON.stringify({ ...facts, signed: true, digest, explorerUrl: explorerTxUrl(digest, network) }));
|
|
92
|
+
say(JSON.stringify({ ...facts, signed: true, digest: outcome.digest, explorerUrl: explorerTxUrl(outcome.digest, network) }));
|
|
118
93
|
return 0;
|
|
119
94
|
}
|
|
120
95
|
say(``);
|
|
121
|
-
say(` Done. Transaction ${digest}`);
|
|
122
|
-
say(` ${explorerTxUrl(digest, network)}`);
|
|
96
|
+
say(` Done. Transaction ${outcome.digest}`);
|
|
97
|
+
say(` ${explorerTxUrl(outcome.digest, network)}`);
|
|
123
98
|
say(` \`${BINARY_NAME} wallet storage\` lists what the wallet holds now.`);
|
|
124
99
|
return 0;
|
|
125
100
|
}
|
|
126
|
-
/**
|
|
127
|
-
function
|
|
101
|
+
/** What one change costs this wallet: no WAL, and the fee the dry run measured. */
|
|
102
|
+
function spendOf(review) {
|
|
103
|
+
return { walFrost: 0n, suiMist: review.feeMist ?? 0n };
|
|
104
|
+
}
|
|
105
|
+
/** The review a person reads, in the order somebody deciding needs it. */
|
|
106
|
+
function describe(say, review, wallet) {
|
|
107
|
+
const { plan } = review;
|
|
108
|
+
say(`Would ${what(plan)}`);
|
|
109
|
+
say(` from ${review.address} (wallet ${wallet})`);
|
|
110
|
+
for (const line of detail(plan))
|
|
111
|
+
say(` ${line}`);
|
|
112
|
+
say(review.feeMist === null
|
|
113
|
+
? ` Chain fee (SUI): could not be measured just now; it is charged with the signature.`
|
|
114
|
+
: ` Chain fee about ${coinAmount(review.feeMist)} SUI, measured by a dry run just now.`);
|
|
115
|
+
if (plan.op === "transfer") {
|
|
116
|
+
say(` ⛔ No file goes with it. What goes is size and remaining time; the files stay sealed with this`);
|
|
117
|
+
say(` account's keys. It cannot be undone, and NMTS cannot recall it. Check the address once more.`);
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
/** The one-line "what this would do", from the resources as the chain has them. */
|
|
121
|
+
function what(plan) {
|
|
122
|
+
const named = (at) => {
|
|
123
|
+
const r = plan.named[at];
|
|
124
|
+
return r === undefined ? "" : `${formatBytes(r.sizeBytes)} · epoch ${r.startEpoch} to ${r.endEpoch} (${r.objectId})`;
|
|
125
|
+
};
|
|
126
|
+
const result = plan.result;
|
|
127
|
+
if (result.kind === "split")
|
|
128
|
+
return `cut ${named(0)} by ${plan.shape.kind === "splitSize" ? "size" : "period"}.`;
|
|
129
|
+
if (result.kind === "merge")
|
|
130
|
+
return `join ${named(1)} into ${named(0)}.`;
|
|
131
|
+
return `hand ${named(0)} to ${result.to}.`;
|
|
132
|
+
}
|
|
133
|
+
/** What it would leave behind, said from the numbers the plan worked out. */
|
|
134
|
+
function detail(plan) {
|
|
135
|
+
const result = plan.result;
|
|
136
|
+
if (result.kind === "split") {
|
|
137
|
+
const { keeps, creates } = result;
|
|
138
|
+
return plan.shape.kind === "splitSize"
|
|
139
|
+
? [`It keeps ${formatBytes(keeps.sizeBytes)}; a new resource of ${formatBytes(creates.sizeBytes)} over the same period appears in this wallet.`]
|
|
140
|
+
: [`It keeps epoch ${keeps.startEpoch} to ${keeps.endEpoch}; a new resource of the same size over epoch ${creates.startEpoch} to ${creates.endEpoch} appears in this wallet.`];
|
|
141
|
+
}
|
|
142
|
+
if (result.kind === "merge") {
|
|
143
|
+
const b = result.becomes;
|
|
144
|
+
return [
|
|
145
|
+
result.how === "amount"
|
|
146
|
+
? `Same period, so the sizes add: the first becomes ${formatBytes(b.sizeBytes)} and the second is gone.`
|
|
147
|
+
: `Same size and touching periods, so the periods join: the first spans epoch ${b.startEpoch} to ${b.endEpoch} and the second is gone.`,
|
|
148
|
+
];
|
|
149
|
+
}
|
|
150
|
+
return [`Afterwards this wallet no longer holds it.`];
|
|
151
|
+
}
|
|
152
|
+
/** What was typed, as the values the library judges — and the line to run to go through with it. */
|
|
153
|
+
function asked(op, rest, options) {
|
|
128
154
|
const [a, b] = rest;
|
|
129
|
-
const resource = (r) => `${formatBytes(r.sizeBytes)} · epoch ${r.startEpoch} to ${r.endEpoch} (${r.objectId})`;
|
|
130
155
|
if (op === "split") {
|
|
131
156
|
if (a === undefined)
|
|
132
157
|
throw usage("split <resource id> --size <bytes> | --epochs <n>");
|
|
133
|
-
const r = held(a);
|
|
134
158
|
if (options.size !== undefined && options.epochs !== undefined)
|
|
135
159
|
throw usage("split takes --size OR --epochs, not both");
|
|
136
160
|
if (options.size !== undefined) {
|
|
137
|
-
const keep = parseBytes(options.size);
|
|
138
|
-
if (keep <= 0 || keep >= r.sizeBytes) {
|
|
139
|
-
throw new NmtsError(`--size must be above 0 and below the resource's ${formatBytes(r.sizeBytes)}.`, { exitCode: 2 });
|
|
140
|
-
}
|
|
141
161
|
return {
|
|
142
|
-
|
|
143
|
-
action: "reshape",
|
|
144
|
-
lines: {
|
|
145
|
-
what: `cut ${resource(r)} by size.`,
|
|
146
|
-
detail: [`It keeps ${formatBytes(keep)}; a new resource of ${formatBytes(r.sizeBytes - keep)} over the same period appears in this wallet.`],
|
|
147
|
-
},
|
|
162
|
+
ask: { kind: "splitSize", objectId: a, keepBytes: parseBytes(options.size) },
|
|
148
163
|
command: `${BINARY_NAME} wallet storage split ${a} --size ${options.size}`,
|
|
149
164
|
};
|
|
150
165
|
}
|
|
151
166
|
if (options.epochs !== undefined) {
|
|
152
|
-
const n = Number(options.epochs);
|
|
153
|
-
const span = r.endEpoch - r.startEpoch;
|
|
154
|
-
if (!Number.isSafeInteger(n) || n <= 0 || n >= span) {
|
|
155
|
-
throw new NmtsError(`--epochs must be a whole number above 0 and below the resource's ${span} epochs.`, { exitCode: 2 });
|
|
156
|
-
}
|
|
157
|
-
const at = r.startEpoch + n;
|
|
158
167
|
return {
|
|
159
|
-
|
|
160
|
-
action: "reshape",
|
|
161
|
-
lines: {
|
|
162
|
-
what: `cut ${resource(r)} by period.`,
|
|
163
|
-
detail: [`It keeps epoch ${r.startEpoch} to ${at}; a new resource of the same size over epoch ${at} to ${r.endEpoch} appears in this wallet.`],
|
|
164
|
-
},
|
|
168
|
+
ask: { kind: "splitEpochs", objectId: a, keepEpochs: Number(options.epochs) },
|
|
165
169
|
command: `${BINARY_NAME} wallet storage split ${a} --epochs ${options.epochs}`,
|
|
166
170
|
};
|
|
167
171
|
}
|
|
@@ -170,41 +174,11 @@ function plan(op, rest, options, held) {
|
|
|
170
174
|
if (op === "merge") {
|
|
171
175
|
if (a === undefined || b === undefined)
|
|
172
176
|
throw usage("merge <resource id> <resource id>");
|
|
173
|
-
|
|
174
|
-
const second = held(b);
|
|
175
|
-
const verdict = canFuse(first, second);
|
|
176
|
-
if (!verdict.can) {
|
|
177
|
-
throw new NmtsError(`These two cannot be joined: ${FUSE_WHY[verdict.why]}`, {
|
|
178
|
-
exitCode: 4,
|
|
179
|
-
nextStep: `Nothing was signed. \`${BINARY_NAME} wallet storage\` shows each one's size and period.`,
|
|
180
|
-
});
|
|
181
|
-
}
|
|
182
|
-
return {
|
|
183
|
-
shape: { kind: "fuse", first: a, second: b, how: verdict.kind },
|
|
184
|
-
action: "reshape",
|
|
185
|
-
lines: {
|
|
186
|
-
what: `join ${resource(second)} into ${resource(first)}.`,
|
|
187
|
-
detail: [
|
|
188
|
-
verdict.kind === "amount"
|
|
189
|
-
? `Same period, so the sizes add: the first becomes ${formatBytes(first.sizeBytes + second.sizeBytes)} and the second is gone.`
|
|
190
|
-
: `Same size and touching periods, so the periods join: the first spans epoch ${Math.min(first.startEpoch, second.startEpoch)} to ${Math.max(first.endEpoch, second.endEpoch)} and the second is gone.`,
|
|
191
|
-
],
|
|
192
|
-
},
|
|
193
|
-
command: `${BINARY_NAME} wallet storage merge ${a} ${b}`,
|
|
194
|
-
};
|
|
177
|
+
return { ask: { kind: "merge", first: a, second: b }, command: `${BINARY_NAME} wallet storage merge ${a} ${b}` };
|
|
195
178
|
}
|
|
196
179
|
if (a === undefined || b === undefined)
|
|
197
180
|
throw usage("transfer <resource id> <address>");
|
|
198
|
-
|
|
199
|
-
if (!isValidSuiAddress(b)) {
|
|
200
|
-
throw new NmtsError("That is not a Sui address: it is 0x followed by 64 hexadecimal characters.", { exitCode: 2 });
|
|
201
|
-
}
|
|
202
|
-
return {
|
|
203
|
-
shape: { kind: "transfer", objectId: a, to: b },
|
|
204
|
-
action: "give",
|
|
205
|
-
lines: { what: `hand ${resource(r)} to ${b}.`, detail: [`Afterwards this wallet no longer holds it.`] },
|
|
206
|
-
command: `${BINARY_NAME} wallet storage transfer ${a} ${b}`,
|
|
207
|
-
};
|
|
181
|
+
return { ask: { kind: "transfer", objectId: a, to: b }, command: `${BINARY_NAME} wallet storage transfer ${a} ${b}` };
|
|
208
182
|
}
|
|
209
183
|
function usage(form) {
|
|
210
184
|
return new NmtsError(`\`${BINARY_NAME} wallet storage ${form}\``, { exitCode: 2 });
|
|
@@ -1,10 +1,7 @@
|
|
|
1
1
|
import { type Network } from "../network.ts";
|
|
2
|
-
import type
|
|
3
|
-
|
|
4
|
-
export
|
|
5
|
-
items: readonly StorageResource[];
|
|
6
|
-
currentEpoch: number | null;
|
|
7
|
-
}
|
|
2
|
+
import { type StorageRead } from "../storage-control.ts";
|
|
3
|
+
export type { StorageRead } from "../storage-control.ts";
|
|
4
|
+
export { formatBytes } from "../storage-control.ts";
|
|
8
5
|
export interface WalletStorageOptions {
|
|
9
6
|
server?: string | undefined;
|
|
10
7
|
network?: string | undefined;
|
|
@@ -14,5 +11,3 @@ export interface WalletStorageOptions {
|
|
|
14
11
|
readStorage?: (network: Network, address: string) => Promise<StorageRead>;
|
|
15
12
|
}
|
|
16
13
|
export declare function walletStorage(options?: WalletStorageOptions): Promise<number>;
|
|
17
|
-
/** Bytes for a person: binary units, two decimals, whole bytes below a KiB. */
|
|
18
|
-
export declare function formatBytes(bytes: number): string;
|