thirdweb 5.108.0-nightly-a94f22928a662a5aff7a203fc2d383d9fa0907ec-20250923000340 → 5.108.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 (47) hide show
  1. package/dist/cjs/exports/x402.js +3 -1
  2. package/dist/cjs/exports/x402.js.map +1 -1
  3. package/dist/cjs/version.js +1 -1
  4. package/dist/cjs/version.js.map +1 -1
  5. package/dist/cjs/x402/common.js +173 -0
  6. package/dist/cjs/x402/common.js.map +1 -0
  7. package/dist/cjs/x402/facilitator.js +1 -1
  8. package/dist/cjs/x402/settle-payment.js +174 -0
  9. package/dist/cjs/x402/settle-payment.js.map +1 -0
  10. package/dist/cjs/x402/types.js +5 -0
  11. package/dist/cjs/x402/types.js.map +1 -0
  12. package/dist/cjs/x402/verify-payment.js +35 -255
  13. package/dist/cjs/x402/verify-payment.js.map +1 -1
  14. package/dist/esm/exports/x402.js +2 -1
  15. package/dist/esm/exports/x402.js.map +1 -1
  16. package/dist/esm/version.js +1 -1
  17. package/dist/esm/version.js.map +1 -1
  18. package/dist/esm/x402/common.js +170 -0
  19. package/dist/esm/x402/common.js.map +1 -0
  20. package/dist/esm/x402/facilitator.js +1 -1
  21. package/dist/esm/x402/settle-payment.js +171 -0
  22. package/dist/esm/x402/settle-payment.js.map +1 -0
  23. package/dist/esm/x402/types.js +2 -0
  24. package/dist/esm/x402/types.js.map +1 -0
  25. package/dist/esm/x402/verify-payment.js +33 -253
  26. package/dist/esm/x402/verify-payment.js.map +1 -1
  27. package/dist/types/exports/x402.d.ts +3 -1
  28. package/dist/types/exports/x402.d.ts.map +1 -1
  29. package/dist/types/version.d.ts +1 -1
  30. package/dist/types/version.d.ts.map +1 -1
  31. package/dist/types/x402/common.d.ts +16 -0
  32. package/dist/types/x402/common.d.ts.map +1 -0
  33. package/dist/types/x402/facilitator.d.ts +1 -1
  34. package/dist/types/x402/settle-payment.d.ts +117 -0
  35. package/dist/types/x402/settle-payment.d.ts.map +1 -0
  36. package/dist/types/x402/types.d.ts +71 -0
  37. package/dist/types/x402/types.d.ts.map +1 -0
  38. package/dist/types/x402/verify-payment.d.ts +19 -108
  39. package/dist/types/x402/verify-payment.d.ts.map +1 -1
  40. package/package.json +3 -3
  41. package/src/exports/x402.ts +7 -5
  42. package/src/version.ts +1 -1
  43. package/src/x402/common.ts +242 -0
  44. package/src/x402/facilitator.ts +1 -1
  45. package/src/x402/settle-payment.ts +186 -0
  46. package/src/x402/types.ts +90 -0
  47. package/src/x402/verify-payment.ts +38 -374
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.verifyPayment = exports.wrapFetchWithPayment = exports.facilitator = exports.encodePayment = exports.decodePayment = void 0;
3
+ exports.verifyPayment = exports.settlePayment = exports.wrapFetchWithPayment = exports.facilitator = exports.encodePayment = exports.decodePayment = void 0;
4
4
  var encode_js_1 = require("../x402/encode.js");
5
5
  Object.defineProperty(exports, "decodePayment", { enumerable: true, get: function () { return encode_js_1.decodePayment; } });
6
6
  Object.defineProperty(exports, "encodePayment", { enumerable: true, get: function () { return encode_js_1.encodePayment; } });
@@ -8,6 +8,8 @@ var facilitator_js_1 = require("../x402/facilitator.js");
8
8
  Object.defineProperty(exports, "facilitator", { enumerable: true, get: function () { return facilitator_js_1.facilitator; } });
9
9
  var fetchWithPayment_js_1 = require("../x402/fetchWithPayment.js");
10
10
  Object.defineProperty(exports, "wrapFetchWithPayment", { enumerable: true, get: function () { return fetchWithPayment_js_1.wrapFetchWithPayment; } });
11
+ var settle_payment_js_1 = require("../x402/settle-payment.js");
12
+ Object.defineProperty(exports, "settlePayment", { enumerable: true, get: function () { return settle_payment_js_1.settlePayment; } });
11
13
  var verify_payment_js_1 = require("../x402/verify-payment.js");
12
14
  Object.defineProperty(exports, "verifyPayment", { enumerable: true, get: function () { return verify_payment_js_1.verifyPayment; } });
13
15
  //# sourceMappingURL=x402.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"x402.js","sourceRoot":"","sources":["../../../src/exports/x402.ts"],"names":[],"mappings":";;;AAAA,+CAAiE;AAAxD,0GAAA,aAAa,OAAA;AAAE,0GAAA,aAAa,OAAA;AACrC,yDAGgC;AAF9B,6GAAA,WAAW,OAAA;AAGb,mEAAmE;AAA1D,2HAAA,oBAAoB,OAAA;AAC7B,+DAImC;AADjC,kHAAA,aAAa,OAAA"}
1
+ {"version":3,"file":"x402.js","sourceRoot":"","sources":["../../../src/exports/x402.ts"],"names":[],"mappings":";;;AAAA,+CAAiE;AAAxD,0GAAA,aAAa,OAAA;AAAE,0GAAA,aAAa,OAAA;AACrC,yDAGgC;AAF9B,6GAAA,WAAW,OAAA;AAGb,mEAAmE;AAA1D,2HAAA,oBAAoB,OAAA;AAC7B,+DAA0D;AAAjD,kHAAA,aAAa,OAAA;AAMtB,+DAA0D;AAAjD,kHAAA,aAAa,OAAA"}
@@ -1,5 +1,5 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.version = void 0;
4
- exports.version = "5.108.0-nightly-a94f22928a662a5aff7a203fc2d383d9fa0907ec-20250923000340";
4
+ exports.version = "5.108.0";
5
5
  //# sourceMappingURL=version.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":";;;AAAa,QAAA,OAAO,GAAG,yEAAyE,CAAC"}
1
+ {"version":3,"file":"version.js","sourceRoot":"","sources":["../../src/version.ts"],"names":[],"mappings":";;;AAAa,QAAA,OAAO,GAAG,SAAS,CAAC"}
@@ -0,0 +1,173 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.decodePaymentRequest = decodePaymentRequest;
4
+ const types_1 = require("x402/types");
5
+ const address_js_1 = require("../utils/address.js");
6
+ const encode_js_1 = require("./encode.js");
7
+ const schemas_js_1 = require("./schemas.js");
8
+ const types_js_1 = require("./types.js");
9
+ /**
10
+ * Decodes a payment request and returns the payment requirements, selected payment requirements, and decoded payment
11
+ * @param args
12
+ * @returns The payment requirements, selected payment requirements, and decoded payment
13
+ */
14
+ async function decodePaymentRequest(args) {
15
+ const { price, network, facilitator, resourceUrl, routeConfig = {}, payTo, method, paymentData, } = args;
16
+ const { description, mimeType, maxTimeoutSeconds, inputSchema, outputSchema, errorMessages, discoverable, } = routeConfig;
17
+ const atomicAmountForAsset = await processPriceToAtomicAmount(price, network, facilitator);
18
+ if ("error" in atomicAmountForAsset) {
19
+ return {
20
+ status: 402,
21
+ responseHeaders: { "Content-Type": "application/json" },
22
+ responseBody: {
23
+ x402Version: types_js_1.x402Version,
24
+ error: atomicAmountForAsset.error,
25
+ accepts: [],
26
+ },
27
+ };
28
+ }
29
+ const { maxAmountRequired, asset } = atomicAmountForAsset;
30
+ const paymentRequirements = [];
31
+ if (types_1.SupportedEVMNetworks.includes(network) ||
32
+ network.startsWith("eip155:")) {
33
+ paymentRequirements.push({
34
+ scheme: "exact",
35
+ network,
36
+ maxAmountRequired,
37
+ resource: resourceUrl,
38
+ description: description ?? "",
39
+ mimeType: mimeType ?? "application/json",
40
+ payTo: (0, address_js_1.getAddress)(payTo),
41
+ maxTimeoutSeconds: maxTimeoutSeconds ?? 300,
42
+ asset: (0, address_js_1.getAddress)(asset.address),
43
+ // TODO: Rename outputSchema to requestStructure
44
+ outputSchema: {
45
+ input: {
46
+ type: "http",
47
+ method,
48
+ discoverable: discoverable ?? true,
49
+ ...inputSchema,
50
+ },
51
+ output: outputSchema,
52
+ },
53
+ extra: asset.eip712,
54
+ });
55
+ }
56
+ else {
57
+ return {
58
+ status: 402,
59
+ responseHeaders: {
60
+ "Content-Type": "application/json",
61
+ },
62
+ responseBody: {
63
+ x402Version: types_js_1.x402Version,
64
+ error: `Unsupported network: ${network}`,
65
+ accepts: paymentRequirements,
66
+ },
67
+ };
68
+ }
69
+ // Check for payment header
70
+ if (!paymentData) {
71
+ return {
72
+ status: 402,
73
+ responseHeaders: {
74
+ "Content-Type": "application/json",
75
+ },
76
+ responseBody: {
77
+ x402Version: types_js_1.x402Version,
78
+ error: errorMessages?.paymentRequired || "X-PAYMENT header is required",
79
+ accepts: paymentRequirements,
80
+ },
81
+ };
82
+ }
83
+ // Verify payment
84
+ let decodedPayment;
85
+ try {
86
+ decodedPayment = (0, encode_js_1.decodePayment)(paymentData);
87
+ decodedPayment.x402Version = types_js_1.x402Version;
88
+ }
89
+ catch (error) {
90
+ return {
91
+ status: 402,
92
+ responseHeaders: {
93
+ "Content-Type": "application/json",
94
+ },
95
+ responseBody: {
96
+ x402Version: types_js_1.x402Version,
97
+ error: errorMessages?.invalidPayment ||
98
+ (error instanceof Error ? error.message : "Invalid payment"),
99
+ accepts: paymentRequirements,
100
+ },
101
+ };
102
+ }
103
+ const selectedPaymentRequirements = paymentRequirements.find((value) => value.scheme === decodedPayment.scheme &&
104
+ value.network === decodedPayment.network);
105
+ if (!selectedPaymentRequirements) {
106
+ return {
107
+ status: 402,
108
+ responseHeaders: {
109
+ "Content-Type": "application/json",
110
+ },
111
+ responseBody: {
112
+ x402Version: types_js_1.x402Version,
113
+ error: errorMessages?.noMatchingRequirements ||
114
+ "Unable to find matching payment requirements",
115
+ accepts: paymentRequirements,
116
+ },
117
+ };
118
+ }
119
+ return {
120
+ status: 200,
121
+ paymentRequirements,
122
+ decodedPayment,
123
+ selectedPaymentRequirements,
124
+ };
125
+ }
126
+ /**
127
+ * Parses the amount from the given price
128
+ *
129
+ * @param price - The price to parse
130
+ * @param network - The network to get the default asset for
131
+ * @returns The parsed amount or an error message
132
+ */
133
+ async function processPriceToAtomicAmount(price, network, facilitator) {
134
+ // Handle USDC amount (string) or token amount (ERC20TokenAmount)
135
+ let maxAmountRequired;
136
+ let asset;
137
+ if (typeof price === "string" || typeof price === "number") {
138
+ // USDC amount in dollars
139
+ const parsedAmount = types_1.moneySchema.safeParse(price);
140
+ if (!parsedAmount.success) {
141
+ return {
142
+ error: `Invalid price (price: ${price}). Must be in the form "$3.10", 0.10, "0.001", ${parsedAmount.error}`,
143
+ };
144
+ }
145
+ const parsedUsdAmount = parsedAmount.data;
146
+ const defaultAsset = await getDefaultAsset(network, facilitator);
147
+ if (!defaultAsset) {
148
+ return {
149
+ error: `Unable to get default asset on ${network}. Please specify an asset in the payment requirements.`,
150
+ };
151
+ }
152
+ asset = defaultAsset;
153
+ maxAmountRequired = (parsedUsdAmount * 10 ** asset.decimals).toString();
154
+ }
155
+ else {
156
+ // Token amount in atomic units
157
+ maxAmountRequired = price.amount;
158
+ asset = price.asset;
159
+ }
160
+ return {
161
+ maxAmountRequired,
162
+ asset,
163
+ };
164
+ }
165
+ async function getDefaultAsset(network, facilitator) {
166
+ const supportedAssets = await facilitator.supported();
167
+ const chainId = (0, schemas_js_1.networkToChainId)(network);
168
+ const matchingAsset = supportedAssets.kinds.find((supported) => supported.network === `eip155:${chainId}`);
169
+ const assetConfig = matchingAsset?.extra
170
+ ?.defaultAsset;
171
+ return assetConfig;
172
+ }
173
+ //# sourceMappingURL=common.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"common.js","sourceRoot":"","sources":["../../../src/x402/common.ts"],"names":[],"mappings":";;AAkCA,oDAiJC;AAnLD,sCAMoB;AACpB,oDAAiD;AACjD,2CAA4C;AAE5C,6CAKsB;AACtB,yCAIoB;AASpB;;;;GAIG;AACI,KAAK,UAAU,oBAAoB,CACxC,IAAiB;IAEjB,MAAM,EACJ,KAAK,EACL,OAAO,EACP,WAAW,EACX,WAAW,EACX,WAAW,GAAG,EAAE,EAChB,KAAK,EACL,MAAM,EACN,WAAW,GACZ,GAAG,IAAI,CAAC;IACT,MAAM,EACJ,WAAW,EACX,QAAQ,EACR,iBAAiB,EACjB,WAAW,EACX,YAAY,EACZ,aAAa,EACb,YAAY,GACb,GAAG,WAAW,CAAC;IAChB,MAAM,oBAAoB,GAAG,MAAM,0BAA0B,CAC3D,KAAK,EACL,OAAO,EACP,WAAW,CACZ,CAAC;IACF,IAAI,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Ae,CAC5B,OAA2B,EAC3B,WAA+C;IAE/C,MAAM,eAAe,GAAG,MAAM,WAAW,CAAC,SAAS,EAAE,CAAC;IACtD,MAAM,OAAO,GAAG,IAAA,6BAAgB,EAAC,OAAO,CAAC,CAAC;IAC1C,MAAM,aAAa,GAAG,eAAe,CAAC,KAAK,CAAC,IAAI,CAC9C,CAAC,SAAS,EAAE,EAAE,CAAC,SAAS,CAAC,OAAO,KAAK,UAAU,OAAO,EAAE,CACzD,CAAC;IACF,MAAM,WAAW,GAAG,aAAa,EAAE,KAAK;QACtC,EAAE,YAAyC,CAAC;IAC9C,OAAO,WAAW,CAAC;AACrB,CAAC"}
@@ -6,7 +6,7 @@ const withCache_js_1 = require("../utils/promise/withCache.js");
6
6
  const DEFAULT_BASE_URL = "https://api.thirdweb.com/v1/payments/x402";
7
7
  /**
8
8
  * Creates a facilitator for the x402 payment protocol.
9
- * You can use this with `verifyPayment` or with any x402 middleware to enable settling transactions with your thirdweb server wallet.
9
+ * You can use this with `settlePayment` or with any x402 middleware to enable settling transactions with your thirdweb server wallet.
10
10
  *
11
11
  * @param config - The configuration for the facilitator
12
12
  * @returns a x402 compatible FacilitatorConfig
@@ -0,0 +1,174 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.settlePayment = settlePayment;
4
+ const json_js_1 = require("../utils/json.js");
5
+ const common_js_1 = require("./common.js");
6
+ const encode_js_1 = require("./encode.js");
7
+ const types_js_1 = require("./types.js");
8
+ /**
9
+ * Verifies and processes X402 payments for protected resources.
10
+ *
11
+ * This function implements the X402 payment protocol, verifying payment proofs
12
+ * and settling payments through a facilitator service. It handles the complete
13
+ * payment flow from validation to settlement.
14
+ *
15
+ * @param args - Configuration object containing payment verification parameters
16
+ * @returns A promise that resolves to either a successful payment result (200) or payment required error (402)
17
+ *
18
+ * @example
19
+ *
20
+ * ### Next.js API route example
21
+ *
22
+ * ```ts
23
+ * // Usage in a Next.js API route
24
+ * import { settlePayment, facilitator } from "thirdweb/x402";
25
+ * import { createThirdwebClient } from "thirdweb";
26
+ *
27
+ * const client = createThirdwebClient({
28
+ * secretKey: process.env.THIRDWEB_SECRET_KEY,
29
+ * });
30
+ *
31
+ * const thirdwebFacilitator = facilitator({
32
+ * client,
33
+ * serverWalletAddress: "0x1234567890123456789012345678901234567890",
34
+ * });
35
+ *
36
+ * export async function GET(request: Request) {
37
+ * const paymentData = request.headers.get("x-payment");
38
+ *
39
+ * // verify and process the payment
40
+ * const result = await settlePayment({
41
+ * resourceUrl: "https://api.example.com/premium-content",
42
+ * method: "GET",
43
+ * paymentData,
44
+ * payTo: "0x1234567890123456789012345678901234567890",
45
+ * network: "eip155:84532", // CAIP2 format: "eip155:<chain_id>"
46
+ * price: "$0.10", // or { amount: "100000", asset: { address: "0x...", decimals: 6 } }
47
+ * facilitator: thirdwebFacilitator,
48
+ * routeConfig: {
49
+ * description: "Access to premium API content",
50
+ * mimeType: "application/json",
51
+ * maxTimeoutSeconds: 300,
52
+ * },
53
+ * });
54
+ *
55
+ * if (result.status === 200) {
56
+ * // Payment verified and settled successfully
57
+ * return Response.json({ data: "premium content" });
58
+ * } else {
59
+ * // Payment required
60
+ * return Response.json(result.responseBody, {
61
+ * status: result.status,
62
+ * headers: result.responseHeaders,
63
+ * });
64
+ * }
65
+ * }
66
+ * ```
67
+ *
68
+ * ### Express middleware example
69
+ *
70
+ * ```ts
71
+ * // Usage in Express middleware
72
+ * import express from "express";
73
+ * import { settlePayment, facilitator } from "thirdweb/x402";
74
+ * import { createThirdwebClient } from "thirdweb";
75
+ *
76
+ * const client = createThirdwebClient({
77
+ * secretKey: process.env.THIRDWEB_SECRET_KEY,
78
+ * });
79
+ *
80
+ * const thirdwebFacilitator = facilitator({
81
+ * client,
82
+ * serverWalletAddress: "0x1234567890123456789012345678901234567890",
83
+ * });
84
+ *
85
+ * const app = express();
86
+ *
87
+ * async function paymentMiddleware(req, res, next) {
88
+ * // verify and process the payment
89
+ * const result = await settlePayment({
90
+ * resourceUrl: `${req.protocol}://${req.get('host')}${req.originalUrl}`,
91
+ * method: req.method,
92
+ * paymentData: req.headers["x-payment"],
93
+ * payTo: "0x1234567890123456789012345678901234567890",
94
+ * network: "eip155:8453", // CAIP2 format: "eip155:<chain_id>"
95
+ * price: "$0.05",
96
+ * facilitator: thirdwebFacilitator,
97
+ * });
98
+ *
99
+ * if (result.status === 200) {
100
+ * // Set payment receipt headers and continue
101
+ * Object.entries(result.responseHeaders).forEach(([key, value]) => {
102
+ * res.setHeader(key, value);
103
+ * });
104
+ * next();
105
+ * } else {
106
+ * // Return payment required response
107
+ * res.status(result.status)
108
+ * .set(result.responseHeaders)
109
+ * .json(result.responseBody);
110
+ * }
111
+ * }
112
+ *
113
+ * app.get("/api/premium", paymentMiddleware, (req, res) => {
114
+ * res.json({ message: "This is premium content!" });
115
+ * });
116
+ * ```
117
+ *
118
+ * @public
119
+ * @beta
120
+ * @bridge x402
121
+ */
122
+ async function settlePayment(args) {
123
+ const { routeConfig = {}, facilitator } = args;
124
+ const { errorMessages } = routeConfig;
125
+ const decodePaymentResult = await (0, common_js_1.decodePaymentRequest)(args);
126
+ if (decodePaymentResult.status !== 200) {
127
+ return decodePaymentResult;
128
+ }
129
+ const { selectedPaymentRequirements, decodedPayment, paymentRequirements } = decodePaymentResult;
130
+ // Settle payment
131
+ try {
132
+ const settlement = await facilitator.settle(decodedPayment, selectedPaymentRequirements);
133
+ if (settlement.success) {
134
+ return {
135
+ status: 200,
136
+ paymentReceipt: settlement,
137
+ responseHeaders: {
138
+ "Access-Control-Expose-Headers": "X-PAYMENT-RESPONSE",
139
+ "X-PAYMENT-RESPONSE": (0, encode_js_1.safeBase64Encode)((0, json_js_1.stringify)(settlement)),
140
+ },
141
+ };
142
+ }
143
+ else {
144
+ return {
145
+ status: 402,
146
+ responseHeaders: {
147
+ "Content-Type": "application/json",
148
+ },
149
+ responseBody: {
150
+ x402Version: types_js_1.x402Version,
151
+ error: errorMessages?.settlementFailed ||
152
+ settlement.errorReason ||
153
+ "Settlement failed",
154
+ accepts: paymentRequirements,
155
+ },
156
+ };
157
+ }
158
+ }
159
+ catch (error) {
160
+ return {
161
+ status: 402,
162
+ responseHeaders: {
163
+ "Content-Type": "application/json",
164
+ },
165
+ responseBody: {
166
+ x402Version: types_js_1.x402Version,
167
+ error: errorMessages?.settlementFailed ||
168
+ (error instanceof Error ? error.message : "Settlement error"),
169
+ accepts: paymentRequirements,
170
+ },
171
+ };
172
+ }
173
+ }
174
+ //# sourceMappingURL=settle-payment.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"settle-payment.js","sourceRoot":"","sources":["../../../src/x402/settle-payment.ts"],"names":[],"mappings":";;AA2HA,sCA8DC;AAzLD,8CAA6C;AAC7C,2CAAmD;AACnD,2CAA+C;AAC/C,yCAIoB;AAEpB;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GAiHG;AACI,KAAK,UAAU,aAAa,CACjC,IAAiB;IAEjB,MAAM,EAAE,WAAW,GAAG,EAAE,EAAE,WAAW,EAAE,GAAG,IAAI,CAAC;IAC/C,MAAM,EAAE,aAAa,EAAE,GAAG,WAAW,CAAC;IAEtC,MAAM,mBAAmB,GAAG,MAAM,IAAA,gCAAoB,EAAC,IAAI,CAAC,CAAC;IAE7D,IAAI,mBAAmB,CAAC,MAAM,KAAK,GAAG,EAAE,CAAC;QACvC,OAAO,mBAAmB,CAAC;IAC7B,CAAC;IAED,MAAM,EAAE,2BAA2B,EAAE,cAAc,EAAE,mBAAmB,EAAE,GACxE,mBAAmB,CAAC;IAEtB,iBAAiB;IACjB,IAAI,CAAC;QACH,MAAM,UAAU,GAAG,MAAM,WAAW,CAAC,MAAM,CACzC,cAAc,EACd,2BAA2B,CAC5B,CAAC;QAEF,IAAI,UAAU,CAAC,OAAO,EAAE,CAAC;YACvB,OAAO;gBACL,MAAM,EAAE,GAAG;gBACX,cAAc,EAAE,UAAU;gBAC1B,eAAe,EAAE;oBACf,+BAA+B,EAAE,oBAAoB;oBACrD,oBAAoB,EAAE,IAAA,4BAAgB,EAAC,IAAA,mBAAS,EAAC,UAAU,CAAC,CAAC;iBAC9D;aACF,CAAC;QACJ,CAAC;aAAM,CAAC;YACN,OAAO;gBACL,MAAM,EAAE,GAAG;gBACX,eAAe,EAAE;oBACf,cAAc,EAAE,kBAAkB;iBACnC;gBACD,YAAY,EAAE;oBACZ,WAAW,EAAX,sBAAW;oBACX,KAAK,EACH,aAAa,EAAE,gBAAgB;wBAC/B,UAAU,CAAC,WAAW;wBACtB,mBAAmB;oBACrB,OAAO,EAAE,mBAAmB;iBAC7B;aACF,CAAC;QACJ,CAAC;IACH,CAAC;IAAC,OAAO,KAAK,EAAE,CAAC;QACf,OAAO;YACL,MAAM,EAAE,GAAG;YACX,eAAe,EAAE;gBACf,cAAc,EAAE,kBAAkB;aACnC;YACD,YAAY,EAAE;gBACZ,WAAW,EAAX,sBAAW;gBACX,KAAK,EACH,aAAa,EAAE,gBAAgB;oBAC/B,CAAC,KAAK,YAAY,KAAK,CAAC,CAAC,CAAC,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,kBAAkB,CAAC;gBAC/D,OAAO,EAAE,mBAAmB;aAC7B;SACF,CAAC;IACJ,CAAC;AACH,CAAC"}
@@ -0,0 +1,5 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.x402Version = void 0;
4
+ exports.x402Version = 1;
5
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../../../src/x402/types.ts"],"names":[],"mappings":";;;AAea,QAAA,WAAW,GAAG,CAAC,CAAC"}