@faremeter/facilitator 0.16.0 → 0.17.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,22 @@
1
+ import { x402, facilitator } from "@faremeter/types";
2
+ export { adaptRequirementsV1ToV2, adaptRequirementsV2ToV1, extractResourceInfoV1, adaptPayloadV1ToV2, adaptVerifyResponseV2ToV1, adaptSettleResponseV2ToV1, adaptSettleResponseV2ToV1Legacy, adaptSettleResponseLegacyToV2, adaptSettleResponseLenientToV2, adaptSupportedKindV2ToV1, adaptSupportedKindV1ToV2, adaptPaymentRequiredResponseV1ToV2, adaptPaymentRequiredResponseV2ToV1, adaptVerifyResponseV1ToV2, adaptSettleResponseV1ToV2, } from "@faremeter/types/x402-adapters";
3
+ /** V1 payment requirements with mimeType guaranteed to be present */
4
+ type x402PaymentRequirementsV1Strict = x402.x402PaymentRequirements & {
5
+ mimeType: string;
6
+ };
7
+ /**
8
+ * Legacy facilitator handler interface using pre-spec field names.
9
+ * Use this to wrap old handlers that return txHash/networkId/error.
10
+ */
11
+ export type LegacyFacilitatorHandler = {
12
+ getSupported?: () => Promise<x402.x402SupportedKind>[];
13
+ getRequirements: (req: x402PaymentRequirementsV1Strict[]) => Promise<x402.x402PaymentRequirements[]>;
14
+ handleVerify?: (requirements: x402PaymentRequirementsV1Strict, payment: x402.x402PaymentPayload) => Promise<x402.x402VerifyResponse | null>;
15
+ handleSettle: (requirements: x402PaymentRequirementsV1Strict, payment: x402.x402PaymentPayload) => Promise<x402.x402SettleResponseLegacy | null>;
16
+ };
17
+ /**
18
+ * Adapts a v1 FacilitatorHandler to the v2 interface.
19
+ * Use this to wrap handlers from external packages that haven't been updated to v2 types.
20
+ */
21
+ export declare function adaptHandlerV1ToV2(handler: LegacyFacilitatorHandler): facilitator.FacilitatorHandler;
22
+ //# sourceMappingURL=adapters.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"adapters.d.ts","sourceRoot":"","sources":["../../src/adapters.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,IAAI,EAAU,WAAW,EAAE,MAAM,kBAAkB,CAAC;AAU7D,OAAO,EACL,uBAAuB,EACvB,uBAAuB,EACvB,qBAAqB,EACrB,kBAAkB,EAClB,yBAAyB,EACzB,yBAAyB,EACzB,+BAA+B,EAC/B,6BAA6B,EAC7B,8BAA8B,EAC9B,wBAAwB,EACxB,wBAAwB,EACxB,kCAAkC,EAClC,kCAAkC,EAClC,yBAAyB,EACzB,yBAAyB,GAC1B,MAAM,gCAAgC,CAAC;AAExC,qEAAqE;AACrE,KAAK,+BAA+B,GAAG,IAAI,CAAC,uBAAuB,GAAG;IACpE,QAAQ,EAAE,MAAM,CAAC;CAClB,CAAC;AAEF;;;GAGG;AACH,MAAM,MAAM,wBAAwB,GAAG;IACrC,YAAY,CAAC,EAAE,MAAM,OAAO,CAAC,IAAI,CAAC,iBAAiB,CAAC,EAAE,CAAC;IACvD,eAAe,EAAE,CACf,GAAG,EAAE,+BAA+B,EAAE,KACnC,OAAO,CAAC,IAAI,CAAC,uBAAuB,EAAE,CAAC,CAAC;IAC7C,YAAY,CAAC,EAAE,CACb,YAAY,EAAE,+BAA+B,EAC7C,OAAO,EAAE,IAAI,CAAC,kBAAkB,KAC7B,OAAO,CAAC,IAAI,CAAC,kBAAkB,GAAG,IAAI,CAAC,CAAC;IAC7C,YAAY,EAAE,CACZ,YAAY,EAAE,+BAA+B,EAC7C,OAAO,EAAE,IAAI,CAAC,kBAAkB,KAC7B,OAAO,CAAC,IAAI,CAAC,wBAAwB,GAAG,IAAI,CAAC,CAAC;CACpD,CAAC;AAEF;;;GAGG;AACH,wBAAgB,kBAAkB,CAChC,OAAO,EAAE,wBAAwB,GAChC,WAAW,CAAC,kBAAkB,CA0EhC"}
@@ -0,0 +1,68 @@
1
+ import { x402, x402v2, facilitator } from "@faremeter/types";
2
+ import { adaptRequirementsV1ToV2, adaptRequirementsV2ToV1, adaptSupportedKindV1ToV2, adaptVerifyResponseV1ToV2, adaptSettleResponseLegacyToV2, } from "@faremeter/types/x402-adapters";
3
+ import { normalizeNetworkId } from "@faremeter/info";
4
+ export { adaptRequirementsV1ToV2, adaptRequirementsV2ToV1, extractResourceInfoV1, adaptPayloadV1ToV2, adaptVerifyResponseV2ToV1, adaptSettleResponseV2ToV1, adaptSettleResponseV2ToV1Legacy, adaptSettleResponseLegacyToV2, adaptSettleResponseLenientToV2, adaptSupportedKindV2ToV1, adaptSupportedKindV1ToV2, adaptPaymentRequiredResponseV1ToV2, adaptPaymentRequiredResponseV2ToV1, adaptVerifyResponseV1ToV2, adaptSettleResponseV1ToV2, } from "@faremeter/types/x402-adapters";
5
+ /**
6
+ * Adapts a v1 FacilitatorHandler to the v2 interface.
7
+ * Use this to wrap handlers from external packages that haven't been updated to v2 types.
8
+ */
9
+ export function adaptHandlerV1ToV2(handler) {
10
+ const adapted = {
11
+ getRequirements: async ({ accepts, resource }) => {
12
+ // Convert v2 requirements to v1, call legacy handler, convert back to v2.
13
+ // Resource may be undefined when the caller has no resource context
14
+ // (e.g. during capability negotiation). Legacy handlers that inspect
15
+ // requirements.resource will see an empty string in that case.
16
+ const fallbackResource = resource ?? {
17
+ url: "",
18
+ };
19
+ const v1Reqs = accepts.map((r) => adaptRequirementsV2ToV1(r, fallbackResource));
20
+ const v1Results = await handler.getRequirements(v1Reqs);
21
+ return v1Results.map((req) => adaptRequirementsV1ToV2(req, normalizeNetworkId));
22
+ },
23
+ handleSettle: async (requirements, payment) => {
24
+ if (!payment.resource) {
25
+ throw new Error("v2 payment payload is missing resource context required for v1 adapter");
26
+ }
27
+ const v1Req = adaptRequirementsV2ToV1(requirements, payment.resource);
28
+ const v1Payload = {
29
+ x402Version: 1,
30
+ scheme: payment.accepted.scheme,
31
+ network: payment.accepted.network,
32
+ asset: payment.accepted.asset,
33
+ payload: payment.payload,
34
+ };
35
+ const v1Result = await handler.handleSettle(v1Req, v1Payload);
36
+ if (v1Result === null) {
37
+ return null;
38
+ }
39
+ return adaptSettleResponseLegacyToV2(v1Result);
40
+ },
41
+ };
42
+ if (handler.getSupported) {
43
+ const legacyGetSupported = handler.getSupported;
44
+ adapted.getSupported = () => legacyGetSupported().map((p) => p.then((kind) => adaptSupportedKindV1ToV2(kind, normalizeNetworkId)));
45
+ }
46
+ if (handler.handleVerify) {
47
+ const legacyHandleVerify = handler.handleVerify;
48
+ adapted.handleVerify = async (requirements, payment) => {
49
+ if (!payment.resource) {
50
+ throw new Error("v2 payment payload is missing resource context required for v1 adapter");
51
+ }
52
+ const v1Req = adaptRequirementsV2ToV1(requirements, payment.resource);
53
+ const v1Payload = {
54
+ x402Version: 1,
55
+ scheme: payment.accepted.scheme,
56
+ network: payment.accepted.network,
57
+ asset: payment.accepted.asset,
58
+ payload: payment.payload,
59
+ };
60
+ const v1Result = await legacyHandleVerify(v1Req, v1Payload);
61
+ if (v1Result === null) {
62
+ return null;
63
+ }
64
+ return adaptVerifyResponseV1ToV2(v1Result);
65
+ };
66
+ }
67
+ return adapted;
68
+ }
@@ -1,2 +1,9 @@
1
+ /**
2
+ * @title Facilitator Package
3
+ * @sidebarTitle Facilitator
4
+ * @description Server-side facilitator for verifying and settling x402 payments on-chain
5
+ * @packageDocumentation
6
+ */
1
7
  export * from "./routes.js";
8
+ export * from "./adapters.js";
2
9
  //# sourceMappingURL=index.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,UAAU,CAAC"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AACH,cAAc,UAAU,CAAC;AACzB,cAAc,YAAY,CAAC"}
package/dist/src/index.js CHANGED
@@ -1 +1,8 @@
1
+ /**
2
+ * @title Facilitator Package
3
+ * @sidebarTitle Facilitator
4
+ * @description Server-side facilitator for verifying and settling x402 payments on-chain
5
+ * @packageDocumentation
6
+ */
1
7
  export * from "./routes.js";
8
+ export * from "./adapters.js";
@@ -1,12 +1,34 @@
1
- import { Hono } from "hono";
1
+ import { Hono, type Context } from "hono";
2
2
  import type { FacilitatorHandler } from "@faremeter/types/facilitator";
3
+ /**
4
+ * Configuration arguments for creating facilitator routes.
5
+ */
3
6
  type CreateFacilitatorRoutesArgs = {
7
+ /** Array of handlers that process payment operations. */
4
8
  handlers: FacilitatorHandler[];
9
+ /** Optional timeout settings in milliseconds for handler operations. */
5
10
  timeout?: {
11
+ /** Timeout for getRequirements calls. Defaults to 500ms. */
6
12
  getRequirements?: number;
13
+ /** Timeout for getSupported calls. Defaults to 500ms. */
7
14
  getSupported?: number;
8
15
  };
9
16
  };
17
+ export declare function getClientIP(c: Context): string | undefined;
18
+ /**
19
+ * Creates a Hono router with x402 facilitator endpoints.
20
+ *
21
+ * The router provides the following endpoints:
22
+ * - POST /verify - Verify a payment without settling
23
+ * - POST /settle - Verify and settle a payment
24
+ * - POST /accepts - Get payment requirements for a resource
25
+ * - GET /supported - List supported payment schemes and networks
26
+ *
27
+ * Both v1 and v2 protocol formats are supported on all endpoints.
28
+ *
29
+ * @param args - Configuration including payment handlers and timeouts
30
+ * @returns A Hono router instance with facilitator endpoints
31
+ */
10
32
  export declare function createFacilitatorRoutes(args: CreateFacilitatorRoutesArgs): Hono<import("hono/types").BlankEnv, import("hono/types").BlankSchema, "/">;
11
33
  export {};
12
34
  //# sourceMappingURL=routes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../../src/routes.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAgB,MAAM,MAAM,CAAC;AAI1C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAKvE,KAAK,2BAA2B,GAAG;IACjC,QAAQ,EAAE,kBAAkB,EAAE,CAAC;IAC/B,OAAO,CAAC,EAAE;QACR,eAAe,CAAC,EAAE,MAAM,CAAC;QACzB,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB,CAAC;CACH,CAAC;AAoCF,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,2BAA2B,8EAuQxE"}
1
+ {"version":3,"file":"routes.d.ts","sourceRoot":"","sources":["../../src/routes.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,IAAI,EAAE,KAAK,OAAO,EAAE,MAAM,MAAM,CAAC;AAI1C,OAAO,KAAK,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAiDvE;;GAEG;AACH,KAAK,2BAA2B,GAAG;IACjC,yDAAyD;IACzD,QAAQ,EAAE,kBAAkB,EAAE,CAAC;IAC/B,wEAAwE;IACxE,OAAO,CAAC,EAAE;QACR,4DAA4D;QAC5D,eAAe,CAAC,EAAE,MAAM,CAAC;QACzB,yDAAyD;QACzD,YAAY,CAAC,EAAE,MAAM,CAAC;KACvB,CAAC;CACH,CAAC;AAkBF,wBAAgB,WAAW,CAAC,CAAC,EAAE,OAAO,GAAG,MAAM,GAAG,SAAS,CAO1D;AAcD;;;;;;;;;;;;;GAaG;AACH,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,2BAA2B,8EA0gBxE"}