diff --git a/app/scripts/controllers/network/network-controller.js b/app/scripts/controllers/network/network-controller.js index 1e04530574b7..6faacea0335e 100644 --- a/app/scripts/controllers/network/network-controller.js +++ b/app/scripts/controllers/network/network-controller.js @@ -2,13 +2,18 @@ import { strict as assert } from 'assert'; import EventEmitter from 'events'; import { ComposedStore, ObservableStore } from '@metamask/obs-store'; import { JsonRpcEngine } from 'json-rpc-engine'; -import { providerFromEngine } from 'eth-json-rpc-middleware'; +import { + providerFromEngine, + providerFromMiddleware, +} from 'eth-json-rpc-middleware'; import log from 'loglevel'; import { createSwappableProxy, createEventEmitterProxy, } from 'swappable-obj-proxy'; import EthQuery from 'eth-query'; +import createFilterMiddleware from 'eth-json-rpc-filters'; +import createSubscriptionManager from 'eth-json-rpc-filters/subscriptionManager'; import { INFURA_PROVIDER_TYPES, BUILT_IN_NETWORKS, @@ -447,13 +452,30 @@ export default class NetworkController extends EventEmitter { } _setNetworkClient({ networkMiddleware, blockTracker }) { + const networkProvider = providerFromMiddleware(networkMiddleware); + const filterMiddleware = createFilterMiddleware({ + provider: networkProvider, + blockTracker, + }); + const subscriptionManager = createSubscriptionManager({ + provider: networkProvider, + blockTracker, + }); const metamaskMiddleware = createMetamaskMiddleware( this._baseProviderParams, ); + const engine = new JsonRpcEngine(); + subscriptionManager.events.on('notification', (message) => + engine.emit('notification', message), + ); + engine.push(filterMiddleware); + engine.push(subscriptionManager.middleware); engine.push(metamaskMiddleware); engine.push(networkMiddleware); + const provider = providerFromEngine(engine); + this._setProviderAndBlockTracker({ provider, blockTracker }); } diff --git a/app/scripts/controllers/permissions/specifications.js b/app/scripts/controllers/permissions/specifications.js index 4072398d12b9..3779a904a232 100644 --- a/app/scripts/controllers/permissions/specifications.js +++ b/app/scripts/controllers/permissions/specifications.js @@ -260,10 +260,12 @@ export const unrestrictedMethods = Object.freeze([ 'eth_signTypedData_v1', 'eth_signTypedData_v3', 'eth_signTypedData_v4', + 'eth_subscribe', 'eth_submitHashrate', 'eth_submitWork', 'eth_syncing', 'eth_uninstallFilter', + 'eth_unsubscribe', 'metamask_getProviderState', 'metamask_watchAsset', 'net_listening', diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 3257da4de33b..91441727b3fc 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -5,8 +5,6 @@ import { storeAsStream } from '@metamask/obs-store/dist/asStream'; import { JsonRpcEngine } from 'json-rpc-engine'; import { debounce } from 'lodash'; import createEngineStream from 'json-rpc-middleware-stream/engineStream'; -import createFilterMiddleware from 'eth-json-rpc-filters'; -import createSubscriptionManager from 'eth-json-rpc-filters/subscriptionManager'; import { providerAsMiddleware } from 'eth-json-rpc-middleware'; import KeyringController from 'eth-keyring-controller'; import { @@ -3734,21 +3732,19 @@ export default class MetamaskController extends EventEmitter { * @param {tabId} [options.tabId] - The tab ID of the sender - if the sender is within a tab */ setupProviderEngine({ origin, subjectType, sender, tabId }) { + const { provider } = this; + // setup json rpc engine stack const engine = new JsonRpcEngine(); - const { blockTracker, provider } = this; - - // create filter polyfill middleware - const filterMiddleware = createFilterMiddleware({ provider, blockTracker }); - // create subscription polyfill middleware - const subscriptionManager = createSubscriptionManager({ - provider, - blockTracker, + // forward notifications from network provider + provider.on('data', (error, message) => { + if (error) { + // This should never happen, this error parameter is never set + throw error; + } + engine.emit('notification', message); }); - subscriptionManager.events.on('notification', (message) => - engine.emit('notification', message), - ); if (isManifestV3) { engine.push(createDupeReqFilterMiddleware()); @@ -3919,9 +3915,6 @@ export default class MetamaskController extends EventEmitter { ); ///: END:ONLY_INCLUDE_IN - // filter and subscription polyfills - engine.push(filterMiddleware); - engine.push(subscriptionManager.middleware); if (subjectType !== SUBJECT_TYPES.INTERNAL) { // permissions engine.push(