1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00

Allow locally hosted RPC and Block Explorer Urls with wallet_addEthereumChain (#14272)

* add function to check if url is localhost

* allow localhost rpcUrls in `wallet_addEthereumChain`

* allow localhost blockExplorerUrls

* wrap new URL in try/catch
This commit is contained in:
Micaiah Reid 2022-06-10 10:53:36 -04:00 committed by Dan Miller
parent 850c68ebb0
commit 1fd4dfbde4

View File

@ -77,14 +77,27 @@ async function addEthereumChainHandler(
);
}
const isLocalhost = (strUrl) => {
try {
const url = new URL(strUrl);
return url.hostname === 'localhost' || url.hostname === '127.0.0.1';
} catch (error) {
return false;
}
};
const firstValidRPCUrl = Array.isArray(rpcUrls)
? rpcUrls.find((rpcUrl) => validUrl.isHttpsUri(rpcUrl))
? rpcUrls.find(
(rpcUrl) => isLocalhost(rpcUrl) || validUrl.isHttpsUri(rpcUrl),
)
: null;
const firstValidBlockExplorerUrl =
blockExplorerUrls !== null && Array.isArray(blockExplorerUrls)
? blockExplorerUrls.find((blockExplorerUrl) =>
validUrl.isHttpsUri(blockExplorerUrl),
? blockExplorerUrls.find(
(blockExplorerUrl) =>
isLocalhost(blockExplorerUrl) ||
validUrl.isHttpsUri(blockExplorerUrl),
)
: null;