@alleyboss/micropay-solana-x402-paywall 2.0.2 → 2.1.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.
package/dist/index.d.cts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { A as ArticlePaymentConfig, d as PaymentAsset, a as PaymentPayload, P as PaymentRequirement, c as PaymentStatus, e as SPLTokenAsset, S as SolanaNetwork, T as TOKEN_MINTS, V as VerificationRequest, b as VerificationResponse, X as X402Network } from './payment-CTxdtqmc.cjs';
2
2
  export { a as SessionConfig, S as SessionData, c as SessionJWTPayload, b as SessionValidation } from './session-D2IoWAWV.cjs';
3
- export { S as SolanaClientConfig, g as getConnection, i as isMainnet, r as resetConnection, t as toX402Network } from './client-vRr48m2x.cjs';
4
- export { SPLVerificationResult, TransactionVerificationResult, VerifyPaymentParams, VerifySPLPaymentParams, getTokenDecimals, getWalletTransactions, isNativeAsset, lamportsToSol, resolveMintAddress, solToLamports, verifyPayment, verifySPLPayment, waitForConfirmation } from './solana/index.cjs';
3
+ export { R as RpcConnectionWithFallback, S as SolanaClientConfig, g as getConnection, a as getConnectionWithFallback, i as isMainnet, r as resetConnection, t as toX402Network, w as withFallback } from './client-Blkcv17a.cjs';
4
+ export { PriorityFeeConfig, SPLVerificationResult, TransactionVerificationResult, VerifyPaymentParams, VerifySPLPaymentParams, VersionedTransactionConfig, VersionedTransactionResult, buildVersionedTransaction, calculatePriorityFeeCost, createPriorityFeeInstructions, estimatePriorityFee, fetchLookupTables, getTokenDecimals, getWalletTransactions, isNativeAsset, isVersionedTransaction, lamportsToSol, resolveMintAddress, solToLamports, verifyPayment, verifySPLPayment, waitForConfirmation } from './solana/index.cjs';
5
5
  export { addArticleToSession, createSession, isArticleUnlocked, validateSession } from './session/index.cjs';
6
6
  export { BuildPaymentParams, X402_HEADERS, buildPaymentRequirement, create402Headers, create402ResponseBody, decodePaymentRequired, encodePaymentRequired, encodePaymentResponse, parsePaymentHeader, verifyX402Payment } from './x402/index.cjs';
7
7
  export { StoreConfig, createRedisStore } from './store/index.cjs';
package/dist/index.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  export { A as ArticlePaymentConfig, d as PaymentAsset, a as PaymentPayload, P as PaymentRequirement, c as PaymentStatus, e as SPLTokenAsset, S as SolanaNetwork, T as TOKEN_MINTS, V as VerificationRequest, b as VerificationResponse, X as X402Network } from './payment-CTxdtqmc.js';
2
2
  export { a as SessionConfig, S as SessionData, c as SessionJWTPayload, b as SessionValidation } from './session-D2IoWAWV.js';
3
- export { S as SolanaClientConfig, g as getConnection, i as isMainnet, r as resetConnection, t as toX402Network } from './client-CSZHI8o8.js';
4
- export { SPLVerificationResult, TransactionVerificationResult, VerifyPaymentParams, VerifySPLPaymentParams, getTokenDecimals, getWalletTransactions, isNativeAsset, lamportsToSol, resolveMintAddress, solToLamports, verifyPayment, verifySPLPayment, waitForConfirmation } from './solana/index.js';
3
+ export { R as RpcConnectionWithFallback, S as SolanaClientConfig, g as getConnection, a as getConnectionWithFallback, i as isMainnet, r as resetConnection, t as toX402Network, w as withFallback } from './client-xhJSoeOH.js';
4
+ export { PriorityFeeConfig, SPLVerificationResult, TransactionVerificationResult, VerifyPaymentParams, VerifySPLPaymentParams, VersionedTransactionConfig, VersionedTransactionResult, buildVersionedTransaction, calculatePriorityFeeCost, createPriorityFeeInstructions, estimatePriorityFee, fetchLookupTables, getTokenDecimals, getWalletTransactions, isNativeAsset, isVersionedTransaction, lamportsToSol, resolveMintAddress, solToLamports, verifyPayment, verifySPLPayment, waitForConfirmation } from './solana/index.js';
5
5
  export { addArticleToSession, createSession, isArticleUnlocked, validateSession } from './session/index.js';
6
6
  export { BuildPaymentParams, X402_HEADERS, buildPaymentRequirement, create402Headers, create402ResponseBody, decodePaymentRequired, encodePaymentRequired, encodePaymentResponse, parsePaymentHeader, verifyX402Payment } from './x402/index.js';
7
7
  export { StoreConfig, createRedisStore } from './store/index.js';
package/dist/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { Connection, PublicKey, LAMPORTS_PER_SOL, clusterApiUrl } from '@solana/web3.js';
1
+ import { PublicKey, LAMPORTS_PER_SOL, ComputeBudgetProgram, TransactionMessage, VersionedTransaction, clusterApiUrl, Connection } from '@solana/web3.js';
2
2
  import { SignJWT, jwtVerify } from 'jose';
3
3
  import { v4 } from 'uuid';
4
4
 
@@ -13,6 +13,8 @@ var TOKEN_MINTS = {
13
13
  };
14
14
  var cachedConnection = null;
15
15
  var cachedNetwork = null;
16
+ var cachedFallbacks = [];
17
+ var cachedFallbackEnabled = false;
16
18
  function buildRpcUrl(config2) {
17
19
  const { network, rpcUrl, tatumApiKey } = config2;
18
20
  if (rpcUrl) {
@@ -27,19 +29,65 @@ function buildRpcUrl(config2) {
27
29
  }
28
30
  return clusterApiUrl(network);
29
31
  }
32
+ function createConnection(rpcUrl) {
33
+ return new Connection(rpcUrl, {
34
+ commitment: "confirmed",
35
+ confirmTransactionInitialTimeout: 6e4
36
+ });
37
+ }
30
38
  function getConnection(config2) {
31
39
  const { network } = config2;
32
40
  if (cachedConnection && cachedNetwork === network) {
33
41
  return cachedConnection;
34
42
  }
35
43
  const rpcUrl = buildRpcUrl(config2);
36
- cachedConnection = new Connection(rpcUrl, {
37
- commitment: "confirmed",
38
- confirmTransactionInitialTimeout: 6e4
39
- });
44
+ cachedConnection = createConnection(rpcUrl);
40
45
  cachedNetwork = network;
46
+ cachedFallbackEnabled = config2.enableFallback ?? false;
47
+ cachedFallbacks = [];
48
+ if (cachedFallbackEnabled && config2.fallbackRpcUrls?.length) {
49
+ cachedFallbacks = config2.fallbackRpcUrls.map(createConnection);
50
+ }
41
51
  return cachedConnection;
42
52
  }
53
+ function getConnectionWithFallback(config2) {
54
+ const connection = getConnection(config2);
55
+ return {
56
+ connection,
57
+ fallbacks: cachedFallbacks,
58
+ fallbackEnabled: cachedFallbackEnabled
59
+ };
60
+ }
61
+ async function withFallback(config2, operation) {
62
+ const { connection, fallbacks, fallbackEnabled } = getConnectionWithFallback(config2);
63
+ try {
64
+ return await operation(connection);
65
+ } catch (error) {
66
+ if (!fallbackEnabled || fallbacks.length === 0) {
67
+ throw error;
68
+ }
69
+ if (!isRetryableError(error)) {
70
+ throw error;
71
+ }
72
+ for (let i = 0; i < fallbacks.length; i++) {
73
+ try {
74
+ return await operation(fallbacks[i]);
75
+ } catch (fallbackError) {
76
+ if (i === fallbacks.length - 1) {
77
+ throw fallbackError;
78
+ }
79
+ }
80
+ }
81
+ throw error;
82
+ }
83
+ }
84
+ function isRetryableError(error) {
85
+ if (error instanceof Error) {
86
+ const message = error.message.toLowerCase();
87
+ return message.includes("429") || message.includes("503") || message.includes("502") || message.includes("timeout") || message.includes("econnrefused") || message.includes("enotfound") || message.includes("rate limit");
88
+ }
89
+ return false;
90
+ }
43
91
  function resetConnection() {
44
92
  cachedConnection = null;
45
93
  cachedNetwork = null;
@@ -396,6 +444,88 @@ async function verifySPLPayment(params) {
396
444
  function isNativeAsset(asset) {
397
445
  return asset === "native";
398
446
  }
447
+ var DEFAULT_COMPUTE_UNITS = 2e5;
448
+ var DEFAULT_MICRO_LAMPORTS = 1e3;
449
+ function createPriorityFeeInstructions(config2 = {}) {
450
+ const { enabled = false, microLamports, computeUnits } = config2;
451
+ if (!enabled) {
452
+ return [];
453
+ }
454
+ const instructions = [];
455
+ const units = computeUnits ?? DEFAULT_COMPUTE_UNITS;
456
+ instructions.push(ComputeBudgetProgram.setComputeUnitLimit({ units }));
457
+ const price = microLamports ?? DEFAULT_MICRO_LAMPORTS;
458
+ instructions.push(ComputeBudgetProgram.setComputeUnitPrice({ microLamports: price }));
459
+ return instructions;
460
+ }
461
+ async function estimatePriorityFee(connection, accounts = []) {
462
+ try {
463
+ const fees = await connection.getRecentPrioritizationFees({
464
+ lockedWritableAccounts: accounts
465
+ });
466
+ if (fees.length === 0) {
467
+ return DEFAULT_MICRO_LAMPORTS;
468
+ }
469
+ const sortedFees = fees.map((f) => f.prioritizationFee).filter((f) => f > 0).sort((a, b) => a - b);
470
+ if (sortedFees.length === 0) {
471
+ return DEFAULT_MICRO_LAMPORTS;
472
+ }
473
+ const medianIndex = Math.floor(sortedFees.length / 2);
474
+ return sortedFees[medianIndex];
475
+ } catch {
476
+ return DEFAULT_MICRO_LAMPORTS;
477
+ }
478
+ }
479
+ function calculatePriorityFeeCost(microLamportsPerCU, computeUnits) {
480
+ return Math.ceil(microLamportsPerCU * computeUnits / 1e6);
481
+ }
482
+ async function buildVersionedTransaction(config2) {
483
+ const {
484
+ connection,
485
+ payer,
486
+ instructions,
487
+ lookupTables = [],
488
+ priorityFee,
489
+ recentBlockhash
490
+ } = config2;
491
+ const priorityIxs = createPriorityFeeInstructions(priorityFee);
492
+ const allInstructions = [...priorityIxs, ...instructions];
493
+ let blockhash;
494
+ let lastValidBlockHeight;
495
+ if (recentBlockhash) {
496
+ blockhash = recentBlockhash;
497
+ const slot = await connection.getSlot();
498
+ lastValidBlockHeight = slot + 150;
499
+ } else {
500
+ const latestBlockhash = await connection.getLatestBlockhash("confirmed");
501
+ blockhash = latestBlockhash.blockhash;
502
+ lastValidBlockHeight = latestBlockhash.lastValidBlockHeight;
503
+ }
504
+ const message = new TransactionMessage({
505
+ payerKey: payer,
506
+ recentBlockhash: blockhash,
507
+ instructions: allInstructions
508
+ }).compileToV0Message(lookupTables);
509
+ const transaction = new VersionedTransaction(message);
510
+ return {
511
+ transaction,
512
+ blockhash,
513
+ lastValidBlockHeight
514
+ };
515
+ }
516
+ async function fetchLookupTables(connection, addresses) {
517
+ const tables = [];
518
+ for (const address of addresses) {
519
+ const result = await connection.getAddressLookupTable(address);
520
+ if (result.value) {
521
+ tables.push(result.value);
522
+ }
523
+ }
524
+ return tables;
525
+ }
526
+ function isVersionedTransaction(tx) {
527
+ return tx instanceof VersionedTransaction;
528
+ }
399
529
  var MAX_ARTICLES_PER_SESSION = 100;
400
530
  var MIN_SECRET_LENGTH = 32;
401
531
  function getSecretKey(secret) {
@@ -1170,6 +1300,6 @@ function getProviders() {
1170
1300
  return PROVIDERS.map((p) => ({ name: p.name, url: p.url }));
1171
1301
  }
1172
1302
 
1173
- export { TOKEN_MINTS, X402_HEADERS, addArticleToSession, buildPaymentRequirement, buildSolanaPayUrl, checkPaywallAccess, clearPriceCache, configurePricing, create402Headers, create402ResponseBody, createMemoryStore, createPaymentFlow, createPaymentReference, createPaywallMiddleware, createRedisStore, createSession, decodePaymentRequired, encodePaymentRequired, encodePaymentResponse, formatPriceDisplay, formatPriceSync, getConnection, getProviders, getSolPrice, getTokenDecimals, getWalletTransactions, isArticleUnlocked, isMainnet, isNativeAsset, isRetryableRPCError, lamportsToSol, lamportsToUsd, parsePaymentHeader, resetConnection, resolveMintAddress, solToLamports, toX402Network, usdToLamports, validateSession, verifyPayment, verifySPLPayment, verifyX402Payment, waitForConfirmation, withPaywall, withRetry };
1303
+ export { TOKEN_MINTS, X402_HEADERS, addArticleToSession, buildPaymentRequirement, buildSolanaPayUrl, buildVersionedTransaction, calculatePriorityFeeCost, checkPaywallAccess, clearPriceCache, configurePricing, create402Headers, create402ResponseBody, createMemoryStore, createPaymentFlow, createPaymentReference, createPaywallMiddleware, createPriorityFeeInstructions, createRedisStore, createSession, decodePaymentRequired, encodePaymentRequired, encodePaymentResponse, estimatePriorityFee, fetchLookupTables, formatPriceDisplay, formatPriceSync, getConnection, getConnectionWithFallback, getProviders, getSolPrice, getTokenDecimals, getWalletTransactions, isArticleUnlocked, isMainnet, isNativeAsset, isRetryableRPCError, isVersionedTransaction, lamportsToSol, lamportsToUsd, parsePaymentHeader, resetConnection, resolveMintAddress, solToLamports, toX402Network, usdToLamports, validateSession, verifyPayment, verifySPLPayment, verifyX402Payment, waitForConfirmation, withFallback, withPaywall, withRetry };
1174
1304
  //# sourceMappingURL=index.js.map
1175
1305
  //# sourceMappingURL=index.js.map