@metalntwx/metal-pay 0.1.1 → 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 (72) hide show
  1. package/README.md +82 -39
  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 +13 -13
  15. package/dist/client.js +45 -18
  16. package/dist/index.d.ts +4 -1
  17. package/dist/index.js +4 -1
  18. package/dist/mandates/account.d.ts +5 -21
  19. package/dist/mandates/account.js +28 -10
  20. package/dist/mandates/binding.d.ts +4 -27
  21. package/dist/mandates/binding.js +5 -9
  22. package/dist/mandates/checkout.js +5 -5
  23. package/dist/mandates/client.js +10 -6
  24. package/dist/mandates/l3a.d.ts +2 -2
  25. package/dist/mandates/l3a.js +5 -4
  26. package/dist/mandates/seller.d.ts +1 -1
  27. package/dist/mandates/seller.js +1 -1
  28. package/dist/mandates/verify.d.ts +4 -6
  29. package/dist/mandates/verify.js +42 -66
  30. package/dist/mandates/vi/credential.js +4 -4
  31. package/dist/node.d.ts +11 -0
  32. package/dist/node.js +97 -0
  33. package/package.json +19 -12
  34. package/src/clearing-abi.ts +2361 -0
  35. package/src/clearing-client.ts +303 -0
  36. package/src/clearing-codec.test.ts +57 -0
  37. package/src/clearing-codec.ts +251 -0
  38. package/src/clearing-custody.test.ts +534 -0
  39. package/src/clearing-custody.ts +665 -0
  40. package/src/clearing-fixture.json +37 -0
  41. package/src/clearing-server.test.ts +388 -0
  42. package/src/clearing-server.ts +123 -0
  43. package/src/clearing-x402.test.ts +181 -0
  44. package/src/clearing-x402.ts +279 -0
  45. package/src/client.test.ts +325 -0
  46. package/src/client.ts +156 -0
  47. package/src/index.ts +33 -0
  48. package/src/mandates/account.ts +91 -0
  49. package/src/mandates/binding.ts +280 -0
  50. package/src/mandates/chain.ts +68 -0
  51. package/src/mandates/checkout.ts +368 -0
  52. package/src/mandates/client.ts +159 -0
  53. package/src/mandates/extension.ts +48 -0
  54. package/src/mandates/index.ts +121 -0
  55. package/src/mandates/ingest.ts +42 -0
  56. package/src/mandates/jcs.ts +16 -0
  57. package/src/mandates/l3a.ts +216 -0
  58. package/src/mandates/seller.ts +91 -0
  59. package/src/mandates/verify.ts +532 -0
  60. package/src/mandates/vi/credential.ts +477 -0
  61. package/src/mandates/vi/issue.ts +118 -0
  62. package/src/mandates/vi/sd-jwt.ts +189 -0
  63. package/src/networks.test.ts +16 -0
  64. package/src/networks.ts +152 -0
  65. package/src/node.test.ts +24 -0
  66. package/src/node.ts +96 -0
  67. package/src/server.test.ts +223 -0
  68. package/src/server.ts +77 -0
  69. package/dist/clearing.d.ts +0 -204
  70. package/dist/clearing.js +0 -151
  71. package/dist/file-storage.d.ts +0 -39
  72. 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,11 +16,15 @@ 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 })`.
26
+ A paid request is never redirected to another origin, so no other site
27
+ receives the payment. `createMandatePaymentClient` does the same.
25
28
 
26
29
  ```typescript
27
30
  import { privateKeyToAccount } from "viem/accounts";
@@ -44,42 +47,78 @@ registers `ExactEvmScheme` for other networks, add
44
47
  `facilitatorUrl` is Metal's x402 facilitator for it:
45
48
  `https://x402-testnet.metalntwx.com/x402/facilitator` on testnet, and
46
49
  `http://127.0.0.1:7090/x402/facilitator` on devnet, where
47
- `just x402-facilitator` runs it. It settles `exact` payments for any
48
- seller; a seller Metal issued a token to sends it with `sellerToken(token)`,
49
- 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.
50
58
 
51
59
  ```typescript
52
60
  import { HTTPFacilitatorClient, x402ResourceServer } from "@x402/core/server";
53
61
  import { paymentMiddleware } from "@x402/hono";
54
62
  import { METAL_NETWORKS, registerMetalScheme } from "@metalntwx/metal-pay";
55
63
 
56
- const facilitator = new HTTPFacilitatorClient({ url: METAL_NETWORKS.devnet.facilitatorUrl });
64
+ const facilitator = new HTTPFacilitatorClient({
65
+ url: METAL_NETWORKS.devnet.facilitatorUrl,
66
+ });
57
67
  const server = registerMetalScheme(new x402ResourceServer(facilitator));
58
68
  app.use(
59
69
  paymentMiddleware(
60
- { "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
+ },
61
80
  server,
62
81
  ),
63
82
  );
64
83
  ```
65
84
 
66
- **Clearing.** Sellers register `ClearingDebitServerScheme` next to the `exact`
67
- scheme and list a `clearing-debit` offer in a route's `accepts`; payers pass a
68
- `ClearingDebitScheme` as `createMetalPaymentClient`'s `clearing`, which pays
69
- by clearing, and by `exact` while custody is short or when the seller cannot
70
- take clearing (the facilitator settles clearing only for sellers Metal issued
71
- a token), and fund custody with `depositToClearing`. Given the facilitator, it
72
- waits until the facilitator counts the deposit, so the next payment can draw
73
- on it. On an x402 client of your own, give it the scheme's selector,
74
- `new x402Client(clearing.prefer)`, and its fetch,
75
- `wrapFetchWithPayment(clearing.wrapFetch(fetch), client)`, so a Debit whose
76
- answer never arrived, or arrived as a bare server error, is presented again
77
- rather than paid twice, for as long as the client runs. `withdrawFromClearing` takes custody out
78
- once the epoch holding it has closed and passed its challenge window, and says so
79
- when that has not happened yet. `@metalntwx/metal-pay/file-storage` keeps a
80
- clearing payer's record across restarts (Node only). The facilitator's
81
- [README](https://github.com/mx56db/metal/tree/main/apps/x402-facilitator)
82
- 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.
83
122
 
84
123
  **Mandates.** `@metalntwx/metal-pay/mandates` lets an agent pay from its
85
124
  owner's account with a P-256 key of its own, within limits Metal enforces on
@@ -88,21 +127,25 @@ budget, how many payments, an expiry) as a Verifiable Intent mandate;
88
127
  `ingestCredential` verifies one against the issuers the owner trusts, and
89
128
  `bindCall`, `replaceCall` and `revokeCall` build the owner's transactions to its own
90
129
  MetalAccount. Wrap those calls with `authorizeMandateCall(client, trustedFactory,
91
- call, signQuorum)` to authorize an account self-call with its existing weighted
92
- quorum. The callback supports the same EOA, P-256 and passkey signatures as
130
+ call, signQuorum)` to authorize an account self-call, or a list of them as one
131
+ batch that runs whole or not at all, with its existing weighted quorum. The
132
+ client must name its chain: the digest the quorum signs is computed from it,
133
+ never read from the RPC endpoint. The callback supports the same EOA, P-256 and passkey signatures as
93
134
  ordinary account execution. Factory-created clones share an immutable VI
94
135
  verifier; budgets and replay protection live in each account. `createMandatePaymentClient({ credential, signDigest, network })`
95
136
  is the payer, and `MandateExactScheme` the same payer as a scheme for an
96
137
  `x402Client` of your own: each payment is an ERC-3009 authorization from the
97
- owner's account whose signature is the agent's per-payment mandate, which
98
- the account accepts or refuses through ERC-1271. Sellers and facilitators
99
- 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.
100
143
  `readCheck` names the limit behind a refusal, `readMandateState` reads the
101
- spend, and `pullCall` builds a direct payment with no seller. Amounts are
102
- whole cents. `verifyMandate` and `verifyMandatePayment` trust the
144
+ spend, and `pullCall` builds a direct payment with no seller. A credential's amounts
145
+ are decimal, to the cent. `verifyMandate` and `verifyMandatePayment` trust the
103
146
  MetalAccountFactory `METAL_NETWORKS` names for the chain, or the
104
- `accountFactory` given, authenticate clone code at the binding and settlement
105
- blocks, and match the successful token transfer to the seller's requirements.
147
+ `accountFactory` given, check the account's clone code against the
148
+ factory's current implementation, and match the successful token transfer to the seller's requirements.
106
149
  The module runs on Node only.
107
150
 
108
151
  **RPC.** Neither payers nor payees need one. Each network's `rpc` is its