@faremeter/facilitator 0.12.0 → 0.14.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 ADDED
@@ -0,0 +1,32 @@
1
+ # @faremeter/facilitator
2
+
3
+ Facilitator server implementation for x402 protocol payment settlement and validation.
4
+
5
+ ## Installation
6
+
7
+ ```bash
8
+ pnpm install @faremeter/facilitator
9
+ ```
10
+
11
+ ## Features
12
+
13
+ - Multi-chain support
14
+ - Standard x402 endpoints
15
+ - Payment verification
16
+ - Blockchain settlement
17
+
18
+ ## API Reference
19
+
20
+ <!-- TSDOC_START -->
21
+
22
+ <!-- TSDOC_END -->
23
+
24
+ ## Related Packages
25
+
26
+ - [@faremeter/middleware](https://www.npmjs.com/package/@faremeter/middleware) - Server middleware that uses facilitator
27
+ - [@faremeter/payment-solana](https://www.npmjs.com/package/@faremeter/payment-solana) - Solana payment handler
28
+ - [@faremeter/payment-evm](https://www.npmjs.com/package/@faremeter/payment-evm) - EVM payment handler
29
+
30
+ ## License
31
+
32
+ LGPL-3.0-only
@@ -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;AAkBF,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,2BAA2B,8EAwLxE"}
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;AAkBF,wBAAgB,uBAAuB,CAAC,IAAI,EAAE,2BAA2B,8EAgSxE"}
@@ -15,6 +15,67 @@ function summarizeRequirements({ scheme, network, asset, payTo, }) {
15
15
  }
16
16
  export function createFacilitatorRoutes(args) {
17
17
  const router = new Hono();
18
+ function sendVerifyError(c, status, msg) {
19
+ const response = {
20
+ isValid: false,
21
+ };
22
+ if (msg !== undefined) {
23
+ response.invalidReason = msg;
24
+ logger.error(msg);
25
+ }
26
+ else {
27
+ logger.error("unknown error during verification");
28
+ }
29
+ c.status(status);
30
+ return c.json(response);
31
+ }
32
+ router.post("/verify", async (c) => {
33
+ const x402Req = x.x402VerifyRequest(await c.req.json());
34
+ if (isValidationError(x402Req)) {
35
+ return sendVerifyError(c, 400, `couldn't validate request: ${x402Req.summary}`);
36
+ }
37
+ let paymentPayload = x402Req.paymentPayload;
38
+ if (paymentPayload === undefined) {
39
+ const decodedHeader = x.x402PaymentHeaderToPayload(x402Req.paymentHeader);
40
+ if (isValidationError(decodedHeader)) {
41
+ return sendVerifyError(c, 400, `couldn't validate x402 payload: ${decodedHeader.summary}`);
42
+ }
43
+ paymentPayload = decodedHeader;
44
+ }
45
+ logger.debug("starting verifyment attempt for request: {*}", x402Req);
46
+ for (const handler of args.handlers) {
47
+ let t;
48
+ if (handler.handleVerify === undefined) {
49
+ continue;
50
+ }
51
+ try {
52
+ t = await handler.handleVerify(x402Req.paymentRequirements, paymentPayload);
53
+ }
54
+ catch (e) {
55
+ let msg = undefined;
56
+ // XXX - We can do a better job of determining if it's a chain
57
+ // error, or some other issue.
58
+ if (e instanceof Error) {
59
+ msg = e.message;
60
+ }
61
+ else {
62
+ msg = "unknown error handling verifyment";
63
+ }
64
+ return sendVerifyError(c, 500, msg);
65
+ }
66
+ if (t === null) {
67
+ continue;
68
+ }
69
+ logger.debug("facilitator handler agreed to verify and returned: {*}", t);
70
+ logger.info(`${t.isValid ? "succeeded" : "failed"} verifying request: {*}`, {
71
+ ...t,
72
+ requirements: summarizeRequirements(x402Req.paymentRequirements),
73
+ });
74
+ return c.json(t);
75
+ }
76
+ logger.warning("attempt to verify was made with no handler found, requirements summary was: {*}", summarizeRequirements(x402Req.paymentRequirements));
77
+ return sendVerifyError(c, 400, "no matching payment handler found");
78
+ });
18
79
  function sendSettleError(c, status, msg) {
19
80
  const response = {
20
81
  success: false,
@@ -36,9 +97,13 @@ export function createFacilitatorRoutes(args) {
36
97
  if (isValidationError(x402Req)) {
37
98
  return sendSettleError(c, 400, `couldn't validate request: ${x402Req.summary}`);
38
99
  }
39
- const paymentPayload = x.x402PaymentHeaderToPayload(x402Req.paymentHeader);
40
- if (isValidationError(paymentPayload)) {
41
- return sendSettleError(c, 400, `couldn't validate x402 payload: ${paymentPayload.summary}`);
100
+ let paymentPayload = x402Req.paymentPayload;
101
+ if (paymentPayload === undefined) {
102
+ const decodedHeader = x.x402PaymentHeaderToPayload(x402Req.paymentHeader);
103
+ if (isValidationError(decodedHeader)) {
104
+ return sendSettleError(c, 400, `couldn't validate x402 payload: ${decodedHeader.summary}`);
105
+ }
106
+ paymentPayload = decodedHeader;
42
107
  }
43
108
  logger.debug("starting settlement attempt for request: {*}", x402Req);
44
109
  for (const handler of args.handlers) {