@openfacilitator/sdk 0.1.1 → 0.2.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.
- package/README.md +19 -20
- package/dist/index.d.mts +30 -3
- package/dist/index.d.ts +30 -3
- package/dist/index.js +64 -8
- package/dist/index.mjs +64 -8
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -28,20 +28,32 @@ const facilitator = createDefaultFacilitator();
|
|
|
28
28
|
### Verify a Payment
|
|
29
29
|
|
|
30
30
|
```typescript
|
|
31
|
-
|
|
31
|
+
// Payment payload from the client
|
|
32
|
+
const payment = {
|
|
32
33
|
x402Version: 1,
|
|
33
34
|
scheme: 'exact',
|
|
34
35
|
network: 'base',
|
|
35
36
|
payload: {
|
|
36
37
|
signature: '0x...',
|
|
37
38
|
authorization: {
|
|
38
|
-
from: '
|
|
39
|
-
to: '
|
|
39
|
+
from: '0xSenderAddress',
|
|
40
|
+
to: '0xRecipientAddress',
|
|
40
41
|
amount: '1000000',
|
|
41
|
-
asset: '
|
|
42
|
+
asset: '0xTokenAddress',
|
|
42
43
|
},
|
|
43
44
|
},
|
|
44
|
-
}
|
|
45
|
+
};
|
|
46
|
+
|
|
47
|
+
// Requirements from the server/resource
|
|
48
|
+
const requirements = {
|
|
49
|
+
scheme: 'exact',
|
|
50
|
+
network: 'base',
|
|
51
|
+
maxAmountRequired: '1000000',
|
|
52
|
+
asset: '0xTokenAddress',
|
|
53
|
+
payTo: '0xRecipientAddress',
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
const result = await facilitator.verify(payment, requirements);
|
|
45
57
|
|
|
46
58
|
if (result.valid) {
|
|
47
59
|
console.log('Payment is valid!');
|
|
@@ -53,20 +65,7 @@ if (result.valid) {
|
|
|
53
65
|
### Settle a Payment
|
|
54
66
|
|
|
55
67
|
```typescript
|
|
56
|
-
const result = await facilitator.settle(
|
|
57
|
-
x402Version: 1,
|
|
58
|
-
scheme: 'exact',
|
|
59
|
-
network: 'base',
|
|
60
|
-
payload: {
|
|
61
|
-
signature: '0x...',
|
|
62
|
-
authorization: {
|
|
63
|
-
from: '0x...',
|
|
64
|
-
to: '0x...',
|
|
65
|
-
amount: '1000000',
|
|
66
|
-
asset: '0x...',
|
|
67
|
-
},
|
|
68
|
-
},
|
|
69
|
-
});
|
|
68
|
+
const result = await facilitator.settle(payment, requirements);
|
|
70
69
|
|
|
71
70
|
if (result.success) {
|
|
72
71
|
console.log('Transaction hash:', result.transactionHash);
|
|
@@ -117,7 +116,7 @@ import {
|
|
|
117
116
|
} from '@openfacilitator/sdk';
|
|
118
117
|
|
|
119
118
|
try {
|
|
120
|
-
await facilitator.settle(payment);
|
|
119
|
+
await facilitator.settle(payment, requirements);
|
|
121
120
|
} catch (error) {
|
|
122
121
|
if (error instanceof SettlementError) {
|
|
123
122
|
console.error('Settlement failed:', error.message);
|
package/dist/index.d.mts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
interface FacilitatorConfig {
|
|
2
|
-
/** Facilitator URL (
|
|
3
|
-
url
|
|
2
|
+
/** Facilitator URL (defaults to https://pay.openfacilitator.io) */
|
|
3
|
+
url?: string;
|
|
4
4
|
/** Optional timeout in ms (default: 30000) */
|
|
5
5
|
timeout?: number;
|
|
6
6
|
/** Optional custom headers */
|
|
@@ -137,7 +137,9 @@ declare class OpenFacilitator {
|
|
|
137
137
|
private readonly baseUrl;
|
|
138
138
|
private readonly timeout;
|
|
139
139
|
private readonly headers;
|
|
140
|
-
|
|
140
|
+
private supportedCache;
|
|
141
|
+
private supportedPromise;
|
|
142
|
+
constructor(config?: FacilitatorConfig);
|
|
141
143
|
/**
|
|
142
144
|
* Get the facilitator URL
|
|
143
145
|
*/
|
|
@@ -162,6 +164,30 @@ declare class OpenFacilitator {
|
|
|
162
164
|
* Health check - verify facilitator is reachable
|
|
163
165
|
*/
|
|
164
166
|
health(): Promise<boolean>;
|
|
167
|
+
/**
|
|
168
|
+
* Get fee payer address for a specific network.
|
|
169
|
+
* Fee payers are wallet addresses that pay transaction fees when settling payments.
|
|
170
|
+
* Currently only Solana networks have fee payers.
|
|
171
|
+
* @param network - Network identifier (e.g., "solana", "solana:mainnet")
|
|
172
|
+
* @returns Fee payer address or undefined if not available for the network
|
|
173
|
+
*/
|
|
174
|
+
getFeePayer(network: string): Promise<string | undefined>;
|
|
175
|
+
/**
|
|
176
|
+
* Get all fee payers mapped by network.
|
|
177
|
+
* Fee payers are wallet addresses that pay transaction fees when settling payments.
|
|
178
|
+
* @returns Map of network identifiers to fee payer addresses
|
|
179
|
+
*/
|
|
180
|
+
getFeePayerMap(): Promise<Record<string, string>>;
|
|
181
|
+
/**
|
|
182
|
+
* Get cached supported response, fetching if needed.
|
|
183
|
+
* Uses deduplication to prevent concurrent requests.
|
|
184
|
+
*/
|
|
185
|
+
private getSupportedCached;
|
|
186
|
+
/**
|
|
187
|
+
* Clear cached supported response.
|
|
188
|
+
* Useful if facilitator configuration may have changed.
|
|
189
|
+
*/
|
|
190
|
+
clearCache(): void;
|
|
165
191
|
/**
|
|
166
192
|
* Internal request helper
|
|
167
193
|
*/
|
|
@@ -169,6 +195,7 @@ declare class OpenFacilitator {
|
|
|
169
195
|
}
|
|
170
196
|
/**
|
|
171
197
|
* Create a facilitator client with default OpenFacilitator URL
|
|
198
|
+
* @deprecated Just use `new OpenFacilitator()` - it defaults to the public endpoint
|
|
172
199
|
*/
|
|
173
200
|
declare function createDefaultFacilitator(): OpenFacilitator;
|
|
174
201
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
interface FacilitatorConfig {
|
|
2
|
-
/** Facilitator URL (
|
|
3
|
-
url
|
|
2
|
+
/** Facilitator URL (defaults to https://pay.openfacilitator.io) */
|
|
3
|
+
url?: string;
|
|
4
4
|
/** Optional timeout in ms (default: 30000) */
|
|
5
5
|
timeout?: number;
|
|
6
6
|
/** Optional custom headers */
|
|
@@ -137,7 +137,9 @@ declare class OpenFacilitator {
|
|
|
137
137
|
private readonly baseUrl;
|
|
138
138
|
private readonly timeout;
|
|
139
139
|
private readonly headers;
|
|
140
|
-
|
|
140
|
+
private supportedCache;
|
|
141
|
+
private supportedPromise;
|
|
142
|
+
constructor(config?: FacilitatorConfig);
|
|
141
143
|
/**
|
|
142
144
|
* Get the facilitator URL
|
|
143
145
|
*/
|
|
@@ -162,6 +164,30 @@ declare class OpenFacilitator {
|
|
|
162
164
|
* Health check - verify facilitator is reachable
|
|
163
165
|
*/
|
|
164
166
|
health(): Promise<boolean>;
|
|
167
|
+
/**
|
|
168
|
+
* Get fee payer address for a specific network.
|
|
169
|
+
* Fee payers are wallet addresses that pay transaction fees when settling payments.
|
|
170
|
+
* Currently only Solana networks have fee payers.
|
|
171
|
+
* @param network - Network identifier (e.g., "solana", "solana:mainnet")
|
|
172
|
+
* @returns Fee payer address or undefined if not available for the network
|
|
173
|
+
*/
|
|
174
|
+
getFeePayer(network: string): Promise<string | undefined>;
|
|
175
|
+
/**
|
|
176
|
+
* Get all fee payers mapped by network.
|
|
177
|
+
* Fee payers are wallet addresses that pay transaction fees when settling payments.
|
|
178
|
+
* @returns Map of network identifiers to fee payer addresses
|
|
179
|
+
*/
|
|
180
|
+
getFeePayerMap(): Promise<Record<string, string>>;
|
|
181
|
+
/**
|
|
182
|
+
* Get cached supported response, fetching if needed.
|
|
183
|
+
* Uses deduplication to prevent concurrent requests.
|
|
184
|
+
*/
|
|
185
|
+
private getSupportedCached;
|
|
186
|
+
/**
|
|
187
|
+
* Clear cached supported response.
|
|
188
|
+
* Useful if facilitator configuration may have changed.
|
|
189
|
+
*/
|
|
190
|
+
clearCache(): void;
|
|
165
191
|
/**
|
|
166
192
|
* Internal request helper
|
|
167
193
|
*/
|
|
@@ -169,6 +195,7 @@ declare class OpenFacilitator {
|
|
|
169
195
|
}
|
|
170
196
|
/**
|
|
171
197
|
* Create a facilitator client with default OpenFacilitator URL
|
|
198
|
+
* @deprecated Just use `new OpenFacilitator()` - it defaults to the public endpoint
|
|
172
199
|
*/
|
|
173
200
|
declare function createDefaultFacilitator(): OpenFacilitator;
|
|
174
201
|
|
package/dist/index.js
CHANGED
|
@@ -89,12 +89,12 @@ function isPaymentPayload(value) {
|
|
|
89
89
|
|
|
90
90
|
// src/client.ts
|
|
91
91
|
var DEFAULT_TIMEOUT = 3e4;
|
|
92
|
+
var DEFAULT_URL = "https://pay.openfacilitator.io";
|
|
92
93
|
var OpenFacilitator = class {
|
|
93
|
-
constructor(config) {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
this.baseUrl = normalizeUrl(config.url);
|
|
94
|
+
constructor(config = {}) {
|
|
95
|
+
this.supportedCache = null;
|
|
96
|
+
this.supportedPromise = null;
|
|
97
|
+
this.baseUrl = normalizeUrl(config.url ?? DEFAULT_URL);
|
|
98
98
|
this.timeout = config.timeout ?? DEFAULT_TIMEOUT;
|
|
99
99
|
this.headers = {
|
|
100
100
|
"Content-Type": "application/json",
|
|
@@ -185,6 +185,64 @@ var OpenFacilitator = class {
|
|
|
185
185
|
return false;
|
|
186
186
|
}
|
|
187
187
|
}
|
|
188
|
+
/**
|
|
189
|
+
* Get fee payer address for a specific network.
|
|
190
|
+
* Fee payers are wallet addresses that pay transaction fees when settling payments.
|
|
191
|
+
* Currently only Solana networks have fee payers.
|
|
192
|
+
* @param network - Network identifier (e.g., "solana", "solana:mainnet")
|
|
193
|
+
* @returns Fee payer address or undefined if not available for the network
|
|
194
|
+
*/
|
|
195
|
+
async getFeePayer(network) {
|
|
196
|
+
const supported = await this.getSupportedCached();
|
|
197
|
+
for (const kind of supported.kinds) {
|
|
198
|
+
if (kind.network === network || kind.network.includes(network) || network.includes(kind.network)) {
|
|
199
|
+
if (kind.extra?.feePayer) {
|
|
200
|
+
return kind.extra.feePayer;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
return void 0;
|
|
205
|
+
}
|
|
206
|
+
/**
|
|
207
|
+
* Get all fee payers mapped by network.
|
|
208
|
+
* Fee payers are wallet addresses that pay transaction fees when settling payments.
|
|
209
|
+
* @returns Map of network identifiers to fee payer addresses
|
|
210
|
+
*/
|
|
211
|
+
async getFeePayerMap() {
|
|
212
|
+
const supported = await this.getSupportedCached();
|
|
213
|
+
const feePayerMap = {};
|
|
214
|
+
for (const kind of supported.kinds) {
|
|
215
|
+
if (kind.extra?.feePayer) {
|
|
216
|
+
feePayerMap[kind.network] = kind.extra.feePayer;
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return feePayerMap;
|
|
220
|
+
}
|
|
221
|
+
/**
|
|
222
|
+
* Get cached supported response, fetching if needed.
|
|
223
|
+
* Uses deduplication to prevent concurrent requests.
|
|
224
|
+
*/
|
|
225
|
+
async getSupportedCached() {
|
|
226
|
+
if (this.supportedCache) {
|
|
227
|
+
return this.supportedCache;
|
|
228
|
+
}
|
|
229
|
+
if (!this.supportedPromise) {
|
|
230
|
+
this.supportedPromise = this.supported().then((response) => {
|
|
231
|
+
this.supportedCache = response;
|
|
232
|
+
this.supportedPromise = null;
|
|
233
|
+
return response;
|
|
234
|
+
});
|
|
235
|
+
}
|
|
236
|
+
return this.supportedPromise;
|
|
237
|
+
}
|
|
238
|
+
/**
|
|
239
|
+
* Clear cached supported response.
|
|
240
|
+
* Useful if facilitator configuration may have changed.
|
|
241
|
+
*/
|
|
242
|
+
clearCache() {
|
|
243
|
+
this.supportedCache = null;
|
|
244
|
+
this.supportedPromise = null;
|
|
245
|
+
}
|
|
188
246
|
/**
|
|
189
247
|
* Internal request helper
|
|
190
248
|
*/
|
|
@@ -226,9 +284,7 @@ var OpenFacilitator = class {
|
|
|
226
284
|
}
|
|
227
285
|
};
|
|
228
286
|
function createDefaultFacilitator() {
|
|
229
|
-
return new OpenFacilitator(
|
|
230
|
-
url: "https://x402.openfacilitator.io"
|
|
231
|
-
});
|
|
287
|
+
return new OpenFacilitator();
|
|
232
288
|
}
|
|
233
289
|
|
|
234
290
|
// src/networks.ts
|
package/dist/index.mjs
CHANGED
|
@@ -48,12 +48,12 @@ function isPaymentPayload(value) {
|
|
|
48
48
|
|
|
49
49
|
// src/client.ts
|
|
50
50
|
var DEFAULT_TIMEOUT = 3e4;
|
|
51
|
+
var DEFAULT_URL = "https://pay.openfacilitator.io";
|
|
51
52
|
var OpenFacilitator = class {
|
|
52
|
-
constructor(config) {
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
this.baseUrl = normalizeUrl(config.url);
|
|
53
|
+
constructor(config = {}) {
|
|
54
|
+
this.supportedCache = null;
|
|
55
|
+
this.supportedPromise = null;
|
|
56
|
+
this.baseUrl = normalizeUrl(config.url ?? DEFAULT_URL);
|
|
57
57
|
this.timeout = config.timeout ?? DEFAULT_TIMEOUT;
|
|
58
58
|
this.headers = {
|
|
59
59
|
"Content-Type": "application/json",
|
|
@@ -144,6 +144,64 @@ var OpenFacilitator = class {
|
|
|
144
144
|
return false;
|
|
145
145
|
}
|
|
146
146
|
}
|
|
147
|
+
/**
|
|
148
|
+
* Get fee payer address for a specific network.
|
|
149
|
+
* Fee payers are wallet addresses that pay transaction fees when settling payments.
|
|
150
|
+
* Currently only Solana networks have fee payers.
|
|
151
|
+
* @param network - Network identifier (e.g., "solana", "solana:mainnet")
|
|
152
|
+
* @returns Fee payer address or undefined if not available for the network
|
|
153
|
+
*/
|
|
154
|
+
async getFeePayer(network) {
|
|
155
|
+
const supported = await this.getSupportedCached();
|
|
156
|
+
for (const kind of supported.kinds) {
|
|
157
|
+
if (kind.network === network || kind.network.includes(network) || network.includes(kind.network)) {
|
|
158
|
+
if (kind.extra?.feePayer) {
|
|
159
|
+
return kind.extra.feePayer;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
return void 0;
|
|
164
|
+
}
|
|
165
|
+
/**
|
|
166
|
+
* Get all fee payers mapped by network.
|
|
167
|
+
* Fee payers are wallet addresses that pay transaction fees when settling payments.
|
|
168
|
+
* @returns Map of network identifiers to fee payer addresses
|
|
169
|
+
*/
|
|
170
|
+
async getFeePayerMap() {
|
|
171
|
+
const supported = await this.getSupportedCached();
|
|
172
|
+
const feePayerMap = {};
|
|
173
|
+
for (const kind of supported.kinds) {
|
|
174
|
+
if (kind.extra?.feePayer) {
|
|
175
|
+
feePayerMap[kind.network] = kind.extra.feePayer;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
return feePayerMap;
|
|
179
|
+
}
|
|
180
|
+
/**
|
|
181
|
+
* Get cached supported response, fetching if needed.
|
|
182
|
+
* Uses deduplication to prevent concurrent requests.
|
|
183
|
+
*/
|
|
184
|
+
async getSupportedCached() {
|
|
185
|
+
if (this.supportedCache) {
|
|
186
|
+
return this.supportedCache;
|
|
187
|
+
}
|
|
188
|
+
if (!this.supportedPromise) {
|
|
189
|
+
this.supportedPromise = this.supported().then((response) => {
|
|
190
|
+
this.supportedCache = response;
|
|
191
|
+
this.supportedPromise = null;
|
|
192
|
+
return response;
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
return this.supportedPromise;
|
|
196
|
+
}
|
|
197
|
+
/**
|
|
198
|
+
* Clear cached supported response.
|
|
199
|
+
* Useful if facilitator configuration may have changed.
|
|
200
|
+
*/
|
|
201
|
+
clearCache() {
|
|
202
|
+
this.supportedCache = null;
|
|
203
|
+
this.supportedPromise = null;
|
|
204
|
+
}
|
|
147
205
|
/**
|
|
148
206
|
* Internal request helper
|
|
149
207
|
*/
|
|
@@ -185,9 +243,7 @@ var OpenFacilitator = class {
|
|
|
185
243
|
}
|
|
186
244
|
};
|
|
187
245
|
function createDefaultFacilitator() {
|
|
188
|
-
return new OpenFacilitator(
|
|
189
|
-
url: "https://x402.openfacilitator.io"
|
|
190
|
-
});
|
|
246
|
+
return new OpenFacilitator();
|
|
191
247
|
}
|
|
192
248
|
|
|
193
249
|
// src/networks.ts
|