From 9e2e353a245da242f52425ff5b95aeb8073bbb1f Mon Sep 17 00:00:00 2001 From: Mark Stacey Date: Thu, 21 May 2020 00:18:25 -0300 Subject: [PATCH] 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. --- app/scripts/inpage.js | 7 ++++++- app/scripts/lib/auto-reload.js | 9 ++++++++- 2 files changed, 14 insertions(+), 2 deletions(-) diff --git a/app/scripts/inpage.js b/app/scripts/inpage.js index e9e64bb68..672502286 100644 --- a/app/scripts/inpage.js +++ b/app/scripts/inpage.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) diff --git a/app/scripts/lib/auto-reload.js b/app/scripts/lib/auto-reload.js index ceb7e5e3f..ce13ef0ca 100644 --- a/app/scripts/lib/auto-reload.js +++ b/app/scripts/lib/auto-reload.js @@ -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