diff options
author | Dan Finlay <542863+danfinlay@users.noreply.github.com> | 2019-07-16 07:28:04 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-16 07:28:04 +0800 |
commit | 78cdee23c0f9ba0a6e54bab0ddccc4df3abfe3d9 (patch) | |
tree | 22ce53689ae0ec16c9df93178cc5df3f6c80fb76 /app/scripts | |
parent | 32fffe53f5016b8fc4f2b78f4501fc271705b96d (diff) | |
download | tangerine-wallet-browser-78cdee23c0f9ba0a6e54bab0ddccc4df3abfe3d9.tar tangerine-wallet-browser-78cdee23c0f9ba0a6e54bab0ddccc4df3abfe3d9.tar.gz tangerine-wallet-browser-78cdee23c0f9ba0a6e54bab0ddccc4df3abfe3d9.tar.bz2 tangerine-wallet-browser-78cdee23c0f9ba0a6e54bab0ddccc4df3abfe3d9.tar.lz tangerine-wallet-browser-78cdee23c0f9ba0a6e54bab0ddccc4df3abfe3d9.tar.xz tangerine-wallet-browser-78cdee23c0f9ba0a6e54bab0ddccc4df3abfe3d9.tar.zst tangerine-wallet-browser-78cdee23c0f9ba0a6e54bab0ddccc4df3abfe3d9.zip |
Abstract domain provider from its stream transport (#6670)
* Abstract domain provider from its stream transport
Creating new provider-consuming extensions, like [a new
platform](https://github.com/MetaMask/metamask-extension/blob/develop/docs/porting_to_new_environment.md)
can be frustrating for new contributors because our provider
construction has been tangled with our streaming interface.
Here I've broken up our streaming domain connection from the provider
construction, so developers can more easily construct local and
domain-restricted providers without dealing with streams.
* Abstract public API from stream interface
* clean up noop
* Document non-streaming interface
* getSiteMetadata must be async
* Clean up filters on stream end
* Document cleaning up filters
* Allow named filterMiddleware to be cleaned up
* Linted
* Require site metadata
* Destroy any destroyable middleware during cleanup
* Lint
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/metamask-controller.js | 45 |
1 files changed, 30 insertions, 15 deletions
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 393154c60..17d38d43c 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1391,6 +1391,32 @@ module.exports = class MetamaskController extends EventEmitter { * @param {string} origin - The URI of the requesting resource. */ setupProviderConnection (outStream, origin, publicApi) { + const getSiteMetadata = publicApi && publicApi.getSiteMetadata + const engine = this.setupProviderEngine(origin, getSiteMetadata) + + // setup connection + const providerStream = createEngineStream({ engine }) + + pump( + outStream, + providerStream, + outStream, + (err) => { + // cleanup filter polyfill middleware + engine._middleware.forEach((mid) => { + if (mid.destroy && typeof mid.destroy === 'function') { + mid.destroy() + } + }) + if (err) log.error(err) + } + ) + } + + /** + * A method for creating a provider that is safely restricted for the requesting domain. + **/ + setupProviderEngine (origin, getSiteMetadata) { // setup json rpc engine stack const engine = new RpcEngine() const provider = this.provider @@ -1398,6 +1424,7 @@ module.exports = class MetamaskController extends EventEmitter { // create filter polyfill middleware const filterMiddleware = createFilterMiddleware({ provider, blockTracker }) + // create subscription polyfill middleware const subscriptionManager = createSubscriptionManager({ provider, blockTracker }) subscriptionManager.events.on('notification', (message) => engine.emit('notification', message)) @@ -1413,24 +1440,11 @@ module.exports = class MetamaskController extends EventEmitter { // requestAccounts engine.push(this.providerApprovalController.createMiddleware({ origin, - getSiteMetadata: publicApi && publicApi.getSiteMetadata, + getSiteMetadata, })) // forward to metamask primary provider engine.push(providerAsMiddleware(provider)) - - // setup connection - const providerStream = createEngineStream({ engine }) - - pump( - outStream, - providerStream, - outStream, - (err) => { - // cleanup filter polyfill middleware - filterMiddleware.destroy() - if (err) log.error(err) - } - ) + return engine } /** @@ -1797,3 +1811,4 @@ module.exports = class MetamaskController extends EventEmitter { return this.keyringController.setLocked() } } + |