-
-
Notifications
You must be signed in to change notification settings - Fork 128
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
49 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,10 @@ | ||
const MetaMaskInpageProvider = require('./src/MetaMaskInpageProvider') | ||
const { initializeProvider, setGlobalProvider } = require('./src/initializeProvider') | ||
const shimWeb3 = require('./src/shimWeb3') | ||
|
||
module.exports = { | ||
MetaMaskInpageProvider, | ||
initializeProvider, | ||
MetaMaskInpageProvider, | ||
setGlobalProvider, | ||
shimWeb3, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
/** | ||
* If no existing window.web3 is found, this function injects a web3 "shim" to | ||
* not break dapps that rely on window.web3.currentProvider. | ||
* | ||
* @param {import('./MetaMaskInpageProvider')} provider - The provider to set as window.web3.currentProvider. | ||
*/ | ||
module.exports = function shimWeb3 (provider) { | ||
if (!window.web3) { | ||
const SHIM_IDENTIFIER = '__isMetaMaskShim__' | ||
const web3Shim = new Proxy( | ||
{ | ||
currentProvider: provider, | ||
[SHIM_IDENTIFIER]: true, | ||
}, | ||
{ | ||
get: (target, property, ...args) => { | ||
if (property === 'currentProvider') { | ||
console.warn( | ||
'You are accessing the MetaMask window.web3.currentProvider shim. Just use window.ethereum instead. For details, see: https://docs.metamask.io/guide/provider-migration.html#replacing-window-web3', | ||
) | ||
} else if (property !== SHIM_IDENTIFIER) { | ||
console.error( | ||
`MetaMask no longer injects web3. For details, see: https://docs.metamask.io/guide/provider-migration.html#replacing-window-web3`, | ||
) | ||
} | ||
return Reflect.get(target, property, ...args) | ||
}, | ||
}, | ||
) | ||
|
||
Object.defineProperty(window, 'web3', { | ||
value: Object.freeze(web3Shim), | ||
enumerable: false, | ||
configurable: true, | ||
writable: true, | ||
}) | ||
} | ||
} |