From 214cf7fb667338e9e84e1163d9094dc0bfecfb1c Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Wed, 24 Jan 2024 17:39:17 +0100 Subject: [PATCH] fix: port isSerializedKeyringsArray function --- packages/keyring-controller/src/KeyringController.ts | 3 ++- packages/keyring-controller/tests/mocks/mockEncryptor.ts | 8 +++----- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/packages/keyring-controller/src/KeyringController.ts b/packages/keyring-controller/src/KeyringController.ts index 262b548fe55..dd465481723 100644 --- a/packages/keyring-controller/src/KeyringController.ts +++ b/packages/keyring-controller/src/KeyringController.ts @@ -31,6 +31,7 @@ import { hasProperty, isObject, isValidHexAddress, + isValidJson, remove0x, } from '@metamask/utils'; import { Mutex } from 'async-mutex'; @@ -442,7 +443,7 @@ function isSerializedKeyringsArray( return ( typeof array === 'object' && Array.isArray(array) && - array.every((value) => value.type && value.data) + array.every((value) => value.type && isValidJson(value.data)) ); } diff --git a/packages/keyring-controller/tests/mocks/mockEncryptor.ts b/packages/keyring-controller/tests/mocks/mockEncryptor.ts index 39e74c6f28f..30a40b97ab9 100644 --- a/packages/keyring-controller/tests/mocks/mockEncryptor.ts +++ b/packages/keyring-controller/tests/mocks/mockEncryptor.ts @@ -1,5 +1,3 @@ -import type { Json } from '@metamask/utils'; - import type { ExportableKeyEncryptor } from '../../src/KeyringController'; export const PASSWORD = 'password123'; @@ -19,7 +17,7 @@ export const MOCK_HEX = '0xabcdef0123456789'; const MOCK_KEY = Buffer.alloc(32); const INVALID_PASSWORD_ERROR = 'Incorrect password.'; -let cacheVal: Json; +let cacheVal: string; export default class MockEncryptor implements ExportableKeyEncryptor { // TODO: Replace `any` with type @@ -36,13 +34,13 @@ export default class MockEncryptor implements ExportableKeyEncryptor { throw new Error(INVALID_PASSWORD_ERROR); } - return cacheVal ?? {}; + return JSON.parse(cacheVal) ?? {}; } // TODO: Replace `any` with type // eslint-disable-next-line @typescript-eslint/no-explicit-any async encryptWithKey(_key: unknown, dataObj: any) { - cacheVal = dataObj; + cacheVal = JSON.stringify(dataObj); return { data: MOCK_HEX, iv: 'anIv',