@alleyboss/micropay-solana-x402-paywall 1.0.0 → 2.0.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 +100 -167
- package/dist/client/index.cjs +99 -0
- package/dist/client/index.cjs.map +1 -0
- package/dist/client/index.d.cts +112 -0
- package/dist/client/index.d.ts +112 -0
- package/dist/client/index.js +95 -0
- package/dist/client/index.js.map +1 -0
- package/dist/client-CSZHI8o8.d.ts +32 -0
- package/dist/client-vRr48m2x.d.cts +32 -0
- package/dist/index.cjs +803 -41
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +11 -3
- package/dist/index.d.ts +11 -3
- package/dist/index.js +783 -42
- package/dist/index.js.map +1 -1
- package/dist/memory-Daxkczti.d.cts +29 -0
- package/dist/memory-Daxkczti.d.ts +29 -0
- package/dist/middleware/index.cjs +261 -0
- package/dist/middleware/index.cjs.map +1 -0
- package/dist/middleware/index.d.cts +90 -0
- package/dist/middleware/index.d.ts +90 -0
- package/dist/middleware/index.js +255 -0
- package/dist/middleware/index.js.map +1 -0
- package/dist/nextjs-BK0pVb9Y.d.ts +78 -0
- package/dist/nextjs-Bm272Jkj.d.cts +78 -0
- package/dist/{client-kfCr7G-P.d.cts → payment-CTxdtqmc.d.cts} +23 -34
- package/dist/{client-kfCr7G-P.d.ts → payment-CTxdtqmc.d.ts} +23 -34
- package/dist/pricing/index.cjs +79 -0
- package/dist/pricing/index.cjs.map +1 -0
- package/dist/pricing/index.d.cts +67 -0
- package/dist/pricing/index.d.ts +67 -0
- package/dist/pricing/index.js +72 -0
- package/dist/pricing/index.js.map +1 -0
- package/dist/session/index.cjs +51 -11
- package/dist/session/index.cjs.map +1 -1
- package/dist/session/index.d.cts +29 -1
- package/dist/session/index.d.ts +29 -1
- package/dist/session/index.js +51 -11
- package/dist/session/index.js.map +1 -1
- package/dist/{index-DptevtnU.d.cts → session-D2IoWAWV.d.cts} +1 -24
- package/dist/{index-DptevtnU.d.ts → session-D2IoWAWV.d.ts} +1 -24
- package/dist/solana/index.cjs +235 -15
- package/dist/solana/index.cjs.map +1 -1
- package/dist/solana/index.d.cts +61 -3
- package/dist/solana/index.d.ts +61 -3
- package/dist/solana/index.js +232 -16
- package/dist/solana/index.js.map +1 -1
- package/dist/store/index.cjs +99 -0
- package/dist/store/index.cjs.map +1 -0
- package/dist/store/index.d.cts +38 -0
- package/dist/store/index.d.ts +38 -0
- package/dist/store/index.js +96 -0
- package/dist/store/index.js.map +1 -0
- package/dist/utils/index.cjs +68 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +30 -0
- package/dist/utils/index.d.ts +30 -0
- package/dist/utils/index.js +65 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/x402/index.cjs +119 -18
- package/dist/x402/index.cjs.map +1 -1
- package/dist/x402/index.d.cts +6 -1
- package/dist/x402/index.d.ts +6 -1
- package/dist/x402/index.js +119 -18
- package/dist/x402/index.js.map +1 -1
- package/package.json +56 -3
|
@@ -0,0 +1,65 @@
|
|
|
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
|
|
@@ -0,0 +1 @@
|
|
|
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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AAK,CAAA;AAAA,MACrB;AAAA,IACJ;AAAA,EACJ;AAEA,EAAA,MAAM,SAAA;AACV;AAKO,SAAS,oBAAoB,KAAA,EAAyB;AACzD,EAAA,IAAI,iBAAiB,KAAA,EAAO;AACxB,IAAA,MAAM,OAAA,GAAU,KAAA,CAAM,OAAA,CAAQ,WAAA,EAAY;AAG1C,IAAA,IAAI,QAAQ,QAAA,CAAS,KAAK,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"]}
|
package/dist/x402/index.cjs
CHANGED
|
@@ -35,6 +35,16 @@ function getConnection(config) {
|
|
|
35
35
|
function toX402Network(network) {
|
|
36
36
|
return network === "mainnet-beta" ? "solana-mainnet" : "solana-devnet";
|
|
37
37
|
}
|
|
38
|
+
var SIGNATURE_REGEX = /^[1-9A-HJ-NP-Za-km-z]{87,88}$/;
|
|
39
|
+
var WALLET_REGEX = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
|
|
40
|
+
function isValidSignature(signature) {
|
|
41
|
+
if (!signature || typeof signature !== "string") return false;
|
|
42
|
+
return SIGNATURE_REGEX.test(signature);
|
|
43
|
+
}
|
|
44
|
+
function isValidWalletAddress(address) {
|
|
45
|
+
if (!address || typeof address !== "string") return false;
|
|
46
|
+
return WALLET_REGEX.test(address);
|
|
47
|
+
}
|
|
38
48
|
function parseSOLTransfer(transaction, expectedRecipient) {
|
|
39
49
|
const instructions = transaction.transaction.message.instructions;
|
|
40
50
|
for (const ix of instructions) {
|
|
@@ -75,6 +85,16 @@ async function verifyPayment(params) {
|
|
|
75
85
|
maxAgeSeconds = 300,
|
|
76
86
|
clientConfig
|
|
77
87
|
} = params;
|
|
88
|
+
if (!isValidSignature(signature)) {
|
|
89
|
+
return { valid: false, confirmed: false, signature, error: "Invalid signature format" };
|
|
90
|
+
}
|
|
91
|
+
if (!isValidWalletAddress(expectedRecipient)) {
|
|
92
|
+
return { valid: false, confirmed: false, signature, error: "Invalid recipient address" };
|
|
93
|
+
}
|
|
94
|
+
if (expectedAmount <= 0n) {
|
|
95
|
+
return { valid: false, confirmed: false, signature, error: "Invalid expected amount" };
|
|
96
|
+
}
|
|
97
|
+
const effectiveMaxAge = Math.min(Math.max(maxAgeSeconds, 60), 3600);
|
|
78
98
|
const connection = getConnection(clientConfig);
|
|
79
99
|
try {
|
|
80
100
|
const transaction = await connection.getParsedTransaction(signature, {
|
|
@@ -89,14 +109,17 @@ async function verifyPayment(params) {
|
|
|
89
109
|
valid: false,
|
|
90
110
|
confirmed: true,
|
|
91
111
|
signature,
|
|
92
|
-
error:
|
|
112
|
+
error: "Transaction failed on-chain"
|
|
93
113
|
};
|
|
94
114
|
}
|
|
95
115
|
if (transaction.blockTime) {
|
|
96
116
|
const now = Math.floor(Date.now() / 1e3);
|
|
97
|
-
if (now - transaction.blockTime >
|
|
117
|
+
if (now - transaction.blockTime > effectiveMaxAge) {
|
|
98
118
|
return { valid: false, confirmed: true, signature, error: "Transaction too old" };
|
|
99
119
|
}
|
|
120
|
+
if (transaction.blockTime > now + 60) {
|
|
121
|
+
return { valid: false, confirmed: true, signature, error: "Invalid transaction time" };
|
|
122
|
+
}
|
|
100
123
|
}
|
|
101
124
|
const transferDetails = parseSOLTransfer(transaction, expectedRecipient);
|
|
102
125
|
if (!transferDetails) {
|
|
@@ -115,7 +138,7 @@ async function verifyPayment(params) {
|
|
|
115
138
|
from: transferDetails.from,
|
|
116
139
|
to: transferDetails.to,
|
|
117
140
|
amount: transferDetails.amount,
|
|
118
|
-
error:
|
|
141
|
+
error: "Insufficient payment amount"
|
|
119
142
|
};
|
|
120
143
|
}
|
|
121
144
|
return {
|
|
@@ -133,26 +156,58 @@ async function verifyPayment(params) {
|
|
|
133
156
|
valid: false,
|
|
134
157
|
confirmed: false,
|
|
135
158
|
signature,
|
|
136
|
-
error:
|
|
159
|
+
error: "Verification failed"
|
|
137
160
|
};
|
|
138
161
|
}
|
|
139
162
|
}
|
|
140
163
|
|
|
141
164
|
// src/x402/config.ts
|
|
165
|
+
var WALLET_REGEX2 = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
|
|
166
|
+
function sanitizeDisplayString(str, maxLength = 200) {
|
|
167
|
+
if (!str || typeof str !== "string") return "";
|
|
168
|
+
return str.slice(0, maxLength).replace(/[<>"'&]/g, "");
|
|
169
|
+
}
|
|
170
|
+
function isValidUrl(url) {
|
|
171
|
+
try {
|
|
172
|
+
const parsed = new URL(url);
|
|
173
|
+
return parsed.protocol === "http:" || parsed.protocol === "https:";
|
|
174
|
+
} catch {
|
|
175
|
+
return false;
|
|
176
|
+
}
|
|
177
|
+
}
|
|
142
178
|
function buildPaymentRequirement(params) {
|
|
179
|
+
if (!WALLET_REGEX2.test(params.creatorWallet)) {
|
|
180
|
+
throw new Error("Invalid creator wallet address");
|
|
181
|
+
}
|
|
182
|
+
if (params.priceInLamports <= 0n) {
|
|
183
|
+
throw new Error("Price must be positive");
|
|
184
|
+
}
|
|
185
|
+
if (!isValidUrl(params.resourceUrl)) {
|
|
186
|
+
throw new Error("Invalid resource URL");
|
|
187
|
+
}
|
|
188
|
+
if (params.network !== "devnet" && params.network !== "mainnet-beta") {
|
|
189
|
+
throw new Error("Invalid network");
|
|
190
|
+
}
|
|
191
|
+
const timeout = params.maxTimeoutSeconds ?? 300;
|
|
192
|
+
if (timeout < 60 || timeout > 3600) {
|
|
193
|
+
throw new Error("Timeout must be between 60 and 3600 seconds");
|
|
194
|
+
}
|
|
143
195
|
const x402Network = toX402Network(params.network);
|
|
196
|
+
const safeTitle = sanitizeDisplayString(params.articleTitle, 200);
|
|
197
|
+
const safeArticleId = sanitizeDisplayString(params.articleId, 128);
|
|
144
198
|
return {
|
|
145
199
|
scheme: "exact",
|
|
146
200
|
network: x402Network,
|
|
147
201
|
maxAmountRequired: params.priceInLamports.toString(),
|
|
148
202
|
resource: params.resourceUrl,
|
|
149
|
-
description: `Unlock: ${
|
|
203
|
+
description: `Unlock: ${safeTitle}`,
|
|
150
204
|
mimeType: "text/html",
|
|
151
205
|
payTo: params.creatorWallet,
|
|
152
|
-
maxTimeoutSeconds:
|
|
206
|
+
maxTimeoutSeconds: timeout,
|
|
153
207
|
asset: "native",
|
|
154
208
|
extra: {
|
|
155
|
-
name:
|
|
209
|
+
name: safeTitle,
|
|
210
|
+
articleId: safeArticleId
|
|
156
211
|
}
|
|
157
212
|
};
|
|
158
213
|
}
|
|
@@ -160,7 +215,18 @@ function encodePaymentRequired(requirement) {
|
|
|
160
215
|
return Buffer.from(JSON.stringify(requirement)).toString("base64");
|
|
161
216
|
}
|
|
162
217
|
function decodePaymentRequired(encoded) {
|
|
163
|
-
|
|
218
|
+
if (!encoded || typeof encoded !== "string") {
|
|
219
|
+
throw new Error("Invalid encoded requirement");
|
|
220
|
+
}
|
|
221
|
+
if (encoded.length > 1e4) {
|
|
222
|
+
throw new Error("Encoded requirement too large");
|
|
223
|
+
}
|
|
224
|
+
try {
|
|
225
|
+
const decoded = Buffer.from(encoded, "base64").toString("utf-8");
|
|
226
|
+
return JSON.parse(decoded);
|
|
227
|
+
} catch {
|
|
228
|
+
throw new Error("Failed to decode payment requirement");
|
|
229
|
+
}
|
|
164
230
|
}
|
|
165
231
|
var X402_HEADERS = {
|
|
166
232
|
PAYMENT_REQUIRED: "X-Payment-Required",
|
|
@@ -168,12 +234,13 @@ var X402_HEADERS = {
|
|
|
168
234
|
PAYMENT_RESPONSE: "X-Payment-Response"
|
|
169
235
|
};
|
|
170
236
|
function create402ResponseBody(requirement) {
|
|
237
|
+
const assetStr = typeof requirement.asset === "string" ? requirement.asset : requirement.asset.mint;
|
|
171
238
|
return {
|
|
172
239
|
error: "Payment Required",
|
|
173
240
|
message: requirement.description,
|
|
174
241
|
price: {
|
|
175
242
|
amount: requirement.maxAmountRequired,
|
|
176
|
-
asset:
|
|
243
|
+
asset: assetStr,
|
|
177
244
|
network: requirement.network
|
|
178
245
|
}
|
|
179
246
|
};
|
|
@@ -188,23 +255,47 @@ function create402Headers(requirement) {
|
|
|
188
255
|
}
|
|
189
256
|
|
|
190
257
|
// src/x402/verification.ts
|
|
258
|
+
var SIGNATURE_REGEX2 = /^[1-9A-HJ-NP-Za-km-z]{87,88}$/;
|
|
191
259
|
async function verifyX402Payment(payload, requirement, clientConfig) {
|
|
192
|
-
if (!payload
|
|
193
|
-
return {
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
260
|
+
if (!payload || typeof payload !== "object") {
|
|
261
|
+
return { valid: false, invalidReason: "Invalid payload" };
|
|
262
|
+
}
|
|
263
|
+
const signature = payload.payload?.signature;
|
|
264
|
+
if (!signature || typeof signature !== "string") {
|
|
265
|
+
return { valid: false, invalidReason: "Missing transaction signature" };
|
|
266
|
+
}
|
|
267
|
+
if (!SIGNATURE_REGEX2.test(signature)) {
|
|
268
|
+
return { valid: false, invalidReason: "Invalid signature format" };
|
|
269
|
+
}
|
|
270
|
+
if (payload.x402Version !== 1) {
|
|
271
|
+
return { valid: false, invalidReason: "Unsupported x402 version" };
|
|
272
|
+
}
|
|
273
|
+
if (payload.scheme !== "exact") {
|
|
274
|
+
return { valid: false, invalidReason: "Unsupported payment scheme" };
|
|
197
275
|
}
|
|
198
276
|
if (payload.network !== requirement.network) {
|
|
199
277
|
return {
|
|
200
278
|
valid: false,
|
|
201
|
-
invalidReason: `Network mismatch: expected ${requirement.network}
|
|
279
|
+
invalidReason: `Network mismatch: expected ${requirement.network}`
|
|
202
280
|
};
|
|
203
281
|
}
|
|
282
|
+
const walletRegex = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
|
|
283
|
+
if (!walletRegex.test(requirement.payTo)) {
|
|
284
|
+
return { valid: false, invalidReason: "Invalid recipient configuration" };
|
|
285
|
+
}
|
|
286
|
+
let expectedAmount;
|
|
287
|
+
try {
|
|
288
|
+
expectedAmount = BigInt(requirement.maxAmountRequired);
|
|
289
|
+
if (expectedAmount <= 0n) {
|
|
290
|
+
return { valid: false, invalidReason: "Invalid payment amount" };
|
|
291
|
+
}
|
|
292
|
+
} catch {
|
|
293
|
+
return { valid: false, invalidReason: "Invalid payment amount format" };
|
|
294
|
+
}
|
|
204
295
|
const verification = await verifyPayment({
|
|
205
|
-
signature
|
|
296
|
+
signature,
|
|
206
297
|
expectedRecipient: requirement.payTo,
|
|
207
|
-
expectedAmount
|
|
298
|
+
expectedAmount,
|
|
208
299
|
maxAgeSeconds: requirement.maxTimeoutSeconds,
|
|
209
300
|
clientConfig
|
|
210
301
|
});
|
|
@@ -225,9 +316,19 @@ async function verifyX402Payment(payload, requirement, clientConfig) {
|
|
|
225
316
|
};
|
|
226
317
|
}
|
|
227
318
|
function parsePaymentHeader(header) {
|
|
319
|
+
if (!header || typeof header !== "string") {
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
if (header.length > 1e4) {
|
|
323
|
+
return null;
|
|
324
|
+
}
|
|
228
325
|
try {
|
|
229
326
|
const decoded = Buffer.from(header, "base64").toString("utf-8");
|
|
230
|
-
|
|
327
|
+
const parsed = JSON.parse(decoded);
|
|
328
|
+
if (!parsed || typeof parsed !== "object") {
|
|
329
|
+
return null;
|
|
330
|
+
}
|
|
331
|
+
return parsed;
|
|
231
332
|
} catch {
|
|
232
333
|
return null;
|
|
233
334
|
}
|
package/dist/x402/index.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../../src/solana/client.ts","../../src/solana/verification.ts","../../src/x402/config.ts","../../src/x402/verification.ts"],"names":["clusterApiUrl","Connection"],"mappings":";;;;;AAeA,IAAI,gBAAA,GAAsC,IAAA;AAC1C,IAAI,aAAA,GAAsC,IAAA;AAQ1C,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,CAAO,QAAA,CAAS,WAAW,CAAA,EAAG;AAC7E,MAAA,OAAO,MAAA,CAAO,QAAA,CAAS,GAAG,CAAA,GAAI,CAAA,EAAG,MAAM,CAAA,EAAG,WAAW,CAAA,CAAA,GAAK,CAAA,EAAG,MAAM,CAAA,CAAA,EAAI,WAAW,CAAA,CAAA;AAAA,IACtF;AACA,IAAA,OAAO,MAAA;AAAA,EACX;AAEA,EAAA,IAAI,WAAA,EAAa;AACb,IAAA,MAAM,OAAA,GAAU,OAAA,KAAY,cAAA,GACtB,yCAAA,GACA,wCAAA;AACN,IAAA,OAAO,CAAA,EAAG,OAAO,CAAA,CAAA,EAAI,WAAW,CAAA,CAAA;AAAA,EACpC;AAGA,E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CAAY,KAAA;AAAA,IAC/B,cAAA,EAAgB,MAAA,CAAO,WAAA,CAAY,iBAAiB,CAAA;AAAA,IACpD,eAAe,WAAA,CAAY,iBAAA;AAAA,IAC3B;AAAA,GACH,CAAA;AAED,EAAA,IAAI,CAAC,aAAa,KAAA,EAAO;AACrB,IAAA,OAAO;AAAA,MACH,KAAA,EAAO,KAAA;AAAA,MACP,aAAA,EAAe,aAAa,KAAA,IAAS;AAAA,KACzC;AAAA,EACJ;AAEA,EAAA,OAAO;AAAA,IACH,KAAA,EAAO,IAAA;AAAA,IACP,SAAS,YAAA,CAAa,SAAA;AAAA,IACtB,WAAA,EAAa;AAAA,MACT,WAAW,YAAA,CAAa,SAAA;AAAA,MACxB,WAAW,YAAA,CAAa,SAAA;AAAA,MACxB,MAAM,YAAA,CAAa;AAAA;AACvB,GACJ;AACJ;AAKO,SAAS,mBAAmB,MAAA,EAAuC;AACtE,EAAA,IAAI;AACA,IAAA,MAAM,UAAU,MAAA,CAAO,IAAA,CAAK,QAAQ,QAAQ,CAAA,CAAE,SAAS,OAAO,CAAA;AAC9D,IAAA,OAAO,IAAA,CAAK,MAAM,OAAO,CAAA;AAAA,EAC7B,CAAA,CAAA,MAAQ;AACJ,IAAA,OAAO,IAAA;AAAA,EACX;AACJ;AAKO,SAAS,sBAAsB,QAAA,EAAwC;AAC1E,EAAA,OAAO,MAAA,CAAO,KAAK,IAAA,CAAK,SAAA,CAAU,QAAQ,CAAC,CAAA,CAAE,SAAS,QAAQ,CAAA;AAClE","file":"index.cjs","sourcesContent":["// Solana RPC client with multi-provider support\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}\n\n// Singleton state\nlet cachedConnection: Connection | null = null;\nlet cachedNetwork: SolanaNetwork | null = null;\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 * 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\n cachedConnection = new Connection(rpcUrl, {\n commitment: 'confirmed',\n confirmTransactionInitialTimeout: 60000,\n });\n cachedNetwork = network;\n\n return cachedConnection;\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\nimport { PublicKey, LAMPORTS_PER_SOL, type ParsedTransactionWithMeta } from '@solana/web3.js';\nimport { getConnection, type SolanaClientConfig } from './client';\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 /** Solana client configuration */\n clientConfig: SolanaClientConfig;\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 */\nexport async function verifyPayment(\n params: VerifyPaymentParams\n): Promise<TransactionVerificationResult> {\n const {\n signature,\n expectedRecipient,\n expectedAmount,\n maxAgeSeconds = 300,\n clientConfig\n } = params;\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: ${JSON.stringify(transaction.meta.err)}`,\n };\n }\n\n // Validate transaction age\n if (transaction.blockTime) {\n const now = Math.floor(Date.now() / 1000);\n if (now - transaction.blockTime > maxAgeSeconds) {\n return { valid: false, confirmed: true, signature, error: 'Transaction too old' };\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 // Validate amount\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 amount: expected ${expectedAmount}, got ${transferDetails.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 return {\n valid: false,\n confirmed: false,\n signature,\n error: error instanceof Error ? error.message : 'Unknown verification error',\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 const connection = getConnection(clientConfig);\n\n try {\n const confirmation = await connection.confirmTransaction(signature, 'confirmed');\n\n if (confirmation.value.err) {\n return {\n confirmed: false,\n error: `Transaction failed: ${JSON.stringify(confirmation.value.err)}`,\n };\n }\n\n return { confirmed: true, slot: confirmation.context?.slot };\n } catch (error) {\n return {\n confirmed: false,\n error: error instanceof Error ? error.message : 'Confirmation timeout',\n };\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 const connection = getConnection(clientConfig);\n const pubkey = new PublicKey(walletAddress);\n\n try {\n const signatures = await connection.getSignaturesForAddress(pubkey, { limit });\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 return BigInt(Math.floor(sol * LAMPORTS_PER_SOL));\n}\n","// x402 payment configuration and helpers\nimport type { PaymentRequirement, X402Network, SolanaNetwork } from '../types';\nimport { toX402Network } from '../solana';\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 * Build a payment requirement for an article\n */\nexport function buildPaymentRequirement(params: BuildPaymentParams): PaymentRequirement {\n const x402Network = toX402Network(params.network);\n\n return {\n scheme: 'exact',\n network: x402Network,\n maxAmountRequired: params.priceInLamports.toString(),\n resource: params.resourceUrl,\n description: `Unlock: ${params.articleTitle}`,\n mimeType: 'text/html',\n payTo: params.creatorWallet,\n maxTimeoutSeconds: params.maxTimeoutSeconds ?? 300,\n asset: 'native',\n extra: {\n name: params.articleTitle,\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 */\nexport function decodePaymentRequired(encoded: string): PaymentRequirement {\n return JSON.parse(Buffer.from(encoded, 'base64').toString('utf-8'));\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 return {\n error: 'Payment Required',\n message: requirement.description,\n price: {\n amount: requirement.maxAmountRequired,\n asset: requirement.asset,\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\nimport { verifyPayment, type SolanaClientConfig } from '../solana';\nimport type { PaymentPayload, PaymentRequirement, VerificationResponse } from '../types';\n\n/**\n * Verify a payment payload against requirements\n */\nexport async function verifyX402Payment(\n payload: PaymentPayload,\n requirement: PaymentRequirement,\n clientConfig: SolanaClientConfig\n): Promise<VerificationResponse> {\n // Validate payload structure\n if (!payload.payload?.signature) {\n return {\n valid: false,\n invalidReason: 'Missing transaction signature',\n };\n }\n\n // Validate network matches\n if (payload.network !== requirement.network) {\n return {\n valid: false,\n invalidReason: `Network mismatch: expected ${requirement.network}, got ${payload.network}`,\n };\n }\n\n // Verify on-chain\n const verification = await verifyPayment({\n signature: payload.payload.signature,\n expectedRecipient: requirement.payTo,\n expectedAmount: BigInt(requirement.maxAmountRequired),\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 transaction: {\n signature: verification.signature,\n blockTime: verification.blockTime,\n slot: verification.slot,\n },\n };\n}\n\n/**\n * Parse payment payload from x402 header\n */\nexport function parsePaymentHeader(header: string): PaymentPayload | null {\n try {\n const decoded = Buffer.from(header, 'base64').toString('utf-8');\n return JSON.parse(decoded) as PaymentPayload;\n } catch {\n return null;\n }\n}\n\n/**\n * Encode payment response for header\n */\nexport function encodePaymentResponse(response: VerificationResponse): string {\n return Buffer.from(JSON.stringify(response)).toString('base64');\n}\n"]}
|
|
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":";;;;;AAeA,IAAI,gBAAA,GAAsC,IAAA;AAC1C,IAAI,aAAA,GAAsC,IAAA;AAQ1C,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,CAAO,QAAA,CAAS,WAAW,CAAA,EAAG;AAC7E,MAAA,OAAO,MAAA,CAAO,QAAA,CAAS,GAAG,CAAA,GAAI,CAAA,EAAG,MAAM,CAAA,EAAG,WAAW,CAAA,CAAA,GAAK,CAAA,EAAG,MAAM,CAAA,CAAA,EAAI,WAAW,CAAA,CAAA;AAAA,IACtF;AACA,IAAA,OAAO,MAAA;AAAA,EACX;AAEA,EAAA,IAAI,WAAA,EAA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E,SAAS,OAAO,CAAA;AAC9D,IAAA,MAAM,MAAA,GAAS,IAAA,CAAK,KAAA,CAAM,OAAO,CAAA;AAGjC,IAAA,IAAI,CAAC,MAAA,IAAU,OAAO,MAAA,KAAW,QAAA,EAAU;AACvC,MAAA,OAAO,IAAA;AAAA,IACX;AAEA,IAAA,OAAO,MAAA;AAAA,EACX,CAAA,CAAA,MAAQ;AACJ,IAAA,OAAO,IAAA;AAAA,EACX;AACJ;AAKO,SAAS,sBAAsB,QAAA,EAAwC;AAC1E,EAAA,OAAO,MAAA,CAAO,KAAK,IAAA,CAAK,SAAA,CAAU,QAAQ,CAAC,CAAA,CAAE,SAAS,QAAQ,CAAA;AAClE","file":"index.cjs","sourcesContent":["// Solana RPC client with multi-provider support\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}\n\n// Singleton state\nlet cachedConnection: Connection | null = null;\nlet cachedNetwork: SolanaNetwork | null = null;\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 * 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\n cachedConnection = new Connection(rpcUrl, {\n commitment: 'confirmed',\n confirmTransactionInitialTimeout: 60000,\n });\n cachedNetwork = network;\n\n return cachedConnection;\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';\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 /** 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 } = 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 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 transaction: {\n signature: verification.signature,\n blockTime: verification.blockTime,\n slot: verification.slot,\n },\n };\n}\n\n/**\n * Parse payment payload from x402 header\n * SECURITY: Safe JSON parsing with try-catch\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 payment response for header\n */\nexport function encodePaymentResponse(response: VerificationResponse): string {\n return Buffer.from(JSON.stringify(response)).toString('base64');\n}\n"]}
|
package/dist/x402/index.d.cts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { S as SolanaNetwork, P as PaymentRequirement, X as X402Network, a as PaymentPayload,
|
|
1
|
+
import { S as SolanaNetwork, P as PaymentRequirement, X as X402Network, a as PaymentPayload, b as VerificationResponse } from '../payment-CTxdtqmc.cjs';
|
|
2
|
+
import { S as SolanaClientConfig } from '../client-vRr48m2x.cjs';
|
|
2
3
|
import '@solana/web3.js';
|
|
3
4
|
|
|
4
5
|
/** Parameters for building a payment requirement */
|
|
@@ -20,6 +21,7 @@ interface BuildPaymentParams {
|
|
|
20
21
|
}
|
|
21
22
|
/**
|
|
22
23
|
* Build a payment requirement for an article
|
|
24
|
+
* SECURITY: Validates all inputs before building requirement
|
|
23
25
|
*/
|
|
24
26
|
declare function buildPaymentRequirement(params: BuildPaymentParams): PaymentRequirement;
|
|
25
27
|
/**
|
|
@@ -28,6 +30,7 @@ declare function buildPaymentRequirement(params: BuildPaymentParams): PaymentReq
|
|
|
28
30
|
declare function encodePaymentRequired(requirement: PaymentRequirement): string;
|
|
29
31
|
/**
|
|
30
32
|
* Decode payment requirement from x402 header
|
|
33
|
+
* SECURITY: Safe parsing with size limit
|
|
31
34
|
*/
|
|
32
35
|
declare function decodePaymentRequired(encoded: string): PaymentRequirement;
|
|
33
36
|
/**
|
|
@@ -57,10 +60,12 @@ declare function create402Headers(requirement: PaymentRequirement): Record<strin
|
|
|
57
60
|
|
|
58
61
|
/**
|
|
59
62
|
* Verify a payment payload against requirements
|
|
63
|
+
* SECURITY: Full validation chain - format, network, on-chain
|
|
60
64
|
*/
|
|
61
65
|
declare function verifyX402Payment(payload: PaymentPayload, requirement: PaymentRequirement, clientConfig: SolanaClientConfig): Promise<VerificationResponse>;
|
|
62
66
|
/**
|
|
63
67
|
* Parse payment payload from x402 header
|
|
68
|
+
* SECURITY: Safe JSON parsing with try-catch
|
|
64
69
|
*/
|
|
65
70
|
declare function parsePaymentHeader(header: string): PaymentPayload | null;
|
|
66
71
|
/**
|
package/dist/x402/index.d.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
import { S as SolanaNetwork, P as PaymentRequirement, X as X402Network, a as PaymentPayload,
|
|
1
|
+
import { S as SolanaNetwork, P as PaymentRequirement, X as X402Network, a as PaymentPayload, b as VerificationResponse } from '../payment-CTxdtqmc.js';
|
|
2
|
+
import { S as SolanaClientConfig } from '../client-CSZHI8o8.js';
|
|
2
3
|
import '@solana/web3.js';
|
|
3
4
|
|
|
4
5
|
/** Parameters for building a payment requirement */
|
|
@@ -20,6 +21,7 @@ interface BuildPaymentParams {
|
|
|
20
21
|
}
|
|
21
22
|
/**
|
|
22
23
|
* Build a payment requirement for an article
|
|
24
|
+
* SECURITY: Validates all inputs before building requirement
|
|
23
25
|
*/
|
|
24
26
|
declare function buildPaymentRequirement(params: BuildPaymentParams): PaymentRequirement;
|
|
25
27
|
/**
|
|
@@ -28,6 +30,7 @@ declare function buildPaymentRequirement(params: BuildPaymentParams): PaymentReq
|
|
|
28
30
|
declare function encodePaymentRequired(requirement: PaymentRequirement): string;
|
|
29
31
|
/**
|
|
30
32
|
* Decode payment requirement from x402 header
|
|
33
|
+
* SECURITY: Safe parsing with size limit
|
|
31
34
|
*/
|
|
32
35
|
declare function decodePaymentRequired(encoded: string): PaymentRequirement;
|
|
33
36
|
/**
|
|
@@ -57,10 +60,12 @@ declare function create402Headers(requirement: PaymentRequirement): Record<strin
|
|
|
57
60
|
|
|
58
61
|
/**
|
|
59
62
|
* Verify a payment payload against requirements
|
|
63
|
+
* SECURITY: Full validation chain - format, network, on-chain
|
|
60
64
|
*/
|
|
61
65
|
declare function verifyX402Payment(payload: PaymentPayload, requirement: PaymentRequirement, clientConfig: SolanaClientConfig): Promise<VerificationResponse>;
|
|
62
66
|
/**
|
|
63
67
|
* Parse payment payload from x402 header
|
|
68
|
+
* SECURITY: Safe JSON parsing with try-catch
|
|
64
69
|
*/
|
|
65
70
|
declare function parsePaymentHeader(header: string): PaymentPayload | null;
|
|
66
71
|
/**
|