Using MetaMask as an injected provider
MetaMask and the Mist browser can be used as a provider for the web3
object. These services expose the web3
API and allow your application to connect to an Ethereum node. In this recipe, you will learn to use Mist or MetaMask as an injected provider for your application, along with other options.
Getting ready
You need to have the MetaMask extension installed in your browser to try this recipe. MetaMask supports Chrome, Firefox, Opera, and Brave browsers. Make sure that you are connected to your network of choice in MetaMask.
These scripts will also work with the Mist browser.
How to do it...
Now, we will start with the steps that are needed to use MetaMask as an injected provider:
- Initialize
web3
using the provider object injected by the browser providers:
// For v0.2x.x var web3 = new Web3(web3.currentProvider); // For v1.x.x varweb3=newWeb3(Web3.givenProvider);
- Check for existing providers to ensure that you are not overwriting the existing Mist or MetaMask...