@toon-protocol/client 0.29.8 → 2.1.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.
- package/README.md +34 -628
- package/dist/chunk-DD7TJKP6.js +166 -0
- package/dist/chunk-DD7TJKP6.js.map +1 -0
- package/dist/chunk-ENNSPYP4.js +8261 -0
- package/dist/chunk-ENNSPYP4.js.map +1 -0
- package/dist/chunk-J6JVCKNV.js +201 -0
- package/dist/chunk-J6JVCKNV.js.map +1 -0
- package/dist/cli/main.js +1669 -0
- package/dist/cli/main.js.map +1 -0
- package/dist/client-ACUZCR5A.js +68 -0
- package/dist/index.d.ts +5506 -6178
- package/dist/index.js +685 -12088
- package/dist/index.js.map +1 -1
- package/package.json +24 -29
- package/dist/chunk-7TDAU43L.js +0 -412
- package/dist/chunk-7TDAU43L.js.map +0 -1
- package/dist/chunk-QEMD5EAI.js +0 -441
- package/dist/chunk-QEMD5EAI.js.map +0 -1
- package/dist/mina-channel-deploy-5GPMFBGR.js +0 -9
- package/dist/render/index.d.ts +0 -807
- package/dist/render/index.js +0 -55
- package/dist/render/index.js.map +0 -1
- /package/dist/{mina-channel-deploy-5GPMFBGR.js.map → client-ACUZCR5A.js.map} +0 -0
package/dist/cli/main.js
ADDED
|
@@ -0,0 +1,1669 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
generateKeystore,
|
|
4
|
+
importKeystore,
|
|
5
|
+
openKeystore
|
|
6
|
+
} from "../chunk-DD7TJKP6.js";
|
|
7
|
+
import {
|
|
8
|
+
DEVNET,
|
|
9
|
+
deriveFullIdentity,
|
|
10
|
+
evmDerivationPath,
|
|
11
|
+
generateRandomIdentity
|
|
12
|
+
} from "../chunk-J6JVCKNV.js";
|
|
13
|
+
|
|
14
|
+
// src/cli/main.ts
|
|
15
|
+
import { homedir as homedir3 } from "os";
|
|
16
|
+
import { readFileSync as readFileSync2 } from "fs";
|
|
17
|
+
import { fileURLToPath, pathToFileURL } from "url";
|
|
18
|
+
|
|
19
|
+
// src/cli/args.ts
|
|
20
|
+
import { parseArgs } from "util";
|
|
21
|
+
var UsageError = class extends Error {
|
|
22
|
+
code = "USAGE";
|
|
23
|
+
/** The command to print usage for alongside the message, when one is known. */
|
|
24
|
+
command;
|
|
25
|
+
constructor(message, command) {
|
|
26
|
+
super(message);
|
|
27
|
+
this.name = "UsageError";
|
|
28
|
+
this.command = command;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
var CliConfigError = class extends Error {
|
|
32
|
+
code = "CONFIG";
|
|
33
|
+
/** What to do about it, printed under the message. */
|
|
34
|
+
hint;
|
|
35
|
+
constructor(message, hint) {
|
|
36
|
+
super(message);
|
|
37
|
+
this.name = "CliConfigError";
|
|
38
|
+
this.hint = hint;
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
var GLOBAL_OPTIONS = {
|
|
42
|
+
connector: {
|
|
43
|
+
type: "string",
|
|
44
|
+
arg: "URL",
|
|
45
|
+
help: "The connector's client-edge URL. Env TOON_CONNECTOR."
|
|
46
|
+
},
|
|
47
|
+
chain: {
|
|
48
|
+
type: "string",
|
|
49
|
+
arg: "evm|solana",
|
|
50
|
+
help: "Which settlement chain to pay on. Env TOON_CHAIN. Default: the first the node offers that you hold a key for."
|
|
51
|
+
},
|
|
52
|
+
rpc: {
|
|
53
|
+
type: "string",
|
|
54
|
+
arg: "URL",
|
|
55
|
+
help: "Chain RPC endpoint. Env TOON_RPC_URL."
|
|
56
|
+
},
|
|
57
|
+
store: {
|
|
58
|
+
type: "string",
|
|
59
|
+
arg: "PATH",
|
|
60
|
+
help: "Channel watermark file. Env TOON_CHANNEL_STORE. Default ~/.toon/channels.json."
|
|
61
|
+
},
|
|
62
|
+
keystore: {
|
|
63
|
+
type: "string",
|
|
64
|
+
arg: "PATH",
|
|
65
|
+
help: "Encrypted mnemonic keystore. Env TOON_KEYSTORE. Default ~/.toon/keystore.json."
|
|
66
|
+
},
|
|
67
|
+
"password-file": {
|
|
68
|
+
type: "string",
|
|
69
|
+
arg: "PATH",
|
|
70
|
+
help: "Read the keystore password from this file instead of prompting."
|
|
71
|
+
},
|
|
72
|
+
transport: {
|
|
73
|
+
type: "string",
|
|
74
|
+
arg: "auto|http|btp",
|
|
75
|
+
help: "Which carriage to pay over. Default auto."
|
|
76
|
+
},
|
|
77
|
+
json: { type: "boolean", help: "Print one JSON document on stdout and nothing else." },
|
|
78
|
+
quiet: { type: "boolean", help: "Suppress progress and warnings on stderr." },
|
|
79
|
+
help: { type: "boolean", short: "h", help: "Show this help." },
|
|
80
|
+
version: { type: "boolean", help: "Print the client version." }
|
|
81
|
+
};
|
|
82
|
+
var COMMAND_OPTIONS = {
|
|
83
|
+
import: { type: "boolean", help: "Import an existing BIP-39 phrase, read from stdin." },
|
|
84
|
+
"legacy-derivation": {
|
|
85
|
+
type: "boolean",
|
|
86
|
+
help: "Derive the EVM key at Nostr's coin type, as this client did before 1.0."
|
|
87
|
+
},
|
|
88
|
+
"all-derivations": {
|
|
89
|
+
type: "boolean",
|
|
90
|
+
help: "Show the addresses of every derivation scheme, not just the one in use."
|
|
91
|
+
},
|
|
92
|
+
deposit: { type: "string", arg: "BASE_UNITS", help: "Collateral to lock, in base units." },
|
|
93
|
+
"settlement-timeout": {
|
|
94
|
+
type: "string",
|
|
95
|
+
arg: "SECONDS",
|
|
96
|
+
help: "Challenge period. Default 86400; EVM floors it at 3600."
|
|
97
|
+
},
|
|
98
|
+
"connector-view": {
|
|
99
|
+
type: "boolean",
|
|
100
|
+
help: "Also ask the connector for its own watermark, and show both."
|
|
101
|
+
},
|
|
102
|
+
method: { type: "string", arg: "VERB", help: "HTTP method. Default POST." },
|
|
103
|
+
target: { type: "string", arg: "PATH", help: "Path beneath the route's handler. Default ''." },
|
|
104
|
+
header: {
|
|
105
|
+
type: "string",
|
|
106
|
+
short: "H",
|
|
107
|
+
multiple: true,
|
|
108
|
+
arg: "NAME:VALUE",
|
|
109
|
+
help: "A request header. Repeat for more; order and duplicates are preserved."
|
|
110
|
+
},
|
|
111
|
+
body: { type: "string", arg: "TEXT", help: "Request body. `-` reads stdin to EOF." },
|
|
112
|
+
"body-file": { type: "string", arg: "PATH", help: "Read the request body from a file." },
|
|
113
|
+
"json-body": {
|
|
114
|
+
type: "boolean",
|
|
115
|
+
help: "Check the body parses as JSON and send it as application/json."
|
|
116
|
+
},
|
|
117
|
+
amount: {
|
|
118
|
+
type: "string",
|
|
119
|
+
arg: "BASE_UNITS",
|
|
120
|
+
help: "Amount in base units. On send, overrides the route price."
|
|
121
|
+
},
|
|
122
|
+
to: { type: "string", arg: "ADDRESS", help: "Destination address." },
|
|
123
|
+
asset: { type: "string", arg: "native|token", help: "Which asset to move. Default 'token'." }
|
|
124
|
+
};
|
|
125
|
+
var ALL_OPTIONS = { ...GLOBAL_OPTIONS, ...COMMAND_OPTIONS };
|
|
126
|
+
var COMMANDS = {
|
|
127
|
+
init: {
|
|
128
|
+
summary: "Create or import a keystore.",
|
|
129
|
+
usage: "init [--import] [--legacy-derivation]",
|
|
130
|
+
options: ["import", "legacy-derivation"],
|
|
131
|
+
minPositionals: 0,
|
|
132
|
+
maxPositionals: 0,
|
|
133
|
+
details: [
|
|
134
|
+
"Writes an encrypted BIP-39 keystore to ~/.toon/keystore.json (mode 0600).",
|
|
135
|
+
"With --import the phrase is read from stdin \u2014 never from an argument, which",
|
|
136
|
+
"would be recorded in shell history and visible in `ps` to everyone on the box."
|
|
137
|
+
]
|
|
138
|
+
},
|
|
139
|
+
identity: {
|
|
140
|
+
summary: "Show the addresses this keystore holds. Offline.",
|
|
141
|
+
usage: "identity [--all-derivations]",
|
|
142
|
+
options: ["all-derivations"],
|
|
143
|
+
minPositionals: 0,
|
|
144
|
+
maxPositionals: 0,
|
|
145
|
+
details: [
|
|
146
|
+
"Touches no network and no chain: the addresses are derived locally from the",
|
|
147
|
+
"phrase. --all-derivations also shows where a pre-1.0 keystore put the EVM key,",
|
|
148
|
+
"which is the address to look at when a channel opened before 1.0 seems to have",
|
|
149
|
+
"vanished."
|
|
150
|
+
]
|
|
151
|
+
},
|
|
152
|
+
describe: {
|
|
153
|
+
summary: "Read a connector\u2019s self-description. Free, needs no keys.",
|
|
154
|
+
usage: "describe [URL]",
|
|
155
|
+
options: [],
|
|
156
|
+
minPositionals: 0,
|
|
157
|
+
maxPositionals: 1,
|
|
158
|
+
details: [
|
|
159
|
+
"One unauthenticated GET returns everything needed to transact with a node:",
|
|
160
|
+
"its addresses, its endpoints, the key payloads are sealed to, what it settles",
|
|
161
|
+
"in, and what each route costs."
|
|
162
|
+
]
|
|
163
|
+
},
|
|
164
|
+
price: {
|
|
165
|
+
summary: "What one route costs. Free, needs no keys.",
|
|
166
|
+
usage: "price <destination> [URL]",
|
|
167
|
+
options: [],
|
|
168
|
+
minPositionals: 1,
|
|
169
|
+
maxPositionals: 2
|
|
170
|
+
},
|
|
171
|
+
probe: {
|
|
172
|
+
summary: "Learn a path's cost without buying the work.",
|
|
173
|
+
usage: "probe <destination>",
|
|
174
|
+
options: [],
|
|
175
|
+
minPositionals: 1,
|
|
176
|
+
maxPositionals: 1,
|
|
177
|
+
details: ["Needs an open channel: a probe carries a claim, it just does not spend it."]
|
|
178
|
+
},
|
|
179
|
+
send: {
|
|
180
|
+
summary: "Pay for one HTTP request and print the answer.",
|
|
181
|
+
usage: "send [destination] [--method VERB] [--target PATH] [-H NAME:VALUE]... [--body TEXT | --body-file PATH | --body -] [--json-body] [--amount BASE_UNITS]",
|
|
182
|
+
options: ["method", "target", "header", "body", "body-file", "json-body", "amount"],
|
|
183
|
+
minPositionals: 0,
|
|
184
|
+
maxPositionals: 1,
|
|
185
|
+
details: [
|
|
186
|
+
"The destination is optional: with none, the request goes to the address the",
|
|
187
|
+
"node published for itself, so a connector URL is the whole of the config.",
|
|
188
|
+
"The status printed is the app\u2019s own: a 404 from the app is a real answer, it",
|
|
189
|
+
"rides home fulfilled and costs exactly what a 200 costs. Only a refusal short",
|
|
190
|
+
"of the app exits 3."
|
|
191
|
+
]
|
|
192
|
+
},
|
|
193
|
+
channel: {
|
|
194
|
+
summary: "Open, fund, inspect, close and settle the payment channel.",
|
|
195
|
+
usage: "channel open|deposit <amount>|close|settle|status [--deposit BASE_UNITS] [--connector-view]",
|
|
196
|
+
options: ["deposit", "settlement-timeout", "connector-view"],
|
|
197
|
+
minPositionals: 1,
|
|
198
|
+
maxPositionals: 2,
|
|
199
|
+
details: [
|
|
200
|
+
"Every one of these is your transaction, on your gas. `close` starts the",
|
|
201
|
+
"challenge period; `settle` pays out once it has elapsed."
|
|
202
|
+
]
|
|
203
|
+
},
|
|
204
|
+
"claim-state": {
|
|
205
|
+
summary: "The connector's own watermark for your channels.",
|
|
206
|
+
usage: "claim-state",
|
|
207
|
+
options: [],
|
|
208
|
+
minPositionals: 0,
|
|
209
|
+
maxPositionals: 0
|
|
210
|
+
},
|
|
211
|
+
balances: {
|
|
212
|
+
summary: "Chain balances for this identity.",
|
|
213
|
+
usage: "balances",
|
|
214
|
+
options: [],
|
|
215
|
+
minPositionals: 0,
|
|
216
|
+
maxPositionals: 0
|
|
217
|
+
},
|
|
218
|
+
transfer: {
|
|
219
|
+
summary: "Move funds out of this wallet.",
|
|
220
|
+
usage: "transfer --to <address> --amount <base units> [--asset native|token]",
|
|
221
|
+
options: ["to", "amount", "asset"],
|
|
222
|
+
minPositionals: 0,
|
|
223
|
+
maxPositionals: 0
|
|
224
|
+
},
|
|
225
|
+
faucet: {
|
|
226
|
+
summary: "Ask the devnet faucet for test funds.",
|
|
227
|
+
usage: "faucet",
|
|
228
|
+
options: [],
|
|
229
|
+
minPositionals: 0,
|
|
230
|
+
maxPositionals: 0
|
|
231
|
+
},
|
|
232
|
+
help: {
|
|
233
|
+
summary: "Show help for a command.",
|
|
234
|
+
usage: "help [COMMAND]",
|
|
235
|
+
options: [],
|
|
236
|
+
minPositionals: 0,
|
|
237
|
+
maxPositionals: 1
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
var CHANNEL_SUBCOMMANDS = ["open", "deposit", "status", "close", "settle"];
|
|
241
|
+
function stringOption(values, name) {
|
|
242
|
+
const v = values[name];
|
|
243
|
+
if (v === void 0) return void 0;
|
|
244
|
+
if (typeof v === "string") return v;
|
|
245
|
+
if (Array.isArray(v)) {
|
|
246
|
+
if (v.length > 1) throw new UsageError(`--${name} was given more than once`);
|
|
247
|
+
return v[0];
|
|
248
|
+
}
|
|
249
|
+
return void 0;
|
|
250
|
+
}
|
|
251
|
+
function boolOption(values, name) {
|
|
252
|
+
return values[name] === true;
|
|
253
|
+
}
|
|
254
|
+
function listOption(values, name) {
|
|
255
|
+
const v = values[name];
|
|
256
|
+
if (v === void 0) return [];
|
|
257
|
+
if (typeof v === "string") return [v];
|
|
258
|
+
if (Array.isArray(v)) return v;
|
|
259
|
+
return [];
|
|
260
|
+
}
|
|
261
|
+
function parseCommandLine(argv) {
|
|
262
|
+
let parsed;
|
|
263
|
+
try {
|
|
264
|
+
parsed = parseArgs({
|
|
265
|
+
args: argv,
|
|
266
|
+
options: ALL_OPTIONS,
|
|
267
|
+
allowPositionals: true,
|
|
268
|
+
strict: true
|
|
269
|
+
});
|
|
270
|
+
} catch (err) {
|
|
271
|
+
throw new UsageError(err instanceof Error ? err.message : String(err));
|
|
272
|
+
}
|
|
273
|
+
const values = parsed.values;
|
|
274
|
+
const positionals = parsed.positionals;
|
|
275
|
+
const command = positionals[0];
|
|
276
|
+
if (command === void 0) {
|
|
277
|
+
return { command: void 0, positionals: [], values };
|
|
278
|
+
}
|
|
279
|
+
const spec = COMMANDS[command];
|
|
280
|
+
if (spec === void 0) {
|
|
281
|
+
throw new UsageError(`unknown command '${command}'. Run 'toon help' for the list.`);
|
|
282
|
+
}
|
|
283
|
+
const rest = positionals.slice(1);
|
|
284
|
+
if (!boolOption(values, "help")) {
|
|
285
|
+
for (const name of Object.keys(values)) {
|
|
286
|
+
if (values[name] === void 0) continue;
|
|
287
|
+
if (name in GLOBAL_OPTIONS) continue;
|
|
288
|
+
if (spec.options.includes(name)) continue;
|
|
289
|
+
throw new UsageError(`--${name} is not an option of '${command}'`, command);
|
|
290
|
+
}
|
|
291
|
+
if (rest.length < spec.minPositionals) {
|
|
292
|
+
throw new UsageError(`'${command}' needs more arguments`, command);
|
|
293
|
+
}
|
|
294
|
+
if (rest.length > spec.maxPositionals) {
|
|
295
|
+
throw new UsageError(
|
|
296
|
+
`'${command}' takes at most ${String(spec.maxPositionals)} argument(s); got ${String(rest.length)}`,
|
|
297
|
+
command
|
|
298
|
+
);
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
return { command, positionals: rest, values };
|
|
302
|
+
}
|
|
303
|
+
function optionLine(name, spec) {
|
|
304
|
+
const short = spec.short === void 0 ? "" : `-${spec.short}, `;
|
|
305
|
+
const arg = spec.arg === void 0 ? "" : ` <${spec.arg}>`;
|
|
306
|
+
return [` ${short}--${name}${arg}`, spec.help];
|
|
307
|
+
}
|
|
308
|
+
function columns(rows) {
|
|
309
|
+
const width = rows.reduce((w, [left]) => Math.max(w, left.length), 0);
|
|
310
|
+
return rows.map(([left, right]) => `${left.padEnd(width)} ${right}`).join("\n");
|
|
311
|
+
}
|
|
312
|
+
function usage(command) {
|
|
313
|
+
if (command !== void 0 && command in COMMANDS) {
|
|
314
|
+
const spec = COMMANDS[command];
|
|
315
|
+
const parts = [`toon ${spec.usage}`, "", spec.summary];
|
|
316
|
+
if (spec.details) parts.push("", ...spec.details);
|
|
317
|
+
const opts = spec.options.map(
|
|
318
|
+
(name) => optionLine(name, COMMAND_OPTIONS[name])
|
|
319
|
+
);
|
|
320
|
+
if (opts.length > 0) parts.push("", "Options:", columns(opts));
|
|
321
|
+
parts.push(
|
|
322
|
+
"",
|
|
323
|
+
"Global options:",
|
|
324
|
+
columns(Object.entries(GLOBAL_OPTIONS).map(([n, s]) => optionLine(n, s)))
|
|
325
|
+
);
|
|
326
|
+
return parts.join("\n");
|
|
327
|
+
}
|
|
328
|
+
return [
|
|
329
|
+
"toon \u2014 pay for an HTTP request, per request, over a payment channel.",
|
|
330
|
+
"",
|
|
331
|
+
"Usage: toon <command> [options]",
|
|
332
|
+
"",
|
|
333
|
+
"Commands:",
|
|
334
|
+
columns(
|
|
335
|
+
Object.entries(COMMANDS).map(([name, spec]) => [` ${name}`, spec.summary])
|
|
336
|
+
),
|
|
337
|
+
"",
|
|
338
|
+
"Global options:",
|
|
339
|
+
columns(Object.entries(GLOBAL_OPTIONS).map(([n, s]) => optionLine(n, s))),
|
|
340
|
+
"",
|
|
341
|
+
"Environment: TOON_CONNECTOR, TOON_MNEMONIC, TOON_KEYSTORE,",
|
|
342
|
+
" TOON_KEYSTORE_PASSWORD, TOON_CHAIN, TOON_RPC_URL, TOON_CHANNEL_STORE.",
|
|
343
|
+
"",
|
|
344
|
+
"Run 'toon help <command>' for one command in detail."
|
|
345
|
+
].join("\n");
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
// src/cli/context.ts
|
|
349
|
+
import { homedir as homedir2 } from "os";
|
|
350
|
+
import { join as join2 } from "path";
|
|
351
|
+
|
|
352
|
+
// src/cli/keystore.ts
|
|
353
|
+
import { existsSync, mkdirSync, readFileSync } from "fs";
|
|
354
|
+
import { dirname, join } from "path";
|
|
355
|
+
import { homedir } from "os";
|
|
356
|
+
var KEYSTORE_ENV = "TOON_KEYSTORE";
|
|
357
|
+
var KEYSTORE_PASSWORD_ENV = "TOON_KEYSTORE_PASSWORD";
|
|
358
|
+
var MNEMONIC_ENV = "TOON_MNEMONIC";
|
|
359
|
+
function defaultKeystorePath(home = homedir()) {
|
|
360
|
+
return join(home, ".toon", "keystore.json");
|
|
361
|
+
}
|
|
362
|
+
function resolveKeystorePath(options) {
|
|
363
|
+
const flag = options.flag;
|
|
364
|
+
if (flag !== void 0 && flag.length > 0) return flag;
|
|
365
|
+
const fromEnv = options.env?.[KEYSTORE_ENV];
|
|
366
|
+
if (fromEnv !== void 0 && fromEnv.length > 0) return fromEnv;
|
|
367
|
+
return defaultKeystorePath(options.home ?? homedir());
|
|
368
|
+
}
|
|
369
|
+
async function resolvePassword(options) {
|
|
370
|
+
const read = options.readFile ?? ((p) => readFileSync(p, "utf8"));
|
|
371
|
+
if (options.passwordFile !== void 0 && options.passwordFile.length > 0) {
|
|
372
|
+
let contents;
|
|
373
|
+
try {
|
|
374
|
+
contents = read(options.passwordFile);
|
|
375
|
+
} catch (err) {
|
|
376
|
+
throw new CliConfigError(
|
|
377
|
+
`cannot read the password file ${options.passwordFile}: ${err instanceof Error ? err.message : String(err)}`
|
|
378
|
+
);
|
|
379
|
+
}
|
|
380
|
+
const password = contents.replace(/\r?\n$/, "");
|
|
381
|
+
if (password.length === 0) {
|
|
382
|
+
throw new CliConfigError(`the password file ${options.passwordFile} is empty`);
|
|
383
|
+
}
|
|
384
|
+
return { password, source: "password-file" };
|
|
385
|
+
}
|
|
386
|
+
const fromEnv = options.env?.[KEYSTORE_PASSWORD_ENV];
|
|
387
|
+
if (fromEnv !== void 0 && fromEnv.length > 0) {
|
|
388
|
+
return { password: fromEnv, source: "env" };
|
|
389
|
+
}
|
|
390
|
+
if (options.prompt !== void 0) {
|
|
391
|
+
const password = await options.prompt(options.message);
|
|
392
|
+
if (password.length === 0) throw new CliConfigError("no password was entered");
|
|
393
|
+
return { password, source: "prompt" };
|
|
394
|
+
}
|
|
395
|
+
throw new CliConfigError(
|
|
396
|
+
"a keystore password is needed and stdin is not a terminal to ask on",
|
|
397
|
+
`Set ${KEYSTORE_PASSWORD_ENV}, or pass --password-file <path>.`
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
function readKeystore(path, password) {
|
|
401
|
+
if (!existsSync(path)) {
|
|
402
|
+
throw new CliConfigError(`no keystore at ${path}`, "Run 'toon init' to create one.");
|
|
403
|
+
}
|
|
404
|
+
try {
|
|
405
|
+
return openKeystore(path, password);
|
|
406
|
+
} catch (err) {
|
|
407
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
408
|
+
throw new CliConfigError(`could not open the keystore at ${path}: ${message}`);
|
|
409
|
+
}
|
|
410
|
+
}
|
|
411
|
+
function ensureKeystoreDirectory(path) {
|
|
412
|
+
mkdirSync(dirname(path), { recursive: true, mode: 448 });
|
|
413
|
+
}
|
|
414
|
+
function keystoreExists(path) {
|
|
415
|
+
return existsSync(path);
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
// src/cli/context.ts
|
|
419
|
+
var CONNECTOR_ENV = "TOON_CONNECTOR";
|
|
420
|
+
var CHAIN_ENV = "TOON_CHAIN";
|
|
421
|
+
var RPC_ENV = "TOON_RPC_URL";
|
|
422
|
+
var CHANNEL_STORE_ENV = "TOON_CHANNEL_STORE";
|
|
423
|
+
function defaultChannelStorePath(home = homedir2()) {
|
|
424
|
+
return join2(home, ".toon", "channels.json");
|
|
425
|
+
}
|
|
426
|
+
function parseChain(value, from) {
|
|
427
|
+
if (value === void 0 || value.length === 0) return void 0;
|
|
428
|
+
if (value === "evm" || value === "solana") return value;
|
|
429
|
+
throw new UsageError(`${from} must be 'evm' or 'solana'; got '${value}'`);
|
|
430
|
+
}
|
|
431
|
+
function parseTransport(value, from) {
|
|
432
|
+
if (value === void 0 || value.length === 0) return "auto";
|
|
433
|
+
if (value === "auto" || value === "http" || value === "btp") return value;
|
|
434
|
+
throw new UsageError(`${from} must be 'auto', 'http' or 'btp'; got '${value}'`);
|
|
435
|
+
}
|
|
436
|
+
function resolveSettings(values, env, options = {}) {
|
|
437
|
+
const home = options.home ?? homedir2();
|
|
438
|
+
const warnings = [];
|
|
439
|
+
const connectorFlag = stringOption(values, "connector");
|
|
440
|
+
const connectorEnv = env[CONNECTOR_ENV];
|
|
441
|
+
let connector;
|
|
442
|
+
let connectorSource;
|
|
443
|
+
if (connectorFlag !== void 0 && connectorFlag.length > 0) {
|
|
444
|
+
connector = connectorFlag;
|
|
445
|
+
connectorSource = "flag";
|
|
446
|
+
} else if (connectorEnv !== void 0 && connectorEnv.length > 0) {
|
|
447
|
+
connector = connectorEnv;
|
|
448
|
+
connectorSource = "env";
|
|
449
|
+
} else {
|
|
450
|
+
connector = DEVNET.store.url;
|
|
451
|
+
connectorSource = "default";
|
|
452
|
+
warnings.push(
|
|
453
|
+
`toon: no connector given, using the devnet store node ${DEVNET.store.url}.
|
|
454
|
+
Set --connector or ${CONNECTOR_ENV} to talk to another node.`
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
const chain = parseChain(stringOption(values, "chain"), "--chain") ?? parseChain(env[CHAIN_ENV], CHAIN_ENV);
|
|
458
|
+
const rpcUrl = stringOption(values, "rpc") ?? env[RPC_ENV];
|
|
459
|
+
const channelStore = stringOption(values, "store") ?? (env[CHANNEL_STORE_ENV] !== void 0 && env[CHANNEL_STORE_ENV].length > 0 ? env[CHANNEL_STORE_ENV] : defaultChannelStorePath(home));
|
|
460
|
+
const transport = parseTransport(stringOption(values, "transport"), "--transport");
|
|
461
|
+
const settings = {
|
|
462
|
+
connector,
|
|
463
|
+
connectorSource,
|
|
464
|
+
...chain !== void 0 ? { chain } : {},
|
|
465
|
+
...rpcUrl !== void 0 && rpcUrl.length > 0 ? { rpcUrl } : {},
|
|
466
|
+
channelStore,
|
|
467
|
+
transport,
|
|
468
|
+
keystorePath: resolveKeystorePath({
|
|
469
|
+
flag: stringOption(values, "keystore"),
|
|
470
|
+
env,
|
|
471
|
+
home
|
|
472
|
+
}),
|
|
473
|
+
...stringOption(values, "password-file") !== void 0 ? { passwordFile: stringOption(values, "password-file") } : {},
|
|
474
|
+
json: boolOption(values, "json"),
|
|
475
|
+
quiet: boolOption(values, "quiet")
|
|
476
|
+
};
|
|
477
|
+
return { settings, warnings };
|
|
478
|
+
}
|
|
479
|
+
async function resolveKeyMaterial(settings, deps, options = {}) {
|
|
480
|
+
const fromEnv = deps.env[MNEMONIC_ENV];
|
|
481
|
+
if (fromEnv !== void 0 && fromEnv.trim().length > 0) {
|
|
482
|
+
return {
|
|
483
|
+
kind: "mnemonic",
|
|
484
|
+
mnemonic: fromEnv.trim(),
|
|
485
|
+
derivation: "standard",
|
|
486
|
+
from: "env"
|
|
487
|
+
};
|
|
488
|
+
}
|
|
489
|
+
if (keystoreExists(settings.keystorePath)) {
|
|
490
|
+
const { password } = await resolvePassword({
|
|
491
|
+
message: `Password for ${settings.keystorePath}: `,
|
|
492
|
+
passwordFile: settings.passwordFile,
|
|
493
|
+
env: deps.env,
|
|
494
|
+
...deps.readFile !== void 0 ? { readFile: deps.readFile } : {},
|
|
495
|
+
prompt: deps.prompt
|
|
496
|
+
});
|
|
497
|
+
const opened = readKeystore(settings.keystorePath, password);
|
|
498
|
+
return {
|
|
499
|
+
kind: "mnemonic",
|
|
500
|
+
mnemonic: opened.mnemonic,
|
|
501
|
+
derivation: opened.derivation,
|
|
502
|
+
from: "keystore"
|
|
503
|
+
};
|
|
504
|
+
}
|
|
505
|
+
if (options.keyless === true) return { kind: "ephemeral" };
|
|
506
|
+
throw new CliConfigError(
|
|
507
|
+
`no keys: ${MNEMONIC_ENV} is unset and there is no keystore at ${settings.keystorePath}`,
|
|
508
|
+
"Run 'toon init' to create one, or set TOON_MNEMONIC."
|
|
509
|
+
);
|
|
510
|
+
}
|
|
511
|
+
function buildClientConfig(settings, keys) {
|
|
512
|
+
const base = {
|
|
513
|
+
connector: settings.connector,
|
|
514
|
+
channelStore: settings.channelStore,
|
|
515
|
+
transport: settings.transport,
|
|
516
|
+
// See this module's docs: a command never opens a channel as a side effect.
|
|
517
|
+
autoOpenChannel: false,
|
|
518
|
+
...settings.chain !== void 0 ? { chain: settings.chain } : {},
|
|
519
|
+
...settings.rpcUrl !== void 0 ? { rpcUrl: settings.rpcUrl } : {}
|
|
520
|
+
};
|
|
521
|
+
if (keys.kind === "mnemonic") {
|
|
522
|
+
return { ...base, mnemonic: keys.mnemonic, keyDerivation: keys.derivation };
|
|
523
|
+
}
|
|
524
|
+
const identity = generateRandomIdentity();
|
|
525
|
+
return {
|
|
526
|
+
...base,
|
|
527
|
+
evmPrivateKey: identity.evm.privateKey,
|
|
528
|
+
solanaSecretKey: identity.solana.secretKey
|
|
529
|
+
};
|
|
530
|
+
}
|
|
531
|
+
async function defaultCreateClient(config) {
|
|
532
|
+
const module = await import("../client-ACUZCR5A.js");
|
|
533
|
+
const factory = module.ToonClient;
|
|
534
|
+
if (factory === void 0) {
|
|
535
|
+
throw new Error(
|
|
536
|
+
"this build of @toon-protocol/client exports no ToonClient; the CLI cannot run against it"
|
|
537
|
+
);
|
|
538
|
+
}
|
|
539
|
+
return factory.create(config);
|
|
540
|
+
}
|
|
541
|
+
var Context = class {
|
|
542
|
+
command;
|
|
543
|
+
positionals;
|
|
544
|
+
values;
|
|
545
|
+
settings;
|
|
546
|
+
out;
|
|
547
|
+
deps;
|
|
548
|
+
instance;
|
|
549
|
+
constructor(init) {
|
|
550
|
+
this.command = init.command;
|
|
551
|
+
this.positionals = init.positionals;
|
|
552
|
+
this.values = init.values;
|
|
553
|
+
this.settings = init.settings;
|
|
554
|
+
this.out = init.out;
|
|
555
|
+
this.deps = init.deps;
|
|
556
|
+
}
|
|
557
|
+
get openClient() {
|
|
558
|
+
return this.instance;
|
|
559
|
+
}
|
|
560
|
+
async client(options = {}) {
|
|
561
|
+
if (this.instance !== void 0) return this.instance;
|
|
562
|
+
const settings = options.connector === void 0 ? this.settings : { ...this.settings, connector: options.connector, connectorSource: "flag" };
|
|
563
|
+
const keys = await resolveKeyMaterial(settings, this.deps, options);
|
|
564
|
+
const config = buildClientConfig(settings, keys);
|
|
565
|
+
const create = this.deps.createClient ?? defaultCreateClient;
|
|
566
|
+
this.instance = await create(config);
|
|
567
|
+
return this.instance;
|
|
568
|
+
}
|
|
569
|
+
};
|
|
570
|
+
|
|
571
|
+
// src/cli/output.ts
|
|
572
|
+
import { formatUnits } from "viem";
|
|
573
|
+
function assetSymbol(tokenAddress) {
|
|
574
|
+
if (tokenAddress === void 0) return void 0;
|
|
575
|
+
const t = tokenAddress.toLowerCase();
|
|
576
|
+
if (t === DEVNET.evm.tokenAddress.toLowerCase()) return "USDC";
|
|
577
|
+
if (t === DEVNET.solana.tokenAddress.toLowerCase()) return "USDC";
|
|
578
|
+
return void 0;
|
|
579
|
+
}
|
|
580
|
+
function assetFromSettlement(settlement) {
|
|
581
|
+
if (settlement === void 0) return {};
|
|
582
|
+
return { decimals: settlement.decimals, symbol: assetSymbol(settlement.tokenAddress) };
|
|
583
|
+
}
|
|
584
|
+
function assetFromTerms(terms) {
|
|
585
|
+
if (terms === void 0) return {};
|
|
586
|
+
return { decimals: terms.decimals, symbol: assetSymbol(terms.token) };
|
|
587
|
+
}
|
|
588
|
+
function formatAmount(amount, asset = {}) {
|
|
589
|
+
const base = typeof amount === "bigint" ? amount : BigInt(amount);
|
|
590
|
+
const raw = base.toString();
|
|
591
|
+
if (asset.decimals === void 0) return raw;
|
|
592
|
+
const human = formatUnits(base, asset.decimals);
|
|
593
|
+
return asset.symbol === void 0 ? `${raw} (${human})` : `${raw} (${human} ${asset.symbol})`;
|
|
594
|
+
}
|
|
595
|
+
function toJsonValue(value) {
|
|
596
|
+
if (typeof value === "bigint") return value.toString();
|
|
597
|
+
if (value instanceof Uint8Array) return Buffer.from(value).toString("base64");
|
|
598
|
+
if (value instanceof Date) return value.toISOString();
|
|
599
|
+
if (typeof value === "function") return void 0;
|
|
600
|
+
if (Array.isArray(value)) return value.map(toJsonValue);
|
|
601
|
+
if (typeof value === "object" && value !== null) {
|
|
602
|
+
const out = {};
|
|
603
|
+
for (const [k, v] of Object.entries(value)) {
|
|
604
|
+
const converted = toJsonValue(v);
|
|
605
|
+
if (converted !== void 0) out[k] = converted;
|
|
606
|
+
}
|
|
607
|
+
return out;
|
|
608
|
+
}
|
|
609
|
+
return value;
|
|
610
|
+
}
|
|
611
|
+
function columns2(rows, indent = "") {
|
|
612
|
+
const width = rows.reduce((w, [left]) => Math.max(w, left.length), 0);
|
|
613
|
+
return rows.map(([left, right]) => `${indent}${left.padEnd(width)} ${right}`).join("\n");
|
|
614
|
+
}
|
|
615
|
+
function decodeUtf8(bytes) {
|
|
616
|
+
try {
|
|
617
|
+
return new TextDecoder("utf-8", { fatal: true }).decode(bytes);
|
|
618
|
+
} catch {
|
|
619
|
+
return void 0;
|
|
620
|
+
}
|
|
621
|
+
}
|
|
622
|
+
var Output = class {
|
|
623
|
+
json;
|
|
624
|
+
quiet;
|
|
625
|
+
out;
|
|
626
|
+
err;
|
|
627
|
+
rendered = false;
|
|
628
|
+
constructor(options = {}) {
|
|
629
|
+
this.json = options.json ?? false;
|
|
630
|
+
this.quiet = options.quiet ?? false;
|
|
631
|
+
this.out = options.stdout ?? ((text) => process.stdout.write(`${text}
|
|
632
|
+
`));
|
|
633
|
+
this.err = options.stderr ?? ((text) => process.stderr.write(`${text}
|
|
634
|
+
`));
|
|
635
|
+
}
|
|
636
|
+
/** A line of human output. Suppressed entirely under `--json`. */
|
|
637
|
+
line(text = "") {
|
|
638
|
+
if (!this.json) this.out(text);
|
|
639
|
+
}
|
|
640
|
+
/** Aligned rows of human output. */
|
|
641
|
+
rows(rows, indent = " ") {
|
|
642
|
+
if (rows.length > 0) this.line(columns2(rows, indent));
|
|
643
|
+
}
|
|
644
|
+
/**
|
|
645
|
+
* A diagnostic. Always stderr — under `--json` because stdout is spoken for,
|
|
646
|
+
* and otherwise because a warning interleaved with results is a warning that
|
|
647
|
+
* gets piped into the next program.
|
|
648
|
+
*/
|
|
649
|
+
warn(text) {
|
|
650
|
+
if (!this.quiet) this.err(text);
|
|
651
|
+
}
|
|
652
|
+
/** An error explanation. Stderr, and never suppressed by `--quiet`. */
|
|
653
|
+
error(text) {
|
|
654
|
+
this.err(text);
|
|
655
|
+
}
|
|
656
|
+
/**
|
|
657
|
+
* The command's answer: the JSON document under `--json`, the human rendering
|
|
658
|
+
* otherwise.
|
|
659
|
+
*
|
|
660
|
+
* Calling this twice is a bug in the command, not a formatting choice — two
|
|
661
|
+
* documents on stdout is not JSON — so it throws rather than emitting them.
|
|
662
|
+
*/
|
|
663
|
+
render(payload, human) {
|
|
664
|
+
if (this.rendered) {
|
|
665
|
+
throw new Error("Output.render was called twice: --json must emit exactly one document");
|
|
666
|
+
}
|
|
667
|
+
this.rendered = true;
|
|
668
|
+
if (this.json) {
|
|
669
|
+
this.out(JSON.stringify(toJsonValue(payload), null, 2));
|
|
670
|
+
} else {
|
|
671
|
+
human();
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
};
|
|
675
|
+
|
|
676
|
+
// src/cli/commands/balances.ts
|
|
677
|
+
function amountRow(token) {
|
|
678
|
+
const label = token.symbol ?? token.address ?? "token";
|
|
679
|
+
return [
|
|
680
|
+
label,
|
|
681
|
+
formatAmount(token.amount, {
|
|
682
|
+
...token.decimals !== void 0 ? { decimals: token.decimals } : {},
|
|
683
|
+
...token.symbol !== void 0 ? { symbol: token.symbol } : {}
|
|
684
|
+
})
|
|
685
|
+
];
|
|
686
|
+
}
|
|
687
|
+
async function run(ctx) {
|
|
688
|
+
const client = await ctx.client();
|
|
689
|
+
const chains = await client.wallet.balances();
|
|
690
|
+
ctx.out.render({ identity: client.identity, chains }, () => {
|
|
691
|
+
for (const chain of chains) {
|
|
692
|
+
ctx.out.line(`${chain.chainKey} ${chain.address}`);
|
|
693
|
+
if (chain.unreadable === true) {
|
|
694
|
+
ctx.out.rows([["unreadable", chain.error ?? "the RPC could not be reached"]], " ");
|
|
695
|
+
ctx.out.line();
|
|
696
|
+
continue;
|
|
697
|
+
}
|
|
698
|
+
const rows = [];
|
|
699
|
+
if (chain.native !== void 0) rows.push(amountRow(chain.native));
|
|
700
|
+
for (const token of chain.tokens) rows.push(amountRow(token));
|
|
701
|
+
ctx.out.rows(rows, " ");
|
|
702
|
+
ctx.out.line();
|
|
703
|
+
}
|
|
704
|
+
});
|
|
705
|
+
return 0;
|
|
706
|
+
}
|
|
707
|
+
|
|
708
|
+
// src/cli/commands/channel.ts
|
|
709
|
+
function stateRows(state, asset) {
|
|
710
|
+
const rows = [
|
|
711
|
+
["channel", state.channelId],
|
|
712
|
+
["chain", state.domain.chain],
|
|
713
|
+
["counterparty", state.counterparty],
|
|
714
|
+
["status", state.status],
|
|
715
|
+
["deposit", formatAmount(state.depositTotal, asset)],
|
|
716
|
+
["spent", formatAmount(state.spent, asset)],
|
|
717
|
+
["available", formatAmount(state.available, asset)],
|
|
718
|
+
["nonce", String(state.nonce)]
|
|
719
|
+
];
|
|
720
|
+
if (state.onChain?.closedAt !== void 0) {
|
|
721
|
+
rows.push(["closed at", `${state.onChain.closedAt.toString()} (unix seconds)`]);
|
|
722
|
+
}
|
|
723
|
+
if (state.onChain?.settleableAt !== void 0) {
|
|
724
|
+
rows.push(["settleable at", `${state.onChain.settleableAt.toString()} (unix seconds)`]);
|
|
725
|
+
}
|
|
726
|
+
return rows;
|
|
727
|
+
}
|
|
728
|
+
function connectorRows(entry, asset) {
|
|
729
|
+
if (!entry.ok) {
|
|
730
|
+
return [["connector", `cannot report this channel (${entry.error})`]];
|
|
731
|
+
}
|
|
732
|
+
return [
|
|
733
|
+
["connector nonce", String(entry.nonce)],
|
|
734
|
+
["connector claimed", formatAmount(entry.cumulativeClaimed, asset)],
|
|
735
|
+
[
|
|
736
|
+
"connector deposit",
|
|
737
|
+
entry.depositTotal === null ? "declared only" : formatAmount(entry.depositTotal, asset)
|
|
738
|
+
],
|
|
739
|
+
[
|
|
740
|
+
"connector available",
|
|
741
|
+
entry.available === null ? "unknown" : formatAmount(entry.available, asset)
|
|
742
|
+
]
|
|
743
|
+
];
|
|
744
|
+
}
|
|
745
|
+
async function run2(ctx) {
|
|
746
|
+
const sub = ctx.positionals[0];
|
|
747
|
+
if (sub === void 0 || !CHANNEL_SUBCOMMANDS.includes(sub)) {
|
|
748
|
+
throw new UsageError(
|
|
749
|
+
`channel needs one of: ${CHANNEL_SUBCOMMANDS.join(", ")}`,
|
|
750
|
+
"channel"
|
|
751
|
+
);
|
|
752
|
+
}
|
|
753
|
+
const client = await ctx.client();
|
|
754
|
+
const channel = client.channel;
|
|
755
|
+
if (sub === "open") {
|
|
756
|
+
const deposit = stringOption(ctx.values, "deposit");
|
|
757
|
+
const timeout = stringOption(ctx.values, "settlement-timeout");
|
|
758
|
+
const state2 = await channel.open({
|
|
759
|
+
...deposit !== void 0 ? { deposit } : {},
|
|
760
|
+
...timeout !== void 0 ? { settlementTimeout: Number(timeout) } : {}
|
|
761
|
+
});
|
|
762
|
+
const asset2 = assetFromTerms(state2.domain);
|
|
763
|
+
ctx.out.render(state2, () => {
|
|
764
|
+
ctx.out.line("Channel open.");
|
|
765
|
+
ctx.out.rows(stateRows(state2, asset2));
|
|
766
|
+
});
|
|
767
|
+
return 0;
|
|
768
|
+
}
|
|
769
|
+
if (sub === "deposit") {
|
|
770
|
+
const amount = ctx.positionals[1] ?? stringOption(ctx.values, "amount");
|
|
771
|
+
if (amount === void 0) {
|
|
772
|
+
throw new UsageError("channel deposit needs an amount in base units", "channel");
|
|
773
|
+
}
|
|
774
|
+
const state2 = await channel.deposit(amount);
|
|
775
|
+
const asset2 = assetFromTerms(state2.domain);
|
|
776
|
+
ctx.out.render(state2, () => {
|
|
777
|
+
ctx.out.line("Deposit confirmed.");
|
|
778
|
+
ctx.out.rows(stateRows(state2, asset2));
|
|
779
|
+
});
|
|
780
|
+
return 0;
|
|
781
|
+
}
|
|
782
|
+
if (sub === "close") {
|
|
783
|
+
const result = await channel.close();
|
|
784
|
+
ctx.out.render(result, () => {
|
|
785
|
+
ctx.out.line("Challenge period started. Settle once it has elapsed.");
|
|
786
|
+
const rows = [];
|
|
787
|
+
if (result.txHash !== void 0) rows.push(["tx", result.txHash]);
|
|
788
|
+
if (result.closedAt !== void 0) rows.push(["closed at", result.closedAt.toString()]);
|
|
789
|
+
if (result.settleableAt !== void 0) {
|
|
790
|
+
rows.push(["settleable at", result.settleableAt.toString()]);
|
|
791
|
+
}
|
|
792
|
+
ctx.out.rows(rows);
|
|
793
|
+
});
|
|
794
|
+
return 0;
|
|
795
|
+
}
|
|
796
|
+
if (sub === "settle") {
|
|
797
|
+
const result = await channel.settle();
|
|
798
|
+
ctx.out.render(result, () => {
|
|
799
|
+
ctx.out.line("Channel settled; the collateral is released.");
|
|
800
|
+
if (result.txHash !== void 0) ctx.out.rows([["tx", result.txHash]]);
|
|
801
|
+
});
|
|
802
|
+
return 0;
|
|
803
|
+
}
|
|
804
|
+
const state = await channel.state({ onChain: true });
|
|
805
|
+
const asset = assetFromTerms(state.domain);
|
|
806
|
+
let connectorView;
|
|
807
|
+
if (boolOption(ctx.values, "connector-view")) {
|
|
808
|
+
const entries = await client.claimState([state.channelId]);
|
|
809
|
+
connectorView = entries[0];
|
|
810
|
+
}
|
|
811
|
+
ctx.out.render({ ...state, ...connectorView !== void 0 ? { connectorView } : {} }, () => {
|
|
812
|
+
ctx.out.rows(stateRows(state, asset));
|
|
813
|
+
if (connectorView !== void 0) {
|
|
814
|
+
ctx.out.line();
|
|
815
|
+
ctx.out.rows(connectorRows(connectorView, asset));
|
|
816
|
+
}
|
|
817
|
+
});
|
|
818
|
+
return 0;
|
|
819
|
+
}
|
|
820
|
+
|
|
821
|
+
// src/cli/commands/claim-state.ts
|
|
822
|
+
async function run3(ctx) {
|
|
823
|
+
const client = await ctx.client();
|
|
824
|
+
const entries = await client.claimState();
|
|
825
|
+
const description = await client.describe();
|
|
826
|
+
const asset = assetFromSettlement(description.settlements[0]);
|
|
827
|
+
ctx.out.render({ connector: client.connector, channels: entries }, () => {
|
|
828
|
+
if (entries.length === 0) {
|
|
829
|
+
ctx.out.line("This connector reports no channels for your account.");
|
|
830
|
+
ctx.out.line("Open one with 'toon channel open --deposit 100000'.");
|
|
831
|
+
return;
|
|
832
|
+
}
|
|
833
|
+
for (const entry of entries) {
|
|
834
|
+
const id = entry.channelId ?? entry.channelAccount ?? "(unnamed)";
|
|
835
|
+
if (!entry.ok) {
|
|
836
|
+
ctx.out.rows([
|
|
837
|
+
["channel", id],
|
|
838
|
+
["chain", entry.blockchain],
|
|
839
|
+
["state", `unreadable (${entry.error})`]
|
|
840
|
+
]);
|
|
841
|
+
ctx.out.line();
|
|
842
|
+
continue;
|
|
843
|
+
}
|
|
844
|
+
ctx.out.rows([
|
|
845
|
+
["channel", id],
|
|
846
|
+
["chain", entry.blockchain],
|
|
847
|
+
["nonce", String(entry.nonce)],
|
|
848
|
+
["claimed", formatAmount(entry.cumulativeClaimed, asset)],
|
|
849
|
+
[
|
|
850
|
+
"deposit",
|
|
851
|
+
entry.depositTotal === null ? "declared only" : formatAmount(entry.depositTotal, asset)
|
|
852
|
+
],
|
|
853
|
+
["available", entry.available === null ? "unknown" : formatAmount(entry.available, asset)],
|
|
854
|
+
[
|
|
855
|
+
// Printed as the connector gave it, unconverted: the field is
|
|
856
|
+
// best-effort and non-durable, and `null` means "unknown", never
|
|
857
|
+
// "never claimed" (client-edge-spec.md §1.10).
|
|
858
|
+
"last claim",
|
|
859
|
+
entry.lastClaimTime === null ? 'unknown \u2014 not "never"' : String(entry.lastClaimTime)
|
|
860
|
+
]
|
|
861
|
+
]);
|
|
862
|
+
ctx.out.line();
|
|
863
|
+
}
|
|
864
|
+
});
|
|
865
|
+
return 0;
|
|
866
|
+
}
|
|
867
|
+
|
|
868
|
+
// src/cli/commands/describe.ts
|
|
869
|
+
async function run4(ctx) {
|
|
870
|
+
const url = ctx.positionals[0];
|
|
871
|
+
const client = await ctx.client({
|
|
872
|
+
keyless: true,
|
|
873
|
+
...url !== void 0 ? { connector: url } : {}
|
|
874
|
+
});
|
|
875
|
+
const description = await client.describe();
|
|
876
|
+
const asset = assetFromSettlement(description.settlements[0]);
|
|
877
|
+
const payload = {
|
|
878
|
+
connector: client.connector,
|
|
879
|
+
ilpAddresses: description.ilpAddresses,
|
|
880
|
+
httpEndpoint: description.httpEndpoint,
|
|
881
|
+
btpEndpoint: description.btpEndpoint,
|
|
882
|
+
peerCarriages: description.peerCarriages,
|
|
883
|
+
edgeIdentity: description.edgeIdentity,
|
|
884
|
+
settlements: description.settlements,
|
|
885
|
+
routes: description.routes,
|
|
886
|
+
requiredTransport: description.requiredTransport,
|
|
887
|
+
supportedVersions: description.supportedVersions,
|
|
888
|
+
defaultVersion: description.defaultVersion
|
|
889
|
+
};
|
|
890
|
+
ctx.out.render(payload, () => {
|
|
891
|
+
const head = [["connector", client.connector]];
|
|
892
|
+
if (description.ilpAddresses.length > 0) {
|
|
893
|
+
head.push(["addresses", description.ilpAddresses.join(", ")]);
|
|
894
|
+
}
|
|
895
|
+
if (description.httpEndpoint !== void 0) head.push(["http", description.httpEndpoint]);
|
|
896
|
+
if (description.btpEndpoint !== void 0) head.push(["btp", description.btpEndpoint]);
|
|
897
|
+
if (description.requiredTransport !== void 0) {
|
|
898
|
+
head.push(["requires", `${description.requiredTransport} carriage`]);
|
|
899
|
+
}
|
|
900
|
+
if (description.edgeIdentity !== void 0) {
|
|
901
|
+
head.push([
|
|
902
|
+
"seals to",
|
|
903
|
+
`${description.edgeIdentity.publicKey}${description.edgeIdentity.keyId === "" ? "" : ` (key ${description.edgeIdentity.keyId})`}`
|
|
904
|
+
]);
|
|
905
|
+
}
|
|
906
|
+
head.push([
|
|
907
|
+
"versions",
|
|
908
|
+
`${description.supportedVersions.join(", ")} (default ${String(description.defaultVersion)})`
|
|
909
|
+
]);
|
|
910
|
+
ctx.out.rows(head);
|
|
911
|
+
ctx.out.line();
|
|
912
|
+
if (description.settlements.length === 0) {
|
|
913
|
+
ctx.out.line("Settlements: none \u2014 this node cannot be paid.");
|
|
914
|
+
} else {
|
|
915
|
+
ctx.out.line("Settlements:");
|
|
916
|
+
ctx.out.rows(
|
|
917
|
+
description.settlements.map((s) => [
|
|
918
|
+
s.chain,
|
|
919
|
+
[
|
|
920
|
+
`counterparty ${s.settlementAddress}`,
|
|
921
|
+
`token ${s.tokenAddress} (${String(s.decimals)}dp)`,
|
|
922
|
+
s.kind === "evm" ? `tokenNetwork ${s.tokenNetwork}` : `program ${s.programId}`
|
|
923
|
+
].join(" ")
|
|
924
|
+
])
|
|
925
|
+
);
|
|
926
|
+
}
|
|
927
|
+
ctx.out.line();
|
|
928
|
+
if (description.routes.length === 0) {
|
|
929
|
+
ctx.out.line("Routes: none published.");
|
|
930
|
+
} else {
|
|
931
|
+
ctx.out.line("Routes:");
|
|
932
|
+
ctx.out.rows(
|
|
933
|
+
description.routes.map((r) => [r.prefix, formatAmount(r.price, asset)])
|
|
934
|
+
);
|
|
935
|
+
}
|
|
936
|
+
});
|
|
937
|
+
return 0;
|
|
938
|
+
}
|
|
939
|
+
|
|
940
|
+
// src/cli/commands/faucet.ts
|
|
941
|
+
async function run5(ctx) {
|
|
942
|
+
const client = await ctx.client();
|
|
943
|
+
const result = await client.wallet.faucet();
|
|
944
|
+
ctx.out.render(result, () => {
|
|
945
|
+
ctx.out.line("Faucet request accepted.");
|
|
946
|
+
ctx.out.rows([
|
|
947
|
+
["chain", result.chain],
|
|
948
|
+
["address", result.address]
|
|
949
|
+
]);
|
|
950
|
+
ctx.out.line();
|
|
951
|
+
ctx.out.line("Check it landed with 'toon balances'.");
|
|
952
|
+
});
|
|
953
|
+
return 0;
|
|
954
|
+
}
|
|
955
|
+
|
|
956
|
+
// src/cli/commands/identity.ts
|
|
957
|
+
var SOLANA_PATH = "m/44'/501'/0'/0'";
|
|
958
|
+
async function run6(ctx) {
|
|
959
|
+
const keys = await resolveKeyMaterial(ctx.settings, ctx.deps);
|
|
960
|
+
if (keys.kind !== "mnemonic") {
|
|
961
|
+
throw new Error("identity requires real keys");
|
|
962
|
+
}
|
|
963
|
+
const showAll = boolOption(ctx.values, "all-derivations");
|
|
964
|
+
const identity = deriveFullIdentity(keys.mnemonic, { scheme: keys.derivation });
|
|
965
|
+
const source = keys.from === "env" ? MNEMONIC_ENV : ctx.settings.keystorePath;
|
|
966
|
+
const derivations = ["standard", "legacy"].map((scheme) => ({
|
|
967
|
+
scheme,
|
|
968
|
+
path: evmDerivationPath(scheme),
|
|
969
|
+
evmAddress: deriveFullIdentity(keys.mnemonic, { scheme }).evm.address
|
|
970
|
+
}));
|
|
971
|
+
ctx.out.render(
|
|
972
|
+
{
|
|
973
|
+
source,
|
|
974
|
+
derivation: keys.derivation,
|
|
975
|
+
evmAddress: identity.evm.address,
|
|
976
|
+
evmPath: evmDerivationPath(keys.derivation),
|
|
977
|
+
solanaPublicKey: identity.solana.publicKey,
|
|
978
|
+
solanaPath: SOLANA_PATH,
|
|
979
|
+
...showAll ? { derivations } : {}
|
|
980
|
+
},
|
|
981
|
+
() => {
|
|
982
|
+
ctx.out.rows([
|
|
983
|
+
["evm", `${identity.evm.address} ${evmDerivationPath(keys.derivation)}`],
|
|
984
|
+
["solana", `${identity.solana.publicKey} ${SOLANA_PATH}`],
|
|
985
|
+
["derivation", keys.derivation],
|
|
986
|
+
["keys from", source]
|
|
987
|
+
]);
|
|
988
|
+
if (showAll) {
|
|
989
|
+
ctx.out.line();
|
|
990
|
+
ctx.out.line("EVM address under each derivation:");
|
|
991
|
+
ctx.out.rows(
|
|
992
|
+
derivations.map((d) => [
|
|
993
|
+
d.scheme === keys.derivation ? `${d.scheme} (in use)` : d.scheme,
|
|
994
|
+
`${d.evmAddress} ${d.path}`
|
|
995
|
+
])
|
|
996
|
+
);
|
|
997
|
+
}
|
|
998
|
+
}
|
|
999
|
+
);
|
|
1000
|
+
return 0;
|
|
1001
|
+
}
|
|
1002
|
+
|
|
1003
|
+
// src/cli/commands/init.ts
|
|
1004
|
+
async function run7(ctx) {
|
|
1005
|
+
const path = ctx.settings.keystorePath;
|
|
1006
|
+
const derivation = boolOption(ctx.values, "legacy-derivation") ? "legacy" : "standard";
|
|
1007
|
+
const importing = boolOption(ctx.values, "import");
|
|
1008
|
+
if (keystoreExists(path)) {
|
|
1009
|
+
throw new CliConfigError(
|
|
1010
|
+
`a keystore already exists at ${path}`,
|
|
1011
|
+
"Move it aside first \u2014 overwriting it would destroy the only copy of its phrase, and any channel collateral that phrase controls."
|
|
1012
|
+
);
|
|
1013
|
+
}
|
|
1014
|
+
let imported;
|
|
1015
|
+
if (importing) {
|
|
1016
|
+
const readStdin2 = ctx.deps.readStdin;
|
|
1017
|
+
if (readStdin2 === void 0) {
|
|
1018
|
+
throw new CliConfigError("--import reads the phrase from stdin, and stdin is unavailable");
|
|
1019
|
+
}
|
|
1020
|
+
ctx.out.warn("Reading the BIP-39 recovery phrase from stdin.");
|
|
1021
|
+
const bytes = await readStdin2();
|
|
1022
|
+
imported = Buffer.from(bytes).toString("utf8").trim();
|
|
1023
|
+
if (imported.length === 0) throw new CliConfigError("no phrase arrived on stdin");
|
|
1024
|
+
}
|
|
1025
|
+
const { password, source } = await resolvePassword({
|
|
1026
|
+
message: `Choose a password for ${path}: `,
|
|
1027
|
+
passwordFile: ctx.settings.passwordFile,
|
|
1028
|
+
env: ctx.deps.env,
|
|
1029
|
+
...ctx.deps.readFile !== void 0 ? { readFile: ctx.deps.readFile } : {},
|
|
1030
|
+
prompt: ctx.deps.prompt
|
|
1031
|
+
});
|
|
1032
|
+
if (source === "prompt" && ctx.deps.prompt !== void 0) {
|
|
1033
|
+
const again = await ctx.deps.prompt("Confirm the password: ");
|
|
1034
|
+
if (again !== password) throw new CliConfigError("the two passwords did not match");
|
|
1035
|
+
}
|
|
1036
|
+
ensureKeystoreDirectory(path);
|
|
1037
|
+
let mnemonic;
|
|
1038
|
+
let generated;
|
|
1039
|
+
if (imported !== void 0) {
|
|
1040
|
+
try {
|
|
1041
|
+
importKeystore(path, imported, password, { derivation });
|
|
1042
|
+
} catch (err) {
|
|
1043
|
+
throw new CliConfigError(err instanceof Error ? err.message : String(err));
|
|
1044
|
+
}
|
|
1045
|
+
mnemonic = imported;
|
|
1046
|
+
generated = false;
|
|
1047
|
+
} else {
|
|
1048
|
+
mnemonic = generateKeystore(path, password, { derivation }).mnemonic;
|
|
1049
|
+
generated = true;
|
|
1050
|
+
}
|
|
1051
|
+
const identity = deriveFullIdentity(mnemonic, { scheme: derivation });
|
|
1052
|
+
ctx.out.render(
|
|
1053
|
+
{
|
|
1054
|
+
keystore: path,
|
|
1055
|
+
derivation,
|
|
1056
|
+
evmAddress: identity.evm.address,
|
|
1057
|
+
evmPath: evmDerivationPath(derivation),
|
|
1058
|
+
solanaPublicKey: identity.solana.publicKey,
|
|
1059
|
+
// Only ever present for a phrase this command just invented: there is one
|
|
1060
|
+
// chance to copy it down, and after that only the keystore has it.
|
|
1061
|
+
...generated ? { mnemonic } : {}
|
|
1062
|
+
},
|
|
1063
|
+
() => {
|
|
1064
|
+
if (generated) {
|
|
1065
|
+
ctx.out.line("Write this recovery phrase down. It is shown once, and it is the only");
|
|
1066
|
+
ctx.out.line("way back to these addresses if the keystore file is lost.");
|
|
1067
|
+
ctx.out.line();
|
|
1068
|
+
ctx.out.line(` ${mnemonic}`);
|
|
1069
|
+
ctx.out.line();
|
|
1070
|
+
}
|
|
1071
|
+
ctx.out.rows([
|
|
1072
|
+
["keystore", path],
|
|
1073
|
+
["derivation", derivation],
|
|
1074
|
+
["evm", `${identity.evm.address} ${evmDerivationPath(derivation)}`],
|
|
1075
|
+
["solana", identity.solana.publicKey]
|
|
1076
|
+
]);
|
|
1077
|
+
ctx.out.line();
|
|
1078
|
+
ctx.out.line("Next: fund it, then open a channel.");
|
|
1079
|
+
ctx.out.line(" toon faucet");
|
|
1080
|
+
ctx.out.line(" toon channel open --deposit 100000");
|
|
1081
|
+
}
|
|
1082
|
+
);
|
|
1083
|
+
return 0;
|
|
1084
|
+
}
|
|
1085
|
+
|
|
1086
|
+
// src/cli/commands/price.ts
|
|
1087
|
+
async function run8(ctx) {
|
|
1088
|
+
const destination = ctx.positionals[0];
|
|
1089
|
+
if (destination === void 0) throw new UsageError("price needs a destination", "price");
|
|
1090
|
+
const url = ctx.positionals[1];
|
|
1091
|
+
const client = await ctx.client({
|
|
1092
|
+
keyless: true,
|
|
1093
|
+
...url !== void 0 ? { connector: url } : {}
|
|
1094
|
+
});
|
|
1095
|
+
const terms = await client.routePrice(destination);
|
|
1096
|
+
const description = await client.describe();
|
|
1097
|
+
const asset = assetFromSettlement(description.settlements[0]);
|
|
1098
|
+
const perKib = terms?.pricePerKib;
|
|
1099
|
+
ctx.out.render(
|
|
1100
|
+
{
|
|
1101
|
+
connector: client.connector,
|
|
1102
|
+
destination,
|
|
1103
|
+
price: terms?.price ?? null,
|
|
1104
|
+
...perKib !== void 0 ? { pricePerKib: perKib } : {},
|
|
1105
|
+
decimals: asset.decimals,
|
|
1106
|
+
asset: asset.symbol
|
|
1107
|
+
},
|
|
1108
|
+
() => {
|
|
1109
|
+
if (terms === null) {
|
|
1110
|
+
ctx.out.line(`${destination} no route \u2014 this node prices nothing that covers it`);
|
|
1111
|
+
} else if (perKib === void 0) {
|
|
1112
|
+
ctx.out.line(`${destination} ${formatAmount(terms.price, asset)}`);
|
|
1113
|
+
} else {
|
|
1114
|
+
ctx.out.line(
|
|
1115
|
+
`${destination} ${formatAmount(terms.price, asset)} + ${formatAmount(perKib, asset)}/KiB of sealed payload`
|
|
1116
|
+
);
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
);
|
|
1120
|
+
return 0;
|
|
1121
|
+
}
|
|
1122
|
+
|
|
1123
|
+
// src/cli/commands/probe.ts
|
|
1124
|
+
async function run9(ctx) {
|
|
1125
|
+
const destination = ctx.positionals[0];
|
|
1126
|
+
if (destination === void 0) throw new UsageError("probe needs a destination", "probe");
|
|
1127
|
+
const client = await ctx.client();
|
|
1128
|
+
const result = await client.probe(destination);
|
|
1129
|
+
const description = await client.describe();
|
|
1130
|
+
const asset = assetFromSettlement(description.settlements[0]);
|
|
1131
|
+
ctx.out.render({ destination, ...result }, () => {
|
|
1132
|
+
ctx.out.rows([
|
|
1133
|
+
["destination", destination],
|
|
1134
|
+
["cost", formatAmount(result.accumulatedCost, asset)],
|
|
1135
|
+
["code", result.code],
|
|
1136
|
+
["message", result.message]
|
|
1137
|
+
]);
|
|
1138
|
+
});
|
|
1139
|
+
return 0;
|
|
1140
|
+
}
|
|
1141
|
+
|
|
1142
|
+
// src/cli/commands/send.ts
|
|
1143
|
+
function parseHeaderSpec(spec) {
|
|
1144
|
+
const colon = spec.indexOf(":");
|
|
1145
|
+
if (colon <= 0) {
|
|
1146
|
+
throw new UsageError(`-H expects 'name:value'; got '${spec}'`, "send");
|
|
1147
|
+
}
|
|
1148
|
+
return [spec.slice(0, colon).trim(), spec.slice(colon + 1).trim()];
|
|
1149
|
+
}
|
|
1150
|
+
function hasHeader(headers, name) {
|
|
1151
|
+
const wanted = name.toLowerCase();
|
|
1152
|
+
return headers.some(([key]) => key.toLowerCase() === wanted);
|
|
1153
|
+
}
|
|
1154
|
+
async function buildRequest(ctx) {
|
|
1155
|
+
const bodyFlag = stringOption(ctx.values, "body");
|
|
1156
|
+
const bodyFile = stringOption(ctx.values, "body-file");
|
|
1157
|
+
if (bodyFlag !== void 0 && bodyFile !== void 0) {
|
|
1158
|
+
throw new UsageError("give --body or --body-file, not both", "send");
|
|
1159
|
+
}
|
|
1160
|
+
const headers = listOption(ctx.values, "header").map(parseHeaderSpec);
|
|
1161
|
+
let body;
|
|
1162
|
+
if (bodyFlag === "-") {
|
|
1163
|
+
const readStdin2 = ctx.deps.readStdin;
|
|
1164
|
+
if (readStdin2 === void 0) {
|
|
1165
|
+
throw new UsageError("--body - reads stdin, and stdin is unavailable", "send");
|
|
1166
|
+
}
|
|
1167
|
+
body = await readStdin2();
|
|
1168
|
+
} else if (bodyFlag !== void 0) {
|
|
1169
|
+
body = bodyFlag;
|
|
1170
|
+
} else if (bodyFile !== void 0) {
|
|
1171
|
+
const readFileBytes = ctx.deps.readFileBytes;
|
|
1172
|
+
if (readFileBytes === void 0) {
|
|
1173
|
+
throw new UsageError("--body-file cannot be read in this environment", "send");
|
|
1174
|
+
}
|
|
1175
|
+
try {
|
|
1176
|
+
body = readFileBytes(bodyFile);
|
|
1177
|
+
} catch (err) {
|
|
1178
|
+
throw new UsageError(
|
|
1179
|
+
`cannot read --body-file ${bodyFile}: ${err instanceof Error ? err.message : String(err)}`,
|
|
1180
|
+
"send"
|
|
1181
|
+
);
|
|
1182
|
+
}
|
|
1183
|
+
}
|
|
1184
|
+
if (ctx.values["json-body"] === true) {
|
|
1185
|
+
if (body === void 0) throw new UsageError("--json-body needs a body", "send");
|
|
1186
|
+
const text = typeof body === "string" ? body : decodeUtf8(body);
|
|
1187
|
+
if (text === void 0) throw new UsageError("--json-body was given bytes that are not UTF-8", "send");
|
|
1188
|
+
try {
|
|
1189
|
+
JSON.parse(text);
|
|
1190
|
+
} catch (err) {
|
|
1191
|
+
throw new UsageError(
|
|
1192
|
+
`--json-body was given something that is not JSON: ${err instanceof Error ? err.message : String(err)}`,
|
|
1193
|
+
"send"
|
|
1194
|
+
);
|
|
1195
|
+
}
|
|
1196
|
+
if (!hasHeader(headers, "content-type")) headers.push(["content-type", "application/json"]);
|
|
1197
|
+
}
|
|
1198
|
+
const method = stringOption(ctx.values, "method");
|
|
1199
|
+
const target = stringOption(ctx.values, "target");
|
|
1200
|
+
return {
|
|
1201
|
+
...method !== void 0 ? { method } : {},
|
|
1202
|
+
...target !== void 0 ? { target } : {},
|
|
1203
|
+
...headers.length > 0 ? { headers } : {},
|
|
1204
|
+
...body !== void 0 ? { body } : {}
|
|
1205
|
+
};
|
|
1206
|
+
}
|
|
1207
|
+
function refusalHint(result, asset) {
|
|
1208
|
+
const cost = result.accumulatedCost === void 0 ? void 0 : formatAmount(result.accumulatedCost, asset);
|
|
1209
|
+
switch (result.code) {
|
|
1210
|
+
case "PAYMENT_REQUIRED":
|
|
1211
|
+
case "TRANSPORT_REQUIRED": {
|
|
1212
|
+
const required = result.terms?.requiredTransport;
|
|
1213
|
+
if (required !== void 0) {
|
|
1214
|
+
return [`This route only accepts the ${required} carriage. Retry with --transport ${required}.`];
|
|
1215
|
+
}
|
|
1216
|
+
return [
|
|
1217
|
+
"The connector answered with terms instead of doing the work, which means it saw no",
|
|
1218
|
+
"usable claim. Check the channel with 'toon channel status --connector-view'."
|
|
1219
|
+
];
|
|
1220
|
+
}
|
|
1221
|
+
case "F03":
|
|
1222
|
+
return cost === void 0 ? ["The claim did not advance far enough to cover the route."] : [
|
|
1223
|
+
`The route costs ${cost}. Either the claim did not advance by that much, or the`,
|
|
1224
|
+
"channel has no room left for it \u2014 add collateral with",
|
|
1225
|
+
"'toon channel deposit <base units>', then send again."
|
|
1226
|
+
];
|
|
1227
|
+
case "F01":
|
|
1228
|
+
return [
|
|
1229
|
+
"The connector would not take the claim: it names a channel it cannot see, or a nonce",
|
|
1230
|
+
"it has already banked. Compare the two watermarks with 'toon channel status --connector-view'."
|
|
1231
|
+
];
|
|
1232
|
+
case "F02":
|
|
1233
|
+
return [
|
|
1234
|
+
"No route to that destination over this carriage. Check what this node actually serves",
|
|
1235
|
+
"with 'toon describe', and which carriage it wants."
|
|
1236
|
+
];
|
|
1237
|
+
case "F00":
|
|
1238
|
+
return [
|
|
1239
|
+
"The target path was refused before the app saw it: --target is resolved strictly beneath",
|
|
1240
|
+
"the route handler, so it cannot be absolute, contain `..`, or name a host."
|
|
1241
|
+
];
|
|
1242
|
+
case "F06":
|
|
1243
|
+
return ["No claim was attached. Open a channel with 'toon channel open --deposit 100000'."];
|
|
1244
|
+
default:
|
|
1245
|
+
if (result.code.startsWith("T")) {
|
|
1246
|
+
return ["A temporary refusal \u2014 the path asked you to try again shortly."];
|
|
1247
|
+
}
|
|
1248
|
+
return [];
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
function payloadFor(result) {
|
|
1252
|
+
if (result.fulfilled) {
|
|
1253
|
+
const text = decodeUtf8(result.body);
|
|
1254
|
+
return {
|
|
1255
|
+
fulfilled: true,
|
|
1256
|
+
transport: result.transport,
|
|
1257
|
+
status: result.status,
|
|
1258
|
+
headers: result.headers,
|
|
1259
|
+
body: result.body,
|
|
1260
|
+
...text !== void 0 ? { text } : {},
|
|
1261
|
+
fulfillment: result.fulfillment,
|
|
1262
|
+
claim: result.claim
|
|
1263
|
+
};
|
|
1264
|
+
}
|
|
1265
|
+
const { terms, ...rest } = result;
|
|
1266
|
+
return {
|
|
1267
|
+
...rest,
|
|
1268
|
+
...terms !== void 0 ? {
|
|
1269
|
+
terms: {
|
|
1270
|
+
destination: terms.destination,
|
|
1271
|
+
price: terms.price,
|
|
1272
|
+
httpEndpoint: terms.httpEndpoint,
|
|
1273
|
+
btpEndpoint: terms.btpEndpoint,
|
|
1274
|
+
requiredTransport: terms.requiredTransport,
|
|
1275
|
+
settlements: terms.settlements,
|
|
1276
|
+
sessionLeaseTtlMs: terms.sessionLeaseTtlMs
|
|
1277
|
+
}
|
|
1278
|
+
} : {}
|
|
1279
|
+
};
|
|
1280
|
+
}
|
|
1281
|
+
async function run10(ctx) {
|
|
1282
|
+
const request = await buildRequest(ctx);
|
|
1283
|
+
const amountFlag = stringOption(ctx.values, "amount");
|
|
1284
|
+
const client = await ctx.client();
|
|
1285
|
+
const destination = ctx.positionals[0] ?? client.defaultDestination;
|
|
1286
|
+
if (destination === void 0) {
|
|
1287
|
+
throw new UsageError(
|
|
1288
|
+
"send needs a destination, and this node published no address to default to",
|
|
1289
|
+
"send"
|
|
1290
|
+
);
|
|
1291
|
+
}
|
|
1292
|
+
const result = await client.send(
|
|
1293
|
+
destination,
|
|
1294
|
+
request,
|
|
1295
|
+
amountFlag === void 0 ? void 0 : { amount: BigInt(amountFlag) }
|
|
1296
|
+
);
|
|
1297
|
+
const description = await client.describe();
|
|
1298
|
+
const asset = assetFromSettlement(description.settlements[0]);
|
|
1299
|
+
ctx.out.render(payloadFor(result), () => {
|
|
1300
|
+
if (result.fulfilled) {
|
|
1301
|
+
ctx.out.line(`FULFILL ${String(result.status)} (${result.transport})`);
|
|
1302
|
+
const claim = result.claim;
|
|
1303
|
+
ctx.out.rows(
|
|
1304
|
+
claim === void 0 ? (
|
|
1305
|
+
// A route priced at zero takes no claim, so there is no channel,
|
|
1306
|
+
// nonce or amount to show. Saying so beats printing zeroes.
|
|
1307
|
+
[["paid", "nothing \u2014 this route is free"]]
|
|
1308
|
+
) : [
|
|
1309
|
+
[
|
|
1310
|
+
"paid",
|
|
1311
|
+
`${formatAmount(claim.amount, asset)} channel ${claim.channelId} nonce ${String(claim.nonce)}`
|
|
1312
|
+
],
|
|
1313
|
+
["cumulative", formatAmount(claim.cumulative, asset)]
|
|
1314
|
+
]
|
|
1315
|
+
);
|
|
1316
|
+
if (result.headers.length > 0) {
|
|
1317
|
+
ctx.out.line();
|
|
1318
|
+
ctx.out.rows(result.headers.map(([k, v]) => [`${k}:`, v]));
|
|
1319
|
+
}
|
|
1320
|
+
ctx.out.line();
|
|
1321
|
+
const text = decodeUtf8(result.body);
|
|
1322
|
+
if (text === void 0) {
|
|
1323
|
+
ctx.out.line(`<${String(result.body.length)} bytes of binary; use --json for base64>`);
|
|
1324
|
+
} else if (text.length > 0) {
|
|
1325
|
+
ctx.out.line(text.replace(/\n$/, ""));
|
|
1326
|
+
}
|
|
1327
|
+
return;
|
|
1328
|
+
}
|
|
1329
|
+
ctx.out.line(
|
|
1330
|
+
`REFUSED ${result.code} (${result.transport}, refused by ${result.refusedBy})`
|
|
1331
|
+
);
|
|
1332
|
+
const rows = [["message", result.message]];
|
|
1333
|
+
if (result.accumulatedCost !== void 0) {
|
|
1334
|
+
rows.push(["cost", formatAmount(result.accumulatedCost, asset)]);
|
|
1335
|
+
}
|
|
1336
|
+
if (result.claimAck !== void 0) {
|
|
1337
|
+
rows.push([
|
|
1338
|
+
"claim",
|
|
1339
|
+
result.claimAck.result === "accepted" ? "accepted" : `rejected (${result.claimAck.reason ?? "no reason given"})`
|
|
1340
|
+
]);
|
|
1341
|
+
}
|
|
1342
|
+
if (result.terms !== void 0) {
|
|
1343
|
+
rows.push(["route price", formatAmount(result.terms.price, asset)]);
|
|
1344
|
+
}
|
|
1345
|
+
ctx.out.rows(rows);
|
|
1346
|
+
const hint = refusalHint(result, asset);
|
|
1347
|
+
if (hint.length > 0) {
|
|
1348
|
+
ctx.out.line();
|
|
1349
|
+
for (const line of hint) ctx.out.line(line);
|
|
1350
|
+
}
|
|
1351
|
+
});
|
|
1352
|
+
return result.fulfilled ? 0 : 3;
|
|
1353
|
+
}
|
|
1354
|
+
|
|
1355
|
+
// src/cli/commands/transfer.ts
|
|
1356
|
+
async function run11(ctx) {
|
|
1357
|
+
const to = stringOption(ctx.values, "to");
|
|
1358
|
+
const amount = stringOption(ctx.values, "amount");
|
|
1359
|
+
const assetFlag = stringOption(ctx.values, "asset") ?? "token";
|
|
1360
|
+
if (to === void 0) throw new UsageError("transfer needs --to <address>", "transfer");
|
|
1361
|
+
if (amount === void 0) {
|
|
1362
|
+
throw new UsageError("transfer needs --amount <base units>", "transfer");
|
|
1363
|
+
}
|
|
1364
|
+
if (assetFlag !== "native" && assetFlag !== "token") {
|
|
1365
|
+
throw new UsageError(`--asset must be 'native' or 'token'; got '${assetFlag}'`, "transfer");
|
|
1366
|
+
}
|
|
1367
|
+
const asset = assetFlag;
|
|
1368
|
+
const client = await ctx.client();
|
|
1369
|
+
const result = await client.wallet.transfer({ chain: client.chain, asset, to, amount });
|
|
1370
|
+
ctx.out.render(result, () => {
|
|
1371
|
+
ctx.out.line("Transfer delivered.");
|
|
1372
|
+
ctx.out.rows([
|
|
1373
|
+
["chain", result.chain],
|
|
1374
|
+
["asset", result.asset],
|
|
1375
|
+
["to", result.to],
|
|
1376
|
+
["amount", formatAmount(result.amount)],
|
|
1377
|
+
["tx", result.txHash],
|
|
1378
|
+
["balance before", formatAmount(result.balanceBefore)],
|
|
1379
|
+
["balance after", formatAmount(result.balanceAfter)]
|
|
1380
|
+
]);
|
|
1381
|
+
});
|
|
1382
|
+
return 0;
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
// src/cli/commands/index.ts
|
|
1386
|
+
var RUNNERS = {
|
|
1387
|
+
init: run7,
|
|
1388
|
+
identity: run6,
|
|
1389
|
+
describe: run4,
|
|
1390
|
+
price: run8,
|
|
1391
|
+
probe: run9,
|
|
1392
|
+
send: run10,
|
|
1393
|
+
channel: run2,
|
|
1394
|
+
"claim-state": run3,
|
|
1395
|
+
balances: run,
|
|
1396
|
+
transfer: run11,
|
|
1397
|
+
faucet: run5
|
|
1398
|
+
};
|
|
1399
|
+
|
|
1400
|
+
// src/cli/stdin.ts
|
|
1401
|
+
async function readStdin(stream = process.stdin) {
|
|
1402
|
+
const chunks = [];
|
|
1403
|
+
for await (const chunk of stream) {
|
|
1404
|
+
chunks.push(typeof chunk === "string" ? Buffer.from(chunk, "utf8") : chunk);
|
|
1405
|
+
}
|
|
1406
|
+
return new Uint8Array(Buffer.concat(chunks));
|
|
1407
|
+
}
|
|
1408
|
+
function isInteractive(input = process.stdin) {
|
|
1409
|
+
return input.isTTY === true && typeof input.setRawMode === "function";
|
|
1410
|
+
}
|
|
1411
|
+
var CTRL_C = String.fromCharCode(3);
|
|
1412
|
+
var CTRL_D = String.fromCharCode(4);
|
|
1413
|
+
var BACKSPACE = String.fromCharCode(8);
|
|
1414
|
+
var DELETE = String.fromCharCode(127);
|
|
1415
|
+
function promptHidden(message, input = process.stdin, output = process.stderr) {
|
|
1416
|
+
return new Promise((resolve, reject) => {
|
|
1417
|
+
output.write(message);
|
|
1418
|
+
const wasRaw = input.isRaw;
|
|
1419
|
+
input.setRawMode(true);
|
|
1420
|
+
input.resume();
|
|
1421
|
+
input.setEncoding("utf8");
|
|
1422
|
+
let buffer = "";
|
|
1423
|
+
const finish = (fn) => {
|
|
1424
|
+
input.removeListener("data", onData);
|
|
1425
|
+
input.setRawMode(wasRaw);
|
|
1426
|
+
input.pause();
|
|
1427
|
+
output.write("\n");
|
|
1428
|
+
fn();
|
|
1429
|
+
};
|
|
1430
|
+
const onData = (chunk) => {
|
|
1431
|
+
for (const char of chunk) {
|
|
1432
|
+
if (char === "\r" || char === "\n" || char === CTRL_D) {
|
|
1433
|
+
finish(() => {
|
|
1434
|
+
resolve(buffer);
|
|
1435
|
+
});
|
|
1436
|
+
return;
|
|
1437
|
+
}
|
|
1438
|
+
if (char === CTRL_C) {
|
|
1439
|
+
finish(() => {
|
|
1440
|
+
reject(new Error("cancelled"));
|
|
1441
|
+
});
|
|
1442
|
+
return;
|
|
1443
|
+
}
|
|
1444
|
+
if (char === DELETE || char === BACKSPACE) {
|
|
1445
|
+
buffer = buffer.slice(0, -1);
|
|
1446
|
+
continue;
|
|
1447
|
+
}
|
|
1448
|
+
buffer += char;
|
|
1449
|
+
}
|
|
1450
|
+
};
|
|
1451
|
+
input.on("data", onData);
|
|
1452
|
+
});
|
|
1453
|
+
}
|
|
1454
|
+
|
|
1455
|
+
// src/cli/main.ts
|
|
1456
|
+
var EXIT = {
|
|
1457
|
+
ok: 0,
|
|
1458
|
+
unexpected: 1,
|
|
1459
|
+
usage: 2,
|
|
1460
|
+
refused: 3,
|
|
1461
|
+
funding: 4,
|
|
1462
|
+
network: 5,
|
|
1463
|
+
paymentRequired: 6
|
|
1464
|
+
};
|
|
1465
|
+
var USAGE_CODES = /* @__PURE__ */ new Set([
|
|
1466
|
+
"USAGE",
|
|
1467
|
+
"CONFIG",
|
|
1468
|
+
"VALIDATION_ERROR",
|
|
1469
|
+
"UNKNOWN_CHAIN",
|
|
1470
|
+
"INVALID_ADDRESS",
|
|
1471
|
+
"INVALID_DESTINATION",
|
|
1472
|
+
"CHAIN_UNAVAILABLE",
|
|
1473
|
+
"ROUTE_NOT_PRICED",
|
|
1474
|
+
"TRANSFER_UNSUPPORTED"
|
|
1475
|
+
]);
|
|
1476
|
+
var FUNDING_CODES = /* @__PURE__ */ new Set([
|
|
1477
|
+
"CHANNEL_FUNDING",
|
|
1478
|
+
"CHANNEL_RESUME",
|
|
1479
|
+
"CHANNEL_NOT_OPEN",
|
|
1480
|
+
"INSUFFICIENT_BALANCE",
|
|
1481
|
+
"TRANSFER_NOT_DELIVERED"
|
|
1482
|
+
]);
|
|
1483
|
+
var NETWORK_CODES = /* @__PURE__ */ new Set(["NETWORK_ERROR", "CONNECTOR_ERROR", "STALE_RPC_READ"]);
|
|
1484
|
+
var PAYMENT_CODES = /* @__PURE__ */ new Set([
|
|
1485
|
+
"PAYMENT_REQUIRED",
|
|
1486
|
+
"TRANSPORT_REQUIRED",
|
|
1487
|
+
"HTTP_402_REQUIRES_BTP",
|
|
1488
|
+
"HTTP_401_REQUIRES_BTP"
|
|
1489
|
+
]);
|
|
1490
|
+
var SYSCALL_CODES = /* @__PURE__ */ new Set([
|
|
1491
|
+
"ECONNREFUSED",
|
|
1492
|
+
"ECONNRESET",
|
|
1493
|
+
"ENOTFOUND",
|
|
1494
|
+
"EAI_AGAIN",
|
|
1495
|
+
"ETIMEDOUT",
|
|
1496
|
+
"EHOSTUNREACH",
|
|
1497
|
+
"ENETUNREACH",
|
|
1498
|
+
"UND_ERR_CONNECT_TIMEOUT",
|
|
1499
|
+
"UND_ERR_SOCKET"
|
|
1500
|
+
]);
|
|
1501
|
+
function errorCode(error) {
|
|
1502
|
+
if (typeof error !== "object" || error === null) return void 0;
|
|
1503
|
+
const code = error.code;
|
|
1504
|
+
return typeof code === "string" ? code : void 0;
|
|
1505
|
+
}
|
|
1506
|
+
function isTransportFailure(error) {
|
|
1507
|
+
let current = error;
|
|
1508
|
+
for (let depth = 0; depth < 8 && current != null; depth += 1) {
|
|
1509
|
+
const code = errorCode(current);
|
|
1510
|
+
if (code !== void 0 && SYSCALL_CODES.has(code)) return true;
|
|
1511
|
+
if (current instanceof TypeError && /fetch failed|network|socket/i.test(current.message)) {
|
|
1512
|
+
return true;
|
|
1513
|
+
}
|
|
1514
|
+
current = current instanceof Error ? current.cause : void 0;
|
|
1515
|
+
}
|
|
1516
|
+
return false;
|
|
1517
|
+
}
|
|
1518
|
+
function exitCodeFor(error) {
|
|
1519
|
+
const code = errorCode(error);
|
|
1520
|
+
if (code !== void 0) {
|
|
1521
|
+
if (USAGE_CODES.has(code)) return EXIT.usage;
|
|
1522
|
+
if (FUNDING_CODES.has(code)) return EXIT.funding;
|
|
1523
|
+
if (NETWORK_CODES.has(code)) return EXIT.network;
|
|
1524
|
+
if (PAYMENT_CODES.has(code)) return EXIT.paymentRequired;
|
|
1525
|
+
if (code.endsWith("_HTTP_STATUS") || code.endsWith("_MALFORMED")) return EXIT.network;
|
|
1526
|
+
}
|
|
1527
|
+
if (isTransportFailure(error)) return EXIT.network;
|
|
1528
|
+
return EXIT.unexpected;
|
|
1529
|
+
}
|
|
1530
|
+
function reportError(error, stderr) {
|
|
1531
|
+
if (error instanceof UsageError) {
|
|
1532
|
+
stderr(`toon: ${error.message}`);
|
|
1533
|
+
stderr("");
|
|
1534
|
+
stderr(usage(error.command));
|
|
1535
|
+
return EXIT.usage;
|
|
1536
|
+
}
|
|
1537
|
+
if (error instanceof CliConfigError) {
|
|
1538
|
+
stderr(`toon: ${error.message}`);
|
|
1539
|
+
if (error.hint !== void 0) stderr(` ${error.hint}`);
|
|
1540
|
+
return EXIT.usage;
|
|
1541
|
+
}
|
|
1542
|
+
const code = exitCodeFor(error);
|
|
1543
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
1544
|
+
stderr(`toon: ${message}`);
|
|
1545
|
+
if (code === EXIT.network) {
|
|
1546
|
+
stderr(" The connector or the chain RPC could not be reached. Check the URL and try again.");
|
|
1547
|
+
}
|
|
1548
|
+
if (code === EXIT.unexpected && error instanceof Error && error.stack !== void 0) {
|
|
1549
|
+
stderr(error.stack);
|
|
1550
|
+
}
|
|
1551
|
+
return code;
|
|
1552
|
+
}
|
|
1553
|
+
function packageVersion() {
|
|
1554
|
+
try {
|
|
1555
|
+
const url = new URL("../../package.json", import.meta.url);
|
|
1556
|
+
const parsed = JSON.parse(readFileSync2(url, "utf8"));
|
|
1557
|
+
const version = parsed.version;
|
|
1558
|
+
return typeof version === "string" ? version : "unknown";
|
|
1559
|
+
} catch {
|
|
1560
|
+
return "unknown";
|
|
1561
|
+
}
|
|
1562
|
+
}
|
|
1563
|
+
var OFFLINE_COMMANDS = /* @__PURE__ */ new Set(["init", "identity"]);
|
|
1564
|
+
function overridesConnector(command, positionals) {
|
|
1565
|
+
if (OFFLINE_COMMANDS.has(command)) return true;
|
|
1566
|
+
if (command === "describe") return positionals[0] !== void 0;
|
|
1567
|
+
if (command === "price") return positionals[1] !== void 0;
|
|
1568
|
+
return false;
|
|
1569
|
+
}
|
|
1570
|
+
function defaultDependencies(overrides = {}) {
|
|
1571
|
+
const interactive = overrides.prompt === void 0 && isInteractive();
|
|
1572
|
+
return {
|
|
1573
|
+
env: process.env,
|
|
1574
|
+
home: homedir3(),
|
|
1575
|
+
readFile: (path) => readFileSync2(path, "utf8"),
|
|
1576
|
+
readFileBytes: (path) => new Uint8Array(readFileSync2(path)),
|
|
1577
|
+
readStdin: () => readStdin(),
|
|
1578
|
+
// A prompt is offered only when there is a terminal to prompt on; otherwise
|
|
1579
|
+
// it stays `undefined` and the password resolution says so plainly instead
|
|
1580
|
+
// of hanging on a pipe that will never answer.
|
|
1581
|
+
...interactive ? { prompt: (message) => promptHidden(message) } : {},
|
|
1582
|
+
...overrides
|
|
1583
|
+
};
|
|
1584
|
+
}
|
|
1585
|
+
async function runCli(argv, options = {}) {
|
|
1586
|
+
const stdout = options.stdout ?? ((text) => process.stdout.write(`${text}
|
|
1587
|
+
`));
|
|
1588
|
+
const stderr = options.stderr ?? ((text) => process.stderr.write(`${text}
|
|
1589
|
+
`));
|
|
1590
|
+
let out = new Output({ stdout, stderr });
|
|
1591
|
+
let context;
|
|
1592
|
+
try {
|
|
1593
|
+
const line = parseCommandLine(argv);
|
|
1594
|
+
out = new Output({
|
|
1595
|
+
json: boolOption(line.values, "json"),
|
|
1596
|
+
quiet: boolOption(line.values, "quiet"),
|
|
1597
|
+
stdout,
|
|
1598
|
+
stderr
|
|
1599
|
+
});
|
|
1600
|
+
if (boolOption(line.values, "version")) {
|
|
1601
|
+
const version = packageVersion();
|
|
1602
|
+
out.render({ version }, () => {
|
|
1603
|
+
out.line(version);
|
|
1604
|
+
});
|
|
1605
|
+
return EXIT.ok;
|
|
1606
|
+
}
|
|
1607
|
+
if (line.command === void 0) {
|
|
1608
|
+
stdout(usage());
|
|
1609
|
+
return EXIT.ok;
|
|
1610
|
+
}
|
|
1611
|
+
if (line.command === "help") {
|
|
1612
|
+
stdout(usage(line.positionals[0]));
|
|
1613
|
+
return EXIT.ok;
|
|
1614
|
+
}
|
|
1615
|
+
if (boolOption(line.values, "help")) {
|
|
1616
|
+
stdout(usage(line.command));
|
|
1617
|
+
return EXIT.ok;
|
|
1618
|
+
}
|
|
1619
|
+
const deps = defaultDependencies(options.deps);
|
|
1620
|
+
const { settings, warnings } = resolveSettings(line.values, deps.env, { home: deps.home });
|
|
1621
|
+
if (!overridesConnector(line.command, line.positionals)) {
|
|
1622
|
+
for (const warning of warnings) out.warn(warning);
|
|
1623
|
+
}
|
|
1624
|
+
const runner = RUNNERS[line.command];
|
|
1625
|
+
if (runner === void 0) {
|
|
1626
|
+
throw new UsageError(`'${line.command}' has no implementation`, line.command);
|
|
1627
|
+
}
|
|
1628
|
+
context = new Context({
|
|
1629
|
+
command: line.command,
|
|
1630
|
+
positionals: line.positionals,
|
|
1631
|
+
values: line.values,
|
|
1632
|
+
settings,
|
|
1633
|
+
out,
|
|
1634
|
+
deps
|
|
1635
|
+
});
|
|
1636
|
+
return await runner(context);
|
|
1637
|
+
} catch (error) {
|
|
1638
|
+
return reportError(error, stderr);
|
|
1639
|
+
} finally {
|
|
1640
|
+
const client = context?.openClient;
|
|
1641
|
+
if (client !== void 0) {
|
|
1642
|
+
try {
|
|
1643
|
+
await client.close();
|
|
1644
|
+
} catch (error) {
|
|
1645
|
+
out.warn(`toon: closing the client failed: ${error instanceof Error ? error.message : String(error)}`);
|
|
1646
|
+
}
|
|
1647
|
+
}
|
|
1648
|
+
}
|
|
1649
|
+
}
|
|
1650
|
+
function isEntryPoint() {
|
|
1651
|
+
const entry = process.argv[1];
|
|
1652
|
+
if (entry === void 0) return false;
|
|
1653
|
+
try {
|
|
1654
|
+
return fileURLToPath(import.meta.url) === fileURLToPath(pathToFileURL(entry).href);
|
|
1655
|
+
} catch {
|
|
1656
|
+
return false;
|
|
1657
|
+
}
|
|
1658
|
+
}
|
|
1659
|
+
if (isEntryPoint()) {
|
|
1660
|
+
process.exitCode = await runCli(process.argv.slice(2));
|
|
1661
|
+
}
|
|
1662
|
+
export {
|
|
1663
|
+
EXIT,
|
|
1664
|
+
exitCodeFor,
|
|
1665
|
+
packageVersion,
|
|
1666
|
+
reportError,
|
|
1667
|
+
runCli
|
|
1668
|
+
};
|
|
1669
|
+
//# sourceMappingURL=main.js.map
|