From 5a671e5200ed3254e124cc469b3f844ebd0f7c05 Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Thu, 24 Aug 2023 00:34:42 +0000 Subject: [PATCH 1/6] deps: eth-rpc-errors@^4.0.2 -> @metamask/rpc-errors@^5.1.1 --- package.json | 2 +- yarn.lock | 169 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 169 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 3ed90826..0a11dae6 100644 --- a/package.json +++ b/package.json @@ -38,9 +38,9 @@ }, "dependencies": { "@metamask/object-multiplex": "^1.1.0", + "@metamask/rpc-errors": "^5.1.1", "@metamask/safe-event-emitter": "^3.0.0", "detect-browser": "^5.2.0", - "eth-rpc-errors": "^4.0.2", "extension-port-stream": "^2.0.1", "fast-deep-equal": "^3.1.3", "is-stream": "^2.0.0", diff --git a/yarn.lock b/yarn.lock index 08f86916..6e495fd5 100644 --- a/yarn.lock +++ b/yarn.lock @@ -470,6 +470,48 @@ __metadata: languageName: node linkType: hard +"@ethereumjs/common@npm:^3.2.0": + version: 3.2.0 + resolution: "@ethereumjs/common@npm:3.2.0" + dependencies: + "@ethereumjs/util": ^8.1.0 + crc-32: ^1.2.0 + checksum: cb9cc11f5c868cb577ba611cebf55046e509218bbb89b47ccce010776dafe8256d70f8f43fab238aec74cf71f62601cd5842bc03a83261200802de365732a14b + languageName: node + linkType: hard + +"@ethereumjs/rlp@npm:^4.0.1": + version: 4.0.1 + resolution: "@ethereumjs/rlp@npm:4.0.1" + bin: + rlp: bin/rlp + checksum: 30db19c78faa2b6ff27275ab767646929207bb207f903f09eb3e4c273ce2738b45f3c82169ddacd67468b4f063d8d96035f2bf36f02b6b7e4d928eefe2e3ecbc + languageName: node + linkType: hard + +"@ethereumjs/tx@npm:^4.1.2": + version: 4.2.0 + resolution: "@ethereumjs/tx@npm:4.2.0" + dependencies: + "@ethereumjs/common": ^3.2.0 + "@ethereumjs/rlp": ^4.0.1 + "@ethereumjs/util": ^8.1.0 + ethereum-cryptography: ^2.0.0 + checksum: 87a3f5f2452cfbf6712f8847525a80c213210ed453c211c793c5df801fe35ecef28bae17fadd222fcbdd94277478a47e52d2b916a90a6b30cda21f1e0cdaee42 + languageName: node + linkType: hard + +"@ethereumjs/util@npm:^8.1.0": + version: 8.1.0 + resolution: "@ethereumjs/util@npm:8.1.0" + dependencies: + "@ethereumjs/rlp": ^4.0.1 + ethereum-cryptography: ^2.0.0 + micro-ftch: ^0.3.1 + checksum: 9ae5dee8f12b0faf81cd83f06a41560e79b0ba96a48262771d897a510ecae605eb6d84f687da001ab8ccffd50f612ae50f988ef76e6312c752897f462f3ac08d + languageName: node + linkType: hard + "@gar/promisify@npm:^1.1.3": version: 1.1.3 resolution: "@gar/promisify@npm:1.1.3" @@ -1017,6 +1059,7 @@ __metadata: "@metamask/eslint-config-nodejs": ^11.0.1 "@metamask/eslint-config-typescript": ^11.0.0 "@metamask/object-multiplex": ^1.1.0 + "@metamask/rpc-errors": ^5.1.1 "@metamask/safe-event-emitter": ^3.0.0 "@types/chrome": ^0.0.233 "@types/jest": ^28.1.6 @@ -1035,7 +1078,6 @@ __metadata: eslint-plugin-jsdoc: ^39.6.2 eslint-plugin-node: ^11.1.0 eslint-plugin-prettier: ^4.2.1 - eth-rpc-errors: ^4.0.2 extension-port-stream: ^2.0.1 fast-deep-equal: ^3.1.3 is-stream: ^2.0.0 @@ -1057,6 +1099,16 @@ __metadata: languageName: unknown linkType: soft +"@metamask/rpc-errors@npm:^5.1.1": + version: 5.1.1 + resolution: "@metamask/rpc-errors@npm:5.1.1" + dependencies: + "@metamask/utils": ^5.0.0 + fast-safe-stringify: ^2.0.6 + checksum: ccd1b24da66af3ae63960b79c04b86efb8b96acb89ca6f7e0bbfe636d23ba5cddeba533c0692eafb87c44ec6f840085372d0f21b39e05df9a80700ff61538a30 + languageName: node + linkType: hard + "@metamask/safe-event-emitter@npm:^2.0.0": version: 2.0.0 resolution: "@metamask/safe-event-emitter@npm:2.0.0" @@ -1071,6 +1123,42 @@ __metadata: languageName: node linkType: hard +"@metamask/utils@npm:^5.0.0": + version: 5.0.2 + resolution: "@metamask/utils@npm:5.0.2" + dependencies: + "@ethereumjs/tx": ^4.1.2 + "@types/debug": ^4.1.7 + debug: ^4.3.4 + semver: ^7.3.8 + superstruct: ^1.0.3 + checksum: eca82e42911b2840deb4f32f0f215c5ffd14d22d68afbbe92d3180e920e509e310777b15eab29def3448f3535b66596ceb4c23666ec846adacc8e1bb093ff882 + languageName: node + linkType: hard + +"@noble/curves@npm:1.1.0, @noble/curves@npm:~1.1.0": + version: 1.1.0 + resolution: "@noble/curves@npm:1.1.0" + dependencies: + "@noble/hashes": 1.3.1 + checksum: 2658cdd3f84f71079b4e3516c47559d22cf4b55c23ac8ee9d2b1f8e5b72916d9689e59820e0f9d9cb4a46a8423af5b56dc6bb7782405c88be06a015180508db5 + languageName: node + linkType: hard + +"@noble/hashes@npm:1.3.1": + version: 1.3.1 + resolution: "@noble/hashes@npm:1.3.1" + checksum: 7fdefc0f7a0c1ec27acc6ff88841793e3f93ec4ce6b8a6a12bfc0dd70ae6b7c4c82fe305fdfeda1735d5ad4a9eebe761e6693b3d355689c559e91242f4bc95b1 + languageName: node + linkType: hard + +"@noble/hashes@npm:~1.3.0, @noble/hashes@npm:~1.3.1": + version: 1.3.2 + resolution: "@noble/hashes@npm:1.3.2" + checksum: fe23536b436539d13f90e4b9be843cc63b1b17666a07634a2b1259dded6f490be3d050249e6af98076ea8f2ea0d56f578773c2197f2aa0eeaa5fba5bc18ba474 + languageName: node + linkType: hard + "@nodelib/fs.scandir@npm:2.1.5": version: 2.1.5 resolution: "@nodelib/fs.scandir@npm:2.1.5" @@ -1161,6 +1249,34 @@ __metadata: languageName: node linkType: hard +"@scure/base@npm:~1.1.0": + version: 1.1.1 + resolution: "@scure/base@npm:1.1.1" + checksum: b4fc810b492693e7e8d0107313ac74c3646970c198bbe26d7332820886fa4f09441991023ec9aa3a2a51246b74409ab5ebae2e8ef148bbc253da79ac49130309 + languageName: node + linkType: hard + +"@scure/bip32@npm:1.3.1": + version: 1.3.1 + resolution: "@scure/bip32@npm:1.3.1" + dependencies: + "@noble/curves": ~1.1.0 + "@noble/hashes": ~1.3.1 + "@scure/base": ~1.1.0 + checksum: 394d65f77a40651eba21a5096da0f4233c3b50d422864751d373fcf142eeedb94a1149f9ab1dbb078086dab2d0bc27e2b1afec8321bf22d4403c7df2fea5bfe2 + languageName: node + linkType: hard + +"@scure/bip39@npm:1.2.1": + version: 1.2.1 + resolution: "@scure/bip39@npm:1.2.1" + dependencies: + "@noble/hashes": ~1.3.0 + "@scure/base": ~1.1.0 + checksum: c5bd6f1328fdbeae2dcdd891825b1610225310e5e62a4942714db51066866e4f7bef242c7b06a1b9dcc8043a4a13412cf5c5df76d3b10aa9e36b82e9b6e3eeaa + languageName: node + linkType: hard + "@sinclair/typebox@npm:^0.24.1": version: 0.24.51 resolution: "@sinclair/typebox@npm:0.24.51" @@ -1314,6 +1430,15 @@ __metadata: languageName: node linkType: hard +"@types/debug@npm:^4.1.7": + version: 4.1.8 + resolution: "@types/debug@npm:4.1.8" + dependencies: + "@types/ms": "*" + checksum: a9a9bb40a199e9724aa944e139a7659173a9b274798ea7efbc277cb084bc37d32fc4c00877c3496fac4fed70a23243d284adb75c00b5fdabb38a22154d18e5df + languageName: node + linkType: hard + "@types/filesystem@npm:*": version: 0.0.30 resolution: "@types/filesystem@npm:0.0.30" @@ -1413,6 +1538,13 @@ __metadata: languageName: node linkType: hard +"@types/ms@npm:*": + version: 0.7.31 + resolution: "@types/ms@npm:0.7.31" + checksum: daadd354aedde024cce6f5aa873fefe7b71b22cd0e28632a69e8b677aeb48ae8caa1c60e5919bb781df040d116b01cb4316335167a3fc0ef6a63fa3614c0f6da + languageName: node + linkType: hard + "@types/node@npm:*, @types/node@npm:^17.0.23": version: 17.0.45 resolution: "@types/node@npm:17.0.45" @@ -2439,6 +2571,15 @@ __metadata: languageName: node linkType: hard +"crc-32@npm:^1.2.0": + version: 1.2.2 + resolution: "crc-32@npm:1.2.2" + bin: + crc32: bin/crc32.njs + checksum: ad2d0ad0cbd465b75dcaeeff0600f8195b686816ab5f3ba4c6e052a07f728c3e70df2e3ca9fd3d4484dc4ba70586e161ca5a2334ec8bf5a41bf022a6103ff243 + languageName: node + linkType: hard + "create-require@npm:^1.1.0": version: 1.1.1 resolution: "create-require@npm:1.1.1" @@ -3197,6 +3338,18 @@ __metadata: languageName: node linkType: hard +"ethereum-cryptography@npm:^2.0.0": + version: 2.1.2 + resolution: "ethereum-cryptography@npm:2.1.2" + dependencies: + "@noble/curves": 1.1.0 + "@noble/hashes": 1.3.1 + "@scure/bip32": 1.3.1 + "@scure/bip39": 1.2.1 + checksum: 2e8f7b8cc90232ae838ab6a8167708e8362621404d26e79b5d9e762c7b53d699f7520aff358d9254de658fcd54d2d0af168ff909943259ed27dc4cef2736410c + languageName: node + linkType: hard + "execa@npm:^5.0.0, execa@npm:^5.1.1": version: 5.1.1 resolution: "execa@npm:5.1.1" @@ -5082,6 +5235,13 @@ __metadata: languageName: node linkType: hard +"micro-ftch@npm:^0.3.1": + version: 0.3.1 + resolution: "micro-ftch@npm:0.3.1" + checksum: 0e496547253a36e98a83fb00c628c53c3fb540fa5aaeaf718438873785afd193244988c09d219bb1802984ff227d04938d9571ef90fe82b48bd282262586aaff + languageName: node + linkType: hard + "micromatch@npm:^4.0.4": version: 4.0.5 resolution: "micromatch@npm:4.0.5" @@ -6397,6 +6557,13 @@ __metadata: languageName: node linkType: hard +"superstruct@npm:^1.0.3": + version: 1.0.3 + resolution: "superstruct@npm:1.0.3" + checksum: 761790bb111e6e21ddd608299c252f3be35df543263a7ebbc004e840d01fcf8046794c274bcb351bdf3eae4600f79d317d085cdbb19ca05803a4361840cc9bb1 + languageName: node + linkType: hard + "supports-color@npm:^5.3.0": version: 5.5.0 resolution: "supports-color@npm:5.5.0" From edba9a9c940f38d41fd87644f5d4159efd27e81a Mon Sep 17 00:00:00 2001 From: Frederik Bolding Date: Wed, 19 Apr 2023 17:15:33 +0200 Subject: [PATCH 2/6] Use metamask/json-rpc-engine and metamask/rpc-errors --- package.json | 3 +- src/BaseProvider.ts | 39 ++++++-------- src/MetaMaskInpageProvider.test.ts | 4 +- src/MetaMaskInpageProvider.ts | 27 +++++----- src/StreamProvider.test.ts | 5 +- src/StreamProvider.ts | 5 +- .../createExternalExtensionProvider.test.ts | 4 +- .../createRpcWarningMiddleware.test.ts | 7 +-- src/middleware/createRpcWarningMiddleware.ts | 5 +- src/siteMetadata.ts | 2 +- src/utils.ts | 16 +++--- test/mocks/MockConnectionStream.ts | 11 ++-- test/mocks/MockPort.ts | 20 +++---- yarn.lock | 53 +++++++++++-------- 14 files changed, 103 insertions(+), 98 deletions(-) diff --git a/package.json b/package.json index 0a11dae6..35aee272 100644 --- a/package.json +++ b/package.json @@ -37,14 +37,15 @@ "test:watch": "jest --watch" }, "dependencies": { + "@metamask/json-rpc-engine": "^7.0.0", "@metamask/object-multiplex": "^1.1.0", "@metamask/rpc-errors": "^5.1.1", "@metamask/safe-event-emitter": "^3.0.0", + "@metamask/utils": "^6.2.0", "detect-browser": "^5.2.0", "extension-port-stream": "^2.0.1", "fast-deep-equal": "^3.1.3", "is-stream": "^2.0.0", - "json-rpc-engine": "^6.1.0", "json-rpc-middleware-stream": "^4.2.1", "pump": "^3.0.0", "webextension-polyfill": "^0.10.0" diff --git a/src/BaseProvider.ts b/src/BaseProvider.ts index 3ab1e17d..db1459d1 100644 --- a/src/BaseProvider.ts +++ b/src/BaseProvider.ts @@ -1,14 +1,15 @@ +import { JsonRpcEngine, JsonRpcMiddleware } from '@metamask/json-rpc-engine'; +import { rpcErrors, JsonRpcError } from '@metamask/rpc-errors'; import SafeEventEmitter from '@metamask/safe-event-emitter'; -import { ethErrors, EthereumRpcError } from 'eth-rpc-errors'; -import dequal from 'fast-deep-equal'; import { - JsonRpcEngine, JsonRpcRequest, JsonRpcId, - JsonRpcVersion, + JsonRpcVersion2, JsonRpcSuccess, - JsonRpcMiddleware, -} from 'json-rpc-engine'; + JsonRpcParams, + Json, +} from '@metamask/utils'; +import dequal from 'fast-deep-equal'; import messages from './messages'; import { @@ -20,7 +21,7 @@ import { export type UnvalidatedJsonRpcRequest = { id?: JsonRpcId; - jsonrpc?: JsonRpcVersion; + jsonrpc?: JsonRpcVersion2; method: string; params?: unknown; }; @@ -37,10 +38,10 @@ export type BaseProviderOptions = { maxEventListeners?: number; /** - * `json-rpc-engine` middleware. The middleware will be inserted in the given + * `@metamask/json-rpc-engine` middleware. The middleware will be inserted in the given * order immediately after engine initialization. */ - rpcMiddleware?: JsonRpcMiddleware[]; + rpcMiddleware?: JsonRpcMiddleware[]; }; export type RequestArguments = { @@ -169,7 +170,7 @@ export abstract class BaseProvider extends SafeEventEmitter { */ async request(args: RequestArguments): Promise> { if (!args || typeof args !== 'object' || Array.isArray(args)) { - throw ethErrors.rpc.invalidRequest({ + throw rpcErrors.invalidRequest({ message: messages.errors.invalidRequestArgs(), data: args, }); @@ -178,7 +179,7 @@ export abstract class BaseProvider extends SafeEventEmitter { const { method, params } = args; if (typeof method !== 'string' || method.length === 0) { - throw ethErrors.rpc.invalidRequest({ + throw rpcErrors.invalidRequest({ message: messages.errors.invalidRequestMethod(), data: args, }); @@ -189,7 +190,7 @@ export abstract class BaseProvider extends SafeEventEmitter { !Array.isArray(params) && (typeof params !== 'object' || params === null) ) { - throw ethErrors.rpc.invalidRequest({ + throw rpcErrors.invalidRequest({ message: messages.errors.invalidRequestParams(), data: args, }); @@ -285,15 +286,9 @@ export abstract class BaseProvider extends SafeEventEmitter { callback(error, response); }; } - return this._rpcEngine.handle( - payload as JsonRpcRequest, - callbackWrapper, - ); + return this._rpcEngine.handle(payload as JsonRpcRequest, callbackWrapper); } - return this._rpcEngine.handle( - payload as JsonRpcRequest[], - callbackWrapper, - ); + return this._rpcEngine.handle(payload as JsonRpcRequest[], callbackWrapper); } /** @@ -331,13 +326,13 @@ export abstract class BaseProvider extends SafeEventEmitter { let error; if (isRecoverable) { - error = new EthereumRpcError( + error = new JsonRpcError( 1013, // Try again later errorMessage ?? messages.errors.disconnected(), ); this._log.debug(error); } else { - error = new EthereumRpcError( + error = new JsonRpcError( 1011, // Internal error errorMessage ?? messages.errors.permanentlyDisconnected(), ); diff --git a/src/MetaMaskInpageProvider.test.ts b/src/MetaMaskInpageProvider.test.ts index ac8a375d..744bb6a3 100644 --- a/src/MetaMaskInpageProvider.test.ts +++ b/src/MetaMaskInpageProvider.test.ts @@ -1,4 +1,4 @@ -import { JsonRpcRequest } from 'json-rpc-engine'; +import { JsonRpcRequest } from '@metamask/utils'; import messages from './messages'; import { @@ -58,7 +58,7 @@ async function getInitializedProvider({ onMethodCalled?: { substream: string; method: string; - callback: (data: JsonRpcRequest) => void; + callback: (data: JsonRpcRequest) => void; }[]; } = {}): Promise { const onWrite = jest.fn(); diff --git a/src/MetaMaskInpageProvider.ts b/src/MetaMaskInpageProvider.ts index cdb07167..d9e03d1f 100644 --- a/src/MetaMaskInpageProvider.ts +++ b/src/MetaMaskInpageProvider.ts @@ -1,8 +1,8 @@ -import { ethErrors } from 'eth-rpc-errors'; -import type { JsonRpcRequest, JsonRpcResponse } from 'json-rpc-engine'; +import { rpcErrors } from '@metamask/rpc-errors'; +import type { Json, JsonRpcRequest, JsonRpcResponse } from '@metamask/utils'; import type { Duplex } from 'stream'; -import type { UnvalidatedJsonRpcRequest } from './BaseProvider'; +import { UnvalidatedJsonRpcRequest } from './BaseProvider'; import messages from './messages'; import { sendSiteMetadata } from './siteMetadata'; import { @@ -22,7 +22,7 @@ export type SendSyncJsonRpcRequest = { | 'eth_coinbase' | 'eth_uninstallFilter' | 'net_version'; -} & JsonRpcRequest; +} & JsonRpcRequest; type WarningEventName = keyof SentWarningsState['events']; @@ -171,8 +171,8 @@ export class MetaMaskInpageProvider extends AbstractStreamProvider { * @param callback - The callback function. */ sendAsync( - payload: JsonRpcRequest, - callback: (error: Error | null, result?: JsonRpcResponse) => void, + payload: JsonRpcRequest, + callback: (error: Error | null, result?: JsonRpcResponse) => void, ): void { this._rpcRequest(payload, callback); } @@ -283,7 +283,10 @@ export class MetaMaskInpageProvider extends AbstractStreamProvider { * @returns A Promise that resolves with the JSON-RPC response object for the * request. */ - send(method: string, params?: T[]): Promise>; + send( + method: string, + params?: T[], + ): Promise>; /** * Submits an RPC request per the given JSON-RPC request object. @@ -293,8 +296,8 @@ export class MetaMaskInpageProvider extends AbstractStreamProvider { * @param callback - An error-first callback that will receive the JSON-RPC * response object. */ - send( - payload: JsonRpcRequest, + send( + payload: JsonRpcRequest, callback: (error: Error | null, result?: JsonRpcResponse) => void, ): void; @@ -306,7 +309,7 @@ export class MetaMaskInpageProvider extends AbstractStreamProvider { * @param payload - A JSON-RPC request object. * @returns A JSON-RPC response object. */ - send(payload: SendSyncJsonRpcRequest): JsonRpcResponse; + send(payload: SendSyncJsonRpcRequest): JsonRpcResponse; // eslint-disable-next-line @typescript-eslint/promise-function-async send(methodOrPayload: unknown, callbackOrArgs?: unknown): unknown { @@ -335,7 +338,7 @@ export class MetaMaskInpageProvider extends AbstractStreamProvider { typeof callbackOrArgs === 'function' ) { return this._rpcRequest( - methodOrPayload as JsonRpcRequest, + methodOrPayload as JsonRpcRequest, callbackOrArgs as (...args: unknown[]) => void, ); } @@ -412,7 +415,7 @@ export class MetaMaskInpageProvider extends AbstractStreamProvider { */ requestBatch: async (requests: UnvalidatedJsonRpcRequest[]) => { if (!Array.isArray(requests)) { - throw ethErrors.rpc.invalidRequest({ + throw rpcErrors.invalidRequest({ message: 'Batch requests must be made with an array of request objects.', data: requests, diff --git a/src/StreamProvider.test.ts b/src/StreamProvider.test.ts index d10b14b5..babfa9d0 100644 --- a/src/StreamProvider.test.ts +++ b/src/StreamProvider.test.ts @@ -1,4 +1,5 @@ -import type { JsonRpcMiddleware } from 'json-rpc-engine'; +import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine'; +import { Json, JsonRpcParams } from '@metamask/utils'; import messages from './messages'; import { StreamProvider } from './StreamProvider'; @@ -15,7 +16,7 @@ const mockStreamName = 'mock-stream'; * @returns A tuple containing the StreamProvider instance and the mock stream. */ function getStreamProvider( - rpcMiddleware: JsonRpcMiddleware[] = [], + rpcMiddleware: JsonRpcMiddleware[] = [], ) { const mockStream = new MockConnectionStream(); const streamProvider = new StreamProvider(mockStream, { diff --git a/src/StreamProvider.ts b/src/StreamProvider.ts index 51745c8a..c53a13df 100644 --- a/src/StreamProvider.ts +++ b/src/StreamProvider.ts @@ -1,7 +1,8 @@ +import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine'; import ObjectMultiplex from '@metamask/object-multiplex'; import SafeEventEmitter from '@metamask/safe-event-emitter'; +import { Json, JsonRpcParams } from '@metamask/utils'; import { duplex as isDuplex } from 'is-stream'; -import type { JsonRpcMiddleware } from 'json-rpc-engine'; import { createStreamMiddleware } from 'json-rpc-middleware-stream'; import pump from 'pump'; import type { Duplex } from 'stream'; @@ -23,7 +24,7 @@ export type StreamProviderOptions = { export type JsonRpcConnection = { events: SafeEventEmitter; - middleware: JsonRpcMiddleware; + middleware: JsonRpcMiddleware; stream: Duplex; }; diff --git a/src/extension-provider/createExternalExtensionProvider.test.ts b/src/extension-provider/createExternalExtensionProvider.test.ts index 26782365..52f3a830 100644 --- a/src/extension-provider/createExternalExtensionProvider.test.ts +++ b/src/extension-provider/createExternalExtensionProvider.test.ts @@ -1,4 +1,4 @@ -import type { JsonRpcRequest } from 'json-rpc-engine'; +import type { JsonRpcRequest } from '@metamask/utils'; import { createExternalExtensionProvider } from './createExternalExtensionProvider'; import config from './external-extension-config.json'; @@ -49,7 +49,7 @@ async function getInitializedProvider({ onMethodCalled?: { substream: string; method: string; - callback: (data: JsonRpcRequest) => void; + callback: (data: JsonRpcRequest) => void; }[]; } = {}): Promise { const onWrite = jest.fn(); diff --git a/src/middleware/createRpcWarningMiddleware.test.ts b/src/middleware/createRpcWarningMiddleware.test.ts index 4e638515..d3bdf120 100644 --- a/src/middleware/createRpcWarningMiddleware.test.ts +++ b/src/middleware/createRpcWarningMiddleware.test.ts @@ -1,4 +1,5 @@ -import { JsonRpcEngine, JsonRpcFailure, JsonRpcSuccess } from 'json-rpc-engine'; +import { JsonRpcEngine } from '@metamask/json-rpc-engine'; +import { Json, JsonRpcFailure, JsonRpcSuccess } from '@metamask/utils'; import { createRpcWarningMiddleware } from './createRpcWarningMiddleware'; import messages from '../messages'; @@ -89,7 +90,7 @@ describe('createRpcWarningMiddleware', () => { jsonrpc: '2.0', id: 1, method, - })) as JsonRpcSuccess; + })) as JsonRpcSuccess; expect(response.result).toBe('success!'); }); @@ -138,7 +139,7 @@ describe('createRpcWarningMiddleware', () => { id: 1, method, params, - })) as JsonRpcSuccess; + })) as JsonRpcSuccess; expect(response.result).toBe('success!'); }); diff --git a/src/middleware/createRpcWarningMiddleware.ts b/src/middleware/createRpcWarningMiddleware.ts index be57d293..313bebf7 100644 --- a/src/middleware/createRpcWarningMiddleware.ts +++ b/src/middleware/createRpcWarningMiddleware.ts @@ -1,4 +1,5 @@ -import type { JsonRpcMiddleware, JsonRpcRequest } from 'json-rpc-engine'; +import type { JsonRpcMiddleware } from '@metamask/json-rpc-engine'; +import type { Json, JsonRpcParams, JsonRpcRequest } from '@metamask/utils'; import { ERC1155, ERC721 } from '../constants'; import messages from '../messages'; @@ -12,7 +13,7 @@ import type { ConsoleLike } from '../utils'; */ export function createRpcWarningMiddleware( log: ConsoleLike, -): JsonRpcMiddleware { +): JsonRpcMiddleware { const sentWarnings = { ethDecryptDeprecation: false, ethGetEncryptionPublicKeyDeprecation: false, diff --git a/src/siteMetadata.ts b/src/siteMetadata.ts index 96d74ca5..e192872c 100644 --- a/src/siteMetadata.ts +++ b/src/siteMetadata.ts @@ -1,4 +1,4 @@ -import { JsonRpcEngine } from 'json-rpc-engine'; +import { JsonRpcEngine } from '@metamask/json-rpc-engine'; import messages from './messages'; import { ConsoleLike, NOOP } from './utils'; diff --git a/src/utils.ts b/src/utils.ts index 43326037..394e7fc0 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -1,9 +1,9 @@ -import { ethErrors } from 'eth-rpc-errors'; import { createIdRemapMiddleware, JsonRpcMiddleware, - PendingJsonRpcResponse, -} from 'json-rpc-engine'; +} from '@metamask/json-rpc-engine'; +import { rpcErrors } from '@metamask/rpc-errors'; +import { Json, JsonRpcParams, PendingJsonRpcResponse } from '@metamask/utils'; import { createRpcWarningMiddleware } from './middleware/createRpcWarningMiddleware'; @@ -27,7 +27,7 @@ export const EMITTED_NOTIFICATIONS = Object.freeze([ * remapping middleware and an error middleware. * * @param logger - The logger to use in the error middleware. - * @returns An array of json-rpc-engine middleware functions. + * @returns An array of @metamask/json-rpc-engine middleware functions. */ export const getDefaultExternalMiddleware = (logger: ConsoleLike = console) => [ createIdRemapMiddleware(), @@ -40,15 +40,15 @@ export const getDefaultExternalMiddleware = (logger: ConsoleLike = console) => [ * method. * * @param log - The logging API to use. - * @returns A json-rpc-engine middleware function. + * @returns A @metamask/json-rpc-engine middleware function. */ function createErrorMiddleware( log: ConsoleLike, -): JsonRpcMiddleware { +): JsonRpcMiddleware { return (request, response, next) => { // json-rpc-engine will terminate the request when it notices this error if (typeof request.method !== 'string' || !request.method) { - response.error = ethErrors.rpc.invalidRequest({ + response.error = rpcErrors.invalidRequest({ message: `The request 'method' must be a non-empty string.`, data: request, }); @@ -72,7 +72,7 @@ export const getRpcPromiseCallback = reject: (error?: Error) => void, unwrapResult = true, ) => - (error: Error, response: PendingJsonRpcResponse): void => { + (error: Error, response: PendingJsonRpcResponse): void => { if (error || response.error) { reject(error || response.error); } else { diff --git a/test/mocks/MockConnectionStream.ts b/test/mocks/MockConnectionStream.ts index c959af99..d421ac48 100644 --- a/test/mocks/MockConnectionStream.ts +++ b/test/mocks/MockConnectionStream.ts @@ -2,7 +2,7 @@ import { JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, -} from 'json-rpc-engine'; +} from '@metamask/utils'; import { Duplex } from 'stream'; /** @@ -10,7 +10,7 @@ import { Duplex } from 'stream'; * provider to the wallet. */ export class MockConnectionStream extends Duplex { - #onWrite?: (name: string, data: JsonRpcRequest) => void; + #onWrite?: (name: string, data: JsonRpcRequest) => void; /** * Construct a mock connection stream. @@ -19,8 +19,7 @@ export class MockConnectionStream extends Duplex { * the provider to the wallet are passed to this function. */ constructor( - onWrite: (name: string, data: JsonRpcRequest) => void = () => - undefined, + onWrite: (name: string, data: JsonRpcRequest) => void = () => undefined, ) { super({ objectMode: true }); this.#onWrite = onWrite; @@ -41,7 +40,7 @@ export class MockConnectionStream extends Duplex { * the write has completed. */ _write( - message: { name: string; data: JsonRpcRequest }, + message: { name: string; data: JsonRpcRequest }, _encoding: string, callback: (error?: Error) => void, ) { @@ -72,7 +71,7 @@ export class MockConnectionStream extends Duplex { * @param substream - The substream this reply is included in. * @param message - The JSON RPC response. */ - reply(substream: string, message: JsonRpcResponse) { + reply(substream: string, message: JsonRpcResponse) { this.push({ name: substream, data: message }); } diff --git a/test/mocks/MockPort.ts b/test/mocks/MockPort.ts index 7264d5f1..5c22a542 100644 --- a/test/mocks/MockPort.ts +++ b/test/mocks/MockPort.ts @@ -1,9 +1,9 @@ -import { EventEmitter } from 'events'; import { JsonRpcNotification, JsonRpcRequest, JsonRpcResponse, -} from 'json-rpc-engine'; +} from '@metamask/utils'; +import { EventEmitter } from 'events'; /** * A mock WebExtension Port for multiplexed JSON-RPC messages, used to @@ -17,7 +17,7 @@ export class MockPort { #eventEmitter = new EventEmitter(); - #onWrite?: (name: string, data: JsonRpcRequest) => void; + #onWrite?: (name: string, data: JsonRpcRequest) => void; /** * Construct a mock WebExtension Port. @@ -26,8 +26,7 @@ export class MockPort { * from another extension to the wallet are passed to this function. */ constructor( - onWrite: (name: string, data: JsonRpcRequest) => void = () => - undefined, + onWrite: (name: string, data: JsonRpcRequest) => void = () => undefined, ) { this.#onWrite = onWrite; } @@ -46,7 +45,7 @@ export class MockPort { * in. * @param message.data - The JSON-RPC request. */ - postMessage(message: { name: string; data: JsonRpcRequest }) { + postMessage(message: { name: string; data: JsonRpcRequest }) { if (!this.#connected) { throw new Error('Disconnected'); } else if (this.#onWrite) { @@ -65,10 +64,7 @@ export class MockPort { get onMessage() { return { addListener: ( - listener: (message: { - name: string; - data: JsonRpcRequest; - }) => void, + listener: (message: { name: string; data: JsonRpcRequest }) => void, ) => { this.#eventEmitter.addListener('message', listener); }, @@ -81,7 +77,7 @@ export class MockPort { * @param substream - The substream this reply is included in. * @param message - The JSON RPC response. */ - reply(substream: string, message: JsonRpcResponse) { + reply(substream: string, message: JsonRpcResponse) { if (!this.#connected) { throw new Error( 'It is not possible to reply after the port has disconnected', @@ -96,7 +92,7 @@ export class MockPort { * @param substream - The substream this notification is included in. * @param message - The JSoN RPC notification. */ - notify(substream: string, message: JsonRpcNotification) { + notify(substream: string, message: JsonRpcNotification) { if (!this.#connected) { throw new Error( 'It is not possible to notify after the port has disconnected', diff --git a/yarn.lock b/yarn.lock index 6e495fd5..4dbfd1b4 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1037,6 +1037,17 @@ __metadata: languageName: node linkType: hard +"@metamask/json-rpc-engine@npm:^7.0.0": + version: 7.0.0 + resolution: "@metamask/json-rpc-engine@npm:7.0.0" + dependencies: + "@metamask/rpc-errors": ^5.0.0 + "@metamask/safe-event-emitter": ^2.0.0 + "@metamask/utils": ^5.0.1 + checksum: d22347ee4597bc72cdc34e65a27872ed8e77de188c5b95fed9133c25289cd4abd7aafa72e3e326d8a7449a857c275ed6435642727c30c51319f5d97a579c5f49 + languageName: node + linkType: hard + "@metamask/object-multiplex@npm:^1.1.0": version: 1.2.0 resolution: "@metamask/object-multiplex@npm:1.2.0" @@ -1058,9 +1069,11 @@ __metadata: "@metamask/eslint-config-jest": ^11.0.0 "@metamask/eslint-config-nodejs": ^11.0.1 "@metamask/eslint-config-typescript": ^11.0.0 + "@metamask/json-rpc-engine": ^7.0.0 "@metamask/object-multiplex": ^1.1.0 "@metamask/rpc-errors": ^5.1.1 "@metamask/safe-event-emitter": ^3.0.0 + "@metamask/utils": ^6.2.0 "@types/chrome": ^0.0.233 "@types/jest": ^28.1.6 "@types/node": ^17.0.23 @@ -1085,7 +1098,6 @@ __metadata: jest-chrome: ^0.7.1 jest-environment-jsdom: ^29.5.0 jest-it-up: ^2.0.2 - json-rpc-engine: ^6.1.0 json-rpc-middleware-stream: ^4.2.1 prettier: ^2.7.1 prettier-plugin-packagejson: ^2.3.0 @@ -1099,7 +1111,7 @@ __metadata: languageName: unknown linkType: soft -"@metamask/rpc-errors@npm:^5.1.1": +"@metamask/rpc-errors@npm:^5.0.0, @metamask/rpc-errors@npm:^5.1.1": version: 5.1.1 resolution: "@metamask/rpc-errors@npm:5.1.1" dependencies: @@ -1123,7 +1135,7 @@ __metadata: languageName: node linkType: hard -"@metamask/utils@npm:^5.0.0": +"@metamask/utils@npm:^5.0.0, @metamask/utils@npm:^5.0.1": version: 5.0.2 resolution: "@metamask/utils@npm:5.0.2" dependencies: @@ -1136,6 +1148,20 @@ __metadata: languageName: node linkType: hard +"@metamask/utils@npm:^6.2.0": + version: 6.2.0 + resolution: "@metamask/utils@npm:6.2.0" + dependencies: + "@ethereumjs/tx": ^4.1.2 + "@noble/hashes": ^1.3.1 + "@types/debug": ^4.1.7 + debug: ^4.3.4 + semver: ^7.3.8 + superstruct: ^1.0.3 + checksum: 0bc675358ecc09b3bc04da613d73666295d7afa51ff6b8554801585966900b24b8545bd93b8b2e9a17db867ebe421fe884baf3558ec4ca3199fa65504f677c1b + languageName: node + linkType: hard + "@noble/curves@npm:1.1.0, @noble/curves@npm:~1.1.0": version: 1.1.0 resolution: "@noble/curves@npm:1.1.0" @@ -1152,7 +1178,7 @@ __metadata: languageName: node linkType: hard -"@noble/hashes@npm:~1.3.0, @noble/hashes@npm:~1.3.1": +"@noble/hashes@npm:^1.3.1, @noble/hashes@npm:~1.3.0, @noble/hashes@npm:~1.3.1": version: 1.3.2 resolution: "@noble/hashes@npm:1.3.2" checksum: fe23536b436539d13f90e4b9be843cc63b1b17666a07634a2b1259dded6f490be3d050249e6af98076ea8f2ea0d56f578773c2197f2aa0eeaa5fba5bc18ba474 @@ -3329,15 +3355,6 @@ __metadata: languageName: node linkType: hard -"eth-rpc-errors@npm:^4.0.2": - version: 4.0.2 - resolution: "eth-rpc-errors@npm:4.0.2" - dependencies: - fast-safe-stringify: ^2.0.6 - checksum: 1dbdee8f416090f1d318e17bdee2251d174d73c8faa4286fa364bc51ae9105672045f2d078ec23ca6a2b4b92af7cfbe7fa1ba17ad49e591fc653a363bf8cbab2 - languageName: node - linkType: hard - "ethereum-cryptography@npm:^2.0.0": version: 2.1.2 resolution: "ethereum-cryptography@npm:2.1.2" @@ -4970,16 +4987,6 @@ __metadata: languageName: node linkType: hard -"json-rpc-engine@npm:^6.1.0": - version: 6.1.0 - resolution: "json-rpc-engine@npm:6.1.0" - dependencies: - "@metamask/safe-event-emitter": ^2.0.0 - eth-rpc-errors: ^4.0.2 - checksum: 33b6c9bbd81abf8e323a0281ee05871713203c40d34a4d0bda27706cd0a0935c7b51845238ba89b73027e44ebc8034bbd82db9f962e6c578eb922d9b95acc8bd - languageName: node - linkType: hard - "json-rpc-middleware-stream@npm:^4.2.1": version: 4.2.1 resolution: "json-rpc-middleware-stream@npm:4.2.1" From 429cd133673d6e3bd1bf959c44d25fb0f75523ac Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Thu, 24 Aug 2023 01:51:12 +0000 Subject: [PATCH 3/6] WIP: test type-compatibility fix for @metamask/rpc-errors --- ...mask-rpc-errors-npm-5.1.1-dc76c26803.patch | 247 ++++++++++++++++++ package.json | 3 + yarn.lock | 12 +- 3 files changed, 261 insertions(+), 1 deletion(-) create mode 100644 .yarn/patches/@metamask-rpc-errors-npm-5.1.1-dc76c26803.patch diff --git a/.yarn/patches/@metamask-rpc-errors-npm-5.1.1-dc76c26803.patch b/.yarn/patches/@metamask-rpc-errors-npm-5.1.1-dc76c26803.patch new file mode 100644 index 00000000..3a319eb3 --- /dev/null +++ b/.yarn/patches/@metamask-rpc-errors-npm-5.1.1-dc76c26803.patch @@ -0,0 +1,247 @@ +diff --git a/dist/classes.d.ts b/dist/classes.d.ts +index 46c2ef8eb999c0063594277974b29c8f239a6dcb..d9befc615d70c27607854fcb5b6dc595c1be226f 100644 +--- a/dist/classes.d.ts ++++ b/dist/classes.d.ts +@@ -1,5 +1,5 @@ + import { JsonRpcError as SerializedJsonRpcError } from '@metamask/utils'; +-import { DataWithOptionalCause } from './utils'; ++import type { OptionalDataWithOptionalCause } from './utils'; + export type { SerializedJsonRpcError }; + /** + * Error subclass implementing JSON RPC 2.0 errors and Ethereum RPC errors +@@ -7,7 +7,7 @@ export type { SerializedJsonRpcError }; + * + * Permits any integer error code. + */ +-export declare class JsonRpcError extends Error { ++export declare class JsonRpcError extends Error { + code: number; + data?: T; + constructor(code: number, message: string, data?: T); +@@ -29,7 +29,7 @@ export declare class JsonRpcError extends Error + * Error subclass implementing Ethereum Provider errors per EIP-1193. + * Permits integer error codes in the [ 1000 <= 4999 ] range. + */ +-export declare class EthereumProviderError extends JsonRpcError { ++export declare class EthereumProviderError extends JsonRpcError { + /** + * Create an Ethereum Provider JSON-RPC error. + * +diff --git a/dist/classes.js.map b/dist/classes.js.map +index 3ec85fd22f21a45e7537436251feb70f8156e585..574d6c312ac289cd4c22291a0d9c1d17123e44c4 100644 +--- a/dist/classes.js.map ++++ b/dist/classes.js.map +@@ -1 +1 @@ +-{"version":3,"file":"classes.js","sourceRoot":"","sources":["../src/classes.ts"],"names":[],"mappings":";;;;;;AAAA,2CAIyB;AACzB,8EAAgD;AAEhD,mCAAgE;AAIhE;;;;;GAKG;AACH,MAAa,YAA8C,SAAQ,KAAK;IAKtE,YAAY,IAAY,EAAE,OAAe,EAAE,IAAQ;QACjD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;SAC/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CAC5D,CAAC;SACH;QAED,KAAK,CAAC,IAAI,EAAE,OAAO,EAAE,IAAI,CAAC,CAAC;IAC7B,CAAC;CACF;AApBD,sDAoBC;AAED;;;;;GAKG;AACH,SAAS,sBAAsB,CAAC,IAAY;IAC1C,OAAO,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,CAAC;AAChE,CAAC;AAED;;;;;;GAMG;AACH,SAAS,iBAAiB,CAAC,CAAU,EAAE,KAAc;IACnD,IAAI,KAAK,KAAK,YAAY,EAAE;QAC1B,OAAO,SAAS,CAAC;KAClB;IAED,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {\n isPlainObject,\n Json,\n JsonRpcError as SerializedJsonRpcError,\n} from '@metamask/utils';\nimport safeStringify from 'fast-safe-stringify';\n\nimport { DataWithOptionalCause, serializeCause } from './utils';\n\nexport type { SerializedJsonRpcError };\n\n/**\n * Error subclass implementing JSON RPC 2.0 errors and Ethereum RPC errors\n * per EIP-1474.\n *\n * Permits any integer error code.\n */\nexport class JsonRpcError extends Error {\n public code: number;\n\n public data?: T;\n\n constructor(code: number, message: string, data?: T) {\n if (!Number.isInteger(code)) {\n throw new Error('\"code\" must be an integer.');\n }\n\n if (!message || typeof message !== 'string') {\n throw new Error('\"message\" must be a non-empty string.');\n }\n\n super(message);\n this.code = code;\n if (data !== undefined) {\n this.data = data;\n }\n }\n\n /**\n * Get the error as JSON-serializable object.\n *\n * @returns A plain object with all public class properties.\n */\n serialize(): SerializedJsonRpcError {\n const serialized: SerializedJsonRpcError = {\n code: this.code,\n message: this.message,\n };\n\n if (this.data !== undefined) {\n // `this.data` is not guaranteed to be a plain object, but this simplifies\n // the type guard below. We can safely cast it because we know it's a\n // JSON-serializable value.\n serialized.data = this.data as { [key: string]: Json };\n\n if (isPlainObject(this.data)) {\n serialized.data.cause = serializeCause(this.data.cause);\n }\n }\n\n if (this.stack) {\n serialized.stack = this.stack;\n }\n\n return serialized;\n }\n\n /**\n * Get a string representation of the serialized error, omitting any circular\n * references.\n *\n * @returns A string representation of the serialized error.\n */\n toString(): string {\n return safeStringify(this.serialize(), stringifyReplacer, 2);\n }\n}\n\n/**\n * Error subclass implementing Ethereum Provider errors per EIP-1193.\n * Permits integer error codes in the [ 1000 <= 4999 ] range.\n */\nexport class EthereumProviderError<\n T extends DataWithOptionalCause,\n> extends JsonRpcError {\n /**\n * Create an Ethereum Provider JSON-RPC error.\n *\n * @param code - The JSON-RPC error code. Must be an integer in the\n * `1000 <= n <= 4999` range.\n * @param message - The JSON-RPC error message.\n * @param data - Optional data to include in the error.\n */\n constructor(code: number, message: string, data?: T) {\n if (!isValidEthProviderCode(code)) {\n throw new Error(\n '\"code\" must be an integer such that: 1000 <= code <= 4999',\n );\n }\n\n super(code, message, data);\n }\n}\n\n/**\n * Check if the given code is a valid JSON-RPC error code.\n *\n * @param code - The code to check.\n * @returns Whether the code is valid.\n */\nfunction isValidEthProviderCode(code: number): boolean {\n return Number.isInteger(code) && code >= 1000 && code <= 4999;\n}\n\n/**\n * A JSON replacer function that omits circular references.\n *\n * @param _ - The key being replaced.\n * @param value - The value being replaced.\n * @returns The value to use in place of the original value.\n */\nfunction stringifyReplacer(_: unknown, value: unknown): unknown {\n if (value === '[Circular]') {\n return undefined;\n }\n\n return value;\n}\n"]} +\ No newline at end of file ++{"version":3,"file":"classes.js","sourceRoot":"","sources":["../src/classes.ts"],"names":[],"mappings":";;;;;;AAAA,2CAIyB;AACzB,8EAAgD;AAGhD,mCAAyC;AAIzC;;;;;GAKG;AACH,MAAa,YAEX,SAAQ,KAAK;IAKb,YAAY,IAAY,EAAE,OAAe,EAAE,IAAQ;QACjD,IAAI,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,EAAE;YAC3B,MAAM,IAAI,KAAK,CAAC,4BAA4B,CAAC,CAAC;SAC/C;QAED,IAAI,CAAC,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,EAA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AAI,CAAC,CAAC;IAC7B,CAAC;CACF;AApBD,sDAoBC;AAED;;;;;GAKG;AACH,SAAS,sBAAsB,CAAC,IAAY;IAC1C,OAAO,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,IAAI,CAAC;AAChE,CAAC;AAED;;;;;;GAMG;AACH,SAAS,iBAAiB,CAAC,CAAU,EAAE,KAAc;IACnD,IAAI,KAAK,KAAK,YAAY,EAAE;QAC1B,OAAO,SAAS,CAAC;KAClB;IAED,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import {\n isPlainObject,\n Json,\n JsonRpcError as SerializedJsonRpcError,\n} from '@metamask/utils';\nimport safeStringify from 'fast-safe-stringify';\n\nimport type { OptionalDataWithOptionalCause } from './utils';\nimport { serializeCause } from './utils';\n\nexport type { SerializedJsonRpcError };\n\n/**\n * Error subclass implementing JSON RPC 2.0 errors and Ethereum RPC errors\n * per EIP-1474.\n *\n * Permits any integer error code.\n */\nexport class JsonRpcError<\n T extends OptionalDataWithOptionalCause,\n> extends Error {\n public code: number;\n\n public data?: T;\n\n constructor(code: number, message: string, data?: T) {\n if (!Number.isInteger(code)) {\n throw new Error('\"code\" must be an integer.');\n }\n\n if (!message || typeof message !== 'string') {\n throw new Error('\"message\" must be a non-empty string.');\n }\n\n super(message);\n this.code = code;\n if (data !== undefined) {\n this.data = data;\n }\n }\n\n /**\n * Get the error as JSON-serializable object.\n *\n * @returns A plain object with all public class properties.\n */\n serialize(): SerializedJsonRpcError {\n const serialized: SerializedJsonRpcError = {\n code: this.code,\n message: this.message,\n };\n\n if (this.data !== undefined) {\n // `this.data` is not guaranteed to be a plain object, but this simplifies\n // the type guard below. We can safely cast it because we know it's a\n // JSON-serializable value.\n serialized.data = this.data as { [key: string]: Json };\n\n if (isPlainObject(this.data)) {\n serialized.data.cause = serializeCause(this.data.cause);\n }\n }\n\n if (this.stack) {\n serialized.stack = this.stack;\n }\n\n return serialized;\n }\n\n /**\n * Get a string representation of the serialized error, omitting any circular\n * references.\n *\n * @returns A string representation of the serialized error.\n */\n toString(): string {\n return safeStringify(this.serialize(), stringifyReplacer, 2);\n }\n}\n\n/**\n * Error subclass implementing Ethereum Provider errors per EIP-1193.\n * Permits integer error codes in the [ 1000 <= 4999 ] range.\n */\nexport class EthereumProviderError<\n T extends OptionalDataWithOptionalCause,\n> extends JsonRpcError {\n /**\n * Create an Ethereum Provider JSON-RPC error.\n *\n * @param code - The JSON-RPC error code. Must be an integer in the\n * `1000 <= n <= 4999` range.\n * @param message - The JSON-RPC error message.\n * @param data - Optional data to include in the error.\n */\n constructor(code: number, message: string, data?: T) {\n if (!isValidEthProviderCode(code)) {\n throw new Error(\n '\"code\" must be an integer such that: 1000 <= code <= 4999',\n );\n }\n\n super(code, message, data);\n }\n}\n\n/**\n * Check if the given code is a valid JSON-RPC error code.\n *\n * @param code - The code to check.\n * @returns Whether the code is valid.\n */\nfunction isValidEthProviderCode(code: number): boolean {\n return Number.isInteger(code) && code >= 1000 && code <= 4999;\n}\n\n/**\n * A JSON replacer function that omits circular references.\n *\n * @param _ - The key being replaced.\n * @param value - The value being replaced.\n * @returns The value to use in place of the original value.\n */\nfunction stringifyReplacer(_: unknown, value: unknown): unknown {\n if (value === '[Circular]') {\n return undefined;\n }\n\n return value;\n}\n"]} +\ No newline at end of file +diff --git a/dist/errors.d.ts b/dist/errors.d.ts +index b4653125ce748f7da32ced278cc60f726c3cb6ee..cbe8abece705fcf13e977e9d107f23f55a18064d 100644 +--- a/dist/errors.d.ts ++++ b/dist/errors.d.ts +@@ -1,14 +1,14 @@ + import { JsonRpcError, EthereumProviderError } from './classes'; +-import { DataWithOptionalCause } from './utils'; +-type EthereumErrorOptions = { ++import { OptionalDataWithOptionalCause } from './utils'; ++type EthereumErrorOptions = { + message?: string; + data?: T; + }; +-type ServerErrorOptions = { ++type ServerErrorOptions = { + code: number; + } & EthereumErrorOptions; +-type CustomErrorArg = ServerErrorOptions; +-type JsonRpcErrorsArg = EthereumErrorOptions | string; ++type CustomErrorArg = ServerErrorOptions; ++type JsonRpcErrorsArg = EthereumErrorOptions | string; + export declare const rpcErrors: { + /** + * Get a JSON RPC 2.0 Parse (-32700) error. +@@ -16,35 +16,35 @@ export declare const rpcErrors: { + * @param arg - The error message or options bag. + * @returns An instance of the {@link JsonRpcError} class. + */ +- parse: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; ++ parse: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; + /** + * Get a JSON RPC 2.0 Invalid Request (-32600) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link JsonRpcError} class. + */ +- invalidRequest: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; ++ invalidRequest: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; + /** + * Get a JSON RPC 2.0 Invalid Params (-32602) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link JsonRpcError} class. + */ +- invalidParams: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; ++ invalidParams: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; + /** + * Get a JSON RPC 2.0 Method Not Found (-32601) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link JsonRpcError} class. + */ +- methodNotFound: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; ++ methodNotFound: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; + /** + * Get a JSON RPC 2.0 Internal (-32603) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link JsonRpcError} class. + */ +- internal: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; ++ internal: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; + /** + * Get a JSON RPC 2.0 Server error. + * Permits integer error codes in the [ -32099 <= -32005 ] range. +@@ -53,49 +53,49 @@ export declare const rpcErrors: { + * @param opts - The error options bag. + * @returns An instance of the {@link JsonRpcError} class. + */ +- server: (opts: ServerErrorOptions) => JsonRpcError; ++ server: (opts: ServerErrorOptions) => JsonRpcError; + /** + * Get an Ethereum JSON RPC Invalid Input (-32000) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link JsonRpcError} class. + */ +- invalidInput: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; ++ invalidInput: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; + /** + * Get an Ethereum JSON RPC Resource Not Found (-32001) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link JsonRpcError} class. + */ +- resourceNotFound: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; ++ resourceNotFound: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; + /** + * Get an Ethereum JSON RPC Resource Unavailable (-32002) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link JsonRpcError} class. + */ +- resourceUnavailable: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; ++ resourceUnavailable: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; + /** + * Get an Ethereum JSON RPC Transaction Rejected (-32003) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link JsonRpcError} class. + */ +- transactionRejected: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; ++ transactionRejected: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; + /** + * Get an Ethereum JSON RPC Method Not Supported (-32004) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link JsonRpcError} class. + */ +- methodNotSupported: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; ++ methodNotSupported: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; + /** + * Get an Ethereum JSON RPC Limit Exceeded (-32005) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link JsonRpcError} class. + */ +- limitExceeded: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; ++ limitExceeded: (arg?: JsonRpcErrorsArg | undefined) => JsonRpcError; + }; + export declare const providerErrors: { + /** +@@ -104,41 +104,41 @@ export declare const providerErrors: { + * @param arg - The error message or options bag. + * @returns An instance of the {@link EthereumProviderError} class. + */ +- userRejectedRequest: (arg?: JsonRpcErrorsArg | undefined) => EthereumProviderError; ++ userRejectedRequest: (arg?: JsonRpcErrorsArg | undefined) => EthereumProviderError; + /** + * Get an Ethereum Provider Unauthorized (4100) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link EthereumProviderError} class. + */ +- unauthorized: (arg?: JsonRpcErrorsArg | undefined) => EthereumProviderError; ++ unauthorized: (arg?: JsonRpcErrorsArg | undefined) => EthereumProviderError; + /** + * Get an Ethereum Provider Unsupported Method (4200) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link EthereumProviderError} class. + */ +- unsupportedMethod: (arg?: JsonRpcErrorsArg | undefined) => EthereumProviderError; ++ unsupportedMethod: (arg?: JsonRpcErrorsArg | undefined) => EthereumProviderError; + /** + * Get an Ethereum Provider Not Connected (4900) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link EthereumProviderError} class. + */ +- disconnected: (arg?: JsonRpcErrorsArg | undefined) => EthereumProviderError; ++ disconnected: (arg?: JsonRpcErrorsArg | undefined) => EthereumProviderError; + /** + * Get an Ethereum Provider Chain Not Connected (4901) error. + * + * @param arg - The error message or options bag. + * @returns An instance of the {@link EthereumProviderError} class. + */ +- chainDisconnected: (arg?: JsonRpcErrorsArg | undefined) => EthereumProviderError; ++ chainDisconnected: (arg?: JsonRpcErrorsArg | undefined) => EthereumProviderError; + /** + * Get a custom Ethereum Provider error. + * + * @param opts - The error options bag. + * @returns An instance of the {@link EthereumProviderError} class. + */ +- custom: (opts: CustomErrorArg) => EthereumProviderError; ++ custom: (opts: CustomErrorArg) => EthereumProviderError; + }; + export {}; +diff --git a/dist/errors.js.map b/dist/errors.js.map +index 3ce4a37cef2de53f217b1573224090813e727bc8..d542732ea1cb5a3f8c8569405ef545edab1ebc12 100644 +--- a/dist/errors.js.map ++++ b/dist/errors.js.map +@@ -1 +1 @@ +-{"version":3,"file":"errors.js","sourceRo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AAM,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;IACvC,OAAO,IAAI,+BAAqB,CAC9B,IAAI,EACJ,OAAO,IAAI,IAAA,0BAAkB,EAAC,IAAI,CAAC,EACnC,IAAI,CACL,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,SAAS,CAChB,GAAyB;IAEzB,IAAI,GAAG,EAAE;QACP,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC3B,OAAO,CAAC,GAAG,CAAC,CAAC;SACd;aAAM,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACzD,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,GAAG,CAAC;YAE9B,IAAI,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;gBAC1C,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;aACjD;YACD,OAAO,CAAC,OAAO,IAAI,SAAS,EAAE,IAAI,CAAC,CAAC;SACrC;KACF;IAED,OAAO,EAAE,CAAC;AACZ,CAAC","sourcesContent":["import { JsonRpcError, EthereumProviderError } from './classes';\nimport { errorCodes } from './error-constants';\nimport { DataWithOptionalCause, getMessageFromCode } from './utils';\n\ntype EthereumErrorOptions = {\n message?: string;\n data?: T;\n};\n\ntype ServerErrorOptions = {\n code: number;\n} & EthereumErrorOptions;\n\ntype CustomErrorArg = ServerErrorOptions;\n\ntype JsonRpcErrorsArg =\n | EthereumErrorOptions\n | string;\n\nexport const rpcErrors = {\n /**\n * Get a JSON RPC 2.0 Parse (-32700) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n parse: (arg?: JsonRpcErrorsArg) =>\n getJsonRpcError(errorCodes.rpc.parse, arg),\n\n /**\n * Get a JSON RPC 2.0 Invalid Request (-32600) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n invalidRequest: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.invalidRequest, arg),\n\n /**\n * Get a JSON RPC 2.0 Invalid Params (-32602) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n invalidParams: (arg?: JsonRpcErrorsArg) =>\n getJsonRpcError(errorCodes.rpc.invalidParams, arg),\n\n /**\n * Get a JSON RPC 2.0 Method Not Found (-32601) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n methodNotFound: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.methodNotFound, arg),\n\n /**\n * Get a JSON RPC 2.0 Internal (-32603) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n internal: (arg?: JsonRpcErrorsArg) =>\n getJsonRpcError(errorCodes.rpc.internal, arg),\n\n /**\n * Get a JSON RPC 2.0 Server error.\n * Permits integer error codes in the [ -32099 <= -32005 ] range.\n * Codes -32000 through -32004 are reserved by EIP-1474.\n *\n * @param opts - The error options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n server: (opts: ServerErrorOptions) => {\n if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {\n throw new Error(\n 'Ethereum RPC Server errors must provide single object argument.',\n );\n }\n const { code } = opts;\n if (!Number.isInteger(code) || code > -32005 || code < -32099) {\n throw new Error(\n '\"code\" must be an integer such that: -32099 <= code <= -32005',\n );\n }\n return getJsonRpcError(code, opts);\n },\n\n /**\n * Get an Ethereum JSON RPC Invalid Input (-32000) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n invalidInput: (arg?: JsonRpcErrorsArg) =>\n getJsonRpcError(errorCodes.rpc.invalidInput, arg),\n\n /**\n * Get an Ethereum JSON RPC Resource Not Found (-32001) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n resourceNotFound: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.resourceNotFound, arg),\n\n /**\n * Get an Ethereum JSON RPC Resource Unavailable (-32002) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n resourceUnavailable: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.resourceUnavailable, arg),\n\n /**\n * Get an Ethereum JSON RPC Transaction Rejected (-32003) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n transactionRejected: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.transactionRejected, arg),\n\n /**\n * Get an Ethereum JSON RPC Method Not Supported (-32004) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n methodNotSupported: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.methodNotSupported, arg),\n\n /**\n * Get an Ethereum JSON RPC Limit Exceeded (-32005) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n limitExceeded: (arg?: JsonRpcErrorsArg) =>\n getJsonRpcError(errorCodes.rpc.limitExceeded, arg),\n};\n\nexport const providerErrors = {\n /**\n * Get an Ethereum Provider User Rejected Request (4001) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\n userRejectedRequest: (\n arg?: JsonRpcErrorsArg,\n ) => {\n return getEthProviderError(errorCodes.provider.userRejectedRequest, arg);\n },\n\n /**\n * Get an Ethereum Provider Unauthorized (4100) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\n unauthorized: (\n arg?: JsonRpcErrorsArg,\n ) => {\n return getEthProviderError(errorCodes.provider.unauthorized, arg);\n },\n\n /**\n * Get an Ethereum Provider Unsupported Method (4200) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\n unsupportedMethod: (\n arg?: JsonRpcErrorsArg,\n ) => {\n return getEthProviderError(errorCodes.provider.unsupportedMethod, arg);\n },\n\n /**\n * Get an Ethereum Provider Not Connected (4900) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\n disconnected: (\n arg?: JsonRpcErrorsArg,\n ) => {\n return getEthProviderError(errorCodes.provider.disconnected, arg);\n },\n\n /**\n * Get an Ethereum Provider Chain Not Connected (4901) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\n chainDisconnected: (\n arg?: JsonRpcErrorsArg,\n ) => {\n return getEthProviderError(errorCodes.provider.chainDisconnected, arg);\n },\n\n /**\n * Get a custom Ethereum Provider error.\n *\n * @param opts - The error options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\n custom: (opts: CustomErrorArg) => {\n if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {\n throw new Error(\n 'Ethereum Provider custom errors must provide single object argument.',\n );\n }\n\n const { code, message, data } = opts;\n\n if (!message || typeof message !== 'string') {\n throw new Error('\"message\" must be a nonempty string');\n }\n return new EthereumProviderError(code, message, data);\n },\n};\n\n/**\n * Get a generic JSON-RPC error class instance.\n *\n * @param code - The error code.\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\nfunction getJsonRpcError(\n code: number,\n arg?: JsonRpcErrorsArg,\n): JsonRpcError {\n const [message, data] = parseOpts(arg);\n return new JsonRpcError(code, message ?? getMessageFromCode(code), data);\n}\n\n/**\n * Get an Ethereum Provider error class instance.\n *\n * @param code - The error code.\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\nfunction getEthProviderError(\n code: number,\n arg?: JsonRpcErrorsArg,\n): EthereumProviderError {\n const [message, data] = parseOpts(arg);\n return new EthereumProviderError(\n code,\n message ?? getMessageFromCode(code),\n data,\n );\n}\n\n/**\n * Get an error message and optional data from an options bag.\n *\n * @param arg - The error message or options bag.\n * @returns A tuple containing the error message and optional data.\n */\nfunction parseOpts(\n arg?: JsonRpcErrorsArg,\n): [message?: string | undefined, data?: T | undefined] {\n if (arg) {\n if (typeof arg === 'string') {\n return [arg];\n } else if (typeof arg === 'object' && !Array.isArray(arg)) {\n const { message, data } = arg;\n\n if (message && typeof message !== 'string') {\n throw new Error('Must specify string message.');\n }\n return [message ?? undefined, data];\n }\n }\n\n return [];\n}\n"]} +\ No newline at end of file ++{"version":3,"file":"errors.js","sourceRoot":"","sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAA,uCAAgE;AAChE,uDAA+C;AAC/C,mCAA4E;AAkB/D,QAAA,SAAS,GAAG;IACvB;;;;;OAKG;IACH,KAAK,EAAE,CAA0C,GAAyB,EAAE,EAAE,CAC5E,eAAe,CAAC,4BAAU,CAAC,GAAG,CAAC,KAAK,EAAE,GAAG,CAAC;IAE5C;;;;;OAKG;IACH,cAAc,EAAE,CACd,GAAyB,EACzB,EAAE,CAAC,eAAe,CAAC,4BAAU,CAAC,GAAG,CAAC,cAAc,EAAE,GAAG,CAAC;IAExD;;;;;OAKG;IACH,aAAa,EAAE,CACb,GAAyB,EACzB,EAAE,CAAC,eAAe,CAAC,4BAAU,CAAC,GAAG,CAAC,aAAa,EAAE,GAAG,CAAC;IAEvD;;;;;OAKG;IACH,cAAc,EAAE,CACd,GAAyB,EACzB,EAAE,CAAC,eAAe,CAAC,4BAAU,CAAC,GAAG,CAAC,cAAc,EAAE,GAAG,CAAC;IAExD;;;;;OAKG;IACH,QAAQ,EAAE,CACR,GAAyB,EACzB,EAAE,CAAC,eAAe,CAAC,4BAAU,CAAC,GAAG,CAAC,QAAQ,EAAE,GAAG,CAAC;IAElD;;;;;;;OAOG;IACH,MAAM,EAAE,CACN,IAA2B,EAC3B,EAAE;QACF,IAAI,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,KAA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CACF,CAAC;AAEF;;;;;;GAMG;AACH,SAAS,eAAe,CACtB,IAAY,EACZ,GAAyB;IAEzB,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;IACvC,OAAO,IAAI,sBAAY,CAAC,IAAI,EAAE,OAAO,IAAI,IAAA,0BAAkB,EAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC;AAC3E,CAAC;AAED;;;;;;GAMG;AACH,SAAS,mBAAmB,CAC1B,IAAY,EACZ,GAAyB;IAEzB,MAAM,CAAC,OAAO,EAAE,IAAI,CAAC,GAAG,SAAS,CAAC,GAAG,CAAC,CAAC;IACvC,OAAO,IAAI,+BAAqB,CAC9B,IAAI,EACJ,OAAO,IAAI,IAAA,0BAAkB,EAAC,IAAI,CAAC,EACnC,IAAI,CACL,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,SAAS,SAAS,CAChB,GAAyB;IAEzB,IAAI,GAAG,EAAE;QACP,IAAI,OAAO,GAAG,KAAK,QAAQ,EAAE;YAC3B,OAAO,CAAC,GAAG,CAAC,CAAC;SACd;aAAM,IAAI,OAAO,GAAG,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,GAAG,CAAC,EAAE;YACzD,MAAM,EAAE,OAAO,EAAE,IAAI,EAAE,GAAG,GAAG,CAAC;YAE9B,IAAI,OAAO,IAAI,OAAO,OAAO,KAAK,QAAQ,EAAE;gBAC1C,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;aACjD;YACD,OAAO,CAAC,OAAO,IAAI,SAAS,EAAE,IAAI,CAAC,CAAC;SACrC;KACF;IAED,OAAO,EAAE,CAAC;AACZ,CAAC","sourcesContent":["import { JsonRpcError, EthereumProviderError } from './classes';\nimport { errorCodes } from './error-constants';\nimport { OptionalDataWithOptionalCause, getMessageFromCode } from './utils';\n\ntype EthereumErrorOptions = {\n message?: string;\n data?: T;\n};\n\ntype ServerErrorOptions = {\n code: number;\n} & EthereumErrorOptions;\n\ntype CustomErrorArg =\n ServerErrorOptions;\n\ntype JsonRpcErrorsArg =\n | EthereumErrorOptions\n | string;\n\nexport const rpcErrors = {\n /**\n * Get a JSON RPC 2.0 Parse (-32700) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n parse: (arg?: JsonRpcErrorsArg) =>\n getJsonRpcError(errorCodes.rpc.parse, arg),\n\n /**\n * Get a JSON RPC 2.0 Invalid Request (-32600) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n invalidRequest: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.invalidRequest, arg),\n\n /**\n * Get a JSON RPC 2.0 Invalid Params (-32602) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n invalidParams: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.invalidParams, arg),\n\n /**\n * Get a JSON RPC 2.0 Method Not Found (-32601) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n methodNotFound: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.methodNotFound, arg),\n\n /**\n * Get a JSON RPC 2.0 Internal (-32603) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n internal: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.internal, arg),\n\n /**\n * Get a JSON RPC 2.0 Server error.\n * Permits integer error codes in the [ -32099 <= -32005 ] range.\n * Codes -32000 through -32004 are reserved by EIP-1474.\n *\n * @param opts - The error options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n server: (\n opts: ServerErrorOptions,\n ) => {\n if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {\n throw new Error(\n 'Ethereum RPC Server errors must provide single object argument.',\n );\n }\n const { code } = opts;\n if (!Number.isInteger(code) || code > -32005 || code < -32099) {\n throw new Error(\n '\"code\" must be an integer such that: -32099 <= code <= -32005',\n );\n }\n return getJsonRpcError(code, opts);\n },\n\n /**\n * Get an Ethereum JSON RPC Invalid Input (-32000) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n invalidInput: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.invalidInput, arg),\n\n /**\n * Get an Ethereum JSON RPC Resource Not Found (-32001) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n resourceNotFound: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.resourceNotFound, arg),\n\n /**\n * Get an Ethereum JSON RPC Resource Unavailable (-32002) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n resourceUnavailable: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.resourceUnavailable, arg),\n\n /**\n * Get an Ethereum JSON RPC Transaction Rejected (-32003) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n transactionRejected: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.transactionRejected, arg),\n\n /**\n * Get an Ethereum JSON RPC Method Not Supported (-32004) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n methodNotSupported: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.methodNotSupported, arg),\n\n /**\n * Get an Ethereum JSON RPC Limit Exceeded (-32005) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\n limitExceeded: (\n arg?: JsonRpcErrorsArg,\n ) => getJsonRpcError(errorCodes.rpc.limitExceeded, arg),\n};\n\nexport const providerErrors = {\n /**\n * Get an Ethereum Provider User Rejected Request (4001) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\n userRejectedRequest: (\n arg?: JsonRpcErrorsArg,\n ) => {\n return getEthProviderError(errorCodes.provider.userRejectedRequest, arg);\n },\n\n /**\n * Get an Ethereum Provider Unauthorized (4100) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\n unauthorized: (\n arg?: JsonRpcErrorsArg,\n ) => {\n return getEthProviderError(errorCodes.provider.unauthorized, arg);\n },\n\n /**\n * Get an Ethereum Provider Unsupported Method (4200) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\n unsupportedMethod: (\n arg?: JsonRpcErrorsArg,\n ) => {\n return getEthProviderError(errorCodes.provider.unsupportedMethod, arg);\n },\n\n /**\n * Get an Ethereum Provider Not Connected (4900) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\n disconnected: (\n arg?: JsonRpcErrorsArg,\n ) => {\n return getEthProviderError(errorCodes.provider.disconnected, arg);\n },\n\n /**\n * Get an Ethereum Provider Chain Not Connected (4901) error.\n *\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\n chainDisconnected: (\n arg?: JsonRpcErrorsArg,\n ) => {\n return getEthProviderError(errorCodes.provider.chainDisconnected, arg);\n },\n\n /**\n * Get a custom Ethereum Provider error.\n *\n * @param opts - The error options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\n custom: (\n opts: CustomErrorArg,\n ) => {\n if (!opts || typeof opts !== 'object' || Array.isArray(opts)) {\n throw new Error(\n 'Ethereum Provider custom errors must provide single object argument.',\n );\n }\n\n const { code, message, data } = opts;\n\n if (!message || typeof message !== 'string') {\n throw new Error('\"message\" must be a nonempty string');\n }\n return new EthereumProviderError(code, message, data);\n },\n};\n\n/**\n * Get a generic JSON-RPC error class instance.\n *\n * @param code - The error code.\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link JsonRpcError} class.\n */\nfunction getJsonRpcError(\n code: number,\n arg?: JsonRpcErrorsArg,\n): JsonRpcError {\n const [message, data] = parseOpts(arg);\n return new JsonRpcError(code, message ?? getMessageFromCode(code), data);\n}\n\n/**\n * Get an Ethereum Provider error class instance.\n *\n * @param code - The error code.\n * @param arg - The error message or options bag.\n * @returns An instance of the {@link EthereumProviderError} class.\n */\nfunction getEthProviderError(\n code: number,\n arg?: JsonRpcErrorsArg,\n): EthereumProviderError {\n const [message, data] = parseOpts(arg);\n return new EthereumProviderError(\n code,\n message ?? getMessageFromCode(code),\n data,\n );\n}\n\n/**\n * Get an error message and optional data from an options bag.\n *\n * @param arg - The error message or options bag.\n * @returns A tuple containing the error message and optional data.\n */\nfunction parseOpts(\n arg?: JsonRpcErrorsArg,\n): [message?: string | undefined, data?: T | undefined] {\n if (arg) {\n if (typeof arg === 'string') {\n return [arg];\n } else if (typeof arg === 'object' && !Array.isArray(arg)) {\n const { message, data } = arg;\n\n if (message && typeof message !== 'string') {\n throw new Error('Must specify string message.');\n }\n return [message ?? undefined, data];\n }\n }\n\n return [];\n}\n"]} +\ No newline at end of file +diff --git a/dist/utils.d.ts b/dist/utils.d.ts +index f2b6a97b759c7b451c7310813efe92c75a9a9552..7fdec5cf4115e2ea70a2d9129e508519e9593e7e 100644 +--- a/dist/utils.d.ts ++++ b/dist/utils.d.ts +@@ -8,8 +8,15 @@ import { Json, JsonRpcError as SerializedJsonRpcError } from '@metamask/utils'; + */ + export type DataWithOptionalCause = Json | { + [key: string]: Json | unknown; +- cause: unknown; ++ cause?: unknown; + }; ++/** ++ * A data object, that must be either: ++ * ++ * - A valid DataWithOptionalCause value. ++ * - undefined. ++ */ ++export type OptionalDataWithOptionalCause = undefined | DataWithOptionalCause; + export declare const JSON_RPC_SERVER_ERROR_MESSAGE = "Unspecified server error."; + /** + * Gets the message for a given code, or a fallback message if the code has +diff --git a/dist/utils.js.map b/dist/utils.js.map +index 88bf91546901530c281d73174775fdefb2068031..9682f83f4bdbb6bd9b64dc8c9e42770da2aaba35 100644 +--- a/dist/utils.js.map ++++ b/dist/utils.js.map +@@ -1 +1 @@ +-{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,2CAQyB;AAEzB,uDAA4D;AAmB5D,MAAM,mBAAmB,GAAG,4BAAU,CAAC,GAAG,CAAC,QAAQ,CAAC;AACpD,MAAM,gBAAgB,GACpB,6DAA6D,CAAC;AAChE,MAAM,cAAc,GAA2B;IAC7C,IAAI,EAAE,mBAAmB;IACzB,OAAO,EAAE,kBAAkB,CAAC,mBAAmB,CAAC;CACjD,CAAC;AAEW,QAAA,6BAA6B,GAAG,2BAA2B,CAAC;AAIzE;;;;;;;;;GASG;AACH,SAAgB,kBAAkB,CAChC,IAAa,EACb,kBAA0B,gBAAgB;IAE1C,IAAI,WAAW,CAAC,IAAI,CAAC,EAAE;QACrB,MAAM,UAAU,GAAG,IAAI,CAAC,QAAQ,EAAE,CAAC;QAEnC,IAAI,IAAA,mBAAW,EAAC,6BAAW,EAAE,UAAU,CAAC,EAAE;YACxC,OAAO,6BAAW,CAAC,UAA2B,CAAC,CAAC,OAAO,CAAC;SACzD;QAED,IAAI,oBAAoB,CAAC,IAAI,CAAC,EAAE;YAC9B,OAAO,qCAA6B,CAAC;SACtC;KACF;IACD,OAAO,eAAe,CAAC;AACzB,CAAC;AAhBD,gDAgBC;AAED;;;;;;GAMG;AACH,SAAgB,WAAW,CAAC,IAAa;IACvC,OAAO,MAAM,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC;AAChC,CAAC;AAFD,kCAEC;AAED;;;;;;;;;;;;GAYG;AACH,SAAgB,cAAc,CAC5B,KAAc,EACd,EAAE,aAAa,GAAG,cAAc,EAAE,kBAAkB,GAAG,IAAI,EAAE,GAAG,EAAE;IAElE,IAAI,CAAC,IAAA,sBAAc,EAAC,aAAa,CAAC,EAAE;QAClC,MAAM,IAAI,KAAK,CACb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AC;QACd,CAAC,CAAC,CAAC;KACJ;SAAM,IAAI,IAAA,gBAAQ,EAAC,KAAK,CAAC,EAAE;QAC1B,OAAO,eAAe,CAAC,KAAK,CAAC,CAAC;KAC/B;IAED,IAAI,IAAA,mBAAW,EAAC,KAAK,CAAC,EAAE;QACtB,OAAO,KAAK,CAAC;KACd;IAED,OAAO,IAAI,CAAC;AACd,CAAC;AAnBD,wCAmBC;AAED;;;;;GAKG;AACH,SAAS,eAAe,CAAC,MAAqB;IAC5C,OAAO,MAAM,CAAC,mBAAmB,CAAC,MAAM,CAAC,CAAC,MAAM,CAC9C,CAAC,GAAG,EAAE,GAAG,EAAE,EAAE;QACX,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QAC1B,IAAI,IAAA,mBAAW,EAAC,KAAK,CAAC,EAAE;YACtB,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;SAClB;QAED,OAAO,GAAG,CAAC;IACb,CAAC,EACD,EAAE,CACH,CAAC;AACJ,CAAC","sourcesContent":["import {\n hasProperty,\n isValidJson,\n isObject,\n isJsonRpcError,\n Json,\n JsonRpcError as SerializedJsonRpcError,\n RuntimeObject,\n} from '@metamask/utils';\n\nimport { errorCodes, errorValues } from './error-constants';\n\n/**\n * A data object, that must be either:\n *\n * - A JSON-serializable object.\n * - An object with a `cause` property that is an error-like value, and any\n * other properties that are JSON-serializable.\n */\nexport type DataWithOptionalCause =\n | Json\n | {\n // Unfortunately we can't use just `Json` here, because all properties of\n // an object with an index signature must be assignable to the index\n // signature's type. So we have to use `Json | unknown` instead.\n [key: string]: Json | unknown;\n cause: unknown;\n };\n\nconst FALLBACK_ERROR_CODE = errorCodes.rpc.internal;\nconst FALLBACK_MESSAGE =\n 'Unspecified error message. This is a bug, please report it.';\nconst FALLBACK_ERROR: SerializedJsonRpcError = {\n code: FALLBACK_ERROR_CODE,\n message: getMessageFromCode(FALLBACK_ERROR_CODE),\n};\n\nexport const JSON_RPC_SERVER_ERROR_MESSAGE = 'Unspecified server error.';\n\ntype ErrorValueKey = keyof typeof errorValues;\n\n/**\n * Gets the message for a given code, or a fallback message if the code has\n * no corresponding message.\n *\n * @param code - The error code.\n * @param fallbackMessage - The fallback message to use if the code has no\n * corresponding message.\n * @returns The message for the given code, or the fallback message if the code\n * has no corresponding message.\n */\nexport function getMessageFromCode(\n code: unknown,\n fallbackMessage: string = FALLBACK_MESSAGE,\n): string {\n if (isValidCode(code)) {\n const codeString = code.toString();\n\n if (hasProperty(errorValues, codeString)) {\n return errorValues[codeString as ErrorValueKey].message;\n }\n\n if (isJsonRpcServerError(code)) {\n return JSON_RPC_SERVER_ERROR_MESSAGE;\n }\n }\n return fallbackMessage;\n}\n\n/**\n * Returns whether the given code is valid.\n * A code is valid if it is an integer.\n *\n * @param code - The error code.\n * @returns Whether the given code is valid.\n */\nexport function isValidCode(code: unknown): code is number {\n return Number.isInteger(code);\n}\n\n/**\n * Serializes the given error to an Ethereum JSON RPC-compatible error object.\n * If the given error is not fully compatible, it will be preserved on the\n * returned object's data.cause property.\n *\n * @param error - The error to serialize.\n * @param options - Options bag.\n * @param options.fallbackError - The error to return if the given error is\n * not compatible. Should be a JSON serializable value.\n * @param options.shouldIncludeStack - Whether to include the error's stack\n * on the returned object.\n * @returns The serialized error.\n */\nexport function serializeError(\n error: unknown,\n { fallbackError = FALLBACK_ERROR, shouldIncludeStack = true } = {},\n): SerializedJsonRpcError {\n if (!isJsonRpcError(fallbackError)) {\n throw new Error(\n 'Must provide fallback error with integer number code and string message.',\n );\n }\n\n const serialized = buildError(error, fallbackError);\n\n if (!shouldIncludeStack) {\n delete serialized.stack;\n }\n\n return serialized;\n}\n\n/**\n * Construct a JSON-serializable object given an error and a JSON serializable `fallbackError`\n *\n * @param error - The error in question.\n * @param fallbackError - A JSON serializable fallback error.\n * @returns A JSON serializable error object.\n */\nfunction buildError(\n error: unknown,\n fallbackError: SerializedJsonRpcError,\n): SerializedJsonRpcError {\n // If an error specifies a `serialize` function, we call it and return the result.\n if (\n error &&\n typeof error === 'object' &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n return error.serialize();\n }\n\n if (isJsonRpcError(error)) {\n return error;\n }\n\n // If the error does not match the JsonRpcError type, use the fallback error, but try to include the original error as `cause`.\n const cause = serializeCause(error);\n const fallbackWithCause = {\n ...fallbackError,\n data: { cause },\n };\n\n return fallbackWithCause;\n}\n\n/**\n * Check if the given code is a valid JSON-RPC server error code.\n *\n * @param code - The error code.\n * @returns Whether the given code is a valid JSON-RPC server error code.\n */\nfunction isJsonRpcServerError(code: number): boolean {\n return code >= -32099 && code <= -32000;\n}\n\n/**\n * Serializes an unknown error to be used as the `cause` in a fallback error.\n *\n * @param error - The unknown error.\n * @returns A JSON-serializable object containing as much information about the original error as possible.\n */\nexport function serializeCause(error: unknown): Json {\n if (Array.isArray(error)) {\n return error.map((entry) => {\n if (isValidJson(entry)) {\n return entry;\n } else if (isObject(entry)) {\n return serializeObject(entry);\n }\n return null;\n });\n } else if (isObject(error)) {\n return serializeObject(error);\n }\n\n if (isValidJson(error)) {\n return error;\n }\n\n return null;\n}\n\n/**\n * Extracts all JSON-serializable properties from an object.\n *\n * @param object - The object in question.\n * @returns An object containing all the JSON-serializable properties.\n */\nfunction serializeObject(object: RuntimeObject): Json {\n return Object.getOwnPropertyNames(object).reduce>(\n (acc, key) => {\n const value = object[key];\n if (isValidJson(value)) {\n acc[key] = value;\n }\n\n return acc;\n },\n {},\n );\n}\n"]} +\ No newline at end of file ++{"version":3,"file":"utils.js","sourceRoot":"","sources":["../src/utils.ts"],"names":[],"mappings":";;;AAAA,2CAQyB;AAEzB,uDAA4D;AA2B5D,MAAM,mBAAmB,GAAG,4BAAU,CAAC,GAAG,CAAC,QAAQ,CAAC;AACpD,MAAM,gBAAgB,GACpB,6DAA6D,CAAC;AAChE,MAAM,cAAc,GAA2B;IAC7C,IAAI,EAAE,mBAAmB;IACzB,OAAO,EAAE,kBAAkB,CAAC,mBAAmB,CAAC;CAC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E;QACX,MAAM,KAAK,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC;QAC1B,IAAI,IAAA,mBAAW,EAAC,KAAK,CAAC,EAAE;YACtB,GAAG,CAAC,GAAG,CAAC,GAAG,KAAK,CAAC;SAClB;QAED,OAAO,GAAG,CAAC;IACb,CAAC,EACD,EAAE,CACH,CAAC;AACJ,CAAC","sourcesContent":["import {\n hasProperty,\n isValidJson,\n isObject,\n isJsonRpcError,\n Json,\n JsonRpcError as SerializedJsonRpcError,\n RuntimeObject,\n} from '@metamask/utils';\n\nimport { errorCodes, errorValues } from './error-constants';\n\n/**\n * A data object, that must be either:\n *\n * - A JSON-serializable object.\n * - An object with a `cause` property that is an error-like value, and any\n * other properties that are JSON-serializable.\n */\nexport type DataWithOptionalCause =\n | Json\n | {\n // Unfortunately we can't use just `Json` here, because all properties of\n // an object with an index signature must be assignable to the index\n // signature's type. So we have to use `Json | unknown` instead.\n [key: string]: Json | unknown;\n cause?: unknown;\n };\n\n/**\n * A data object, that must be either:\n *\n * - A valid DataWithOptionalCause value.\n * - undefined.\n */\nexport type OptionalDataWithOptionalCause = undefined | DataWithOptionalCause;\n\nconst FALLBACK_ERROR_CODE = errorCodes.rpc.internal;\nconst FALLBACK_MESSAGE =\n 'Unspecified error message. This is a bug, please report it.';\nconst FALLBACK_ERROR: SerializedJsonRpcError = {\n code: FALLBACK_ERROR_CODE,\n message: getMessageFromCode(FALLBACK_ERROR_CODE),\n};\n\nexport const JSON_RPC_SERVER_ERROR_MESSAGE = 'Unspecified server error.';\n\ntype ErrorValueKey = keyof typeof errorValues;\n\n/**\n * Gets the message for a given code, or a fallback message if the code has\n * no corresponding message.\n *\n * @param code - The error code.\n * @param fallbackMessage - The fallback message to use if the code has no\n * corresponding message.\n * @returns The message for the given code, or the fallback message if the code\n * has no corresponding message.\n */\nexport function getMessageFromCode(\n code: unknown,\n fallbackMessage: string = FALLBACK_MESSAGE,\n): string {\n if (isValidCode(code)) {\n const codeString = code.toString();\n\n if (hasProperty(errorValues, codeString)) {\n return errorValues[codeString as ErrorValueKey].message;\n }\n\n if (isJsonRpcServerError(code)) {\n return JSON_RPC_SERVER_ERROR_MESSAGE;\n }\n }\n return fallbackMessage;\n}\n\n/**\n * Returns whether the given code is valid.\n * A code is valid if it is an integer.\n *\n * @param code - The error code.\n * @returns Whether the given code is valid.\n */\nexport function isValidCode(code: unknown): code is number {\n return Number.isInteger(code);\n}\n\n/**\n * Serializes the given error to an Ethereum JSON RPC-compatible error object.\n * If the given error is not fully compatible, it will be preserved on the\n * returned object's data.cause property.\n *\n * @param error - The error to serialize.\n * @param options - Options bag.\n * @param options.fallbackError - The error to return if the given error is\n * not compatible. Should be a JSON serializable value.\n * @param options.shouldIncludeStack - Whether to include the error's stack\n * on the returned object.\n * @returns The serialized error.\n */\nexport function serializeError(\n error: unknown,\n { fallbackError = FALLBACK_ERROR, shouldIncludeStack = true } = {},\n): SerializedJsonRpcError {\n if (!isJsonRpcError(fallbackError)) {\n throw new Error(\n 'Must provide fallback error with integer number code and string message.',\n );\n }\n\n const serialized = buildError(error, fallbackError);\n\n if (!shouldIncludeStack) {\n delete serialized.stack;\n }\n\n return serialized;\n}\n\n/**\n * Construct a JSON-serializable object given an error and a JSON serializable `fallbackError`\n *\n * @param error - The error in question.\n * @param fallbackError - A JSON serializable fallback error.\n * @returns A JSON serializable error object.\n */\nfunction buildError(\n error: unknown,\n fallbackError: SerializedJsonRpcError,\n): SerializedJsonRpcError {\n // If an error specifies a `serialize` function, we call it and return the result.\n if (\n error &&\n typeof error === 'object' &&\n 'serialize' in error &&\n typeof error.serialize === 'function'\n ) {\n return error.serialize();\n }\n\n if (isJsonRpcError(error)) {\n return error;\n }\n\n // If the error does not match the JsonRpcError type, use the fallback error, but try to include the original error as `cause`.\n const cause = serializeCause(error);\n const fallbackWithCause = {\n ...fallbackError,\n data: { cause },\n };\n\n return fallbackWithCause;\n}\n\n/**\n * Check if the given code is a valid JSON-RPC server error code.\n *\n * @param code - The error code.\n * @returns Whether the given code is a valid JSON-RPC server error code.\n */\nfunction isJsonRpcServerError(code: number): boolean {\n return code >= -32099 && code <= -32000;\n}\n\n/**\n * Serializes an unknown error to be used as the `cause` in a fallback error.\n *\n * @param error - The unknown error.\n * @returns A JSON-serializable object containing as much information about the original error as possible.\n */\nexport function serializeCause(error: unknown): Json {\n if (Array.isArray(error)) {\n return error.map((entry) => {\n if (isValidJson(entry)) {\n return entry;\n } else if (isObject(entry)) {\n return serializeObject(entry);\n }\n return null;\n });\n } else if (isObject(error)) {\n return serializeObject(error);\n }\n\n if (isValidJson(error)) {\n return error;\n }\n\n return null;\n}\n\n/**\n * Extracts all JSON-serializable properties from an object.\n *\n * @param object - The object in question.\n * @returns An object containing all the JSON-serializable properties.\n */\nfunction serializeObject(object: RuntimeObject): Json {\n return Object.getOwnPropertyNames(object).reduce>(\n (acc, key) => {\n const value = object[key];\n if (isValidJson(value)) {\n acc[key] = value;\n }\n\n return acc;\n },\n {},\n );\n}\n"]} +\ No newline at end of file diff --git a/package.json b/package.json index 35aee272..717f0d94 100644 --- a/package.json +++ b/package.json @@ -95,5 +95,8 @@ }, "lavamoat": { "allowScripts": {} + }, + "resolutions": { + "@metamask/rpc-errors@^5.1.1": "patch:@metamask/rpc-errors@npm:5.1.1#.yarn/patches/@metamask-rpc-errors-npm-5.1.1-dc76c26803.patch" } } diff --git a/yarn.lock b/yarn.lock index 4dbfd1b4..cd94cd1d 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1111,7 +1111,7 @@ __metadata: languageName: unknown linkType: soft -"@metamask/rpc-errors@npm:^5.0.0, @metamask/rpc-errors@npm:^5.1.1": +"@metamask/rpc-errors@npm:5.1.1, @metamask/rpc-errors@npm:^5.0.0": version: 5.1.1 resolution: "@metamask/rpc-errors@npm:5.1.1" dependencies: @@ -1121,6 +1121,16 @@ __metadata: languageName: node linkType: hard +"@metamask/rpc-errors@patch:@metamask/rpc-errors@npm:5.1.1#.yarn/patches/@metamask-rpc-errors-npm-5.1.1-dc76c26803.patch::locator=%40metamask%2Fproviders%40workspace%3A.": + version: 5.1.1 + resolution: "@metamask/rpc-errors@patch:@metamask/rpc-errors@npm%3A5.1.1#.yarn/patches/@metamask-rpc-errors-npm-5.1.1-dc76c26803.patch::version=5.1.1&hash=cfc1a2&locator=%40metamask%2Fproviders%40workspace%3A." + dependencies: + "@metamask/utils": ^5.0.0 + fast-safe-stringify: ^2.0.6 + checksum: 3a382e33ed163632070c6c49f6ba226723566af8b568c99f9d5d5a5e992643ab16f52d95756ea3d6ccec01f18bf574659643f00d2f63fb28f25d695571b5764e + languageName: node + linkType: hard + "@metamask/safe-event-emitter@npm:^2.0.0": version: 2.0.0 resolution: "@metamask/safe-event-emitter@npm:2.0.0" From 2534c28ae650baa1e2613746577416adcb826ae9 Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Thu, 24 Aug 2023 04:53:47 +0000 Subject: [PATCH 4/6] deps:@metamask/utils@^6.2.0->^7.1.0 --- package.json | 2 +- yarn.lock | 23 +++++++++++++++++------ 2 files changed, 18 insertions(+), 7 deletions(-) diff --git a/package.json b/package.json index 717f0d94..83b2dc0f 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "@metamask/object-multiplex": "^1.1.0", "@metamask/rpc-errors": "^5.1.1", "@metamask/safe-event-emitter": "^3.0.0", - "@metamask/utils": "^6.2.0", + "@metamask/utils": "^7.1.0", "detect-browser": "^5.2.0", "extension-port-stream": "^2.0.1", "fast-deep-equal": "^3.1.3", diff --git a/yarn.lock b/yarn.lock index cd94cd1d..11e9f0de 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1073,7 +1073,7 @@ __metadata: "@metamask/object-multiplex": ^1.1.0 "@metamask/rpc-errors": ^5.1.1 "@metamask/safe-event-emitter": ^3.0.0 - "@metamask/utils": ^6.2.0 + "@metamask/utils": ^7.1.0 "@types/chrome": ^0.0.233 "@types/jest": ^28.1.6 "@types/node": ^17.0.23 @@ -1158,17 +1158,17 @@ __metadata: languageName: node linkType: hard -"@metamask/utils@npm:^6.2.0": - version: 6.2.0 - resolution: "@metamask/utils@npm:6.2.0" +"@metamask/utils@npm:^7.1.0": + version: 7.1.0 + resolution: "@metamask/utils@npm:7.1.0" dependencies: "@ethereumjs/tx": ^4.1.2 "@noble/hashes": ^1.3.1 "@types/debug": ^4.1.7 debug: ^4.3.4 - semver: ^7.3.8 + semver: ^7.5.4 superstruct: ^1.0.3 - checksum: 0bc675358ecc09b3bc04da613d73666295d7afa51ff6b8554801585966900b24b8545bd93b8b2e9a17db867ebe421fe884baf3558ec4ca3199fa65504f677c1b + checksum: 165ed378f4ac5ca42c241d32154e15b609f9e772a9dc069b870613c005dc0e7e4fa92204c30e98ec2317f1e38c77747057671a26fd0a5ba36a288e3c9ef03790 languageName: node linkType: hard @@ -6253,6 +6253,17 @@ __metadata: languageName: node linkType: hard +"semver@npm:^7.5.4": + version: 7.5.4 + resolution: "semver@npm:7.5.4" + dependencies: + lru-cache: ^6.0.0 + bin: + semver: bin/semver.js + checksum: 12d8ad952fa353b0995bf180cdac205a4068b759a140e5d3c608317098b3575ac2f1e09182206bf2eb26120e1c0ed8fb92c48c592f6099680de56bb071423ca3 + languageName: node + linkType: hard + "set-blocking@npm:^2.0.0": version: 2.0.0 resolution: "set-blocking@npm:2.0.0" From f5a52918b08e67d6a13c27dcb0af67c58710d0ad Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Thu, 24 Aug 2023 04:56:05 +0000 Subject: [PATCH 5/6] lint:fix --- package.json | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/package.json b/package.json index 83b2dc0f..95828478 100644 --- a/package.json +++ b/package.json @@ -36,6 +36,9 @@ "test": "jest && jest-it-up", "test:watch": "jest --watch" }, + "resolutions": { + "@metamask/rpc-errors@^5.1.1": "patch:@metamask/rpc-errors@npm:5.1.1#.yarn/patches/@metamask-rpc-errors-npm-5.1.1-dc76c26803.patch" + }, "dependencies": { "@metamask/json-rpc-engine": "^7.0.0", "@metamask/object-multiplex": "^1.1.0", @@ -95,8 +98,5 @@ }, "lavamoat": { "allowScripts": {} - }, - "resolutions": { - "@metamask/rpc-errors@^5.1.1": "patch:@metamask/rpc-errors@npm:5.1.1#.yarn/patches/@metamask-rpc-errors-npm-5.1.1-dc76c26803.patch" } } From ecd7899c258c88552772da9fc24401396293523f Mon Sep 17 00:00:00 2001 From: legobt <6wbvkn0j@anonaddy.me> Date: Thu, 24 Aug 2023 05:01:06 +0000 Subject: [PATCH 6/6] yarn dedupe --- yarn.lock | 19 ++++--------------- 1 file changed, 4 insertions(+), 15 deletions(-) diff --git a/yarn.lock b/yarn.lock index 11e9f0de..7f7f1578 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6233,14 +6233,14 @@ __metadata: languageName: node linkType: hard -"semver@npm:7.x, semver@npm:^7.3.2, semver@npm:^7.3.5, semver@npm:^7.3.7, semver@npm:^7.3.8": - version: 7.5.0 - resolution: "semver@npm:7.5.0" +"semver@npm:7.x, semver@npm:^7.3.2, semver@npm:^7.3.5, semver@npm:^7.3.7, semver@npm:^7.3.8, semver@npm:^7.5.4": + version: 7.5.4 + resolution: "semver@npm:7.5.4" dependencies: lru-cache: ^6.0.0 bin: semver: bin/semver.js - checksum: 2d266937756689a76f124ffb4c1ea3e1bbb2b263219f90ada8a11aebebe1280b13bb76cca2ca96bdee3dbc554cbc0b24752eb895b2a51577aa644427e9229f2b + checksum: 12d8ad952fa353b0995bf180cdac205a4068b759a140e5d3c608317098b3575ac2f1e09182206bf2eb26120e1c0ed8fb92c48c592f6099680de56bb071423ca3 languageName: node linkType: hard @@ -6253,17 +6253,6 @@ __metadata: languageName: node linkType: hard -"semver@npm:^7.5.4": - version: 7.5.4 - resolution: "semver@npm:7.5.4" - dependencies: - lru-cache: ^6.0.0 - bin: - semver: bin/semver.js - checksum: 12d8ad952fa353b0995bf180cdac205a4068b759a140e5d3c608317098b3575ac2f1e09182206bf2eb26120e1c0ed8fb92c48c592f6099680de56bb071423ca3 - languageName: node - linkType: hard - "set-blocking@npm:^2.0.0": version: 2.0.0 resolution: "set-blocking@npm:2.0.0"