@1sat/cli 0.0.109 → 0.0.111
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/dist/cli.js +48 -27
- package/package.json +2 -2
- package/src/commands/authfetch-request.ts +24 -6
- package/src/commands/authfetch.ts +4 -1
package/dist/cli.js
CHANGED
|
@@ -1,7 +1,15 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
2
|
+
var __esm = (fn, res, err) => () => {
|
|
3
|
+
if (fn)
|
|
4
|
+
try {
|
|
5
|
+
res = fn(fn = 0);
|
|
6
|
+
} catch (e) {
|
|
7
|
+
err = [e];
|
|
8
|
+
}
|
|
9
|
+
if (err)
|
|
10
|
+
throw err[0];
|
|
11
|
+
return res;
|
|
12
|
+
};
|
|
5
13
|
|
|
6
14
|
// src/args.ts
|
|
7
15
|
function parseGlobalFlags(args) {
|
|
@@ -505,11 +513,11 @@ async function handleActionCommand(args, opts) {
|
|
|
505
513
|
Registered actions:
|
|
506
514
|
`);
|
|
507
515
|
const grouped = new Map;
|
|
508
|
-
for (const
|
|
509
|
-
const cat =
|
|
516
|
+
for (const action of actions) {
|
|
517
|
+
const cat = action.meta.category;
|
|
510
518
|
if (!grouped.has(cat))
|
|
511
519
|
grouped.set(cat, []);
|
|
512
|
-
grouped.get(cat).push(
|
|
520
|
+
grouped.get(cat).push(action);
|
|
513
521
|
}
|
|
514
522
|
for (const [category, categoryActions] of grouped) {
|
|
515
523
|
console.log(` ${category}:`);
|
|
@@ -1428,11 +1436,12 @@ import { AuthFetch } from "@bsv/sdk";
|
|
|
1428
1436
|
function createApprovalAuth(wallet) {
|
|
1429
1437
|
let approved = false;
|
|
1430
1438
|
let submitted = false;
|
|
1439
|
+
let currentRequest;
|
|
1431
1440
|
const guarded = new Proxy(wallet, {
|
|
1432
1441
|
get(target, property) {
|
|
1433
1442
|
if (property === "createAction")
|
|
1434
1443
|
return async (...args) => {
|
|
1435
|
-
const satoshis = args[0].outputs?.reduce((sum,
|
|
1444
|
+
const satoshis = args[0].outputs?.reduce((sum, output) => sum + output.satoshis, 0) ?? 0;
|
|
1436
1445
|
if (!Number.isSafeInteger(satoshis) || satoshis <= 0)
|
|
1437
1446
|
throw new Error("Invalid HTTP payment amount");
|
|
1438
1447
|
if (approved === false)
|
|
@@ -1442,7 +1451,10 @@ function createApprovalAuth(wallet) {
|
|
|
1442
1451
|
if (submitted)
|
|
1443
1452
|
throw new Error("Payment already submitted; reconcile wallet activity before retrying");
|
|
1444
1453
|
submitted = true;
|
|
1445
|
-
|
|
1454
|
+
const result = await target.createAction(...args);
|
|
1455
|
+
if (currentRequest?.retryCounter === 0)
|
|
1456
|
+
currentRequest.retryCounter = 1;
|
|
1457
|
+
return result;
|
|
1446
1458
|
};
|
|
1447
1459
|
if (property === "signAction")
|
|
1448
1460
|
return async () => {
|
|
@@ -1454,18 +1466,27 @@ function createApprovalAuth(wallet) {
|
|
|
1454
1466
|
});
|
|
1455
1467
|
return {
|
|
1456
1468
|
auth: new AuthFetch(guarded),
|
|
1469
|
+
prepareRequest: (config) => {
|
|
1470
|
+
currentRequest = config;
|
|
1471
|
+
},
|
|
1457
1472
|
authorizePayment: (satoshis) => {
|
|
1458
1473
|
approved = satoshis ?? true;
|
|
1459
1474
|
}
|
|
1460
1475
|
};
|
|
1461
1476
|
}
|
|
1462
1477
|
async function requestWithApproval(url, init, options, dependencies) {
|
|
1478
|
+
const send = () => {
|
|
1479
|
+
const config = {
|
|
1480
|
+
...init,
|
|
1481
|
+
paymentRetryAttempts: 1,
|
|
1482
|
+
...init.method === "GET" || init.method === "HEAD" ? {} : { retryCounter: 1 }
|
|
1483
|
+
};
|
|
1484
|
+
dependencies.prepareRequest?.(config);
|
|
1485
|
+
return dependencies.auth.fetch(url, config);
|
|
1486
|
+
};
|
|
1463
1487
|
let res;
|
|
1464
1488
|
try {
|
|
1465
|
-
res = await
|
|
1466
|
-
...init,
|
|
1467
|
-
paymentRetryAttempts: 1
|
|
1468
|
-
});
|
|
1489
|
+
res = await send();
|
|
1469
1490
|
} catch (error) {
|
|
1470
1491
|
if (error instanceof PaymentRequired) {
|
|
1471
1492
|
res = new Response(null, {
|
|
@@ -1503,7 +1524,7 @@ async function requestWithApproval(url, init, options, dependencies) {
|
|
|
1503
1524
|
}
|
|
1504
1525
|
}
|
|
1505
1526
|
dependencies.authorizePayment?.(satoshis);
|
|
1506
|
-
return
|
|
1527
|
+
return send();
|
|
1507
1528
|
}
|
|
1508
1529
|
var PaymentRequired;
|
|
1509
1530
|
var init_authfetch_request = __esm(() => {
|
|
@@ -1586,7 +1607,7 @@ Unknown or missing method: ${method ?? ""}`);
|
|
|
1586
1607
|
chain: opts.chain
|
|
1587
1608
|
});
|
|
1588
1609
|
try {
|
|
1589
|
-
const { auth, authorizePayment } = createApprovalAuth(walletResult.wallet);
|
|
1610
|
+
const { auth, authorizePayment, prepareRequest } = createApprovalAuth(walletResult.wallet);
|
|
1590
1611
|
const init = {
|
|
1591
1612
|
method,
|
|
1592
1613
|
headers,
|
|
@@ -1598,6 +1619,7 @@ Unknown or missing method: ${method ?? ""}`);
|
|
|
1598
1619
|
}, {
|
|
1599
1620
|
auth,
|
|
1600
1621
|
authorizePayment,
|
|
1622
|
+
prepareRequest,
|
|
1601
1623
|
plainFetch: fetch,
|
|
1602
1624
|
confirmPayment: async (message) => {
|
|
1603
1625
|
const ok = await confirm({ message });
|
|
@@ -1943,15 +1965,15 @@ async function handleInitCommand(_args, opts) {
|
|
|
1943
1965
|
}
|
|
1944
1966
|
let wif;
|
|
1945
1967
|
if (keySource === "generate") {
|
|
1946
|
-
const
|
|
1947
|
-
wif =
|
|
1948
|
-
const
|
|
1968
|
+
const pk = PrivateKey2.fromRandom();
|
|
1969
|
+
wif = pk.toWif();
|
|
1970
|
+
const address = pk.toPublicKey().toAddress();
|
|
1949
1971
|
console.log();
|
|
1950
1972
|
console.log(formatWarning(" IMPORTANT: Back up your private key!"));
|
|
1951
1973
|
console.log(formatWarning(" If you lose it, your funds are gone forever."));
|
|
1952
1974
|
console.log();
|
|
1953
1975
|
console.log(` ${formatValue("WIF:")} ${wif}`);
|
|
1954
|
-
console.log(` ${formatValue("Address:")} ${
|
|
1976
|
+
console.log(` ${formatValue("Address:")} ${address}`);
|
|
1955
1977
|
console.log();
|
|
1956
1978
|
const confirmed = await confirm2({
|
|
1957
1979
|
message: "Have you saved your private key somewhere safe?"
|
|
@@ -3665,7 +3687,7 @@ async function startWalletServer(resolved, walletResult, accounts, mode) {
|
|
|
3665
3687
|
ttlSeconds: sessionStoreCfg.ttlSeconds
|
|
3666
3688
|
} : undefined;
|
|
3667
3689
|
if (mode === "wallet") {
|
|
3668
|
-
const
|
|
3690
|
+
const handle = createWalletServer({
|
|
3669
3691
|
wallet: walletResult.wallet,
|
|
3670
3692
|
storage: walletResult.getActiveStorage(),
|
|
3671
3693
|
serverIdentityKey: walletResult.wallet.identityKey,
|
|
@@ -3675,10 +3697,10 @@ async function startWalletServer(resolved, walletResult, accounts, mode) {
|
|
|
3675
3697
|
accounts: accounts?.walletServerAccounts,
|
|
3676
3698
|
sessionStore
|
|
3677
3699
|
});
|
|
3678
|
-
const
|
|
3679
|
-
const
|
|
3680
|
-
console.log(`[wallet] listening on ${resolved.host}:${
|
|
3681
|
-
return { stop: () =>
|
|
3700
|
+
const port = await handle.start();
|
|
3701
|
+
const notes = [resolved.accounts.enabled ? "accounts: on" : ""].filter(Boolean).join(", ");
|
|
3702
|
+
console.log(`[wallet] listening on ${resolved.host}:${port}${notes ? ` (${notes})` : ""}`);
|
|
3703
|
+
return { stop: () => handle.stop() };
|
|
3682
3704
|
}
|
|
3683
3705
|
const serverCfg = config.server ?? {};
|
|
3684
3706
|
const paymailCfg = serverCfg.paymail;
|
|
@@ -4845,8 +4867,8 @@ async function walletListOutputs(args, opts) {
|
|
|
4845
4867
|
${result.totalOutputs} total outputs in basket '${basket}':
|
|
4846
4868
|
`);
|
|
4847
4869
|
for (const out of result.outputs) {
|
|
4848
|
-
const
|
|
4849
|
-
console.log(` ${out.outpoint} | ${out.satoshis} sats | tags: ${
|
|
4870
|
+
const tags = out.tags?.join(", ") || "none";
|
|
4871
|
+
console.log(` ${out.outpoint} | ${out.satoshis} sats | tags: ${tags}`);
|
|
4850
4872
|
}
|
|
4851
4873
|
console.log();
|
|
4852
4874
|
}
|
|
@@ -5104,7 +5126,6 @@ var init_wallet = __esm(() => {
|
|
|
5104
5126
|
});
|
|
5105
5127
|
|
|
5106
5128
|
// src/main.ts
|
|
5107
|
-
var exports_main = {};
|
|
5108
5129
|
import { existsSync as existsSync4 } from "node:fs";
|
|
5109
5130
|
import { resolve } from "node:path";
|
|
5110
5131
|
import { config as loadEnv } from "dotenv";
|
|
@@ -5244,4 +5265,4 @@ var init_main = __esm(() => {
|
|
|
5244
5265
|
|
|
5245
5266
|
// src/cli.ts
|
|
5246
5267
|
process.env.DOTENV_CONFIG_QUIET = "true";
|
|
5247
|
-
await Promise.resolve().then(() => (init_main(),
|
|
5268
|
+
await Promise.resolve().then(() => (init_main(), {}));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@1sat/cli",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.111",
|
|
4
4
|
"description": "CLI for 1Sat Ordinals SDK",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"main": "./dist/cli.js",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
],
|
|
28
28
|
"license": "MIT",
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@1sat/actions": "0.0.
|
|
30
|
+
"@1sat/actions": "0.0.208",
|
|
31
31
|
"@1sat/client": "0.0.51",
|
|
32
32
|
"@1sat/types": "0.0.41",
|
|
33
33
|
"@1sat/wallet-node": "0.0.73",
|
|
@@ -10,6 +10,7 @@ class PaymentRequired extends Error {
|
|
|
10
10
|
export function createApprovalAuth(wallet: WalletInterface) {
|
|
11
11
|
let approved: false | number | true = false
|
|
12
12
|
let submitted = false
|
|
13
|
+
let currentRequest: { retryCounter?: number } | undefined
|
|
13
14
|
const guarded = new Proxy(wallet, {
|
|
14
15
|
get(target, property) {
|
|
15
16
|
if (property === 'createAction')
|
|
@@ -31,7 +32,12 @@ export function createApprovalAuth(wallet: WalletInterface) {
|
|
|
31
32
|
'Payment already submitted; reconcile wallet activity before retrying',
|
|
32
33
|
)
|
|
33
34
|
submitted = true
|
|
34
|
-
|
|
35
|
+
const result = await target.createAction(...args)
|
|
36
|
+
// A verified 402 reached the approved wallet boundary. Permit its
|
|
37
|
+
// one paid send, but not stale-session replay of that send.
|
|
38
|
+
if (currentRequest?.retryCounter === 0)
|
|
39
|
+
currentRequest.retryCounter = 1
|
|
40
|
+
return result
|
|
35
41
|
}
|
|
36
42
|
if (property === 'signAction')
|
|
37
43
|
return async () => {
|
|
@@ -43,6 +49,9 @@ export function createApprovalAuth(wallet: WalletInterface) {
|
|
|
43
49
|
})
|
|
44
50
|
return {
|
|
45
51
|
auth: new AuthFetch(guarded),
|
|
52
|
+
prepareRequest: (config: { retryCounter?: number }) => {
|
|
53
|
+
currentRequest = config
|
|
54
|
+
},
|
|
46
55
|
authorizePayment: (satoshis?: number) => {
|
|
47
56
|
approved = satoshis ?? true
|
|
48
57
|
},
|
|
@@ -67,14 +76,23 @@ export async function requestWithApproval(
|
|
|
67
76
|
plainFetch: typeof fetch
|
|
68
77
|
confirmPayment: (message: string) => Promise<boolean>
|
|
69
78
|
authorizePayment?: (satoshis?: number) => void
|
|
79
|
+
prepareRequest?: (config: { retryCounter?: number }) => void
|
|
70
80
|
},
|
|
71
81
|
): Promise<Response | PaymentApprovalRequired> {
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
res = await dependencies.auth.fetch(url, {
|
|
82
|
+
const send = () => {
|
|
83
|
+
const config = {
|
|
75
84
|
...init,
|
|
76
85
|
paymentRetryAttempts: 1,
|
|
77
|
-
|
|
86
|
+
...(init.method === 'GET' || init.method === 'HEAD'
|
|
87
|
+
? {}
|
|
88
|
+
: { retryCounter: 1 }),
|
|
89
|
+
}
|
|
90
|
+
dependencies.prepareRequest?.(config)
|
|
91
|
+
return dependencies.auth.fetch(url, config)
|
|
92
|
+
}
|
|
93
|
+
let res: Response
|
|
94
|
+
try {
|
|
95
|
+
res = await send()
|
|
78
96
|
} catch (error) {
|
|
79
97
|
if (error instanceof PaymentRequired) {
|
|
80
98
|
res = new Response(null, {
|
|
@@ -130,5 +148,5 @@ export async function requestWithApproval(
|
|
|
130
148
|
}
|
|
131
149
|
}
|
|
132
150
|
dependencies.authorizePayment?.(satoshis)
|
|
133
|
-
return
|
|
151
|
+
return send()
|
|
134
152
|
}
|
|
@@ -107,7 +107,9 @@ export async function handleAuthfetchCommand(
|
|
|
107
107
|
})
|
|
108
108
|
|
|
109
109
|
try {
|
|
110
|
-
const { auth, authorizePayment } = createApprovalAuth(
|
|
110
|
+
const { auth, authorizePayment, prepareRequest } = createApprovalAuth(
|
|
111
|
+
walletResult.wallet,
|
|
112
|
+
)
|
|
111
113
|
const init = {
|
|
112
114
|
method,
|
|
113
115
|
headers,
|
|
@@ -124,6 +126,7 @@ export async function handleAuthfetchCommand(
|
|
|
124
126
|
{
|
|
125
127
|
auth,
|
|
126
128
|
authorizePayment,
|
|
129
|
+
prepareRequest,
|
|
127
130
|
plainFetch: fetch,
|
|
128
131
|
confirmPayment: async (message) => {
|
|
129
132
|
const ok = await confirm({ message })
|