@metalntwx/metal-pay 0.2.0 → 0.3.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.
Files changed (70) hide show
  1. package/README.md +72 -33
  2. package/dist/clearing-abi.d.ts +1836 -0
  3. package/dist/clearing-abi.js +2361 -0
  4. package/dist/clearing-client.d.ts +49 -159
  5. package/dist/clearing-client.js +191 -564
  6. package/dist/clearing-codec.d.ts +57 -0
  7. package/dist/clearing-codec.js +173 -0
  8. package/dist/clearing-custody.d.ts +191 -0
  9. package/dist/clearing-custody.js +490 -0
  10. package/dist/clearing-server.d.ts +30 -49
  11. package/dist/clearing-server.js +87 -67
  12. package/dist/clearing-x402.d.ts +29 -0
  13. package/dist/clearing-x402.js +203 -0
  14. package/dist/client.d.ts +7 -13
  15. package/dist/client.js +16 -18
  16. package/dist/index.d.ts +4 -1
  17. package/dist/index.js +4 -1
  18. package/dist/mandates/binding.d.ts +4 -27
  19. package/dist/mandates/binding.js +5 -9
  20. package/dist/mandates/checkout.js +5 -5
  21. package/dist/mandates/client.js +4 -3
  22. package/dist/mandates/l3a.d.ts +2 -2
  23. package/dist/mandates/l3a.js +5 -4
  24. package/dist/mandates/seller.d.ts +1 -1
  25. package/dist/mandates/seller.js +1 -1
  26. package/dist/mandates/verify.d.ts +4 -6
  27. package/dist/mandates/verify.js +42 -66
  28. package/dist/mandates/vi/credential.js +4 -4
  29. package/dist/node.d.ts +11 -0
  30. package/dist/node.js +97 -0
  31. package/package.json +18 -11
  32. package/src/clearing-abi.ts +2361 -0
  33. package/src/clearing-client.ts +303 -0
  34. package/src/clearing-codec.test.ts +57 -0
  35. package/src/clearing-codec.ts +251 -0
  36. package/src/clearing-custody.test.ts +534 -0
  37. package/src/clearing-custody.ts +665 -0
  38. package/src/clearing-fixture.json +37 -0
  39. package/src/clearing-server.test.ts +388 -0
  40. package/src/clearing-server.ts +123 -0
  41. package/src/clearing-x402.test.ts +181 -0
  42. package/src/clearing-x402.ts +279 -0
  43. package/src/client.test.ts +325 -0
  44. package/src/client.ts +156 -0
  45. package/src/index.ts +33 -0
  46. package/src/mandates/account.ts +91 -0
  47. package/src/mandates/binding.ts +280 -0
  48. package/src/mandates/chain.ts +68 -0
  49. package/src/mandates/checkout.ts +368 -0
  50. package/src/mandates/client.ts +159 -0
  51. package/src/mandates/extension.ts +48 -0
  52. package/src/mandates/index.ts +121 -0
  53. package/src/mandates/ingest.ts +42 -0
  54. package/src/mandates/jcs.ts +16 -0
  55. package/src/mandates/l3a.ts +216 -0
  56. package/src/mandates/seller.ts +91 -0
  57. package/src/mandates/verify.ts +532 -0
  58. package/src/mandates/vi/credential.ts +477 -0
  59. package/src/mandates/vi/issue.ts +118 -0
  60. package/src/mandates/vi/sd-jwt.ts +189 -0
  61. package/src/networks.test.ts +16 -0
  62. package/src/networks.ts +152 -0
  63. package/src/node.test.ts +24 -0
  64. package/src/node.ts +96 -0
  65. package/src/server.test.ts +223 -0
  66. package/src/server.ts +77 -0
  67. package/dist/clearing.d.ts +0 -204
  68. package/dist/clearing.js +0 -151
  69. package/dist/file-storage.d.ts +0 -39
  70. package/dist/file-storage.js +0 -195
package/README.md CHANGED
@@ -2,10 +2,9 @@
2
2
 
3
3
  Agent payments on Metal for the official x402 packages: registers the Metal
4
4
  devnet (`eip155:1337`) and testnet (`eip155:63825`) for the `exact` scheme,
5
- priced in `*USDZ`, for both sides of a payment, and adds Metal's clearing
6
- lane for payers who pay per call without a transaction each time, and
7
- mandates, which let an agent pay from its owner's account within limits
8
- Metal enforces.
5
+ priced in `*USDZ`, for both sides of an on-chain payment, and mandates,
6
+ which let an agent pay from its owner's account within limits Metal
7
+ enforces.
9
8
 
10
9
  ```bash
11
10
  npm install @metalntwx/metal-pay @x402/core viem
@@ -17,10 +16,12 @@ registers the scheme on an `x402Client` you already have. Both refuse,
17
16
  before signing, a Metal `exact` offer in any token but `*USDZ` or under any
18
17
  EIP-712 domain but USDZ's. `createMetalPaymentClient` also refuses any offer
19
18
  above `maxAmount`, a `bigint` in `*USDZ` base units. The default is
20
- `DEFAULT_MAX_PAYMENT`, 1 `*USDZ`. It works with `@x402/*` 2.21 and later.
21
- From 2.23 the stock client pays only tokens in its own table unless its spend
22
- controls list others; `createMetalPaymentClient` lists `*USDZ` there, capped
23
- at `maxAmount`. On a client of your own, add
19
+ `DEFAULT_MAX_PAYMENT`, 1 `*USDZ`. It works with `@x402/core`, `@x402/evm`
20
+ and `@x402/fetch` from 2.28 up to the next major, installed as its peers
21
+ (npm 7+ and pnpm do that themselves). The stock client pays only tokens in
22
+ its own table unless its spend controls list others;
23
+ `createMetalPaymentClient` turns them off on its client, so its own refusals
24
+ name the offer. On a client of your own, add
24
25
  `{ network, asset: USDZ.address }` to `setSpendControls({ allowedAssets })`.
25
26
  A paid request is never redirected to another origin, so no other site
26
27
  receives the payment. `createMandatePaymentClient` does the same.
@@ -46,42 +47,78 @@ registers `ExactEvmScheme` for other networks, add
46
47
  `facilitatorUrl` is Metal's x402 facilitator for it:
47
48
  `https://x402-testnet.metalntwx.com/x402/facilitator` on testnet, and
48
49
  `http://127.0.0.1:7090/x402/facilitator` on devnet, where
49
- `just x402-facilitator` runs it. It settles `exact` payments for any
50
- seller; a seller Metal issued a token to sends it with `sellerToken(token)`,
51
- the `createAuthHeaders` option, and clearing settlement needs one.
50
+ `just x402-facilitator` runs it. It settles payments for any seller, in
51
+ either scheme; a seller Metal issued a token to sends it with
52
+ `sellerToken(token)`, the `createAuthHeaders` option, and `GET
53
+ /settlements` then lists its payments under that label. To also take
54
+ clearing, register `new ClearingServerScheme(network)` on that network
55
+ beside `exact` and list a `bajillion` offer whose `payTo` is the seller's
56
+ clearing key; the offer names the Clearing deployment the facilitator lists
57
+ in `/supported` and binds its invoices to the resource.
52
58
 
53
59
  ```typescript
54
60
  import { HTTPFacilitatorClient, x402ResourceServer } from "@x402/core/server";
55
61
  import { paymentMiddleware } from "@x402/hono";
56
62
  import { METAL_NETWORKS, registerMetalScheme } from "@metalntwx/metal-pay";
57
63
 
58
- const facilitator = new HTTPFacilitatorClient({ url: METAL_NETWORKS.devnet.facilitatorUrl });
64
+ const facilitator = new HTTPFacilitatorClient({
65
+ url: METAL_NETWORKS.devnet.facilitatorUrl,
66
+ });
59
67
  const server = registerMetalScheme(new x402ResourceServer(facilitator));
60
68
  app.use(
61
69
  paymentMiddleware(
62
- { "GET /quote": { accepts: { scheme: "exact", network: "eip155:1337", payTo: "0x…", price: "0.01" } } },
70
+ {
71
+ "GET /quote": {
72
+ accepts: {
73
+ scheme: "exact",
74
+ network: "eip155:1337",
75
+ payTo: "0x…",
76
+ price: "0.01",
77
+ },
78
+ },
79
+ },
63
80
  server,
64
81
  ),
65
82
  );
66
83
  ```
67
84
 
68
- **Clearing.** Sellers register `ClearingDebitServerScheme` next to the `exact`
69
- scheme and list a `clearing-debit` offer in a route's `accepts`; payers pass a
70
- `ClearingDebitScheme` as `createMetalPaymentClient`'s `clearing`, which pays
71
- by clearing, and by `exact` while custody is short or when the seller cannot
72
- take clearing (the facilitator settles clearing only for sellers Metal issued
73
- a token), and fund custody with `depositToClearing`. Given the facilitator, it
74
- waits until the facilitator counts the deposit, so the next payment can draw
75
- on it. On an x402 client of your own, give it the scheme's selector,
76
- `new x402Client(clearing.prefer)`, and its fetch,
77
- `wrapFetchWithPayment(clearing.wrapFetch(fetch), client)`, so a Debit whose
78
- answer never arrived, or arrived as a bare server error, is presented again
79
- rather than paid twice, for as long as the client runs. `withdrawFromClearing` takes custody out
80
- once the epoch holding it has closed and passed its challenge window, and says so
81
- when that has not happened yet. `@metalntwx/metal-pay/file-storage` keeps a
82
- clearing payer's record across restarts (Node only). The facilitator's
83
- [README](https://github.com/mx56db/metal/tree/main/apps/x402-facilitator)
84
- covers the lane.
85
+ ## Native Bajillion payments
86
+
87
+ `createClearingClient` signs cumulative P-256 payments and retains the exact
88
+ pending authorization across transport errors. Supply trusted `serviceUrl`,
89
+ `rpcUrl`, `network`, `settlement`, `asset`, a signer, an atomic store and a
90
+ `maxAmount` limit. In Node 22.13 or later (23.4 or later on Node 23),
91
+ `openClearingWallet(directory)` from `@metalntwx/metal-pay/node` supplies a
92
+ persistent signer and store with exclusive
93
+ process ownership. Preserve that directory across restarts; call `close()` on
94
+ shutdown. Retry an unknown outcome using the same invoice before another payment.
95
+
96
+ `clearingRequirements` and `clearingInvoice` bind an invoice to a resource,
97
+ recipient, amount and deployment. `registerClearingClient` adds the `bajillion`
98
+ scheme to an official x402 client, which then pays by clearing wherever its
99
+ wallet can pay the offer. Behind the stock middleware, Metal's facilitator
100
+ verifies and deduplicates clearing payments before the handler runs. A seller that
101
+ verifies them itself calls `verifyClearingPayment` with its own trusted
102
+ RPC/service configuration and deduplicates `clearingPaymentId` in durable
103
+ storage before delivering. The returned receipt is **preconfirmed**;
104
+ watch `Clearing.finalizedCount` before labeling its epoch finalized. It is not
105
+ an EVM transaction hash.
106
+
107
+ `depositToClearing` funds a clearing account from an EVM account, waiting
108
+ for the contract's deposit window; `clearingSpendable` reads the service's
109
+ estimate of what the account can spend, in token base units, and
110
+ `clearingBalance` the finalized balance and its proof, in clearing units
111
+ (`Clearing.unit()` base units each, a millionth of `*USDZ`);
112
+ `withdrawFromClearing` queues a withdrawal of the finalized balance and,
113
+ once its epoch has finalized, claims it, for payer and seller alike.
114
+ `clearingClose` and `clearingLogProof` are the pieces it is built from.
115
+ `recoverFromClearing` takes an account's funds out of a deployment whose
116
+ service has missed a deadline: it faults the deployment, starts recovery,
117
+ and takes the payout of the account's finalized withdrawal, its last
118
+ finalized balance and its uncounted deposits, with proofs from the service
119
+ or the first of several validators that retained the epoch. The
120
+ [service README](../../apps/clearing-service/README.md) describes
121
+ operator/validator setup, receipt retention and proof endpoints.
85
122
 
86
123
  **Mandates.** `@metalntwx/metal-pay/mandates` lets an agent pay from its
87
124
  owner's account with a P-256 key of its own, within limits Metal enforces on
@@ -98,9 +135,11 @@ ordinary account execution. Factory-created clones share an immutable VI
98
135
  verifier; budgets and replay protection live in each account. `createMandatePaymentClient({ credential, signDigest, network })`
99
136
  is the payer, and `MandateExactScheme` the same payer as a scheme for an
100
137
  `x402Client` of your own: each payment is an ERC-3009 authorization from the
101
- owner's account whose signature is the agent's per-payment mandate, which
102
- the account accepts or refuses through ERC-1271. Sellers and facilitators
103
- see an ordinary `exact` payment. The client sets no limit of its own.
138
+ owner's account whose signature is the agent's per-payment mandate; Metal's
139
+ facilitator submits it as `pull` on the owner's account, which checks it
140
+ against the mandate, spends the budget and hands the token the
141
+ authorization. Sellers see an ordinary `exact` payment. The client sets no
142
+ limit of its own.
104
143
  `readCheck` names the limit behind a refusal, `readMandateState` reads the
105
144
  spend, and `pullCall` builds a direct payment with no seller. A credential's amounts
106
145
  are decimal, to the cent. `verifyMandate` and `verifyMandatePayment` trust the