From 1816a4e7773c0b22b748f9f30ff856a075091633 Mon Sep 17 00:00:00 2001 From: Sylva Elendu Date: Wed, 23 Feb 2022 07:40:11 +0100 Subject: [PATCH 1/5] check network exists before adding a new network (use chainId) --- .../NetworksSettings/NetworkSettings/index.js | 18 +++++++++++++++++- locales/languages/en.json | 3 ++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js b/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js index f735dc292f8..339ccd4617e 100644 --- a/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js +++ b/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js @@ -243,6 +243,20 @@ class NetworkSettings extends PureComponent { return true; }; + checkIfChainIdExists = async (chainId) => { + const checkCustomNetworks = this.props.frequentRpcList.filter((item) => item.chainId === chainId); + if (checkCustomNetworks.length > 0) { + this.setState({ warningChainId: strings('app_settings.chain_id_exists') }); + return checkCustomNetworks; + } + const defaultNetworksChainIds = getAllNetworks().map((item) => Networks[item]); + const checkDefaultNetworks = defaultNetworksChainIds.filter((item) => Number(item.chainId) === Number(chainId)); + if (checkDefaultNetworks.length > 0) { + return checkDefaultNetworks; + } + return []; + }; + /** * Add rpc url and parameters to PreferencesController * Setting NetworkController provider to this custom rpc @@ -254,6 +268,8 @@ class NetworkSettings extends PureComponent { const { navigation } = this.props; const formChainId = stateChainId.trim().toLowerCase(); + + const isChainIdExists = await this.checkIfChainIdExists(formChainId); // Ensure chainId is a 0x-prefixed, lowercase hex string let chainId = formChainId; if (!chainId.startsWith('0x')) { @@ -264,7 +280,7 @@ class NetworkSettings extends PureComponent { return; } - if (this.validateRpcUrl()) { + if (this.validateRpcUrl() && isChainIdExists.length === 0) { const url = new URL(rpcUrl); const decimalChainId = this.getDecimalChainId(chainId); !isprivateConnection(url.hostname) && url.set('protocol', 'https:'); diff --git a/locales/languages/en.json b/locales/languages/en.json index ce917487129..b78d59e4d5f 100644 --- a/locales/languages/en.json +++ b/locales/languages/en.json @@ -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.", + "chain_id_exists": "This chain ID already exists." }, "app_information": { "title": "Information", From cc207fc8af0ee729d1b1d5a2aa6858bc39cbc662 Mon Sep 17 00:00:00 2001 From: Sylva Elendu Date: Wed, 2 Mar 2022 19:09:25 +0100 Subject: [PATCH 2/5] updated the network exist message --- locales/languages/en.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/locales/languages/en.json b/locales/languages/en.json index b78d59e4d5f..5d2b6034982 100644 --- a/locales/languages/en.json +++ b/locales/languages/en.json @@ -551,7 +551,7 @@ "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.", - "chain_id_exists": "This chain ID already exists." + "chain_id_exists": "This chain ID has already been added." }, "app_information": { "title": "Information", From 26c256b6e33c1d81d9a33c3d3955b9e1fb68b3aa Mon Sep 17 00:00:00 2001 From: Sylva Elendu Date: Thu, 3 Mar 2022 14:59:18 +0100 Subject: [PATCH 3/5] replace the logic to use rpcurl instead of chainId --- .../NetworksSettings/NetworkSettings/index.js | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js b/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js index 339ccd4617e..aecfc9a0671 100644 --- a/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js +++ b/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js @@ -243,14 +243,14 @@ class NetworkSettings extends PureComponent { return true; }; - checkIfChainIdExists = async (chainId) => { - const checkCustomNetworks = this.props.frequentRpcList.filter((item) => item.chainId === chainId); + checkIfNetworkExists = async (rpcUrl) => { + const checkCustomNetworks = this.props.frequentRpcList.filter((item) => item.rpcUrl === rpcUrl); if (checkCustomNetworks.length > 0) { - this.setState({ warningChainId: strings('app_settings.chain_id_exists') }); + this.setState({ warningRpcUrl: strings('app_settings.chain_id_exists') }); return checkCustomNetworks; } - const defaultNetworksChainIds = getAllNetworks().map((item) => Networks[item]); - const checkDefaultNetworks = defaultNetworksChainIds.filter((item) => Number(item.chainId) === Number(chainId)); + const defaultNetworks = getAllNetworks().map((item) => Networks[item]); + const checkDefaultNetworks = defaultNetworks.filter((item) => Number(item.rpcUrl) === rpcUrl); if (checkDefaultNetworks.length > 0) { return checkDefaultNetworks; } @@ -269,7 +269,7 @@ class NetworkSettings extends PureComponent { const formChainId = stateChainId.trim().toLowerCase(); - const isChainIdExists = await this.checkIfChainIdExists(formChainId); + const isChainIdExists = await this.checkIfNetworkExists(rpcUrl); // Ensure chainId is a 0x-prefixed, lowercase hex string let chainId = formChainId; if (!chainId.startsWith('0x')) { From 39aeddcda696e8cc0809957b9d520d77eb3355b6 Mon Sep 17 00:00:00 2001 From: Sylva Elendu Date: Thu, 3 Mar 2022 19:25:34 +0100 Subject: [PATCH 4/5] updated warning text --- .../Views/Settings/NetworksSettings/NetworkSettings/index.js | 2 +- locales/languages/en.json | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js b/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js index aecfc9a0671..29efe63d0a5 100644 --- a/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js +++ b/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js @@ -246,7 +246,7 @@ class NetworkSettings extends PureComponent { checkIfNetworkExists = async (rpcUrl) => { const checkCustomNetworks = this.props.frequentRpcList.filter((item) => item.rpcUrl === rpcUrl); if (checkCustomNetworks.length > 0) { - this.setState({ warningRpcUrl: strings('app_settings.chain_id_exists') }); + this.setState({ warningRpcUrl: strings('app_settings.network_exists') }); return checkCustomNetworks; } const defaultNetworks = getAllNetworks().map((item) => Networks[item]); diff --git a/locales/languages/en.json b/locales/languages/en.json index 5d2b6034982..dd728f23bae 100644 --- a/locales/languages/en.json +++ b/locales/languages/en.json @@ -551,7 +551,7 @@ "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.", - "chain_id_exists": "This chain ID has already been added." + "network_exists": "This network has already been added." }, "app_information": { "title": "Information", From 22a36849271e09e5aa6bcde6120de662feb59886 Mon Sep 17 00:00:00 2001 From: Sylva Elendu Date: Fri, 4 Mar 2022 20:10:56 +0100 Subject: [PATCH 5/5] updated flash message --- .../NetworksSettings/NetworkSettings/index.js | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js b/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js index 29efe63d0a5..0bfddd3384c 100644 --- a/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js +++ b/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js @@ -269,7 +269,7 @@ class NetworkSettings extends PureComponent { const formChainId = stateChainId.trim().toLowerCase(); - const isChainIdExists = await this.checkIfNetworkExists(rpcUrl); + const isNetworkExists = await this.checkIfNetworkExists(rpcUrl); // Ensure chainId is a 0x-prefixed, lowercase hex string let chainId = formChainId; if (!chainId.startsWith('0x')) { @@ -280,7 +280,7 @@ class NetworkSettings extends PureComponent { return; } - if (this.validateRpcUrl() && isChainIdExists.length === 0) { + if (this.validateRpcUrl() && isNetworkExists.length === 0) { const url = new URL(rpcUrl); const decimalChainId = this.getDecimalChainId(chainId); !isprivateConnection(url.hostname) && url.set('protocol', 'https:'); @@ -308,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)) { @@ -319,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:') {