diff --git a/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js b/app/components/Views/Settings/NetworksSettings/NetworkSettings/index.js index f735dc292f8..0bfddd3384c 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; }; + 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 @@ -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')) { @@ -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:'); @@ -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)) { @@ -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:') { diff --git a/locales/languages/en.json b/locales/languages/en.json index ce917487129..dd728f23bae 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.", + "network_exists": "This network has already been added." }, "app_information": { "title": "Information",