@alleyboss/micropay-solana-x402-paywall 2.1.2 → 2.2.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 CHANGED
@@ -19,7 +19,7 @@ npm install @alleyboss/micropay-solana-x402-paywall @solana/web3.js
19
19
  | Feature | Description |
20
20
  |---------|-------------|
21
21
  | 💰 **SOL & USDC Payments** | Native SOL and SPL tokens (USDC, USDT) |
22
- | 🔐 **x402 Protocol** | HTTP 402 Payment Required standard |
22
+ | 🔐 **x402 Protocol** | Full HTTP 402 compliance with `X-Payment-Required` headers |
23
23
  | 🔑 **JWT Sessions** | Secure unlock tracking with anti-replay |
24
24
  | 🛡️ **Signature Store** | Prevent double-spend at app layer |
25
25
  | 🔌 **Express & Next.js** | Zero-boilerplate middleware |
@@ -82,27 +82,42 @@ import { getSolPrice, formatPriceDisplay, configurePricing } from '@alleyboss/mi
82
82
  import { withRetry } from '@alleyboss/micropay-solana-x402-paywall/utils';
83
83
  ```
84
84
 
85
- ## 🔥 New in v2.1.0
85
+ ## 🔥 New in v2.2.0
86
86
 
87
- - **RPC Fallback Support** — Automatic failover on primary RPC failure (configurable, default: off)
88
- - **Priority Fees** — Compute budget instructions for landing transactions faster (configurable, default: off)
89
- - **Versioned Transactions** — Full v0 transaction support with lookup tables
90
- - **TDD Test Suite** — Comprehensive tests with vitest (must pass before npm publish)
87
+ ### x402 Protocol Compliance
88
+
89
+ Full compliance with the [x402.org](https://x402.org) specification:
91
90
 
92
91
  ```typescript
93
- // RPC Fallback configuration
94
- const config = {
95
- network: 'mainnet-beta',
96
- rpcUrl: 'https://primary-rpc.com',
97
- enableFallback: true, // default: false
98
- fallbackRpcUrls: [
99
- 'https://fallback1.com',
100
- 'https://fallback2.com',
101
- ],
102
- };
92
+ import { create402Response, parsePaymentHeader, encodePaymentRequirement } from '@alleyboss/micropay-solana-x402-paywall/x402';
93
+
94
+ // Create 402 response with X-Payment-Required header
95
+ const response = create402Response({
96
+ scheme: 'exact',
97
+ network: 'solana-mainnet',
98
+ maxAmountRequired: '10000000',
99
+ payTo: 'CreatorWallet...',
100
+ resource: '/api/premium',
101
+ description: 'Premium content access',
102
+ maxTimeoutSeconds: 300,
103
+ asset: 'native',
104
+ });
105
+ // Response includes: X-Payment-Required: <base64-encoded-requirement>
103
106
 
107
+ // Parse X-Payment header from client
108
+ const payload = parsePaymentHeader(request.headers.get('x-payment'));
109
+ ```
110
+
111
+ ### Previous Features (v2.1.x)
112
+
113
+ - **RPC Fallback Support** — Automatic failover on primary RPC failure
114
+ - **Priority Fees** — Compute budget instructions for landing transactions faster
115
+ - **Versioned Transactions** — Full v0 transaction support with lookup tables
116
+ - **TDD Test Suite** — Comprehensive tests with vitest
117
+
118
+ ```typescript
104
119
  // Priority fees
105
- import { createPriorityFeeInstructions, estimatePriorityFee } from '@alleyboss/micropay-solana-x402-paywall/solana';
120
+ import { createPriorityFeeInstructions } from '@alleyboss/micropay-solana-x402-paywall/solana';
106
121
 
107
122
  const instructions = createPriorityFeeInstructions({
108
123
  enabled: true,
package/dist/index.cjs CHANGED
@@ -829,9 +829,28 @@ function parsePaymentHeader(header) {
829
829
  return null;
830
830
  }
831
831
  }
832
+ function encodePaymentRequirement(requirement) {
833
+ return Buffer.from(JSON.stringify(requirement)).toString("base64");
834
+ }
832
835
  function encodePaymentResponse(response) {
833
836
  return Buffer.from(JSON.stringify(response)).toString("base64");
834
837
  }
838
+ function create402Response(requirement, body) {
839
+ const headers = new Headers({
840
+ "Content-Type": "application/json",
841
+ "X-Payment-Required": encodePaymentRequirement(requirement)
842
+ });
843
+ const responseBody = body || {
844
+ error: "Payment Required",
845
+ message: "This resource requires payment to access",
846
+ x402Version: 1,
847
+ accepts: [requirement]
848
+ };
849
+ return new Response(JSON.stringify(responseBody), {
850
+ status: 402,
851
+ headers
852
+ });
853
+ }
835
854
 
836
855
  // src/store/memory.ts
837
856
  function createMemoryStore(options = {}) {
@@ -976,16 +995,22 @@ function createPaywallMiddleware(config2) {
976
995
  const sessionToken = cookies[cookieName];
977
996
  const result = await checkPaywallAccess(path, sessionToken, config2);
978
997
  if (!result.allowed && result.requiresPayment) {
998
+ const headers = {
999
+ "Content-Type": "application/json"
1000
+ };
1001
+ if (config2.paymentRequirement) {
1002
+ const requirement = typeof config2.paymentRequirement === "function" ? config2.paymentRequirement(path) : config2.paymentRequirement;
1003
+ headers["X-Payment-Required"] = encodePaymentRequirement(requirement);
1004
+ }
979
1005
  const body = config2.custom402Response ? config2.custom402Response(path) : {
980
1006
  error: "Payment Required",
981
1007
  message: "This resource requires payment to access",
1008
+ x402Version: 1,
982
1009
  path
983
1010
  };
984
1011
  return new Response(JSON.stringify(body), {
985
1012
  status: 402,
986
- headers: {
987
- "Content-Type": "application/json"
988
- }
1013
+ headers
989
1014
  });
990
1015
  }
991
1016
  return null;
@@ -1314,6 +1339,7 @@ exports.checkPaywallAccess = checkPaywallAccess;
1314
1339
  exports.clearPriceCache = clearPriceCache;
1315
1340
  exports.configurePricing = configurePricing;
1316
1341
  exports.create402Headers = create402Headers;
1342
+ exports.create402Response = create402Response;
1317
1343
  exports.create402ResponseBody = create402ResponseBody;
1318
1344
  exports.createMemoryStore = createMemoryStore;
1319
1345
  exports.createPaymentFlow = createPaymentFlow;
@@ -1324,6 +1350,7 @@ exports.createRedisStore = createRedisStore;
1324
1350
  exports.createSession = createSession;
1325
1351
  exports.decodePaymentRequired = decodePaymentRequired;
1326
1352
  exports.encodePaymentRequired = encodePaymentRequired;
1353
+ exports.encodePaymentRequirement = encodePaymentRequirement;
1327
1354
  exports.encodePaymentResponse = encodePaymentResponse;
1328
1355
  exports.estimatePriorityFee = estimatePriorityFee;
1329
1356
  exports.fetchLookupTables = fetchLookupTables;