p-sdk-wallet
Version:
A comprehensive wallet SDK for React Native (pwc), supporting multi-chain and multi-account features.
115 lines (114 loc) • 4.85 kB
JavaScript
;
/**
* Environment configuration for PWC Wallet SDK
* Centralizes all environment variables and provides type-safe access
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.getEnvironmentConfig = getEnvironmentConfig;
exports.validateEnvironmentConfig = validateEnvironmentConfig;
exports.getEnvVar = getEnvVar;
exports.getEnvVarNumber = getEnvVarNumber;
exports.getEnvVarBigInt = getEnvVarBigInt;
exports.getEnvVarBoolean = getEnvVarBoolean;
/**
* Get environment configuration with defaults
*/
function getEnvironmentConfig() {
return {
rpcUrls: {
'1': process.env.RPC_URL_1 || 'https://eth.llamarpc.com',
'56': process.env.RPC_URL_56 || 'https://bsc-dataseed.binance.org/',
'137': process.env.RPC_URL_137 || 'https://polygon-rpc.com/',
'42161': process.env.RPC_URL_42161 || 'https://arb1.arbitrum.io/rpc',
'10': process.env.RPC_URL_10 || 'https://mainnet.optimism.io',
'8453': process.env.RPC_URL_8453 || 'https://mainnet.base.org',
'solana': process.env.RPC_URL_SOLANA || 'https://api.mainnet-beta.solana.com',
'solana-devnet': process.env.RPC_URL_SOLANA_DEVNET || 'https://api.devnet.solana.com',
'97': process.env.RPC_URL_97 || 'https://bsc-testnet-dataseed.bnbchain.org',
},
explorerUrls: {
'1': process.env.EXPLORER_URL_1 || 'https://etherscan.io',
'56': process.env.EXPLORER_URL_56 || 'https://bscscan.com',
'137': process.env.EXPLORER_URL_137 || 'https://polygonscan.com',
'42161': process.env.EXPLORER_URL_42161 || 'https://arbiscan.io',
'10': process.env.EXPLORER_URL_10 || 'https://optimistic.etherscan.io',
'8453': process.env.EXPLORER_URL_8453 || 'https://basescan.org',
'solana': process.env.EXPLORER_URL_SOLANA || 'https://solscan.io',
'solana-devnet': process.env.EXPLORER_URL_SOLANA_DEVNET || 'https://solscan.io?cluster=devnet',
'97': process.env.EXPLORER_URL_97 || 'https://testnet.bscscan.com',
},
gas: {
maxFeePerGas: process.env.MAX_FEE_PER_GAS ? BigInt(process.env.MAX_FEE_PER_GAS) : undefined,
maxPriorityFeePerGas: process.env.MAX_PRIORITY_FEE_PER_GAS ? BigInt(process.env.MAX_PRIORITY_FEE_PER_GAS) : undefined,
gasLimit: process.env.DEFAULT_GAS_LIMIT ? parseInt(process.env.DEFAULT_GAS_LIMIT) : undefined,
},
network: {
defaultChainId: process.env.DEFAULT_CHAIN_ID || '1',
supportedChains: process.env.SUPPORTED_CHAINS ? process.env.SUPPORTED_CHAINS.split(',') : ['1', '56', '137', '42161', '10', '8453', 'solana'],
},
features: {
enableEIP1559: process.env.ENABLE_EIP1559 !== 'false',
enableBatchProcessing: process.env.ENABLE_BATCH_PROCESSING !== 'false',
enableGasEstimation: process.env.ENABLE_GAS_ESTIMATION !== 'false',
},
};
}
/**
* Validate environment configuration
*/
function validateEnvironmentConfig(config) {
const errors = [];
// Validate RPC URLs
Object.entries(config.rpcUrls).forEach(([chainId, url]) => {
if (!url.startsWith('http')) {
errors.push(`Invalid RPC URL for chain ${chainId}: ${url}`);
}
});
// Validate explorer URLs
Object.entries(config.explorerUrls).forEach(([chainId, url]) => {
if (!url.startsWith('http')) {
errors.push(`Invalid explorer URL for chain ${chainId}: ${url}`);
}
});
// Validate gas configuration
if (config.gas.maxFeePerGas && config.gas.maxFeePerGas <= 0n) {
errors.push('MAX_FEE_PER_GAS must be greater than 0');
}
if (config.gas.maxPriorityFeePerGas && config.gas.maxPriorityFeePerGas <= 0n) {
errors.push('MAX_PRIORITY_FEE_PER_GAS must be greater than 0');
}
if (config.gas.gasLimit && (config.gas.gasLimit < 21000 || config.gas.gasLimit > 500000)) {
errors.push('DEFAULT_GAS_LIMIT must be between 21000 and 500000');
}
return errors;
}
/**
* Get environment variable with type conversion
*/
function getEnvVar(key, defaultValue) {
return process.env[key] || defaultValue;
}
function getEnvVarNumber(key, defaultValue) {
const value = process.env[key];
if (!value)
return defaultValue;
const num = parseInt(value);
return isNaN(num) ? defaultValue : num;
}
function getEnvVarBigInt(key, defaultValue) {
const value = process.env[key];
if (!value)
return defaultValue;
try {
return BigInt(value);
}
catch {
return defaultValue;
}
}
function getEnvVarBoolean(key, defaultValue) {
const value = process.env[key];
if (!value)
return defaultValue;
return value.toLowerCase() === 'true';
}