UNPKG

p-sdk-wallet

Version:

A comprehensive wallet SDK for React Native (pwc), supporting multi-chain and multi-account features.

286 lines (285 loc) 9.83 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.DERIVATION_PATHS = exports.getProvider = exports.SUPPORTED_CHAINS = void 0; exports.setGlobalRPCConfig = setGlobalRPCConfig; exports.setGlobalExplorerConfig = setGlobalExplorerConfig; exports.getGlobalRPCConfig = getGlobalRPCConfig; exports.getGlobalExplorerConfig = getGlobalExplorerConfig; exports.clearGlobalConfigs = clearGlobalConfigs; exports.registerCustomChain = registerCustomChain; exports.overrideChain = overrideChain; exports.overrideChains = overrideChains; exports.getChainConfig = getChainConfig; exports.setupChainConfigs = setupChainConfigs; exports.getAllAvailableChains = getAllAvailableChains; exports.clearCustomChains = clearCustomChains; exports.clearOverrides = clearOverrides; exports.clearOverride = clearOverride; exports.getCustomChains = getCustomChains; exports.getOverrides = getOverrides; const ethers_1 = require("ethers"); // Global config registry for mobile app overrides let GLOBAL_RPC_CONFIG = {}; let GLOBAL_EXPLORER_CONFIG = {}; /** * Set global RPC configuration from mobile app (highest priority) * @param config - RPC URLs configuration */ function setGlobalRPCConfig(config) { GLOBAL_RPC_CONFIG = { ...GLOBAL_RPC_CONFIG, ...config }; } /** * Set global explorer configuration from mobile app (highest priority) * @param config - Explorer URLs configuration */ function setGlobalExplorerConfig(config) { GLOBAL_EXPLORER_CONFIG = { ...GLOBAL_EXPLORER_CONFIG, ...config }; } /** * Get global RPC configuration * @returns Current global RPC configuration */ function getGlobalRPCConfig() { return { ...GLOBAL_RPC_CONFIG }; } /** * Get global explorer configuration * @returns Current global explorer configuration */ function getGlobalExplorerConfig() { return { ...GLOBAL_EXPLORER_CONFIG }; } /** * Clear global configurations */ function clearGlobalConfigs() { GLOBAL_RPC_CONFIG = {}; GLOBAL_EXPLORER_CONFIG = {}; } /** * Environment-based configuration for RPC URLs * Allows customization via environment variables with fallbacks to public RPCs * Priority: Mobile App Config > Environment Variables > SDK Defaults */ const getRpcUrl = (chainId, defaultUrl) => { // Priority: Mobile App Config > Environment Variables > SDK Defaults return GLOBAL_RPC_CONFIG[chainId] || process.env[`RPC_URL_${chainId}`] || defaultUrl; }; const getExplorerUrl = (chainId, defaultUrl) => { // Priority: Mobile App Config > Environment Variables > SDK Defaults return GLOBAL_EXPLORER_CONFIG[chainId] || process.env[`EXPLORER_URL_${chainId}`] || defaultUrl; }; /** * Configuration object containing all supported blockchain networks. * Each chain has its RPC URL, explorer URL, native currency details, and type. * RPC URLs can be customized via environment variables. */ exports.SUPPORTED_CHAINS = { '1': { name: 'Ethereum', rpcUrl: getRpcUrl('1', 'https://eth.llamarpc.com'), explorerUrl: getExplorerUrl('1', 'https://etherscan.io'), nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 }, type: 'evm' }, '56': { name: 'BNB Smart Chain', rpcUrl: getRpcUrl('56', 'https://bsc-dataseed.binance.org/'), explorerUrl: getExplorerUrl('56', 'https://bscscan.com'), nativeCurrency: { name: 'BNB', symbol: 'BNB', decimals: 18 }, type: 'evm' }, '137': { name: 'Polygon', rpcUrl: getRpcUrl('137', 'https://polygon-rpc.com/'), explorerUrl: getExplorerUrl('137', 'https://polygonscan.com'), nativeCurrency: { name: 'MATIC', symbol: 'MATIC', decimals: 18 }, type: 'evm' }, '42161': { name: 'Arbitrum One', rpcUrl: getRpcUrl('42161', 'https://arb1.arbitrum.io/rpc'), explorerUrl: getExplorerUrl('42161', 'https://arbiscan.io'), nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 }, type: 'evm' }, '10': { name: 'OP Mainnet', rpcUrl: getRpcUrl('10', 'https://mainnet.optimism.io'), explorerUrl: getExplorerUrl('10', 'https://optimistic.etherscan.io'), nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 }, type: 'evm' }, '8453': { name: 'Base', rpcUrl: getRpcUrl('8453', 'https://mainnet.base.org'), explorerUrl: getExplorerUrl('8453', 'https://basescan.org'), nativeCurrency: { name: 'Ether', symbol: 'ETH', decimals: 18 }, type: 'evm' }, 'solana': { name: 'Solana', rpcUrl: getRpcUrl('solana', 'https://api.mainnet-beta.solana.com'), explorerUrl: getExplorerUrl('solana', 'https://solscan.io'), nativeCurrency: { name: 'SOL', symbol: 'SOL', decimals: 9 }, type: 'solana' }, 'solana-devnet': { name: 'Solana Devnet', rpcUrl: getRpcUrl('solana-devnet', 'https://api.devnet.solana.com'), explorerUrl: getExplorerUrl('solana-devnet', 'https://solscan.io?cluster=devnet'), nativeCurrency: { name: 'SOL', symbol: 'SOL', decimals: 9 }, type: 'solana' }, '97': { name: 'BNB Smart Chain Testnet', rpcUrl: getRpcUrl('97', 'https://bsc-testnet-dataseed.bnbchain.org'), explorerUrl: getExplorerUrl('97', 'https://testnet.bscscan.com'), nativeCurrency: { name: 'BNB', symbol: 'tBNB', decimals: 18 }, type: 'evm' } }; /** * Creates and returns an ethers.js provider for the specified EVM chain. * @param chainId - The chain ID to create a provider for (must be an EVM chain) * @returns An ethers.js JsonRpcProvider instance for the specified chain * @throws Error if the chain is not supported or is not an EVM chain */ const getProvider = (chainId) => { const chainInfo = exports.SUPPORTED_CHAINS[chainId]; if (!chainInfo) { throw new Error(`Unsupported chain: ${chainId}`); } if (chainInfo.type !== 'evm') { throw new Error(`Chain ${chainId} is not an EVM chain`); } return new ethers_1.ethers.JsonRpcProvider(chainInfo.rpcUrl); }; exports.getProvider = getProvider; /** * BIP-44 derivation paths for different blockchain types. * Used for deterministic wallet generation from mnemonics. */ exports.DERIVATION_PATHS = { /** Standard EVM derivation path following BIP-44 */ EVM: "m/44'/60'/0'/0/0", /** Solana derivation path following BIP-44 with hardened derivation */ SOLANA: "m/44'/501'/0'/0'", // Solana BIP-44 path }; // Global registry for custom chains and overrides let CUSTOM_CHAINS = {}; let CHAIN_OVERRIDES = {}; /** * Register a custom chain (new chain not in built-in list) * @param chainId - Custom chain ID (e.g., 'custom-1', 'my-chain') * @param config - Complete chain configuration */ function registerCustomChain(chainId, config) { CUSTOM_CHAINS[chainId] = config; } /** * Override a built-in chain configuration * @param chainId - Built-in chain ID to override * @param override - Partial configuration to override (only specify fields to change) * @throws Error if chainId is not a built-in chain */ function overrideChain(chainId, override) { const supportedChains = exports.SUPPORTED_CHAINS; if (!supportedChains[chainId]) { throw new Error(`Cannot override chain ${chainId} - not a built-in chain`); } CHAIN_OVERRIDES[chainId] = override; } /** * Override multiple built-in chains at once * @param overrides - Object containing multiple chain overrides */ function overrideChains(overrides) { Object.entries(overrides).forEach(([chainId, override]) => { overrideChain(chainId, override); }); } /** * Get chain configuration with priority: Custom > Override > Built-in * @param chainId - Chain ID to get configuration for * @returns Chain configuration * @throws Error if chain is not found */ function getChainConfig(chainId) { // 1. Check custom chains first (highest priority) if (CUSTOM_CHAINS[chainId]) { return CUSTOM_CHAINS[chainId]; } // 2. Check overrides if (CHAIN_OVERRIDES[chainId]) { const baseConfig = exports.SUPPORTED_CHAINS[chainId]; if (!baseConfig) { throw new Error(`Chain ${chainId} not found`); } return { ...baseConfig, ...CHAIN_OVERRIDES[chainId] }; } // 3. Check built-in chains const builtInConfig = exports.SUPPORTED_CHAINS[chainId]; if (!builtInConfig) { throw new Error(`Chain ${chainId} not found`); } return builtInConfig; } /** * Setup chain configurations with overrides and custom chains * @param options - Configuration options */ function setupChainConfigs(options) { if (options.overrides) { overrideChains(options.overrides); } if (options.customChains) { Object.entries(options.customChains).forEach(([chainId, config]) => { registerCustomChain(chainId, config); }); } } /** * Get all available chains (built-in + custom) * @returns Object containing all chain configurations */ function getAllAvailableChains() { return { ...exports.SUPPORTED_CHAINS, ...CUSTOM_CHAINS }; } /** * Clear all custom chains */ function clearCustomChains() { CUSTOM_CHAINS = {}; } /** * Clear all chain overrides */ function clearOverrides() { CHAIN_OVERRIDES = {}; } /** * Clear override for a specific chain * @param chainId - Chain ID to clear override for */ function clearOverride(chainId) { delete CHAIN_OVERRIDES[chainId]; } /** * Get all custom chains * @returns Object containing custom chain configurations */ function getCustomChains() { return { ...CUSTOM_CHAINS }; } /** * Get all chain overrides * @returns Object containing chain override configurations */ function getOverrides() { return { ...CHAIN_OVERRIDES }; }