Last active
January 10, 2019 08:14
-
-
Save MaHu6/1f09420c4829abe65329765a061199fe to your computer and use it in GitHub Desktop.
get metamask info
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // check metamask exist | |
| if (typeof web3 !== 'undefined') { | |
| var currentProvider = web3.currentProvider; | |
| if(currentProvider){ | |
| if(currentProvider.isMetaMask){ | |
| console.log("currentProvider is Metamask"); | |
| if(currentProvider.isConnected()){ | |
| console.log("Connected Metamask"); | |
| var web3Instance = new Web3(); | |
| var currentAddress = web3.eth.accounts[0]; | |
| console.log("currentAddress:"+currentAddress); | |
| }else{ | |
| console.log("Disconnect Metamask"); | |
| } | |
| } | |
| }else{ | |
| console.log("currentProvider not exist"); | |
| } | |
| } else { | |
| console.log("Please use Metamask"); | |
| } | |
| // check network | |
| web3.version.getNetwork((err, netId) => { | |
| switch (netId) { | |
| case "1": | |
| console.log('This is mainnet'); | |
| break; | |
| case "3": | |
| console.log("This is Ropsten network"); | |
| break; | |
| default: | |
| console.log('This is an unknown network.'); | |
| } | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment