uvd-x402-sdk 2.102.0 → 2.103.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 (64) hide show
  1. package/README.md +70 -7
  2. package/dist/adapters/index.js +3 -1
  3. package/dist/adapters/index.js.map +1 -1
  4. package/dist/adapters/index.mjs +3 -1
  5. package/dist/adapters/index.mjs.map +1 -1
  6. package/dist/backend/index.d.mts +1 -1
  7. package/dist/backend/index.d.ts +1 -1
  8. package/dist/backend/index.js +135 -43
  9. package/dist/backend/index.js.map +1 -1
  10. package/dist/backend/index.mjs +135 -44
  11. package/dist/backend/index.mjs.map +1 -1
  12. package/dist/{index-Di8sCYHx.d.ts → index-CLlhFKbm.d.ts} +75 -4
  13. package/dist/{index-DKUUBqZO.d.mts → index-fImniLTl.d.mts} +75 -4
  14. package/dist/index.d.mts +1 -1
  15. package/dist/index.d.ts +1 -1
  16. package/dist/index.js +125 -44
  17. package/dist/index.js.map +1 -1
  18. package/dist/index.mjs +125 -45
  19. package/dist/index.mjs.map +1 -1
  20. package/dist/providers/algorand/index.js +3 -1
  21. package/dist/providers/algorand/index.js.map +1 -1
  22. package/dist/providers/algorand/index.mjs +3 -1
  23. package/dist/providers/algorand/index.mjs.map +1 -1
  24. package/dist/providers/evm/index.js +3 -1
  25. package/dist/providers/evm/index.js.map +1 -1
  26. package/dist/providers/evm/index.mjs +3 -1
  27. package/dist/providers/evm/index.mjs.map +1 -1
  28. package/dist/providers/near/index.js +3 -1
  29. package/dist/providers/near/index.js.map +1 -1
  30. package/dist/providers/near/index.mjs +3 -1
  31. package/dist/providers/near/index.mjs.map +1 -1
  32. package/dist/providers/solana/index.js +3 -1
  33. package/dist/providers/solana/index.js.map +1 -1
  34. package/dist/providers/solana/index.mjs +3 -1
  35. package/dist/providers/solana/index.mjs.map +1 -1
  36. package/dist/providers/stellar/index.js +3 -1
  37. package/dist/providers/stellar/index.js.map +1 -1
  38. package/dist/providers/stellar/index.mjs +3 -1
  39. package/dist/providers/stellar/index.mjs.map +1 -1
  40. package/dist/providers/sui/index.js +3 -1
  41. package/dist/providers/sui/index.js.map +1 -1
  42. package/dist/providers/sui/index.mjs +3 -1
  43. package/dist/providers/sui/index.mjs.map +1 -1
  44. package/dist/providers/xrpl/index.js +3 -1
  45. package/dist/providers/xrpl/index.js.map +1 -1
  46. package/dist/providers/xrpl/index.mjs +3 -1
  47. package/dist/providers/xrpl/index.mjs.map +1 -1
  48. package/dist/react/index.js +3 -1
  49. package/dist/react/index.js.map +1 -1
  50. package/dist/react/index.mjs +3 -1
  51. package/dist/react/index.mjs.map +1 -1
  52. package/dist/react/picker/index.js +3 -1
  53. package/dist/react/picker/index.js.map +1 -1
  54. package/dist/react/picker/index.mjs +3 -1
  55. package/dist/react/picker/index.mjs.map +1 -1
  56. package/dist/utils/index.js +4 -2
  57. package/dist/utils/index.js.map +1 -1
  58. package/dist/utils/index.mjs +4 -2
  59. package/dist/utils/index.mjs.map +1 -1
  60. package/package.json +2 -1
  61. package/src/backend/index.ts +317 -55
  62. package/src/chains/index.ts +3 -1
  63. package/src/index.ts +6 -0
  64. package/src/utils/x402.ts +5 -2
package/README.md CHANGED
@@ -888,11 +888,74 @@ app.post('/api/premium', async (req, res) => {
888
888
  });
889
889
  ```
890
890
 
891
- `createPaymentMiddleware()` and `createHonoMiddleware()` verify and settle automatically by default (`before-handler`). Use `settlementStrategy: 'manual'` if you need to control when settlement happens (e.g., settle only after confirming you can fulfill the request).
891
+ `createPaymentMiddleware()`, `createHonoMiddleware()` and `createFetchPaywall()` verify and settle automatically by default (`before-handler`). Use `settlementStrategy: 'manual'` if you need to control when settlement happens (e.g., settle only after confirming you can fulfill the request).
892
+
893
+ A buyer in a browser on another origin reads the terms of a v2 402, in the `PAYMENT-REQUIRED` header, only if the app's CORS exposes that header: list it in `Access-Control-Expose-Headers` (with `hono/cors`, `exposeHeaders: ['PAYMENT-REQUIRED']`) and send `Access-Control-Allow-Origin`. The 402 of `createHonoMiddleware` and `createFetchPaywall` sets no CORS header of its own; `create402Response` lists the header in its `headers` but sets no `Access-Control-Allow-Origin` (spread `getCorsHeaders(origin)` too).
894
+
895
+ ### Astro, Next.js, Vercel and Cloudflare (`createFetchPaywall`)
896
+
897
+ A route that takes a `Request` and returns a `Response` gets the same paywall as `createHonoMiddleware`: same options, same answers, same code underneath. `createFetchPaywall(options)` returns a wrapper; the route it wraps runs only for a verified payment (settled first, unless `settlementStrategy: 'manual'`) and receives it as its second argument. Without a payment the route answers `402` with the accepts, and it never runs for a refused, unverifiable or already used one.
898
+
899
+ **Astro** (`src/pages/api/premium/[id].ts`, on-demand rendering with any adapter, e.g. `@astrojs/vercel`):
900
+
901
+ ```typescript
902
+ import type { APIRoute } from 'astro';
903
+ import { createFetchPaywall } from 'uvd-x402-sdk/backend';
904
+
905
+ // Run on every request: a prerendered endpoint is called once, at build time, without a payment.
906
+ export const prerender = false;
907
+
908
+ const paywall = createFetchPaywall({
909
+ accepts: [{
910
+ network: 'base',
911
+ asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', // USDC on Base
912
+ amount: '10000', // $0.01 (6 decimals)
913
+ payTo: process.env.RECEIVING_ADDRESS as string,
914
+ }],
915
+ });
916
+
917
+ export const GET: APIRoute = paywall(async (context, x402) => {
918
+ return Response.json({ id: context.params.id, payer: x402.verifyResult.payer });
919
+ });
920
+ ```
921
+
922
+ **Next.js** (App Router, `app/api/articles/[slug]/route.ts`):
923
+
924
+ ```typescript
925
+ import { createFetchPaywall } from 'uvd-x402-sdk/backend';
926
+
927
+ const paywall = createFetchPaywall({ accepts: [/* as above */] });
928
+
929
+ export const GET = paywall(async (request: Request, x402, { params }: { params: Promise<{ slug: string }> }) => {
930
+ const { slug } = await params;
931
+ const settled = await x402.settle(); // the settle already made before the handler ran
932
+ return Response.json({ slug, transaction: settled.transactionHash });
933
+ });
934
+ ```
935
+
936
+ **Vercel Functions and Cloudflare Workers** take the `Request` itself; whatever else the platform passes (`env`, `ctx`) comes after the payment:
937
+
938
+ ```typescript
939
+ import { createFetchPaywall } from 'uvd-x402-sdk/backend';
940
+
941
+ const paywall = createFetchPaywall({
942
+ accepts: [{ network: 'base', asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913', amount: '10000', payTo: '0xYourWallet' }],
943
+ });
944
+
945
+ export default {
946
+ fetch: paywall(async (request: Request, x402, env: Env, ctx: ExecutionContext) => new Response('paid content')),
947
+ };
948
+ ```
949
+
950
+ - The route's first argument is the `Request`, or an object that carries it as `request` (Astro, SvelteKit, React Router). The handler gets that same first argument back, then the payment, then the rest.
951
+ - The advertised `resource` is `request.url`. Behind a proxy that rewrites the host, set `resource` in the accept.
952
+ - A facilitator receipt goes out as `PAYMENT-RESPONSE` on the response the handler returns, added to its own `Access-Control-Expose-Headers` and `Cache-Control` rather than replacing them; a `Response.redirect()` works too. In `'manual'` mode, a `settle()` that ends after the handler returned changes nothing in the response.
953
+ - Once a receipt exists, a handler that throws an `Error` gets a `500` that keeps the receipt headers, as Hono's default error handler answers one, and the error goes to `console.error`: the buyer may have paid, and the receipt is their proof. Such an error does not reach the framework's error reporting (Next.js `onRequestError`, a Sentry wrapper); catch it inside the handler if it must. The framework's own control flow passes through untouched: a thrown `Response` (React Router, Remix) is rethrown with the receipt headers, and SvelteKit's `redirect()` / `error()` and Next.js' `redirect()` / `notFound()` are rethrown as they are, so the receipt does not ride on them; to send it with a redirect, return `Response.redirect()` instead. Without a receipt every error reaches the framework as usual.
954
+ - The paywall answers every request that reaches it, `OPTIONS` included. If browsers on another origin pay, answer the preflight before the paywall (an `OPTIONS` export in Astro or Next.js, a method check in a Worker) and add `Access-Control-Allow-Origin` to what the route returns (`getCorsHeaders(origin)`).
892
955
 
893
956
  ### Stack key (services run by Ultravioleta DAO)
894
957
 
895
- Services run by Ultravioleta DAO carry a per-service credential, the stack key, so that the facilitator's rate-limit policy does not answer them `429`. Everything in this SDK that calls the facilitator sends it as `X-UVD-Stack-Key`: the clients `FacilitatorClient` (and the two middlewares), `Erc8004Client`, `BazaarClient`, `EscrowClient` and `AdvancedEscrowClient`, and the functions `anchorEvidence`, `availableBackends`, `streamTrafficEvents` and `getFacilitatorReceipt`, which take the same `stackKey` / `stackKeyHosts` options. It changes nothing else about a payment, and a facilitator that does not know the header ignores it.
958
+ Services run by Ultravioleta DAO carry a per-service credential, the stack key, so that the facilitator's rate-limit policy does not answer them `429`. Everything in this SDK that calls the facilitator sends it as `X-UVD-Stack-Key`: the clients `FacilitatorClient` (and the two middlewares and `createFetchPaywall`), `Erc8004Client`, `BazaarClient`, `EscrowClient` and `AdvancedEscrowClient`, and the functions `anchorEvidence`, `availableBackends`, `streamTrafficEvents` and `getFacilitatorReceipt`, which take the same `stackKey` / `stackKeyHosts` options. It changes nothing else about a payment, and a facilitator that does not know the header ignores it.
896
959
 
897
960
  - **Where it comes from:** the facilitator's operator issues one per service: `uvdsk_` followed by 43-128 base64url characters. The process receives it as `UVD_STACK_KEY` (read by default) or as the `stackKey` option, from a secret store, never from code.
898
961
  - **Not for third parties:** if you integrate this SDK in your own product you have no key and need none. Leave both unset and no header is sent.
@@ -1248,7 +1311,7 @@ anti-double-settle guard, adopted here.
1248
1311
 
1249
1312
  ### Middleware
1250
1313
 
1251
- `createPaymentMiddleware` and `createHonoMiddleware` answer **503 with a
1314
+ `createPaymentMiddleware`, `createHonoMiddleware` and `createFetchPaywall` answer **503 with a
1252
1315
  `Retry-After` header** — not `402`, not `500` — whenever the facilitator reached
1253
1316
  no verdict, and keep answering `402` for genuine rejections.
1254
1317
 
@@ -1260,7 +1323,7 @@ the buyer's client can reconcile instead of paying again.
1260
1323
  An X-PAYMENT the facilitator already admitted for another request is answered
1261
1324
  **`409`** (`authorization_already_settled`, `receipt_request_conflict`: it was
1262
1325
  used, the handler does not run) or **`503` + `Retry-After`** while it is
1263
- `authorization_in_flight` — never `402` and never `500`. Both middlewares add
1326
+ `authorization_in_flight` — never `402` and never `500`. All three add
1264
1327
  `PAYMENT-RESPONSE` to an existing `Access-Control-Expose-Headers` and `no-store`
1265
1328
  to an existing `Cache-Control` instead of replacing them, and in `'manual'` mode
1266
1329
  a `settle()` after the handler already answered no longer touches the sent
@@ -1771,7 +1834,7 @@ bazaarExtension({
1771
1834
  });
1772
1835
  ```
1773
1836
 
1774
- The result is `{ bazaar: { info, schema } }`: `info.input` (`type: "http"`, `method`, then `bodyType` and `body` for a body method, or `queryParams` for a query method, plus optional `headers`), `info.output` (`type`, default `json`, and `example`), and `schema`, the JSON Schema (draft 2020-12) that `info` validates against. `bodyType` is `json` (default), `form-data` or `text` (then `body` is a string). A body method without `body`, a query method with one, or a method the spec does not name (`CONNECT`, `OPTIONS`, ...) throws. `extensions` is a v2 field: `create402Response` throws if the response would be v1 rather than dropping it, and without `extensions` it returns exactly what it returned before.
1837
+ The result is `{ bazaar: { info, schema } }`: `info.input` (`type: "http"`, `method`, then `bodyType` and `body` for a body method, or `queryParams` for a query method, plus optional `headers`), `info.output` (`type`, default `json`, and `example`), and `schema`, the JSON Schema (draft 2020-12) that `info` validates against. `bodyType` is `json` (default), `form-data` or `text` (then `body` is a string). A body method without `body`, a query method with one, or a method the spec does not name (`CONNECT`, `OPTIONS`, ...) throws. `extensions` is a v2 field: `create402Response` throws if the response would be v1 rather than dropping it, and without `extensions` the body has no `extensions` key.
1775
1838
 
1776
1839
  ## x402 v2 requests (`buildVerifyRequestV2` / `buildSettleRequestV2`)
1777
1840
 
@@ -2103,8 +2166,8 @@ the authorization and reuse it after uncertainty. Payment confirmation does not
2103
2166
  prove merchant delivery. See [the receipt guide](docs/facilitator-receipts.md).
2104
2167
 
2105
2168
  Every `/verify` and `/settle` carries an `Idempotency-Key`, the same one for both
2106
- calls of a payment and for their retries: `verifyAndSettle` and both middlewares
2107
- create one per payment, and `verify`/`settle` accept `{ idempotencyKey }`
2169
+ calls of a payment and for their retries: `verifyAndSettle`, both middlewares and
2170
+ `createFetchPaywall` create one per payment, and `verify`/`settle` accept `{ idempotencyKey }`
2108
2171
  (`createIdempotencyKey()`). The facilitator returns an admitted payment's original
2109
2172
  answer (`SettleResponse.replayed`, from `Idempotent-Replayed: true`) only to that
2110
2173
  key or to the buyer's `X-UVD-Purchase`; a resend without them is
@@ -103,10 +103,12 @@ var SUPPORTED_CHAINS = {
103
103
  name: "USD Coin",
104
104
  version: "2"
105
105
  },
106
+ // name() on-chain is "Euro Coin" here and "EURC" on Base: pinned to each
107
+ // contract's DOMAIN_SEPARATOR() by src/eurc-domains.test.ts.
106
108
  eurc: {
107
109
  address: "0xC891EB4cbdEFf6e073e859e987815Ed1505c2ACD",
108
110
  decimals: 6,
109
- name: "EURC",
111
+ name: "Euro Coin",
110
112
  version: "2",
111
113
  usdPegged: false
112
114
  },