diff --git a/packages/snaps-execution-environments/coverage.json b/packages/snaps-execution-environments/coverage.json index a22d613aa7..d7ef84e23c 100644 --- a/packages/snaps-execution-environments/coverage.json +++ b/packages/snaps-execution-environments/coverage.json @@ -1,6 +1,6 @@ { "branches": 90.74, "functions": 94.96, - "lines": 90.85, - "statements": 90.27 + "lines": 90.84, + "statements": 90.26 } diff --git a/packages/snaps-execution-environments/src/common/BaseSnapExecutor.ts b/packages/snaps-execution-environments/src/common/BaseSnapExecutor.ts index fd12eb2dbc..5202217e27 100644 --- a/packages/snaps-execution-environments/src/common/BaseSnapExecutor.ts +++ b/packages/snaps-execution-environments/src/common/BaseSnapExecutor.ts @@ -346,7 +346,7 @@ export class BaseSnapExecutor { }); } - async #respond(id: JsonRpcId, response: Record) { + async #respond(id: JsonRpcId, response: Record) { if (!isValidResponse(response)) { // Instead of throwing, we directly respond with an error. // This prevents an issue where we wouldn't respond when errors were non-serializable diff --git a/packages/snaps-execution-environments/src/common/utils.test.ts b/packages/snaps-execution-environments/src/common/utils.test.ts index 8edf9a538a..d0fb4ccd08 100644 --- a/packages/snaps-execution-environments/src/common/utils.test.ts +++ b/packages/snaps-execution-environments/src/common/utils.test.ts @@ -63,10 +63,6 @@ describe('isValidResponse', () => { expect(isValidResponse('foo')).toBe(false); }); - it('returns false if the value is not JSON serializable', () => { - expect(isValidResponse({ foo: BigInt(0) })).toBe(false); - }); - it('returns false if the value is too large', () => { expect(isValidResponse({ foo: '1'.repeat(100_000_000) })).toBe(false); }); diff --git a/packages/snaps-execution-environments/src/common/utils.ts b/packages/snaps-execution-environments/src/common/utils.ts index d7e143e086..96a414bb6c 100644 --- a/packages/snaps-execution-environments/src/common/utils.ts +++ b/packages/snaps-execution-environments/src/common/utils.ts @@ -1,6 +1,8 @@ import type { RequestArguments } from '@metamask/providers'; import { rpcErrors } from '@metamask/rpc-errors'; -import { assert, getJsonSize, getSafeJson, isObject } from '@metamask/utils'; +import { getJsonSizeUnsafe } from '@metamask/snaps-utils'; +import type { Json } from '@metamask/utils'; +import { assert, getSafeJson, isObject } from '@metamask/utils'; import { log } from '../logging'; @@ -128,16 +130,13 @@ export function sanitizeRequestArguments(value: unknown): RequestArguments { * @param response - The response. * @returns True if the response is valid, otherwise false. */ -export function isValidResponse(response: Record) { +export function isValidResponse(response: Record) { if (!isObject(response)) { return false; } - try { - // If the JSON is invalid this will throw and we should return false. - const size = getJsonSize(response); - return size < MAX_RESPONSE_JSON_SIZE; - } catch { - return false; - } + // We know that the response is valid JSON already and we don't + // need the size to be exact. + const size = getJsonSizeUnsafe(response); + return size < MAX_RESPONSE_JSON_SIZE; } diff --git a/packages/snaps-utils/src/index.executionenv.ts b/packages/snaps-utils/src/index.executionenv.ts index 65b7b13096..e89c458c88 100644 --- a/packages/snaps-utils/src/index.executionenv.ts +++ b/packages/snaps-utils/src/index.executionenv.ts @@ -3,5 +3,6 @@ export * from './errors'; export * from './handlers/exports'; export * from './handlers/types'; export * from './iframe'; +export * from './json'; export * from './logging'; export * from './types';