|
| 1 | +# Network Enablement Controller |
| 2 | + |
| 3 | +A MetaMask controller for managing network enablement state across different blockchain networks. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The NetworkEnablementController tracks which networks are enabled/disabled for the user and provides methods to toggle network states. It supports both EVM (EIP-155) and non-EVM networks like Solana. |
| 8 | + |
| 9 | +## Installation |
| 10 | + |
| 11 | +```bash |
| 12 | +npm install @metamask/network-enablement-controller |
| 13 | +``` |
| 14 | + |
| 15 | +```bash |
| 16 | +yarn add @metamask/network-enablement-controller |
| 17 | +``` |
| 18 | + |
| 19 | +## Usage |
| 20 | + |
| 21 | +### Basic Controller Usage |
| 22 | + |
| 23 | +```typescript |
| 24 | +import { NetworkEnablementController } from '@metamask/network-enablement-controller'; |
| 25 | + |
| 26 | +// Create controller instance |
| 27 | +const controller = new NetworkEnablementController({ |
| 28 | + messenger, |
| 29 | + state: { |
| 30 | + enabledNetworkMap: { |
| 31 | + eip155: { |
| 32 | + '0x1': true, // Ethereum mainnet enabled |
| 33 | + '0xa': false, // Optimism disabled |
| 34 | + }, |
| 35 | + solana: { |
| 36 | + 'solana:mainnet': true, |
| 37 | + }, |
| 38 | + }, |
| 39 | + }, |
| 40 | +}); |
| 41 | + |
| 42 | +// Enable a network |
| 43 | +controller.setEnabledNetwork('0x1'); // Hex format for EVM |
| 44 | +controller.setEnabledNetwork('eip155:1'); // CAIP-2 format for EVM |
| 45 | +controller.setEnabledNetwork('solana:mainnet'); // CAIP-2 format for Solana |
| 46 | + |
| 47 | +// Disable a network |
| 48 | +controller.setDisabledNetwork('0xa'); |
| 49 | + |
| 50 | +// Check if network is enabled |
| 51 | +const isEnabled = controller.isNetworkEnabled('0x1'); |
| 52 | + |
| 53 | +// Get all enabled networks for a namespace |
| 54 | +const evmNetworks = controller.getEnabledNetworksForNamespace('eip155'); |
| 55 | + |
| 56 | +// Get all enabled networks across all namespaces |
| 57 | +const allNetworks = controller.getAllEnabledNetworks(); |
| 58 | +``` |
| 59 | + |
| 60 | +### Using Selectors (Redux-style) |
| 61 | + |
| 62 | +The controller also provides selectors that can be used in Redux contexts or any state management system: |
| 63 | + |
| 64 | +```typescript |
| 65 | +import { |
| 66 | + selectIsNetworkEnabled, |
| 67 | + selectAllEnabledNetworks, |
| 68 | + selectEnabledNetworksForNamespace, |
| 69 | + selectEnabledEvmNetworks, |
| 70 | + selectEnabledSolanaNetworks, |
| 71 | +} from '@metamask/network-enablement-controller'; |
| 72 | + |
| 73 | +// Get controller state |
| 74 | +const state = controller.state; |
| 75 | + |
| 76 | +// Check if a specific network is enabled |
| 77 | +const isEthereumEnabled = selectIsNetworkEnabled('0x1')(state); |
| 78 | +const isSolanaEnabled = selectIsNetworkEnabled('solana:mainnet')(state); |
| 79 | + |
| 80 | +// Get all enabled networks across all namespaces |
| 81 | +const allEnabledNetworks = selectAllEnabledNetworks(state); |
| 82 | +// Returns: { eip155: ['0x1'], solana: ['solana:mainnet'] } |
| 83 | + |
| 84 | +// Get enabled networks for a specific namespace |
| 85 | +const evmNetworks = selectEnabledNetworksForNamespace('eip155')(state); |
| 86 | +const solanaNetworks = selectEnabledNetworksForNamespace('solana')(state); |
| 87 | + |
| 88 | +// Convenience selectors for specific network types |
| 89 | +const enabledEvmNetworks = selectEnabledEvmNetworks(state); |
| 90 | +const enabledSolanaNetworks = selectEnabledSolanaNetworks(state); |
| 91 | + |
| 92 | +// Get total count of enabled networks |
| 93 | +const totalEnabled = selectEnabledNetworksCount(state); |
| 94 | + |
| 95 | +// Check if any networks are enabled for a namespace |
| 96 | +const hasEvmNetworks = selectHasEnabledNetworksForNamespace('eip155')(state); |
| 97 | +``` |
| 98 | + |
| 99 | +## API Reference |
| 100 | + |
| 101 | +### Controller Methods |
| 102 | + |
| 103 | +#### `setEnabledNetwork(chainId: Hex | CaipChainId): void` |
| 104 | + |
| 105 | +Enables a network for the user. Accepts either Hex chain IDs (for EVM networks) or CAIP-2 chain IDs (for any blockchain network). |
| 106 | + |
| 107 | +#### `setDisabledNetwork(chainId: Hex | CaipChainId): void` |
| 108 | + |
| 109 | +Disables a network for the user. Prevents disabling the last remaining enabled network. |
| 110 | + |
| 111 | +#### `isNetworkEnabled(chainId: Hex | CaipChainId): boolean` |
| 112 | + |
| 113 | +Checks if a network is currently enabled. Returns false for unknown networks. |
| 114 | + |
| 115 | +#### `getEnabledNetworksForNamespace(namespace: CaipNamespace): string[]` |
| 116 | + |
| 117 | +Gets all enabled networks for a specific namespace. |
| 118 | + |
| 119 | +#### `getAllEnabledNetworks(): Record<CaipNamespace, string[]>` |
| 120 | + |
| 121 | +Gets all enabled networks across all namespaces. |
| 122 | + |
| 123 | +### Selectors |
| 124 | + |
| 125 | +#### `selectIsNetworkEnabled(chainId: Hex | CaipChainId)` |
| 126 | + |
| 127 | +Returns a selector function that checks if a specific network is enabled. |
| 128 | + |
| 129 | +#### `selectAllEnabledNetworks` |
| 130 | + |
| 131 | +Returns a selector function that gets all enabled networks across all namespaces. |
| 132 | + |
| 133 | +#### `selectEnabledNetworksForNamespace(namespace: CaipNamespace)` |
| 134 | + |
| 135 | +Returns a selector function that gets enabled networks for a specific namespace. |
| 136 | + |
| 137 | +#### `selectEnabledNetworksCount` |
| 138 | + |
| 139 | +Returns a selector function that gets the total count of enabled networks. |
| 140 | + |
| 141 | +#### `selectHasEnabledNetworksForNamespace(namespace: CaipNamespace)` |
| 142 | + |
| 143 | +Returns a selector function that checks if any networks are enabled for a namespace. |
| 144 | + |
| 145 | +#### `selectEnabledEvmNetworks` |
| 146 | + |
| 147 | +Returns a selector function that gets all enabled EVM networks. |
| 148 | + |
| 149 | +#### `selectEnabledSolanaNetworks` |
| 150 | + |
| 151 | +Returns a selector function that gets all enabled Solana networks. |
| 152 | + |
| 153 | +## Chain ID Formats |
| 154 | + |
| 155 | +The controller supports two chain ID formats: |
| 156 | + |
| 157 | +1. **Hex format**: Traditional EVM chain IDs (e.g., `'0x1'` for Ethereum mainnet) |
| 158 | +2. **CAIP-2 format**: Chain Agnostic Improvement Proposal format (e.g., `'eip155:1'` for Ethereum mainnet, `'solana:mainnet'` for Solana) |
| 159 | + |
| 160 | +## Network Types |
| 161 | + |
| 162 | +### EVM Networks (eip155 namespace) |
| 163 | + |
| 164 | +- Ethereum Mainnet: `'0x1'` or `'eip155:1'` |
| 165 | +- Optimism: `'0xa'` or `'eip155:10'` |
| 166 | +- Arbitrum One: `'0xa4b1'` or `'eip155:42161'` |
| 167 | + |
| 168 | +### Solana Networks (solana namespace) |
| 169 | + |
| 170 | +- Solana Mainnet: `'solana:mainnet'` |
| 171 | +- Solana Testnet: `'solana:testnet'` |
| 172 | + |
| 173 | +## State Persistence |
| 174 | + |
| 175 | +The controller state is automatically persisted and restored between sessions. The `enabledNetworkMap` is stored anonymously to protect user privacy. |
| 176 | + |
| 177 | +## Safety Features |
| 178 | + |
| 179 | +- **At least one network enabled**: The controller ensures at least one network is always enabled |
| 180 | +- **Unknown network protection**: Prevents enabling networks not configured in the system |
| 181 | +- **Exclusive mode**: When enabling non-popular networks, all other networks are disabled |
| 182 | +- **Last network protection**: Prevents disabling the last remaining enabled network |
0 commit comments