@parallel-protocol/x402 0.8.1 → 0.8.3
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/README.md +29 -19
- package/dist/{chunk-GXPUE3ES.js → chunk-G7HSURPM.js} +21 -7
- package/dist/{chunk-SNW5Z5HB.cjs → chunk-TH77IB3T.cjs} +20 -6
- package/dist/express.cjs +5 -5
- package/dist/express.js +1 -1
- package/dist/fastify.cjs +11 -11
- package/dist/fastify.js +1 -1
- package/dist/hono.cjs +6 -6
- package/dist/hono.js +1 -1
- package/dist/index.cjs +15 -15
- package/dist/index.js +1 -1
- package/dist/next.cjs +13 -13
- package/dist/next.js +1 -1
- package/package.json +2 -2
package/README.md
CHANGED
|
@@ -40,7 +40,7 @@ Agent Merchant (this SDK) Facilitator
|
|
|
40
40
|
- The handler only runs after the signature is verified.
|
|
41
41
|
- The on-chain settlement happens after the handler produces a 2xx response — the agent is not charged on errors.
|
|
42
42
|
- The settle is bound to the merchant that verified: `POST /x402/verify` hands the middleware a settle token that `POST /x402/settle` must present. The token stays in memory for the life of the request and never reaches the agent.
|
|
43
|
-
- If settlement fails, the middleware discards the handler's response and returns `402` with `{ error: "PAYMENT_SETTLEMENT_FAILED", reason, message }`: `reason` is the facilitator's code (`INSUFFICIENT_BALANCE`, `AUTHORIZATION_USED`, `PAYMENT_EXPIRED`, ...) or this SDK's (`SETTLE_TIMEOUT`, `FACILITATOR_UNAVAILABLE`, `FACILITATOR_INVALID_RESPONSE`, `AMOUNT_MISMATCH`, `UNKNOWN`), `message` the facilitator's text or this SDK's, and the facilitator's `Retry-After` is copied when it sent one. A settle that timed out or could not reach the facilitator is retried once first
|
|
43
|
+
- If settlement fails, the middleware discards the handler's response and returns `402` with `{ error: "PAYMENT_SETTLEMENT_FAILED", reason, message }`: `reason` is the facilitator's code (`INSUFFICIENT_BALANCE`, `AUTHORIZATION_USED`, `PAYMENT_EXPIRED`, ...) or this SDK's (`SETTLE_TIMEOUT`, `FACILITATOR_UNAVAILABLE`, `FACILITATOR_INVALID_RESPONSE`, `AMOUNT_MISMATCH`, `UNKNOWN`), `message` the facilitator's text or this SDK's, and the facilitator's `Retry-After` is copied when it sent one. A settle that timed out or could not reach the facilitator is retried once first, with the same settle token: once the first attempt has finished, the facilitator answers its recorded outcome, and any other answer to the retry tells the agent to check with the merchant.
|
|
44
44
|
- A metered settle must settle the amount the handler asked for: a confirmation for another amount is refused and the response discarded.
|
|
45
45
|
- Handlers are bounded by a 30-second timeout on Express, Hono and Next.js — a slow handler returns `504` and the agent is not charged. (Fastify manages handler execution itself, so no timeout is applied there.) Metered routes get their announced window minus two minutes kept for the settlement and its retry.
|
|
46
46
|
- On paid Fastify routes, writing to `reply.raw` (the SSE/streaming pattern) fails loudly instead of sending: a raw response skips the settlement hooks and would go out unpaid. Streaming is not supported on paid routes.
|
|
@@ -222,7 +222,7 @@ For the four natively supported networks, `acceptedTokens` defaults to `[USDp, U
|
|
|
222
222
|
| `avalanche` | `0x9eE1963f05553eF838604Dd39403be21ceF26AA4` | `0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E` | `0x9d92c21205383651610f90722131655a5b8ed3e0` |
|
|
223
223
|
| `hyperevm` | `0xBE65F0F410A72BeC163dC65d46c83699e957D588` | `0xb88339cb7199b77e23db6e890353e22632ba630f` | `0x9B3a8f7CEC208e247d97dEE13313690977e24459` |
|
|
224
224
|
|
|
225
|
-
|
|
225
|
+
A route on any other chain of the Parallel catalog, such as Sonic, raises `X402ConfigError` at construction.
|
|
226
226
|
|
|
227
227
|
You can also import the token list directly:
|
|
228
228
|
|
|
@@ -255,7 +255,7 @@ routes: {
|
|
|
255
255
|
}
|
|
256
256
|
```
|
|
257
257
|
|
|
258
|
-
**Accepting Parallelizer collateral payments:** Each supported network exposes additional collateral tokens whitelisted in the Parallelizer contract. They are available in `PARALLEL_TOKENS` but excluded from the defaults. Add them to `acceptedTokens` to
|
|
258
|
+
**Accepting Parallelizer collateral payments:** Each supported network exposes additional collateral tokens whitelisted in the Parallelizer contract. They are available in `PARALLEL_TOKENS` but excluded from the defaults. Add them to `acceptedTokens` to receive them: the agent pays in USDp or sUSDp, and the Parallelizer swaps to the collateral in the same settlement (routes D, K and L):
|
|
259
259
|
|
|
260
260
|
| Network | Token | Key | Yield-bearing |
|
|
261
261
|
|---------|-------|-----|---------------|
|
|
@@ -347,6 +347,10 @@ const header = Buffer.from(JSON.stringify(signedPayload)).toString("base64");
|
|
|
347
347
|
|
|
348
348
|
See [`@parallel-protocol/payment-core`](../payment-core) for EIP-3009 signing helpers.
|
|
349
349
|
|
|
350
|
+
### Request header: `x402-client`
|
|
351
|
+
|
|
352
|
+
A payer on `@parallel-protocol/x402-fetch` 0.5.0 or later also sends an `x402-client` header with the x402-fetch and CLI versions it runs, for example `x402-client: x402-fetch/0.5.0 cli/0.5.1`, and nothing else about the agent. Earlier releases and other clients send none. The middleware does not read it. Your handler can read it to spot a payer on outdated versions and tell it to update.
|
|
353
|
+
|
|
350
354
|
### 200 response: `payment-response`
|
|
351
355
|
|
|
352
356
|
On success, the middleware sets a `payment-response` header with a base64-encoded settlement confirmation:
|
|
@@ -545,9 +549,9 @@ The facilitator supports 13 settlement routes. The route is selected automatical
|
|
|
545
549
|
| **B** | `swapExactOutputWithAuthorization` | USDp | USDC (exact) | ~120K | Parallelizer swaps agent's USDp → exact USDC for merchant |
|
|
546
550
|
| **C** | `depositWithAuthorization` | USDp | sUSDp | ~110K | Agent's USDp deposited into the savings vault, the merchant receives sUSDp shares |
|
|
547
551
|
| **D** | `swapExactOutputWithAuthorization` | USDp | non-USDC backing collateral (exact) | ~120K | Same as B but for other Parallelizer collaterals |
|
|
548
|
-
| **E** | `swapExactInputWithAuthorization` | USDC
|
|
549
|
-
| **F** | `transferWithAuthorization` | USDC
|
|
550
|
-
| **G** | `swapExactInputWithAuthorization` | USDC
|
|
552
|
+
| **E** | `swapExactInputWithAuthorization` | USDC (exact), on Base and Avalanche | USDp (min) | ~120K | Agent pays an exact amount, the Parallelizer delivers at least the minimum USDp to the merchant |
|
|
553
|
+
| **F** | `transferWithAuthorization` | USDC | USDC | ~65K | Direct USDC transfer |
|
|
554
|
+
| **G** | `swapExactInputWithAuthorization` | USDC (exact), on Base and Avalanche | sUSDp | ~200K | The USDC is swapped to USDp, then the USDp is deposited as sUSDp for the merchant (atomic multicall3) |
|
|
551
555
|
| **H** | `redeemWithAuthorization` | sUSDp shares | USDp | ~130K | Agent burns sUSDp shares; merchant receives USDp assets |
|
|
552
556
|
| **I** | `redeemWithAuthorization` + `swapExactOutputWithAuthorization` | sUSDp | USDC (exact) | ~180K | Agent redeems sUSDp → USDp, then swaps USDp → exact USDC for merchant (atomic multicall3) |
|
|
553
557
|
| **J** | `transferWithAuthorization` | sUSDp | sUSDp | ~65K | Direct sUSDp share transfer |
|
|
@@ -557,9 +561,10 @@ The facilitator supports 13 settlement routes. The route is selected automatical
|
|
|
557
561
|
|
|
558
562
|
**Chain requirements:**
|
|
559
563
|
- Routes B, D, E, G, I, K, L require the **Parallelizer** to be deployed on the target chain (Ethereum, Base, Avalanche, HyperEVM).
|
|
560
|
-
- Routes B and I swap to USDC, so they need USDC among the chain's collaterals, which is the case on Base and Avalanche.
|
|
564
|
+
- Routes B and I swap to USDC, and routes E and G swap from it, so they need USDC among the chain's collaterals, which is the case on Base and Avalanche.
|
|
561
565
|
- Routes C, G, H, I, J, K, L require **sUSDp** (savings module) to be deployed on the target chain.
|
|
562
|
-
-
|
|
566
|
+
- Agents pay with USDp, sUSDp or the chain's USDC. Route F takes the chain's USDC, routes E and G the chain's USDC where it is a collateral (Base and Avalanche). On these three routes any other `tokenIn` is refused at verify and at settle with `PAYMENT_METHOD_NOT_SUPPORTED`, before the signature is checked.
|
|
567
|
+
- The facilitator settles on Ethereum, Base, Avalanche and HyperEVM. A payment on another chain is refused at verify and at settle with `NETWORK_MISMATCH`.
|
|
563
568
|
- Route U needs the escrow contract, deployed on Base only. A metered payment on another chain is refused at verify with `PAYMENT_METHOD_NOT_SUPPORTED`.
|
|
564
569
|
- Gas sponsorship is decided by the facilitator per payment and reported as `gasSponsored` in the settlement receipt.
|
|
565
570
|
- The facilitator's `GET /capabilities?chain=<slug>` lists the routes available on a chain right now, with the gas estimates above and the burn fees.
|
|
@@ -755,7 +760,7 @@ if (result.success) {
|
|
|
755
760
|
}
|
|
756
761
|
```
|
|
757
762
|
|
|
758
|
-
The constructor throws `X402ConfigError` when the URL is neither https nor plain http on `localhost`, `127.0.0.1` or `[::1]`, and the client keeps the address it checked. No call follows a redirect. `verify` uses a 10-second timeout. `settle` uses a 60-second timeout to accommodate on-chain confirmation latency. `verify` throws on any failure, with the facilitator's message in `.detail`, its HTTP status in `.status` and its `Retry-After` in `.retryAfter`. A facilitator that issues no settle token is an invalid response. `settle` returns a `FacilitatorResponse` union on facilitator errors, with the facilitator's status and `Retry-After` on `error`, and throws `X402RuntimeError` on connectivity issues (`SETTLE_TIMEOUT` when the 60 seconds ran out, the payment may still settle), an undecodable payment header, or a malformed facilitator response. A settle
|
|
763
|
+
The constructor throws `X402ConfigError` when the URL is neither https nor plain http on `localhost`, `127.0.0.1` or `[::1]`, and the client keeps the address it checked. No call follows a redirect. `verify` uses a 10-second timeout. `settle` uses a 60-second timeout to accommodate on-chain confirmation latency. `verify` throws on any failure, with the facilitator's message in `.detail`, its HTTP status in `.status` and its `Retry-After` in `.retryAfter`. A facilitator that issues no settle token is an invalid response. `settle` returns a `FacilitatorResponse` union on facilitator errors, with the facilitator's status and `Retry-After` on `error`, and throws `X402RuntimeError` on connectivity issues (`SETTLE_TIMEOUT` when the 60 seconds ran out, the payment may still settle), an undecodable payment header, or a malformed facilitator response. A settle answered with an error that carries no `error` field throws `FACILITATOR_UNAVAILABLE` with that status: the answer comes from something in front of the facilitator, and the payment may still settle. The settle token is a secret for the life of the request: keep it in memory, never log it, never send it to the payer.
|
|
759
764
|
|
|
760
765
|
For a metered payment, the requirements also carry `scheme: "upto"` and `maxTimeoutSeconds`, and `settle` takes the amount to charge, in the token's smallest unit, next to the settle token: `{ settleToken, amount }`.
|
|
761
766
|
|
|
@@ -779,7 +784,7 @@ What the middleware answers on a paid route instead of the handler's response:
|
|
|
779
784
|
|
|
780
785
|
| Error class | When |
|
|
781
786
|
|-------------|------|
|
|
782
|
-
| `X402ConfigError` | Invalid config at construction, the message naming the first field at fault: a bad `payTo`, a facilitator URL that is neither https nor plain http on `localhost`, `127.0.0.1` or `[::1]`, empty `routes`, a malformed or non-positive `price`, a `meter` on a route that is not `upto`, or an `upto` route with `maxTimeoutSeconds` above 3600 or a token other than USDp. Then, on a request to a route: an unknown network with no `acceptedTokens`, an accepted token whose decimals are neither in the catalog nor set via `decimals`, a price finer than the token's precision, or a price function that throws or returns something that is not a price |
|
|
787
|
+
| `X402ConfigError` | Invalid config at construction, the message naming the first field at fault: a bad `payTo`, a facilitator URL that is neither https nor plain http on `localhost`, `127.0.0.1` or `[::1]`, empty `routes`, a `network` of the catalog other than `ethereum`, `base`, `avalanche` or `hyperevm`, a malformed or non-positive `price`, a `meter` on a route that is not `upto`, or an `upto` route with `maxTimeoutSeconds` above 3600 or a token other than USDp. Then, on a request to a route: an unknown network with no `acceptedTokens`, an accepted token whose decimals are neither in the catalog nor set via `decimals`, a price finer than the token's precision, or a price function that throws or returns something that is not a price |
|
|
783
788
|
| `X402RuntimeError` | Runtime failure; `.code` is one of the SDK codes below, or a facilitator error code propagated verbatim (see next table) |
|
|
784
789
|
| `X402PriceRejectedError` | Thrown by a price function to refuse a request before anything is invoiced; the client gets `.status` (400 by default) with `{ error: .code, message }`. `isPriceRejection(err)` recognises one by shape. |
|
|
785
790
|
|
|
@@ -807,12 +812,12 @@ The SDK raises it when the facilitator gave no usable answer:
|
|
|
807
812
|
- It could not be reached: a DNS, connection or TLS failure.
|
|
808
813
|
- It answered with a redirect, which the SDK never follows. The error's `status` is the redirect's, and the configured `url` is then usually not the facilitator's own https address.
|
|
809
814
|
- It did not answer a `verify` within 10 seconds. A `settle` that runs out of its 60 seconds raises `SETTLE_TIMEOUT` instead.
|
|
810
|
-
- It answered
|
|
815
|
+
- It answered with an error that carries no `error` field, such as an error page from a gateway or a firewall in front of it. The error's `status` is that answer's.
|
|
811
816
|
- It answered with a success status but a body that is not JSON, or that was cut off.
|
|
812
817
|
|
|
813
818
|
At `verify`, nothing was settled. The middleware answers the agent 402 with a new invoice and `error: "FACILITATOR_UNAVAILABLE"`, and the agent can pay again later.
|
|
814
819
|
|
|
815
|
-
At `settle`, the outcome is unknown, since the facilitator may have settled before its answer was lost. The middleware retries the settle once with the same settle token, which returns the recorded outcome
|
|
820
|
+
At `settle`, the outcome is unknown, since the facilitator may have settled before its answer was lost. The middleware retries the settle once with the same settle token, which returns the recorded outcome once the first attempt has finished. If the retry raises it too, the response is not served, and the agent gets a 402 `{ error: "PAYMENT_SETTLEMENT_FAILED", reason: "FACILITATOR_UNAVAILABLE" }` whose message tells it not to sign another payment before checking with the merchant. A retry that fails another way reports that reason instead.
|
|
816
821
|
|
|
817
822
|
### Propagated from the facilitator
|
|
818
823
|
|
|
@@ -822,7 +827,7 @@ When the facilitator refuses a payment at verify, its code is forwarded as the `
|
|
|
822
827
|
|------|--------------------|---------|--------------------------|
|
|
823
828
|
| `INVALID_PAYLOAD` | 400 | The payload does not have the shape of its route, carries a field another route would read, or uses a method of the other scheme | Sign a payment that matches one of the `accepts` entries |
|
|
824
829
|
| `INVALID_SIGNATURE` | 400 | EIP-712 signature recovery failed | Sign again |
|
|
825
|
-
| `INVALID_NONCE` | 409 | Nonce already used
|
|
830
|
+
| `INVALID_NONCE` | 409 | Nonce already used: the same payment reached the facilitator before, and may have settled | Do not sign another payment before checking with the merchant |
|
|
826
831
|
| `PAYMENT_EXPIRED` | 402 | `validBefore` has passed | Sign a new payment |
|
|
827
832
|
| `INSUFFICIENT_AMOUNT` | 402 | The amount the route guarantees the merchant is below the price, or the authorization is for zero | Sign for the invoiced `amount` |
|
|
828
833
|
| `INSUFFICIENT_BALANCE` | 402 | The payer does not hold the signed amount (metered payments) | Fund the wallet, sign a new payment |
|
|
@@ -833,6 +838,8 @@ When the facilitator refuses a payment at verify, its code is forwarded as the `
|
|
|
833
838
|
| `PAYMENT_METHOD_NOT_SUPPORTED` | 400 | No route for this method or token on this chain, including a metered payment on a chain without the escrow | Pay with another method or token |
|
|
834
839
|
| `COLLATERAL_NOT_SUPPORTED` | 400 | The swap delivers a token that is not a collateral of the chain | Pay for another of the `accepts` assets: the merchant lists one the facilitator does not deliver |
|
|
835
840
|
| `RATE_LIMIT_EXCEEDED` | 429 | The payer passed 1,000 verifies in the current UTC hour | Pay again after `Retry-After`, which points to the next hour |
|
|
841
|
+
| `PARALLELIZER_PAUSED` | 503 | The Parallelizer has closed the swap the payment needs, or its state could not be read. Nothing was taken | Pay with another token, or pay again later |
|
|
842
|
+
| `SAVINGS_PAUSED` | 503 | The sUSDp savings vault the payment deposits into or redeems from is paused, or its state could not be read. Nothing was taken | Pay with another token, or pay again later |
|
|
836
843
|
| `SERVICE_UNAVAILABLE` | 503 | The facilitator could not read the chain or its own store | Pay again after a short wait |
|
|
837
844
|
|
|
838
845
|
An `error` that is not a code (capitals, digits and underscores) is not forwarded: the 402 carries `error: "FACILITATOR_INVALID_RESPONSE"` with the facilitator's message instead. The facilitator answers this way with `Bad Request` to a payment payload that does not match its request schema, and with `Too Many Requests` from its per-IP limit (see [Rate limits](#rate-limits)).
|
|
@@ -842,19 +849,22 @@ When the facilitator refuses at settle, the 402 body is `{ error: "PAYMENT_SETTL
|
|
|
842
849
|
| Reason | Facilitator status | Meaning | What the agent should do |
|
|
843
850
|
|--------|--------------------|---------|--------------------------|
|
|
844
851
|
| `INSUFFICIENT_BALANCE` | 402 | The payer no longer holds the signed amount | Fund the wallet, sign a new payment |
|
|
845
|
-
| `AUTHORIZATION_USED` | 409 | The authorization
|
|
852
|
+
| `AUTHORIZATION_USED` | 409 | The authorization is already consumed or cancelled on-chain. It may have paid the merchant | Do not sign another payment before checking with the merchant |
|
|
846
853
|
| `PAYMENT_EXPIRED` | 402 | The authorization expired before settlement | Sign a new payment |
|
|
847
|
-
| `PAYMENT_NOT_VERIFIED` | 402 | No verified payment for this authorization, or it
|
|
854
|
+
| `PAYMENT_NOT_VERIFIED` | 402 | No verified payment for this authorization, or an earlier settle of it failed. After `AUTHORIZATION_USED`, `RECEIPT_UNAVAILABLE` or `SUBMISSION_FAILED`, a repeated settle gets that code again instead. The merchant may have been paid, or may still be | Do not sign another payment before checking with the merchant |
|
|
848
855
|
| `SETTLE_IN_PROGRESS` | 409 | Another settle of the same payment is still running | Do not sign another payment before checking with the merchant |
|
|
849
856
|
| `SETTLE_TOKEN_INVALID`, `SETTLE_TOKEN_REQUIRED` | 403 | The merchant did not present the settle token verify issued: a merchant client issue, not the payer's, and nothing was settled | Do not sign another payment before checking with the merchant |
|
|
850
|
-
| `RELAYER_UNAVAILABLE
|
|
857
|
+
| `RELAYER_UNAVAILABLE` | 503 | The facilitator is temporarily unable to settle. The payment was not taken, and the facilitator will not settle it | Retry later with a new payment, after `Retry-After` when given |
|
|
858
|
+
| `PARALLELIZER_PAUSED`, `SAVINGS_PAUSED`, `SERVICE_UNAVAILABLE` | 503 | The swap or the savings vault the payment needs is paused, or its state could not be read, or the facilitator or the chain is temporarily unable to settle. This settle sent nothing, but the payment may still settle until its authorization expires, and an earlier attempt may already have settled it | Do not sign another payment before checking with the merchant |
|
|
851
859
|
| `TX_REVERTED` | 500 | The transaction reverted for a reason the facilitator could not attribute | Sign a new payment |
|
|
852
|
-
| `
|
|
860
|
+
| `SWAP_PRICE_MOVED` | 402 | The Parallelizer refused the swap: its price or fee moved, between signing and settlement, beyond the margin signed with the payment. The payment was not taken, and the facilitator will not settle it | Sign a new payment |
|
|
861
|
+
| `SWAP_UNAVAILABLE` | 503 | The Parallelizer refused the swap because it cannot make it now: the swap closed since the facilitator checked it, a cap or a maximum fee is reached, the amount exceeds what it can release, or a price feed is stale. The payment was not taken, and the facilitator will not settle it | Pay with another token, or later with a new payment |
|
|
862
|
+
| `SUBMISSION_FAILED` | 500 | The settle failed. When the message says the outcome is unknown, a transaction may have been sent, or the facilitator could not read whether the authorization was used, and the payment may have settled or may still settle | Do not sign another payment before checking with the merchant. x402-fetch says so for this reason whatever the message. A client that reads the 402 itself may sign a new payment when the message says no transaction was submitted |
|
|
853
863
|
| `RECEIPT_UNAVAILABLE` | 500 | The transaction was sent but its confirmation could not be read | Do not sign another payment before checking with the merchant |
|
|
854
864
|
| `INVALID_PAYLOAD`, `NETWORK_MISMATCH`, `PAYMENT_METHOD_NOT_SUPPORTED` | 400 | The settle request does not match the payment that was verified, or a metered amount is missing, malformed or above the signed cap. Nothing was settled | Sign a new payment |
|
|
855
865
|
| `INSUFFICIENT_AMOUNT` | 402 | A metered amount of zero, or an authorization for zero. Nothing was settled | Sign a new payment |
|
|
856
866
|
|
|
857
|
-
Reasons from this SDK: `SETTLE_TIMEOUT`, `FACILITATOR_UNAVAILABLE` and `FACILITATOR_INVALID_RESPONSE` (the settle may still have gone through, do not pay again before checking with the merchant), `AMOUNT_MISMATCH` (the facilitator settled another amount than the one asked, money moved, the response was not served, do not pay again before checking), `UNKNOWN` (anything that is not a code, such as the `Too Many Requests` of the facilitator's per-IP limit). For `SETTLE_IN_PROGRESS`, `RECEIPT_UNAVAILABLE`, `SETTLE_TOKEN_INVALID` and `SETTLE_TOKEN_REQUIRED` the message is this SDK's as well, since the facilitator's text addresses the merchant.
|
|
867
|
+
Reasons from this SDK: `SETTLE_TIMEOUT`, `FACILITATOR_UNAVAILABLE` and `FACILITATOR_INVALID_RESPONSE` (the settle may still have gone through, do not pay again before checking with the merchant), `AMOUNT_MISMATCH` (the facilitator settled another amount than the one asked, money moved, the response was not served, do not pay again before checking), `UNKNOWN` (anything that is not a code, such as the `Too Many Requests` of the facilitator's per-IP limit: the payment may have settled, do not pay again before checking). For `SETTLE_IN_PROGRESS`, `RECEIPT_UNAVAILABLE`, `SETTLE_TOKEN_INVALID` and `SETTLE_TOKEN_REQUIRED` the message is this SDK's as well, since the facilitator's text addresses the merchant. The message is this SDK's too for `AUTHORIZATION_USED`, `PAYMENT_NOT_VERIFIED`, `PARALLELIZER_PAUSED`, `SAVINGS_PAUSED` and `SERVICE_UNAVAILABLE`: the payment may have settled or may still settle, so the message tells the agent to check with the merchant before paying again.
|
|
858
868
|
|
|
859
869
|
`AMOUNT_MISMATCH` is also the one reason that points at the facilitator rather than at the request: it confirmed a settlement of another amount than the one the route asked for, so money moved. Alert on it, for example on 402 responses whose `reason` is `AMOUNT_MISMATCH`, rather than treating it as a routine refusal.
|
|
860
870
|
|
|
@@ -865,7 +875,7 @@ The facilitator counts the requests of each IP address that calls it, which is t
|
|
|
865
875
|
| Limit | Facilitator answer | What the agent gets | What the agent should do |
|
|
866
876
|
|-------|--------------------|---------------------|--------------------------|
|
|
867
877
|
| Verifies per IP | 429 `{ error: "Too Many Requests", message }`, the message naming the time left in the minute (`Rate limit exceeded, retry in 42 seconds`, for instance), with `Retry-After` in seconds | 402 with a new invoice, `error: "FACILITATOR_INVALID_RESPONSE"`, the facilitator's message and its `Retry-After`. Nothing was verified | Send the payment again after `Retry-After` |
|
|
868
|
-
| Settles per IP | The same 429 | 402 `{ error: "PAYMENT_SETTLEMENT_FAILED", reason: "UNKNOWN", message
|
|
878
|
+
| Settles per IP | The same 429 | 402 `{ error: "PAYMENT_SETTLEMENT_FAILED", reason: "UNKNOWN", message }` with the facilitator's `Retry-After`. The handler's response is discarded and the settle is not retried. This settle did nothing, but when the limit answers the SDK's retry of a settle that timed out, the first attempt may have settled | The message says the payment may have settled: check with the merchant, then sign a new payment after `Retry-After` |
|
|
869
879
|
| Verifies per payer | 429 `RATE_LIMIT_EXCEEDED`, with `Retry-After` until the next UTC hour | 402 with a new invoice, `error: "RATE_LIMIT_EXCEEDED"`, the facilitator's message and its `Retry-After` | Pay again after `Retry-After` |
|
|
870
880
|
|
|
871
881
|
The `error` of the per-IP answer is not a code, which is why the middleware reports `FACILITATOR_INVALID_RESPONSE` at verify and `UNKNOWN` at settle. A direct caller of `FacilitatorClient` sees it as it is: `verify` throws an `X402RuntimeError` whose `.code` is `"Too Many Requests"`, with `.status` 429 and `.retryAfter`, and `settle` returns it as a failure with the same code.
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { X402_ERROR_CODES, X402ConfigError, X402RuntimeError, isPriceRejection } from './chunk-QAECRSWF.js';
|
|
2
|
-
import { getTokenDecimals as getTokenDecimals$1 } from '@parallel-protocol/chains';
|
|
2
|
+
import { isKnownChain, isActiveParallelizerChain, ACTIVE_PARALLELIZER_CHAINS, getTokenDecimals as getTokenDecimals$1 } from '@parallel-protocol/chains';
|
|
3
3
|
import { z } from 'zod';
|
|
4
4
|
|
|
5
5
|
var PARALLEL_TOKENS = {
|
|
@@ -155,6 +155,12 @@ var routeConfigSchema = z.object({
|
|
|
155
155
|
"meter must be a function of the response"
|
|
156
156
|
).optional()
|
|
157
157
|
}).refine(
|
|
158
|
+
(route) => !isKnownChain(route.network) || isActiveParallelizerChain(route.network),
|
|
159
|
+
{
|
|
160
|
+
message: `network must be one of the chains the facilitator settles on: ${ACTIVE_PARALLELIZER_CHAINS.join(", ")}`,
|
|
161
|
+
path: ["network"]
|
|
162
|
+
}
|
|
163
|
+
).refine(
|
|
158
164
|
(route) => route.meter === void 0 || route.scheme === "upto",
|
|
159
165
|
"meter only applies to upto routes"
|
|
160
166
|
).refine(
|
|
@@ -325,14 +331,16 @@ var FacilitatorClient = class {
|
|
|
325
331
|
throwIfRedirect(response);
|
|
326
332
|
if (!response.ok) {
|
|
327
333
|
const body = await readErrorBody(response);
|
|
328
|
-
if (body.error === void 0
|
|
329
|
-
throw new X402RuntimeError(X402_ERROR_CODES.FACILITATOR_UNAVAILABLE
|
|
334
|
+
if (body.error === void 0) {
|
|
335
|
+
throw new X402RuntimeError(X402_ERROR_CODES.FACILITATOR_UNAVAILABLE, {
|
|
336
|
+
status: response.status
|
|
337
|
+
});
|
|
330
338
|
}
|
|
331
339
|
const retryAfter = retryAfterOf(response);
|
|
332
340
|
return {
|
|
333
341
|
success: false,
|
|
334
342
|
error: {
|
|
335
|
-
code: body.error
|
|
343
|
+
code: body.error,
|
|
336
344
|
message: body.message ?? "Settlement failed",
|
|
337
345
|
status: response.status,
|
|
338
346
|
...retryAfter !== void 0 && { retryAfter }
|
|
@@ -772,8 +780,9 @@ function createPaymentGate(config) {
|
|
|
772
780
|
};
|
|
773
781
|
};
|
|
774
782
|
}
|
|
775
|
-
var
|
|
783
|
+
var DEFAULT_SETTLEMENT_MESSAGE = "The payment could not be settled.";
|
|
776
784
|
var MERCHANT_SETTLEMENT_MESSAGE = "The merchant could not complete the settlement of this payment. The payment was not taken: do not sign another payment for this request before checking with the merchant.";
|
|
785
|
+
var UNAVAILABLE_SETTLEMENT_MESSAGE = "The facilitator could not settle the payment for now. It may still settle until its authorization expires, or an earlier attempt may already have settled it: do not sign another payment for this request before checking with the merchant.";
|
|
777
786
|
var AGENT_MESSAGES = {
|
|
778
787
|
[X402_ERROR_CODES.SETTLE_TIMEOUT]: "The facilitator did not confirm the settlement in time. The payment may still settle: do not sign another payment for this request before checking with the merchant.",
|
|
779
788
|
[X402_ERROR_CODES.FACILITATOR_UNAVAILABLE]: "The facilitator could not be reached to settle the payment. It may still have settled: do not sign another payment for this request before checking with the merchant.",
|
|
@@ -782,15 +791,20 @@ var AGENT_MESSAGES = {
|
|
|
782
791
|
RECEIPT_UNAVAILABLE: "The facilitator sent the transaction but could not read its confirmation. The payment may have settled: do not sign another payment for this request before checking with the merchant.",
|
|
783
792
|
SETTLE_TOKEN_INVALID: MERCHANT_SETTLEMENT_MESSAGE,
|
|
784
793
|
SETTLE_TOKEN_REQUIRED: MERCHANT_SETTLEMENT_MESSAGE,
|
|
794
|
+
AUTHORIZATION_USED: "The payment's authorization was already consumed or cancelled on-chain. It may have paid the merchant: do not sign another payment for this request before checking with the merchant.",
|
|
795
|
+
PAYMENT_NOT_VERIFIED: "The facilitator holds no payment it can settle for this request. An earlier attempt may have paid the merchant, or may still settle: do not sign another payment for this request before checking with the merchant.",
|
|
796
|
+
PARALLELIZER_PAUSED: UNAVAILABLE_SETTLEMENT_MESSAGE,
|
|
797
|
+
SAVINGS_PAUSED: UNAVAILABLE_SETTLEMENT_MESSAGE,
|
|
798
|
+
SERVICE_UNAVAILABLE: UNAVAILABLE_SETTLEMENT_MESSAGE,
|
|
785
799
|
AMOUNT_MISMATCH: "The facilitator settled a different amount than this request asked for. The response was not served: do not sign another payment for this request before checking with the merchant.",
|
|
786
|
-
UNKNOWN:
|
|
800
|
+
UNKNOWN: "The facilitator gave no reason for this failure, and the payment may have settled: do not sign another payment for this request before checking with the merchant."
|
|
787
801
|
};
|
|
788
802
|
function settlementFailure(code, failure) {
|
|
789
803
|
const reason = shapedCode(code) ?? "UNKNOWN";
|
|
790
804
|
return {
|
|
791
805
|
settled: false,
|
|
792
806
|
reason,
|
|
793
|
-
message: AGENT_MESSAGES[reason] ?? boundedMessage(failure?.message) ??
|
|
807
|
+
message: AGENT_MESSAGES[reason] ?? boundedMessage(failure?.message) ?? DEFAULT_SETTLEMENT_MESSAGE,
|
|
794
808
|
...failure?.retryAfter !== void 0 && {
|
|
795
809
|
retryAfter: failure.retryAfter
|
|
796
810
|
}
|
|
@@ -157,6 +157,12 @@ var routeConfigSchema = zod.z.object({
|
|
|
157
157
|
"meter must be a function of the response"
|
|
158
158
|
).optional()
|
|
159
159
|
}).refine(
|
|
160
|
+
(route) => !chains.isKnownChain(route.network) || chains.isActiveParallelizerChain(route.network),
|
|
161
|
+
{
|
|
162
|
+
message: `network must be one of the chains the facilitator settles on: ${chains.ACTIVE_PARALLELIZER_CHAINS.join(", ")}`,
|
|
163
|
+
path: ["network"]
|
|
164
|
+
}
|
|
165
|
+
).refine(
|
|
160
166
|
(route) => route.meter === void 0 || route.scheme === "upto",
|
|
161
167
|
"meter only applies to upto routes"
|
|
162
168
|
).refine(
|
|
@@ -327,14 +333,16 @@ var FacilitatorClient = class {
|
|
|
327
333
|
throwIfRedirect(response);
|
|
328
334
|
if (!response.ok) {
|
|
329
335
|
const body = await readErrorBody(response);
|
|
330
|
-
if (body.error === void 0
|
|
331
|
-
throw new chunkKZZY5LS6_cjs.X402RuntimeError(chunkKZZY5LS6_cjs.X402_ERROR_CODES.FACILITATOR_UNAVAILABLE
|
|
336
|
+
if (body.error === void 0) {
|
|
337
|
+
throw new chunkKZZY5LS6_cjs.X402RuntimeError(chunkKZZY5LS6_cjs.X402_ERROR_CODES.FACILITATOR_UNAVAILABLE, {
|
|
338
|
+
status: response.status
|
|
339
|
+
});
|
|
332
340
|
}
|
|
333
341
|
const retryAfter = retryAfterOf(response);
|
|
334
342
|
return {
|
|
335
343
|
success: false,
|
|
336
344
|
error: {
|
|
337
|
-
code: body.error
|
|
345
|
+
code: body.error,
|
|
338
346
|
message: body.message ?? "Settlement failed",
|
|
339
347
|
status: response.status,
|
|
340
348
|
...retryAfter !== void 0 && { retryAfter }
|
|
@@ -774,8 +782,9 @@ function createPaymentGate(config) {
|
|
|
774
782
|
};
|
|
775
783
|
};
|
|
776
784
|
}
|
|
777
|
-
var
|
|
785
|
+
var DEFAULT_SETTLEMENT_MESSAGE = "The payment could not be settled.";
|
|
778
786
|
var MERCHANT_SETTLEMENT_MESSAGE = "The merchant could not complete the settlement of this payment. The payment was not taken: do not sign another payment for this request before checking with the merchant.";
|
|
787
|
+
var UNAVAILABLE_SETTLEMENT_MESSAGE = "The facilitator could not settle the payment for now. It may still settle until its authorization expires, or an earlier attempt may already have settled it: do not sign another payment for this request before checking with the merchant.";
|
|
779
788
|
var AGENT_MESSAGES = {
|
|
780
789
|
[chunkKZZY5LS6_cjs.X402_ERROR_CODES.SETTLE_TIMEOUT]: "The facilitator did not confirm the settlement in time. The payment may still settle: do not sign another payment for this request before checking with the merchant.",
|
|
781
790
|
[chunkKZZY5LS6_cjs.X402_ERROR_CODES.FACILITATOR_UNAVAILABLE]: "The facilitator could not be reached to settle the payment. It may still have settled: do not sign another payment for this request before checking with the merchant.",
|
|
@@ -784,15 +793,20 @@ var AGENT_MESSAGES = {
|
|
|
784
793
|
RECEIPT_UNAVAILABLE: "The facilitator sent the transaction but could not read its confirmation. The payment may have settled: do not sign another payment for this request before checking with the merchant.",
|
|
785
794
|
SETTLE_TOKEN_INVALID: MERCHANT_SETTLEMENT_MESSAGE,
|
|
786
795
|
SETTLE_TOKEN_REQUIRED: MERCHANT_SETTLEMENT_MESSAGE,
|
|
796
|
+
AUTHORIZATION_USED: "The payment's authorization was already consumed or cancelled on-chain. It may have paid the merchant: do not sign another payment for this request before checking with the merchant.",
|
|
797
|
+
PAYMENT_NOT_VERIFIED: "The facilitator holds no payment it can settle for this request. An earlier attempt may have paid the merchant, or may still settle: do not sign another payment for this request before checking with the merchant.",
|
|
798
|
+
PARALLELIZER_PAUSED: UNAVAILABLE_SETTLEMENT_MESSAGE,
|
|
799
|
+
SAVINGS_PAUSED: UNAVAILABLE_SETTLEMENT_MESSAGE,
|
|
800
|
+
SERVICE_UNAVAILABLE: UNAVAILABLE_SETTLEMENT_MESSAGE,
|
|
787
801
|
AMOUNT_MISMATCH: "The facilitator settled a different amount than this request asked for. The response was not served: do not sign another payment for this request before checking with the merchant.",
|
|
788
|
-
UNKNOWN:
|
|
802
|
+
UNKNOWN: "The facilitator gave no reason for this failure, and the payment may have settled: do not sign another payment for this request before checking with the merchant."
|
|
789
803
|
};
|
|
790
804
|
function settlementFailure(code, failure) {
|
|
791
805
|
const reason = shapedCode(code) ?? "UNKNOWN";
|
|
792
806
|
return {
|
|
793
807
|
settled: false,
|
|
794
808
|
reason,
|
|
795
|
-
message: AGENT_MESSAGES[reason] ?? boundedMessage(failure?.message) ??
|
|
809
|
+
message: AGENT_MESSAGES[reason] ?? boundedMessage(failure?.message) ?? DEFAULT_SETTLEMENT_MESSAGE,
|
|
796
810
|
...failure?.retryAfter !== void 0 && {
|
|
797
811
|
retryAfter: failure.retryAfter
|
|
798
812
|
}
|
package/dist/express.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkTH77IB3T_cjs = require('./chunk-TH77IB3T.cjs');
|
|
4
4
|
require('./chunk-KZZY5LS6.cjs');
|
|
5
5
|
|
|
6
6
|
// src/express/adapter.ts
|
|
@@ -108,8 +108,8 @@ var ExpressAdapter = class {
|
|
|
108
108
|
return originalEnd(...args);
|
|
109
109
|
});
|
|
110
110
|
next();
|
|
111
|
-
const outcome = await
|
|
112
|
-
if (outcome ===
|
|
111
|
+
const outcome = await chunkTH77IB3T_cjs.raceTimeout(endPromise, options?.timeoutMs);
|
|
112
|
+
if (outcome === chunkTH77IB3T_cjs.HANDLER_TIMEOUT) {
|
|
113
113
|
discarded = true;
|
|
114
114
|
return {
|
|
115
115
|
status: 504,
|
|
@@ -146,7 +146,7 @@ var ExpressAdapter = class {
|
|
|
146
146
|
const chunk = chunkBytes(args);
|
|
147
147
|
if (chunk) bytes.push(chunk);
|
|
148
148
|
}
|
|
149
|
-
return
|
|
149
|
+
return chunkTH77IB3T_cjs.parseJsonBody(
|
|
150
150
|
contentType,
|
|
151
151
|
Buffer.concat(bytes).toString("utf8")
|
|
152
152
|
);
|
|
@@ -170,7 +170,7 @@ var ExpressAdapter = class {
|
|
|
170
170
|
|
|
171
171
|
// src/express/index.ts
|
|
172
172
|
function paymentMiddleware(config) {
|
|
173
|
-
const middleware =
|
|
173
|
+
const middleware = chunkTH77IB3T_cjs.createPaymentMiddleware(config);
|
|
174
174
|
return async (req, res, next) => {
|
|
175
175
|
const result = await middleware(new ExpressAdapter(req, res, next));
|
|
176
176
|
if ("pass" in result) {
|
package/dist/express.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { raceTimeout, HANDLER_TIMEOUT, parseJsonBody, createPaymentMiddleware } from './chunk-
|
|
1
|
+
import { raceTimeout, HANDLER_TIMEOUT, parseJsonBody, createPaymentMiddleware } from './chunk-G7HSURPM.js';
|
|
2
2
|
import './chunk-QAECRSWF.js';
|
|
3
3
|
|
|
4
4
|
// src/express/adapter.ts
|
package/dist/fastify.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkTH77IB3T_cjs = require('./chunk-TH77IB3T.cjs');
|
|
4
4
|
require('./chunk-KZZY5LS6.cjs');
|
|
5
5
|
var fp = require('fastify-plugin');
|
|
6
6
|
|
|
@@ -37,7 +37,7 @@ var FastifyAdapter = class {
|
|
|
37
37
|
// src/fastify/index.ts
|
|
38
38
|
function payloadJson(contentType, payload) {
|
|
39
39
|
const text = typeof payload === "string" ? payload : payload instanceof Uint8Array ? Buffer.from(payload).toString("utf8") : void 0;
|
|
40
|
-
return
|
|
40
|
+
return chunkTH77IB3T_cjs.parseJsonBody(contentType, text);
|
|
41
41
|
}
|
|
42
42
|
function refuse(reply, result) {
|
|
43
43
|
const json = JSON.stringify(result.body);
|
|
@@ -54,11 +54,11 @@ function headerValue(value) {
|
|
|
54
54
|
}
|
|
55
55
|
function paymentMiddleware(config) {
|
|
56
56
|
return fp__default.default(async (fastify) => {
|
|
57
|
-
const gate =
|
|
57
|
+
const gate = chunkTH77IB3T_cjs.createPaymentGate(config);
|
|
58
58
|
const verifiedMap = /* @__PURE__ */ new WeakMap();
|
|
59
59
|
const rawWriters = /* @__PURE__ */ new WeakMap();
|
|
60
60
|
const needsBody = (req) => {
|
|
61
|
-
const route =
|
|
61
|
+
const route = chunkTH77IB3T_cjs.matchRoute(
|
|
62
62
|
new FastifyAdapter(req).getPath(),
|
|
63
63
|
config.routes
|
|
64
64
|
);
|
|
@@ -111,29 +111,29 @@ function paymentMiddleware(config) {
|
|
|
111
111
|
if (!verified) return payload;
|
|
112
112
|
let raw;
|
|
113
113
|
if (verified.scheme === "upto") {
|
|
114
|
-
raw = headerValue(reply.getHeader(
|
|
115
|
-
reply.removeHeader(
|
|
114
|
+
raw = headerValue(reply.getHeader(chunkTH77IB3T_cjs.SETTLE_AMOUNT_HEADER));
|
|
115
|
+
reply.removeHeader(chunkTH77IB3T_cjs.SETTLE_AMOUNT_HEADER);
|
|
116
116
|
}
|
|
117
117
|
if (reply.statusCode < 200 || reply.statusCode >= 300) {
|
|
118
118
|
return payload;
|
|
119
119
|
}
|
|
120
120
|
let settleOptions;
|
|
121
121
|
if (verified.scheme === "upto") {
|
|
122
|
-
const resolved = await
|
|
122
|
+
const resolved = await chunkTH77IB3T_cjs.resolveCharge(verified, raw, {
|
|
123
123
|
status: reply.statusCode,
|
|
124
124
|
header: (name) => headerValue(reply.getHeader(name)),
|
|
125
125
|
body: async () => payloadJson(headerValue(reply.getHeader("content-type")), payload)
|
|
126
126
|
});
|
|
127
127
|
if (resolved.kind === "invalid") {
|
|
128
|
-
return refuse(reply,
|
|
128
|
+
return refuse(reply, chunkTH77IB3T_cjs.invalidSettleAmountResponse(resolved.reason));
|
|
129
129
|
}
|
|
130
130
|
if (resolved.kind === "skip") return payload;
|
|
131
131
|
settleOptions = { amount: resolved.amount };
|
|
132
132
|
}
|
|
133
|
-
const outcome = await
|
|
133
|
+
const outcome = await chunkTH77IB3T_cjs.settleVerified(verified, settleOptions);
|
|
134
134
|
if (!outcome.settled)
|
|
135
|
-
return refuse(reply,
|
|
136
|
-
reply.header("payment-response",
|
|
135
|
+
return refuse(reply, chunkTH77IB3T_cjs.settlementFailureResponse(outcome));
|
|
136
|
+
reply.header("payment-response", chunkTH77IB3T_cjs.encodeBase64(outcome.confirmation));
|
|
137
137
|
reply.header("access-control-expose-headers", "payment-response");
|
|
138
138
|
return payload;
|
|
139
139
|
});
|
package/dist/fastify.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createPaymentGate, SETTLE_AMOUNT_HEADER, resolveCharge, invalidSettleAmountResponse, settleVerified, settlementFailureResponse, encodeBase64, matchRoute, parseJsonBody } from './chunk-
|
|
1
|
+
import { createPaymentGate, SETTLE_AMOUNT_HEADER, resolveCharge, invalidSettleAmountResponse, settleVerified, settlementFailureResponse, encodeBase64, matchRoute, parseJsonBody } from './chunk-G7HSURPM.js';
|
|
2
2
|
import './chunk-QAECRSWF.js';
|
|
3
3
|
import fp from 'fastify-plugin';
|
|
4
4
|
|
package/dist/hono.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkTH77IB3T_cjs = require('./chunk-TH77IB3T.cjs');
|
|
4
4
|
require('./chunk-KZZY5LS6.cjs');
|
|
5
5
|
|
|
6
6
|
// src/hono/adapter.ts
|
|
@@ -30,7 +30,7 @@ var HonoAdapter = class {
|
|
|
30
30
|
// Hono caches the parsed body on the request, so the handler can still read
|
|
31
31
|
// it after the price function did.
|
|
32
32
|
async getBody() {
|
|
33
|
-
if (!
|
|
33
|
+
if (!chunkTH77IB3T_cjs.isJsonContentType(this.c.req.header("content-type"))) return void 0;
|
|
34
34
|
try {
|
|
35
35
|
return await this.c.req.json();
|
|
36
36
|
} catch {
|
|
@@ -38,8 +38,8 @@ var HonoAdapter = class {
|
|
|
38
38
|
}
|
|
39
39
|
}
|
|
40
40
|
async runHandler(options) {
|
|
41
|
-
const outcome = await
|
|
42
|
-
if (outcome ===
|
|
41
|
+
const outcome = await chunkTH77IB3T_cjs.raceTimeout(this.next(), options?.timeoutMs);
|
|
42
|
+
if (outcome === chunkTH77IB3T_cjs.HANDLER_TIMEOUT) {
|
|
43
43
|
return {
|
|
44
44
|
status: 504,
|
|
45
45
|
setResponseHeader: () => {
|
|
@@ -64,7 +64,7 @@ var HonoAdapter = class {
|
|
|
64
64
|
getResponseHeader: (name) => res.headers.get(name) ?? void 0,
|
|
65
65
|
removeResponseHeader: (name) => res.headers.delete(name),
|
|
66
66
|
// Read from a clone: the response body streams once, to the client.
|
|
67
|
-
getResponseBody: () =>
|
|
67
|
+
getResponseBody: () => chunkTH77IB3T_cjs.jsonFromResponse(res),
|
|
68
68
|
sendResponse: () => {
|
|
69
69
|
},
|
|
70
70
|
// The response is replaced: release its body (and whatever upstream
|
|
@@ -79,7 +79,7 @@ var HonoAdapter = class {
|
|
|
79
79
|
|
|
80
80
|
// src/hono/index.ts
|
|
81
81
|
function paymentMiddleware(config) {
|
|
82
|
-
const middleware =
|
|
82
|
+
const middleware = chunkTH77IB3T_cjs.createPaymentMiddleware(config);
|
|
83
83
|
return async (c, next) => {
|
|
84
84
|
const result = await middleware(new HonoAdapter(c, next));
|
|
85
85
|
if ("pass" in result) {
|
package/dist/hono.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isJsonContentType, raceTimeout, HANDLER_TIMEOUT, jsonFromResponse, createPaymentMiddleware } from './chunk-
|
|
1
|
+
import { isJsonContentType, raceTimeout, HANDLER_TIMEOUT, jsonFromResponse, createPaymentMiddleware } from './chunk-G7HSURPM.js';
|
|
2
2
|
import './chunk-QAECRSWF.js';
|
|
3
3
|
|
|
4
4
|
// src/hono/adapter.ts
|
package/dist/index.cjs
CHANGED
|
@@ -1,65 +1,65 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkTH77IB3T_cjs = require('./chunk-TH77IB3T.cjs');
|
|
4
4
|
var chunkKZZY5LS6_cjs = require('./chunk-KZZY5LS6.cjs');
|
|
5
5
|
|
|
6
6
|
|
|
7
7
|
|
|
8
8
|
Object.defineProperty(exports, "FacilitatorClient", {
|
|
9
9
|
enumerable: true,
|
|
10
|
-
get: function () { return
|
|
10
|
+
get: function () { return chunkTH77IB3T_cjs.FacilitatorClient; }
|
|
11
11
|
});
|
|
12
12
|
Object.defineProperty(exports, "PARALLEL_TOKENS", {
|
|
13
13
|
enumerable: true,
|
|
14
|
-
get: function () { return
|
|
14
|
+
get: function () { return chunkTH77IB3T_cjs.PARALLEL_TOKENS; }
|
|
15
15
|
});
|
|
16
16
|
Object.defineProperty(exports, "SETTLE_AMOUNT_HEADER", {
|
|
17
17
|
enumerable: true,
|
|
18
|
-
get: function () { return
|
|
18
|
+
get: function () { return chunkTH77IB3T_cjs.SETTLE_AMOUNT_HEADER; }
|
|
19
19
|
});
|
|
20
20
|
Object.defineProperty(exports, "createPaymentGate", {
|
|
21
21
|
enumerable: true,
|
|
22
|
-
get: function () { return
|
|
22
|
+
get: function () { return chunkTH77IB3T_cjs.createPaymentGate; }
|
|
23
23
|
});
|
|
24
24
|
Object.defineProperty(exports, "createPaymentMiddleware", {
|
|
25
25
|
enumerable: true,
|
|
26
|
-
get: function () { return
|
|
26
|
+
get: function () { return chunkTH77IB3T_cjs.createPaymentMiddleware; }
|
|
27
27
|
});
|
|
28
28
|
Object.defineProperty(exports, "encodeBase64", {
|
|
29
29
|
enumerable: true,
|
|
30
|
-
get: function () { return
|
|
30
|
+
get: function () { return chunkTH77IB3T_cjs.encodeBase64; }
|
|
31
31
|
});
|
|
32
32
|
Object.defineProperty(exports, "getDefaultAcceptedTokens", {
|
|
33
33
|
enumerable: true,
|
|
34
|
-
get: function () { return
|
|
34
|
+
get: function () { return chunkTH77IB3T_cjs.getDefaultAcceptedTokens; }
|
|
35
35
|
});
|
|
36
36
|
Object.defineProperty(exports, "parsePrice", {
|
|
37
37
|
enumerable: true,
|
|
38
|
-
get: function () { return
|
|
38
|
+
get: function () { return chunkTH77IB3T_cjs.parsePrice; }
|
|
39
39
|
});
|
|
40
40
|
Object.defineProperty(exports, "resolveCharge", {
|
|
41
41
|
enumerable: true,
|
|
42
|
-
get: function () { return
|
|
42
|
+
get: function () { return chunkTH77IB3T_cjs.resolveCharge; }
|
|
43
43
|
});
|
|
44
44
|
Object.defineProperty(exports, "resolveSettleAmount", {
|
|
45
45
|
enumerable: true,
|
|
46
|
-
get: function () { return
|
|
46
|
+
get: function () { return chunkTH77IB3T_cjs.resolveSettleAmount; }
|
|
47
47
|
});
|
|
48
48
|
Object.defineProperty(exports, "settleVerified", {
|
|
49
49
|
enumerable: true,
|
|
50
|
-
get: function () { return
|
|
50
|
+
get: function () { return chunkTH77IB3T_cjs.settleVerified; }
|
|
51
51
|
});
|
|
52
52
|
Object.defineProperty(exports, "settlementFailureResponse", {
|
|
53
53
|
enumerable: true,
|
|
54
|
-
get: function () { return
|
|
54
|
+
get: function () { return chunkTH77IB3T_cjs.settlementFailureResponse; }
|
|
55
55
|
});
|
|
56
56
|
Object.defineProperty(exports, "toEip155Network", {
|
|
57
57
|
enumerable: true,
|
|
58
|
-
get: function () { return
|
|
58
|
+
get: function () { return chunkTH77IB3T_cjs.toEip155Network; }
|
|
59
59
|
});
|
|
60
60
|
Object.defineProperty(exports, "validateAddress", {
|
|
61
61
|
enumerable: true,
|
|
62
|
-
get: function () { return
|
|
62
|
+
get: function () { return chunkTH77IB3T_cjs.validateAddress; }
|
|
63
63
|
});
|
|
64
64
|
Object.defineProperty(exports, "X402ConfigError", {
|
|
65
65
|
enumerable: true,
|
package/dist/index.js
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
export { FacilitatorClient, PARALLEL_TOKENS, SETTLE_AMOUNT_HEADER, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, parsePrice, resolveCharge, resolveSettleAmount, settleVerified, settlementFailureResponse, toEip155Network, validateAddress } from './chunk-
|
|
1
|
+
export { FacilitatorClient, PARALLEL_TOKENS, SETTLE_AMOUNT_HEADER, createPaymentGate, createPaymentMiddleware, encodeBase64, getDefaultAcceptedTokens, parsePrice, resolveCharge, resolveSettleAmount, settleVerified, settlementFailureResponse, toEip155Network, validateAddress } from './chunk-G7HSURPM.js';
|
|
2
2
|
export { X402ConfigError, X402PriceRejectedError, X402RuntimeError, X402_ERROR_CODES, isPriceRejection } from './chunk-QAECRSWF.js';
|
package/dist/next.cjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var chunkTH77IB3T_cjs = require('./chunk-TH77IB3T.cjs');
|
|
4
4
|
require('./chunk-KZZY5LS6.cjs');
|
|
5
5
|
var server = require('next/server');
|
|
6
6
|
|
|
@@ -25,7 +25,7 @@ var NextAdapter = class {
|
|
|
25
25
|
// Read from a clone: a request body is a stream that can be consumed once,
|
|
26
26
|
// and the handler still needs the original.
|
|
27
27
|
async getBody() {
|
|
28
|
-
if (!
|
|
28
|
+
if (!chunkTH77IB3T_cjs.isJsonContentType(this.req.headers.get("content-type") ?? void 0))
|
|
29
29
|
return void 0;
|
|
30
30
|
try {
|
|
31
31
|
return await this.req.clone().json();
|
|
@@ -43,7 +43,7 @@ function fromResult(result) {
|
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
45
|
function withPayment(config, handler) {
|
|
46
|
-
const gate =
|
|
46
|
+
const gate = chunkTH77IB3T_cjs.createPaymentGate(config);
|
|
47
47
|
return async (req) => {
|
|
48
48
|
const gateResult = await gate(new NextAdapter(req));
|
|
49
49
|
if (gateResult.type === "pass") {
|
|
@@ -52,11 +52,11 @@ function withPayment(config, handler) {
|
|
|
52
52
|
if (gateResult.type === "error") return fromResult(gateResult.result);
|
|
53
53
|
let response;
|
|
54
54
|
try {
|
|
55
|
-
const outcome = await
|
|
55
|
+
const outcome = await chunkTH77IB3T_cjs.raceTimeout(
|
|
56
56
|
handler(req),
|
|
57
57
|
gateResult.handlerTimeoutMs
|
|
58
58
|
);
|
|
59
|
-
if (outcome ===
|
|
59
|
+
if (outcome === chunkTH77IB3T_cjs.HANDLER_TIMEOUT) {
|
|
60
60
|
return new server.NextResponse(null, { status: 504 });
|
|
61
61
|
}
|
|
62
62
|
response = outcome;
|
|
@@ -65,10 +65,10 @@ function withPayment(config, handler) {
|
|
|
65
65
|
}
|
|
66
66
|
let raw;
|
|
67
67
|
if (gateResult.scheme === "upto") {
|
|
68
|
-
raw = response.headers.get(
|
|
68
|
+
raw = response.headers.get(chunkTH77IB3T_cjs.SETTLE_AMOUNT_HEADER) ?? void 0;
|
|
69
69
|
if (raw !== void 0) {
|
|
70
70
|
const headers = new Headers(response.headers);
|
|
71
|
-
headers.delete(
|
|
71
|
+
headers.delete(chunkTH77IB3T_cjs.SETTLE_AMOUNT_HEADER);
|
|
72
72
|
response = new server.NextResponse(response.body, {
|
|
73
73
|
status: response.status,
|
|
74
74
|
headers
|
|
@@ -79,24 +79,24 @@ function withPayment(config, handler) {
|
|
|
79
79
|
let settleOptions;
|
|
80
80
|
if (gateResult.scheme === "upto") {
|
|
81
81
|
const handled = response;
|
|
82
|
-
const resolved = await
|
|
82
|
+
const resolved = await chunkTH77IB3T_cjs.resolveCharge(gateResult, raw, {
|
|
83
83
|
status: handled.status,
|
|
84
84
|
header: (name) => handled.headers.get(name) ?? void 0,
|
|
85
85
|
// Read from a clone: the body streams once, to the client.
|
|
86
|
-
body: () =>
|
|
86
|
+
body: () => chunkTH77IB3T_cjs.jsonFromResponse(handled)
|
|
87
87
|
});
|
|
88
88
|
if (resolved.kind === "invalid") {
|
|
89
89
|
handled.body?.cancel().catch(() => {
|
|
90
90
|
});
|
|
91
|
-
return fromResult(
|
|
91
|
+
return fromResult(chunkTH77IB3T_cjs.invalidSettleAmountResponse(resolved.reason));
|
|
92
92
|
}
|
|
93
93
|
if (resolved.kind === "skip") return response;
|
|
94
94
|
settleOptions = { amount: resolved.amount };
|
|
95
95
|
}
|
|
96
|
-
const outcome = await
|
|
96
|
+
const outcome = await chunkTH77IB3T_cjs.settleVerified(gateResult, settleOptions);
|
|
97
97
|
if (outcome.settled) {
|
|
98
98
|
const headers = new Headers(response.headers);
|
|
99
|
-
headers.set("payment-response",
|
|
99
|
+
headers.set("payment-response", chunkTH77IB3T_cjs.encodeBase64(outcome.confirmation));
|
|
100
100
|
headers.set("access-control-expose-headers", "payment-response");
|
|
101
101
|
return new server.NextResponse(response.body, {
|
|
102
102
|
status: response.status,
|
|
@@ -105,7 +105,7 @@ function withPayment(config, handler) {
|
|
|
105
105
|
}
|
|
106
106
|
response.body?.cancel().catch(() => {
|
|
107
107
|
});
|
|
108
|
-
return fromResult(
|
|
108
|
+
return fromResult(chunkTH77IB3T_cjs.settlementFailureResponse(outcome));
|
|
109
109
|
}
|
|
110
110
|
return response;
|
|
111
111
|
};
|
package/dist/next.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { isJsonContentType, createPaymentGate, raceTimeout, HANDLER_TIMEOUT, SETTLE_AMOUNT_HEADER, resolveCharge, jsonFromResponse, invalidSettleAmountResponse, settleVerified, encodeBase64, settlementFailureResponse } from './chunk-
|
|
1
|
+
import { isJsonContentType, createPaymentGate, raceTimeout, HANDLER_TIMEOUT, SETTLE_AMOUNT_HEADER, resolveCharge, jsonFromResponse, invalidSettleAmountResponse, settleVerified, encodeBase64, settlementFailureResponse } from './chunk-G7HSURPM.js';
|
|
2
2
|
import './chunk-QAECRSWF.js';
|
|
3
3
|
import { NextResponse } from 'next/server';
|
|
4
4
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@parallel-protocol/x402",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.3",
|
|
4
4
|
"description": "Parallel Protocol x402 payment middleware — Express, Next.js, Fastify, Hono",
|
|
5
5
|
"author": "Parallel Protocol <contact@parallel.best>",
|
|
6
6
|
"repository": {
|
|
@@ -133,7 +133,7 @@
|
|
|
133
133
|
"vitest": "^4.0.14"
|
|
134
134
|
},
|
|
135
135
|
"dependencies": {
|
|
136
|
-
"@parallel-protocol/chains": "^0.5.
|
|
136
|
+
"@parallel-protocol/chains": "^0.5.1",
|
|
137
137
|
"fastify-plugin": "^6.0.0",
|
|
138
138
|
"zod": "^4.4.3"
|
|
139
139
|
},
|