Assuming you need this to confirm that you are on the expected blockchain, and considering a blockchain is a decentralised network of nodes, not sure whether knowing the IP of the node you are connected to will tell you any relevant information.
On the other hand, the chain id will indeed tell whether you are on the mainnet (1), a public testnet (3, 4, 42...) or a local blockchain (1337, ...).
Once said that and application-wise, the IP is commonly defined at the moment of defining the provider, for instance:
const providers = [
new Web3(new Web3.providers.HttpProvider('http://127.0.0.1:7545')),
new Web3(new Web3.providers.HttpProvider(`https://ropsten.infura.io/v3/${infuraKey}`)),
];
Nevertheless, since a blockchain keeps the same global state across all nodes, there shouldn't be differences when reading data from one node or another (different IPs) within the same network.
Here is what I use:
export function connectMetaMask(chainId) {
if (!window.ethereum && isMobile()) connectMetamaskMobile();
else return metaMaskConnector.activate(chainId);
}
export const connectMetamaskMobile = () => {
const dappUrl = window.location.href.split("//")[1].split("/")[0];
const metamaskAppDeepLink = "https://metamask.app.link/dapp/" + dappUrl;
window.open(metamaskAppDeepLink, "_self");
};
This is described in the MetaMask Docs.
I'm using this together with web3-react, however you only need the connectMetaMaskMobile
part. Check this out if you want more info on detecting mobile browsers.
Best Answer
I found this Metamask git for create deep links:
https://metamask.github.io/metamask-deeplinks/
https://github.com/MetaMask/metamask-deeplinks