diff --git a/jest.config.js b/jest.config.js index 701b3064..322de793 100644 --- a/jest.config.js +++ b/jest.config.js @@ -41,10 +41,10 @@ module.exports = { // An object that configures minimum threshold enforcement for coverage results coverageThreshold: { global: { - branches: 68.67, - functions: 92.59, - lines: 90.2, - statements: 90.42, + branches: 70.37, + functions: 92.72, + lines: 90.72, + statements: 90.93, }, }, preset: 'ts-jest', diff --git a/src/KeyringController.ts b/src/KeyringController.ts index 901fe4e9..a70017f8 100644 --- a/src/KeyringController.ts +++ b/src/KeyringController.ts @@ -3,7 +3,7 @@ import * as encryptorUtils from '@metamask/browser-passworder'; import HDKeyring from '@metamask/eth-hd-keyring'; import { normalize as normalizeToHex } from '@metamask/eth-sig-util'; import SimpleKeyring from '@metamask/eth-simple-keyring'; -import { remove0x, isValidJson } from '@metamask/utils'; +import { remove0x } from '@metamask/utils'; import type { Hex, Json, @@ -87,7 +87,7 @@ class KeyringController extends EventEmitter { * * @returns The controller state. */ - #fullUpdate() { + fullUpdate() { this.emit('update', this.memStore.getState()); return this.memStore.getState(); } @@ -117,7 +117,7 @@ class KeyringController extends EventEmitter { await this.#createFirstKeyTree(); this.#setUnlocked(); - return this.#fullUpdate(); + return this.fullUpdate(); } /** @@ -154,7 +154,7 @@ class KeyringController extends EventEmitter { throw new Error(KeyringControllerError.NoFirstAccount); } this.#setUnlocked(); - return this.#fullUpdate(); + return this.fullUpdate(); } /** @@ -178,7 +178,7 @@ class KeyringController extends EventEmitter { this.keyrings = []; await this.updateMemStoreKeyrings(); this.emit('lock'); - return this.#fullUpdate(); + return this.fullUpdate(); } /** @@ -198,7 +198,7 @@ class KeyringController extends EventEmitter { this.keyrings = await this.unlockKeyrings(password); this.#setUnlocked(); - return this.#fullUpdate(); + return this.fullUpdate(); } /** @@ -222,7 +222,7 @@ class KeyringController extends EventEmitter { encryptionSalt, ); this.#setUnlocked(); - return this.#fullUpdate(); + return this.fullUpdate(); } /** @@ -265,7 +265,7 @@ class KeyringController extends EventEmitter { }); await this.persistAllKeyrings(); - return this.#fullUpdate(); + return this.fullUpdate(); } /** @@ -285,7 +285,7 @@ class KeyringController extends EventEmitter { throw new Error(KeyringControllerError.UnsupportedExportAccount); } - return await keyring.exportAccount(normalizeToHex(address)); + return await keyring.exportAccount(normalizeToHex(address) as Hex); } /** @@ -314,7 +314,7 @@ class KeyringController extends EventEmitter { } await this.persistAllKeyrings(); - return this.#fullUpdate(); + return this.fullUpdate(); } /** @@ -388,7 +388,7 @@ class KeyringController extends EventEmitter { rawAddress: string, opts: Record = {}, ): Promise { - const address = normalizeToHex(rawAddress); + const address = normalizeToHex(rawAddress) as Hex; const keyring = await this.getKeyringForAccount(address); if (!keyring.signTransaction) { throw new Error(KeyringControllerError.UnsupportedSignTransaction); @@ -415,7 +415,7 @@ class KeyringController extends EventEmitter { }, opts: Record = {}, ): Promise { - const address = normalizeToHex(msgParams.from); + const address = normalizeToHex(msgParams.from) as Hex; const keyring = await this.getKeyringForAccount(address); if (!keyring.signMessage) { throw new Error(KeyringControllerError.UnsupportedSignMessage); @@ -443,13 +443,13 @@ class KeyringController extends EventEmitter { }, opts: Record = {}, ): Promise { - const address = normalizeToHex(msgParams.from); + const address = normalizeToHex(msgParams.from) as Hex; const keyring = await this.getKeyringForAccount(address); if (!keyring.signPersonalMessage) { throw new Error(KeyringControllerError.UnsupportedSignPersonalMessage); } - const normalizedData = normalizeToHex(msgParams.data); + const normalizedData = normalizeToHex(msgParams.data) as Hex; return await keyring.signPersonalMessage(address, normalizedData, opts); } @@ -467,7 +467,7 @@ class KeyringController extends EventEmitter { address: string, opts: Record = {}, ): Promise { - const normalizedAddress = normalizeToHex(address); + const normalizedAddress = normalizeToHex(address) as Hex; const keyring = await this.getKeyringForAccount(address); if (!keyring.getEncryptionPublicKey) { throw new Error(KeyringControllerError.UnsupportedGetEncryptionPublicKey); @@ -490,7 +490,7 @@ class KeyringController extends EventEmitter { from: string; data: Eip1024EncryptedData; }): Promise { - const address = normalizeToHex(msgParams.from); + const address = normalizeToHex(msgParams.from) as Hex; const keyring = await this.getKeyringForAccount(address); if (!keyring.decryptMessage) { throw new Error(KeyringControllerError.UnsupportedDecryptMessage); @@ -536,7 +536,7 @@ class KeyringController extends EventEmitter { * @returns The app key address. */ async getAppKeyAddress(rawAddress: string, origin: string): Promise { - const address = normalizeToHex(rawAddress); + const address = normalizeToHex(rawAddress) as Hex; const keyring = await this.getKeyringForAccount(address); if (!keyring.getAppKeyAddress) { throw new Error(KeyringControllerError.UnsupportedGetAppKeyAddress); @@ -556,7 +556,7 @@ class KeyringController extends EventEmitter { rawAddress: string, origin: string, ): Promise { - const address = normalizeToHex(rawAddress); + const address = normalizeToHex(rawAddress) as Hex; const keyring = await this.getKeyringForAccount(address); if (!keyring.exportAccount) { throw new Error(KeyringControllerError.UnsupportedExportAppKeyForAddress); @@ -618,7 +618,7 @@ class KeyringController extends EventEmitter { this.keyrings.push(keyring); await this.persistAllKeyrings(); - this.#fullUpdate(); + this.fullUpdate(); return keyring; } @@ -756,11 +756,7 @@ class KeyringController extends EventEmitter { * @returns Keyrings matching the specified type. */ getKeyringsByType(type: string): Keyring[] { - const keyrings = this.keyrings.filter((keyring) => keyring.type === type); - if (!keyrings.length) { - throw new Error(KeyringControllerError.NoKeyring); - } - return keyrings; + return this.keyrings.filter((keyring) => keyring.type === type); } /** @@ -1030,10 +1026,7 @@ class KeyringController extends EventEmitter { const keyring = keyringBuilder(); - if (!isValidJson(data)) { - throw new Error(KeyringControllerError.DataType); - } - + // @ts-expect-error Enforce data type after updating clients await keyring.deserialize(data); if (keyring.init) { diff --git a/types/@metamask/eth-hd-keyring.d.ts b/src/types/@metamask/eth-hd-keyring.d.ts similarity index 100% rename from types/@metamask/eth-hd-keyring.d.ts rename to src/types/@metamask/eth-hd-keyring.d.ts diff --git a/types/@metamask/eth-sig-util.d.ts b/src/types/@metamask/eth-sig-util.d.ts similarity index 100% rename from types/@metamask/eth-sig-util.d.ts rename to src/types/@metamask/eth-sig-util.d.ts diff --git a/types/@metamask/eth-simple-keyring.d.ts b/src/types/@metamask/eth-simple-keyring.d.ts similarity index 100% rename from types/@metamask/eth-simple-keyring.d.ts rename to src/types/@metamask/eth-simple-keyring.d.ts diff --git a/types/obs-store.d.ts b/src/types/obs-store.d.ts similarity index 100% rename from types/obs-store.d.ts rename to src/types/obs-store.d.ts diff --git a/tsconfig.build.json b/tsconfig.build.json index 43db3a45..c6e00d65 100644 --- a/tsconfig.build.json +++ b/tsconfig.build.json @@ -8,11 +8,6 @@ "rootDir": "src", "sourceMap": true }, - "include": ["./src/**/*.ts", "./types"], - "exclude": [ - "./src/test/**/*.ts", - "./src/**/*.test.ts", - "./src/**/*.test-d.ts", - "./src/__fixtures__" - ] + "include": ["./src/**/*.ts"], + "exclude": ["./src/**/*.test.ts"] }