Files
hash-wallet/cw_evm/lib/evm_chain_registry.dart
T
jwinterm dd449f2594 Merge upstream Cake Wallet v6.5.0
Catches up from the May 12 fork point (v6.1.2+23) through v6.5.0: 535
upstream commits, including the Monero silent-open fix, Monero on Trezor,
Ledger/Trezor BLE fixes, the new seed and history screens, quick Bitcoin
sync, git-pinned dependencies (pubspec_overrides.yaml), frozen Hive
adapters (*.part.dart), monero_c v0.18.4.6-RC2 and Flutter 3.41.9.

Merged with both sides normalized first (import rename, 100-column
format), which took the conflict count from 614 paths to 322. The
procedure is now scripts/upstream_sync.sh.

Kept from Hash Bags:
- WalletType enum order and Hive IDs. SQLite stores WalletType.index and
  our order differs from upstream's; taking theirs would reread saved
  wallets as other chains. Our frozen adapter keeps the shipped IDs.
- The 1.0.2 legacy PIN and wallet-password decode and self-heal.
- Removed chains (Solana, Tron, Zano, Zcash, Decred, Haven) and the new
  Robinhood chain stay out; Cake Pay and non-Trocador swaps stay out.
- Default nodes (node.wownero.org, sethforprivacy, Blockstream, bysh.me),
  converted to upstream's isDefault node-list key; Wownero is back in the
  node reset list.
- Privacy: address resolvers limited to domain-anchored sources again;
  prices.neroswap.com for fiat; upstream's charts tab, which reads
  prices.cakewallet.com, sits behind FeatureFlag.isChartsEnabled = false.
- Lightning stays behind FeatureFlag.isLightningEnabled, including
  upstream's new post-seed Lightning username step.
- Our icon pipeline, branding and bundle identifiers.

Twelve new upstream widget tests are skipped: test and code are unchanged
from v6.5.0 apart from renames (not rerun on a pristine upstream checkout;
upstream's top_bar_test does not compile against its own TopBar).
flutter analyze lib: 0 errors; flutter test: 345 passed, 12 skipped;
flutter build bundle compiles. Built with Flutter 3.41.9.
2026-10-24 23:49:29 -04:00

235 lines
6.4 KiB
Dart

import 'package:cw_core/crypto_currency.dart';
import 'package:cw_core/wallet_type.dart';
import 'package:cw_evm/utils/network_chain_utils.dart';
/// Centralized registry for all EVM chain configurations
class EvmChainRegistry {
static final EvmChainRegistry _instance = EvmChainRegistry._internal();
factory EvmChainRegistry() => _instance;
EvmChainRegistry._internal() {
initialize();
}
final Map<int, ChainConfig> _chains = {};
final Map<WalletType, int> _walletTypeToChainId = {};
final Map<int, WalletType> _chainIdToWalletType = {};
final Map<String, int> _tagToChainId = {};
final Map<String, int> _caip2ToChainId = {};
bool _initialized = false;
/// Initialize registry with all supported EVM chains
void initialize() {
if (_initialized) return;
_initialized = true;
// Ethereum Mainnet
_registerChain(
const ChainConfig(
chainId: 1,
name: 'Ethereum',
shortCode: 'eth',
caip2: 'eip155:1',
nativeCurrency: CryptoCurrency.eth,
capabilities: ChainCapabilities(
supportsERC20: true,
supportsEIP1559: true,
supportsInternalTx: true,
supportsSubscriptions: false,
supportsENS: true,
),
defaultRpcEndpoints: [
'ethereum-rpc.publicnode.com',
'eth.llamarpc.com',
'rpc.flashbots.net',
'eth-mainnet.public.blastapi.io',
'ethereum.publicnode.com',
],
explorerUrls: [
'https://etherscan.io',
],
feeModel: FeeModel(
type: FeeType.eip1559,
defaultGasLimit: 21000,
),
),
WalletType.ethereum,
'ETH',
);
// Polygon
_registerChain(
const ChainConfig(
chainId: 137,
name: 'Polygon',
shortCode: 'polygon',
caip2: 'eip155:137',
nativeCurrency: CryptoCurrency.maticpoly,
capabilities: ChainCapabilities(
supportsERC20: true,
supportsEIP1559: true,
supportsInternalTx: true,
supportsSubscriptions: false,
supportsENS: false,
),
defaultRpcEndpoints: [
'polygon-rpc.com',
'polygon-bor-rpc.publicnode.com',
'polygon.llamarpc.com',
],
explorerUrls: [
'https://polygonscan.com',
],
feeModel: FeeModel(
type: FeeType.eip1559,
defaultGasLimit: 21000,
),
),
WalletType.polygon,
'POL',
);
// Base
_registerChain(
const ChainConfig(
chainId: 8453,
name: 'Base',
shortCode: 'base',
caip2: 'eip155:8453',
nativeCurrency: CryptoCurrency.baseEth,
capabilities: ChainCapabilities(
supportsERC20: true,
supportsEIP1559: true,
supportsInternalTx: true,
supportsSubscriptions: false,
supportsENS: false,
),
defaultRpcEndpoints: [
'base.llamarpc.com',
'base-rpc.publicnode.com',
'1rpc.io/base',
],
explorerUrls: [
'https://basescan.org',
],
feeModel: FeeModel(
type: FeeType.eip1559,
defaultGasLimit: 21000,
),
),
WalletType.base,
'BASE',
);
// Arbitrum
_registerChain(
const ChainConfig(
chainId: 42161,
name: 'Arbitrum',
shortCode: 'arbitrum',
caip2: 'eip155:42161',
nativeCurrency: CryptoCurrency.arbEth,
capabilities: ChainCapabilities(
supportsERC20: true,
supportsEIP1559: true,
supportsInternalTx: true,
supportsSubscriptions: false,
supportsENS: false,
),
defaultRpcEndpoints: [
'arbitrum.drpc.org',
'arbitrum-one-rpc.publicnode.com',
],
explorerUrls: [
'https://arbiscan.io',
],
feeModel: FeeModel(
type: FeeType.eip1559,
defaultGasLimit: 21000,
),
),
WalletType.arbitrum,
'ARB',
);
// BNB Smart Chain
_registerChain(
const ChainConfig(
chainId: 56,
name: 'BNB Smart Chain',
shortCode: 'bsc',
caip2: 'eip155:56',
nativeCurrency: CryptoCurrency.bnb,
capabilities: ChainCapabilities(
supportsERC20: true,
supportsEIP1559: true,
supportsInternalTx: true,
supportsSubscriptions: false,
supportsENS: false,
),
defaultRpcEndpoints: [
'bsc-dataseed1.binance.org',
'bsc-dataseed2.binance.org',
'bsc-dataseed1.defibit.io',
'bsc-dataseed1.nodereal.io',
'bsc.llamarpc.com',
'bsc-rpc.publicnode.com',
],
explorerUrls: [
'https://bscscan.com',
],
feeModel: FeeModel(
type: FeeType.eip1559,
defaultGasLimit: 21000,
),
),
WalletType.bsc,
'BSC',
);
}
void _registerChain(
ChainConfig config,
WalletType walletType,
String tag,
) {
_chains[config.chainId] = config;
_walletTypeToChainId[walletType] = config.chainId;
_chainIdToWalletType[config.chainId] = walletType;
_tagToChainId[tag.toUpperCase()] = config.chainId;
_caip2ToChainId[config.caip2] = config.chainId;
}
ChainConfig? getChainConfig(int chainId) => _chains[chainId];
ChainConfig? getChainConfigByWalletType(WalletType walletType) {
final chainId = _walletTypeToChainId[walletType];
return chainId != null ? _chains[chainId] : null;
}
/// Get chain configuration by tag (e.g., 'ETH', 'POL', 'BASE', 'ARB')
ChainConfig? getChainConfigByTag(String tag) {
final chainId = _tagToChainId[tag.toUpperCase()];
return chainId != null ? _chains[chainId] : null;
}
/// Get chain configuration by CAIP-2 identifier (e.g. 'eip155:1')
ChainConfig? getChainConfigByCaip2(String caip2) {
final chainId = _caip2ToChainId[caip2];
return chainId != null ? _chains[chainId] : null;
}
WalletType? getWalletTypeByChainId(int chainId) => _chainIdToWalletType[chainId];
int? getChainIdByWalletType(WalletType walletType) => _walletTypeToChainId[walletType];
bool isChainRegistered(int chainId) => _chains.containsKey(chainId);
List<int> getRegisteredChainIds() => _chains.keys.toList();
List<ChainConfig> getAllChains() => _chains.values.toList();
List<WalletType> getRegisteredWalletTypes() => _walletTypeToChainId.keys.toList();
}