This repository has been archived by the owner on Aug 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Update metamask-inpage-provider; remove old functionality (#6)
* update inpage-provider * remove functionality now handled by inpage-provider * add dev build scripts * 1.0.1
- Loading branch information
Showing
13 changed files
with
450 additions
and
293 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
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 +1,2 @@ | ||
node_modules/ | ||
node_modules/ | ||
*.log |
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 @@ | ||
v10.16.3 |
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
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
Large diffs are not rendered by default.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,113 +1,33 @@ | ||
|
||
const MetamaskInpageProvider = require('metamask-inpage-provider') | ||
const createStandardProvider = require('./createStandardProvider').default | ||
const ObjectMultiplex = require('obj-multiplex') | ||
const pump = require('pump') | ||
const MobilePortStream = require('./MobilePortStream') | ||
const ReactNativePostMessageStream = require('./ReactNativePostMessageStream') | ||
|
||
let warned = false | ||
|
||
const metamaskStream = new ReactNativePostMessageStream({ | ||
name: 'inpage', | ||
target: 'contentscript', | ||
}) | ||
|
||
window.inpageProvider = new MetamaskInpageProvider(metamaskStream) | ||
|
||
// compose the inpage provider | ||
// set a high max listener count to avoid unnecesary warnings | ||
window.inpageProvider.setMaxListeners(100) | ||
function setupProvider () { | ||
|
||
let warnedOfAutoRefreshDeprecation = false | ||
// augment the provider with its enable method | ||
window.inpageProvider.enable = function ({ force } = {}) { | ||
if ( | ||
!warnedOfAutoRefreshDeprecation && | ||
window.inpageProvider.autoRefreshOnNetworkChange | ||
) { | ||
console.warn(`MetaMask: MetaMask will soon stop reloading pages on network change. | ||
If you rely upon this behavior, add a 'networkChanged' event handler to trigger the reload manually: https://metamask.github.io/metamask-docs/API_Reference/Ethereum_Provider#ethereum.on(eventname%2C-callback) | ||
Set 'ethereum.autoRefreshOnNetworkChange' to 'false' to silence this warning: https://metamask.github.io/metamask-docs/API_Reference/Ethereum_Provider#ethereum.autorefreshonnetworkchange' | ||
`) | ||
warnedOfAutoRefreshDeprecation = true | ||
} | ||
return new Promise((resolve, reject) => { | ||
window.inpageProvider.sendAsync({ method: 'eth_requestAccounts', params: (force && [force] || []) }, (error, response) => { | ||
if (error || response.error) { | ||
reject(error || response.error) | ||
} else { | ||
resolve(response.result) | ||
} | ||
}) | ||
}) | ||
} | ||
const inpageProvider = new MetamaskInpageProvider(metamaskStream, false) | ||
|
||
// give the dapps control of a refresh they can toggle this off on the window.ethereum | ||
// this will be default true so it does not break any old apps. | ||
window.inpageProvider.autoRefreshOnNetworkChange = true | ||
// compose the inpage provider | ||
// set a high max listener count to avoid unnecesary warnings | ||
inpageProvider.setMaxListeners(100) | ||
|
||
|
||
const getPublicConfigWhenReady = async () => { | ||
const store = window.inpageProvider.publicConfigStore | ||
let state = store.getState() | ||
// if state is missing, wait for first update | ||
if (!state.networkVersion) { | ||
state = await new Promise(resolve => store.once('update', resolve)) | ||
} | ||
return state | ||
// Work around for [email protected] deleting the bound `sendAsync` but not the unbound | ||
// `sendAsync` method on the prototype, causing `this` reference issues | ||
window.ethereum = new Proxy(inpageProvider, { | ||
// straight up lie that we deleted the property so that it doesnt | ||
// throw an error in strict mode | ||
deleteProperty: () => true, | ||
}) | ||
} | ||
|
||
// add metamask-specific convenience methods | ||
window.inpageProvider._metamask = new Proxy({ | ||
/** | ||
* Synchronously determines if this domain is currently enabled, with a potential false negative if called to soon | ||
* | ||
* @returns {boolean} - returns true if this domain is currently enabled | ||
*/ | ||
isEnabled: function () { | ||
const { isEnabled } = window.inpageProvider.publicConfigStore.getState() | ||
return Boolean(isEnabled) | ||
}, | ||
|
||
/** | ||
* Asynchronously determines if this domain is currently enabled | ||
* | ||
* @returns {Promise<boolean>} - Promise resolving to true if this domain is currently enabled | ||
*/ | ||
isApproved: async function () { | ||
const { isEnabled } = await getPublicConfigWhenReady() | ||
return Boolean(isEnabled) | ||
}, | ||
|
||
/** | ||
* Determines if MetaMask is unlocked by the user | ||
* | ||
* @returns {Promise<boolean>} - Promise resolving to true if MetaMask is currently unlocked | ||
*/ | ||
isUnlocked: async function () { | ||
const { isUnlocked } = await getPublicConfigWhenReady() | ||
return Boolean(isUnlocked) | ||
}, | ||
}, { | ||
get: function (obj, prop) { | ||
!warned && console.warn('Heads up! ethereum._metamask exposes methods that have ' + | ||
'not been standardized yet. This means that these methods may not be implemented ' + | ||
'in other dapp browsers and may be removed from MetaMask in the future.') | ||
warned = true | ||
return obj[prop] | ||
}, | ||
}) | ||
|
||
// Work around for [email protected] deleting the bound `sendAsync` but not the unbound | ||
// `sendAsync` method on the prototype, causing `this` reference issues | ||
window.proxiedInpageProvider = new Proxy(window.inpageProvider, { | ||
// straight up lie that we deleted the property so that it doesnt | ||
// throw an error in strict mode | ||
deleteProperty: () => true, | ||
}) | ||
|
||
window.ethereum = createStandardProvider(window.proxiedInpageProvider) | ||
setupProvider() | ||
|
||
window.setupStreams = function () { | ||
// the transport-specific streams for communication between inpage and background | ||
|
@@ -120,7 +40,7 @@ window.setupStreams = function () { | |
name: 'contentscript', | ||
}) | ||
|
||
// create and connect channel muxers | ||
// create and connect channel muxes | ||
// so we can handle the channels individually | ||
const pageMux = new ObjectMultiplex() | ||
pageMux.setMaxListeners(25) | ||
|
@@ -141,12 +61,18 @@ window.setupStreams = function () { | |
) | ||
|
||
// forward communication across inpage-background for these channels only | ||
forwardTrafficBetweenMuxers('provider', pageMux, appMux) | ||
forwardTrafficBetweenMuxers('publicConfig', pageMux, appMux) | ||
|
||
forwardTrafficBetweenMuxes('provider', pageMux, appMux) | ||
forwardTrafficBetweenMuxes('publicConfig', pageMux, appMux) | ||
} | ||
|
||
function forwardTrafficBetweenMuxers (channelName, muxA, muxB) { | ||
/** | ||
* Set up two-way communication between muxes for a single, named channel. | ||
* | ||
* @param {string} channelName - The name of the channel. | ||
* @param {ObjectMultiplex} muxA - The first mux. | ||
* @param {ObjectMultiplex} muxB - The second mux. | ||
*/ | ||
function forwardTrafficBetweenMuxes (channelName, muxA, muxB) { | ||
const channelA = muxA.createStream(channelName) | ||
const channelB = muxB.createStream(channelName) | ||
pump( | ||
|
@@ -160,8 +86,8 @@ function forwardTrafficBetweenMuxers (channelName, muxA, muxB) { | |
/** | ||
* Error handler for page to extension stream disconnections | ||
* | ||
* @param {string} remoteLabel Remote stream name | ||
* @param {Error} err Stream connection error | ||
* @param {string} remoteLabel - Remote stream name | ||
* @param {Error} err - Stream connection error | ||
*/ | ||
function logStreamDisconnectWarning (remoteLabel, err) { | ||
let warningMsg = `MetamaskContentscript - lost connection to ${remoteLabel}` | ||
|
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
Oops, something went wrong.