Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.

[FIX] Inconsistent data types and validations #228

Merged
merged 12 commits into from
May 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions jest.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
49 changes: 21 additions & 28 deletions src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -87,7 +87,7 @@ class KeyringController extends EventEmitter {
*
* @returns The controller state.
*/
#fullUpdate() {
fullUpdate() {
this.emit('update', this.memStore.getState());
return this.memStore.getState();
}
Expand Down Expand Up @@ -117,7 +117,7 @@ class KeyringController extends EventEmitter {

await this.#createFirstKeyTree();
this.#setUnlocked();
return this.#fullUpdate();
return this.fullUpdate();
}

/**
Expand Down Expand Up @@ -154,7 +154,7 @@ class KeyringController extends EventEmitter {
throw new Error(KeyringControllerError.NoFirstAccount);
}
this.#setUnlocked();
return this.#fullUpdate();
return this.fullUpdate();
}

/**
Expand All @@ -178,7 +178,7 @@ class KeyringController extends EventEmitter {
this.keyrings = [];
await this.updateMemStoreKeyrings();
this.emit('lock');
return this.#fullUpdate();
return this.fullUpdate();
}

/**
Expand All @@ -198,7 +198,7 @@ class KeyringController extends EventEmitter {
this.keyrings = await this.unlockKeyrings(password);

this.#setUnlocked();
return this.#fullUpdate();
return this.fullUpdate();
}

/**
Expand All @@ -222,7 +222,7 @@ class KeyringController extends EventEmitter {
encryptionSalt,
);
this.#setUnlocked();
return this.#fullUpdate();
return this.fullUpdate();
}

/**
Expand Down Expand Up @@ -265,7 +265,7 @@ class KeyringController extends EventEmitter {
});

await this.persistAllKeyrings();
return this.#fullUpdate();
return this.fullUpdate();
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -314,7 +314,7 @@ class KeyringController extends EventEmitter {
}

await this.persistAllKeyrings();
return this.#fullUpdate();
return this.fullUpdate();
}

/**
Expand Down Expand Up @@ -388,7 +388,7 @@ class KeyringController extends EventEmitter {
rawAddress: string,
opts: Record<string, unknown> = {},
): Promise<TxData> {
const address = normalizeToHex(rawAddress);
const address = normalizeToHex(rawAddress) as Hex;
const keyring = await this.getKeyringForAccount(address);
if (!keyring.signTransaction) {
throw new Error(KeyringControllerError.UnsupportedSignTransaction);
Expand All @@ -415,7 +415,7 @@ class KeyringController extends EventEmitter {
},
opts: Record<string, unknown> = {},
): Promise<string> {
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);
Expand Down Expand Up @@ -443,13 +443,13 @@ class KeyringController extends EventEmitter {
},
opts: Record<string, unknown> = {},
): Promise<string> {
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);
}
Expand All @@ -467,7 +467,7 @@ class KeyringController extends EventEmitter {
address: string,
opts: Record<string, unknown> = {},
): Promise<Bytes> {
const normalizedAddress = normalizeToHex(address);
const normalizedAddress = normalizeToHex(address) as Hex;
const keyring = await this.getKeyringForAccount(address);
if (!keyring.getEncryptionPublicKey) {
throw new Error(KeyringControllerError.UnsupportedGetEncryptionPublicKey);
Expand All @@ -490,7 +490,7 @@ class KeyringController extends EventEmitter {
from: string;
data: Eip1024EncryptedData;
}): Promise<Bytes> {
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);
Expand Down Expand Up @@ -536,7 +536,7 @@ class KeyringController extends EventEmitter {
* @returns The app key address.
*/
async getAppKeyAddress(rawAddress: string, origin: string): Promise<string> {
const address = normalizeToHex(rawAddress);
const address = normalizeToHex(rawAddress) as Hex;
const keyring = await this.getKeyringForAccount(address);
if (!keyring.getAppKeyAddress) {
throw new Error(KeyringControllerError.UnsupportedGetAppKeyAddress);
Expand All @@ -556,7 +556,7 @@ class KeyringController extends EventEmitter {
rawAddress: string,
origin: string,
): Promise<string> {
const address = normalizeToHex(rawAddress);
const address = normalizeToHex(rawAddress) as Hex;
const keyring = await this.getKeyringForAccount(address);
if (!keyring.exportAccount) {
throw new Error(KeyringControllerError.UnsupportedExportAppKeyForAddress);
Expand Down Expand Up @@ -618,7 +618,7 @@ class KeyringController extends EventEmitter {
this.keyrings.push(keyring);
await this.persistAllKeyrings();

this.#fullUpdate();
this.fullUpdate();

return keyring;
}
Expand Down Expand Up @@ -756,11 +756,7 @@ class KeyringController extends EventEmitter {
* @returns Keyrings matching the specified type.
*/
getKeyringsByType(type: string): Keyring<Json>[] {
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);
}

/**
Expand Down Expand Up @@ -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) {
Expand Down
File renamed without changes.
9 changes: 2 additions & 7 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
}