@armory-sh/middleware-hono 0.3.13 → 0.3.14

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.ts CHANGED
@@ -59,7 +59,7 @@ declare const acceptPaymentsViaArmory: (config: SimpleMiddlewareConfig & {
59
59
  }) => (c: Context, next: Next) => Promise<(Response & hono.TypedResponse<{
60
60
  error: string;
61
61
  x402Version: number;
62
- requirements: {
62
+ accepts: ({
63
63
  scheme: "exact";
64
64
  network: X402V1Network;
65
65
  maxAmountRequired: string;
@@ -89,7 +89,7 @@ declare const acceptPaymentsViaArmory: (config: SimpleMiddlewareConfig & {
89
89
  contractAddress: string;
90
90
  payTo: string;
91
91
  expiry: number;
92
- };
92
+ })[];
93
93
  }, hono_utils_http_status.ContentfulStatusCode, "json">) | (Response & hono.TypedResponse<{
94
94
  error: string;
95
95
  x402Version: PaymentVersion;
package/dist/index.js CHANGED
@@ -298,7 +298,6 @@ var getPrimaryConfig = (resolved) => {
298
298
 
299
299
  // src/payment-utils.ts
300
300
  import { extractPaymentFromHeaders, X402_HEADERS } from "@armory-sh/base";
301
- import { verifyX402Payment as verifyPayment } from "@armory-sh/facilitator";
302
301
  var getHeadersForVersion = (version) => version === 1 ? { payment: "X-PAYMENT", required: "X-PAYMENT-REQUIRED", response: "X-PAYMENT-RESPONSE" } : { payment: "PAYMENT-SIGNATURE", required: "PAYMENT-REQUIRED", response: "PAYMENT-RESPONSE" };
303
302
  function isLegacyV1(payload) {
304
303
  return typeof payload === "object" && payload !== null && "contractAddress" in payload && "network" in payload && "signature" in payload && typeof payload.signature === "string";
@@ -308,17 +307,36 @@ function isLegacyV2(payload) {
308
307
  }
309
308
  var decodePayload = (headerValue) => {
310
309
  let parsed;
310
+ let isJsonString = false;
311
311
  try {
312
312
  if (headerValue.startsWith("{")) {
313
313
  parsed = JSON.parse(headerValue);
314
+ isJsonString = true;
314
315
  } else {
315
- parsed = JSON.parse(atob(headerValue));
316
+ try {
317
+ parsed = JSON.parse(atob(headerValue));
318
+ } catch {
319
+ const padding = 4 - headerValue.length % 4;
320
+ const padded = padding !== 4 ? headerValue + "=".repeat(padding) : headerValue;
321
+ const standard = padded.replace(/-/g, "+").replace(/_/g, "/");
322
+ parsed = JSON.parse(atob(standard));
323
+ }
316
324
  }
317
325
  } catch {
318
326
  throw new Error("Invalid payment payload");
319
327
  }
328
+ if (typeof parsed === "object" && parsed !== null && "x402Version" in parsed) {
329
+ const version = parsed.x402Version;
330
+ if (version === 1) {
331
+ return { payload: parsed, version: 1 };
332
+ }
333
+ if (version === 2) {
334
+ return { payload: parsed, version: 2 };
335
+ }
336
+ }
337
+ const base64Value = isJsonString ? Buffer.from(headerValue).toString("base64") : headerValue;
320
338
  const headers = new Headers();
321
- headers.set(X402_HEADERS.PAYMENT, headerValue);
339
+ headers.set(X402_HEADERS.PAYMENT, base64Value);
322
340
  const x402Payload = extractPaymentFromHeaders(headers);
323
341
  if (x402Payload) {
324
342
  return { payload: x402Payload, version: 2 };
@@ -372,7 +390,7 @@ var acceptPaymentsViaArmory = (config) => {
372
390
  );
373
391
  };
374
392
  return async (c, next) => {
375
- const paymentHeader = c.req.header(V1_HEADERS2.PAYMENT) || c.req.header(V2_HEADERS2.PAYMENT_SIGNATURE) || c.req.header("X-Payment");
393
+ const paymentHeader = c.req.header(V1_HEADERS2.PAYMENT) || c.req.header(V2_HEADERS2.PAYMENT_SIGNATURE) || c.req.header("X-PAYMENT");
376
394
  const resourceUrl = c.req.url;
377
395
  if (!paymentHeader) {
378
396
  const version = defaultVersion === 1 ? 1 : 2;
@@ -385,7 +403,7 @@ var acceptPaymentsViaArmory = (config) => {
385
403
  return c.json({
386
404
  error: "Payment required",
387
405
  x402Version: version,
388
- requirements
406
+ accepts: [requirements]
389
407
  });
390
408
  }
391
409
  try {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@armory-sh/middleware-hono",
3
- "version": "0.3.13",
3
+ "version": "0.3.14",
4
4
  "license": "MIT",
5
5
  "author": "Sawyer Cutler <sawyer@dirtroad.dev>",
6
6
  "type": "module",
@@ -28,8 +28,7 @@
28
28
  "hono": "^4"
29
29
  },
30
30
  "dependencies": {
31
- "@armory-sh/base": "0.2.12",
32
- "@armory-sh/facilitator": "0.2.12"
31
+ "@armory-sh/base": "workspace:*"
33
32
  },
34
33
  "devDependencies": {
35
34
  "bun-types": "latest",
@@ -37,8 +36,8 @@
37
36
  "hono": "^4"
38
37
  },
39
38
  "scripts": {
40
- "build": "tsup",
41
- "build:dts": "tsc --declaration --skipLibCheck",
39
+ "build": "rm -rf dist && tsup",
40
+ "build:dts": "rm -rf dist && tsc --declaration --skipLibCheck",
42
41
  "test": "bun test"
43
42
  }
44
43
  }