@alleyboss/micropay-solana-x402-paywall 3.0.2 → 3.0.4

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
@@ -60,26 +60,27 @@ app.get('/premium', x402Middleware(server, {
60
60
 
61
61
  ## 📦 Next.js (App Router)
62
62
 
63
- Use the official `@x402/next` adapter with our session management:
64
-
65
63
  ```typescript
66
64
  // app/api/premium/route.ts
67
- import { withX402 } from '@x402/next';
68
- import { x402ResourceServer } from '@x402/core/server';
65
+ import { createX402Middleware } from '@alleyboss/micropay-solana-x402-paywall/next';
69
66
 
70
- const server = new x402ResourceServer({ ... });
67
+ const withMicropay = createX402Middleware({
68
+ walletAddress: 'YOUR_WALLET_ADDRESS',
69
+ network: 'devnet'
70
+ });
71
71
 
72
72
  const handler = (req) => {
73
- return Response.json({ content: "Premium Data" });
73
+ return Response.json({ content: "Premium Data" });
74
74
  };
75
75
 
76
- export const GET = withX402(handler, {
77
- accepts: {
78
- scheme: 'exact',
79
- amount: '1000000',
80
- network: 'solana-mainnet'
81
- }
82
- }, server);
76
+ // Protect the route
77
+ export const GET = withMicropay(handler, {
78
+ description: "Unlock Premium Content",
79
+ accepts: {
80
+ amount: "1000000", // 0.001 SOL
81
+ scheme: "exact"
82
+ }
83
+ });
83
84
  ```
84
85
 
85
86
  ## 🔧 Modules
@@ -1,5 +1,7 @@
1
1
  'use strict';
2
2
 
3
+ var http = require('@x402/core/http');
4
+
3
5
  // src/client/types.ts
4
6
  var TOKEN_MINTS = {
5
7
  /** USDC on mainnet */
@@ -91,7 +93,26 @@ function createPaymentReference() {
91
93
  }
92
94
  return `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
93
95
  }
96
+ function createX402AuthorizationHeader(signature, paymentRequiredHeader) {
97
+ const cleanHeader = paymentRequiredHeader.replace(/^[Xx]402\s+/, "");
98
+ const required = http.decodePaymentRequiredHeader(cleanHeader);
99
+ const accepts = Array.isArray(required.accepts) ? required.accepts[0] : required.accepts;
100
+ const payload = {
101
+ accepted: accepts,
102
+ client: {
103
+ scheme: accepts.scheme,
104
+ // TypeScript knows this exists on PaymentRequirements
105
+ network: accepts.network
106
+ },
107
+ payment: {
108
+ signature
109
+ }
110
+ };
111
+ const token = http.encodePaymentSignatureHeader(payload);
112
+ return `x402 ${token}`;
113
+ }
94
114
 
95
115
  exports.buildSolanaPayUrl = buildSolanaPayUrl;
96
116
  exports.createPaymentFlow = createPaymentFlow;
97
117
  exports.createPaymentReference = createPaymentReference;
118
+ exports.createX402AuthorizationHeader = createX402AuthorizationHeader;
@@ -118,4 +118,12 @@ declare function createPaymentFlow(config: PaymentFlowConfig): {
118
118
  */
119
119
  declare function createPaymentReference(): string;
120
120
 
121
- export { type PaymentFlowConfig, type SolanaPayUrlParams, buildSolanaPayUrl, createPaymentFlow, createPaymentReference };
121
+ /**
122
+ * Creates the Authorization header value for x402 authentication
123
+ *
124
+ * @param signature - The payment signature/proof
125
+ * @param paymentRequiredHeader - The WWW-Authenticate header value from the 402 response
126
+ */
127
+ declare function createX402AuthorizationHeader(signature: string, paymentRequiredHeader: string): string;
128
+
129
+ export { type PaymentFlowConfig, type SolanaPayUrlParams, buildSolanaPayUrl, createPaymentFlow, createPaymentReference, createX402AuthorizationHeader };
@@ -118,4 +118,12 @@ declare function createPaymentFlow(config: PaymentFlowConfig): {
118
118
  */
119
119
  declare function createPaymentReference(): string;
120
120
 
121
- export { type PaymentFlowConfig, type SolanaPayUrlParams, buildSolanaPayUrl, createPaymentFlow, createPaymentReference };
121
+ /**
122
+ * Creates the Authorization header value for x402 authentication
123
+ *
124
+ * @param signature - The payment signature/proof
125
+ * @param paymentRequiredHeader - The WWW-Authenticate header value from the 402 response
126
+ */
127
+ declare function createX402AuthorizationHeader(signature: string, paymentRequiredHeader: string): string;
128
+
129
+ export { type PaymentFlowConfig, type SolanaPayUrlParams, buildSolanaPayUrl, createPaymentFlow, createPaymentReference, createX402AuthorizationHeader };
@@ -1,3 +1,5 @@
1
+ import { decodePaymentRequiredHeader, encodePaymentSignatureHeader } from '@x402/core/http';
2
+
1
3
  // src/client/types.ts
2
4
  var TOKEN_MINTS = {
3
5
  /** USDC on mainnet */
@@ -89,5 +91,23 @@ function createPaymentReference() {
89
91
  }
90
92
  return `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
91
93
  }
94
+ function createX402AuthorizationHeader(signature, paymentRequiredHeader) {
95
+ const cleanHeader = paymentRequiredHeader.replace(/^[Xx]402\s+/, "");
96
+ const required = decodePaymentRequiredHeader(cleanHeader);
97
+ const accepts = Array.isArray(required.accepts) ? required.accepts[0] : required.accepts;
98
+ const payload = {
99
+ accepted: accepts,
100
+ client: {
101
+ scheme: accepts.scheme,
102
+ // TypeScript knows this exists on PaymentRequirements
103
+ network: accepts.network
104
+ },
105
+ payment: {
106
+ signature
107
+ }
108
+ };
109
+ const token = encodePaymentSignatureHeader(payload);
110
+ return `x402 ${token}`;
111
+ }
92
112
 
93
- export { buildSolanaPayUrl, createPaymentFlow, createPaymentReference };
113
+ export { buildSolanaPayUrl, createPaymentFlow, createPaymentReference, createX402AuthorizationHeader };
package/dist/index.cjs CHANGED
@@ -4,6 +4,7 @@ var core = require('@x402/core');
4
4
  var types = require('@x402/core/types');
5
5
  var client = require('@x402/core/client');
6
6
  var svm = require('@x402/svm');
7
+ var http = require('@x402/core/http');
7
8
  var web3_js = require('@solana/web3.js');
8
9
  var jose = require('jose');
9
10
 
@@ -100,6 +101,24 @@ function createPaymentReference() {
100
101
  }
101
102
  return `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
102
103
  }
104
+ function createX402AuthorizationHeader(signature, paymentRequiredHeader) {
105
+ const cleanHeader = paymentRequiredHeader.replace(/^[Xx]402\s+/, "");
106
+ const required = http.decodePaymentRequiredHeader(cleanHeader);
107
+ const accepts = Array.isArray(required.accepts) ? required.accepts[0] : required.accepts;
108
+ const payload = {
109
+ accepted: accepts,
110
+ client: {
111
+ scheme: accepts.scheme,
112
+ // TypeScript knows this exists on PaymentRequirements
113
+ network: accepts.network
114
+ },
115
+ payment: {
116
+ signature
117
+ }
118
+ };
119
+ const token = http.encodePaymentSignatureHeader(payload);
120
+ return `x402 ${token}`;
121
+ }
103
122
  var DEFAULT_COMPUTE_UNITS = 2e5;
104
123
  var DEFAULT_MICRO_LAMPORTS = 1e3;
105
124
  function createPriorityFeeInstructions(config2 = {}) {
@@ -577,6 +596,7 @@ exports.configurePricing = configurePricing;
577
596
  exports.createCreditSession = createCreditSession;
578
597
  exports.createPaymentFlow = createPaymentFlow;
579
598
  exports.createPaymentReference = createPaymentReference;
599
+ exports.createX402AuthorizationHeader = createX402AuthorizationHeader;
580
600
  exports.executeAgentPayment = executeAgentPayment;
581
601
  exports.formatPriceDisplay = formatPriceDisplay;
582
602
  exports.formatPriceSync = formatPriceSync;
package/dist/index.d.cts CHANGED
@@ -2,7 +2,7 @@ export * from '@x402/core';
2
2
  export * from '@x402/core/types';
3
3
  export * from '@x402/core/client';
4
4
  export * from '@x402/svm';
5
- export { PaymentFlowConfig, SolanaPayUrlParams, buildSolanaPayUrl, createPaymentFlow, createPaymentReference } from './client/index.cjs';
5
+ export { PaymentFlowConfig, SolanaPayUrlParams, buildSolanaPayUrl, createPaymentFlow, createPaymentReference, createX402AuthorizationHeader } from './client/index.cjs';
6
6
  export { AgentPaymentResult, CreditSessionClaims, CreditSessionConfig, CreditSessionData, CreditValidation, ExecuteAgentPaymentParams, UseCreditResult, addCredits, createCreditSession, executeAgentPayment, generateAgentKeypair, getAgentBalance, getRemainingCredits, hasAgentSufficientBalance, keypairFromBase58, useCredit, validateCreditSession } from './agent/index.cjs';
7
7
  export { CustomPriceProvider, PriceConfig, PriceData, clearPriceCache, configurePricing, formatPriceDisplay, formatPriceSync, getProviders, getSolPrice, lamportsToUsd, usdToLamports } from './pricing/index.cjs';
8
8
  import '@solana/web3.js';
package/dist/index.d.ts CHANGED
@@ -2,7 +2,7 @@ export * from '@x402/core';
2
2
  export * from '@x402/core/types';
3
3
  export * from '@x402/core/client';
4
4
  export * from '@x402/svm';
5
- export { PaymentFlowConfig, SolanaPayUrlParams, buildSolanaPayUrl, createPaymentFlow, createPaymentReference } from './client/index.js';
5
+ export { PaymentFlowConfig, SolanaPayUrlParams, buildSolanaPayUrl, createPaymentFlow, createPaymentReference, createX402AuthorizationHeader } from './client/index.js';
6
6
  export { AgentPaymentResult, CreditSessionClaims, CreditSessionConfig, CreditSessionData, CreditValidation, ExecuteAgentPaymentParams, UseCreditResult, addCredits, createCreditSession, executeAgentPayment, generateAgentKeypair, getAgentBalance, getRemainingCredits, hasAgentSufficientBalance, keypairFromBase58, useCredit, validateCreditSession } from './agent/index.js';
7
7
  export { CustomPriceProvider, PriceConfig, PriceData, clearPriceCache, configurePricing, formatPriceDisplay, formatPriceSync, getProviders, getSolPrice, lamportsToUsd, usdToLamports } from './pricing/index.js';
8
8
  import '@solana/web3.js';
package/dist/index.js CHANGED
@@ -2,6 +2,7 @@ export * from '@x402/core';
2
2
  export * from '@x402/core/types';
3
3
  export * from '@x402/core/client';
4
4
  export * from '@x402/svm';
5
+ import { decodePaymentRequiredHeader, encodePaymentSignatureHeader } from '@x402/core/http';
5
6
  import { PublicKey, SystemProgram, LAMPORTS_PER_SOL, Keypair, TransactionMessage, VersionedTransaction, ComputeBudgetProgram } from '@solana/web3.js';
6
7
  import { SignJWT, jwtVerify } from 'jose';
7
8
 
@@ -98,6 +99,24 @@ function createPaymentReference() {
98
99
  }
99
100
  return `${Date.now()}-${Math.random().toString(36).slice(2, 11)}`;
100
101
  }
102
+ function createX402AuthorizationHeader(signature, paymentRequiredHeader) {
103
+ const cleanHeader = paymentRequiredHeader.replace(/^[Xx]402\s+/, "");
104
+ const required = decodePaymentRequiredHeader(cleanHeader);
105
+ const accepts = Array.isArray(required.accepts) ? required.accepts[0] : required.accepts;
106
+ const payload = {
107
+ accepted: accepts,
108
+ client: {
109
+ scheme: accepts.scheme,
110
+ // TypeScript knows this exists on PaymentRequirements
111
+ network: accepts.network
112
+ },
113
+ payment: {
114
+ signature
115
+ }
116
+ };
117
+ const token = encodePaymentSignatureHeader(payload);
118
+ return `x402 ${token}`;
119
+ }
101
120
  var DEFAULT_COMPUTE_UNITS = 2e5;
102
121
  var DEFAULT_MICRO_LAMPORTS = 1e3;
103
122
  function createPriorityFeeInstructions(config2 = {}) {
@@ -568,4 +587,4 @@ function getProviders() {
568
587
  return PROVIDERS.map((p) => ({ name: p.name, url: p.url }));
569
588
  }
570
589
 
571
- export { addCredits, buildSolanaPayUrl, clearPriceCache, configurePricing, createCreditSession, createPaymentFlow, createPaymentReference, executeAgentPayment, formatPriceDisplay, formatPriceSync, generateAgentKeypair, getAgentBalance, getProviders, getRemainingCredits, getSolPrice, hasAgentSufficientBalance, keypairFromBase58, lamportsToUsd, usdToLamports, useCredit, validateCreditSession };
590
+ export { addCredits, buildSolanaPayUrl, clearPriceCache, configurePricing, createCreditSession, createPaymentFlow, createPaymentReference, createX402AuthorizationHeader, executeAgentPayment, formatPriceDisplay, formatPriceSync, generateAgentKeypair, getAgentBalance, getProviders, getRemainingCredits, getSolPrice, hasAgentSufficientBalance, keypairFromBase58, lamportsToUsd, usdToLamports, useCredit, validateCreditSession };
@@ -0,0 +1,34 @@
1
+ 'use strict';
2
+
3
+ var next = require('@x402/next');
4
+ var server = require('@x402/core/server');
5
+ var http = require('@x402/core/http');
6
+ var server$1 = require('@x402/svm/exact/server');
7
+
8
+ // src/next/index.ts
9
+ function createX402Middleware(config) {
10
+ const facilitatorUrl = config.facilitatorUrl || "https://x402.org/facilitator";
11
+ const client = new http.HTTPFacilitatorClient({ url: facilitatorUrl });
12
+ const server$2 = new server.x402ResourceServer(client);
13
+ server$1.registerExactSvmScheme(server$2, {});
14
+ return function withMicropay(handler, routeConfig) {
15
+ const finalConfig = routeConfig || {
16
+ accepts: {
17
+ scheme: "exact",
18
+ payTo: config.walletAddress,
19
+ maxAmountRequired: config.price?.toString() || "0",
20
+ network: config.network === "mainnet-beta" ? "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp" : "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1",
21
+ asset: "native"
22
+ }
23
+ };
24
+ return next.withX402(handler, finalConfig, server$2);
25
+ };
26
+ }
27
+ var withX402 = next.withX402;
28
+
29
+ Object.defineProperty(exports, "x402ResourceServer", {
30
+ enumerable: true,
31
+ get: function () { return server.x402ResourceServer; }
32
+ });
33
+ exports.createX402Middleware = createX402Middleware;
34
+ exports.withX402 = withX402;
@@ -0,0 +1,24 @@
1
+ import * as next_server from 'next/server';
2
+ import { withX402 as withX402$1 } from '@x402/next';
3
+ export { x402ResourceServer } from '@x402/core/server';
4
+
5
+ /**
6
+ * Configuration for the x402 middleware
7
+ */
8
+ interface X402Config {
9
+ /** x402 Facilitator URL (default: x402.org) */
10
+ facilitatorUrl?: string;
11
+ /** Wallet address to receive payments */
12
+ walletAddress: string;
13
+ /** Price in lamports (string or number) */
14
+ price?: string | number;
15
+ /** Network (mainnet-beta or devnet) */
16
+ network?: 'mainnet-beta' | 'devnet';
17
+ }
18
+ /**
19
+ * Create a specialized Next.js middleware with Solana support pre-configured
20
+ */
21
+ declare function createX402Middleware(config: X402Config): (handler: any, routeConfig?: any) => (request: next_server.NextRequest) => Promise<next_server.NextResponse<unknown>>;
22
+ declare const withX402: typeof withX402$1;
23
+
24
+ export { type X402Config, createX402Middleware, withX402 };
@@ -0,0 +1,24 @@
1
+ import * as next_server from 'next/server';
2
+ import { withX402 as withX402$1 } from '@x402/next';
3
+ export { x402ResourceServer } from '@x402/core/server';
4
+
5
+ /**
6
+ * Configuration for the x402 middleware
7
+ */
8
+ interface X402Config {
9
+ /** x402 Facilitator URL (default: x402.org) */
10
+ facilitatorUrl?: string;
11
+ /** Wallet address to receive payments */
12
+ walletAddress: string;
13
+ /** Price in lamports (string or number) */
14
+ price?: string | number;
15
+ /** Network (mainnet-beta or devnet) */
16
+ network?: 'mainnet-beta' | 'devnet';
17
+ }
18
+ /**
19
+ * Create a specialized Next.js middleware with Solana support pre-configured
20
+ */
21
+ declare function createX402Middleware(config: X402Config): (handler: any, routeConfig?: any) => (request: next_server.NextRequest) => Promise<next_server.NextResponse<unknown>>;
22
+ declare const withX402: typeof withX402$1;
23
+
24
+ export { type X402Config, createX402Middleware, withX402 };
@@ -0,0 +1,28 @@
1
+ import { withX402 as withX402$1 } from '@x402/next';
2
+ import { x402ResourceServer } from '@x402/core/server';
3
+ export { x402ResourceServer } from '@x402/core/server';
4
+ import { HTTPFacilitatorClient } from '@x402/core/http';
5
+ import { registerExactSvmScheme } from '@x402/svm/exact/server';
6
+
7
+ // src/next/index.ts
8
+ function createX402Middleware(config) {
9
+ const facilitatorUrl = config.facilitatorUrl || "https://x402.org/facilitator";
10
+ const client = new HTTPFacilitatorClient({ url: facilitatorUrl });
11
+ const server = new x402ResourceServer(client);
12
+ registerExactSvmScheme(server, {});
13
+ return function withMicropay(handler, routeConfig) {
14
+ const finalConfig = routeConfig || {
15
+ accepts: {
16
+ scheme: "exact",
17
+ payTo: config.walletAddress,
18
+ maxAmountRequired: config.price?.toString() || "0",
19
+ network: config.network === "mainnet-beta" ? "solana:5eykt4UsFv8P8NJdTREpY1vzqKqZKvdp" : "solana:EtWTRABZaYq6iMfeYKouRu166VU2xqa1",
20
+ asset: "native"
21
+ }
22
+ };
23
+ return withX402$1(handler, finalConfig, server);
24
+ };
25
+ }
26
+ var withX402 = withX402$1;
27
+
28
+ export { createX402Middleware, withX402 };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alleyboss/micropay-solana-x402-paywall",
3
- "version": "3.0.2",
3
+ "version": "3.0.4",
4
4
  "description": "Production-ready Solana micropayments library wrapper for official x402 SDK",
5
5
  "author": "AlleyBoss",
6
6
  "license": "MIT",
@@ -42,6 +42,16 @@
42
42
  "default": "./dist/express/index.cjs"
43
43
  }
44
44
  },
45
+ "./next": {
46
+ "import": {
47
+ "types": "./dist/next/index.d.ts",
48
+ "default": "./dist/next/index.js"
49
+ },
50
+ "require": {
51
+ "types": "./dist/next/index.d.cts",
52
+ "default": "./dist/next/index.cjs"
53
+ }
54
+ },
45
55
  "./agent": {
46
56
  "import": {
47
57
  "types": "./dist/agent/index.d.ts",
@@ -99,6 +109,7 @@
99
109
  },
100
110
  "dependencies": {
101
111
  "@x402/core": "^2.1.0",
112
+ "@x402/next": "^2.1.0",
102
113
  "@x402/svm": "^2.1.0",
103
114
  "jose": "^6.1.3"
104
115
  },