From 3c1b3a39bd6ccad6436cc2b24f22d2e9cd4da8a6 Mon Sep 17 00:00:00 2001 From: shanejonas Date: Wed, 28 Apr 2021 11:49:12 -0700 Subject: [PATCH] fix: use shared jsonRpcConnection --- src/BaseProvider.ts | 21 +++++++++++++++------ src/MetaMaskInpageProvider.ts | 28 ++++++++++------------------ 2 files changed, 25 insertions(+), 24 deletions(-) diff --git a/src/BaseProvider.ts b/src/BaseProvider.ts index 1e74ac5d..118e5073 100644 --- a/src/BaseProvider.ts +++ b/src/BaseProvider.ts @@ -7,6 +7,7 @@ import { JsonRpcId, JsonRpcVersion, JsonRpcSuccess, + JsonRpcMiddleware, } from 'json-rpc-engine'; import { createStreamMiddleware } from 'json-rpc-middleware-stream'; import ObjectMultiplex from '@metamask/object-multiplex'; @@ -68,6 +69,12 @@ export interface BaseProviderState { isPermanentlyDisconnected: boolean; } +export interface JsonRpcConnection { + events: SafeEventEmitter; + middleware: JsonRpcMiddleware; + stream: _Readable.Duplex; +} + export default class BaseProvider extends SafeEventEmitter { protected readonly _log: ConsoleLike; @@ -76,6 +83,8 @@ export default class BaseProvider extends SafeEventEmitter { protected _rpcEngine: JsonRpcEngine; + protected _jsonRpcConnection: JsonRpcConnection; + protected static _defaultState: BaseProviderState = { accounts: null, isConnected: false, @@ -161,11 +170,11 @@ export default class BaseProvider extends SafeEventEmitter { // setup RPC connection - const jsonRpcConnection = createStreamMiddleware(); + this._jsonRpcConnection = createStreamMiddleware(); pump( - jsonRpcConnection.stream, + this._jsonRpcConnection.stream, mux.createStream(jsonRpcStreamName) as unknown as Duplex, - // jsonRpcConnection.stream, + this._jsonRpcConnection.stream, this._handleStreamDisconnect.bind(this, 'MetaMask RpcProvider'), ); @@ -173,13 +182,13 @@ export default class BaseProvider extends SafeEventEmitter { const rpcEngine = new JsonRpcEngine(); rpcEngine.push(createIdRemapMiddleware()); rpcEngine.push(createErrorMiddleware(this._log)); - rpcEngine.push(jsonRpcConnection.middleware); + rpcEngine.push(this._jsonRpcConnection.middleware); this._rpcEngine = rpcEngine; this._initializeState(); // handle JSON-RPC notifications - jsonRpcConnection.events.on('notification', (payload) => { + this._jsonRpcConnection.events.on('notification', (payload) => { const { method, params } = payload; if (method === 'metamask_accountsChanged') { this._handleAccountsChanged(params); @@ -494,7 +503,7 @@ export default class BaseProvider extends SafeEventEmitter { this.selectedAddress = _accounts[0] as string || null; } - // finally, after all state has be/.senen updated, emit the event + // finally, after all state has been updated, emit the event if (this._state.initialized) { this.emit('accountsChanged', _accounts); } diff --git a/src/MetaMaskInpageProvider.ts b/src/MetaMaskInpageProvider.ts index 712862b8..1c9370ae 100644 --- a/src/MetaMaskInpageProvider.ts +++ b/src/MetaMaskInpageProvider.ts @@ -44,7 +44,7 @@ interface SentWarningsState { export default class MetaMaskInpageProvider extends BaseProvider { - protected sentWarnings: SentWarningsState = { + protected _sentWarnings: SentWarningsState = { // methods enable: false, experimentalMethods: false, @@ -104,16 +104,8 @@ export default class MetaMaskInpageProvider extends BaseProvider { this._metamask = this._getExperimentalApi(); - // TODO: figure out where to put this - // ignore phishing warning message (handled elsewhere) - // mux.ignoreStream('phishing'); - - // setup RPC connection - - const jsonRpcConnection = createStreamMiddleware(); - // handle JSON-RPC notifications - jsonRpcConnection.events.on('notification', (payload) => { + this._jsonRpcConnection.events.on('notification', (payload) => { const { method } = payload; if (EMITTED_NOTIFICATIONS.includes(method)) { // deprecated @@ -218,9 +210,9 @@ export default class MetaMaskInpageProvider extends BaseProvider { * Warns of deprecation for the given event, if applicable. */ protected _warnOfDeprecation(eventName: string): void { - if (this.sentWarnings && this.sentWarnings.events[eventName as WarningEventName] === false) { + if (this._sentWarnings?.events[eventName as WarningEventName] === false) { this._log.warn(messages.warnings.events[eventName as WarningEventName]); - this.sentWarnings.events[eventName as WarningEventName] = true; + this._sentWarnings.events[eventName as WarningEventName] = true; } } @@ -235,9 +227,9 @@ export default class MetaMaskInpageProvider extends BaseProvider { * @returns A promise that resolves to an array of addresses. */ enable(): Promise { - if (!this.sentWarnings.enable) { + if (!this._sentWarnings.enable) { this._log.warn(messages.warnings.enableDeprecation); - this.sentWarnings.enable = true; + this._sentWarnings.enable = true; } return new Promise((resolve, reject) => { @@ -287,9 +279,9 @@ export default class MetaMaskInpageProvider extends BaseProvider { send(payload: SendSyncJsonRpcRequest): JsonRpcResponse; send(methodOrPayload: unknown, callbackOrArgs?: unknown): unknown { - if (!this.sentWarnings.send) { + if (!this._sentWarnings.send) { this._log.warn(messages.warnings.sendDeprecation); - this.sentWarnings.send = true; + this._sentWarnings.send = true; } if ( @@ -401,9 +393,9 @@ export default class MetaMaskInpageProvider extends BaseProvider { { get: (obj, prop, ...args) => { - if (!this.sentWarnings.experimentalMethods) { + if (!this._sentWarnings.experimentalMethods) { this._log.warn(messages.warnings.experimentalMethods); - this.sentWarnings.experimentalMethods = true; + this._sentWarnings.experimentalMethods = true; } return Reflect.get(obj, prop, ...args); },