diff --git a/packages/controllers/src/idCounter.ts b/packages/controllers/src/idCounter.ts deleted file mode 100644 index c4111e3025..0000000000 --- a/packages/controllers/src/idCounter.ts +++ /dev/null @@ -1,6 +0,0 @@ -let id = 1; - -export const getId = () => { - id = id + 1; - return id; -}; diff --git a/packages/controllers/src/plugins/PluginController.ts b/packages/controllers/src/plugins/PluginController.ts index 8a1bd5e5ff..7192a31754 100644 --- a/packages/controllers/src/plugins/PluginController.ts +++ b/packages/controllers/src/plugins/PluginController.ts @@ -3,12 +3,11 @@ import EventEmitter from '@metamask/safe-event-emitter'; import { ethErrors, serializeError } from 'eth-rpc-errors'; import { IOcapLdCapability } from 'rpc-cap/dist/src/@types/ocap-ld'; import { IRequestedPermissions } from 'rpc-cap/dist/src/@types'; -import { JsonRpcResponse } from 'json-rpc-engine'; +import { nanoid } from 'nanoid'; import { WorkerController, SetupWorkerConnection, } from '../workers/WorkerController'; -import { getId } from '../idCounter'; import { INLINE_PLUGINS } from './inlinePlugins'; export const PLUGIN_PREFIX = 'wallet_plugin_'; @@ -40,7 +39,7 @@ export interface Plugin extends SerializablePlugin { export type PluginRpcHook = ( origin: string, request: Record, -) => Promise>; +) => Promise; export type ProcessPluginReturnType = | SerializablePlugin @@ -744,7 +743,7 @@ export class PluginController extends EventEmitter { request: Record, ) => { return await this.workerController.command(workerId, { - id: getId(), + id: nanoid(), jsonrpc: '2.0', method: 'pluginRpc', params: { diff --git a/packages/controllers/src/workers/WorkerController.ts b/packages/controllers/src/workers/WorkerController.ts index a98575193f..05366530cc 100644 --- a/packages/controllers/src/workers/WorkerController.ts +++ b/packages/controllers/src/workers/WorkerController.ts @@ -11,11 +11,9 @@ import { PluginData } from '@mm-snap/types'; import { JsonRpcEngine, JsonRpcRequest, - JsonRpcResponse, + PendingJsonRpcResponse, } from 'json-rpc-engine'; -import { getId } from '../idCounter'; - export type SetupWorkerConnection = (metadata: any, stream: Duplex) => void; interface PluginWorkerMetadata { @@ -82,7 +80,10 @@ export class WorkerController extends SafeEventEmitter { this.store.updateState({ workers: newWorkerState }); } - async command(workerId: string, message: JsonRpcRequest): Promise { + async command( + workerId: string, + message: JsonRpcRequest, + ): Promise { if (typeof message !== 'object') { throw new Error('Must send object.'); } @@ -93,7 +94,9 @@ export class WorkerController extends SafeEventEmitter { } console.log('Parent: Sending Command', message); - const response: any = await workerObj.rpcEngine.handle(message); + const response: PendingJsonRpcResponse = await workerObj.rpcEngine.handle( + message, + ); if (response.error) { throw new Error(response.error.message); } @@ -134,7 +137,7 @@ export class WorkerController extends SafeEventEmitter { async startPlugin( workerId: string, pluginData: PluginData, - ): Promise> { + ): Promise { const _workerId: string = workerId || this.workers.keys().next()?.value(); if (!_workerId) { throw new Error('No workers available.'); @@ -146,7 +149,7 @@ export class WorkerController extends SafeEventEmitter { jsonrpc: '2.0', method: 'installPlugin', params: pluginData, - id: getId(), + id: nanoid(), }); } @@ -211,7 +214,7 @@ export class WorkerController extends SafeEventEmitter { await this.command(workerId, { jsonrpc: '2.0', method: 'ping', - id: getId(), + id: nanoid(), }); return workerId; } diff --git a/packages/workers/package.json b/packages/workers/package.json index b83f8c21e8..8a915aceff 100644 --- a/packages/workers/package.json +++ b/packages/workers/package.json @@ -31,6 +31,7 @@ "@types/pump": "^1.1.0", "@types/readable-stream": "^2.3.9", "browserify": "16.2.3", + "json-rpc-engine": "^6.1.0", "pump": "^3.0.0", "ses": "^0.11.0", "tinyify": "^3.0.0", diff --git a/packages/workers/src/PluginWorker.ts b/packages/workers/src/PluginWorker.ts index aec15cb63f..650f3bf491 100644 --- a/packages/workers/src/PluginWorker.ts +++ b/packages/workers/src/PluginWorker.ts @@ -4,6 +4,7 @@ import ObjectMultiplex from '@metamask/object-multiplex'; import pump from 'pump'; import { WorkerPostMessageStream } from '@metamask/post-message-stream'; import { PluginData, PluginProvider } from '@mm-snap/types'; +import type { JsonRpcId, JsonRpcRequest } from 'json-rpc-engine'; import { STREAM_NAMES } from './enums'; // eslint-disable-next-line import/no-unassigned-import @@ -75,7 +76,7 @@ lockdown({ this.rpcStream = mux.createStream(STREAM_NAMES.JSON_RPC) as any; } - private async onCommandRequest(message: any) { + private async onCommandRequest(message: JsonRpcRequest) { if (!message || typeof message !== 'object' || Array.isArray(message)) { console.error('Command stream received non-object message.'); return; @@ -83,7 +84,7 @@ lockdown({ const { id, method, params } = message; - if (!id && typeof id !== 'string' && typeof id !== 'number') { + if (typeof id !== 'string' && typeof id !== 'number') { console.error(`Command stream received invalid id "${id}".`); return; } @@ -112,7 +113,7 @@ lockdown({ } } - private respond(id: string, responseObj: Record) { + private respond(id: JsonRpcId, responseObj: Record) { this.commandStream.write({ ...responseObj, id, @@ -121,7 +122,7 @@ lockdown({ } private async handlePluginRpc( - id: string, + id: JsonRpcId, { origin, request, target }: PluginRpcRequest, ) { const handler = this.pluginRpcHandlers.get(target); @@ -142,7 +143,7 @@ lockdown({ } private installPlugin( - id: string, + id: JsonRpcId, { pluginName, sourceCode }: Partial = {}, ) { if (!isTruthyString(pluginName) || !isTruthyString(sourceCode)) {