Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
(dispatch) => {
const { network, serverAddress } = getNetwork(env);
const config = { nethash: 'net' };
if (network === 'customNode') {
config.nodes = [serverAddress];
const liskAPIClient = new Lisk.APIClient(config.nodes, { nethash: config.nethash });
// loadingStarted('getConstants');
liskAPIClient.node.getConstants().then((response) => {
// loadingFinished('getConstants');
config.nethash = response.data.nethash;
dispatch(peerSet(config));
}).catch(() => {
// loadingFinished('getConstants');
// dispatch(errorToastDisplayed({ label: i18next.t('Unable to connect to the node') }));
});
} else if (network === 'testnet') {
config.nodes = Lisk.APIClient.constants.TESTNET_NODES;
config.nethash = Lisk.APIClient.constants.TESTNET_NETHASH;
dispatch(peerSet(config));
} else if (network === 'mainnet') {
config.nodes = Lisk.APIClient.constants.MAINNET_NODES;
config.nethash = Lisk.APIClient.constants.MAINNET_NETHASH;
config.nodes = [config.address];
} else if (config.testnet) {
config.nethash = Lisk.APIClient.constants.TESTNET_NETHASH;
config.nodes = networks.testnet.nodes;
} else {
config.nethash = Lisk.APIClient.constants.MAINNET_NETHASH;
config.nodes = networks.mainnet.nodes;
}
// TODO calling token-agnostic action inside LSK action is hacky, should be refactored
dispatch(networkSet({
...data.network,
nodeUrl: data.network.address,
}));
if (config.custom) {
const liskAPIClient = new Lisk.APIClient(config.nodes, {});
loadingStarted('getConstants');
liskAPIClient.node.getConstants().then((response) => {
dispatch(loadingFinished('getConstants'));
config.nethash = response.data.nethash;
dispatch(peerSet(data, config));
if (data.passphrase || data.hwInfo) {
login(data, config)(dispatch, getState);
}
}).catch((error) => {
dispatch(loadingFinished('getConstants'));
dispatch(errorToastDisplayed({
label: getConnectionErrorMessage(error),
}));
});
} else {
dispatch(peerSet(data, config));
if (network.name && (!apiClients[network.name] || network.name === networks.customNode.name)) {
const { nethash, nodes } = {
[networks.testnet.name]: {
nethash: Lisk.APIClient.constants.TESTNET_NETHASH,
nodes: networks.testnet.nodes,
},
[networks.mainnet.name]: {
nethash: Lisk.APIClient.constants.MAINNET_NETHASH,
nodes: networks.mainnet.nodes,
},
[networks.customNode.name]: {
nethash: network.networks[tokenMap.LSK.key] && network.networks[tokenMap.LSK.key].nethash,
nodes: [network.networks[tokenMap.LSK.key] && network.networks[tokenMap.LSK.key].nodeUrl],
},
}[network.name] || {};
apiClients[network.name] = new Lisk.APIClient(nodes, { nethash });
apiClients[network.name].networkConfig = network;
}
return apiClients[network.name];
};
validateCorrectNode(network, address, nextPath) {
const nodeURL = address !== '' ? addHttp(address) : '';
const newNetwork = this.getNetwork(network);
if (network === networks.customNode.code) {
const liskAPIClient = new Lisk.APIClient([nodeURL], {});
liskAPIClient.node.getConstants()
.then((res) => {
if (res.data) {
this.props.networkSet({
name: newNetwork.name,
network: {
...newNetwork,
},
});
this.props.history.push(nextPath);
this.setState({ validationError: '', connected: true });
this.childRef.toggleDropdown();
this.changeNetwork(networks.customNode.code);
} else {
throw new Error();
const peerSet = config => ({
data: new Lisk.APIClient(config.nodes, { nethash: config.nethash }),
type: actionTypes.activePeerSet,
});
const peerSet = (data, config) => ({
data: {
passphrase: data.passphrase,
publicKey: data.publicKey,
liskAPIClient: new Lisk.APIClient(config.nodes, { nethash: config.nethash }),
options: config,
loginType: data.loginType,
},
type: actionTypes.liskAPIClientSet,
});
new Promise(async (resolve, reject) => {
new Lisk.APIClient([nodeUrl], {}).node.getConstants().then((response) => {
resolve(response.data.nethash);
}).catch((error) => {
reject(getConnectionErrorMessage(error));
});
})
);