-
-
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.
return provider from initProvider
- Loading branch information
Showing
3 changed files
with
81 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,55 @@ | ||
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.shouldSendMetadata - Whether the provider should send page metadata. | ||
* @param {Object} opts.proxyHandler - A proxy handler object. The provider is proxied if present. | ||
* @param {boolean} opts.shouldSet - Whether the provider should be set as window.ethereum | ||
* @returns {MetamaskInpageProvider | Proxy} The initialized provider (whether set or not). | ||
*/ | ||
function initProvider ({ | ||
connectionStream, | ||
shouldSendMetadata = true, | ||
maxEventListeners = 100, | ||
proxyHandler, | ||
shouldSet = true, | ||
}) { | ||
|
||
if (!connectionStream) { | ||
throw new Error('Must provide a connection stream.') | ||
} | ||
|
||
let provider = new MetamaskInpageProvider( | ||
connectionStream, { shouldSendMetadata, maxEventListeners }, | ||
) | ||
|
||
if (proxyHandler) { | ||
provider = new Proxy(provider, proxyHandler) | ||
} | ||
|
||
if (shouldSet) { | ||
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, | ||
} |