moltspay 0.8.14 → 0.9.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.
@@ -0,0 +1,233 @@
1
+ /**
2
+ * Facilitator Interface
3
+ *
4
+ * A facilitator is a service that handles x402 payment verification and settlement.
5
+ * This abstraction allows MoltsPay to support multiple facilitators.
6
+ *
7
+ * @see https://www.x402.org/ecosystem?category=facilitators
8
+ */
9
+ /**
10
+ * x402 Payment Payload (from client)
11
+ */
12
+ interface X402PaymentPayload {
13
+ x402Version: number;
14
+ scheme?: string;
15
+ network?: string;
16
+ accepted?: {
17
+ scheme: string;
18
+ network: string;
19
+ asset: string;
20
+ amount: string;
21
+ payTo: string;
22
+ maxTimeoutSeconds: number;
23
+ extra?: Record<string, unknown>;
24
+ };
25
+ payload: unknown;
26
+ }
27
+ /**
28
+ * x402 Payment Requirements (server specifies what it accepts)
29
+ */
30
+ interface X402PaymentRequirements {
31
+ scheme: string;
32
+ network: string;
33
+ asset: string;
34
+ amount: string;
35
+ payTo: string;
36
+ maxTimeoutSeconds: number;
37
+ extra?: Record<string, unknown>;
38
+ }
39
+ /**
40
+ * Result of payment verification
41
+ */
42
+ interface VerifyResult {
43
+ valid: boolean;
44
+ error?: string;
45
+ details?: Record<string, unknown>;
46
+ }
47
+ /**
48
+ * Result of payment settlement
49
+ */
50
+ interface SettleResult {
51
+ success: boolean;
52
+ transaction?: string;
53
+ error?: string;
54
+ status?: string;
55
+ }
56
+ /**
57
+ * Facilitator health check result
58
+ */
59
+ interface HealthCheckResult {
60
+ healthy: boolean;
61
+ latencyMs?: number;
62
+ error?: string;
63
+ }
64
+ /**
65
+ * Facilitator fee information (for selection strategies)
66
+ */
67
+ interface FacilitatorFee {
68
+ perTx: number;
69
+ currency: string;
70
+ freeQuota?: number;
71
+ }
72
+ /**
73
+ * Facilitator configuration
74
+ */
75
+ interface FacilitatorConfig {
76
+ /** Facilitator endpoint URL */
77
+ endpoint?: string;
78
+ /** API key (if required) */
79
+ apiKey?: string;
80
+ /** API secret (if required) */
81
+ apiSecret?: string;
82
+ /** Additional config specific to facilitator */
83
+ [key: string]: unknown;
84
+ }
85
+ /**
86
+ * Facilitator Interface
87
+ *
88
+ * All facilitators must implement this interface.
89
+ */
90
+ interface Facilitator {
91
+ /** Unique identifier for this facilitator */
92
+ readonly name: string;
93
+ /** Human-readable display name */
94
+ readonly displayName: string;
95
+ /** Supported networks (e.g., ["eip155:8453", "eip155:84532"]) */
96
+ readonly supportedNetworks: string[];
97
+ /**
98
+ * Check if facilitator is available and responsive
99
+ */
100
+ healthCheck(): Promise<HealthCheckResult>;
101
+ /**
102
+ * Verify a payment signature without executing it
103
+ *
104
+ * @param paymentPayload - The x402 payment payload from client
105
+ * @param requirements - The payment requirements from server
106
+ */
107
+ verify(paymentPayload: X402PaymentPayload, requirements: X402PaymentRequirements): Promise<VerifyResult>;
108
+ /**
109
+ * Settle a payment on-chain
110
+ *
111
+ * @param paymentPayload - The x402 payment payload from client
112
+ * @param requirements - The payment requirements from server
113
+ */
114
+ settle(paymentPayload: X402PaymentPayload, requirements: X402PaymentRequirements): Promise<SettleResult>;
115
+ /**
116
+ * Get current fee information (optional, for selection strategies)
117
+ */
118
+ getFee?(): Promise<FacilitatorFee>;
119
+ /**
120
+ * Check if this facilitator supports a given network
121
+ */
122
+ supportsNetwork(network: string): boolean;
123
+ }
124
+ /**
125
+ * Base class with common functionality
126
+ */
127
+ declare abstract class BaseFacilitator implements Facilitator {
128
+ abstract readonly name: string;
129
+ abstract readonly displayName: string;
130
+ abstract readonly supportedNetworks: string[];
131
+ abstract healthCheck(): Promise<HealthCheckResult>;
132
+ abstract verify(paymentPayload: X402PaymentPayload, requirements: X402PaymentRequirements): Promise<VerifyResult>;
133
+ abstract settle(paymentPayload: X402PaymentPayload, requirements: X402PaymentRequirements): Promise<SettleResult>;
134
+ supportsNetwork(network: string): boolean;
135
+ }
136
+
137
+ /**
138
+ * Facilitator Registry
139
+ *
140
+ * Central registry for all available facilitators.
141
+ * Supports selection strategies for failover, load balancing, etc.
142
+ */
143
+
144
+ /**
145
+ * Selection strategy for choosing facilitators
146
+ */
147
+ type SelectionStrategy = 'failover' | 'cheapest' | 'fastest' | 'random' | 'roundrobin';
148
+ /**
149
+ * Facilitator selection configuration
150
+ */
151
+ interface FacilitatorSelection {
152
+ /** Primary facilitator to use */
153
+ primary: string;
154
+ /** Fallback facilitators (in order of preference) */
155
+ fallback?: string[];
156
+ /** Selection strategy */
157
+ strategy?: SelectionStrategy;
158
+ /** Per-facilitator config overrides */
159
+ config?: Record<string, FacilitatorConfig>;
160
+ }
161
+ /**
162
+ * Factory function type for creating facilitators
163
+ */
164
+ type FacilitatorFactory = (config?: FacilitatorConfig) => Facilitator;
165
+ /**
166
+ * Facilitator Registry
167
+ *
168
+ * Manages available facilitators and provides selection logic.
169
+ */
170
+ declare class FacilitatorRegistry {
171
+ private factories;
172
+ private instances;
173
+ private selection;
174
+ private roundRobinIndex;
175
+ constructor(selection?: FacilitatorSelection);
176
+ /**
177
+ * Register a new facilitator factory
178
+ */
179
+ registerFactory(name: string, factory: FacilitatorFactory): void;
180
+ /**
181
+ * Get or create a facilitator instance
182
+ */
183
+ get(name: string, config?: FacilitatorConfig): Facilitator;
184
+ /**
185
+ * Get all configured facilitator names
186
+ */
187
+ getConfiguredNames(): string[];
188
+ /**
189
+ * Get list of facilitators based on selection strategy
190
+ */
191
+ private getOrderedFacilitators;
192
+ private shuffle;
193
+ private sortByCheapest;
194
+ private sortByFastest;
195
+ /**
196
+ * Verify payment using configured facilitators
197
+ */
198
+ verify(paymentPayload: X402PaymentPayload, requirements: X402PaymentRequirements): Promise<VerifyResult & {
199
+ facilitator: string;
200
+ }>;
201
+ /**
202
+ * Settle payment using configured facilitators
203
+ */
204
+ settle(paymentPayload: X402PaymentPayload, requirements: X402PaymentRequirements): Promise<SettleResult & {
205
+ facilitator: string;
206
+ }>;
207
+ /**
208
+ * Check health of all configured facilitators
209
+ */
210
+ healthCheckAll(): Promise<Record<string, HealthCheckResult>>;
211
+ /**
212
+ * Check if an error is transient (network/server issue) vs permanent (bad request)
213
+ */
214
+ private isTransientError;
215
+ /**
216
+ * Update selection configuration
217
+ */
218
+ setSelection(selection: FacilitatorSelection): void;
219
+ /**
220
+ * Get current selection configuration
221
+ */
222
+ getSelection(): FacilitatorSelection;
223
+ }
224
+ /**
225
+ * Get the default facilitator registry
226
+ */
227
+ declare function getDefaultRegistry(): FacilitatorRegistry;
228
+ /**
229
+ * Create a new registry with custom selection
230
+ */
231
+ declare function createRegistry(selection?: FacilitatorSelection): FacilitatorRegistry;
232
+
233
+ export { BaseFacilitator as B, type Facilitator as F, type HealthCheckResult as H, type SelectionStrategy as S, type VerifyResult as V, type X402PaymentPayload as X, type FacilitatorConfig as a, type FacilitatorFee as b, FacilitatorRegistry as c, type FacilitatorSelection as d, type SettleResult as e, type X402PaymentRequirements as f, createRegistry as g, getDefaultRegistry as h };
@@ -0,0 +1,233 @@
1
+ /**
2
+ * Facilitator Interface
3
+ *
4
+ * A facilitator is a service that handles x402 payment verification and settlement.
5
+ * This abstraction allows MoltsPay to support multiple facilitators.
6
+ *
7
+ * @see https://www.x402.org/ecosystem?category=facilitators
8
+ */
9
+ /**
10
+ * x402 Payment Payload (from client)
11
+ */
12
+ interface X402PaymentPayload {
13
+ x402Version: number;
14
+ scheme?: string;
15
+ network?: string;
16
+ accepted?: {
17
+ scheme: string;
18
+ network: string;
19
+ asset: string;
20
+ amount: string;
21
+ payTo: string;
22
+ maxTimeoutSeconds: number;
23
+ extra?: Record<string, unknown>;
24
+ };
25
+ payload: unknown;
26
+ }
27
+ /**
28
+ * x402 Payment Requirements (server specifies what it accepts)
29
+ */
30
+ interface X402PaymentRequirements {
31
+ scheme: string;
32
+ network: string;
33
+ asset: string;
34
+ amount: string;
35
+ payTo: string;
36
+ maxTimeoutSeconds: number;
37
+ extra?: Record<string, unknown>;
38
+ }
39
+ /**
40
+ * Result of payment verification
41
+ */
42
+ interface VerifyResult {
43
+ valid: boolean;
44
+ error?: string;
45
+ details?: Record<string, unknown>;
46
+ }
47
+ /**
48
+ * Result of payment settlement
49
+ */
50
+ interface SettleResult {
51
+ success: boolean;
52
+ transaction?: string;
53
+ error?: string;
54
+ status?: string;
55
+ }
56
+ /**
57
+ * Facilitator health check result
58
+ */
59
+ interface HealthCheckResult {
60
+ healthy: boolean;
61
+ latencyMs?: number;
62
+ error?: string;
63
+ }
64
+ /**
65
+ * Facilitator fee information (for selection strategies)
66
+ */
67
+ interface FacilitatorFee {
68
+ perTx: number;
69
+ currency: string;
70
+ freeQuota?: number;
71
+ }
72
+ /**
73
+ * Facilitator configuration
74
+ */
75
+ interface FacilitatorConfig {
76
+ /** Facilitator endpoint URL */
77
+ endpoint?: string;
78
+ /** API key (if required) */
79
+ apiKey?: string;
80
+ /** API secret (if required) */
81
+ apiSecret?: string;
82
+ /** Additional config specific to facilitator */
83
+ [key: string]: unknown;
84
+ }
85
+ /**
86
+ * Facilitator Interface
87
+ *
88
+ * All facilitators must implement this interface.
89
+ */
90
+ interface Facilitator {
91
+ /** Unique identifier for this facilitator */
92
+ readonly name: string;
93
+ /** Human-readable display name */
94
+ readonly displayName: string;
95
+ /** Supported networks (e.g., ["eip155:8453", "eip155:84532"]) */
96
+ readonly supportedNetworks: string[];
97
+ /**
98
+ * Check if facilitator is available and responsive
99
+ */
100
+ healthCheck(): Promise<HealthCheckResult>;
101
+ /**
102
+ * Verify a payment signature without executing it
103
+ *
104
+ * @param paymentPayload - The x402 payment payload from client
105
+ * @param requirements - The payment requirements from server
106
+ */
107
+ verify(paymentPayload: X402PaymentPayload, requirements: X402PaymentRequirements): Promise<VerifyResult>;
108
+ /**
109
+ * Settle a payment on-chain
110
+ *
111
+ * @param paymentPayload - The x402 payment payload from client
112
+ * @param requirements - The payment requirements from server
113
+ */
114
+ settle(paymentPayload: X402PaymentPayload, requirements: X402PaymentRequirements): Promise<SettleResult>;
115
+ /**
116
+ * Get current fee information (optional, for selection strategies)
117
+ */
118
+ getFee?(): Promise<FacilitatorFee>;
119
+ /**
120
+ * Check if this facilitator supports a given network
121
+ */
122
+ supportsNetwork(network: string): boolean;
123
+ }
124
+ /**
125
+ * Base class with common functionality
126
+ */
127
+ declare abstract class BaseFacilitator implements Facilitator {
128
+ abstract readonly name: string;
129
+ abstract readonly displayName: string;
130
+ abstract readonly supportedNetworks: string[];
131
+ abstract healthCheck(): Promise<HealthCheckResult>;
132
+ abstract verify(paymentPayload: X402PaymentPayload, requirements: X402PaymentRequirements): Promise<VerifyResult>;
133
+ abstract settle(paymentPayload: X402PaymentPayload, requirements: X402PaymentRequirements): Promise<SettleResult>;
134
+ supportsNetwork(network: string): boolean;
135
+ }
136
+
137
+ /**
138
+ * Facilitator Registry
139
+ *
140
+ * Central registry for all available facilitators.
141
+ * Supports selection strategies for failover, load balancing, etc.
142
+ */
143
+
144
+ /**
145
+ * Selection strategy for choosing facilitators
146
+ */
147
+ type SelectionStrategy = 'failover' | 'cheapest' | 'fastest' | 'random' | 'roundrobin';
148
+ /**
149
+ * Facilitator selection configuration
150
+ */
151
+ interface FacilitatorSelection {
152
+ /** Primary facilitator to use */
153
+ primary: string;
154
+ /** Fallback facilitators (in order of preference) */
155
+ fallback?: string[];
156
+ /** Selection strategy */
157
+ strategy?: SelectionStrategy;
158
+ /** Per-facilitator config overrides */
159
+ config?: Record<string, FacilitatorConfig>;
160
+ }
161
+ /**
162
+ * Factory function type for creating facilitators
163
+ */
164
+ type FacilitatorFactory = (config?: FacilitatorConfig) => Facilitator;
165
+ /**
166
+ * Facilitator Registry
167
+ *
168
+ * Manages available facilitators and provides selection logic.
169
+ */
170
+ declare class FacilitatorRegistry {
171
+ private factories;
172
+ private instances;
173
+ private selection;
174
+ private roundRobinIndex;
175
+ constructor(selection?: FacilitatorSelection);
176
+ /**
177
+ * Register a new facilitator factory
178
+ */
179
+ registerFactory(name: string, factory: FacilitatorFactory): void;
180
+ /**
181
+ * Get or create a facilitator instance
182
+ */
183
+ get(name: string, config?: FacilitatorConfig): Facilitator;
184
+ /**
185
+ * Get all configured facilitator names
186
+ */
187
+ getConfiguredNames(): string[];
188
+ /**
189
+ * Get list of facilitators based on selection strategy
190
+ */
191
+ private getOrderedFacilitators;
192
+ private shuffle;
193
+ private sortByCheapest;
194
+ private sortByFastest;
195
+ /**
196
+ * Verify payment using configured facilitators
197
+ */
198
+ verify(paymentPayload: X402PaymentPayload, requirements: X402PaymentRequirements): Promise<VerifyResult & {
199
+ facilitator: string;
200
+ }>;
201
+ /**
202
+ * Settle payment using configured facilitators
203
+ */
204
+ settle(paymentPayload: X402PaymentPayload, requirements: X402PaymentRequirements): Promise<SettleResult & {
205
+ facilitator: string;
206
+ }>;
207
+ /**
208
+ * Check health of all configured facilitators
209
+ */
210
+ healthCheckAll(): Promise<Record<string, HealthCheckResult>>;
211
+ /**
212
+ * Check if an error is transient (network/server issue) vs permanent (bad request)
213
+ */
214
+ private isTransientError;
215
+ /**
216
+ * Update selection configuration
217
+ */
218
+ setSelection(selection: FacilitatorSelection): void;
219
+ /**
220
+ * Get current selection configuration
221
+ */
222
+ getSelection(): FacilitatorSelection;
223
+ }
224
+ /**
225
+ * Get the default facilitator registry
226
+ */
227
+ declare function getDefaultRegistry(): FacilitatorRegistry;
228
+ /**
229
+ * Create a new registry with custom selection
230
+ */
231
+ declare function createRegistry(selection?: FacilitatorSelection): FacilitatorRegistry;
232
+
233
+ export { BaseFacilitator as B, type Facilitator as F, type HealthCheckResult as H, type SelectionStrategy as S, type VerifyResult as V, type X402PaymentPayload as X, type FacilitatorConfig as a, type FacilitatorFee as b, FacilitatorRegistry as c, type FacilitatorSelection as d, type SettleResult as e, type X402PaymentRequirements as f, createRegistry as g, getDefaultRegistry as h };
@@ -1,3 +1,5 @@
1
+ import { d as FacilitatorSelection } from '../registry-OsEO2dOu.mjs';
2
+
1
3
  /**
2
4
  * MoltsPay Server Types
3
5
  */
@@ -78,8 +80,7 @@ interface MoltsPayServerOptions {
78
80
  /**
79
81
  * MoltsPay Server - Payment infrastructure for AI Agents
80
82
  *
81
- * Supports both testnet (x402.org) and mainnet (CDP) facilitators.
82
- * Server does NOT need private key - facilitator handles on-chain settlement.
83
+ * Now uses pluggable Facilitator abstraction for payment verification/settlement.
83
84
  *
84
85
  * Environment variables (from ~/.moltspay/.env or process.env):
85
86
  * USE_MAINNET=true - Use Base mainnet (requires CDP keys)
@@ -92,14 +93,21 @@ interface MoltsPayServerOptions {
92
93
  * server.listen(3000);
93
94
  */
94
95
 
96
+ /**
97
+ * Extended server options with facilitator config
98
+ */
99
+ interface MoltsPayServerOptionsExtended extends MoltsPayServerOptions {
100
+ /** Facilitator selection configuration */
101
+ facilitators?: FacilitatorSelection;
102
+ }
95
103
  declare class MoltsPayServer {
96
104
  private manifest;
97
105
  private skills;
98
106
  private options;
99
- private cdpConfig;
100
- private facilitatorUrl;
107
+ private registry;
101
108
  private networkId;
102
- constructor(servicesPath: string, options?: MoltsPayServerOptions);
109
+ private useMainnet;
110
+ constructor(servicesPath: string, options?: MoltsPayServerOptionsExtended);
103
111
  /**
104
112
  * Register a skill handler for a service
105
113
  */
@@ -116,6 +124,10 @@ declare class MoltsPayServer {
116
124
  * GET /services - List available services
117
125
  */
118
126
  private handleGetServices;
127
+ /**
128
+ * GET /health - Health check endpoint
129
+ */
130
+ private handleHealthCheck;
119
131
  /**
120
132
  * POST /execute - Execute service with x402 payment
121
133
  */
@@ -129,19 +141,11 @@ declare class MoltsPayServer {
129
141
  */
130
142
  private validatePayment;
131
143
  /**
132
- * Build complete payment requirements for facilitator
144
+ * Build payment requirements for facilitator
133
145
  */
134
146
  private buildPaymentRequirements;
135
- /**
136
- * Verify payment with facilitator (testnet or CDP)
137
- */
138
- private verifyWithFacilitator;
139
- /**
140
- * Settle payment with facilitator (execute on-chain transfer)
141
- */
142
- private settleWithFacilitator;
143
147
  private readBody;
144
148
  private sendJson;
145
149
  }
146
150
 
147
- export { type Charge, type ChargeStatus, type InputField, MoltsPayServer, type MoltsPayServerOptions, type OutputField, type PaymentRequest, type ProviderConfig, type RegisteredSkill, type ServiceConfig, type ServicesManifest, type SkillFunction, type VerifyRequest };
151
+ export { type Charge, type ChargeStatus, type InputField, MoltsPayServer, type MoltsPayServerOptions, type MoltsPayServerOptionsExtended, type OutputField, type PaymentRequest, type ProviderConfig, type RegisteredSkill, type ServiceConfig, type ServicesManifest, type SkillFunction, type VerifyRequest };
@@ -1,3 +1,5 @@
1
+ import { d as FacilitatorSelection } from '../registry-OsEO2dOu.js';
2
+
1
3
  /**
2
4
  * MoltsPay Server Types
3
5
  */
@@ -78,8 +80,7 @@ interface MoltsPayServerOptions {
78
80
  /**
79
81
  * MoltsPay Server - Payment infrastructure for AI Agents
80
82
  *
81
- * Supports both testnet (x402.org) and mainnet (CDP) facilitators.
82
- * Server does NOT need private key - facilitator handles on-chain settlement.
83
+ * Now uses pluggable Facilitator abstraction for payment verification/settlement.
83
84
  *
84
85
  * Environment variables (from ~/.moltspay/.env or process.env):
85
86
  * USE_MAINNET=true - Use Base mainnet (requires CDP keys)
@@ -92,14 +93,21 @@ interface MoltsPayServerOptions {
92
93
  * server.listen(3000);
93
94
  */
94
95
 
96
+ /**
97
+ * Extended server options with facilitator config
98
+ */
99
+ interface MoltsPayServerOptionsExtended extends MoltsPayServerOptions {
100
+ /** Facilitator selection configuration */
101
+ facilitators?: FacilitatorSelection;
102
+ }
95
103
  declare class MoltsPayServer {
96
104
  private manifest;
97
105
  private skills;
98
106
  private options;
99
- private cdpConfig;
100
- private facilitatorUrl;
107
+ private registry;
101
108
  private networkId;
102
- constructor(servicesPath: string, options?: MoltsPayServerOptions);
109
+ private useMainnet;
110
+ constructor(servicesPath: string, options?: MoltsPayServerOptionsExtended);
103
111
  /**
104
112
  * Register a skill handler for a service
105
113
  */
@@ -116,6 +124,10 @@ declare class MoltsPayServer {
116
124
  * GET /services - List available services
117
125
  */
118
126
  private handleGetServices;
127
+ /**
128
+ * GET /health - Health check endpoint
129
+ */
130
+ private handleHealthCheck;
119
131
  /**
120
132
  * POST /execute - Execute service with x402 payment
121
133
  */
@@ -129,19 +141,11 @@ declare class MoltsPayServer {
129
141
  */
130
142
  private validatePayment;
131
143
  /**
132
- * Build complete payment requirements for facilitator
144
+ * Build payment requirements for facilitator
133
145
  */
134
146
  private buildPaymentRequirements;
135
- /**
136
- * Verify payment with facilitator (testnet or CDP)
137
- */
138
- private verifyWithFacilitator;
139
- /**
140
- * Settle payment with facilitator (execute on-chain transfer)
141
- */
142
- private settleWithFacilitator;
143
147
  private readBody;
144
148
  private sendJson;
145
149
  }
146
150
 
147
- export { type Charge, type ChargeStatus, type InputField, MoltsPayServer, type MoltsPayServerOptions, type OutputField, type PaymentRequest, type ProviderConfig, type RegisteredSkill, type ServiceConfig, type ServicesManifest, type SkillFunction, type VerifyRequest };
151
+ export { type Charge, type ChargeStatus, type InputField, MoltsPayServer, type MoltsPayServerOptions, type MoltsPayServerOptionsExtended, type OutputField, type PaymentRequest, type ProviderConfig, type RegisteredSkill, type ServiceConfig, type ServicesManifest, type SkillFunction, type VerifyRequest };