Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add global initialization event #31

Merged
merged 4 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion index.js
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,
}
19 changes: 18 additions & 1 deletion src/MetamaskInpageProvider.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,29 @@ const {

module.exports = class MetamaskInpageProvider extends SafeEventEmitter {

constructor (connectionStream, shouldSendMetadata = true) {
/**
* @param {Object} connectionStream - A Node.js stream
* @param {Object} opts - An options bag
* @param {number} opts.maxEventListeners - The maximum number of event listeners
* @param {boolean} opts.shouldSendMetadata - Whether the provider should send page metadata
*/
constructor (
connectionStream,
{ shouldSendMetadata = true, maxEventListeners = 100 },
) {

if (
typeof shouldSendMetadata !== 'boolean' || typeof maxEventListeners !== 'number'
) {
throw new Error('Invalid options.')
}

super()

this.isMetaMask = true

this.setMaxListeners(maxEventListeners)

// private state, kept here in part for use in the _metamask proxy
this._state = {
sentWarnings: {
Expand Down
59 changes: 59 additions & 0 deletions src/initProvider.js
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 web3@1.0 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,
}