@1sat/cli 0.0.109 → 0.0.110

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 CHANGED
@@ -1428,6 +1428,7 @@ import { AuthFetch } from "@bsv/sdk";
1428
1428
  function createApprovalAuth(wallet) {
1429
1429
  let approved = false;
1430
1430
  let submitted = false;
1431
+ let currentRequest;
1431
1432
  const guarded = new Proxy(wallet, {
1432
1433
  get(target, property) {
1433
1434
  if (property === "createAction")
@@ -1442,7 +1443,10 @@ function createApprovalAuth(wallet) {
1442
1443
  if (submitted)
1443
1444
  throw new Error("Payment already submitted; reconcile wallet activity before retrying");
1444
1445
  submitted = true;
1445
- return target.createAction(...args);
1446
+ const result = await target.createAction(...args);
1447
+ if (currentRequest?.retryCounter === 0)
1448
+ currentRequest.retryCounter = 1;
1449
+ return result;
1446
1450
  };
1447
1451
  if (property === "signAction")
1448
1452
  return async () => {
@@ -1454,18 +1458,27 @@ function createApprovalAuth(wallet) {
1454
1458
  });
1455
1459
  return {
1456
1460
  auth: new AuthFetch(guarded),
1461
+ prepareRequest: (config) => {
1462
+ currentRequest = config;
1463
+ },
1457
1464
  authorizePayment: (satoshis) => {
1458
1465
  approved = satoshis ?? true;
1459
1466
  }
1460
1467
  };
1461
1468
  }
1462
1469
  async function requestWithApproval(url, init, options, dependencies) {
1470
+ const send = () => {
1471
+ const config = {
1472
+ ...init,
1473
+ paymentRetryAttempts: 1,
1474
+ ...init.method === "GET" || init.method === "HEAD" ? {} : { retryCounter: 1 }
1475
+ };
1476
+ dependencies.prepareRequest?.(config);
1477
+ return dependencies.auth.fetch(url, config);
1478
+ };
1463
1479
  let res;
1464
1480
  try {
1465
- res = await dependencies.auth.fetch(url, {
1466
- ...init,
1467
- paymentRetryAttempts: 1
1468
- });
1481
+ res = await send();
1469
1482
  } catch (error) {
1470
1483
  if (error instanceof PaymentRequired) {
1471
1484
  res = new Response(null, {
@@ -1503,7 +1516,7 @@ async function requestWithApproval(url, init, options, dependencies) {
1503
1516
  }
1504
1517
  }
1505
1518
  dependencies.authorizePayment?.(satoshis);
1506
- return dependencies.auth.fetch(url, { ...init, paymentRetryAttempts: 1 });
1519
+ return send();
1507
1520
  }
1508
1521
  var PaymentRequired;
1509
1522
  var init_authfetch_request = __esm(() => {
@@ -1586,7 +1599,7 @@ Unknown or missing method: ${method ?? ""}`);
1586
1599
  chain: opts.chain
1587
1600
  });
1588
1601
  try {
1589
- const { auth, authorizePayment } = createApprovalAuth(walletResult.wallet);
1602
+ const { auth, authorizePayment, prepareRequest } = createApprovalAuth(walletResult.wallet);
1590
1603
  const init = {
1591
1604
  method,
1592
1605
  headers,
@@ -1598,6 +1611,7 @@ Unknown or missing method: ${method ?? ""}`);
1598
1611
  }, {
1599
1612
  auth,
1600
1613
  authorizePayment,
1614
+ prepareRequest,
1601
1615
  plainFetch: fetch,
1602
1616
  confirmPayment: async (message) => {
1603
1617
  const ok = await confirm({ message });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@1sat/cli",
3
- "version": "0.0.109",
3
+ "version": "0.0.110",
4
4
  "description": "CLI for 1Sat Ordinals SDK",
5
5
  "type": "module",
6
6
  "main": "./dist/cli.js",
@@ -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
- return target.createAction(...args)
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
- let res: Response
73
- try {
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 dependencies.auth.fetch(url, { ...init, paymentRetryAttempts: 1 })
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(walletResult.wallet)
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 })