@getciro/cli 0.0.0-stage → 0.1.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/bin/ciro.js +5 -0
- package/package.json +38 -4
- package/src/cli.js +226 -0
- package/README.md +0 -3
package/bin/ciro.js
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,40 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@getciro/cli",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "The ciro CLI: register a name, create an invoice, check a name, pay, and read a receipt. It signs locally with a key file you own.",
|
|
5
|
+
"license": "MIT",
|
|
6
|
+
"author": "Solution Kanu",
|
|
7
|
+
"homepage": "https://github.com/solutiono1/ciro#readme",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/solutiono1/ciro.git",
|
|
11
|
+
"directory": "packages/cli"
|
|
12
|
+
},
|
|
13
|
+
"keywords": [
|
|
14
|
+
"ciro",
|
|
15
|
+
"invoice",
|
|
16
|
+
"stablecoin",
|
|
17
|
+
"cli",
|
|
18
|
+
"tusd8"
|
|
19
|
+
],
|
|
20
|
+
"type": "module",
|
|
21
|
+
"bin": {
|
|
22
|
+
"ciro": "./bin/ciro.js"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"bin",
|
|
26
|
+
"src"
|
|
27
|
+
],
|
|
28
|
+
"publishConfig": {
|
|
29
|
+
"access": "public"
|
|
30
|
+
},
|
|
31
|
+
"engines": {
|
|
32
|
+
"node": ">=22"
|
|
33
|
+
},
|
|
34
|
+
"scripts": {
|
|
35
|
+
"test": "node --test --test-timeout 120000"
|
|
36
|
+
},
|
|
37
|
+
"dependencies": {
|
|
38
|
+
"viem": "^2.38.0"
|
|
39
|
+
}
|
|
40
|
+
}
|
package/src/cli.js
ADDED
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
import { readFileSync, statSync } from "node:fs";
|
|
2
|
+
import { createInterface } from "node:readline";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { createPublicClient, createWalletClient, defineChain, http } from "viem";
|
|
5
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
6
|
+
import { parseHuman } from "../../shared/src/money.js";
|
|
7
|
+
|
|
8
|
+
const USAGE = `ciro register <name>
|
|
9
|
+
ciro invoice --amount 100 --reference "March design" --due 2026-12-01
|
|
10
|
+
ciro check --payee 0x… --name "Ada Okonkwo"
|
|
11
|
+
ciro pay <id> --name "Ada Okonkwo" [--from-allowance]
|
|
12
|
+
ciro receipt <id>
|
|
13
|
+
ciro allow <spender> --cap 500 --expiry 2026-12-31
|
|
14
|
+
ciro revoke <spender>
|
|
15
|
+
|
|
16
|
+
Signing commands print the summary, then require --yes when stdin is not a terminal.
|
|
17
|
+
The key file is mode 0600. The API never sees it.`;
|
|
18
|
+
|
|
19
|
+
export function parseArgs(argv) {
|
|
20
|
+
const positional = [];
|
|
21
|
+
const flags = {};
|
|
22
|
+
let yes = false;
|
|
23
|
+
let fromAllowance = false;
|
|
24
|
+
for (let i = 0; i < argv.length; i++) {
|
|
25
|
+
const arg = argv[i];
|
|
26
|
+
if (arg === "--yes") {
|
|
27
|
+
yes = true;
|
|
28
|
+
continue;
|
|
29
|
+
}
|
|
30
|
+
if (arg === "--from-allowance") {
|
|
31
|
+
fromAllowance = true;
|
|
32
|
+
continue;
|
|
33
|
+
}
|
|
34
|
+
if (arg.startsWith("--")) {
|
|
35
|
+
const value = argv[++i];
|
|
36
|
+
if (value === undefined || value.startsWith("--")) throw new Error(`missing value for ${arg}`);
|
|
37
|
+
flags[arg.slice(2)] = value;
|
|
38
|
+
continue;
|
|
39
|
+
}
|
|
40
|
+
positional.push(arg);
|
|
41
|
+
}
|
|
42
|
+
return { positional, flags, yes, fromAllowance };
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function loadConfig(file) {
|
|
46
|
+
let raw;
|
|
47
|
+
try {
|
|
48
|
+
raw = JSON.parse(readFileSync(file, "utf8"));
|
|
49
|
+
} catch {
|
|
50
|
+
throw new Error(`cannot read config ${file}`);
|
|
51
|
+
}
|
|
52
|
+
if (!raw.apiUrl || !raw.rpcUrl || raw.chainId === undefined) {
|
|
53
|
+
throw new Error("config needs apiUrl, rpcUrl, and chainId");
|
|
54
|
+
}
|
|
55
|
+
return {
|
|
56
|
+
apiUrl: String(raw.apiUrl).replace(/\/$/, ""),
|
|
57
|
+
rpcUrl: String(raw.rpcUrl),
|
|
58
|
+
chainId: Number(raw.chainId),
|
|
59
|
+
keyPath: raw.keyPath ? String(raw.keyPath) : null,
|
|
60
|
+
};
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
function configFile(env) {
|
|
64
|
+
if (env.CIRO_CONFIG) return env.CIRO_CONFIG;
|
|
65
|
+
return join(env.HOME ?? "", ".ciro", "config.json");
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function loadKey(file) {
|
|
69
|
+
if (!file) throw new Error("config keyPath is required to sign");
|
|
70
|
+
const mode = statSync(file).mode & 0o077;
|
|
71
|
+
if (mode !== 0) throw new Error("key file must be mode 0600");
|
|
72
|
+
const text = readFileSync(file, "utf8").trim();
|
|
73
|
+
const hex = text.startsWith("0x") ? text : `0x${text}`;
|
|
74
|
+
if (!/^0x[0-9a-fA-F]{64}$/.test(hex)) throw new Error("key file must contain one hex private key");
|
|
75
|
+
return hex;
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function safe(err) {
|
|
79
|
+
const message = err instanceof Error ? err.message : "error";
|
|
80
|
+
if (/0x[0-9a-fA-F]{64}/.test(message)) return "request failed";
|
|
81
|
+
return message;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
async function callApi(config, method, pathname, body) {
|
|
85
|
+
const response = await fetch(`${config.apiUrl}${pathname}`, {
|
|
86
|
+
method,
|
|
87
|
+
headers: body ? { "content-type": "application/json" } : undefined,
|
|
88
|
+
body: body ? JSON.stringify(body) : undefined,
|
|
89
|
+
signal: AbortSignal.timeout(30_000),
|
|
90
|
+
});
|
|
91
|
+
const json = await response.json();
|
|
92
|
+
if (!response.ok) {
|
|
93
|
+
const result = json.result ? ` ${json.result}` : "";
|
|
94
|
+
throw new Error(`${json.error ?? "request_failed"}${result}`);
|
|
95
|
+
}
|
|
96
|
+
return json;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function ask(stdin, stderr, prompt) {
|
|
100
|
+
return new Promise((resolve) => {
|
|
101
|
+
const rl = createInterface({ input: stdin, output: stderr });
|
|
102
|
+
rl.question(prompt, (answer) => {
|
|
103
|
+
rl.close();
|
|
104
|
+
resolve(answer);
|
|
105
|
+
});
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
async function sendSteps(config, account, tx, yes, stdin, stdout, stderr) {
|
|
110
|
+
const steps = [...(tx.approvals ?? []), tx];
|
|
111
|
+
for (const step of steps) {
|
|
112
|
+
if (step.chainId !== config.chainId) throw new Error("chain_mismatch");
|
|
113
|
+
if (!/^0x[0-9a-fA-F]{40}$/.test(step.to) || !/^0x[0-9a-fA-F]+$/.test(step.data)) {
|
|
114
|
+
throw new Error("bad_transaction");
|
|
115
|
+
}
|
|
116
|
+
stdout.write(`${step.summary}\n`);
|
|
117
|
+
}
|
|
118
|
+
if (!yes) {
|
|
119
|
+
if (!stdin.isTTY) throw new Error("pass --yes to sign without a prompt");
|
|
120
|
+
const answer = await ask(stdin, stderr, "Sign and send? [y/N] ");
|
|
121
|
+
if (answer.trim().toLowerCase() !== "y") throw new Error("not_signed");
|
|
122
|
+
}
|
|
123
|
+
const chain = defineChain({
|
|
124
|
+
id: config.chainId,
|
|
125
|
+
name: config.chainId === 44005 ? "Aevum" : "Ciro",
|
|
126
|
+
nativeCurrency: { name: "tQDAY", symbol: "tQDAY", decimals: 18 },
|
|
127
|
+
rpcUrls: { default: { http: [config.rpcUrl] } },
|
|
128
|
+
});
|
|
129
|
+
const wallet = createWalletClient({ account, chain, transport: http(config.rpcUrl) });
|
|
130
|
+
const reader = createPublicClient({ chain, transport: http(config.rpcUrl) });
|
|
131
|
+
for (const step of steps) {
|
|
132
|
+
const hash = await wallet.sendTransaction({ to: step.to, data: step.data, value: 0n });
|
|
133
|
+
const receipt = await reader.waitForTransactionReceipt({ hash });
|
|
134
|
+
if (receipt.status !== "success") throw new Error("transaction_reverted");
|
|
135
|
+
stdout.write(`${hash}\n`);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
export async function main(argv, options = {}) {
|
|
140
|
+
const env = options.env ?? process.env;
|
|
141
|
+
const stdout = options.stdout ?? process.stdout;
|
|
142
|
+
const stderr = options.stderr ?? process.stderr;
|
|
143
|
+
const stdin = options.stdin ?? process.stdin;
|
|
144
|
+
try {
|
|
145
|
+
if (argv.includes("--help") || argv.includes("-h")) {
|
|
146
|
+
stderr.write(`${USAGE}\n`);
|
|
147
|
+
return 0;
|
|
148
|
+
}
|
|
149
|
+
const parsed = parseArgs(argv);
|
|
150
|
+
const command = parsed.positional[0];
|
|
151
|
+
if (!command || command === "help") {
|
|
152
|
+
stderr.write(`${USAGE}\n`);
|
|
153
|
+
return command === "help" ? 0 : 1;
|
|
154
|
+
}
|
|
155
|
+
const config = loadConfig(configFile(env));
|
|
156
|
+
if (command === "check") {
|
|
157
|
+
const json = await callApi(config, "POST", "/v1/check", {
|
|
158
|
+
payee: parsed.flags.payee,
|
|
159
|
+
name: parsed.flags.name,
|
|
160
|
+
});
|
|
161
|
+
stdout.write(`${json.result}\n`);
|
|
162
|
+
return 0;
|
|
163
|
+
}
|
|
164
|
+
if (command === "receipt") {
|
|
165
|
+
const id = parsed.positional[1];
|
|
166
|
+
if (!id) throw new Error("usage: ciro receipt <id>");
|
|
167
|
+
const json = await callApi(config, "GET", `/v1/receipts/${id}`);
|
|
168
|
+
stdout.write(`Receipt ${json.id}\n`);
|
|
169
|
+
stdout.write(`Payee: ${json.payeeName}\n`);
|
|
170
|
+
stdout.write(`Payer: ${json.payer}\n`);
|
|
171
|
+
stdout.write(`Amount: ${json.amountDisplay} ${json.symbol}\n`);
|
|
172
|
+
stdout.write(`Fee: ${json.feeDisplay}\n`);
|
|
173
|
+
stdout.write(`Reference: ${json.reference}\n`);
|
|
174
|
+
stdout.write(`Network: ${json.chainId}\n`);
|
|
175
|
+
stdout.write(`Paid: ${json.paidAt}\n`);
|
|
176
|
+
stdout.write(`Transaction: ${json.transactionHash}\n`);
|
|
177
|
+
return 0;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
const account = privateKeyToAccount(loadKey(config.keyPath));
|
|
181
|
+
let tx;
|
|
182
|
+
if (command === "register") {
|
|
183
|
+
const name = parsed.positional.slice(1).join(" ");
|
|
184
|
+
if (!name) throw new Error("usage: ciro register <name>");
|
|
185
|
+
tx = await callApi(config, "POST", "/v1/tx/register", { name });
|
|
186
|
+
} else if (command === "invoice") {
|
|
187
|
+
if (!parsed.flags.amount || parsed.flags.reference === undefined || !parsed.flags.due) {
|
|
188
|
+
throw new Error("usage: ciro invoice --amount <tUSD8> --reference <text> --due <YYYY-MM-DD>");
|
|
189
|
+
}
|
|
190
|
+
tx = await callApi(config, "POST", "/v1/tx/invoice", {
|
|
191
|
+
amount: parseHuman(parsed.flags.amount).toString(),
|
|
192
|
+
reference: parsed.flags.reference,
|
|
193
|
+
due: parsed.flags.due,
|
|
194
|
+
});
|
|
195
|
+
} else if (command === "pay") {
|
|
196
|
+
const id = parsed.positional[1];
|
|
197
|
+
if (!id || !parsed.flags.name) throw new Error("usage: ciro pay <id> --name <name>");
|
|
198
|
+
const pathname = parsed.fromAllowance ? "/v1/tx/pay-from-allowance" : "/v1/tx/pay";
|
|
199
|
+
const body = parsed.fromAllowance
|
|
200
|
+
? { id, name: parsed.flags.name, spender: account.address }
|
|
201
|
+
: { id, name: parsed.flags.name, payer: account.address };
|
|
202
|
+
tx = await callApi(config, "POST", pathname, body);
|
|
203
|
+
} else if (command === "allow") {
|
|
204
|
+
const spender = parsed.positional[1];
|
|
205
|
+
if (!spender || !parsed.flags.cap || !parsed.flags.expiry) {
|
|
206
|
+
throw new Error("usage: ciro allow <spender> --cap <tUSD8> --expiry <YYYY-MM-DD>");
|
|
207
|
+
}
|
|
208
|
+
tx = await callApi(config, "POST", "/v1/tx/allow", {
|
|
209
|
+
spender,
|
|
210
|
+
cap: parseHuman(parsed.flags.cap).toString(),
|
|
211
|
+
expiry: parsed.flags.expiry,
|
|
212
|
+
});
|
|
213
|
+
} else if (command === "revoke") {
|
|
214
|
+
const spender = parsed.positional[1];
|
|
215
|
+
if (!spender) throw new Error("usage: ciro revoke <spender>");
|
|
216
|
+
tx = await callApi(config, "POST", "/v1/tx/revoke", { spender });
|
|
217
|
+
} else {
|
|
218
|
+
throw new Error(`unknown command ${command}`);
|
|
219
|
+
}
|
|
220
|
+
await sendSteps(config, account, tx, parsed.yes, stdin, stdout, stderr);
|
|
221
|
+
return 0;
|
|
222
|
+
} catch (err) {
|
|
223
|
+
stderr.write(`${safe(err)}\n`);
|
|
224
|
+
return 1;
|
|
225
|
+
}
|
|
226
|
+
}
|
package/README.md
DELETED