Skip to content

Commit

Permalink
Check that network exists when adding a custom network (#3802)
Browse files Browse the repository at this point in the history
* check network exists before adding a new network (use chainId)

* updated the network exist message

* replace the logic to use rpcurl instead of chainId

* updated warning text

* updated flash message
  • Loading branch information
blackdevelopa authored Mar 7, 2022
1 parent 2bf3fe0 commit ee82077
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,20 @@ class NetworkSettings extends PureComponent {
return true;
};

checkIfNetworkExists = async (rpcUrl) => {
const checkCustomNetworks = this.props.frequentRpcList.filter((item) => item.rpcUrl === rpcUrl);
if (checkCustomNetworks.length > 0) {
this.setState({ warningRpcUrl: strings('app_settings.network_exists') });
return checkCustomNetworks;
}
const defaultNetworks = getAllNetworks().map((item) => Networks[item]);
const checkDefaultNetworks = defaultNetworks.filter((item) => Number(item.rpcUrl) === rpcUrl);
if (checkDefaultNetworks.length > 0) {
return checkDefaultNetworks;
}
return [];
};

/**
* Add rpc url and parameters to PreferencesController
* Setting NetworkController provider to this custom rpc
Expand All @@ -254,6 +268,8 @@ class NetworkSettings extends PureComponent {
const { navigation } = this.props;

const formChainId = stateChainId.trim().toLowerCase();

const isNetworkExists = await this.checkIfNetworkExists(rpcUrl);
// Ensure chainId is a 0x-prefixed, lowercase hex string
let chainId = formChainId;
if (!chainId.startsWith('0x')) {
Expand All @@ -264,7 +280,7 @@ class NetworkSettings extends PureComponent {
return;
}

if (this.validateRpcUrl()) {
if (this.validateRpcUrl() && isNetworkExists.length === 0) {
const url = new URL(rpcUrl);
const decimalChainId = this.getDecimalChainId(chainId);
!isprivateConnection(url.hostname) && url.set('protocol', 'https:');
Expand Down Expand Up @@ -292,8 +308,9 @@ class NetworkSettings extends PureComponent {
* Validates rpc url, setting a warningRpcUrl if is invalid
* It also changes validatedRpcURL to true, indicating that was validated
*/
validateRpcUrl = () => {
validateRpcUrl = async () => {
const { rpcUrl } = this.state;
const isNetworkExists = await this.checkIfNetworkExists(rpcUrl);
if (!isWebUri(rpcUrl)) {
const appendedRpc = `http://${rpcUrl}`;
if (isWebUri(appendedRpc)) {
Expand All @@ -303,6 +320,10 @@ class NetworkSettings extends PureComponent {
}
return false;
}

if (isNetworkExists.length > 0) {
return this.setState({ validatedRpcURL: true, warningRpcUrl: strings('app_settings.network_exists') });
}
const url = new URL(rpcUrl);
const privateConnection = isprivateConnection(url.hostname);
if (!privateConnection && url.protocol === 'http:') {
Expand Down
3 changes: 2 additions & 1 deletion locales/languages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,8 @@
"hide_zero_balance_tokens_title": "Hide Tokens Without Balance",
"hide_zero_balance_tokens_desc": "Prevents tokens with no balance from displaying in your token listing.",
"token_detection_title": "Enhanced Token Detection",
"token_detection_description": "We use third-party APIs to detect and display new tokens sent to your wallet. Turn off if you don’t want the app to pull data from those services."
"token_detection_description": "We use third-party APIs to detect and display new tokens sent to your wallet. Turn off if you don’t want the app to pull data from those services.",
"network_exists": "This network has already been added."
},
"app_information": {
"title": "Information",
Expand Down

0 comments on commit ee82077

Please sign in to comment.