Define global `web3` as non-enumerable (#8634)

We inject `web3` globally on most websites. This has been breaking
websites that attempted to serialize the `window` object, because any
attempt to access certain `web3` properties (such as `web3.eth.mining`)
would throw an error. This is because `web3` defined a getter for these
properties that would call `.send([method])`, which doesn't work for
most methods.

An example of a site that this breaks is `Storybook`, when the
`@storybook/addon-actions` addon is being used. When using storybook
with this addon and with the MetaMask extension installed, actions
would not be properly dispatched because an error would be thrown in
the attempt to serialize the event (which includes a reference to the
`window`).

The `web3` global we inject is now defined as non-enumerable, so it
will be skipped automatically in any attempt to serialize the `window`
object.
feature/default_network_editable
Mark Stacey 5 years ago committed by GitHub
parent ec99cc82b8
commit 9e2e353a24
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 7
      app/scripts/inpage.js
  2. 9
      app/scripts/lib/auto-reload.js

@ -79,7 +79,12 @@ web3.setProvider = function () {
}
log.debug('MetaMask - injected web3')
window.ethereum._web3Ref = web3.eth
Object.defineProperty(window.ethereum, '_web3Ref', {
enumerable: false,
writable: true,
configurable: true,
value: web3.eth,
})
// setup dapp auto reload AND proxy web3
setupDappAutoReload(web3, window.ethereum._publicConfigStore)

@ -8,7 +8,7 @@ export default function setupDappAutoReload (web3, observable) {
let lastSeenNetwork
let hasBeenWarned = false
global.web3 = new Proxy(web3, {
const web3Proxy = new Proxy(web3, {
get: (_web3, key) => {
// get the time of use
lastTimeUsed = Date.now()
@ -26,6 +26,13 @@ export default function setupDappAutoReload (web3, observable) {
},
})
Object.defineProperty(global, 'web3', {
enumerable: false,
writable: true,
configurable: true,
value: web3Proxy,
})
observable.subscribe(function (state) {
// if the auto refresh on network change is false do not
// do anything

Loading…
Cancel
Save