-
-
Notifications
You must be signed in to change notification settings - Fork 126
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add global initialization event (#31)
* add global initialization event * add init function Co-authored-by: Mark Stacey <[email protected]>
- Loading branch information
Showing
3 changed files
with
85 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 +1,8 @@ | ||
module.exports = require('./src/MetamaskInpageProvider') | ||
const MetamaskInpageProvider = require('./src/MetamaskInpageProvider') | ||
const { initProvider, setGlobalProvider } = require('./src/initProvider') | ||
|
||
module.exports = { | ||
MetamaskInpageProvider, | ||
initProvider, | ||
setGlobalProvider, | ||
} |
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,59 @@ | ||
const MetamaskInpageProvider = require('./MetamaskInpageProvider') | ||
|
||
/** | ||
* Initializes a MetamaskInpageProvider and (optionally) sets it on window.ethereum. | ||
* | ||
* @param {Object} opts - An options bag. | ||
* @param {Object} opts.connectionStream - A Node.js stream. | ||
* @param {number} opts.maxEventListeners - The maximum number of event listeners. | ||
* @param {boolean} opts.preventPropertyDeletion - Whether to wrap the provider in a proxy that prevents property deletion. | ||
* @param {boolean} opts.shouldSendMetadata - Whether the provider should send page metadata. | ||
* @param {boolean} opts.shouldSetOnWindow - Whether the provider should be set as window.ethereum | ||
* @returns {MetamaskInpageProvider | Proxy} The initialized provider (whether set or not). | ||
*/ | ||
function initProvider ({ | ||
connectionStream, | ||
maxEventListeners = 100, | ||
preventPropertyDeletion = true, | ||
shouldSendMetadata = true, | ||
shouldSetOnWindow = true, | ||
}) { | ||
|
||
if (!connectionStream) { | ||
throw new Error('Must provide a connection stream.') | ||
} | ||
|
||
let provider = new MetamaskInpageProvider( | ||
connectionStream, { shouldSendMetadata, maxEventListeners }, | ||
) | ||
|
||
if (preventPropertyDeletion) { | ||
// Workaround for [email protected] deleting the bound `sendAsync` but not the unbound | ||
// `sendAsync` method on the prototype, causing `this` reference issues | ||
provider = new Proxy(provider, { | ||
deleteProperty: () => true, | ||
}) | ||
} | ||
|
||
if (shouldSetOnWindow) { | ||
setGlobalProvider(provider) | ||
} | ||
|
||
return provider | ||
} | ||
|
||
/** | ||
* Sets the given provider instance as window.ethereum and dispatches the | ||
* 'ethereum#initialized' event on window. | ||
* | ||
* @param {MetamaskInpageProvider} providerInstance - The provider instance. | ||
*/ | ||
function setGlobalProvider (providerInstance) { | ||
window.ethereum = providerInstance | ||
window.dispatchEvent(new Event('ethereum#initialized')) | ||
} | ||
|
||
module.exports = { | ||
initProvider, | ||
setGlobalProvider, | ||
} |