16

If I log the two functions, I get two objects, that both contain for example selectedAddress, but the structure of the objects seem different. I couldn't find a comparison in the documentation.

I did find out that window.web3.currentProviderand window.ethereum are equal, but then, what is window.web3 for?

Marcellvs
  • 423
  • 1
  • 4
  • 13

2 Answers2

9

As per the Metamask API documentation:

MetaMask injects a global API into websites visited by its users at window.ethereum (Also available at window.web3.currentProvider for legacy reasons). This API allows websites to request user login, load data from blockchains the user has a connection to, and suggest the user sign messages and transactions. You can use this API to detect the user of a web3 browser.


Edit:

You can see what window.web3 returns by opening a console in Chrome using "F12" (or whatever browser you're using), and typing window.web3:

> window.web3
Proxy {_requestManager: a, currentProvider: Proxy, eth: n, db: e.exports, shh: s, …}
[[Handler]]: Object
[[Target]]: n
bzz: e.exports {_requestManager: a, blockNetworkRead: ƒ, syncEnabled: ƒ, swapEnabled: ƒ, download: ƒ, …}
currentProvider: Proxy {selectedAddress: undefined, networkVersion: "4", _events: {…}, _eventsCount: 1, _maxListeners: 100, …}
db: e.exports {_requestManager: a, putString: ƒ, getString: ƒ, putHex: ƒ, getHex: ƒ}
eth: n {_requestManager: a, getBalance: ƒ, getStorageAt: ƒ, getCode: ƒ, getBlock: ƒ, …}
net: e.exports {_requestManager: a, getListening: ƒ, getPeerCount: ƒ}
personal: e.exports {_requestManager: a, newAccount: ƒ, importRawKey: ƒ, unlockAccount: ƒ, ecRecover: ƒ, …}
providers: {HttpProvider: ƒ, IpcProvider: ƒ}
setProvider: ƒ ()
settings: e.exports {defaultBlock: "latest", defaultAccount: undefined}
shh: s {_requestManager: a, version: ƒ, info: ƒ, setMaxMessageSize: ƒ, setMinPoW: ƒ, …}
version: {api: "0.20.3", getNode: ƒ, getNetwork: ƒ, …}
_extend: ƒ (e)
_requestManager: a {provider: Proxy, polls: {…}, timeout: null}
__proto__: Object
[[IsRevoked]]: false

The same can be done for window.ethereum.

Richard Horrocks
  • 37,835
  • 13
  • 87
  • 144
  • 1
    I read that before, but it doesn't say what exactly window.web3 returns. – Marcellvs Mar 13 '19 at 10:22
  • Edited my answer. Shout if that still doesn't help :-) – Richard Horrocks Mar 13 '19 at 11:03
  • I'm logging pretty much every object and command to learn, and as I mentioned in the post ("the structure of the objects seem different") I know what they look like, but I don't understand the purpose ob web3. Why the need for two different objects? – Marcellvs Mar 13 '19 at 14:17
  • 1
    The answer is in the Metamask documentation I quoted above. The original API injected window.web3.currentProvider. There was an upgrade that introduced window.ethereum, which was slightly different to the old API. This is why they are different. window.web3.currentProvider still exists "for legacy reasons", i.e. because some people will still be using it in their code. – Richard Horrocks Mar 13 '19 at 14:35
  • Then I rephrase my question to: what's the difference between window.web3.currentProviderand window.web3. And why isn't there window.ethereum.currentProvider? Does the old version offer more functionality? – Marcellvs Mar 13 '19 at 15:40
  • 1
    Ah, okay, I understand what you're asking now :-) window.web3.currentProvider basically provided a subset of information relating to window.web3. By instantiating a Web3() object you would get access to everything. That same information is now available from window.ethereum (I think with some minor differences, hence the upgrade.) You can still get access to everything by instantiating Web3() in the same way (e.g. window.web3 = new Web3(ethereum); rather than window.web3 = new Web3(web3.currentProvider);). – Richard Horrocks Mar 13 '19 at 16:07
  • Thank you for you answer. Sometimes it's not really made clear, why you should use a different class/object in a newer version, and seemingly, the objects injected by my metamask contained similar data, but I wasn't sure if they have slightly different purposes. – Marcellvs Mar 13 '19 at 16:30
  • Okay, did a bit of digging around. This article might help detail the changes that were made when the API changed: https://medium.com/metamask/https-medium-com-metamask-breaking-change-injecting-web3-7722797916a8 – Richard Horrocks Mar 13 '19 at 16:38
  • I actually skimmed that post a few weeks ago... So then I assume the new object can preserve privacy much better. It's interesting that .currentProvider isn't necessary anymore. – Marcellvs Mar 13 '19 at 22:43
2

web3 is an older implementation that still some clients can use.

window.ethereum is new spec

https://github.com/MetaMask/metamask-extension/issues/8077

Yilmaz
  • 1,580
  • 10
  • 24