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
Changes from 1 commit
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
Next Next commit
check network exists before adding a new network (use chainId)
blackdevelopa committed Mar 3, 2022
commit 1816a4e7773c0b22b748f9f30ff856a075091633
Original file line number Diff line number Diff line change
@@ -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:');
3 changes: 2 additions & 1 deletion locales/languages/en.json
Original file line number Diff line number Diff line change
@@ -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",