@wasit-dev/cli 0.2.0 → 0.4.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/README.md +38 -3
- package/dist/dashboard/App.d.ts +9 -0
- package/dist/dashboard/App.js +39 -0
- package/dist/dashboard/CatalogueView.d.ts +5 -0
- package/dist/dashboard/CatalogueView.js +23 -0
- package/dist/dashboard/Home.d.ts +8 -0
- package/dist/dashboard/Home.js +54 -0
- package/dist/dashboard/RunView.d.ts +10 -0
- package/dist/dashboard/RunView.js +165 -0
- package/dist/dashboard/TargetForm.d.ts +8 -0
- package/dist/dashboard/TargetForm.js +60 -0
- package/dist/dashboard/WalletView.d.ts +5 -0
- package/dist/dashboard/WalletView.js +253 -0
- package/dist/dashboard/banner.d.ts +12 -0
- package/dist/dashboard/banner.js +32 -0
- package/dist/dashboard/env-file.d.ts +20 -0
- package/dist/dashboard/env-file.js +67 -0
- package/dist/dashboard/environment.d.ts +14 -0
- package/dist/dashboard/environment.js +25 -0
- package/dist/dashboard/format.d.ts +6 -0
- package/dist/dashboard/format.js +10 -0
- package/dist/dashboard/runners.d.ts +15 -0
- package/dist/dashboard/runners.js +45 -0
- package/dist/dashboard/theme.d.ts +21 -0
- package/dist/dashboard/theme.js +21 -0
- package/dist/dashboard/useElapsedMs.d.ts +10 -0
- package/dist/dashboard/useElapsedMs.js +21 -0
- package/dist/dashboard/useSpinnerFrame.d.ts +7 -0
- package/dist/dashboard/useSpinnerFrame.js +20 -0
- package/dist/dashboard/wallet-actions.d.ts +41 -0
- package/dist/dashboard/wallet-actions.js +64 -0
- package/dist/index.js +15 -2
- package/dist/version.d.ts +1 -0
- package/dist/version.js +8 -0
- package/dist/wallet-command.d.ts +14 -0
- package/dist/wallet-command.js +311 -0
- package/package.json +17 -4
|
@@ -0,0 +1,253 @@
|
|
|
1
|
+
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
|
|
2
|
+
import { useCallback, useEffect, useRef, useState } from "react";
|
|
3
|
+
import { Box, Text, useApp, useInput } from "ink";
|
|
4
|
+
import SelectInput from "ink-select-input";
|
|
5
|
+
import { THEME } from "./theme.js";
|
|
6
|
+
import { useSpinnerFrame } from "./useSpinnerFrame.js";
|
|
7
|
+
import { writeEnvValue } from "./env-file.js";
|
|
8
|
+
import { ROLE_ENV_VAR, ROLE_LABEL, TESTNET_USDC_ISSUER, WALLET_ROLES, describeError, friendbotText, loadRoleOverview, generateTestnetWallet, generateCommitmentKey, fundWithFriendbot, createUsdcTrustline, sendUsdcFromDistributor, } from "./wallet-actions.js";
|
|
9
|
+
const USDC_DISTRIBUTOR_AMOUNT = "50";
|
|
10
|
+
function formatBalanceLine(balance) {
|
|
11
|
+
const note = balance.issuer === TESTNET_USDC_ISSUER ? " (Circle testnet USDC)" : "";
|
|
12
|
+
return ` ${balance.code.padEnd(10)} ${balance.balance}${note}`;
|
|
13
|
+
}
|
|
14
|
+
function buildItems(overviews) {
|
|
15
|
+
const items = [];
|
|
16
|
+
for (const overview of overviews) {
|
|
17
|
+
const label = ROLE_LABEL[overview.role];
|
|
18
|
+
items.push({ label: `${label} — create new key`, value: `create:${overview.role}` });
|
|
19
|
+
if (overview.role !== "mpp-channel" && overview.configured && overview.publicKey) {
|
|
20
|
+
items.push({ label: `${label} — fund with XLM`, value: `fund-xlm:${overview.role}` });
|
|
21
|
+
items.push({ label: `${label} — fund with USDC`, value: `fund-usdc:${overview.role}` });
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
items.push({ label: "Refresh", value: "refresh" });
|
|
25
|
+
items.push({ label: "Back", value: "back" });
|
|
26
|
+
return items;
|
|
27
|
+
}
|
|
28
|
+
export function WalletView({ onBack }) {
|
|
29
|
+
const { exit } = useApp();
|
|
30
|
+
const [overviews, setOverviews] = useState([]);
|
|
31
|
+
const [stage, setStage] = useState({ name: "loading" });
|
|
32
|
+
const spinnerFrame = useSpinnerFrame(stage.name === "loading" || stage.name === "running");
|
|
33
|
+
// Guards every async completion below. Two things can make a late result
|
|
34
|
+
// wrong: the screen was left (React would be asked to update an unmounted
|
|
35
|
+
// component), or a newer refresh was started and this one's answer is
|
|
36
|
+
// stale. Both are the same question — "is this still the live request?" —
|
|
37
|
+
// so one monotonic id answers both.
|
|
38
|
+
const requestIdRef = useRef(0);
|
|
39
|
+
const mountedRef = useRef(true);
|
|
40
|
+
useEffect(() => {
|
|
41
|
+
mountedRef.current = true;
|
|
42
|
+
return () => {
|
|
43
|
+
mountedRef.current = false;
|
|
44
|
+
};
|
|
45
|
+
}, []);
|
|
46
|
+
const refresh = useCallback(() => {
|
|
47
|
+
const requestId = (requestIdRef.current += 1);
|
|
48
|
+
setStage({ name: "loading" });
|
|
49
|
+
Promise.all(WALLET_ROLES.map((role) => loadRoleOverview(role)))
|
|
50
|
+
.then((loaded) => {
|
|
51
|
+
if (!mountedRef.current || requestId !== requestIdRef.current)
|
|
52
|
+
return;
|
|
53
|
+
setOverviews(loaded);
|
|
54
|
+
setStage({ name: "overview" });
|
|
55
|
+
})
|
|
56
|
+
.catch((error) => {
|
|
57
|
+
// loadRoleOverview resolves even for a broken key, so reaching here
|
|
58
|
+
// means something unforeseen. Showing it beats the previous
|
|
59
|
+
// behavior, where the rejection escaped as an unhandled rejection
|
|
60
|
+
// and killed the process from under Ink's renderer.
|
|
61
|
+
if (!mountedRef.current || requestId !== requestIdRef.current)
|
|
62
|
+
return;
|
|
63
|
+
setStage({
|
|
64
|
+
name: "result",
|
|
65
|
+
ok: false,
|
|
66
|
+
lines: ["Could not load wallet status.", describeError(error)],
|
|
67
|
+
});
|
|
68
|
+
});
|
|
69
|
+
}, []);
|
|
70
|
+
// Loads the three roles' status once on entry; every later refresh (after
|
|
71
|
+
// an action completes) goes through refresh() above instead.
|
|
72
|
+
useEffect(() => {
|
|
73
|
+
refresh();
|
|
74
|
+
}, [refresh]);
|
|
75
|
+
const runTask = (label, task) => {
|
|
76
|
+
const requestId = (requestIdRef.current += 1);
|
|
77
|
+
setStage({ name: "running", label });
|
|
78
|
+
task()
|
|
79
|
+
.then((lines) => {
|
|
80
|
+
if (!mountedRef.current || requestId !== requestIdRef.current)
|
|
81
|
+
return;
|
|
82
|
+
setStage({ name: "result", ok: true, lines });
|
|
83
|
+
})
|
|
84
|
+
.catch((error) => {
|
|
85
|
+
if (!mountedRef.current || requestId !== requestIdRef.current)
|
|
86
|
+
return;
|
|
87
|
+
setStage({ name: "result", ok: false, lines: [describeError(error)] });
|
|
88
|
+
});
|
|
89
|
+
};
|
|
90
|
+
/**
|
|
91
|
+
* Writes the generated lines to .env and reports where they went.
|
|
92
|
+
*
|
|
93
|
+
* This runs inside the keypress handler, so a throw here would escape into
|
|
94
|
+
* Ink's input dispatch with nothing to catch it — a read-only directory or
|
|
95
|
+
* a full disk would take the dashboard down rather than showing a message.
|
|
96
|
+
* On success the absolute path is shown, since .env is resolved against
|
|
97
|
+
* whichever directory `wasit` was started from and the user cannot
|
|
98
|
+
* otherwise tell which file was touched.
|
|
99
|
+
*/
|
|
100
|
+
const saveToEnv = (entries) => {
|
|
101
|
+
try {
|
|
102
|
+
let path = "";
|
|
103
|
+
for (const [key, value] of entries)
|
|
104
|
+
path = writeEnvValue(key, value);
|
|
105
|
+
setStage({
|
|
106
|
+
name: "result",
|
|
107
|
+
ok: true,
|
|
108
|
+
lines: [`Saved to ${path}`, "Press Enter to reload wallet status."],
|
|
109
|
+
});
|
|
110
|
+
}
|
|
111
|
+
catch (error) {
|
|
112
|
+
setStage({
|
|
113
|
+
name: "result",
|
|
114
|
+
ok: false,
|
|
115
|
+
lines: [
|
|
116
|
+
"Could not write .env — this key is not saved anywhere yet.",
|
|
117
|
+
describeError(error),
|
|
118
|
+
"",
|
|
119
|
+
"Copy these into .env yourself before leaving this screen:",
|
|
120
|
+
...entries.map(([key, value]) => `${key}=${value}`),
|
|
121
|
+
],
|
|
122
|
+
});
|
|
123
|
+
}
|
|
124
|
+
};
|
|
125
|
+
const createKey = (role) => {
|
|
126
|
+
if (role === "mpp-channel") {
|
|
127
|
+
const key = generateCommitmentKey();
|
|
128
|
+
const lines = [
|
|
129
|
+
`COMMITMENT_SECRET_HEX=${key.secretHex}`,
|
|
130
|
+
`COMMITMENT_PUBKEY_HEX=${key.publicKeyHex}`,
|
|
131
|
+
];
|
|
132
|
+
setStage({
|
|
133
|
+
name: "confirm-write",
|
|
134
|
+
lines,
|
|
135
|
+
onConfirm: () => saveToEnv([
|
|
136
|
+
["COMMITMENT_SECRET_HEX", key.secretHex],
|
|
137
|
+
["COMMITMENT_PUBKEY_HEX", key.publicKeyHex],
|
|
138
|
+
]),
|
|
139
|
+
});
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const generated = generateTestnetWallet();
|
|
143
|
+
const lines = role === "x402"
|
|
144
|
+
? [`STELLAR_PRIVATE_KEY=${generated.secretKey}`]
|
|
145
|
+
: [`MPP_PAYER_SECRET=${generated.secretKey}`, `MPP_PAYER_PUBLIC=${generated.publicKey}`];
|
|
146
|
+
setStage({
|
|
147
|
+
name: "confirm-write",
|
|
148
|
+
lines,
|
|
149
|
+
onConfirm: () => saveToEnv(role === "x402"
|
|
150
|
+
? [["STELLAR_PRIVATE_KEY", generated.secretKey]]
|
|
151
|
+
: [
|
|
152
|
+
["MPP_PAYER_SECRET", generated.secretKey],
|
|
153
|
+
["MPP_PAYER_PUBLIC", generated.publicKey],
|
|
154
|
+
]),
|
|
155
|
+
});
|
|
156
|
+
};
|
|
157
|
+
const fundXlm = (role, publicKey) => {
|
|
158
|
+
runTask(`Funding ${ROLE_LABEL[role]} with testnet XLM via Friendbot...`, async () => {
|
|
159
|
+
return [friendbotText(await fundWithFriendbot(publicKey))];
|
|
160
|
+
});
|
|
161
|
+
};
|
|
162
|
+
const fundUsdc = (role, publicKey, secret, status) => {
|
|
163
|
+
runTask(`Setting up USDC for ${ROLE_LABEL[role]}...`, async () => {
|
|
164
|
+
const lines = [];
|
|
165
|
+
const xlmBalance = status?.balances.find((balance) => balance.code === "XLM");
|
|
166
|
+
if (!status?.exists || Number(xlmBalance?.balance ?? "0") < 2) {
|
|
167
|
+
lines.push(friendbotText(await fundWithFriendbot(publicKey)));
|
|
168
|
+
}
|
|
169
|
+
await createUsdcTrustline(secret, ROLE_ENV_VAR[role]);
|
|
170
|
+
lines.push("Trustline created.");
|
|
171
|
+
const distributorSecret = process.env.WASIT_USDC_DISTRIBUTOR_SECRET;
|
|
172
|
+
if (!distributorSecret) {
|
|
173
|
+
lines.push("", "No USDC balance yet. There is no scriptable USDC faucet for Stellar —", `visit https://faucet.circle.com (Stellar Testnet, paste ${publicKey}),`, "or set WASIT_USDC_DISTRIBUTOR_SECRET in .env and try again to send automatically.");
|
|
174
|
+
return lines;
|
|
175
|
+
}
|
|
176
|
+
await sendUsdcFromDistributor(distributorSecret, publicKey, USDC_DISTRIBUTOR_AMOUNT);
|
|
177
|
+
lines.push(`Sent ${USDC_DISTRIBUTOR_AMOUNT} USDC from the distributor account.`);
|
|
178
|
+
return lines;
|
|
179
|
+
});
|
|
180
|
+
};
|
|
181
|
+
const handleSelect = (item) => {
|
|
182
|
+
if (item.value === "back") {
|
|
183
|
+
onBack();
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
if (item.value === "refresh") {
|
|
187
|
+
refresh();
|
|
188
|
+
return;
|
|
189
|
+
}
|
|
190
|
+
const separator = item.value.indexOf(":");
|
|
191
|
+
const kind = item.value.slice(0, separator);
|
|
192
|
+
const role = item.value.slice(separator + 1);
|
|
193
|
+
if (kind === "create") {
|
|
194
|
+
createKey(role);
|
|
195
|
+
return;
|
|
196
|
+
}
|
|
197
|
+
const overview = overviews.find((entry) => entry.role === role);
|
|
198
|
+
if (!overview?.publicKey)
|
|
199
|
+
return;
|
|
200
|
+
if (kind === "fund-xlm") {
|
|
201
|
+
fundXlm(role, overview.publicKey);
|
|
202
|
+
return;
|
|
203
|
+
}
|
|
204
|
+
if (kind === "fund-usdc") {
|
|
205
|
+
const secret = process.env[ROLE_ENV_VAR[role]];
|
|
206
|
+
if (!secret) {
|
|
207
|
+
// Only reachable if .env changed under a stale overview; say so
|
|
208
|
+
// rather than swallowing the keypress with no feedback at all.
|
|
209
|
+
setStage({
|
|
210
|
+
name: "result",
|
|
211
|
+
ok: false,
|
|
212
|
+
lines: [`${ROLE_ENV_VAR[role]} is no longer set. Press Enter to refresh.`],
|
|
213
|
+
});
|
|
214
|
+
return;
|
|
215
|
+
}
|
|
216
|
+
fundUsdc(role, overview.publicKey, secret, overview.status);
|
|
217
|
+
}
|
|
218
|
+
};
|
|
219
|
+
useInput((input, key) => {
|
|
220
|
+
if (stage.name === "confirm-write") {
|
|
221
|
+
if (key.return || input.toLowerCase() === "y") {
|
|
222
|
+
stage.onConfirm();
|
|
223
|
+
return;
|
|
224
|
+
}
|
|
225
|
+
if (key.escape || input.toLowerCase() === "n") {
|
|
226
|
+
setStage({ name: "manual-copy", lines: stage.lines });
|
|
227
|
+
}
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
if (stage.name === "loading" || stage.name === "running") {
|
|
231
|
+
if (input === "q")
|
|
232
|
+
exit();
|
|
233
|
+
return;
|
|
234
|
+
}
|
|
235
|
+
if (input === "q") {
|
|
236
|
+
exit();
|
|
237
|
+
return;
|
|
238
|
+
}
|
|
239
|
+
if (key.escape) {
|
|
240
|
+
if (stage.name === "overview") {
|
|
241
|
+
onBack();
|
|
242
|
+
return;
|
|
243
|
+
}
|
|
244
|
+
refresh();
|
|
245
|
+
return;
|
|
246
|
+
}
|
|
247
|
+
if ((stage.name === "manual-copy" || stage.name === "result") && key.return) {
|
|
248
|
+
refresh();
|
|
249
|
+
}
|
|
250
|
+
});
|
|
251
|
+
return (_jsxs(Box, { flexDirection: "column", borderStyle: "round", borderColor: THEME.accent, paddingX: 2, paddingY: 1, children: [_jsx(Text, { bold: true, color: THEME.accent, children: "Testnet wallets" }), stage.name === "loading" && (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { children: [spinnerFrame, " Loading wallet status..."] }) })), stage.name === "overview" && (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [overviews.map((overview) => (_jsxs(Box, { flexDirection: "column", marginBottom: 1, children: [_jsx(Text, { bold: true, children: ROLE_LABEL[overview.role] }), !overview.configured && (_jsxs(Text, { dimColor: true, children: [" ", ROLE_ENV_VAR[overview.role], " not set"] })), overview.configured && overview.publicKey !== undefined && (_jsxs(Text, { dimColor: true, children: [" ", overview.publicKey] })), overview.error !== undefined && (_jsxs(Text, { color: THEME.danger, children: [" Could not check: ", overview.error] })), overview.status !== undefined && !overview.status.exists && (_jsx(Text, { dimColor: true, children: " Not yet created on-chain" })), overview.status?.exists === true &&
|
|
252
|
+
overview.status.balances.map((balance, index) => (_jsx(Text, { dimColor: true, children: formatBalanceLine(balance) }, index)))] }, overview.role))), _jsx(SelectInput, { items: buildItems(overviews), onSelect: handleSelect }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Esc to go back \u00B7 q to quit" }) })] })), stage.name === "confirm-write" && (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [stage.lines.map((line, index) => (_jsx(Text, { children: line }, index))), _jsxs(Box, { marginTop: 1, flexDirection: "column", children: [_jsx(Text, { color: THEME.warning, children: "Save this to .env now? (Y/n)" }), _jsx(Text, { dimColor: true, children: "A secret key is on screen \u2014 do not leave this view up while recording." })] })] })), stage.name === "manual-copy" && (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [_jsx(Text, { children: "Not saved. Paste into .env yourself:" }), stage.lines.map((line, index) => (_jsx(Text, { children: line }, index))), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Enter to continue \u00B7 q to quit" }) })] })), stage.name === "running" && (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: THEME.accent, children: [spinnerFrame, " ", stage.label] }) })), stage.name === "result" && (_jsxs(Box, { flexDirection: "column", marginTop: 1, children: [stage.lines.map((line, index) => (_jsx(Text, { color: stage.ok ? undefined : THEME.danger, children: line }, index))), _jsx(Box, { marginTop: 1, children: _jsx(Text, { dimColor: true, children: "Enter to continue \u00B7 q to quit" }) })] }))] }));
|
|
253
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders `text` as block-letter ASCII art, uncolored — `colors: ["system"]`
|
|
3
|
+
* tells cfonts not to wrap the output in its own ANSI codes, so the caller
|
|
4
|
+
* (Ink) applies color per line instead of fighting embedded escape codes.
|
|
5
|
+
* `spaceless` drops cfonts' own blank padding lines; the caller controls
|
|
6
|
+
* spacing with its own layout instead.
|
|
7
|
+
*
|
|
8
|
+
* Returns the input as a single line, unchanged, if cfonts fails to render
|
|
9
|
+
* it (an unsupported character, for instance) — a missing wordmark is a
|
|
10
|
+
* cosmetic problem the dashboard should never crash over.
|
|
11
|
+
*/
|
|
12
|
+
export declare function renderWordmark(text: string): readonly string[];
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import cfonts from "cfonts";
|
|
2
|
+
// Node's ESM/CJS interop only guarantees the default import (the whole
|
|
3
|
+
// module.exports object) for a CommonJS package — cfonts assigns its
|
|
4
|
+
// exports via `module.exports = exports = {...}`, a pattern the runtime
|
|
5
|
+
// loader does not always statically detect as named exports, even though
|
|
6
|
+
// cfonts' own .d.ts declares one. `import { render } from "cfonts"` threw
|
|
7
|
+
// "does not provide an export named 'render'" at runtime for exactly this
|
|
8
|
+
// reason, despite type-checking fine. The default import always works.
|
|
9
|
+
const cfontsRender = cfonts.render;
|
|
10
|
+
/**
|
|
11
|
+
* Renders `text` as block-letter ASCII art, uncolored — `colors: ["system"]`
|
|
12
|
+
* tells cfonts not to wrap the output in its own ANSI codes, so the caller
|
|
13
|
+
* (Ink) applies color per line instead of fighting embedded escape codes.
|
|
14
|
+
* `spaceless` drops cfonts' own blank padding lines; the caller controls
|
|
15
|
+
* spacing with its own layout instead.
|
|
16
|
+
*
|
|
17
|
+
* Returns the input as a single line, unchanged, if cfonts fails to render
|
|
18
|
+
* it (an unsupported character, for instance) — a missing wordmark is a
|
|
19
|
+
* cosmetic problem the dashboard should never crash over.
|
|
20
|
+
*/
|
|
21
|
+
export function renderWordmark(text) {
|
|
22
|
+
const result = cfontsRender(text, {
|
|
23
|
+
font: "block",
|
|
24
|
+
colors: ["system"],
|
|
25
|
+
background: "transparent",
|
|
26
|
+
spaceless: true,
|
|
27
|
+
});
|
|
28
|
+
if (result === false) {
|
|
29
|
+
return [text];
|
|
30
|
+
}
|
|
31
|
+
return result.array;
|
|
32
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Sets one `KEY=value` line in `.env`, replacing an existing line for that
|
|
3
|
+
* key or appending a new one — every other line, comment, and blank line is
|
|
4
|
+
* left exactly as it was. Also sets `process.env[key]` in the same call, so
|
|
5
|
+
* the running dashboard picks up the change immediately: `.env` is only
|
|
6
|
+
* read once, at process startup, by `dotenv/config`.
|
|
7
|
+
*
|
|
8
|
+
* The write goes to a sibling temp file first and is then renamed into
|
|
9
|
+
* place. A rename is atomic within a filesystem, so an interrupted write can
|
|
10
|
+
* never leave a half-written `.env` — which for this file means losing keys
|
|
11
|
+
* that may be the only copy in existence. The temp file is created at 0600
|
|
12
|
+
* and the rename carries that mode onto the destination, so a `.env` that
|
|
13
|
+
* was previously world-readable is tightened by the next write rather than
|
|
14
|
+
* left as it was found.
|
|
15
|
+
*
|
|
16
|
+
* `path` is resolved to an absolute path and returned, because it defaults
|
|
17
|
+
* to the process's current directory: whoever calls this should be able to
|
|
18
|
+
* tell the user which `.env` was actually written, not just that one was.
|
|
19
|
+
*/
|
|
20
|
+
export declare function writeEnvValue(key: string, value: string, path?: string): string;
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
import { chmodSync, existsSync, readFileSync, realpathSync, renameSync, unlinkSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
/**
|
|
4
|
+
* Permissions for a file that holds secret keys: owner read/write only.
|
|
5
|
+
* Node's default would be 0666 masked by the process umask, which on a
|
|
6
|
+
* typical machine yields a world-readable 0644 — the wrong default for a
|
|
7
|
+
* file whose entire purpose is to hold Stellar secrets.
|
|
8
|
+
*/
|
|
9
|
+
const ENV_FILE_MODE = 0o600;
|
|
10
|
+
/**
|
|
11
|
+
* Sets one `KEY=value` line in `.env`, replacing an existing line for that
|
|
12
|
+
* key or appending a new one — every other line, comment, and blank line is
|
|
13
|
+
* left exactly as it was. Also sets `process.env[key]` in the same call, so
|
|
14
|
+
* the running dashboard picks up the change immediately: `.env` is only
|
|
15
|
+
* read once, at process startup, by `dotenv/config`.
|
|
16
|
+
*
|
|
17
|
+
* The write goes to a sibling temp file first and is then renamed into
|
|
18
|
+
* place. A rename is atomic within a filesystem, so an interrupted write can
|
|
19
|
+
* never leave a half-written `.env` — which for this file means losing keys
|
|
20
|
+
* that may be the only copy in existence. The temp file is created at 0600
|
|
21
|
+
* and the rename carries that mode onto the destination, so a `.env` that
|
|
22
|
+
* was previously world-readable is tightened by the next write rather than
|
|
23
|
+
* left as it was found.
|
|
24
|
+
*
|
|
25
|
+
* `path` is resolved to an absolute path and returned, because it defaults
|
|
26
|
+
* to the process's current directory: whoever calls this should be able to
|
|
27
|
+
* tell the user which `.env` was actually written, not just that one was.
|
|
28
|
+
*/
|
|
29
|
+
export function writeEnvValue(key, value, path = ".env") {
|
|
30
|
+
// Follow a symlink to its target so the link is updated rather than
|
|
31
|
+
// replaced by the rename below.
|
|
32
|
+
const target = existsSync(path) ? realpathSync(resolve(path)) : resolve(path);
|
|
33
|
+
const existing = existsSync(target) ? readFileSync(target, "utf8") : "";
|
|
34
|
+
const lines = existing.length > 0 ? existing.split("\n") : [];
|
|
35
|
+
// A literal prefix rather than a RegExp: the key is interpolated, and a
|
|
36
|
+
// key carrying a regex metacharacter would otherwise match the wrong line
|
|
37
|
+
// (or nothing at all) instead of being compared as the text it is.
|
|
38
|
+
const prefix = `${key}=`;
|
|
39
|
+
let replaced = false;
|
|
40
|
+
const updated = lines.map((line) => {
|
|
41
|
+
if (line.startsWith(prefix)) {
|
|
42
|
+
replaced = true;
|
|
43
|
+
return `${key}=${value}`;
|
|
44
|
+
}
|
|
45
|
+
return line;
|
|
46
|
+
});
|
|
47
|
+
if (!replaced) {
|
|
48
|
+
updated.push(`${key}=${value}`);
|
|
49
|
+
}
|
|
50
|
+
const content = updated.join("\n").replace(/\n*$/, "\n");
|
|
51
|
+
const temp = `${target}.wasit-tmp`;
|
|
52
|
+
try {
|
|
53
|
+
writeFileSync(temp, content, { encoding: "utf8", mode: ENV_FILE_MODE });
|
|
54
|
+
renameSync(temp, target);
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
// Never leave a temp file holding a secret behind on a failed write.
|
|
58
|
+
if (existsSync(temp))
|
|
59
|
+
unlinkSync(temp);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
// Belt and braces: a pre-existing file replaced by rename already carries
|
|
63
|
+
// the temp file's mode, but an exotic filesystem may not honour it.
|
|
64
|
+
chmodSync(target, ENV_FILE_MODE);
|
|
65
|
+
process.env[key] = value;
|
|
66
|
+
return target;
|
|
67
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface EnvironmentCheck {
|
|
2
|
+
readonly label: string;
|
|
3
|
+
readonly ready: boolean;
|
|
4
|
+
}
|
|
5
|
+
/**
|
|
6
|
+
* What Home shows before anything runs, so a missing key surfaces up front
|
|
7
|
+
* on the menu screen instead of after picking an action, typing a target
|
|
8
|
+
* URL, and only then hitting RunView's "could not run this check" screen.
|
|
9
|
+
*
|
|
10
|
+
* All three payer roles are listed, including x402's: the wallet screen can
|
|
11
|
+
* create and fund a key for every one of them, and showing only two of the
|
|
12
|
+
* three made the third look like it was not a role the tool knew about.
|
|
13
|
+
*/
|
|
14
|
+
export declare function getEnvironmentStatus(): readonly EnvironmentCheck[];
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* What Home shows before anything runs, so a missing key surfaces up front
|
|
3
|
+
* on the menu screen instead of after picking an action, typing a target
|
|
4
|
+
* URL, and only then hitting RunView's "could not run this check" screen.
|
|
5
|
+
*
|
|
6
|
+
* All three payer roles are listed, including x402's: the wallet screen can
|
|
7
|
+
* create and fund a key for every one of them, and showing only two of the
|
|
8
|
+
* three made the third look like it was not a role the tool knew about.
|
|
9
|
+
*/
|
|
10
|
+
export function getEnvironmentStatus() {
|
|
11
|
+
return [
|
|
12
|
+
{
|
|
13
|
+
label: "STELLAR_PRIVATE_KEY (x402 payment)",
|
|
14
|
+
ready: Boolean(process.env.STELLAR_PRIVATE_KEY),
|
|
15
|
+
},
|
|
16
|
+
{
|
|
17
|
+
label: "MPP_PAYER_SECRET (MPP charge)",
|
|
18
|
+
ready: Boolean(process.env.MPP_PAYER_SECRET),
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
label: "COMMITMENT_SECRET_HEX (MPP channel)",
|
|
22
|
+
ready: Boolean(process.env.COMMITMENT_SECRET_HEX),
|
|
23
|
+
},
|
|
24
|
+
];
|
|
25
|
+
}
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders a duration for a person glancing at a running check, not for
|
|
3
|
+
* machine parsing — millisecond precision below 1s (where it's the only
|
|
4
|
+
* signal something is happening), one decimal of seconds above it.
|
|
5
|
+
*/
|
|
6
|
+
export declare function formatDuration(ms: number): string;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders a duration for a person glancing at a running check, not for
|
|
3
|
+
* machine parsing — millisecond precision below 1s (where it's the only
|
|
4
|
+
* signal something is happening), one decimal of seconds above it.
|
|
5
|
+
*/
|
|
6
|
+
export function formatDuration(ms) {
|
|
7
|
+
if (ms < 1000)
|
|
8
|
+
return `${Math.round(ms)}ms`;
|
|
9
|
+
return `${(ms / 1000).toFixed(1)}s`;
|
|
10
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridges a dashboard menu selection to the corresponding core suite.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately narrower than the direct CLI subcommands: no destructive
|
|
5
|
+
* MPP-13 (channel close stays skipped, same as the CLI's own default), and
|
|
6
|
+
* the x402 action runs read-only checks only (X402-01..05). Settling a real
|
|
7
|
+
* payment from a menu selection with no flag to review first is not a safe
|
|
8
|
+
* default for an interactive picker — `wasit test --payer-key ...` still
|
|
9
|
+
* covers that directly. MPP-01 (charge mode) has no read-only mode at all,
|
|
10
|
+
* so that action is gated behind an explicit confirmation step in the form
|
|
11
|
+
* screen before this ever runs.
|
|
12
|
+
*/
|
|
13
|
+
import { type CheckResult } from "@wasit-dev/core";
|
|
14
|
+
import type { DashboardAction } from "./App.js";
|
|
15
|
+
export declare function runAction(action: DashboardAction, target: string, onResult: (result: CheckResult) => void): Promise<CheckResult[]>;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Bridges a dashboard menu selection to the corresponding core suite.
|
|
3
|
+
*
|
|
4
|
+
* Deliberately narrower than the direct CLI subcommands: no destructive
|
|
5
|
+
* MPP-13 (channel close stays skipped, same as the CLI's own default), and
|
|
6
|
+
* the x402 action runs read-only checks only (X402-01..05). Settling a real
|
|
7
|
+
* payment from a menu selection with no flag to review first is not a safe
|
|
8
|
+
* default for an interactive picker — `wasit test --payer-key ...` still
|
|
9
|
+
* covers that directly. MPP-01 (charge mode) has no read-only mode at all,
|
|
10
|
+
* so that action is gated behind an explicit confirmation step in the form
|
|
11
|
+
* screen before this ever runs.
|
|
12
|
+
*/
|
|
13
|
+
import { runMppChannelSuite, runMppChargeSuite, runX402ReadChecks, } from "@wasit-dev/core";
|
|
14
|
+
export async function runAction(action, target, onResult) {
|
|
15
|
+
switch (action.kind) {
|
|
16
|
+
case "x402-read":
|
|
17
|
+
return runX402ReadChecks({ target, onResult });
|
|
18
|
+
case "mpp-channel": {
|
|
19
|
+
const commitmentSecretHex = process.env.COMMITMENT_SECRET_HEX;
|
|
20
|
+
if (!commitmentSecretHex) {
|
|
21
|
+
throw new Error("No commitment key. Set COMMITMENT_SECRET_HEX in .env, or run " +
|
|
22
|
+
"`wasit mpp-channel --commitment-key ...` directly.");
|
|
23
|
+
}
|
|
24
|
+
return runMppChannelSuite({
|
|
25
|
+
target,
|
|
26
|
+
commitmentSecretHex,
|
|
27
|
+
network: process.env.MPP_STELLAR_NETWORK ?? "stellar:testnet",
|
|
28
|
+
onResult,
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
case "mpp-charge": {
|
|
32
|
+
const payerSecretKey = process.env.MPP_PAYER_SECRET;
|
|
33
|
+
if (!payerSecretKey) {
|
|
34
|
+
throw new Error("No payer key. Set MPP_PAYER_SECRET in .env, or run " +
|
|
35
|
+
"`wasit mpp-charge --payer-key ...` directly.");
|
|
36
|
+
}
|
|
37
|
+
return runMppChargeSuite({
|
|
38
|
+
target,
|
|
39
|
+
payerSecretKey,
|
|
40
|
+
network: process.env.MPP_STELLAR_NETWORK ?? "stellar:testnet",
|
|
41
|
+
onResult,
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brand palette shared with the marketing site (frontend/app/globals.css),
|
|
3
|
+
* so the dashboard reads as the same product as wasit.dev rather than a
|
|
4
|
+
* generic Ink starter theme. The status colors match the exact traffic-light
|
|
5
|
+
* dots the site already renders in its terminal-window mockup.
|
|
6
|
+
*
|
|
7
|
+
* Ink renders hex colors directly (via chalk); a terminal without truecolor
|
|
8
|
+
* support falls back to its nearest ANSI color automatically.
|
|
9
|
+
*/
|
|
10
|
+
export declare const THEME: {
|
|
11
|
+
/** frontend --accent */
|
|
12
|
+
readonly accent: "#7c3aed";
|
|
13
|
+
/** frontend --muted */
|
|
14
|
+
readonly muted: "#a0a0a0";
|
|
15
|
+
/** frontend .terminal-dot-green */
|
|
16
|
+
readonly success: "#28c840";
|
|
17
|
+
/** frontend .terminal-dot-yellow */
|
|
18
|
+
readonly warning: "#febc2e";
|
|
19
|
+
/** frontend .terminal-dot-red */
|
|
20
|
+
readonly danger: "#ff5f57";
|
|
21
|
+
};
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Brand palette shared with the marketing site (frontend/app/globals.css),
|
|
3
|
+
* so the dashboard reads as the same product as wasit.dev rather than a
|
|
4
|
+
* generic Ink starter theme. The status colors match the exact traffic-light
|
|
5
|
+
* dots the site already renders in its terminal-window mockup.
|
|
6
|
+
*
|
|
7
|
+
* Ink renders hex colors directly (via chalk); a terminal without truecolor
|
|
8
|
+
* support falls back to its nearest ANSI color automatically.
|
|
9
|
+
*/
|
|
10
|
+
export const THEME = {
|
|
11
|
+
/** frontend --accent */
|
|
12
|
+
accent: "#7c3aed",
|
|
13
|
+
/** frontend --muted */
|
|
14
|
+
muted: "#a0a0a0",
|
|
15
|
+
/** frontend .terminal-dot-green */
|
|
16
|
+
success: "#28c840",
|
|
17
|
+
/** frontend .terminal-dot-yellow */
|
|
18
|
+
warning: "#febc2e",
|
|
19
|
+
/** frontend .terminal-dot-red */
|
|
20
|
+
danger: "#ff5f57",
|
|
21
|
+
};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Milliseconds since `startedAt`, re-read on a timer while `active`.
|
|
3
|
+
*
|
|
4
|
+
* The elapsed time has to live in state rather than being computed as
|
|
5
|
+
* `Date.now() - startedAt` during render: a render that reads the clock
|
|
6
|
+
* produces a different result every time it runs, which is exactly what
|
|
7
|
+
* React assumes renders do not do. Once the run finishes the interval stops
|
|
8
|
+
* and the last value is frozen, which is also the correct final total.
|
|
9
|
+
*/
|
|
10
|
+
export declare function useElapsedMs(active: boolean, startedAt: number, intervalMs?: number): number;
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
/**
|
|
3
|
+
* Milliseconds since `startedAt`, re-read on a timer while `active`.
|
|
4
|
+
*
|
|
5
|
+
* The elapsed time has to live in state rather than being computed as
|
|
6
|
+
* `Date.now() - startedAt` during render: a render that reads the clock
|
|
7
|
+
* produces a different result every time it runs, which is exactly what
|
|
8
|
+
* React assumes renders do not do. Once the run finishes the interval stops
|
|
9
|
+
* and the last value is frozen, which is also the correct final total.
|
|
10
|
+
*/
|
|
11
|
+
export function useElapsedMs(active, startedAt, intervalMs = 100) {
|
|
12
|
+
const [elapsedMs, setElapsedMs] = useState(0);
|
|
13
|
+
useEffect(() => {
|
|
14
|
+
if (!active)
|
|
15
|
+
return;
|
|
16
|
+
setElapsedMs(Date.now() - startedAt);
|
|
17
|
+
const timer = setInterval(() => setElapsedMs(Date.now() - startedAt), intervalMs);
|
|
18
|
+
return () => clearInterval(timer);
|
|
19
|
+
}, [active, startedAt, intervalMs]);
|
|
20
|
+
return elapsedMs;
|
|
21
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Advances through the braille spinner frames on an 80ms tick while `active`
|
|
3
|
+
* is true, and stops the interval (no wasted re-renders) once it isn't.
|
|
4
|
+
* Shared by RunView and WalletView so the two don't each keep their own
|
|
5
|
+
* copy of the same interval bookkeeping.
|
|
6
|
+
*/
|
|
7
|
+
export declare function useSpinnerFrame(active: boolean): string;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
const SPINNER_FRAMES = ["⠋", "⠙", "⠹", "⠸", "⠼", "⠴", "⠦", "⠧", "⠇", "⠏"];
|
|
3
|
+
/**
|
|
4
|
+
* Advances through the braille spinner frames on an 80ms tick while `active`
|
|
5
|
+
* is true, and stops the interval (no wasted re-renders) once it isn't.
|
|
6
|
+
* Shared by RunView and WalletView so the two don't each keep their own
|
|
7
|
+
* copy of the same interval bookkeeping.
|
|
8
|
+
*/
|
|
9
|
+
export function useSpinnerFrame(active) {
|
|
10
|
+
const [frameIndex, setFrameIndex] = useState(0);
|
|
11
|
+
useEffect(() => {
|
|
12
|
+
if (!active)
|
|
13
|
+
return;
|
|
14
|
+
const timer = setInterval(() => {
|
|
15
|
+
setFrameIndex((current) => (current + 1) % SPINNER_FRAMES.length);
|
|
16
|
+
}, 80);
|
|
17
|
+
return () => clearInterval(timer);
|
|
18
|
+
}, [active]);
|
|
19
|
+
return SPINNER_FRAMES[frameIndex] ?? SPINNER_FRAMES[0];
|
|
20
|
+
}
|