@faremeter/facilitator 0.11.1 → 0.13.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.
@@ -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) {