{"version":3,"sources":["../../src/constants.ts","../../src/utils.ts"],"sourcesContent":["/**\n * CAIP-2 network identifiers for Stellar (V2)\n */\nexport const STELLAR_PUBNET_CAIP2 = \"stellar:pubnet\";\nexport const STELLAR_TESTNET_CAIP2 = \"stellar:testnet\";\nexport const STELLAR_WILDCARD_CAIP2 = \"stellar:*\";\n\n/**\n * Default testnet RPC URL\n */\nexport const DEFAULT_TESTNET_RPC_URL = \"https://soroban-testnet.stellar.org\";\n\n/**\n * Stellar validation regex for destination and asset addresses\n */\nexport const STELLAR_DESTINATION_ADDRESS_REGEX = /^(?:[GC][ABCD][A-Z2-7]{54}|M[ABCD][A-Z2-7]{67})$/; // Stellar address: G-account (56 chars), C-account (56 chars), or M-account (69 chars, muxed)\nexport const STELLAR_ASSET_ADDRESS_REGEX = /^(?:[C][ABCD][A-Z2-7]{54})$/; // Stellar token contract address: C-account (56 chars)\n\n/**\n * USDC contract addresses (default stablecoin)\n */\nexport const USDC_PUBNET_ADDRESS = \"CCW67TSZV3SSS2HXMBQ5JFGCKJNXKZM7UQUWUZPUTHXSTZLEO7SJMI75\";\nexport const USDC_TESTNET_ADDRESS = \"CBIELTK6YBZJU5UP2WWQEUCYKLPU6AUNZ2BQ4WWFEIE3USCIHMXQDAMA\";\n\nexport const STELLAR_NETWORK_TO_PASSPHRASE: ReadonlyMap<string, string> = new Map([\n  [STELLAR_PUBNET_CAIP2, \"Public Global Stellar Network ; September 2015\"],\n  [STELLAR_TESTNET_CAIP2, \"Test SDF Network ; September 2015\"],\n]);\n\n/**\n * Default token decimals\n */\nexport const DEFAULT_TOKEN_DECIMALS = 7;\n","import { rpc } from \"@stellar/stellar-sdk\";\nimport {\n  DEFAULT_TESTNET_RPC_URL,\n  DEFAULT_TOKEN_DECIMALS,\n  STELLAR_ASSET_ADDRESS_REGEX,\n  STELLAR_DESTINATION_ADDRESS_REGEX,\n  STELLAR_NETWORK_TO_PASSPHRASE,\n  STELLAR_PUBNET_CAIP2,\n  STELLAR_TESTNET_CAIP2,\n  USDC_PUBNET_ADDRESS,\n  USDC_TESTNET_ADDRESS,\n} from \"./constants\";\nimport type { Network } from \"@bankofai/x402-core/types\";\n\nexport const DEFAULT_ESTIMATED_LEDGER_SECONDS = 5;\nconst RPC_LEDGERS_SAMPLE_SIZE = 20;\n\n/**\n * Configuration for RPC client connections\n */\nexport interface RpcConfig {\n  /** Custom RPC URL to use instead of defaults */\n  url?: string;\n}\n\n/**\n * Checks if a network is a Stellar network\n *\n * @param network - The CAIP-2 network identifier\n * @returns `true` if the network is a Stellar network, `false` otherwise\n */\nexport function isStellarNetwork(network: Network): boolean {\n  return STELLAR_NETWORK_TO_PASSPHRASE.has(network);\n}\n\n/**\n * Validates a Stellar destination address (G-account, C-account, or M-account)\n *\n * @param address - Stellar destination address to validate\n * @returns `true` if the address is valid, `false` otherwise\n */\nexport function validateStellarDestinationAddress(address: string): boolean {\n  return STELLAR_DESTINATION_ADDRESS_REGEX.test(address);\n}\n\n/**\n * Validates a Stellar asset/contract address (C-account only)\n *\n * @param address - Stellar asset address to validate\n * @returns `true` if the address is valid, `false` otherwise\n */\nexport function validateStellarAssetAddress(address: string): boolean {\n  return STELLAR_ASSET_ADDRESS_REGEX.test(address);\n}\n\n/**\n * Gets the network passphrase for a given Stellar network\n *\n * @param network - The CAIP-2 network identifier\n * @returns The network passphrase string\n * @throws {Error} If the network is not a known Stellar network\n */\nexport function getNetworkPassphrase(network: Network): string {\n  const networkPassphrase = STELLAR_NETWORK_TO_PASSPHRASE.get(network);\n  if (!networkPassphrase) {\n    throw new Error(`Unknown Stellar network: ${network}`);\n  }\n  return networkPassphrase;\n}\n\n/**\n * Gets the RPC URL for a given Stellar network\n *\n * @param network - The CAIP-2 network identifier\n * @param rpcConfig - Optional RPC configuration with custom URL\n * @returns The RPC URL string\n * @throws {Error} If the network is unknown or mainnet RPC URL is not provided\n */\nexport function getRpcUrl(network: Network, rpcConfig?: RpcConfig): string {\n  const customRpcUrl = rpcConfig?.url;\n  switch (network) {\n    case STELLAR_TESTNET_CAIP2:\n      return customRpcUrl || DEFAULT_TESTNET_RPC_URL;\n    case STELLAR_PUBNET_CAIP2:\n      if (!customRpcUrl) {\n        throw new Error(\n          \"Stellar mainnet requires a non-empty rpcUrl. For a list of RPC providers, see https://developers.stellar.org/docs/data/apis/rpc/providers#publicly-accessible-apis\",\n        );\n      }\n      return customRpcUrl;\n    default:\n      throw new Error(`Unknown Stellar network: ${network}`);\n  }\n}\n\n/**\n * Creates a Soroban RPC client for the given network\n *\n * @param network - The CAIP-2 network identifier\n * @param rpcConfig - Optional RPC configuration with custom URL\n * @returns A configured Soroban RPC Server instance\n * @throws {Error} If the network is not a valid Stellar network\n */\nexport function getRpcClient(network: Network, rpcConfig?: RpcConfig): rpc.Server {\n  const rpcUrl = getRpcUrl(network, rpcConfig);\n  return new rpc.Server(rpcUrl, {\n    allowHttp: network === STELLAR_TESTNET_CAIP2, // Allow HTTP for testnet\n  });\n}\n\n/**\n * Fetches the estimated ledger close time (seconds per ledger) from RPC getLedgers.\n *\n * @param server - The Soroban RPC Server instance\n * @returns Estimated seconds per ledger, or DEFAULT_ESTIMATED_LEDGER_SECONDS (5) on error\n */\nexport async function getEstimatedLedgerCloseTimeSeconds(server: rpc.Server): Promise<number> {\n  try {\n    const latestLedger = await server.getLatestLedger();\n    const startLedger = latestLedger.sequence;\n    const { ledgers } = await server.getLedgers({\n      startLedger,\n      pagination: { limit: RPC_LEDGERS_SAMPLE_SIZE },\n    });\n    if (!ledgers || ledgers.length < 2) return DEFAULT_ESTIMATED_LEDGER_SECONDS;\n\n    const oldestTs = parseInt(ledgers[0].ledgerCloseTime);\n    const newestTs = parseInt(ledgers[ledgers.length - 1].ledgerCloseTime);\n    const intervals = ledgers.length - 1;\n    return Math.ceil((newestTs - oldestTs) / intervals);\n  } catch {\n    return DEFAULT_ESTIMATED_LEDGER_SECONDS;\n  }\n}\n\n/**\n * Gets the default USDC contract address for a network\n *\n * @param network - The CAIP-2 network identifier\n * @returns The USDC contract address for the network\n * @throws {Error} If the network doesn't have a configured USDC address\n */\nexport function getUsdcAddress(network: Network): string {\n  switch (network) {\n    case STELLAR_PUBNET_CAIP2:\n      return USDC_PUBNET_ADDRESS;\n    case STELLAR_TESTNET_CAIP2:\n      return USDC_TESTNET_ADDRESS;\n    default:\n      throw new Error(`No USDC address configured for network: ${network}`);\n  }\n}\n\n/**\n * Converts a decimal amount to token smallest units\n *\n * Handles both regular decimal strings (e.g., \"0.10\") and scientific notation (e.g., \"1e-7\").\n * The result is truncated (not rounded) to the specified number of decimal places.\n *\n * @param decimalAmount - The decimal amount as a string\n * @param decimals - Number of decimal places for the token (default: 7 for USDC)\n * @returns The amount in smallest units as a string with leading zeros removed\n * @throws {Error} If the amount is invalid or decimals is out of range\n *\n * @example\n * ```ts\n * convertToTokenAmount(\"0.1\", 7)      // \"1000000\"\n * convertToTokenAmount(\"1.5\", 7)      // \"15000000\"\n * convertToTokenAmount(\"1e-7\", 7)     // \"1\"\n * convertToTokenAmount(\"1.5\", 0)      // \"1\" (truncated)\n * ```\n */\nexport function convertToTokenAmount(\n  decimalAmount: string,\n  decimals: number = DEFAULT_TOKEN_DECIMALS,\n): string {\n  const amount = parseFloat(decimalAmount);\n  if (isNaN(amount)) {\n    throw new Error(`Invalid amount: ${decimalAmount}`);\n  }\n\n  if (decimals < 0 || decimals > 20) {\n    throw new Error(`Decimals must be between 0 and 20, got ${decimals}`);\n  }\n\n  // Normalize scientific notation to fixed decimal string\n  const normalizedDecimal = /[eE]/.test(decimalAmount)\n    ? amount.toFixed(Math.max(decimals, 20))\n    : decimalAmount;\n\n  const [intPart, decPart = \"\"] = normalizedDecimal.split(\".\");\n  const paddedDec = decPart.padEnd(decimals, \"0\").slice(0, decimals);\n\n  return (intPart + paddedDec).replace(/^0+/, \"\") || \"0\";\n}\n"],"mappings":";AAGO,IAAM,uBAAuB;AAC7B,IAAM,wBAAwB;AAC9B,IAAM,yBAAyB;AAK/B,IAAM,0BAA0B;AAKhC,IA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