@alleyboss/micropay-solana-x402-paywall 2.3.1 → 3.0.1

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.
Files changed (72) hide show
  1. package/README.md +85 -139
  2. package/dist/agent/index.cjs +1 -2
  3. package/dist/agent/index.cjs.map +1 -1
  4. package/dist/agent/index.d.cts +11 -2
  5. package/dist/agent/index.d.ts +11 -2
  6. package/dist/agent/index.js +1 -2
  7. package/dist/agent/index.js.map +1 -1
  8. package/dist/client/index.cjs +1 -1
  9. package/dist/client/index.cjs.map +1 -1
  10. package/dist/client/index.d.cts +10 -1
  11. package/dist/client/index.d.ts +10 -1
  12. package/dist/client/index.js +1 -1
  13. package/dist/client/index.js.map +1 -1
  14. package/dist/express/index.cjs +79 -0
  15. package/dist/express/index.cjs.map +1 -0
  16. package/dist/express/index.d.cts +40 -0
  17. package/dist/express/index.d.ts +40 -0
  18. package/dist/express/index.js +76 -0
  19. package/dist/express/index.js.map +1 -0
  20. package/dist/index.cjs +257 -1357
  21. package/dist/index.cjs.map +1 -1
  22. package/dist/index.d.cts +6 -12
  23. package/dist/index.d.ts +6 -12
  24. package/dist/index.js +239 -1319
  25. package/dist/index.js.map +1 -1
  26. package/dist/session/index.cjs.map +1 -1
  27. package/dist/session/index.d.cts +1 -1
  28. package/dist/session/index.d.ts +1 -1
  29. package/dist/session/index.js.map +1 -1
  30. package/dist/{session-D2IoWAWV.d.cts → types-BWYQMw03.d.cts} +1 -16
  31. package/dist/{session-D2IoWAWV.d.ts → types-BWYQMw03.d.ts} +1 -16
  32. package/package.json +28 -68
  33. package/dist/client-D-dteoJw.d.cts +0 -63
  34. package/dist/client-DfCIRrNG.d.ts +0 -63
  35. package/dist/memory-Daxkczti.d.cts +0 -29
  36. package/dist/memory-Daxkczti.d.ts +0 -29
  37. package/dist/middleware/index.cjs +0 -273
  38. package/dist/middleware/index.cjs.map +0 -1
  39. package/dist/middleware/index.d.cts +0 -91
  40. package/dist/middleware/index.d.ts +0 -91
  41. package/dist/middleware/index.js +0 -267
  42. package/dist/middleware/index.js.map +0 -1
  43. package/dist/nextjs-BDyOqGAq.d.cts +0 -81
  44. package/dist/nextjs-CbX8_9yK.d.ts +0 -81
  45. package/dist/payment-BGp7eMQl.d.cts +0 -103
  46. package/dist/payment-BGp7eMQl.d.ts +0 -103
  47. package/dist/priority-fees-C-OH4Trr.d.cts +0 -50
  48. package/dist/priority-fees-C-OH4Trr.d.ts +0 -50
  49. package/dist/solana/index.cjs +0 -589
  50. package/dist/solana/index.cjs.map +0 -1
  51. package/dist/solana/index.d.cts +0 -195
  52. package/dist/solana/index.d.ts +0 -195
  53. package/dist/solana/index.js +0 -567
  54. package/dist/solana/index.js.map +0 -1
  55. package/dist/store/index.cjs +0 -99
  56. package/dist/store/index.cjs.map +0 -1
  57. package/dist/store/index.d.cts +0 -38
  58. package/dist/store/index.d.ts +0 -38
  59. package/dist/store/index.js +0 -96
  60. package/dist/store/index.js.map +0 -1
  61. package/dist/utils/index.cjs +0 -68
  62. package/dist/utils/index.cjs.map +0 -1
  63. package/dist/utils/index.d.cts +0 -30
  64. package/dist/utils/index.d.ts +0 -30
  65. package/dist/utils/index.js +0 -65
  66. package/dist/utils/index.js.map +0 -1
  67. package/dist/x402/index.cjs +0 -387
  68. package/dist/x402/index.cjs.map +0 -1
  69. package/dist/x402/index.d.cts +0 -96
  70. package/dist/x402/index.d.ts +0 -96
  71. package/dist/x402/index.js +0 -375
  72. package/dist/x402/index.js.map +0 -1
@@ -1,65 +0,0 @@
1
- // src/utils/retry.ts
2
- function sleep(ms) {
3
- return new Promise((resolve) => setTimeout(resolve, ms));
4
- }
5
- function calculateDelay(attempt, options) {
6
- const { baseDelay, maxDelay, jitter } = options;
7
- let delay = baseDelay * Math.pow(2, attempt);
8
- delay = Math.min(delay, maxDelay);
9
- if (jitter) {
10
- const jitterAmount = delay * 0.25;
11
- delay += Math.random() * jitterAmount * 2 - jitterAmount;
12
- }
13
- return Math.floor(delay);
14
- }
15
- async function withRetry(fn, options = {}) {
16
- const {
17
- maxAttempts = 3,
18
- baseDelay = 500,
19
- maxDelay = 1e4,
20
- jitter = true,
21
- retryOn = () => true
22
- } = options;
23
- let lastError;
24
- for (let attempt = 0; attempt < maxAttempts; attempt++) {
25
- try {
26
- return await fn();
27
- } catch (error) {
28
- lastError = error;
29
- if (!retryOn(error)) {
30
- throw error;
31
- }
32
- if (attempt < maxAttempts - 1) {
33
- const delay = calculateDelay(attempt, {
34
- baseDelay,
35
- maxDelay,
36
- jitter
37
- });
38
- await sleep(delay);
39
- }
40
- }
41
- }
42
- throw lastError;
43
- }
44
- function isRetryableRPCError(error) {
45
- if (error instanceof Error) {
46
- const message = error.message.toLowerCase();
47
- if (message.includes("429") || message.includes("rate limit")) {
48
- return true;
49
- }
50
- if (message.includes("timeout") || message.includes("econnreset")) {
51
- return true;
52
- }
53
- if (message.includes("503") || message.includes("502") || message.includes("500")) {
54
- return true;
55
- }
56
- if (message.includes("blockhash not found") || message.includes("slot skipped")) {
57
- return true;
58
- }
59
- }
60
- return false;
61
- }
62
-
63
- export { isRetryableRPCError, withRetry };
64
- //# sourceMappingURL=index.js.map
65
- //# sourceMappingURL=index.js.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/utils/retry.ts"],"names":[],"mappings":";AAmBA,SAAS,MAAM,EAAA,EAA2B;AACtC,EAAA,OAAO,IAAI,OAAA,CAAQ,CAAA,OAAA,KAAW,UAAA,CAAW,OAAA,EAAS,EAAE,CAAC,CAAA;AACzD;AAKA,SAAS,cAAA,CAAe,SAAiB,OAAA,EAA0D;AAC/F,EAAA,MAAM,EAAE,SAAA,EAAW,QAAA,EAAU,MAAA,EAAO,GAAI,OAAA;AAGxC,EAAA,IAAI,KAAA,GAAQ,SAAA,GAAY,IAAA,CAAK,GAAA,CAAI,GAAG,OAAO,CAAA;AAG3C,EAAA,KAAA,GAAQ,IAAA,CAAK,GAAA,CAAI,KAAA,EAAO,QAAQ,CAAA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AK,KAAK,OAAA,CAAQ,QAAA,CAAS,YAAY,CAAA,EAAG;AAC3D,MAAA,OAAO,IAAA;AAAA,IACX;AAGA,IAAA,IAAI,QAAQ,QAAA,CAAS,SAAS,KAAK,OAAA,CAAQ,QAAA,CAAS,YAAY,CAAA,EAAG;AAC/D,MAAA,OAAO,IAAA;AAAA,IACX;AAGA,IAAA,IAAI,OAAA,CAAQ,QAAA,CAAS,KAAK,CAAA,IAAK,OAAA,CAAQ,QAAA,CAAS,KAAK,CAAA,IAAK,OAAA,CAAQ,QAAA,CAAS,KAAK,CAAA,EAAG;AAC/E,MAAA,OAAO,IAAA;AAAA,IACX;AAGA,IAAA,IAAI,QAAQ,QAAA,CAAS,qBAAqB,KAAK,OAAA,CAAQ,QAAA,CAAS,cAAc,CAAA,EAAG;AAC7E,MAAA,OAAO,IAAA;AAAA,IACX;AAAA,EACJ;AAEA,EAAA,OAAO,KAAA;AACX","file":"index.js","sourcesContent":["// Utility functions\n// Retry logic with exponential backoff for RPC resilience\n\nexport interface RetryOptions {\n /** Maximum number of attempts */\n maxAttempts?: number;\n /** Base delay in milliseconds */\n baseDelay?: number;\n /** Maximum delay in milliseconds */\n maxDelay?: number;\n /** Whether to add jitter to delay */\n jitter?: boolean;\n /** Errors to retry on (default: all) */\n retryOn?: (error: unknown) => boolean;\n}\n\n/**\n * Sleep for a given duration\n */\nfunction sleep(ms: number): Promise<void> {\n return new Promise(resolve => setTimeout(resolve, ms));\n}\n\n/**\n * Calculate delay with exponential backoff and optional jitter\n */\nfunction calculateDelay(attempt: number, options: Required<Omit<RetryOptions, 'retryOn'>>): number {\n const { baseDelay, maxDelay, jitter } = options;\n\n // Exponential backoff: baseDelay * 2^attempt\n let delay = baseDelay * Math.pow(2, attempt);\n\n // Cap at maxDelay\n delay = Math.min(delay, maxDelay);\n\n // Add jitter (±25%)\n if (jitter) {\n const jitterAmount = delay * 0.25;\n delay += (Math.random() * jitterAmount * 2) - jitterAmount;\n }\n\n return Math.floor(delay);\n}\n\n/**\n * Execute a function with retry logic and exponential backoff\n * \n * @example\n * ```typescript\n * const result = await withRetry(\n * () => connection.getBalance(publicKey),\n * { maxAttempts: 3, baseDelay: 500 }\n * );\n * ```\n */\nexport async function withRetry<T>(\n fn: () => Promise<T>,\n options: RetryOptions = {}\n): Promise<T> {\n const {\n maxAttempts = 3,\n baseDelay = 500,\n maxDelay = 10000,\n jitter = true,\n retryOn = () => true,\n } = options;\n\n let lastError: unknown;\n\n for (let attempt = 0; attempt < maxAttempts; attempt++) {\n try {\n return await fn();\n } catch (error) {\n lastError = error;\n\n // Check if we should retry this error\n if (!retryOn(error)) {\n throw error;\n }\n\n // Don't sleep after the last attempt\n if (attempt < maxAttempts - 1) {\n const delay = calculateDelay(attempt, {\n maxAttempts,\n baseDelay,\n maxDelay,\n jitter\n });\n await sleep(delay);\n }\n }\n }\n\n throw lastError;\n}\n\n/**\n * Check if error is a transient RPC error that should be retried\n */\nexport function isRetryableRPCError(error: unknown): boolean {\n if (error instanceof Error) {\n const message = error.message.toLowerCase();\n\n // Rate limiting\n if (message.includes('429') || message.includes('rate limit')) {\n return true;\n }\n\n // Network errors\n if (message.includes('timeout') || message.includes('econnreset')) {\n return true;\n }\n\n // Server errors\n if (message.includes('503') || message.includes('502') || message.includes('500')) {\n return true;\n }\n\n // Solana-specific transient errors\n if (message.includes('blockhash not found') || message.includes('slot skipped')) {\n return true;\n }\n }\n\n return false;\n}\n"]}
@@ -1,387 +0,0 @@
1
- 'use strict';
2
-
3
- var web3_js = require('@solana/web3.js');
4
-
5
- // src/solana/client.ts
6
- var cachedConnection = null;
7
- var cachedNetwork = null;
8
- var cachedFallbackEnabled = false;
9
- function buildRpcUrl(config) {
10
- const { network, rpcUrl, tatumApiKey } = config;
11
- if (rpcUrl) {
12
- if (rpcUrl.includes("tatum.io") && tatumApiKey && !rpcUrl.includes(tatumApiKey)) {
13
- return rpcUrl.endsWith("/") ? `${rpcUrl}${tatumApiKey}` : `${rpcUrl}/${tatumApiKey}`;
14
- }
15
- return rpcUrl;
16
- }
17
- if (tatumApiKey) {
18
- const baseUrl = network === "mainnet-beta" ? "https://solana-mainnet.gateway.tatum.io" : "https://solana-devnet.gateway.tatum.io";
19
- return `${baseUrl}/${tatumApiKey}`;
20
- }
21
- return web3_js.clusterApiUrl(network);
22
- }
23
- function createConnection(rpcUrl) {
24
- return new web3_js.Connection(rpcUrl, {
25
- commitment: "confirmed",
26
- confirmTransactionInitialTimeout: 6e4
27
- });
28
- }
29
- function getConnection(config) {
30
- const { network } = config;
31
- if (cachedConnection && cachedNetwork === network) {
32
- return cachedConnection;
33
- }
34
- const rpcUrl = buildRpcUrl(config);
35
- cachedConnection = createConnection(rpcUrl);
36
- cachedNetwork = network;
37
- cachedFallbackEnabled = config.enableFallback ?? false;
38
- if (cachedFallbackEnabled && config.fallbackRpcUrls?.length) {
39
- config.fallbackRpcUrls.map(createConnection);
40
- }
41
- return cachedConnection;
42
- }
43
- function toX402Network(network) {
44
- return network === "mainnet-beta" ? "solana-mainnet" : "solana-devnet";
45
- }
46
- var SIGNATURE_REGEX = /^[1-9A-HJ-NP-Za-km-z]{87,88}$/;
47
- var WALLET_REGEX = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
48
- function isValidSignature(signature) {
49
- if (!signature || typeof signature !== "string") return false;
50
- return SIGNATURE_REGEX.test(signature);
51
- }
52
- function isValidWalletAddress(address) {
53
- if (!address || typeof address !== "string") return false;
54
- return WALLET_REGEX.test(address);
55
- }
56
- function parseSOLTransfer(transaction, expectedRecipient) {
57
- const instructions = transaction.transaction.message.instructions;
58
- for (const ix of instructions) {
59
- if ("parsed" in ix && ix.program === "system") {
60
- const parsed = ix.parsed;
61
- if (parsed.type === "transfer" && parsed.info.destination === expectedRecipient) {
62
- return {
63
- from: parsed.info.source,
64
- to: parsed.info.destination,
65
- amount: BigInt(parsed.info.lamports)
66
- };
67
- }
68
- }
69
- }
70
- if (transaction.meta?.innerInstructions) {
71
- for (const inner of transaction.meta.innerInstructions) {
72
- for (const ix of inner.instructions) {
73
- if ("parsed" in ix && ix.program === "system") {
74
- const parsed = ix.parsed;
75
- if (parsed.type === "transfer" && parsed.info.destination === expectedRecipient) {
76
- return {
77
- from: parsed.info.source,
78
- to: parsed.info.destination,
79
- amount: BigInt(parsed.info.lamports)
80
- };
81
- }
82
- }
83
- }
84
- }
85
- }
86
- return null;
87
- }
88
- async function verifyPayment(params) {
89
- const {
90
- signature,
91
- expectedRecipient,
92
- expectedAmount,
93
- maxAgeSeconds = 300,
94
- clientConfig,
95
- signatureStore
96
- } = params;
97
- if (!isValidSignature(signature)) {
98
- return { valid: false, confirmed: false, signature, error: "Invalid signature format" };
99
- }
100
- if (!isValidWalletAddress(expectedRecipient)) {
101
- return { valid: false, confirmed: false, signature, error: "Invalid recipient address" };
102
- }
103
- if (expectedAmount <= 0n) {
104
- return { valid: false, confirmed: false, signature, error: "Invalid expected amount" };
105
- }
106
- if (signatureStore) {
107
- const isUsed = await signatureStore.hasBeenUsed(signature);
108
- if (isUsed) {
109
- return { valid: false, confirmed: true, signature, error: "Signature already used" };
110
- }
111
- }
112
- const effectiveMaxAge = Math.min(Math.max(maxAgeSeconds, 60), 3600);
113
- const connection = getConnection(clientConfig);
114
- try {
115
- const transaction = await connection.getParsedTransaction(signature, {
116
- commitment: "confirmed",
117
- maxSupportedTransactionVersion: 0
118
- });
119
- if (!transaction) {
120
- return { valid: false, confirmed: false, signature, error: "Transaction not found" };
121
- }
122
- if (transaction.meta?.err) {
123
- return {
124
- valid: false,
125
- confirmed: true,
126
- signature,
127
- error: "Transaction failed on-chain"
128
- };
129
- }
130
- if (transaction.blockTime) {
131
- const now = Math.floor(Date.now() / 1e3);
132
- if (now - transaction.blockTime > effectiveMaxAge) {
133
- return { valid: false, confirmed: true, signature, error: "Transaction too old" };
134
- }
135
- if (transaction.blockTime > now + 60) {
136
- return { valid: false, confirmed: true, signature, error: "Invalid transaction time" };
137
- }
138
- }
139
- const transferDetails = parseSOLTransfer(transaction, expectedRecipient);
140
- if (!transferDetails) {
141
- return {
142
- valid: false,
143
- confirmed: true,
144
- signature,
145
- error: "No valid SOL transfer to recipient found"
146
- };
147
- }
148
- if (transferDetails.amount < expectedAmount) {
149
- return {
150
- valid: false,
151
- confirmed: true,
152
- signature,
153
- from: transferDetails.from,
154
- to: transferDetails.to,
155
- amount: transferDetails.amount,
156
- error: "Insufficient payment amount"
157
- };
158
- }
159
- return {
160
- valid: true,
161
- confirmed: true,
162
- signature,
163
- from: transferDetails.from,
164
- to: transferDetails.to,
165
- amount: transferDetails.amount,
166
- blockTime: transaction.blockTime ?? void 0,
167
- slot: transaction.slot
168
- };
169
- } catch (error) {
170
- return {
171
- valid: false,
172
- confirmed: false,
173
- signature,
174
- error: "Verification failed"
175
- };
176
- }
177
- }
178
-
179
- // src/x402/config.ts
180
- var WALLET_REGEX2 = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
181
- function sanitizeDisplayString(str, maxLength = 200) {
182
- if (!str || typeof str !== "string") return "";
183
- return str.slice(0, maxLength).replace(/[<>"'&]/g, "");
184
- }
185
- function isValidUrl(url) {
186
- try {
187
- const parsed = new URL(url);
188
- return parsed.protocol === "http:" || parsed.protocol === "https:";
189
- } catch {
190
- return false;
191
- }
192
- }
193
- function buildPaymentRequirement(params) {
194
- if (!WALLET_REGEX2.test(params.creatorWallet)) {
195
- throw new Error("Invalid creator wallet address");
196
- }
197
- if (params.priceInLamports <= 0n) {
198
- throw new Error("Price must be positive");
199
- }
200
- if (!isValidUrl(params.resourceUrl)) {
201
- throw new Error("Invalid resource URL");
202
- }
203
- if (params.network !== "devnet" && params.network !== "mainnet-beta") {
204
- throw new Error("Invalid network");
205
- }
206
- const timeout = params.maxTimeoutSeconds ?? 300;
207
- if (timeout < 60 || timeout > 3600) {
208
- throw new Error("Timeout must be between 60 and 3600 seconds");
209
- }
210
- const x402Network = toX402Network(params.network);
211
- const safeTitle = sanitizeDisplayString(params.articleTitle, 200);
212
- const safeArticleId = sanitizeDisplayString(params.articleId, 128);
213
- return {
214
- scheme: "exact",
215
- network: x402Network,
216
- maxAmountRequired: params.priceInLamports.toString(),
217
- resource: params.resourceUrl,
218
- description: `Unlock: ${safeTitle}`,
219
- mimeType: "text/html",
220
- payTo: params.creatorWallet,
221
- maxTimeoutSeconds: timeout,
222
- asset: "native",
223
- extra: {
224
- name: safeTitle,
225
- articleId: safeArticleId
226
- }
227
- };
228
- }
229
- function encodePaymentRequired(requirement) {
230
- return Buffer.from(JSON.stringify(requirement)).toString("base64");
231
- }
232
- function decodePaymentRequired(encoded) {
233
- if (!encoded || typeof encoded !== "string") {
234
- throw new Error("Invalid encoded requirement");
235
- }
236
- if (encoded.length > 1e4) {
237
- throw new Error("Encoded requirement too large");
238
- }
239
- try {
240
- const decoded = Buffer.from(encoded, "base64").toString("utf-8");
241
- return JSON.parse(decoded);
242
- } catch {
243
- throw new Error("Failed to decode payment requirement");
244
- }
245
- }
246
- var X402_HEADERS = {
247
- PAYMENT_REQUIRED: "X-Payment-Required",
248
- PAYMENT: "X-Payment",
249
- PAYMENT_RESPONSE: "X-Payment-Response"
250
- };
251
- function create402ResponseBody(requirement) {
252
- const assetStr = typeof requirement.asset === "string" ? requirement.asset : requirement.asset.mint;
253
- return {
254
- error: "Payment Required",
255
- message: requirement.description,
256
- price: {
257
- amount: requirement.maxAmountRequired,
258
- asset: assetStr,
259
- network: requirement.network
260
- }
261
- };
262
- }
263
- function create402Headers(requirement) {
264
- const encoded = encodePaymentRequired(requirement);
265
- return {
266
- "Content-Type": "application/json",
267
- [X402_HEADERS.PAYMENT_REQUIRED]: encoded,
268
- "Access-Control-Expose-Headers": X402_HEADERS.PAYMENT_REQUIRED
269
- };
270
- }
271
-
272
- // src/x402/verification.ts
273
- var SIGNATURE_REGEX2 = /^[1-9A-HJ-NP-Za-km-z]{87,88}$/;
274
- async function verifyX402Payment(payload, requirement, clientConfig) {
275
- if (!payload || typeof payload !== "object") {
276
- return { valid: false, invalidReason: "Invalid payload" };
277
- }
278
- const signature = payload.payload?.signature;
279
- if (!signature || typeof signature !== "string") {
280
- return { valid: false, invalidReason: "Missing transaction signature" };
281
- }
282
- if (!SIGNATURE_REGEX2.test(signature)) {
283
- return { valid: false, invalidReason: "Invalid signature format" };
284
- }
285
- if (payload.x402Version !== 1) {
286
- return { valid: false, invalidReason: "Unsupported x402 version" };
287
- }
288
- if (payload.scheme !== "exact") {
289
- return { valid: false, invalidReason: "Unsupported payment scheme" };
290
- }
291
- if (payload.network !== requirement.network) {
292
- return {
293
- valid: false,
294
- invalidReason: `Network mismatch: expected ${requirement.network}`
295
- };
296
- }
297
- const walletRegex = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
298
- if (!walletRegex.test(requirement.payTo)) {
299
- return { valid: false, invalidReason: "Invalid recipient configuration" };
300
- }
301
- let expectedAmount;
302
- try {
303
- expectedAmount = BigInt(requirement.maxAmountRequired);
304
- if (expectedAmount <= 0n) {
305
- return { valid: false, invalidReason: "Invalid payment amount" };
306
- }
307
- } catch {
308
- return { valid: false, invalidReason: "Invalid payment amount format" };
309
- }
310
- const verification = await verifyPayment({
311
- signature,
312
- expectedRecipient: requirement.payTo,
313
- expectedAmount,
314
- maxAgeSeconds: requirement.maxTimeoutSeconds,
315
- clientConfig
316
- });
317
- if (!verification.valid) {
318
- return {
319
- valid: false,
320
- invalidReason: verification.error || "Transaction verification failed"
321
- };
322
- }
323
- return {
324
- valid: true,
325
- settled: verification.confirmed,
326
- from: verification.from,
327
- transaction: {
328
- signature: verification.signature,
329
- blockTime: verification.blockTime,
330
- slot: verification.slot
331
- }
332
- };
333
- }
334
- function parsePaymentHeader(header) {
335
- if (!header || typeof header !== "string") {
336
- return null;
337
- }
338
- if (header.length > 1e4) {
339
- return null;
340
- }
341
- try {
342
- const decoded = Buffer.from(header, "base64").toString("utf-8");
343
- const parsed = JSON.parse(decoded);
344
- if (!parsed || typeof parsed !== "object") {
345
- return null;
346
- }
347
- return parsed;
348
- } catch {
349
- return null;
350
- }
351
- }
352
- function encodePaymentRequirement(requirement) {
353
- return Buffer.from(JSON.stringify(requirement)).toString("base64");
354
- }
355
- function encodePaymentResponse(response) {
356
- return Buffer.from(JSON.stringify(response)).toString("base64");
357
- }
358
- function create402Response(requirement, body) {
359
- const headers = new Headers({
360
- "Content-Type": "application/json",
361
- "X-Payment-Required": encodePaymentRequirement(requirement)
362
- });
363
- const responseBody = body || {
364
- error: "Payment Required",
365
- message: "This resource requires payment to access",
366
- x402Version: 1,
367
- accepts: [requirement]
368
- };
369
- return new Response(JSON.stringify(responseBody), {
370
- status: 402,
371
- headers
372
- });
373
- }
374
-
375
- exports.X402_HEADERS = X402_HEADERS;
376
- exports.buildPaymentRequirement = buildPaymentRequirement;
377
- exports.create402Headers = create402Headers;
378
- exports.create402Response = create402Response;
379
- exports.create402ResponseBody = create402ResponseBody;
380
- exports.decodePaymentRequired = decodePaymentRequired;
381
- exports.encodePaymentRequired = encodePaymentRequired;
382
- exports.encodePaymentRequirement = encodePaymentRequirement;
383
- exports.encodePaymentResponse = encodePaymentResponse;
384
- exports.parsePaymentHeader = parsePaymentHeader;
385
- exports.verifyX402Payment = verifyX402Payment;
386
- //# sourceMappingURL=index.cjs.map
387
- //# sourceMappingURL=index.cjs.map
@@ -1 +0,0 @@
1
- {"version":3,"sources":["../../src/solana/client.ts","../../src/solana/verification.ts","../../src/x402/config.ts","../../src/x402/verification.ts"],"names":["clusterApiUrl","Connection","WALLET_REGEX","SIGNATURE_REGEX"],"mappings":";;;;;AA6BA,IAAI,gBAAA,GAAsC,IAAA;AAC1C,IAAI,aAAA,GAAsC,IAAA;AAE1C,IAAI,qBAAA,GAAwB,KAAA;AAQ5B,SAAS,YAAY,MAAA,EAAoC;AACrD,EAAA,MAAM,EAAE,OAAA,EAAS,MAAA,EAAQ,WAAA,EAAY,GAAI,MAAA;AAEzC,EAAA,IAAI,MAAA,EAAQ;AAER,IAAA,IAAI,MAAA,CAAO,SAAS,UAAU,CAAA,IAAK,eAAe,CAAC,MAAA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AAK,SAAA,CAAU,WAAW,CAAC,CAAA,CAAE,SAAS,QAAQ,CAAA;AACrE;AAKO,SAAS,sBAAsB,QAAA,EAAwC;AAC1E,EAAA,OAAO,MAAA,CAAO,KAAK,IAAA,CAAK,SAAA,CAAU,QAAQ,CAAC,CAAA,CAAE,SAAS,QAAQ,CAAA;AAClE;AAUO,SAAS,iBAAA,CACZ,aACA,IAAA,EACQ;AACR,EAAA,MAAM,OAAA,GAAU,IAAI,OAAA,CAAQ;AAAA,IACxB,cAAA,EAAgB,kBAAA;AAAA,IAChB,oBAAA,EAAsB,yBAAyB,WAAW;AAAA,GAC7D,CAAA;AAED,EAAA,MAAM,eAAe,IAAA,IAAQ;AAAA,IACzB,KAAA,EAAO,kBAAA;AAAA,IACP,OAAA,EAAS,0CAAA;AAAA,IACT,WAAA,EAAa,CAAA;AAAA,IACb,OAAA,EAAS,CAAC,WAAW;AAAA,GACzB;AAEA,EAAA,OAAO,IAAI,QAAA,CAAS,IAAA,CAAK,SAAA,CAAU,YAAY,CAAA,EAAG;AAAA,IAC9C,MAAA,EAAQ,GAAA;AAAA,IACR;AAAA,GACH,CAAA;AACL","file":"index.cjs","sourcesContent":["// Solana RPC client with multi-provider support and optional fallback\nimport { Connection, clusterApiUrl, type Cluster } from '@solana/web3.js';\nimport type { SolanaNetwork } from '../types';\n\n/** Configuration for Solana client */\nexport interface SolanaClientConfig {\n /** Network to connect to */\n network: SolanaNetwork;\n /** Custom RPC URL (optional) */\n rpcUrl?: string;\n /** Tatum.io API key for RPC (optional) */\n tatumApiKey?: string;\n /** Enable RPC fallback on errors (default: false) */\n enableFallback?: boolean;\n /** Fallback RPC URLs to try on primary failure (optional) */\n fallbackRpcUrls?: string[];\n}\n\n/** RPC connection with fallback support */\nexport interface RpcConnectionWithFallback {\n /** Primary connection */\n connection: Connection;\n /** Fallback connections (if configured) */\n fallbacks: Connection[];\n /** Whether fallback is enabled */\n fallbackEnabled: boolean;\n}\n\n// Singleton state\nlet cachedConnection: Connection | null = null;\nlet cachedNetwork: SolanaNetwork | null = null;\nlet cachedFallbacks: Connection[] = [];\nlet cachedFallbackEnabled = false;\n\n/**\n * Build RPC URL based on configuration priority:\n * 1. Custom RPC URL\n * 2. Tatum.io with API key\n * 3. Public RPC (rate limited)\n */\nfunction buildRpcUrl(config: SolanaClientConfig): string {\n const { network, rpcUrl, tatumApiKey } = config;\n\n if (rpcUrl) {\n // If Tatum URL without key, append key if available\n if (rpcUrl.includes('tatum.io') && tatumApiKey && !rpcUrl.includes(tatumApiKey)) {\n return rpcUrl.endsWith('/') ? `${rpcUrl}${tatumApiKey}` : `${rpcUrl}/${tatumApiKey}`;\n }\n return rpcUrl;\n }\n\n if (tatumApiKey) {\n const baseUrl = network === 'mainnet-beta'\n ? 'https://solana-mainnet.gateway.tatum.io'\n : 'https://solana-devnet.gateway.tatum.io';\n return `${baseUrl}/${tatumApiKey}`;\n }\n\n // Fallback to public RPC\n return clusterApiUrl(network as Cluster);\n}\n\n/**\n * Create a connection with specified options\n */\nfunction createConnection(rpcUrl: string): Connection {\n return new Connection(rpcUrl, {\n commitment: 'confirmed',\n confirmTransactionInitialTimeout: 60000,\n });\n}\n\n/**\n * Get or create a Solana connection\n * Uses singleton pattern with network-aware caching\n */\nexport function getConnection(config: SolanaClientConfig): Connection {\n const { network } = config;\n\n // Return cached if same network\n if (cachedConnection && cachedNetwork === network) {\n return cachedConnection;\n }\n\n const rpcUrl = buildRpcUrl(config);\n cachedConnection = createConnection(rpcUrl);\n cachedNetwork = network;\n\n // Setup fallbacks if enabled\n cachedFallbackEnabled = config.enableFallback ?? false;\n cachedFallbacks = [];\n\n if (cachedFallbackEnabled && config.fallbackRpcUrls?.length) {\n cachedFallbacks = config.fallbackRpcUrls.map(createConnection);\n }\n\n return cachedConnection;\n}\n\n/**\n * Get connection with fallback support\n * Returns both primary and fallback connections for manual failover\n */\nexport function getConnectionWithFallback(config: SolanaClientConfig): RpcConnectionWithFallback {\n const connection = getConnection(config);\n return {\n connection,\n fallbacks: cachedFallbacks,\n fallbackEnabled: cachedFallbackEnabled,\n };\n}\n\n/**\n * Execute an RPC call with automatic fallback on failure\n * Only used when fallback is enabled in config\n * \n * @example\n * ```typescript\n * const balance = await withFallback(\n * config,\n * (conn) => conn.getBalance(publicKey)\n * );\n * ```\n */\nexport async function withFallback<T>(\n config: SolanaClientConfig,\n operation: (connection: Connection) => Promise<T>\n): Promise<T> {\n const { connection, fallbacks, fallbackEnabled } = getConnectionWithFallback(config);\n\n try {\n return await operation(connection);\n } catch (error) {\n if (!fallbackEnabled || fallbacks.length === 0) {\n throw error;\n }\n\n // Check if error is retryable (network errors, rate limits)\n if (!isRetryableError(error)) {\n throw error;\n }\n\n // Try fallbacks in order\n for (let i = 0; i < fallbacks.length; i++) {\n try {\n return await operation(fallbacks[i]);\n } catch (fallbackError) {\n // If last fallback, throw the error\n if (i === fallbacks.length - 1) {\n throw fallbackError;\n }\n // Otherwise continue to next fallback\n }\n }\n\n // Should never reach here, but TypeScript needs this\n throw error;\n }\n}\n\n/**\n * Check if an error is retryable (network issues, rate limits)\n */\nfunction isRetryableError(error: unknown): boolean {\n if (error instanceof Error) {\n const message = error.message.toLowerCase();\n return (\n message.includes('429') ||\n message.includes('503') ||\n message.includes('502') ||\n message.includes('timeout') ||\n message.includes('econnrefused') ||\n message.includes('enotfound') ||\n message.includes('rate limit')\n );\n }\n return false;\n}\n\n/**\n * Reset the cached connection\n * Useful for testing or network switching\n */\nexport function resetConnection(): void {\n cachedConnection = null;\n cachedNetwork = null;\n}\n\n/**\n * Check if network is mainnet\n */\nexport function isMainnet(network: SolanaNetwork): boolean {\n return network === 'mainnet-beta';\n}\n\n/**\n * Convert Solana network to x402 network identifier\n */\nexport function toX402Network(network: SolanaNetwork): 'solana-devnet' | 'solana-mainnet' {\n return network === 'mainnet-beta' ? 'solana-mainnet' : 'solana-devnet';\n}\n","// Transaction verification for SOL payments\n// SECURITY: On-chain verification, signature validation, replay protection\nimport { PublicKey, LAMPORTS_PER_SOL, type ParsedTransactionWithMeta } from '@solana/web3.js';\nimport { getConnection, type SolanaClientConfig } from './client';\nimport type { SignatureStore } from '../store';\n\n/** Result of transaction verification */\nexport interface TransactionVerificationResult {\n /** Whether the transaction is valid for the payment */\n valid: boolean;\n /** Whether the transaction is confirmed on-chain */\n confirmed: boolean;\n /** Transaction signature */\n signature: string;\n /** Sender wallet address */\n from?: string;\n /** Recipient wallet address */\n to?: string;\n /** Amount transferred in lamports */\n amount?: bigint;\n /** Block time (Unix timestamp) */\n blockTime?: number;\n /** Slot number */\n slot?: number;\n /** Error message if verification failed */\n error?: string;\n}\n\n/** Parameters for verifying a payment */\nexport interface VerifyPaymentParams {\n /** Transaction signature to verify */\n signature: string;\n /** Expected recipient wallet address */\n expectedRecipient: string;\n /** Expected amount in lamports */\n expectedAmount: bigint;\n /** Maximum age of transaction in seconds (default: 300) */\n maxAgeSeconds?: number;\n /** Optional signature store for anti-replay protection */\n signatureStore?: SignatureStore;\n /** Solana client configuration */\n clientConfig: SolanaClientConfig;\n}\n\n// Signature validation regex (base58, 87-88 chars)\nconst SIGNATURE_REGEX = /^[1-9A-HJ-NP-Za-km-z]{87,88}$/;\n\n// Wallet address validation regex\nconst WALLET_REGEX = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;\n\n/**\n * Validate transaction signature format\n * SECURITY: Prevents malformed signatures from reaching RPC\n */\nfunction isValidSignature(signature: string): boolean {\n if (!signature || typeof signature !== 'string') return false;\n return SIGNATURE_REGEX.test(signature);\n}\n\n/**\n * Validate wallet address format\n * SECURITY: Ensures valid base58 address\n */\nfunction isValidWalletAddress(address: string): boolean {\n if (!address || typeof address !== 'string') return false;\n return WALLET_REGEX.test(address);\n}\n\n/**\n * Parse SOL transfer details from a transaction\n */\nfunction parseSOLTransfer(\n transaction: ParsedTransactionWithMeta,\n expectedRecipient: string\n): { from: string; to: string; amount: bigint } | null {\n const instructions = transaction.transaction.message.instructions;\n\n // Check main instructions\n for (const ix of instructions) {\n if ('parsed' in ix && ix.program === 'system') {\n const parsed = ix.parsed as {\n type: string;\n info: { source: string; destination: string; lamports: number }\n };\n\n if (parsed.type === 'transfer' && parsed.info.destination === expectedRecipient) {\n return {\n from: parsed.info.source,\n to: parsed.info.destination,\n amount: BigInt(parsed.info.lamports),\n };\n }\n }\n }\n\n // Check inner instructions\n if (transaction.meta?.innerInstructions) {\n for (const inner of transaction.meta.innerInstructions) {\n for (const ix of inner.instructions) {\n if ('parsed' in ix && ix.program === 'system') {\n const parsed = ix.parsed as {\n type: string;\n info: { source: string; destination: string; lamports: number }\n };\n\n if (parsed.type === 'transfer' && parsed.info.destination === expectedRecipient) {\n return {\n from: parsed.info.source,\n to: parsed.info.destination,\n amount: BigInt(parsed.info.lamports),\n };\n }\n }\n }\n }\n }\n\n return null;\n}\n\n/**\n * Verify a SOL transfer transaction\n * SECURITY: Full on-chain verification with amount/recipient/age checks\n */\nexport async function verifyPayment(\n params: VerifyPaymentParams\n): Promise<TransactionVerificationResult> {\n const {\n signature,\n expectedRecipient,\n expectedAmount,\n maxAgeSeconds = 300,\n clientConfig,\n signatureStore,\n } = params;\n\n // SECURITY: Validate signature format before RPC call\n if (!isValidSignature(signature)) {\n return { valid: false, confirmed: false, signature, error: 'Invalid signature format' };\n }\n\n // SECURITY: Validate recipient address format\n if (!isValidWalletAddress(expectedRecipient)) {\n return { valid: false, confirmed: false, signature, error: 'Invalid recipient address' };\n }\n\n // SECURITY: Validate expected amount is positive\n if (expectedAmount <= 0n) {\n return { valid: false, confirmed: false, signature, error: 'Invalid expected amount' };\n }\n\n // SECURITY: Enforce reasonable max age (prevent replay with very old transactions)\n // Check local signature store (if provided)\n if (signatureStore) {\n const isUsed = await signatureStore.hasBeenUsed(signature);\n if (isUsed) {\n return { valid: false, confirmed: true, signature, error: 'Signature already used' };\n }\n }\n\n const effectiveMaxAge = Math.min(Math.max(maxAgeSeconds, 60), 3600); // 1 min to 1 hour\n\n const connection = getConnection(clientConfig);\n\n try {\n const transaction = await connection.getParsedTransaction(signature, {\n commitment: 'confirmed',\n maxSupportedTransactionVersion: 0,\n });\n\n if (!transaction) {\n return { valid: false, confirmed: false, signature, error: 'Transaction not found' };\n }\n\n // Check for transaction errors\n if (transaction.meta?.err) {\n return {\n valid: false,\n confirmed: true,\n signature,\n error: 'Transaction failed on-chain',\n };\n }\n\n // SECURITY: Validate transaction age (replay protection)\n if (transaction.blockTime) {\n const now = Math.floor(Date.now() / 1000);\n if (now - transaction.blockTime > effectiveMaxAge) {\n return { valid: false, confirmed: true, signature, error: 'Transaction too old' };\n }\n // Also reject future-dated transactions (clock skew attack)\n if (transaction.blockTime > now + 60) {\n return { valid: false, confirmed: true, signature, error: 'Invalid transaction time' };\n }\n }\n\n // Parse transfer details\n const transferDetails = parseSOLTransfer(transaction, expectedRecipient);\n\n if (!transferDetails) {\n return {\n valid: false,\n confirmed: true,\n signature,\n error: 'No valid SOL transfer to recipient found',\n };\n }\n\n // SECURITY: Validate amount (must meet or exceed expected)\n if (transferDetails.amount < expectedAmount) {\n return {\n valid: false,\n confirmed: true,\n signature,\n from: transferDetails.from,\n to: transferDetails.to,\n amount: transferDetails.amount,\n error: 'Insufficient payment amount',\n };\n }\n\n return {\n valid: true,\n confirmed: true,\n signature,\n from: transferDetails.from,\n to: transferDetails.to,\n amount: transferDetails.amount,\n blockTime: transaction.blockTime ?? undefined,\n slot: transaction.slot,\n };\n } catch (error) {\n // SECURITY: Don't expose internal error details\n return {\n valid: false,\n confirmed: false,\n signature,\n error: 'Verification failed',\n };\n }\n}\n\n/**\n * Wait for transaction confirmation\n */\nexport async function waitForConfirmation(\n signature: string,\n clientConfig: SolanaClientConfig\n): Promise<{ confirmed: boolean; slot?: number; error?: string }> {\n if (!isValidSignature(signature)) {\n return { confirmed: false, error: 'Invalid signature format' };\n }\n\n const connection = getConnection(clientConfig);\n\n try {\n const confirmation = await connection.confirmTransaction(signature, 'confirmed');\n\n if (confirmation.value.err) {\n return { confirmed: false, error: 'Transaction failed' };\n }\n\n return { confirmed: true, slot: confirmation.context?.slot };\n } catch {\n return { confirmed: false, error: 'Confirmation timeout' };\n }\n}\n\n/**\n * Get recent transactions for a wallet\n */\nexport async function getWalletTransactions(\n walletAddress: string,\n clientConfig: SolanaClientConfig,\n limit: number = 20\n): Promise<Array<{ signature: string; blockTime?: number; slot: number }>> {\n if (!isValidWalletAddress(walletAddress)) {\n return [];\n }\n\n // SECURITY: Cap limit to prevent abuse\n const safeLimit = Math.min(Math.max(limit, 1), 100);\n\n const connection = getConnection(clientConfig);\n\n try {\n const pubkey = new PublicKey(walletAddress);\n const signatures = await connection.getSignaturesForAddress(pubkey, { limit: safeLimit });\n return signatures.map((sig) => ({\n signature: sig.signature,\n blockTime: sig.blockTime ?? undefined,\n slot: sig.slot,\n }));\n } catch {\n return [];\n }\n}\n\n/**\n * Convert lamports to SOL\n */\nexport function lamportsToSol(lamports: bigint | number): number {\n return Number(lamports) / LAMPORTS_PER_SOL;\n}\n\n/**\n * Convert SOL to lamports\n */\nexport function solToLamports(sol: number): bigint {\n if (!Number.isFinite(sol) || sol < 0) {\n throw new Error('Invalid SOL amount');\n }\n return BigInt(Math.floor(sol * LAMPORTS_PER_SOL));\n}\n","// x402 payment configuration and helpers\n// SECURITY: Input sanitization for payment requirements\nimport type { PaymentRequirement, X402Network, SolanaNetwork } from '../types';\nimport { toX402Network } from '../solana';\n\n// Wallet address validation regex\nconst WALLET_REGEX = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;\n\n/** Parameters for building a payment requirement */\nexport interface BuildPaymentParams {\n /** Unique article identifier */\n articleId: string;\n /** Article title for display */\n articleTitle: string;\n /** Price in lamports */\n priceInLamports: bigint;\n /** Creator wallet address */\n creatorWallet: string;\n /** Full URL of the protected resource */\n resourceUrl: string;\n /** Solana network */\n network: SolanaNetwork;\n /** Max time to complete payment (default: 300s) */\n maxTimeoutSeconds?: number;\n}\n\n/**\n * Sanitize string for safe display (prevent XSS in UIs)\n */\nfunction sanitizeDisplayString(str: string, maxLength: number = 200): string {\n if (!str || typeof str !== 'string') return '';\n return str.slice(0, maxLength).replace(/[<>\"'&]/g, '');\n}\n\n/**\n * Validate URL format\n */\nfunction isValidUrl(url: string): boolean {\n try {\n const parsed = new URL(url);\n return parsed.protocol === 'http:' || parsed.protocol === 'https:';\n } catch {\n return false;\n }\n}\n\n/**\n * Build a payment requirement for an article\n * SECURITY: Validates all inputs before building requirement\n */\nexport function buildPaymentRequirement(params: BuildPaymentParams): PaymentRequirement {\n // Validate wallet address\n if (!WALLET_REGEX.test(params.creatorWallet)) {\n throw new Error('Invalid creator wallet address');\n }\n\n // Validate price\n if (params.priceInLamports <= 0n) {\n throw new Error('Price must be positive');\n }\n\n // Validate URL\n if (!isValidUrl(params.resourceUrl)) {\n throw new Error('Invalid resource URL');\n }\n\n // Validate network\n if (params.network !== 'devnet' && params.network !== 'mainnet-beta') {\n throw new Error('Invalid network');\n }\n\n // Validate timeout\n const timeout = params.maxTimeoutSeconds ?? 300;\n if (timeout < 60 || timeout > 3600) {\n throw new Error('Timeout must be between 60 and 3600 seconds');\n }\n\n const x402Network = toX402Network(params.network);\n\n // Sanitize display strings\n const safeTitle = sanitizeDisplayString(params.articleTitle, 200);\n const safeArticleId = sanitizeDisplayString(params.articleId, 128);\n\n return {\n scheme: 'exact',\n network: x402Network,\n maxAmountRequired: params.priceInLamports.toString(),\n resource: params.resourceUrl,\n description: `Unlock: ${safeTitle}`,\n mimeType: 'text/html',\n payTo: params.creatorWallet,\n maxTimeoutSeconds: timeout,\n asset: 'native',\n extra: {\n name: safeTitle,\n articleId: safeArticleId,\n },\n };\n}\n\n/**\n * Encode payment requirement for x402 header\n */\nexport function encodePaymentRequired(requirement: PaymentRequirement): string {\n return Buffer.from(JSON.stringify(requirement)).toString('base64');\n}\n\n/**\n * Decode payment requirement from x402 header\n * SECURITY: Safe parsing with size limit\n */\nexport function decodePaymentRequired(encoded: string): PaymentRequirement {\n if (!encoded || typeof encoded !== 'string') {\n throw new Error('Invalid encoded requirement');\n }\n\n // SECURITY: Limit size to prevent DoS\n if (encoded.length > 10000) {\n throw new Error('Encoded requirement too large');\n }\n\n try {\n const decoded = Buffer.from(encoded, 'base64').toString('utf-8');\n return JSON.parse(decoded);\n } catch {\n throw new Error('Failed to decode payment requirement');\n }\n}\n\n/**\n * x402 response header names\n */\nexport const X402_HEADERS = {\n PAYMENT_REQUIRED: 'X-Payment-Required',\n PAYMENT: 'X-Payment',\n PAYMENT_RESPONSE: 'X-Payment-Response',\n} as const;\n\n/**\n * Create 402 Payment Required response body\n */\nexport function create402ResponseBody(requirement: PaymentRequirement): {\n error: string;\n message: string;\n price: { amount: string; asset: string; network: X402Network };\n} {\n // Convert asset to string for JSON serialization\n const assetStr = typeof requirement.asset === 'string'\n ? requirement.asset\n : requirement.asset.mint;\n\n return {\n error: 'Payment Required',\n message: requirement.description,\n price: {\n amount: requirement.maxAmountRequired,\n asset: assetStr,\n network: requirement.network,\n },\n };\n}\n\n/**\n * Create headers for 402 response\n */\nexport function create402Headers(requirement: PaymentRequirement): Record<string, string> {\n const encoded = encodePaymentRequired(requirement);\n return {\n 'Content-Type': 'application/json',\n [X402_HEADERS.PAYMENT_REQUIRED]: encoded,\n 'Access-Control-Expose-Headers': X402_HEADERS.PAYMENT_REQUIRED,\n };\n}\n","// x402 payment verification service\n// SECURITY: Input validation, network verification, on-chain settlement check\nimport { verifyPayment, type SolanaClientConfig } from '../solana';\nimport type { PaymentPayload, PaymentRequirement, VerificationResponse } from '../types';\n\n// Signature validation regex (base58, 87-88 chars)\nconst SIGNATURE_REGEX = /^[1-9A-HJ-NP-Za-km-z]{87,88}$/;\n\n/**\n * Verify a payment payload against requirements\n * SECURITY: Full validation chain - format, network, on-chain\n */\nexport async function verifyX402Payment(\n payload: PaymentPayload,\n requirement: PaymentRequirement,\n clientConfig: SolanaClientConfig\n): Promise<VerificationResponse> {\n // SECURITY: Validate payload exists\n if (!payload || typeof payload !== 'object') {\n return { valid: false, invalidReason: 'Invalid payload' };\n }\n\n // SECURITY: Validate signature exists and format\n const signature = payload.payload?.signature;\n if (!signature || typeof signature !== 'string') {\n return { valid: false, invalidReason: 'Missing transaction signature' };\n }\n if (!SIGNATURE_REGEX.test(signature)) {\n return { valid: false, invalidReason: 'Invalid signature format' };\n }\n\n // SECURITY: Validate x402 version\n if (payload.x402Version !== 1) {\n return { valid: false, invalidReason: 'Unsupported x402 version' };\n }\n\n // SECURITY: Validate scheme\n if (payload.scheme !== 'exact') {\n return { valid: false, invalidReason: 'Unsupported payment scheme' };\n }\n\n // SECURITY: Validate network matches exactly\n if (payload.network !== requirement.network) {\n return {\n valid: false,\n invalidReason: `Network mismatch: expected ${requirement.network}`,\n };\n }\n\n // SECURITY: Validate requirement has valid payTo address\n const walletRegex = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;\n if (!walletRegex.test(requirement.payTo)) {\n return { valid: false, invalidReason: 'Invalid recipient configuration' };\n }\n\n // SECURITY: Validate amount is positive\n let expectedAmount: bigint;\n try {\n expectedAmount = BigInt(requirement.maxAmountRequired);\n if (expectedAmount <= 0n) {\n return { valid: false, invalidReason: 'Invalid payment amount' };\n }\n } catch {\n return { valid: false, invalidReason: 'Invalid payment amount format' };\n }\n\n // Verify on-chain\n const verification = await verifyPayment({\n signature,\n expectedRecipient: requirement.payTo,\n expectedAmount,\n maxAgeSeconds: requirement.maxTimeoutSeconds,\n clientConfig,\n });\n\n if (!verification.valid) {\n return {\n valid: false,\n invalidReason: verification.error || 'Transaction verification failed',\n };\n }\n\n return {\n valid: true,\n settled: verification.confirmed,\n from: verification.from,\n transaction: {\n signature: verification.signature,\n blockTime: verification.blockTime,\n slot: verification.slot,\n },\n };\n}\n\n/**\n * Parse payment payload from X-Payment header (base64 encoded JSON)\n * SECURITY: Safe JSON parsing with try-catch\n * \n * @example\n * const payload = parsePaymentHeader(request.headers.get('x-payment'));\n */\nexport function parsePaymentHeader(header: string): PaymentPayload | null {\n if (!header || typeof header !== 'string') {\n return null;\n }\n\n // SECURITY: Limit header size to prevent DoS\n if (header.length > 10000) {\n return null;\n }\n\n try {\n const decoded = Buffer.from(header, 'base64').toString('utf-8');\n const parsed = JSON.parse(decoded);\n\n // Basic structure validation\n if (!parsed || typeof parsed !== 'object') {\n return null;\n }\n\n return parsed as PaymentPayload;\n } catch {\n return null;\n }\n}\n\n/**\n * Encode PaymentRequirement for X-Payment-Required header\n * Per x402 spec: base64 encoded JSON\n * \n * @example\n * response.headers.set('X-Payment-Required', encodePaymentRequirement(requirement));\n */\nexport function encodePaymentRequirement(requirement: PaymentRequirement): string {\n return Buffer.from(JSON.stringify(requirement)).toString('base64');\n}\n\n/**\n * Encode VerificationResponse for X-Payment-Response header\n */\nexport function encodePaymentResponse(response: VerificationResponse): string {\n return Buffer.from(JSON.stringify(response)).toString('base64');\n}\n\n/**\n * Create a 402 Payment Required response with proper x402 headers\n * \n * @example\n * if (!hasValidPayment) {\n * return create402Response(requirement, { message: 'Payment required' });\n * }\n */\nexport function create402Response(\n requirement: PaymentRequirement,\n body?: object\n): Response {\n const headers = new Headers({\n 'Content-Type': 'application/json',\n 'X-Payment-Required': encodePaymentRequirement(requirement),\n });\n\n const responseBody = body || {\n error: 'Payment Required',\n message: 'This resource requires payment to access',\n x402Version: 1,\n accepts: [requirement],\n };\n\n return new Response(JSON.stringify(responseBody), {\n status: 402,\n headers,\n });\n}\n"]}
@@ -1,96 +0,0 @@
1
- import { S as SolanaNetwork, P as PaymentRequirement, X as X402Network, a as PaymentPayload, b as VerificationResponse } from '../payment-BGp7eMQl.cjs';
2
- import { S as SolanaClientConfig } from '../client-D-dteoJw.cjs';
3
- import '@solana/web3.js';
4
-
5
- /** Parameters for building a payment requirement */
6
- interface BuildPaymentParams {
7
- /** Unique article identifier */
8
- articleId: string;
9
- /** Article title for display */
10
- articleTitle: string;
11
- /** Price in lamports */
12
- priceInLamports: bigint;
13
- /** Creator wallet address */
14
- creatorWallet: string;
15
- /** Full URL of the protected resource */
16
- resourceUrl: string;
17
- /** Solana network */
18
- network: SolanaNetwork;
19
- /** Max time to complete payment (default: 300s) */
20
- maxTimeoutSeconds?: number;
21
- }
22
- /**
23
- * Build a payment requirement for an article
24
- * SECURITY: Validates all inputs before building requirement
25
- */
26
- declare function buildPaymentRequirement(params: BuildPaymentParams): PaymentRequirement;
27
- /**
28
- * Encode payment requirement for x402 header
29
- */
30
- declare function encodePaymentRequired(requirement: PaymentRequirement): string;
31
- /**
32
- * Decode payment requirement from x402 header
33
- * SECURITY: Safe parsing with size limit
34
- */
35
- declare function decodePaymentRequired(encoded: string): PaymentRequirement;
36
- /**
37
- * x402 response header names
38
- */
39
- declare const X402_HEADERS: {
40
- readonly PAYMENT_REQUIRED: "X-Payment-Required";
41
- readonly PAYMENT: "X-Payment";
42
- readonly PAYMENT_RESPONSE: "X-Payment-Response";
43
- };
44
- /**
45
- * Create 402 Payment Required response body
46
- */
47
- declare function create402ResponseBody(requirement: PaymentRequirement): {
48
- error: string;
49
- message: string;
50
- price: {
51
- amount: string;
52
- asset: string;
53
- network: X402Network;
54
- };
55
- };
56
- /**
57
- * Create headers for 402 response
58
- */
59
- declare function create402Headers(requirement: PaymentRequirement): Record<string, string>;
60
-
61
- /**
62
- * Verify a payment payload against requirements
63
- * SECURITY: Full validation chain - format, network, on-chain
64
- */
65
- declare function verifyX402Payment(payload: PaymentPayload, requirement: PaymentRequirement, clientConfig: SolanaClientConfig): Promise<VerificationResponse>;
66
- /**
67
- * Parse payment payload from X-Payment header (base64 encoded JSON)
68
- * SECURITY: Safe JSON parsing with try-catch
69
- *
70
- * @example
71
- * const payload = parsePaymentHeader(request.headers.get('x-payment'));
72
- */
73
- declare function parsePaymentHeader(header: string): PaymentPayload | null;
74
- /**
75
- * Encode PaymentRequirement for X-Payment-Required header
76
- * Per x402 spec: base64 encoded JSON
77
- *
78
- * @example
79
- * response.headers.set('X-Payment-Required', encodePaymentRequirement(requirement));
80
- */
81
- declare function encodePaymentRequirement(requirement: PaymentRequirement): string;
82
- /**
83
- * Encode VerificationResponse for X-Payment-Response header
84
- */
85
- declare function encodePaymentResponse(response: VerificationResponse): string;
86
- /**
87
- * Create a 402 Payment Required response with proper x402 headers
88
- *
89
- * @example
90
- * if (!hasValidPayment) {
91
- * return create402Response(requirement, { message: 'Payment required' });
92
- * }
93
- */
94
- declare function create402Response(requirement: PaymentRequirement, body?: object): Response;
95
-
96
- export { type BuildPaymentParams, X402_HEADERS, buildPaymentRequirement, create402Headers, create402Response, create402ResponseBody, decodePaymentRequired, encodePaymentRequired, encodePaymentRequirement, encodePaymentResponse, parsePaymentHeader, verifyX402Payment };