@alleyboss/micropay-solana-x402-paywall 2.2.0 → 2.3.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/dist/index.js CHANGED
@@ -146,7 +146,8 @@ async function verifyPayment(params) {
146
146
  expectedRecipient,
147
147
  expectedAmount,
148
148
  maxAgeSeconds = 300,
149
- clientConfig
149
+ clientConfig,
150
+ signatureStore
150
151
  } = params;
151
152
  if (!isValidSignature(signature)) {
152
153
  return { valid: false, confirmed: false, signature, error: "Invalid signature format" };
@@ -157,6 +158,12 @@ async function verifyPayment(params) {
157
158
  if (expectedAmount <= 0n) {
158
159
  return { valid: false, confirmed: false, signature, error: "Invalid expected amount" };
159
160
  }
161
+ if (signatureStore) {
162
+ const isUsed = await signatureStore.hasBeenUsed(signature);
163
+ if (isUsed) {
164
+ return { valid: false, confirmed: true, signature, error: "Signature already used" };
165
+ }
166
+ }
160
167
  const effectiveMaxAge = Math.min(Math.max(maxAgeSeconds, 60), 3600);
161
168
  const connection = getConnection(clientConfig);
162
169
  try {
@@ -265,8 +272,6 @@ function solToLamports(sol) {
265
272
  }
266
273
  return BigInt(Math.floor(sol * LAMPORTS_PER_SOL));
267
274
  }
268
-
269
- // src/solana/spl.ts
270
275
  var SIGNATURE_REGEX2 = /^[1-9A-HJ-NP-Za-km-z]{87,88}$/;
271
276
  var WALLET_REGEX2 = /^[1-9A-HJ-NP-Za-km-z]{32,44}$/;
272
277
  function resolveMintAddress(asset, network) {
@@ -360,8 +365,15 @@ async function verifySPLPayment(params) {
360
365
  expectedAmount,
361
366
  asset,
362
367
  clientConfig,
363
- maxAgeSeconds = 300
368
+ maxAgeSeconds = 300,
369
+ signatureStore
364
370
  } = params;
371
+ if (signatureStore) {
372
+ const isUsed = await signatureStore.hasBeenUsed(signature);
373
+ if (isUsed) {
374
+ return { valid: false, confirmed: true, signature, error: "Signature already used" };
375
+ }
376
+ }
365
377
  if (!SIGNATURE_REGEX2.test(signature)) {
366
378
  return { valid: false, confirmed: false, signature, error: "Invalid signature format" };
367
379
  }
@@ -406,6 +418,27 @@ async function verifySPLPayment(params) {
406
418
  error: "No valid token transfer to recipient found"
407
419
  };
408
420
  }
421
+ if (transfer.to) {
422
+ try {
423
+ const destinationInfo = await connection.getParsedAccountInfo(new PublicKey(transfer.to));
424
+ const owner = destinationInfo.value?.data?.parsed?.info?.owner;
425
+ if (owner && owner !== expectedRecipient) {
426
+ return {
427
+ valid: false,
428
+ confirmed: true,
429
+ signature,
430
+ error: "Recipient mismatch: Token account not owned by merchant"
431
+ };
432
+ }
433
+ } catch (e) {
434
+ return {
435
+ valid: false,
436
+ confirmed: true,
437
+ signature,
438
+ error: "Could not verify token account owner"
439
+ };
440
+ }
441
+ }
409
442
  if (transfer.mint !== mintAddress) {
410
443
  return {
411
444
  valid: false,
@@ -1284,14 +1317,14 @@ async function getSolPrice() {
1284
1317
  }
1285
1318
  }
1286
1319
  if (cachedPrice) {
1287
- return cachedPrice;
1320
+ return {
1321
+ ...cachedPrice,
1322
+ source: `${cachedPrice.source} (stale)`
1323
+ };
1288
1324
  }
1289
- return {
1290
- solPrice: 150,
1291
- // Reasonable fallback
1292
- fetchedAt: /* @__PURE__ */ new Date(),
1293
- source: "fallback"
1294
- };
1325
+ throw new Error(
1326
+ "Failed to fetch SOL price from all providers. Configure a custom provider or ensure network connectivity."
1327
+ );
1295
1328
  }
1296
1329
  async function lamportsToUsd(lamports) {
1297
1330
  const { solPrice } = await getSolPrice();