Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check that network exists when adding a custom network #3802

Merged
merged 5 commits into from
Mar 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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