Skip to content

Commit

Permalink
fix: use shared jsonRpcConnection
Browse files Browse the repository at this point in the history
  • Loading branch information
shanejonas committed Apr 28, 2021
1 parent 83bfde8 commit 3c1b3a3
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 24 deletions.
21 changes: 15 additions & 6 deletions src/BaseProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -68,6 +69,12 @@ export interface BaseProviderState {
isPermanentlyDisconnected: boolean;
}

export interface JsonRpcConnection {
events: SafeEventEmitter;
middleware: JsonRpcMiddleware<unknown, unknown>;
stream: _Readable.Duplex;
}

export default class BaseProvider extends SafeEventEmitter {

protected readonly _log: ConsoleLike;
Expand All @@ -76,6 +83,8 @@ export default class BaseProvider extends SafeEventEmitter {

protected _rpcEngine: JsonRpcEngine;

protected _jsonRpcConnection: JsonRpcConnection;

protected static _defaultState: BaseProviderState = {
accounts: null,
isConnected: false,
Expand Down Expand Up @@ -161,25 +170,25 @@ 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'),
);

// handle RPC requests via dapp-side rpc engine
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);
Expand Down Expand Up @@ -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);
}
Expand Down
28 changes: 10 additions & 18 deletions src/MetaMaskInpageProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ interface SentWarningsState {

export default class MetaMaskInpageProvider extends BaseProvider {

protected sentWarnings: SentWarningsState = {
protected _sentWarnings: SentWarningsState = {
// methods
enable: false,
experimentalMethods: false,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}
}

Expand All @@ -235,9 +227,9 @@ export default class MetaMaskInpageProvider extends BaseProvider {
* @returns A promise that resolves to an array of addresses.
*/
enable(): Promise<string[]> {
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<string[]>((resolve, reject) => {
Expand Down Expand Up @@ -287,9 +279,9 @@ export default class MetaMaskInpageProvider extends BaseProvider {
send<T>(payload: SendSyncJsonRpcRequest): JsonRpcResponse<T>;

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 (
Expand Down Expand Up @@ -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);
},
Expand Down

0 comments on commit 3c1b3a3

Please sign in to comment.