x402-express-mantle 1.0.0 → 1.0.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.
Files changed (125) hide show
  1. package/README.md +52 -30
  2. package/lib/cjs/index.d.ts +119 -81
  3. package/lib/cjs/index.js +3 -3
  4. package/lib/esm/index.d.mts +5 -5
  5. package/lib/esm/index.mjs +3 -3
  6. package/package.json +17 -5
  7. package/lib/cjs/client/index.d.ts +0 -243
  8. package/lib/cjs/client/index.js +0 -413
  9. package/lib/cjs/client/index.js.map +0 -1
  10. package/lib/cjs/exact/client/index.d.ts +0 -37
  11. package/lib/cjs/exact/client/index.js +0 -281
  12. package/lib/cjs/exact/client/index.js.map +0 -1
  13. package/lib/cjs/exact/facilitator/index.d.ts +0 -110
  14. package/lib/cjs/exact/facilitator/index.js +0 -714
  15. package/lib/cjs/exact/facilitator/index.js.map +0 -1
  16. package/lib/cjs/exact/server/index.d.ts +0 -87
  17. package/lib/cjs/exact/server/index.js +0 -209
  18. package/lib/cjs/exact/server/index.js.map +0 -1
  19. package/lib/cjs/exact/v1/client/index.d.ts +0 -33
  20. package/lib/cjs/exact/v1/client/index.js +0 -169
  21. package/lib/cjs/exact/v1/client/index.js.map +0 -1
  22. package/lib/cjs/exact/v1/facilitator/index.d.ts +0 -71
  23. package/lib/cjs/exact/v1/facilitator/index.js +0 -384
  24. package/lib/cjs/exact/v1/facilitator/index.js.map +0 -1
  25. package/lib/cjs/facilitator/index.d.ts +0 -192
  26. package/lib/cjs/facilitator/index.js +0 -398
  27. package/lib/cjs/facilitator/index.js.map +0 -1
  28. package/lib/cjs/http/index.d.ts +0 -52
  29. package/lib/cjs/http/index.js +0 -827
  30. package/lib/cjs/http/index.js.map +0 -1
  31. package/lib/cjs/mechanisms-CzuGzYsS.d.ts +0 -270
  32. package/lib/cjs/scheme-MoBRXFM8.d.ts +0 -29
  33. package/lib/cjs/server/index.d.ts +0 -2
  34. package/lib/cjs/server/index.js +0 -1305
  35. package/lib/cjs/server/index.js.map +0 -1
  36. package/lib/cjs/signer-5OVDxViv.d.ts +0 -79
  37. package/lib/cjs/signer-BMkbhFYE.d.ts +0 -123
  38. package/lib/cjs/types/index.d.ts +0 -1
  39. package/lib/cjs/types/index.js +0 -66
  40. package/lib/cjs/types/index.js.map +0 -1
  41. package/lib/cjs/types/v1/index.d.ts +0 -1
  42. package/lib/cjs/types/v1/index.js +0 -19
  43. package/lib/cjs/types/v1/index.js.map +0 -1
  44. package/lib/cjs/utils/index.d.ts +0 -48
  45. package/lib/cjs/utils/index.js +0 -116
  46. package/lib/cjs/utils/index.js.map +0 -1
  47. package/lib/cjs/v1/index.d.ts +0 -12
  48. package/lib/cjs/v1/index.js +0 -180
  49. package/lib/cjs/v1/index.js.map +0 -1
  50. package/lib/cjs/x402HTTPResourceServer-D1YtlH_r.d.ts +0 -719
  51. package/lib/esm/chunk-3CEIVWNN.mjs +0 -339
  52. package/lib/esm/chunk-3CEIVWNN.mjs.map +0 -1
  53. package/lib/esm/chunk-BJTO5JO5.mjs +0 -11
  54. package/lib/esm/chunk-BJTO5JO5.mjs.map +0 -1
  55. package/lib/esm/chunk-EEA7DKZI.mjs +0 -111
  56. package/lib/esm/chunk-EEA7DKZI.mjs.map +0 -1
  57. package/lib/esm/chunk-FOUXRQAV.mjs +0 -88
  58. package/lib/esm/chunk-FOUXRQAV.mjs.map +0 -1
  59. package/lib/esm/chunk-IKSTWKEM.mjs +0 -157
  60. package/lib/esm/chunk-IKSTWKEM.mjs.map +0 -1
  61. package/lib/esm/chunk-JYZWCLMP.mjs +0 -305
  62. package/lib/esm/chunk-JYZWCLMP.mjs.map +0 -1
  63. package/lib/esm/chunk-PNSAJQCF.mjs +0 -108
  64. package/lib/esm/chunk-PNSAJQCF.mjs.map +0 -1
  65. package/lib/esm/chunk-PSA4YVU2.mjs +0 -92
  66. package/lib/esm/chunk-PSA4YVU2.mjs.map +0 -1
  67. package/lib/esm/chunk-QLXM7BIB.mjs +0 -23
  68. package/lib/esm/chunk-QLXM7BIB.mjs.map +0 -1
  69. package/lib/esm/chunk-TDLQZ6MP.mjs +0 -86
  70. package/lib/esm/chunk-TDLQZ6MP.mjs.map +0 -1
  71. package/lib/esm/chunk-VE37GDG2.mjs +0 -7
  72. package/lib/esm/chunk-VE37GDG2.mjs.map +0 -1
  73. package/lib/esm/chunk-WWACQNRQ.mjs +0 -7
  74. package/lib/esm/chunk-WWACQNRQ.mjs.map +0 -1
  75. package/lib/esm/chunk-X4W4S5RB.mjs +0 -39
  76. package/lib/esm/chunk-X4W4S5RB.mjs.map +0 -1
  77. package/lib/esm/chunk-Z4QX3O5V.mjs +0 -748
  78. package/lib/esm/chunk-Z4QX3O5V.mjs.map +0 -1
  79. package/lib/esm/chunk-ZYXTTU74.mjs +0 -88
  80. package/lib/esm/chunk-ZYXTTU74.mjs.map +0 -1
  81. package/lib/esm/client/index.d.mts +0 -243
  82. package/lib/esm/client/index.mjs +0 -260
  83. package/lib/esm/client/index.mjs.map +0 -1
  84. package/lib/esm/exact/client/index.d.mts +0 -37
  85. package/lib/esm/exact/client/index.mjs +0 -36
  86. package/lib/esm/exact/client/index.mjs.map +0 -1
  87. package/lib/esm/exact/facilitator/index.d.mts +0 -110
  88. package/lib/esm/exact/facilitator/index.mjs +0 -350
  89. package/lib/esm/exact/facilitator/index.mjs.map +0 -1
  90. package/lib/esm/exact/server/index.d.mts +0 -87
  91. package/lib/esm/exact/server/index.mjs +0 -129
  92. package/lib/esm/exact/server/index.mjs.map +0 -1
  93. package/lib/esm/exact/v1/client/index.d.mts +0 -33
  94. package/lib/esm/exact/v1/client/index.mjs +0 -8
  95. package/lib/esm/exact/v1/client/index.mjs.map +0 -1
  96. package/lib/esm/exact/v1/facilitator/index.d.mts +0 -71
  97. package/lib/esm/exact/v1/facilitator/index.mjs +0 -8
  98. package/lib/esm/exact/v1/facilitator/index.mjs.map +0 -1
  99. package/lib/esm/facilitator/index.d.mts +0 -192
  100. package/lib/esm/facilitator/index.mjs +0 -373
  101. package/lib/esm/facilitator/index.mjs.map +0 -1
  102. package/lib/esm/http/index.d.mts +0 -52
  103. package/lib/esm/http/index.mjs +0 -29
  104. package/lib/esm/http/index.mjs.map +0 -1
  105. package/lib/esm/mechanisms-CzuGzYsS.d.mts +0 -270
  106. package/lib/esm/scheme-fjF-9LhT.d.mts +0 -29
  107. package/lib/esm/server/index.d.mts +0 -2
  108. package/lib/esm/server/index.mjs +0 -563
  109. package/lib/esm/server/index.mjs.map +0 -1
  110. package/lib/esm/signer-5OVDxViv.d.mts +0 -79
  111. package/lib/esm/signer-BMkbhFYE.d.mts +0 -123
  112. package/lib/esm/types/index.d.mts +0 -1
  113. package/lib/esm/types/index.mjs +0 -10
  114. package/lib/esm/types/index.mjs.map +0 -1
  115. package/lib/esm/types/v1/index.d.mts +0 -1
  116. package/lib/esm/types/v1/index.mjs +0 -1
  117. package/lib/esm/types/v1/index.mjs.map +0 -1
  118. package/lib/esm/utils/index.d.mts +0 -48
  119. package/lib/esm/utils/index.mjs +0 -20
  120. package/lib/esm/utils/index.mjs.map +0 -1
  121. package/lib/esm/v1/index.d.mts +0 -12
  122. package/lib/esm/v1/index.mjs +0 -13
  123. package/lib/esm/v1/index.mjs.map +0 -1
  124. package/lib/esm/x402HTTPResourceServer-BIfIK5HS.d.mts +0 -719
  125. package/src/index.js +0 -4
@@ -1,71 +0,0 @@
1
- import { SchemeNetworkFacilitator, PaymentPayload, PaymentRequirements, VerifyResponse, SettleResponse } from '@x402/core/types';
2
- import { F as FacilitatorSvmSigner } from '../../../signer-BMkbhFYE.mjs';
3
- import '@solana/kit';
4
-
5
- /**
6
- * SVM facilitator implementation for the Exact payment scheme (V1).
7
- */
8
- declare class ExactSvmSchemeV1 implements SchemeNetworkFacilitator {
9
- private readonly signer;
10
- readonly scheme = "exact";
11
- readonly caipFamily = "solana:*";
12
- /**
13
- * Creates a new ExactSvmFacilitatorV1 instance.
14
- *
15
- * @param signer - The SVM RPC client for facilitator operations
16
- * @returns ExactSvmFacilitatorV1 instance
17
- */
18
- constructor(signer: FacilitatorSvmSigner);
19
- /**
20
- * Get mechanism-specific extra data for the supported kinds endpoint.
21
- * For SVM, this includes a randomly selected fee payer address.
22
- * Random selection distributes load across multiple signers.
23
- *
24
- * @param _ - The network identifier (unused for SVM)
25
- * @returns Extra data with feePayer address
26
- */
27
- getExtra(_: string): Record<string, unknown> | undefined;
28
- /**
29
- * Get signer addresses used by this facilitator.
30
- * For SVM, returns all available fee payer addresses.
31
- *
32
- * @param _ - The network identifier (unused for SVM)
33
- * @returns Array of fee payer addresses
34
- */
35
- getSigners(_: string): string[];
36
- /**
37
- * Verifies a payment payload (V1).
38
- *
39
- * @param payload - The payment payload to verify
40
- * @param requirements - The payment requirements
41
- * @returns Promise resolving to verification response
42
- */
43
- verify(payload: PaymentPayload, requirements: PaymentRequirements): Promise<VerifyResponse>;
44
- /**
45
- * Settles a payment by submitting the transaction (V1).
46
- * Ensures the correct signer is used based on the feePayer specified in requirements.
47
- *
48
- * @param payload - The payment payload to settle
49
- * @param requirements - The payment requirements
50
- * @returns Promise resolving to settlement response
51
- */
52
- settle(payload: PaymentPayload, requirements: PaymentRequirements): Promise<SettleResponse>;
53
- /**
54
- * Verify compute limit instruction
55
- *
56
- * @param instruction - The compute limit instruction
57
- * @param instruction.programAddress - Program address
58
- * @param instruction.data - Instruction data bytes
59
- */
60
- private verifyComputeLimitInstruction;
61
- /**
62
- * Verify compute price instruction
63
- *
64
- * @param instruction - The compute price instruction
65
- * @param instruction.programAddress - Program address
66
- * @param instruction.data - Instruction data bytes
67
- */
68
- private verifyComputePriceInstruction;
69
- }
70
-
71
- export { ExactSvmSchemeV1 };
@@ -1,8 +0,0 @@
1
- import {
2
- ExactSvmSchemeV1
3
- } from "../../../chunk-3CEIVWNN.mjs";
4
- import "../../../chunk-IKSTWKEM.mjs";
5
- export {
6
- ExactSvmSchemeV1
7
- };
8
- //# sourceMappingURL=index.mjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":[],"sourcesContent":[],"mappings":"","names":[]}
@@ -1,192 +0,0 @@
1
- import { P as PaymentPayload, a as PaymentRequirements, V as VerifyResponse, S as SettleResponse, N as Network, b as SchemeNetworkFacilitator } from '../mechanisms-CzuGzYsS.mjs';
2
-
3
- /**
4
- * Facilitator Hook Context Interfaces
5
- */
6
- interface FacilitatorVerifyContext {
7
- paymentPayload: PaymentPayload;
8
- requirements: PaymentRequirements;
9
- }
10
- interface FacilitatorVerifyResultContext extends FacilitatorVerifyContext {
11
- result: VerifyResponse;
12
- }
13
- interface FacilitatorVerifyFailureContext extends FacilitatorVerifyContext {
14
- error: Error;
15
- }
16
- interface FacilitatorSettleContext {
17
- paymentPayload: PaymentPayload;
18
- requirements: PaymentRequirements;
19
- }
20
- interface FacilitatorSettleResultContext extends FacilitatorSettleContext {
21
- result: SettleResponse;
22
- }
23
- interface FacilitatorSettleFailureContext extends FacilitatorSettleContext {
24
- error: Error;
25
- }
26
- /**
27
- * Facilitator Hook Type Definitions
28
- */
29
- type FacilitatorBeforeVerifyHook = (context: FacilitatorVerifyContext) => Promise<void | {
30
- abort: true;
31
- reason: string;
32
- }>;
33
- type FacilitatorAfterVerifyHook = (context: FacilitatorVerifyResultContext) => Promise<void>;
34
- type FacilitatorOnVerifyFailureHook = (context: FacilitatorVerifyFailureContext) => Promise<void | {
35
- recovered: true;
36
- result: VerifyResponse;
37
- }>;
38
- type FacilitatorBeforeSettleHook = (context: FacilitatorSettleContext) => Promise<void | {
39
- abort: true;
40
- reason: string;
41
- }>;
42
- type FacilitatorAfterSettleHook = (context: FacilitatorSettleResultContext) => Promise<void>;
43
- type FacilitatorOnSettleFailureHook = (context: FacilitatorSettleFailureContext) => Promise<void | {
44
- recovered: true;
45
- result: SettleResponse;
46
- }>;
47
- /**
48
- * Facilitator client for the x402 payment protocol.
49
- * Manages payment scheme registration, verification, and settlement.
50
- */
51
- declare class x402Facilitator {
52
- private readonly registeredFacilitatorSchemes;
53
- private readonly extensions;
54
- private beforeVerifyHooks;
55
- private afterVerifyHooks;
56
- private onVerifyFailureHooks;
57
- private beforeSettleHooks;
58
- private afterSettleHooks;
59
- private onSettleFailureHooks;
60
- /**
61
- * Registers a scheme facilitator for the current x402 version.
62
- * Networks are stored and used for getSupported() - no need to specify them later.
63
- *
64
- * @param networks - Single network or array of networks this facilitator supports
65
- * @param facilitator - The scheme network facilitator to register
66
- * @returns The x402Facilitator instance for chaining
67
- */
68
- register(networks: Network | Network[], facilitator: SchemeNetworkFacilitator): x402Facilitator;
69
- /**
70
- * Registers a scheme facilitator for x402 version 1.
71
- * Networks are stored and used for getSupported() - no need to specify them later.
72
- *
73
- * @param networks - Single network or array of networks this facilitator supports
74
- * @param facilitator - The scheme network facilitator to register
75
- * @returns The x402Facilitator instance for chaining
76
- */
77
- registerV1(networks: Network | Network[], facilitator: SchemeNetworkFacilitator): x402Facilitator;
78
- /**
79
- * Registers a protocol extension.
80
- *
81
- * @param extension - The extension name to register (e.g., "bazaar", "sign_in_with_x")
82
- * @returns The x402Facilitator instance for chaining
83
- */
84
- registerExtension(extension: string): x402Facilitator;
85
- /**
86
- * Gets the list of registered extensions.
87
- *
88
- * @returns Array of extension names
89
- */
90
- getExtensions(): string[];
91
- /**
92
- * Register a hook to execute before facilitator payment verification.
93
- * Can abort verification by returning { abort: true, reason: string }
94
- *
95
- * @param hook - The hook function to register
96
- * @returns The x402Facilitator instance for chaining
97
- */
98
- onBeforeVerify(hook: FacilitatorBeforeVerifyHook): x402Facilitator;
99
- /**
100
- * Register a hook to execute after successful facilitator payment verification (isValid: true).
101
- * This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.
102
- *
103
- * @param hook - The hook function to register
104
- * @returns The x402Facilitator instance for chaining
105
- */
106
- onAfterVerify(hook: FacilitatorAfterVerifyHook): x402Facilitator;
107
- /**
108
- * Register a hook to execute when facilitator payment verification fails.
109
- * Called when: verification returns isValid: false, or an exception is thrown during verification.
110
- * Can recover from failure by returning { recovered: true, result: VerifyResponse }
111
- *
112
- * @param hook - The hook function to register
113
- * @returns The x402Facilitator instance for chaining
114
- */
115
- onVerifyFailure(hook: FacilitatorOnVerifyFailureHook): x402Facilitator;
116
- /**
117
- * Register a hook to execute before facilitator payment settlement.
118
- * Can abort settlement by returning { abort: true, reason: string }
119
- *
120
- * @param hook - The hook function to register
121
- * @returns The x402Facilitator instance for chaining
122
- */
123
- onBeforeSettle(hook: FacilitatorBeforeSettleHook): x402Facilitator;
124
- /**
125
- * Register a hook to execute after successful facilitator payment settlement.
126
- *
127
- * @param hook - The hook function to register
128
- * @returns The x402Facilitator instance for chaining
129
- */
130
- onAfterSettle(hook: FacilitatorAfterSettleHook): x402Facilitator;
131
- /**
132
- * Register a hook to execute when facilitator payment settlement fails.
133
- * Can recover from failure by returning { recovered: true, result: SettleResponse }
134
- *
135
- * @param hook - The hook function to register
136
- * @returns The x402Facilitator instance for chaining
137
- */
138
- onSettleFailure(hook: FacilitatorOnSettleFailureHook): x402Facilitator;
139
- /**
140
- * Gets supported payment kinds, extensions, and signers.
141
- * Uses networks registered during register() calls - no parameters needed.
142
- * Returns flat array format for backward compatibility with V1 clients.
143
- *
144
- * @returns Supported response with kinds as array (with version in each element), extensions, and signers
145
- */
146
- getSupported(): {
147
- kinds: Array<{
148
- x402Version: number;
149
- scheme: string;
150
- network: string;
151
- extra?: Record<string, unknown>;
152
- }>;
153
- extensions: string[];
154
- signers: Record<string, string[]>;
155
- };
156
- /**
157
- * Verifies a payment payload against requirements.
158
- *
159
- * @param paymentPayload - The payment payload to verify
160
- * @param paymentRequirements - The payment requirements to verify against
161
- * @returns Promise resolving to the verification response
162
- */
163
- verify(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<VerifyResponse>;
164
- /**
165
- * Settles a payment based on the payload and requirements.
166
- *
167
- * @param paymentPayload - The payment payload to settle
168
- * @param paymentRequirements - The payment requirements for settlement
169
- * @returns Promise resolving to the settlement response
170
- */
171
- settle(paymentPayload: PaymentPayload, paymentRequirements: PaymentRequirements): Promise<SettleResponse>;
172
- /**
173
- * Internal method to register a scheme facilitator.
174
- *
175
- * @param x402Version - The x402 protocol version
176
- * @param networks - Array of concrete networks this facilitator supports
177
- * @param facilitator - The scheme network facilitator to register
178
- * @returns The x402Facilitator instance for chaining
179
- */
180
- private _registerScheme;
181
- /**
182
- * Derives a wildcard pattern from an array of networks.
183
- * If all networks share the same namespace, returns wildcard pattern.
184
- * Otherwise returns the first network for exact matching.
185
- *
186
- * @param networks - Array of networks
187
- * @returns Derived pattern for matching
188
- */
189
- private derivePattern;
190
- }
191
-
192
- export { type FacilitatorAfterSettleHook, type FacilitatorAfterVerifyHook, type FacilitatorBeforeSettleHook, type FacilitatorBeforeVerifyHook, type FacilitatorOnSettleFailureHook, type FacilitatorOnVerifyFailureHook, type FacilitatorSettleContext, type FacilitatorSettleFailureContext, type FacilitatorSettleResultContext, type FacilitatorVerifyContext, type FacilitatorVerifyFailureContext, type FacilitatorVerifyResultContext, x402Facilitator };
@@ -1,373 +0,0 @@
1
- import {
2
- x402Version
3
- } from "../chunk-VE37GDG2.mjs";
4
- import "../chunk-BJTO5JO5.mjs";
5
-
6
- // src/facilitator/x402Facilitator.ts
7
- var x402Facilitator = class {
8
- constructor() {
9
- this.registeredFacilitatorSchemes = /* @__PURE__ */ new Map();
10
- this.extensions = [];
11
- this.beforeVerifyHooks = [];
12
- this.afterVerifyHooks = [];
13
- this.onVerifyFailureHooks = [];
14
- this.beforeSettleHooks = [];
15
- this.afterSettleHooks = [];
16
- this.onSettleFailureHooks = [];
17
- }
18
- /**
19
- * Registers a scheme facilitator for the current x402 version.
20
- * Networks are stored and used for getSupported() - no need to specify them later.
21
- *
22
- * @param networks - Single network or array of networks this facilitator supports
23
- * @param facilitator - The scheme network facilitator to register
24
- * @returns The x402Facilitator instance for chaining
25
- */
26
- register(networks, facilitator) {
27
- const networksArray = Array.isArray(networks) ? networks : [networks];
28
- return this._registerScheme(x402Version, networksArray, facilitator);
29
- }
30
- /**
31
- * Registers a scheme facilitator for x402 version 1.
32
- * Networks are stored and used for getSupported() - no need to specify them later.
33
- *
34
- * @param networks - Single network or array of networks this facilitator supports
35
- * @param facilitator - The scheme network facilitator to register
36
- * @returns The x402Facilitator instance for chaining
37
- */
38
- registerV1(networks, facilitator) {
39
- const networksArray = Array.isArray(networks) ? networks : [networks];
40
- return this._registerScheme(1, networksArray, facilitator);
41
- }
42
- /**
43
- * Registers a protocol extension.
44
- *
45
- * @param extension - The extension name to register (e.g., "bazaar", "sign_in_with_x")
46
- * @returns The x402Facilitator instance for chaining
47
- */
48
- registerExtension(extension) {
49
- if (!this.extensions.includes(extension)) {
50
- this.extensions.push(extension);
51
- }
52
- return this;
53
- }
54
- /**
55
- * Gets the list of registered extensions.
56
- *
57
- * @returns Array of extension names
58
- */
59
- getExtensions() {
60
- return [...this.extensions];
61
- }
62
- /**
63
- * Register a hook to execute before facilitator payment verification.
64
- * Can abort verification by returning { abort: true, reason: string }
65
- *
66
- * @param hook - The hook function to register
67
- * @returns The x402Facilitator instance for chaining
68
- */
69
- onBeforeVerify(hook) {
70
- this.beforeVerifyHooks.push(hook);
71
- return this;
72
- }
73
- /**
74
- * Register a hook to execute after successful facilitator payment verification (isValid: true).
75
- * This hook is NOT called when verification fails (isValid: false) - use onVerifyFailure for that.
76
- *
77
- * @param hook - The hook function to register
78
- * @returns The x402Facilitator instance for chaining
79
- */
80
- onAfterVerify(hook) {
81
- this.afterVerifyHooks.push(hook);
82
- return this;
83
- }
84
- /**
85
- * Register a hook to execute when facilitator payment verification fails.
86
- * Called when: verification returns isValid: false, or an exception is thrown during verification.
87
- * Can recover from failure by returning { recovered: true, result: VerifyResponse }
88
- *
89
- * @param hook - The hook function to register
90
- * @returns The x402Facilitator instance for chaining
91
- */
92
- onVerifyFailure(hook) {
93
- this.onVerifyFailureHooks.push(hook);
94
- return this;
95
- }
96
- /**
97
- * Register a hook to execute before facilitator payment settlement.
98
- * Can abort settlement by returning { abort: true, reason: string }
99
- *
100
- * @param hook - The hook function to register
101
- * @returns The x402Facilitator instance for chaining
102
- */
103
- onBeforeSettle(hook) {
104
- this.beforeSettleHooks.push(hook);
105
- return this;
106
- }
107
- /**
108
- * Register a hook to execute after successful facilitator payment settlement.
109
- *
110
- * @param hook - The hook function to register
111
- * @returns The x402Facilitator instance for chaining
112
- */
113
- onAfterSettle(hook) {
114
- this.afterSettleHooks.push(hook);
115
- return this;
116
- }
117
- /**
118
- * Register a hook to execute when facilitator payment settlement fails.
119
- * Can recover from failure by returning { recovered: true, result: SettleResponse }
120
- *
121
- * @param hook - The hook function to register
122
- * @returns The x402Facilitator instance for chaining
123
- */
124
- onSettleFailure(hook) {
125
- this.onSettleFailureHooks.push(hook);
126
- return this;
127
- }
128
- /**
129
- * Gets supported payment kinds, extensions, and signers.
130
- * Uses networks registered during register() calls - no parameters needed.
131
- * Returns flat array format for backward compatibility with V1 clients.
132
- *
133
- * @returns Supported response with kinds as array (with version in each element), extensions, and signers
134
- */
135
- getSupported() {
136
- const kinds = [];
137
- const signersByFamily = {};
138
- for (const [version, schemeDataArray] of this.registeredFacilitatorSchemes) {
139
- for (const schemeData of schemeDataArray) {
140
- const { facilitator, networks } = schemeData;
141
- const scheme = facilitator.scheme;
142
- for (const network of networks) {
143
- const extra = facilitator.getExtra(network);
144
- kinds.push({
145
- x402Version: version,
146
- scheme,
147
- network,
148
- ...extra && { extra }
149
- });
150
- const family = facilitator.caipFamily;
151
- if (!signersByFamily[family]) {
152
- signersByFamily[family] = /* @__PURE__ */ new Set();
153
- }
154
- facilitator.getSigners(network).forEach((signer) => signersByFamily[family].add(signer));
155
- }
156
- }
157
- }
158
- const signers = {};
159
- for (const [family, signerSet] of Object.entries(signersByFamily)) {
160
- signers[family] = Array.from(signerSet);
161
- }
162
- return {
163
- kinds,
164
- extensions: this.extensions,
165
- signers
166
- };
167
- }
168
- /**
169
- * Verifies a payment payload against requirements.
170
- *
171
- * @param paymentPayload - The payment payload to verify
172
- * @param paymentRequirements - The payment requirements to verify against
173
- * @returns Promise resolving to the verification response
174
- */
175
- async verify(paymentPayload, paymentRequirements) {
176
- const context = {
177
- paymentPayload,
178
- requirements: paymentRequirements
179
- };
180
- for (const hook of this.beforeVerifyHooks) {
181
- const result = await hook(context);
182
- if (result && "abort" in result && result.abort) {
183
- return {
184
- isValid: false,
185
- invalidReason: result.reason
186
- };
187
- }
188
- }
189
- try {
190
- const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);
191
- if (!schemeDataArray) {
192
- throw new Error(
193
- `No facilitator registered for x402 version: ${paymentPayload.x402Version}`
194
- );
195
- }
196
- let schemeNetworkFacilitator;
197
- for (const schemeData of schemeDataArray) {
198
- if (schemeData.facilitator.scheme === paymentRequirements.scheme) {
199
- if (schemeData.networks.has(paymentRequirements.network)) {
200
- schemeNetworkFacilitator = schemeData.facilitator;
201
- break;
202
- }
203
- const patternRegex = new RegExp("^" + schemeData.pattern.replace("*", ".*") + "$");
204
- if (patternRegex.test(paymentRequirements.network)) {
205
- schemeNetworkFacilitator = schemeData.facilitator;
206
- break;
207
- }
208
- }
209
- }
210
- if (!schemeNetworkFacilitator) {
211
- throw new Error(
212
- `No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`
213
- );
214
- }
215
- const verifyResult = await schemeNetworkFacilitator.verify(
216
- paymentPayload,
217
- paymentRequirements
218
- );
219
- if (!verifyResult.isValid) {
220
- const failureContext = {
221
- ...context,
222
- error: new Error(verifyResult.invalidReason || "Verification failed")
223
- };
224
- for (const hook of this.onVerifyFailureHooks) {
225
- const result = await hook(failureContext);
226
- if (result && "recovered" in result && result.recovered) {
227
- const recoveredContext = {
228
- ...context,
229
- result: result.result
230
- };
231
- for (const hook2 of this.afterVerifyHooks) {
232
- await hook2(recoveredContext);
233
- }
234
- return result.result;
235
- }
236
- }
237
- return verifyResult;
238
- }
239
- const resultContext = {
240
- ...context,
241
- result: verifyResult
242
- };
243
- for (const hook of this.afterVerifyHooks) {
244
- await hook(resultContext);
245
- }
246
- return verifyResult;
247
- } catch (error) {
248
- const failureContext = {
249
- ...context,
250
- error
251
- };
252
- for (const hook of this.onVerifyFailureHooks) {
253
- const result = await hook(failureContext);
254
- if (result && "recovered" in result && result.recovered) {
255
- return result.result;
256
- }
257
- }
258
- throw error;
259
- }
260
- }
261
- /**
262
- * Settles a payment based on the payload and requirements.
263
- *
264
- * @param paymentPayload - The payment payload to settle
265
- * @param paymentRequirements - The payment requirements for settlement
266
- * @returns Promise resolving to the settlement response
267
- */
268
- async settle(paymentPayload, paymentRequirements) {
269
- const context = {
270
- paymentPayload,
271
- requirements: paymentRequirements
272
- };
273
- for (const hook of this.beforeSettleHooks) {
274
- const result = await hook(context);
275
- if (result && "abort" in result && result.abort) {
276
- throw new Error(`Settlement aborted: ${result.reason}`);
277
- }
278
- }
279
- try {
280
- const schemeDataArray = this.registeredFacilitatorSchemes.get(paymentPayload.x402Version);
281
- if (!schemeDataArray) {
282
- throw new Error(
283
- `No facilitator registered for x402 version: ${paymentPayload.x402Version}`
284
- );
285
- }
286
- let schemeNetworkFacilitator;
287
- for (const schemeData of schemeDataArray) {
288
- if (schemeData.facilitator.scheme === paymentRequirements.scheme) {
289
- if (schemeData.networks.has(paymentRequirements.network)) {
290
- schemeNetworkFacilitator = schemeData.facilitator;
291
- break;
292
- }
293
- const patternRegex = new RegExp("^" + schemeData.pattern.replace("*", ".*") + "$");
294
- if (patternRegex.test(paymentRequirements.network)) {
295
- schemeNetworkFacilitator = schemeData.facilitator;
296
- break;
297
- }
298
- }
299
- }
300
- if (!schemeNetworkFacilitator) {
301
- throw new Error(
302
- `No facilitator registered for scheme: ${paymentRequirements.scheme} and network: ${paymentRequirements.network}`
303
- );
304
- }
305
- const settleResult = await schemeNetworkFacilitator.settle(
306
- paymentPayload,
307
- paymentRequirements
308
- );
309
- const resultContext = {
310
- ...context,
311
- result: settleResult
312
- };
313
- for (const hook of this.afterSettleHooks) {
314
- await hook(resultContext);
315
- }
316
- return settleResult;
317
- } catch (error) {
318
- const failureContext = {
319
- ...context,
320
- error
321
- };
322
- for (const hook of this.onSettleFailureHooks) {
323
- const result = await hook(failureContext);
324
- if (result && "recovered" in result && result.recovered) {
325
- return result.result;
326
- }
327
- }
328
- throw error;
329
- }
330
- }
331
- /**
332
- * Internal method to register a scheme facilitator.
333
- *
334
- * @param x402Version - The x402 protocol version
335
- * @param networks - Array of concrete networks this facilitator supports
336
- * @param facilitator - The scheme network facilitator to register
337
- * @returns The x402Facilitator instance for chaining
338
- */
339
- _registerScheme(x402Version2, networks, facilitator) {
340
- if (!this.registeredFacilitatorSchemes.has(x402Version2)) {
341
- this.registeredFacilitatorSchemes.set(x402Version2, []);
342
- }
343
- const schemeDataArray = this.registeredFacilitatorSchemes.get(x402Version2);
344
- schemeDataArray.push({
345
- facilitator,
346
- networks: new Set(networks),
347
- pattern: this.derivePattern(networks)
348
- });
349
- return this;
350
- }
351
- /**
352
- * Derives a wildcard pattern from an array of networks.
353
- * If all networks share the same namespace, returns wildcard pattern.
354
- * Otherwise returns the first network for exact matching.
355
- *
356
- * @param networks - Array of networks
357
- * @returns Derived pattern for matching
358
- */
359
- derivePattern(networks) {
360
- if (networks.length === 0) return "";
361
- if (networks.length === 1) return networks[0];
362
- const namespaces = networks.map((n) => n.split(":")[0]);
363
- const uniqueNamespaces = new Set(namespaces);
364
- if (uniqueNamespaces.size === 1) {
365
- return `${namespaces[0]}:*`;
366
- }
367
- return networks[0];
368
- }
369
- };
370
- export {
371
- x402Facilitator
372
- };
373
- //# sourceMappingURL=index.mjs.map