@dvmkit/dvm-cli 0.1.4 → 0.1.5

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.
Files changed (58) hide show
  1. package/dist/{_esm-M2HUAA3B.js → _esm-MN4BUYV6.js} +56 -54
  2. package/dist/{accounts-S747UW3A.js → accounts-2WZE4N5F.js} +5 -5
  3. package/dist/actions-LR65XILR.js +244 -0
  4. package/dist/{ccip-XZ5F5J6R.js → ccip-JXYJSXTW.js} +3 -3
  5. package/dist/{chains-PINFXYUE.js → chains-LJ7XK6H3.js} +8 -8
  6. package/dist/{chunk-YZUBKHHA.js → chunk-2VVVWBQA.js} +471 -471
  7. package/dist/chunk-3SWX5BQP.js +66 -0
  8. package/dist/chunk-3W2M46WM.js +61142 -0
  9. package/dist/chunk-4JXFXP4Y.js +757 -0
  10. package/dist/{chunk-GIDSTROG.js → chunk-4NWEJTAW.js} +160 -158
  11. package/dist/{chunk-UXQCP26E.js → chunk-65EY7CT4.js} +2 -2
  12. package/dist/{chunk-6UGZAFIR.js → chunk-6CC6JGEU.js} +348 -18
  13. package/dist/{chunk-PHZHPRQE.js → chunk-6O27TEXD.js} +173 -173
  14. package/dist/{chunk-HPRIQEJS.js → chunk-7OK7MBT6.js} +19 -19
  15. package/dist/{chunk-7UPZZGC5.js → chunk-BECPI5H5.js} +193 -193
  16. package/dist/chunk-CJTE5TS3.js +1298 -0
  17. package/dist/{chunk-RWBYCM47.js → chunk-ECEM3ZAS.js} +10 -10
  18. package/dist/{chunk-ACNNU5RY.js → chunk-ECK465AY.js} +694 -565
  19. package/dist/{chunk-XGABM4H5.js → chunk-EGY4KDBW.js} +3822 -2012
  20. package/dist/{chunk-SLDVIXCS.js → chunk-EVQERRCP.js} +12 -12
  21. package/dist/{chunk-4AQ4G7L5.js → chunk-EZHT2RLJ.js} +383 -383
  22. package/dist/{chunk-IC6OXFYW.js → chunk-F55NNH2F.js} +63 -63
  23. package/dist/chunk-G5C6E6PJ.js +9063 -0
  24. package/dist/{chunk-6AJ7H2VX.js → chunk-GZWKCIYN.js} +213 -1281
  25. package/dist/{chunk-ZGXCK43G.js → chunk-LH3QPVN6.js} +8499 -8498
  26. package/dist/{chunk-MMJR6N3J.js → chunk-MLDUKBU3.js} +1368 -1368
  27. package/dist/chunk-N3UJT2NJ.js +3 -0
  28. package/dist/{chunk-BZNZWRBB.js → chunk-OWJZYYMG.js} +11 -15
  29. package/dist/chunk-ZJYO7OLO.js +514 -0
  30. package/dist/{client-D3XFLWOK.js → client-OWIOFBKS.js} +18 -16
  31. package/dist/{dist-7MMV3EZA.js → dist-2G3QBGID.js} +11 -10
  32. package/dist/dvm.js +26781 -4423
  33. package/dist/esm-RQLHAH2K.js +5183 -0
  34. package/dist/internal-QFQ7MJ5V.js +1228 -0
  35. package/dist/payout-reporter-3UB5WRCV-XTVZFIAO.js +17 -0
  36. package/dist/{pocket-refill-GDYSTECR.js → pocket-refill-PQ5ZVSBY.js} +26 -30
  37. package/dist/revenue-reporter-JIKUPXOK-TYUBTF4P.js +11 -0
  38. package/dist/{secp256k1-4YWQEMGF.js → secp256k1-JLZEQ2H7.js} +2 -2
  39. package/dist/tempo-lifecycle-SQL3KLEZ-AATA2SKI.js +28 -0
  40. package/dist/{tempo-wallet-5FEOI4CF.js → tempo-wallet-QOLEIPCV-SWCXRBDS.js} +2 -4
  41. package/dist/{x402-VNFA3DBY.js → x402-FTG2GRAQ-UGAUMUL6.js} +13 -24
  42. package/package.json +2 -10
  43. package/dist/channel-store-GOV7TEDN.js +0 -62
  44. package/dist/chunk-3PPOU55E.js +0 -312
  45. package/dist/chunk-3TBULV3F.js +0 -551
  46. package/dist/chunk-5BUV73FO.js +0 -2351
  47. package/dist/chunk-EY5K4BM3.js +0 -1780
  48. package/dist/chunk-G3RLVJF5.js +0 -19
  49. package/dist/chunk-KC4UPK44.js +0 -113
  50. package/dist/chunk-MTBCEXJ6.js +0 -4602
  51. package/dist/chunk-ONQMRWY3.js +0 -83
  52. package/dist/chunk-TO47XRK7.js +0 -24890
  53. package/dist/chunk-U5TNSUYW.js +0 -1757
  54. package/dist/chunk-UU6QMSTQ.js +0 -171
  55. package/dist/chunk-ZELLQI7F.js +0 -248
  56. package/dist/lifecycle-5RLM5JMN.js +0 -29
  57. package/dist/tempo-lifecycle-DHZPQTSB.js +0 -31
  58. package/dist/wallet-G3VOUCL4.js +0 -30
@@ -0,0 +1,17 @@
1
+ #!/usr/bin/env node
2
+ import { createRequire as __dvmCreateRequire } from "node:module";
3
+ const require = __dvmCreateRequire(import.meta.url);
4
+ import {
5
+ MemoryX402BatchStore,
6
+ PAYOUT_SNAPSHOT_INTERVAL_MS,
7
+ PayoutReporter,
8
+ PostgresX402BatchStore
9
+ } from "./chunk-4JXFXP4Y.js";
10
+ import "./chunk-3SWX5BQP.js";
11
+ import "./chunk-IYHTITNZ.js";
12
+ export {
13
+ MemoryX402BatchStore,
14
+ PAYOUT_SNAPSHOT_INTERVAL_MS,
15
+ PayoutReporter,
16
+ PostgresX402BatchStore
17
+ };
@@ -18,42 +18,38 @@ import {
18
18
  recordFloatSpend,
19
19
  resolveFundMint,
20
20
  sweepPendingMints
21
- } from "./chunk-TO47XRK7.js";
22
- import "./chunk-ACNNU5RY.js";
23
- import "./chunk-GIDSTROG.js";
24
- import "./chunk-RWBYCM47.js";
25
- import "./chunk-YZUBKHHA.js";
26
- import "./chunk-BZNZWRBB.js";
27
- import "./chunk-5BUV73FO.js";
21
+ } from "./chunk-3W2M46WM.js";
28
22
  import {
29
23
  tryResolveBtcUsdRate
30
- } from "./chunk-3PPOU55E.js";
31
- import "./chunk-G3RLVJF5.js";
24
+ } from "./chunk-G5C6E6PJ.js";
25
+ import "./chunk-ZJYO7OLO.js";
26
+ import "./chunk-4JXFXP4Y.js";
27
+ import "./chunk-3SWX5BQP.js";
28
+ import "./chunk-ECK465AY.js";
29
+ import "./chunk-OWJZYYMG.js";
30
+ import "./chunk-ECEM3ZAS.js";
31
+ import "./chunk-4NWEJTAW.js";
32
+ import "./chunk-BECPI5H5.js";
33
+ import "./chunk-N3UJT2NJ.js";
34
+ import "./chunk-2VVVWBQA.js";
35
+ import "./chunk-EZHT2RLJ.js";
32
36
  import {
33
- formatUsd
34
- } from "./chunk-ZELLQI7F.js";
35
- import "./chunk-EY5K4BM3.js";
37
+ formatUsd,
38
+ info
39
+ } from "./chunk-6CC6JGEU.js";
36
40
  import {
37
41
  loadConfig
38
- } from "./chunk-XGABM4H5.js";
39
- import "./chunk-MTBCEXJ6.js";
40
- import "./chunk-ONQMRWY3.js";
41
- import "./chunk-3TBULV3F.js";
42
- import {
43
- info
44
- } from "./chunk-6UGZAFIR.js";
45
- import "./chunk-U5TNSUYW.js";
42
+ } from "./chunk-EGY4KDBW.js";
43
+ import "./chunk-EVQERRCP.js";
46
44
  import "./chunk-RA24J4QM.js";
47
- import "./chunk-6AJ7H2VX.js";
48
- import "./chunk-SLDVIXCS.js";
49
- import "./chunk-7UPZZGC5.js";
50
- import "./chunk-4AQ4G7L5.js";
51
- import "./chunk-ZGXCK43G.js";
52
- import "./chunk-IC6OXFYW.js";
53
- import "./chunk-PHZHPRQE.js";
54
- import "./chunk-UXQCP26E.js";
55
- import "./chunk-MMJR6N3J.js";
56
- import "./chunk-HPRIQEJS.js";
45
+ import "./chunk-CJTE5TS3.js";
46
+ import "./chunk-GZWKCIYN.js";
47
+ import "./chunk-LH3QPVN6.js";
48
+ import "./chunk-6O27TEXD.js";
49
+ import "./chunk-65EY7CT4.js";
50
+ import "./chunk-F55NNH2F.js";
51
+ import "./chunk-MLDUKBU3.js";
52
+ import "./chunk-7OK7MBT6.js";
57
53
  import "./chunk-IYHTITNZ.js";
58
54
 
59
55
  // src/lib/pocket-refill.ts
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env node
2
+ import { createRequire as __dvmCreateRequire } from "node:module";
3
+ const require = __dvmCreateRequire(import.meta.url);
4
+ import {
5
+ RevenueReporter
6
+ } from "./chunk-ZJYO7OLO.js";
7
+ import "./chunk-3SWX5BQP.js";
8
+ import "./chunk-IYHTITNZ.js";
9
+ export {
10
+ RevenueReporter
11
+ };
@@ -7,8 +7,8 @@ import {
7
7
  schnorr,
8
8
  secp256k1,
9
9
  secp256k1_hasher
10
- } from "./chunk-UXQCP26E.js";
11
- import "./chunk-HPRIQEJS.js";
10
+ } from "./chunk-65EY7CT4.js";
11
+ import "./chunk-7OK7MBT6.js";
12
12
  import "./chunk-IYHTITNZ.js";
13
13
  export {
14
14
  encodeToCurve,
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env node
2
+ import { createRequire as __dvmCreateRequire } from "node:module";
3
+ const require = __dvmCreateRequire(import.meta.url);
4
+ import {
5
+ assertTempoWalletIdle
6
+ } from "./chunk-ECK465AY.js";
7
+ import "./chunk-OWJZYYMG.js";
8
+ import "./chunk-ECEM3ZAS.js";
9
+ import "./chunk-4NWEJTAW.js";
10
+ import "./chunk-BECPI5H5.js";
11
+ import "./chunk-N3UJT2NJ.js";
12
+ import "./chunk-2VVVWBQA.js";
13
+ import "./chunk-EZHT2RLJ.js";
14
+ import "./chunk-6CC6JGEU.js";
15
+ import "./chunk-EGY4KDBW.js";
16
+ import "./chunk-EVQERRCP.js";
17
+ import "./chunk-CJTE5TS3.js";
18
+ import "./chunk-GZWKCIYN.js";
19
+ import "./chunk-LH3QPVN6.js";
20
+ import "./chunk-6O27TEXD.js";
21
+ import "./chunk-65EY7CT4.js";
22
+ import "./chunk-F55NNH2F.js";
23
+ import "./chunk-MLDUKBU3.js";
24
+ import "./chunk-7OK7MBT6.js";
25
+ import "./chunk-IYHTITNZ.js";
26
+ export {
27
+ assertTempoWalletIdle
28
+ };
@@ -14,10 +14,8 @@ import {
14
14
  tempoTokenReference,
15
15
  tempoWrongAssetHint,
16
16
  tryResolveTempoAsset
17
- } from "./chunk-BZNZWRBB.js";
18
- import "./chunk-XGABM4H5.js";
19
- import "./chunk-ONQMRWY3.js";
20
- import "./chunk-U5TNSUYW.js";
17
+ } from "./chunk-OWJZYYMG.js";
18
+ import "./chunk-EGY4KDBW.js";
21
19
  import "./chunk-IYHTITNZ.js";
22
20
  export {
23
21
  TEMPO_CHAIN_ID,
@@ -1,30 +1,22 @@
1
1
  #!/usr/bin/env node
2
2
  import { createRequire as __dvmCreateRequire } from "node:module";
3
3
  const require = __dvmCreateRequire(import.meta.url);
4
- import {
5
- resolveX402Asset,
6
- verifyX402Payment,
7
- x402WrongAssetHint
8
- } from "./chunk-KC4UPK44.js";
9
- import {
10
- tryX402Payment
11
- } from "./chunk-3PPOU55E.js";
12
- import "./chunk-G3RLVJF5.js";
13
- import "./chunk-ZELLQI7F.js";
14
4
  import {
15
5
  X402_DEFAULT_FACILITATOR,
16
6
  X402_DEFAULT_NETWORK,
7
+ X402_NETWORKS,
17
8
  X402_V1_VERSION,
18
9
  X402_VERSION,
10
+ assertSupportedX402Network,
19
11
  buildPaymentRequiredV2,
20
12
  buildPaymentRequirements,
21
13
  caip2ToX402Network,
22
14
  chainIdFromCaip2,
23
15
  connectedX402Payer,
24
16
  decodePayment,
25
- decodePaymentRequiredHeader,
17
+ decodePaymentRequiredHeader2 as decodePaymentRequiredHeader,
26
18
  encodePayment,
27
- encodePaymentRequiredHeader,
19
+ encodePaymentRequiredHeader2 as encodePaymentRequiredHeader,
28
20
  encodeSettleResponseHeader,
29
21
  ensureAddress,
30
22
  exactEvmAuthorization,
@@ -34,23 +26,20 @@ import {
34
26
  paymentRequiredV2FromV1,
35
27
  removeX402Wallet,
36
28
  repinX402WalletNetwork,
29
+ resolveX402Asset,
37
30
  saveX402Wallet,
31
+ tryX402Payment,
38
32
  usdcContractByCaip2,
39
- x402NetworkToCaip2
40
- } from "./chunk-EY5K4BM3.js";
41
- import "./chunk-XGABM4H5.js";
42
- import {
43
- X402_NETWORKS,
44
- assertSupportedX402Network,
33
+ verifyX402Payment,
45
34
  x402NetworkByCaip2,
46
35
  x402NetworkLabel,
47
36
  x402NetworkListForCopy,
48
- x402SupportedNetworksHint
49
- } from "./chunk-MTBCEXJ6.js";
50
- import "./chunk-ONQMRWY3.js";
51
- import "./chunk-3TBULV3F.js";
52
- import "./chunk-6UGZAFIR.js";
53
- import "./chunk-U5TNSUYW.js";
37
+ x402NetworkToCaip2,
38
+ x402SupportedNetworksHint,
39
+ x402WrongAssetHint
40
+ } from "./chunk-G5C6E6PJ.js";
41
+ import "./chunk-6CC6JGEU.js";
42
+ import "./chunk-EGY4KDBW.js";
54
43
  import "./chunk-IYHTITNZ.js";
55
44
  export {
56
45
  X402_DEFAULT_FACILITATOR,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dvmkit/dvm-cli",
3
- "version": "0.1.4",
3
+ "version": "0.1.5",
4
4
  "description": "CLI for Digital Vending Machines — discover, pay, and use DVMs from the terminal",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {
@@ -51,20 +51,13 @@
51
51
  },
52
52
  "dependencies": {
53
53
  "@cashu/cashu-ts": "4.2.1",
54
- "@noble/curves": "2.0.1",
55
- "@noble/hashes": "2.0.1",
56
- "@scure/base": "2.0.0",
57
- "@scure/bip32": "2.2.0",
58
- "@scure/bip39": "2.0.1",
59
- "@x402/core": "2.21.0",
54
+ "@dvmkit/sdk": "0.1.0-rc.6",
60
55
  "@x402/evm": "2.21.0",
61
56
  "commander": "14.0.3",
62
- "jose": "6.2.1",
63
57
  "mppx": "0.8.15",
64
58
  "nostr-tools": "2.23.3",
65
59
  "proper-lockfile": "4.1.2",
66
60
  "qrcode": "1.5.4",
67
- "ws": "8.21.0",
68
61
  "zod": "4.4.3"
69
62
  },
70
63
  "optionalDependencies": {
@@ -75,7 +68,6 @@
75
68
  "@types/node": "25.3.4",
76
69
  "@types/proper-lockfile": "4.1.4",
77
70
  "@types/qrcode": "1.5.6",
78
- "@types/ws": "8.18.1",
79
71
  "eslint": "10.9.1",
80
72
  "globals": "17.11.0",
81
73
  "tsup": "8.5.1",
@@ -1,62 +0,0 @@
1
- #!/usr/bin/env node
2
- import { createRequire as __dvmCreateRequire } from "node:module";
3
- const require = __dvmCreateRequire(import.meta.url);
4
- import {
5
- DvmX402ChannelStorage,
6
- X402_PENDING_STALE_MS,
7
- acknowledgeX402ChannelOwner,
8
- clearPendingX402ExactFunding,
9
- clearPendingX402Funding,
10
- findX402Channel,
11
- listPendingX402Fundings,
12
- listPendingX402FundingsForChannel,
13
- listResumableX402ExactFundings,
14
- listResumableX402Fundings,
15
- listX402Channels,
16
- loadResumableX402ExactFunding,
17
- loadResumableX402Funding,
18
- recordPendingX402ExactFunding,
19
- recordPendingX402Funding,
20
- recordX402ChannelMetadata,
21
- retirePendingX402Funding,
22
- withX402ChannelLock,
23
- x402ChannelBalanceUnreconciledAt,
24
- x402ExactFundingDead,
25
- x402PendingBlockExpiresAt,
26
- x402PendingDepositExpiresAt,
27
- x402PendingFundingJson,
28
- x402PendingRetired,
29
- x402PendingStale,
30
- x402PendingUnsettled
31
- } from "./chunk-3TBULV3F.js";
32
- import "./chunk-6UGZAFIR.js";
33
- import "./chunk-U5TNSUYW.js";
34
- import "./chunk-IYHTITNZ.js";
35
- export {
36
- DvmX402ChannelStorage,
37
- X402_PENDING_STALE_MS,
38
- acknowledgeX402ChannelOwner,
39
- clearPendingX402ExactFunding,
40
- clearPendingX402Funding,
41
- findX402Channel,
42
- listPendingX402Fundings,
43
- listPendingX402FundingsForChannel,
44
- listResumableX402ExactFundings,
45
- listResumableX402Fundings,
46
- listX402Channels,
47
- loadResumableX402ExactFunding,
48
- loadResumableX402Funding,
49
- recordPendingX402ExactFunding,
50
- recordPendingX402Funding,
51
- recordX402ChannelMetadata,
52
- retirePendingX402Funding,
53
- withX402ChannelLock,
54
- x402ChannelBalanceUnreconciledAt,
55
- x402ExactFundingDead,
56
- x402PendingBlockExpiresAt,
57
- x402PendingDepositExpiresAt,
58
- x402PendingFundingJson,
59
- x402PendingRetired,
60
- x402PendingStale,
61
- x402PendingUnsettled
62
- };
@@ -1,312 +0,0 @@
1
- #!/usr/bin/env node
2
- import { createRequire as __dvmCreateRequire } from "node:module";
3
- const require = __dvmCreateRequire(import.meta.url);
4
- import {
5
- maxAdvertisedMicro,
6
- withinAdvertisedTolerance
7
- } from "./chunk-G3RLVJF5.js";
8
- import {
9
- costAnchor,
10
- fetchBtcUsdRate,
11
- formatStaleness,
12
- formatUsd,
13
- getLastKnownBtcRate,
14
- msatsToUsdc,
15
- usdcToMsats
16
- } from "./chunk-ZELLQI7F.js";
17
- import {
18
- X402_V1_VERSION,
19
- X402_VERSION,
20
- buildPaymentRequirements,
21
- chainIdFromCaip2,
22
- encodePayment,
23
- ensureAddress,
24
- usdcContractFor,
25
- usdcDomainNameFor,
26
- usdcDomainVersionFor,
27
- x402NetworkToCaip2
28
- } from "./chunk-EY5K4BM3.js";
29
- import {
30
- x402NetworkByCaip2
31
- } from "./chunk-MTBCEXJ6.js";
32
- import {
33
- X402_EXACT_AUTHORIZATION_TTL_SECONDS
34
- } from "./chunk-3TBULV3F.js";
35
- import {
36
- info
37
- } from "./chunk-6UGZAFIR.js";
38
- import {
39
- DvmError
40
- } from "./chunk-U5TNSUYW.js";
41
-
42
- // src/lib/btc-rate.ts
43
- async function resolveBtcUsdRate(purpose) {
44
- try {
45
- return { rate: await fetchBtcUsdRate(), stale: false };
46
- } catch {
47
- const cached = getLastKnownBtcRate();
48
- if (cached) {
49
- warnStaleRate(cached.cachedAt);
50
- return { rate: cached.rate, stale: true, cachedAt: cached.cachedAt };
51
- }
52
- throw rateUnavailable(purpose);
53
- }
54
- }
55
- async function tryResolveBtcUsdRate() {
56
- try {
57
- return await resolveBtcUsdRate("conversion");
58
- } catch {
59
- return null;
60
- }
61
- }
62
- function staleRateNotice(resolved) {
63
- if (!resolved?.stale || resolved.cachedAt === void 0) return void 0;
64
- const age = formatStaleness(resolved.cachedAt);
65
- return {
66
- cached_at: new Date(resolved.cachedAt).toISOString(),
67
- age,
68
- display: `Priced with a BTC/USD rate cached ${age} \u2014 the rate provider is unreachable, so dollar figures may have drifted.`
69
- };
70
- }
71
- var warnedStale = false;
72
- function warnStaleRate(cachedAt) {
73
- if (warnedStale) return;
74
- warnedStale = true;
75
- info(
76
- `Warning: the BTC/USD rate provider is unreachable \u2014 using the last rate this machine saw (cached ${formatStaleness(cachedAt)}). Dollar figures may have drifted.`
77
- );
78
- }
79
- function rateUnavailable(purpose) {
80
- const outcome = purpose === "payment" ? "nothing was paid" : "no conversion was performed";
81
- const noun = purpose === "payment" ? "payment" : "conversion";
82
- const capped = outcome.charAt(0).toUpperCase() + outcome.slice(1);
83
- return new DvmError(
84
- "rate_unavailable",
85
- `Could not price this ${noun}: the BTC/USD rate provider is unreachable and no cached rate is available.`,
86
- `${capped}. The rate provider (CoinGecko) is rate-limiting or down, and this machine has never cached a rate. Retry in a minute; if it keeps failing, check network access to api.coingecko.com.${purpose === "payment" ? " Budgets are denominated in dollars, so a payment cannot be authorised without a rate." : ""}`,
87
- {
88
- display: `BTC/USD rate unavailable \u2014 ${outcome}.`,
89
- anchor: "rate_unavailable",
90
- purpose,
91
- provider: "coingecko",
92
- retryable: true
93
- }
94
- );
95
- }
96
-
97
- // src/lib/x402/client.ts
98
- import { randomBytes } from "crypto";
99
- async function tryX402Payment(request, wallet) {
100
- const selection = selectPaymentRequirement(request);
101
- let networkSlug;
102
- let networkCaip2;
103
- let chainId;
104
- let usdc;
105
- try {
106
- networkCaip2 = x402NetworkToCaip2(selection.network);
107
- const mapped = x402NetworkByCaip2(networkCaip2);
108
- networkSlug = mapped?.slug ?? selection.network;
109
- chainId = chainIdFromCaip2(networkCaip2);
110
- usdc = usdcContractFor(networkSlug);
111
- } catch (err) {
112
- throw new DvmError(
113
- "x402_build_failed",
114
- `Could not resolve x402 network from '${selection.network}': ${err instanceof Error ? err.message : String(err)}`,
115
- "Check that the DVM's advertised x402.network is a CAIP-2 chain id (e.g. 'eip155:8453' for Base)."
116
- );
117
- }
118
- if (!usdc) return null;
119
- const rate = await priceableRate(request.btcRate);
120
- if (rate === null) return null;
121
- const localUsdcMicro = BigInt(msatsToUsdc(request.amountMsats, rate));
122
- const requiredUsdcMicro = resolveRequiredUsdcMicro(selection.amount, localUsdcMicro);
123
- const signedAmountMsats = usdcToMsats(Number(requiredUsdcMicro) / 1e6, rate);
124
- try {
125
- const w = await ensureAddress(wallet);
126
- const validAfter = 0n;
127
- const validBefore = BigInt(
128
- Math.floor(Date.now() / 1e3) + X402_EXACT_AUTHORIZATION_TTL_SECONDS
129
- );
130
- const nonce = request.nonce ?? `0x${randomBytes(32).toString("hex")}`;
131
- const domainName = selectedDomainField(selection.accepted?.extra?.name, "name") ?? usdcDomainNameFor(networkSlug) ?? "USD Coin";
132
- const domainVersion = selectedDomainField(selection.accepted?.extra?.version, "version") ?? usdcDomainVersionFor(networkSlug) ?? "2";
133
- const { signTypedData } = await import("./accounts-S747UW3A.js");
134
- const signature = await signTypedData({
135
- privateKey: w.privateKey,
136
- domain: {
137
- name: domainName,
138
- version: domainVersion,
139
- chainId,
140
- verifyingContract: usdc
141
- },
142
- types: {
143
- TransferWithAuthorization: [
144
- { name: "from", type: "address" },
145
- { name: "to", type: "address" },
146
- { name: "value", type: "uint256" },
147
- { name: "validAfter", type: "uint256" },
148
- { name: "validBefore", type: "uint256" },
149
- { name: "nonce", type: "bytes32" }
150
- ]
151
- },
152
- primaryType: "TransferWithAuthorization",
153
- message: {
154
- from: w.address,
155
- to: selection.payTo,
156
- value: requiredUsdcMicro,
157
- validAfter,
158
- validBefore,
159
- nonce
160
- }
161
- });
162
- const exactPayload = {
163
- signature,
164
- payload: {
165
- authorization: {
166
- from: w.address,
167
- to: selection.payTo,
168
- value: requiredUsdcMicro.toString(),
169
- validAfter: validAfter.toString(),
170
- validBefore: validBefore.toString(),
171
- nonce
172
- }
173
- }
174
- };
175
- const payload = selection.version === X402_V1_VERSION ? {
176
- x402Version: X402_V1_VERSION,
177
- scheme: "exact",
178
- network: networkSlug,
179
- payload: { signature: exactPayload.signature, ...exactPayload.payload }
180
- } : {
181
- x402Version: X402_VERSION,
182
- ...selection.resource ? { resource: selection.resource } : {},
183
- accepted: selection.accepted ?? buildPaymentRequirements({
184
- x402Config: {
185
- payTo: selection.payTo,
186
- network: networkCaip2,
187
- ...selection.asset ? { asset: selection.asset } : {}
188
- },
189
- requiredUsdcMicro,
190
- resource: selection.resource?.url ?? "",
191
- version: X402_VERSION
192
- }),
193
- payload: { signature: exactPayload.signature, ...exactPayload.payload },
194
- extensions: selection.extensions ?? {}
195
- };
196
- return {
197
- paymentHeader: encodePayment(payload),
198
- x402Version: selection.version,
199
- amountUsdcMicro: requiredUsdcMicro.toString(),
200
- amountMsats: signedAmountMsats,
201
- btcRate: rate
202
- };
203
- } catch (err) {
204
- throw new DvmError(
205
- "x402_build_failed",
206
- `Failed to build x402 payment: ${err instanceof Error ? err.message : String(err)}`,
207
- "Check the configured x402 private key and RPC reachability for the target chain."
208
- );
209
- }
210
- }
211
- function isSignableExactRequirement(requirement) {
212
- const transferMethod = requirement.extra?.assetTransferMethod;
213
- return requirement.scheme === "exact" && (transferMethod === void 0 || transferMethod === "eip3009");
214
- }
215
- function selectPaymentRequirement(request) {
216
- const challenge = request.paymentRequired;
217
- if (challenge?.x402Version === X402_VERSION) {
218
- const accepted = challenge.accepts.find(isSignableExactRequirement);
219
- if (!accepted) {
220
- throw invalidChallenge("The DVM's v2 x402 challenge has no exact EIP-3009 requirement.");
221
- }
222
- return {
223
- version: X402_VERSION,
224
- network: accepted.network,
225
- amount: accepted.amount,
226
- payTo: accepted.payTo,
227
- asset: accepted.asset,
228
- accepted,
229
- resource: challenge.resource,
230
- extensions: challenge.extensions
231
- };
232
- }
233
- if (challenge?.x402Version === X402_V1_VERSION) {
234
- const accepted = challenge.accepts.find((candidate) => candidate.scheme === "exact");
235
- if (!accepted) throw invalidChallenge("The DVM's v1 x402 challenge has no exact requirement.");
236
- return {
237
- version: X402_V1_VERSION,
238
- network: accepted.network,
239
- amount: accepted.maxAmountRequired,
240
- payTo: accepted.payTo,
241
- asset: accepted.asset
242
- };
243
- }
244
- return {
245
- version: request.x402Version ?? X402_V1_VERSION,
246
- network: request.network,
247
- amount: request.requiredUsdcMicro,
248
- payTo: request.payTo,
249
- asset: request.asset,
250
- resource: request.resource
251
- };
252
- }
253
- function invalidChallenge(message) {
254
- return new DvmError(
255
- "x402_build_failed",
256
- message,
257
- "Ask the DVM operator to advertise an exact x402 payment requirement."
258
- );
259
- }
260
- function selectedDomainField(value, field) {
261
- if (value === void 0 || value === null) return void 0;
262
- if (typeof value === "string" && value.length > 0) return value;
263
- throw invalidChallenge(`The DVM advertised an invalid x402 ${field}.`);
264
- }
265
- function resolveRequiredUsdcMicro(advertised, localUsdcMicro) {
266
- if (advertised === void 0) return localUsdcMicro;
267
- if (!/^[1-9]\d*$/.test(advertised)) {
268
- throw new DvmError(
269
- "x402_amount_invalid",
270
- `The DVM advertised an invalid x402 maxAmountRequired: '${advertised}'.`,
271
- "Do not sign this payment. Ask the DVM operator to return a positive integer number of USDC micro-units."
272
- );
273
- }
274
- const required = BigInt(advertised);
275
- if (withinAdvertisedTolerance(required, localUsdcMicro)) return required;
276
- const advertisedUsd = Number(required) / 1e6;
277
- const localUsd = Number(localUsdcMicro) / 1e6;
278
- throw new DvmError(
279
- "x402_amount_exceeds_limit",
280
- `The DVM's x402 charge ($${formatUsd(advertisedUsd)} USDC) is more than 10% above the caller's local price ($${formatUsd(localUsd)} USDC).`,
281
- "The payment was not signed. Refresh the quote or ask the DVM operator to check its BTC/USD rate.",
282
- {
283
- advertised_usdc_microunits: required.toString(),
284
- local_usdc_microunits: localUsdcMicro.toString(),
285
- max_usdc_microunits: maxAdvertisedMicro(localUsdcMicro).toString(),
286
- anchor: costAnchor(advertisedUsd),
287
- display: `Payment refused: $${formatUsd(advertisedUsd)} USDC exceeds the 10% safety margin over the caller's $${formatUsd(localUsd)} USDC estimate.`
288
- }
289
- );
290
- }
291
- async function priceableRate(btcRate) {
292
- if (btcRate && btcRate > 0) return btcRate;
293
- try {
294
- return (await resolveBtcUsdRate("payment")).rate;
295
- } catch (err) {
296
- if (err instanceof DvmError && err.code === "rate_unavailable") {
297
- info(
298
- "Skipping the x402 rail: it prices in USDC and the BTC/USD rate is unavailable (no cached rate on this machine). Trying the remaining rails."
299
- );
300
- return null;
301
- }
302
- throw err;
303
- }
304
- }
305
-
306
- export {
307
- resolveBtcUsdRate,
308
- tryResolveBtcUsdRate,
309
- staleRateNotice,
310
- tryX402Payment,
311
- isSignableExactRequirement
312
- };