@openfacilitator/sdk 0.2.0 → 0.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.
- package/dist/index.d.mts +379 -9
- package/dist/index.d.ts +379 -9
- package/dist/index.js +438 -2
- package/dist/index.mjs +427 -1
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -73,9 +73,11 @@ interface PaymentRequirements {
|
|
|
73
73
|
}
|
|
74
74
|
interface VerifyResponse {
|
|
75
75
|
/** Whether the payment is valid */
|
|
76
|
-
|
|
77
|
-
/**
|
|
78
|
-
|
|
76
|
+
isValid: boolean;
|
|
77
|
+
/** Reason if invalid (x402 standard) */
|
|
78
|
+
invalidReason?: string;
|
|
79
|
+
/** Payer address */
|
|
80
|
+
payer?: string;
|
|
79
81
|
/** Additional verification details */
|
|
80
82
|
details?: {
|
|
81
83
|
/** Verified amount */
|
|
@@ -89,12 +91,14 @@ interface VerifyResponse {
|
|
|
89
91
|
interface SettleResponse {
|
|
90
92
|
/** Whether settlement was successful */
|
|
91
93
|
success: boolean;
|
|
92
|
-
/** Transaction hash/signature */
|
|
93
|
-
|
|
94
|
+
/** Transaction hash/signature (empty string when failed, x402 standard) */
|
|
95
|
+
transaction: string;
|
|
96
|
+
/** Payer address (x402 standard) */
|
|
97
|
+
payer: string;
|
|
94
98
|
/** Network the transaction was settled on */
|
|
95
|
-
network
|
|
96
|
-
/** Error
|
|
97
|
-
|
|
99
|
+
network: string;
|
|
100
|
+
/** Error reason if failed (x402 standard) */
|
|
101
|
+
errorReason?: string;
|
|
98
102
|
}
|
|
99
103
|
interface SupportedResponse {
|
|
100
104
|
/** Supported payment kinds */
|
|
@@ -253,4 +257,370 @@ declare function getTestnets(): NetworkInfo[];
|
|
|
253
257
|
*/
|
|
254
258
|
declare function isPaymentPayload(value: unknown): value is PaymentPayload;
|
|
255
259
|
|
|
256
|
-
|
|
260
|
+
/**
|
|
261
|
+
* Claims module for reporting failures and managing refunds
|
|
262
|
+
*/
|
|
263
|
+
interface ReportFailureParams {
|
|
264
|
+
/** The facilitator URL (e.g., https://api.openfacilitator.io) */
|
|
265
|
+
facilitatorUrl: string;
|
|
266
|
+
/** The API key from server registration */
|
|
267
|
+
apiKey: string;
|
|
268
|
+
/** The original transaction hash that failed */
|
|
269
|
+
originalTxHash: string;
|
|
270
|
+
/** The user's wallet address to receive the refund */
|
|
271
|
+
userWallet: string;
|
|
272
|
+
/** The amount to refund (in atomic units, e.g., "1000000" for $1 USDC) */
|
|
273
|
+
amount: string;
|
|
274
|
+
/** The asset address (token contract) */
|
|
275
|
+
asset: string;
|
|
276
|
+
/** The network (e.g., "base", "solana") */
|
|
277
|
+
network: string;
|
|
278
|
+
/** Optional reason for the failure */
|
|
279
|
+
reason?: string;
|
|
280
|
+
}
|
|
281
|
+
interface ReportFailureResponse {
|
|
282
|
+
success: boolean;
|
|
283
|
+
claimId?: string;
|
|
284
|
+
error?: string;
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Report a failure to the facilitator to create a refund claim
|
|
288
|
+
*
|
|
289
|
+
* @example
|
|
290
|
+
* ```typescript
|
|
291
|
+
* import { reportFailure } from '@openfacilitator/sdk/claims';
|
|
292
|
+
*
|
|
293
|
+
* const result = await reportFailure({
|
|
294
|
+
* facilitatorUrl: 'https://my-facilitator.openfacilitator.io',
|
|
295
|
+
* apiKey: 'sk_...',
|
|
296
|
+
* originalTxHash: '0x123...',
|
|
297
|
+
* userWallet: '0xabc...',
|
|
298
|
+
* amount: '1000000', // 1 USDC
|
|
299
|
+
* asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
|
|
300
|
+
* network: 'base',
|
|
301
|
+
* reason: 'Service unavailable',
|
|
302
|
+
* });
|
|
303
|
+
*
|
|
304
|
+
* if (result.success) {
|
|
305
|
+
* console.log('Claim created:', result.claimId);
|
|
306
|
+
* } else {
|
|
307
|
+
* console.error('Failed to create claim:', result.error);
|
|
308
|
+
* }
|
|
309
|
+
* ```
|
|
310
|
+
*/
|
|
311
|
+
declare function reportFailure(params: ReportFailureParams): Promise<ReportFailureResponse>;
|
|
312
|
+
interface GetClaimableParams {
|
|
313
|
+
/** The facilitator URL */
|
|
314
|
+
facilitatorUrl: string;
|
|
315
|
+
/** The user's wallet address */
|
|
316
|
+
wallet: string;
|
|
317
|
+
/** Optional facilitator subdomain filter */
|
|
318
|
+
facilitator?: string;
|
|
319
|
+
}
|
|
320
|
+
interface ClaimableItem {
|
|
321
|
+
id: string;
|
|
322
|
+
originalTxHash: string;
|
|
323
|
+
amount: string;
|
|
324
|
+
asset: string;
|
|
325
|
+
network: string;
|
|
326
|
+
reason?: string;
|
|
327
|
+
status: 'pending' | 'approved';
|
|
328
|
+
reportedAt: string;
|
|
329
|
+
expiresAt?: string;
|
|
330
|
+
}
|
|
331
|
+
interface GetClaimableResponse {
|
|
332
|
+
claims: ClaimableItem[];
|
|
333
|
+
}
|
|
334
|
+
/**
|
|
335
|
+
* Get claimable refunds for a wallet
|
|
336
|
+
*/
|
|
337
|
+
declare function getClaimable(params: GetClaimableParams): Promise<GetClaimableResponse>;
|
|
338
|
+
interface ClaimHistoryItem {
|
|
339
|
+
id: string;
|
|
340
|
+
originalTxHash: string;
|
|
341
|
+
amount: string;
|
|
342
|
+
asset: string;
|
|
343
|
+
network: string;
|
|
344
|
+
reason?: string;
|
|
345
|
+
status: 'pending' | 'approved' | 'paid' | 'rejected' | 'expired';
|
|
346
|
+
reportedAt: string;
|
|
347
|
+
expiresAt?: string;
|
|
348
|
+
payoutTxHash?: string;
|
|
349
|
+
paidAt?: string;
|
|
350
|
+
}
|
|
351
|
+
interface GetClaimHistoryResponse {
|
|
352
|
+
claims: ClaimHistoryItem[];
|
|
353
|
+
}
|
|
354
|
+
/**
|
|
355
|
+
* Get claim history for a wallet
|
|
356
|
+
*/
|
|
357
|
+
declare function getClaimHistory(params: GetClaimableParams): Promise<GetClaimHistoryResponse>;
|
|
358
|
+
interface ExecuteClaimParams {
|
|
359
|
+
/** The facilitator URL */
|
|
360
|
+
facilitatorUrl: string;
|
|
361
|
+
/** The claim ID to execute */
|
|
362
|
+
claimId: string;
|
|
363
|
+
/** Optional signature for verification (recommended in production) */
|
|
364
|
+
signature?: string;
|
|
365
|
+
}
|
|
366
|
+
interface ExecuteClaimResponse {
|
|
367
|
+
success: boolean;
|
|
368
|
+
transactionHash?: string;
|
|
369
|
+
error?: string;
|
|
370
|
+
}
|
|
371
|
+
/**
|
|
372
|
+
* Execute a claim payout (claim must be approved)
|
|
373
|
+
*/
|
|
374
|
+
declare function executeClaim(params: ExecuteClaimParams): Promise<ExecuteClaimResponse>;
|
|
375
|
+
|
|
376
|
+
/**
|
|
377
|
+
* Payment and refund protection middleware
|
|
378
|
+
*/
|
|
379
|
+
|
|
380
|
+
interface RefundProtectionConfig {
|
|
381
|
+
/** The API key from server registration */
|
|
382
|
+
apiKey: string;
|
|
383
|
+
/** The facilitator URL */
|
|
384
|
+
facilitatorUrl: string;
|
|
385
|
+
/** Optional: Custom error filter - return false to skip reporting */
|
|
386
|
+
shouldReport?: (error: Error) => boolean;
|
|
387
|
+
/** Optional: Called when a failure is reported */
|
|
388
|
+
onReport?: (claimId: string | undefined, error: Error) => void;
|
|
389
|
+
/** Optional: Called when reporting fails */
|
|
390
|
+
onReportError?: (reportError: Error, originalError: Error) => void;
|
|
391
|
+
}
|
|
392
|
+
interface PaymentContext {
|
|
393
|
+
/** Transaction hash from settlement */
|
|
394
|
+
transactionHash: string;
|
|
395
|
+
/** User's wallet address (payer) */
|
|
396
|
+
userWallet: string;
|
|
397
|
+
/** Payment amount in atomic units */
|
|
398
|
+
amount: string;
|
|
399
|
+
/** Asset/token address */
|
|
400
|
+
asset: string;
|
|
401
|
+
/** Network identifier (e.g., "base", "solana") */
|
|
402
|
+
network: string;
|
|
403
|
+
}
|
|
404
|
+
/**
|
|
405
|
+
* Wrap an async function with refund protection.
|
|
406
|
+
* If the function throws, a failure is automatically reported.
|
|
407
|
+
*
|
|
408
|
+
* @example
|
|
409
|
+
* ```typescript
|
|
410
|
+
* import { withRefundProtection } from '@openfacilitator/sdk';
|
|
411
|
+
*
|
|
412
|
+
* const protectedHandler = withRefundProtection(
|
|
413
|
+
* {
|
|
414
|
+
* apiKey: process.env.REFUND_API_KEY!,
|
|
415
|
+
* facilitatorUrl: 'https://free.openfacilitator.xyz',
|
|
416
|
+
* },
|
|
417
|
+
* async (paymentContext) => {
|
|
418
|
+
* // Your logic here - if this throws, failure is auto-reported
|
|
419
|
+
* const result = await doExpensiveOperation();
|
|
420
|
+
* return result;
|
|
421
|
+
* }
|
|
422
|
+
* );
|
|
423
|
+
*
|
|
424
|
+
* // Call with payment context from settle response
|
|
425
|
+
* const result = await protectedHandler({
|
|
426
|
+
* transactionHash: settleResponse.transaction,
|
|
427
|
+
* userWallet: settleResponse.payer,
|
|
428
|
+
* amount: paymentPayload.payload.authorization.amount,
|
|
429
|
+
* asset: paymentPayload.payload.authorization.asset,
|
|
430
|
+
* network: settleResponse.network,
|
|
431
|
+
* });
|
|
432
|
+
* ```
|
|
433
|
+
*/
|
|
434
|
+
declare function withRefundProtection<T>(config: RefundProtectionConfig, handler: (context: PaymentContext) => Promise<T>): (context: PaymentContext) => Promise<T>;
|
|
435
|
+
/**
|
|
436
|
+
* Express request with payment context attached
|
|
437
|
+
*/
|
|
438
|
+
interface PaymentRequest {
|
|
439
|
+
paymentContext?: PaymentContext;
|
|
440
|
+
}
|
|
441
|
+
/**
|
|
442
|
+
* Create Express middleware that attaches payment context and reports failures.
|
|
443
|
+
*
|
|
444
|
+
* @example
|
|
445
|
+
* ```typescript
|
|
446
|
+
* import express from 'express';
|
|
447
|
+
* import { createRefundMiddleware } from '@openfacilitator/sdk';
|
|
448
|
+
*
|
|
449
|
+
* const app = express();
|
|
450
|
+
*
|
|
451
|
+
* const refundMiddleware = createRefundMiddleware({
|
|
452
|
+
* apiKey: process.env.REFUND_API_KEY!,
|
|
453
|
+
* facilitatorUrl: 'https://free.openfacilitator.xyz',
|
|
454
|
+
* });
|
|
455
|
+
*
|
|
456
|
+
* // Apply after your x402 payment verification
|
|
457
|
+
* app.post('/api/resource', paymentMiddleware, refundMiddleware, async (req, res) => {
|
|
458
|
+
* // If this throws, failure is auto-reported
|
|
459
|
+
* const result = await doExpensiveOperation();
|
|
460
|
+
* res.json(result);
|
|
461
|
+
* });
|
|
462
|
+
* ```
|
|
463
|
+
*/
|
|
464
|
+
declare function createRefundMiddleware(config: RefundProtectionConfig): (req: PaymentRequest & {
|
|
465
|
+
body?: unknown;
|
|
466
|
+
}, res: {
|
|
467
|
+
locals?: {
|
|
468
|
+
paymentContext?: PaymentContext;
|
|
469
|
+
};
|
|
470
|
+
}, next: (error?: unknown) => void) => Promise<void>;
|
|
471
|
+
/**
|
|
472
|
+
* Create Hono middleware for refund protection.
|
|
473
|
+
*
|
|
474
|
+
* @example
|
|
475
|
+
* ```typescript
|
|
476
|
+
* import { Hono } from 'hono';
|
|
477
|
+
* import { honoRefundMiddleware } from '@openfacilitator/sdk';
|
|
478
|
+
*
|
|
479
|
+
* const app = new Hono();
|
|
480
|
+
*
|
|
481
|
+
* // Apply after your x402 payment verification
|
|
482
|
+
* app.post('/api/resource', paymentMiddleware, honoRefundMiddleware({
|
|
483
|
+
* apiKey: process.env.REFUND_API_KEY!,
|
|
484
|
+
* facilitatorUrl: 'https://free.openfacilitator.xyz',
|
|
485
|
+
* getPaymentContext: (c) => c.get('paymentContext'),
|
|
486
|
+
* }), async (c) => {
|
|
487
|
+
* const result = await doExpensiveOperation();
|
|
488
|
+
* return c.json(result);
|
|
489
|
+
* });
|
|
490
|
+
* ```
|
|
491
|
+
*/
|
|
492
|
+
interface HonoRefundConfig extends RefundProtectionConfig {
|
|
493
|
+
/** Function to extract payment context from Hono context */
|
|
494
|
+
getPaymentContext: (c: HonoContext) => PaymentContext | undefined;
|
|
495
|
+
}
|
|
496
|
+
interface HonoContext {
|
|
497
|
+
get: (key: string) => unknown;
|
|
498
|
+
set: (key: string, value: unknown) => void;
|
|
499
|
+
}
|
|
500
|
+
declare function honoRefundMiddleware(config: HonoRefundConfig): (c: HonoContext, next: () => Promise<void>) => Promise<void>;
|
|
501
|
+
/**
|
|
502
|
+
* Helper to create PaymentContext from settle response and payment payload.
|
|
503
|
+
*
|
|
504
|
+
* @example
|
|
505
|
+
* ```typescript
|
|
506
|
+
* import { OpenFacilitator, createPaymentContext } from '@openfacilitator/sdk';
|
|
507
|
+
*
|
|
508
|
+
* const facilitator = new OpenFacilitator({ url: '...' });
|
|
509
|
+
* const settleResult = await facilitator.settle(paymentPayload, requirements);
|
|
510
|
+
*
|
|
511
|
+
* const paymentContext = createPaymentContext(settleResult, paymentPayload);
|
|
512
|
+
* // Use with withRefundProtection or attach to request
|
|
513
|
+
* ```
|
|
514
|
+
*/
|
|
515
|
+
declare function createPaymentContext(settleResponse: {
|
|
516
|
+
transaction: string;
|
|
517
|
+
payer: string;
|
|
518
|
+
network: string;
|
|
519
|
+
}, paymentPayload: {
|
|
520
|
+
payload: {
|
|
521
|
+
authorization: {
|
|
522
|
+
amount: string;
|
|
523
|
+
asset: string;
|
|
524
|
+
};
|
|
525
|
+
};
|
|
526
|
+
}): PaymentContext;
|
|
527
|
+
interface PaymentMiddlewareConfig {
|
|
528
|
+
/** Facilitator instance or URL */
|
|
529
|
+
facilitator: OpenFacilitator | string;
|
|
530
|
+
/** Function to get payment requirements for the request */
|
|
531
|
+
getRequirements: (req: unknown) => PaymentRequirements | Promise<PaymentRequirements>;
|
|
532
|
+
/** Optional: Refund protection config (enables auto failure reporting) */
|
|
533
|
+
refundProtection?: RefundProtectionConfig;
|
|
534
|
+
/** Optional: Custom 402 response handler */
|
|
535
|
+
on402?: (req: unknown, res: unknown, requirements: PaymentRequirements) => void | Promise<void>;
|
|
536
|
+
}
|
|
537
|
+
/**
|
|
538
|
+
* Create x402 payment middleware that handles verification, settlement, and optional refund protection.
|
|
539
|
+
*
|
|
540
|
+
* @example
|
|
541
|
+
* ```typescript
|
|
542
|
+
* import express from 'express';
|
|
543
|
+
* import { createPaymentMiddleware, OpenFacilitator } from '@openfacilitator/sdk';
|
|
544
|
+
*
|
|
545
|
+
* const app = express();
|
|
546
|
+
*
|
|
547
|
+
* const paymentMiddleware = createPaymentMiddleware({
|
|
548
|
+
* facilitator: new OpenFacilitator({ url: 'https://free.openfacilitator.xyz' }),
|
|
549
|
+
* getRequirements: (req) => ({
|
|
550
|
+
* scheme: 'exact',
|
|
551
|
+
* network: 'base',
|
|
552
|
+
* maxAmountRequired: '1000000', // $1 USDC
|
|
553
|
+
* asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
|
|
554
|
+
* payTo: '0xYourAddress',
|
|
555
|
+
* resource: req.url,
|
|
556
|
+
* }),
|
|
557
|
+
* refundProtection: {
|
|
558
|
+
* apiKey: process.env.REFUND_API_KEY!,
|
|
559
|
+
* facilitatorUrl: 'https://free.openfacilitator.xyz',
|
|
560
|
+
* },
|
|
561
|
+
* });
|
|
562
|
+
*
|
|
563
|
+
* app.post('/api/resource', paymentMiddleware, async (req, res) => {
|
|
564
|
+
* // Payment verified & settled, refund protection active
|
|
565
|
+
* const result = await doExpensiveOperation();
|
|
566
|
+
* res.json(result);
|
|
567
|
+
* });
|
|
568
|
+
* ```
|
|
569
|
+
*/
|
|
570
|
+
declare function createPaymentMiddleware(config: PaymentMiddlewareConfig): (req: {
|
|
571
|
+
headers: Record<string, string | string[] | undefined>;
|
|
572
|
+
url?: string;
|
|
573
|
+
paymentContext?: PaymentContext;
|
|
574
|
+
}, res: {
|
|
575
|
+
status: (code: number) => {
|
|
576
|
+
json: (body: unknown) => void;
|
|
577
|
+
};
|
|
578
|
+
locals?: Record<string, unknown>;
|
|
579
|
+
}, next: (error?: unknown) => void) => Promise<void>;
|
|
580
|
+
interface HonoPaymentConfig {
|
|
581
|
+
/** Facilitator instance or URL */
|
|
582
|
+
facilitator: OpenFacilitator | string;
|
|
583
|
+
/** Function to get payment requirements for the request */
|
|
584
|
+
getRequirements: (c: HonoContext) => PaymentRequirements | Promise<PaymentRequirements>;
|
|
585
|
+
/** Optional: Refund protection config */
|
|
586
|
+
refundProtection?: RefundProtectionConfig;
|
|
587
|
+
}
|
|
588
|
+
/**
|
|
589
|
+
* Create Hono x402 payment middleware.
|
|
590
|
+
*
|
|
591
|
+
* @example
|
|
592
|
+
* ```typescript
|
|
593
|
+
* import { Hono } from 'hono';
|
|
594
|
+
* import { honoPaymentMiddleware, OpenFacilitator } from '@openfacilitator/sdk';
|
|
595
|
+
*
|
|
596
|
+
* const app = new Hono();
|
|
597
|
+
*
|
|
598
|
+
* app.post('/api/resource', honoPaymentMiddleware({
|
|
599
|
+
* facilitator: new OpenFacilitator({ url: 'https://free.openfacilitator.xyz' }),
|
|
600
|
+
* getRequirements: (c) => ({
|
|
601
|
+
* scheme: 'exact',
|
|
602
|
+
* network: 'base',
|
|
603
|
+
* maxAmountRequired: '1000000',
|
|
604
|
+
* asset: '0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913',
|
|
605
|
+
* payTo: '0xYourAddress',
|
|
606
|
+
* }),
|
|
607
|
+
* refundProtection: {
|
|
608
|
+
* apiKey: process.env.REFUND_API_KEY!,
|
|
609
|
+
* facilitatorUrl: 'https://free.openfacilitator.xyz',
|
|
610
|
+
* },
|
|
611
|
+
* }), async (c) => {
|
|
612
|
+
* const paymentContext = c.get('paymentContext');
|
|
613
|
+
* const result = await doExpensiveOperation();
|
|
614
|
+
* return c.json(result);
|
|
615
|
+
* });
|
|
616
|
+
* ```
|
|
617
|
+
*/
|
|
618
|
+
declare function honoPaymentMiddleware(config: HonoPaymentConfig): (c: HonoContext & {
|
|
619
|
+
req: {
|
|
620
|
+
header: (name: string) => string | undefined;
|
|
621
|
+
url: string;
|
|
622
|
+
};
|
|
623
|
+
json: (body: unknown, status?: number) => Response;
|
|
624
|
+
}, next: () => Promise<void>) => Promise<Response | undefined>;
|
|
625
|
+
|
|
626
|
+
export { type ClaimHistoryItem, type ClaimableItem, ConfigurationError, type ExecuteClaimParams, type ExecuteClaimResponse, type FacilitatorConfig, FacilitatorError, type GetClaimHistoryResponse, type GetClaimableParams, type GetClaimableResponse, type HonoPaymentConfig, type HonoRefundConfig, NETWORKS, NetworkError, type NetworkInfo, type NetworkType, OpenFacilitator, type PaymentAuthorization, type PaymentContext, type PaymentKind, type PaymentMiddlewareConfig, type PaymentPayload, type PaymentRequest, type PaymentRequirements, type RefundProtectionConfig, type ReportFailureParams, type ReportFailureResponse, type SettleResponse, SettlementError, type SupportedResponse, VerificationError, type VerifyResponse, createDefaultFacilitator, createPaymentContext, createPaymentMiddleware, createRefundMiddleware, executeClaim, getClaimHistory, getClaimable, getMainnets, getNetwork, getNetworkType, getTestnets, honoPaymentMiddleware, honoRefundMiddleware, isPaymentPayload, isValidNetwork, reportFailure, toV1NetworkId, toV2NetworkId, withRefundProtection };
|