@x402/extensions 2.4.0 → 2.5.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.
@@ -131,4 +131,230 @@ declare function extractEip2612GasSponsoringInfo(paymentPayload: PaymentPayload)
131
131
  */
132
132
  declare function validateEip2612GasSponsoringInfo(info: Eip2612GasSponsoringInfo): boolean;
133
133
 
134
- export { EIP2612_GAS_SPONSORING, type Eip2612GasSponsoringExtension, type Eip2612GasSponsoringInfo, type Eip2612GasSponsoringServerInfo, declareEip2612GasSponsoringExtension, extractEip2612GasSponsoringInfo, validateEip2612GasSponsoringInfo };
134
+ /**
135
+ * Type definitions for the ERC-20 Approval Gas Sponsoring Extension
136
+ *
137
+ * This extension enables gasless Permit2 approval for generic ERC-20 tokens
138
+ * that do NOT implement EIP-2612. The client signs (but does not broadcast) a
139
+ * raw `approve(Permit2, MaxUint256)` transaction, and the facilitator broadcasts
140
+ * it atomically before settling the Permit2 payment.
141
+ */
142
+
143
+ /**
144
+ * Signer capability carried by the ERC-20 approval extension when registered in a facilitator.
145
+ *
146
+ * Mirrors FacilitatorEvmSigner (from @x402/evm) plus `sendRawTransaction`.
147
+ * The extension signer owns the full approve+settle flow: it broadcasts the
148
+ * pre-signed approval transaction AND executes the Permit2 settle call, enabling
149
+ * production implementations to bundle both atomically (e.g., Flashbots, multicall).
150
+ *
151
+ * The method signatures are duplicated here (rather than extending FacilitatorEvmSigner)
152
+ * to avoid a circular dependency between @x402/extensions and @x402/evm.
153
+ */
154
+ interface Erc20ApprovalGasSponsoringSigner {
155
+ getAddresses(): readonly `0x${string}`[];
156
+ readContract(args: {
157
+ address: `0x${string}`;
158
+ abi: readonly unknown[];
159
+ functionName: string;
160
+ args?: readonly unknown[];
161
+ }): Promise<unknown>;
162
+ verifyTypedData(args: {
163
+ address: `0x${string}`;
164
+ domain: Record<string, unknown>;
165
+ types: Record<string, unknown>;
166
+ primaryType: string;
167
+ message: Record<string, unknown>;
168
+ signature: `0x${string}`;
169
+ }): Promise<boolean>;
170
+ writeContract(args: {
171
+ address: `0x${string}`;
172
+ abi: readonly unknown[];
173
+ functionName: string;
174
+ args: readonly unknown[];
175
+ }): Promise<`0x${string}`>;
176
+ sendTransaction(args: {
177
+ to: `0x${string}`;
178
+ data: `0x${string}`;
179
+ }): Promise<`0x${string}`>;
180
+ waitForTransactionReceipt(args: {
181
+ hash: `0x${string}`;
182
+ }): Promise<{
183
+ status: string;
184
+ }>;
185
+ getCode(args: {
186
+ address: `0x${string}`;
187
+ }): Promise<`0x${string}` | undefined>;
188
+ sendRawTransaction(args: {
189
+ serializedTransaction: `0x${string}`;
190
+ }): Promise<`0x${string}`>;
191
+ }
192
+ /**
193
+ * Extension identifier for the ERC-20 approval gas sponsoring extension.
194
+ */
195
+ declare const ERC20_APPROVAL_GAS_SPONSORING: {
196
+ readonly key: "erc20ApprovalGasSponsoring";
197
+ };
198
+ /** Current schema version for the ERC-20 approval gas sponsoring extension info. */
199
+ declare const ERC20_APPROVAL_GAS_SPONSORING_VERSION = "1";
200
+ /**
201
+ * Extended extension object registered in a facilitator via registerExtension().
202
+ * Carries the signer that owns the full approve+settle flow for ERC-20 tokens
203
+ * that lack EIP-2612. The signer must have all FacilitatorEvmSigner capabilities
204
+ * plus `sendRawTransaction` for broadcasting the pre-signed approval tx.
205
+ *
206
+ * @example
207
+ * ```typescript
208
+ * import { createErc20ApprovalGasSponsoringExtension } from '@x402/extensions';
209
+ *
210
+ * facilitator.registerExtension(
211
+ * createErc20ApprovalGasSponsoringExtension(evmSigner, viemClient),
212
+ * );
213
+ * ```
214
+ */
215
+ interface Erc20ApprovalGasSponsoringFacilitatorExtension extends FacilitatorExtension {
216
+ key: "erc20ApprovalGasSponsoring";
217
+ /** Signer with broadcast + settle capability. Optional — settlement fails gracefully if absent. */
218
+ signer?: Erc20ApprovalGasSponsoringSigner;
219
+ }
220
+ /**
221
+ * Signer input for {@link createErc20ApprovalGasSponsoringExtension}.
222
+ * Matches the FacilitatorEvmSigner shape from @x402/evm (duplicated to avoid circular dep).
223
+ */
224
+ type Erc20ApprovalGasSponsoringBaseSigner = Omit<Erc20ApprovalGasSponsoringSigner, "sendRawTransaction">;
225
+ /**
226
+ * Create an ERC-20 approval gas sponsoring extension ready to register in a facilitator.
227
+ *
228
+ * @param signer - The EVM facilitator signer (e.g. from `toFacilitatorEvmSigner()`)
229
+ * @param client - Object providing `sendRawTransaction` (e.g. a viem WalletClient)
230
+ * @param client.sendRawTransaction - Broadcasts a signed transaction to the network
231
+ * @returns A fully configured extension to pass to `facilitator.registerExtension()`
232
+ */
233
+ declare function createErc20ApprovalGasSponsoringExtension(signer: Erc20ApprovalGasSponsoringBaseSigner, client: {
234
+ sendRawTransaction: (args: {
235
+ serializedTransaction: `0x${string}`;
236
+ }) => Promise<`0x${string}`>;
237
+ }): Erc20ApprovalGasSponsoringFacilitatorExtension;
238
+ /**
239
+ * ERC-20 approval gas sponsoring info populated by the client.
240
+ *
241
+ * Contains the RLP-encoded signed `approve(Permit2, MaxUint256)` transaction
242
+ * that the facilitator broadcasts before settling the Permit2 payment.
243
+ *
244
+ * Note: Unlike EIP-2612, there is no nonce/deadline/signature — instead the
245
+ * entire signed transaction is included as `signedTransaction`.
246
+ */
247
+ interface Erc20ApprovalGasSponsoringInfo {
248
+ /** Index signature for compatibility with Record<string, unknown> */
249
+ [key: string]: unknown;
250
+ /** The address of the sender (token owner who signed the tx). */
251
+ from: `0x${string}`;
252
+ /** The address of the ERC-20 token contract. */
253
+ asset: `0x${string}`;
254
+ /** The address of the spender (Canonical Permit2). */
255
+ spender: `0x${string}`;
256
+ /** The amount approved (uint256 as decimal string). Always MaxUint256. */
257
+ amount: string;
258
+ /** The RLP-encoded signed EIP-1559 transaction as a hex string. */
259
+ signedTransaction: `0x${string}`;
260
+ /** Schema version identifier. */
261
+ version: string;
262
+ }
263
+ /**
264
+ * Server-side ERC-20 approval gas sponsoring info included in PaymentRequired.
265
+ * Contains a description and version; the client populates the rest.
266
+ */
267
+ interface Erc20ApprovalGasSponsoringServerInfo {
268
+ /** Index signature for compatibility with Record<string, unknown> */
269
+ [key: string]: unknown;
270
+ /** Human-readable description of the extension. */
271
+ description: string;
272
+ /** Schema version identifier. */
273
+ version: string;
274
+ }
275
+ /**
276
+ * The full extension object as it appears in PaymentRequired.extensions
277
+ * and PaymentPayload.extensions.
278
+ */
279
+ interface Erc20ApprovalGasSponsoringExtension {
280
+ /** Extension info - server-provided or client-enriched. */
281
+ info: Erc20ApprovalGasSponsoringServerInfo | Erc20ApprovalGasSponsoringInfo;
282
+ /** JSON Schema describing the expected structure of info. */
283
+ schema: Record<string, unknown>;
284
+ }
285
+
286
+ /**
287
+ * Resource Service functions for declaring the ERC-20 Approval Gas Sponsoring extension.
288
+ *
289
+ * These functions help servers declare support for ERC-20 approval gas sponsoring
290
+ * in the PaymentRequired response extensions. Use this for tokens that do NOT
291
+ * implement EIP-2612 (generic ERC-20 tokens).
292
+ */
293
+
294
+ /**
295
+ * The JSON Schema for the ERC-20 approval gas sponsoring extension info.
296
+ * Matches the schema defined in the spec.
297
+ */
298
+ declare const erc20ApprovalGasSponsoringSchema: Record<string, unknown>;
299
+ /**
300
+ * Declares the ERC-20 approval gas sponsoring extension for inclusion in
301
+ * PaymentRequired.extensions.
302
+ *
303
+ * The server advertises that it (or its facilitator) supports broadcasting
304
+ * a pre-signed `approve(Permit2, MaxUint256)` transaction on the client's behalf.
305
+ * Use this for tokens that do NOT implement EIP-2612.
306
+ *
307
+ * @returns An object keyed by the extension identifier containing the extension declaration
308
+ *
309
+ * @example
310
+ * ```typescript
311
+ * import { declareErc20ApprovalGasSponsoringExtension } from '@x402/extensions';
312
+ *
313
+ * const routes = [
314
+ * {
315
+ * path: "/api/data",
316
+ * price: { amount: "1000", asset: "0x...", extra: { assetTransferMethod: "permit2" } },
317
+ * extensions: {
318
+ * ...declareErc20ApprovalGasSponsoringExtension(),
319
+ * },
320
+ * },
321
+ * ];
322
+ * ```
323
+ */
324
+ declare function declareErc20ApprovalGasSponsoringExtension(): Record<string, Erc20ApprovalGasSponsoringExtension>;
325
+
326
+ /**
327
+ * Facilitator functions for extracting and validating ERC-20 Approval Gas Sponsoring
328
+ * extension data.
329
+ *
330
+ * These functions help facilitators extract the pre-signed approve() transaction
331
+ * from payment payloads and validate it before broadcasting and settling.
332
+ */
333
+
334
+ /**
335
+ * Extracts the ERC-20 approval gas sponsoring info from a payment payload's extensions.
336
+ *
337
+ * Performs structural extraction only — checks that the extension is present and
338
+ * contains all required fields. Does NOT validate field formats (use
339
+ * validateErc20ApprovalGasSponsoringInfo for that).
340
+ *
341
+ * @param paymentPayload - The payment payload to extract from
342
+ * @returns The ERC-20 approval gas sponsoring info, or null if not present
343
+ */
344
+ declare function extractErc20ApprovalGasSponsoringInfo(paymentPayload: PaymentPayload): Erc20ApprovalGasSponsoringInfo | null;
345
+ /**
346
+ * Validates that the ERC-20 approval gas sponsoring info has valid format.
347
+ *
348
+ * Validates the info against the canonical JSON Schema, checking:
349
+ * - All required fields are present
350
+ * - Addresses are valid hex (0x + 40 hex chars)
351
+ * - Amount is a numeric string
352
+ * - signedTransaction is a hex string
353
+ * - Version is a numeric version string
354
+ *
355
+ * @param info - The ERC-20 approval gas sponsoring info to validate
356
+ * @returns True if the info is valid, false otherwise
357
+ */
358
+ declare function validateErc20ApprovalGasSponsoringInfo(info: Erc20ApprovalGasSponsoringInfo): boolean;
359
+
360
+ export { EIP2612_GAS_SPONSORING, ERC20_APPROVAL_GAS_SPONSORING, ERC20_APPROVAL_GAS_SPONSORING_VERSION, type Eip2612GasSponsoringExtension, type Eip2612GasSponsoringInfo, type Eip2612GasSponsoringServerInfo, type Erc20ApprovalGasSponsoringBaseSigner, type Erc20ApprovalGasSponsoringExtension, type Erc20ApprovalGasSponsoringFacilitatorExtension, type Erc20ApprovalGasSponsoringInfo, type Erc20ApprovalGasSponsoringServerInfo, type Erc20ApprovalGasSponsoringSigner, createErc20ApprovalGasSponsoringExtension, declareEip2612GasSponsoringExtension, declareErc20ApprovalGasSponsoringExtension, erc20ApprovalGasSponsoringSchema, extractEip2612GasSponsoringInfo, extractErc20ApprovalGasSponsoringInfo, validateEip2612GasSponsoringInfo, validateErc20ApprovalGasSponsoringInfo };
package/dist/cjs/index.js CHANGED
@@ -32,6 +32,8 @@ var src_exports = {};
32
32
  __export(src_exports, {
33
33
  BAZAAR: () => BAZAAR,
34
34
  EIP2612_GAS_SPONSORING: () => EIP2612_GAS_SPONSORING,
35
+ ERC20_APPROVAL_GAS_SPONSORING: () => ERC20_APPROVAL_GAS_SPONSORING,
36
+ ERC20_APPROVAL_GAS_SPONSORING_VERSION: () => ERC20_APPROVAL_GAS_SPONSORING_VERSION,
35
37
  InMemorySIWxStorage: () => InMemorySIWxStorage,
36
38
  PAYMENT_IDENTIFIER: () => PAYMENT_IDENTIFIER,
37
39
  PAYMENT_ID_MAX_LENGTH: () => PAYMENT_ID_MAX_LENGTH,
@@ -45,6 +47,7 @@ __export(src_exports, {
45
47
  appendPaymentIdentifierToExtensions: () => appendPaymentIdentifierToExtensions,
46
48
  bazaarResourceServerExtension: () => bazaarResourceServerExtension,
47
49
  buildSIWxSchema: () => buildSIWxSchema,
50
+ createErc20ApprovalGasSponsoringExtension: () => createErc20ApprovalGasSponsoringExtension,
48
51
  createSIWxClientHook: () => createSIWxClientHook,
49
52
  createSIWxMessage: () => createSIWxMessage,
50
53
  createSIWxPayload: () => createSIWxPayload,
@@ -52,17 +55,20 @@ __export(src_exports, {
52
55
  createSIWxSettleHook: () => createSIWxSettleHook,
53
56
  declareDiscoveryExtension: () => declareDiscoveryExtension,
54
57
  declareEip2612GasSponsoringExtension: () => declareEip2612GasSponsoringExtension,
58
+ declareErc20ApprovalGasSponsoringExtension: () => declareErc20ApprovalGasSponsoringExtension,
55
59
  declarePaymentIdentifierExtension: () => declarePaymentIdentifierExtension,
56
60
  declareSIWxExtension: () => declareSIWxExtension,
57
61
  decodeBase58: () => decodeBase58,
58
62
  encodeBase58: () => encodeBase58,
59
63
  encodeSIWxHeader: () => encodeSIWxHeader,
64
+ erc20ApprovalGasSponsoringSchema: () => erc20ApprovalGasSponsoringSchema,
60
65
  extractAndValidatePaymentIdentifier: () => extractAndValidatePaymentIdentifier,
61
66
  extractDiscoveryInfo: () => extractDiscoveryInfo,
62
67
  extractDiscoveryInfoFromExtension: () => extractDiscoveryInfoFromExtension,
63
68
  extractDiscoveryInfoV1: () => extractDiscoveryInfoV1,
64
69
  extractEVMChainId: () => extractEVMChainId,
65
70
  extractEip2612GasSponsoringInfo: () => extractEip2612GasSponsoringInfo,
71
+ extractErc20ApprovalGasSponsoringInfo: () => extractErc20ApprovalGasSponsoringInfo,
66
72
  extractPaymentIdentifier: () => extractPaymentIdentifier,
67
73
  extractResourceMetadataV1: () => extractResourceMetadataV1,
68
74
  extractSolanaChainReference: () => extractSolanaChainReference,
@@ -90,6 +96,7 @@ __export(src_exports, {
90
96
  validateAndExtract: () => validateAndExtract,
91
97
  validateDiscoveryExtension: () => validateDiscoveryExtension,
92
98
  validateEip2612GasSponsoringInfo: () => validateEip2612GasSponsoringInfo,
99
+ validateErc20ApprovalGasSponsoringInfo: () => validateErc20ApprovalGasSponsoringInfo,
93
100
  validatePaymentIdentifier: () => validatePaymentIdentifier,
94
101
  validatePaymentIdentifierRequirement: () => validatePaymentIdentifierRequirement,
95
102
  validateSIWxMessage: () => validateSIWxMessage,
@@ -1318,7 +1325,9 @@ function createSIWxSettleHook(options) {
1318
1325
  if (!ctx.result.success) return;
1319
1326
  const address = ctx.result.payer;
1320
1327
  if (!address) return;
1321
- const resource = new URL(ctx.paymentPayload.resource.url).pathname;
1328
+ const resourceUrl = ctx.paymentPayload.resource?.url;
1329
+ if (!resourceUrl) return;
1330
+ const resource = new URL(resourceUrl).pathname;
1322
1331
  await storage.recordPayment(resource, address);
1323
1332
  onEvent?.({ type: "payment_recorded", resource, address });
1324
1333
  };
@@ -1702,10 +1711,100 @@ function validateEip2612GasSponsoringInfo(info) {
1702
1711
  const versionPattern = /^[0-9]+(\.[0-9]+)*$/;
1703
1712
  return addressPattern.test(info.from) && addressPattern.test(info.asset) && addressPattern.test(info.spender) && numericPattern.test(info.amount) && numericPattern.test(info.nonce) && numericPattern.test(info.deadline) && hexPattern.test(info.signature) && versionPattern.test(info.version);
1704
1713
  }
1714
+
1715
+ // src/erc20-approval-gas-sponsoring/types.ts
1716
+ var ERC20_APPROVAL_GAS_SPONSORING = {
1717
+ key: "erc20ApprovalGasSponsoring"
1718
+ };
1719
+ var ERC20_APPROVAL_GAS_SPONSORING_VERSION = "1";
1720
+ function createErc20ApprovalGasSponsoringExtension(signer, client) {
1721
+ return {
1722
+ ...ERC20_APPROVAL_GAS_SPONSORING,
1723
+ signer: {
1724
+ ...signer,
1725
+ sendRawTransaction: client.sendRawTransaction.bind(client)
1726
+ }
1727
+ };
1728
+ }
1729
+
1730
+ // src/erc20-approval-gas-sponsoring/resourceService.ts
1731
+ var erc20ApprovalGasSponsoringSchema = {
1732
+ $schema: "https://json-schema.org/draft/2020-12/schema",
1733
+ type: "object",
1734
+ properties: {
1735
+ from: {
1736
+ type: "string",
1737
+ pattern: "^0x[a-fA-F0-9]{40}$",
1738
+ description: "The address of the sender (token owner)."
1739
+ },
1740
+ asset: {
1741
+ type: "string",
1742
+ pattern: "^0x[a-fA-F0-9]{40}$",
1743
+ description: "The address of the ERC-20 token contract."
1744
+ },
1745
+ spender: {
1746
+ type: "string",
1747
+ pattern: "^0x[a-fA-F0-9]{40}$",
1748
+ description: "The address of the spender (Canonical Permit2)."
1749
+ },
1750
+ amount: {
1751
+ type: "string",
1752
+ pattern: "^[0-9]+$",
1753
+ description: "The amount approved (uint256). Always MaxUint256."
1754
+ },
1755
+ signedTransaction: {
1756
+ type: "string",
1757
+ pattern: "^0x[a-fA-F0-9]+$",
1758
+ description: "The RLP-encoded signed EIP-1559 transaction as a hex string."
1759
+ },
1760
+ version: {
1761
+ type: "string",
1762
+ pattern: "^[0-9]+(\\.[0-9]+)*$",
1763
+ description: "Schema version identifier."
1764
+ }
1765
+ },
1766
+ required: ["from", "asset", "spender", "amount", "signedTransaction", "version"]
1767
+ };
1768
+ function declareErc20ApprovalGasSponsoringExtension() {
1769
+ const key = ERC20_APPROVAL_GAS_SPONSORING.key;
1770
+ return {
1771
+ [key]: {
1772
+ info: {
1773
+ description: "The facilitator broadcasts a pre-signed ERC-20 approve() transaction to grant Permit2 allowance.",
1774
+ version: ERC20_APPROVAL_GAS_SPONSORING_VERSION
1775
+ },
1776
+ schema: erc20ApprovalGasSponsoringSchema
1777
+ }
1778
+ };
1779
+ }
1780
+
1781
+ // src/erc20-approval-gas-sponsoring/facilitator.ts
1782
+ var import__3 = __toESM(require("ajv/dist/2020.js"));
1783
+ function extractErc20ApprovalGasSponsoringInfo(paymentPayload) {
1784
+ if (!paymentPayload.extensions) {
1785
+ return null;
1786
+ }
1787
+ const extension = paymentPayload.extensions[ERC20_APPROVAL_GAS_SPONSORING.key];
1788
+ if (!extension?.info) {
1789
+ return null;
1790
+ }
1791
+ const info = extension.info;
1792
+ if (!info.from || !info.asset || !info.spender || !info.amount || !info.signedTransaction || !info.version) {
1793
+ return null;
1794
+ }
1795
+ return info;
1796
+ }
1797
+ function validateErc20ApprovalGasSponsoringInfo(info) {
1798
+ const ajv = new import__3.default({ strict: false, allErrors: true });
1799
+ const validate = ajv.compile(erc20ApprovalGasSponsoringSchema);
1800
+ return validate(info);
1801
+ }
1705
1802
  // Annotate the CommonJS export names for ESM import in node:
1706
1803
  0 && (module.exports = {
1707
1804
  BAZAAR,
1708
1805
  EIP2612_GAS_SPONSORING,
1806
+ ERC20_APPROVAL_GAS_SPONSORING,
1807
+ ERC20_APPROVAL_GAS_SPONSORING_VERSION,
1709
1808
  InMemorySIWxStorage,
1710
1809
  PAYMENT_IDENTIFIER,
1711
1810
  PAYMENT_ID_MAX_LENGTH,
@@ -1719,6 +1818,7 @@ function validateEip2612GasSponsoringInfo(info) {
1719
1818
  appendPaymentIdentifierToExtensions,
1720
1819
  bazaarResourceServerExtension,
1721
1820
  buildSIWxSchema,
1821
+ createErc20ApprovalGasSponsoringExtension,
1722
1822
  createSIWxClientHook,
1723
1823
  createSIWxMessage,
1724
1824
  createSIWxPayload,
@@ -1726,17 +1826,20 @@ function validateEip2612GasSponsoringInfo(info) {
1726
1826
  createSIWxSettleHook,
1727
1827
  declareDiscoveryExtension,
1728
1828
  declareEip2612GasSponsoringExtension,
1829
+ declareErc20ApprovalGasSponsoringExtension,
1729
1830
  declarePaymentIdentifierExtension,
1730
1831
  declareSIWxExtension,
1731
1832
  decodeBase58,
1732
1833
  encodeBase58,
1733
1834
  encodeSIWxHeader,
1835
+ erc20ApprovalGasSponsoringSchema,
1734
1836
  extractAndValidatePaymentIdentifier,
1735
1837
  extractDiscoveryInfo,
1736
1838
  extractDiscoveryInfoFromExtension,
1737
1839
  extractDiscoveryInfoV1,
1738
1840
  extractEVMChainId,
1739
1841
  extractEip2612GasSponsoringInfo,
1842
+ extractErc20ApprovalGasSponsoringInfo,
1740
1843
  extractPaymentIdentifier,
1741
1844
  extractResourceMetadataV1,
1742
1845
  extractSolanaChainReference,
@@ -1764,6 +1867,7 @@ function validateEip2612GasSponsoringInfo(info) {
1764
1867
  validateAndExtract,
1765
1868
  validateDiscoveryExtension,
1766
1869
  validateEip2612GasSponsoringInfo,
1870
+ validateErc20ApprovalGasSponsoringInfo,
1767
1871
  validatePaymentIdentifier,
1768
1872
  validatePaymentIdentifierRequirement,
1769
1873
  validateSIWxMessage,