HenryNguyen5 01fc5f1a89 Move Nodes/Networks to Redux (#961)
* Start splitting networks into their own reducers

* Split out nodes and networks into their own reducers

* Cleanup file structure

* Make selectors for new state

* Change custom network typing

* re-type repo

* Fix up components to use selectors, work on fixing sagas

* Provide consistency in naming, fix more sagas

* Get non web3 node switching working

* Split config rehydration off into a different file for store

* Inline auth for custom nodes

* Include typing for app state

* moar selectors

* Get web3 working + cleanup sagas

* Cleanup tsc errors

* Use forof loop instead of foreach for clearing pruning custom networks

* Add reducer tests for new redux state

* Export needed variables

* Add console error

* Remove old comment

* Work on saga tests

* Get passing existing saga tests

* Fix more tests

* Remove irrlevant tests

* add console error

* Get rest of tests passing

* Fix merge errors

* Remove random text

* Fix store saving

* Fix selector lib only grabbing from static nodes

* Fix custom node removal crashing app

* Infer selected network via node

* Prune custom networks properly on node removal

* Infer network name from chainid from selecting state

* Cleanup tsc errors

* Remove MEW nodes for main and testnet
2018-02-12 14:43:07 -06:00

83 lines
2.6 KiB
TypeScript

import { InsecureWalletName, SecureWalletName, WalletName, walletNames } from 'config';
import { EXTRA_PATHS } from 'config/dpaths';
import sortedUniq from 'lodash/sortedUniq';
import difference from 'lodash/difference';
import { StaticNetworkConfig, DPathFormats } from 'types/network';
import { AppState } from 'reducers';
import { getStaticNetworkConfigs, getStaticNetworkConfig } from 'selectors/config';
type PathType = keyof DPathFormats;
type DPathFormat =
| SecureWalletName.TREZOR
| SecureWalletName.LEDGER_NANO_S
| InsecureWalletName.MNEMONIC_PHRASE;
export function getPaths(state: AppState, pathType: PathType): DPath[] {
const paths = Object.values(getStaticNetworkConfigs(state))
.reduce(
(networkPaths: DPath[], { dPathFormats }) =>
dPathFormats ? [...networkPaths, dPathFormats[pathType]] : networkPaths,
[]
)
.concat(EXTRA_PATHS);
return sortedUniq(paths);
}
export function getSingleDPath(state: AppState, format: DPathFormat): DPath {
const network = getStaticNetworkConfig(state);
if (!network) {
throw Error('No static network config loaded');
}
const dPathFormats = network.dPathFormats;
return dPathFormats[format];
}
export function isNetworkUnit(state: AppState, unit: string) {
const currentNetwork = getStaticNetworkConfig(state);
//TODO: logic check
if (!currentNetwork) {
return false;
}
const networks = getStaticNetworkConfigs(state);
const validNetworks = Object.values(networks).filter((n: StaticNetworkConfig) => n.unit === unit);
return validNetworks.includes(currentNetwork);
}
export function isWalletFormatSupportedOnNetwork(state: AppState, format: WalletName): boolean {
const network = getStaticNetworkConfig(state);
const CHECK_FORMATS: DPathFormat[] = [
SecureWalletName.LEDGER_NANO_S,
SecureWalletName.TREZOR,
InsecureWalletName.MNEMONIC_PHRASE
];
const isHDFormat = (f: string): f is DPathFormat => CHECK_FORMATS.includes(f as DPathFormat);
// Ensure DPath's are found
if (isHDFormat(format)) {
if (!network) {
return false;
}
const dPath = network.dPathFormats && network.dPathFormats[format];
return !!dPath;
}
// Ensure Web3 is only enabled on ETH or ETH Testnets (MetaMask does not support other networks)
if (format === SecureWalletName.WEB3) {
return isNetworkUnit(state, 'ETH');
}
// All other wallet formats are supported
return true;
}
export function unSupportedWalletFormatsOnNetwork(state: AppState): WalletName[] {
const supportedFormats = walletNames.filter(walletName =>
isWalletFormatSupportedOnNetwork(state, walletName)
);
return difference(walletNames, supportedFormats);
}