I am writing DApp in react, and basically I want to initialize web3, unlock account, and display it. Having web3
in my package.json
In the onload
I've tried to go following:
let Web3 = require('web3');
let instance = new Web3(window.web3.currentProvider);
First of all, I am completely missing the point of such a step. What do we do here? What is the difference between window.web3
and instance
now?
Second, what line should I use and why?
console.log(window.web3.eth.accounts[0]); // this?
console.log(instance.eth.accounts[0]); // or that?
I want to figure out browser-agnostic scheme, that would work on Metamask, Opera, and on mobile browsers like Cipher.
I've tried a bunch of schemes, on some platforms I'm missing web3.eth.accounts[0]
, but accounts can be availble via web3.eth.getAccounts()
I've looked at following, but still can't generalize:
- Better pattern to detect web3 default account when using metamask
- New web3 method to request account access from dapp browser, no request
I run into a problem that it's neither straightforward, nor documented process. To face such a problem, lets create a place with answer, detailing basics of init and unlock procedure, that should work in any browser, for any web3.
window.web3
represents a very different internal implementation of web3, whatever it is on a target browsing platform. and instantiation ofnew Web3(provider)
creates a client of some fixed version, that connects to the browser's web3 and works through it, but provides some consistent interface. Is that corect guess?