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

Commit 20fa615

Browse files
authored
[FIX] Inconsistent data types and validations (#228)
1 parent b9a600a commit 20fa615

7 files changed

+27
-39
lines changed

jest.config.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ module.exports = {
4141
// An object that configures minimum threshold enforcement for coverage results
4242
coverageThreshold: {
4343
global: {
44-
branches: 68.67,
45-
functions: 92.59,
46-
lines: 90.2,
47-
statements: 90.42,
44+
branches: 70.37,
45+
functions: 92.72,
46+
lines: 90.72,
47+
statements: 90.93,
4848
},
4949
},
5050
preset: 'ts-jest',

src/KeyringController.ts

Lines changed: 21 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import * as encryptorUtils from '@metamask/browser-passworder';
33
import HDKeyring from '@metamask/eth-hd-keyring';
44
import { normalize as normalizeToHex } from '@metamask/eth-sig-util';
55
import SimpleKeyring from '@metamask/eth-simple-keyring';
6-
import { remove0x, isValidJson } from '@metamask/utils';
6+
import { remove0x } from '@metamask/utils';
77
import type {
88
Hex,
99
Json,
@@ -87,7 +87,7 @@ class KeyringController extends EventEmitter {
8787
*
8888
* @returns The controller state.
8989
*/
90-
#fullUpdate() {
90+
fullUpdate() {
9191
this.emit('update', this.memStore.getState());
9292
return this.memStore.getState();
9393
}
@@ -117,7 +117,7 @@ class KeyringController extends EventEmitter {
117117

118118
await this.#createFirstKeyTree();
119119
this.#setUnlocked();
120-
return this.#fullUpdate();
120+
return this.fullUpdate();
121121
}
122122

123123
/**
@@ -154,7 +154,7 @@ class KeyringController extends EventEmitter {
154154
throw new Error(KeyringControllerError.NoFirstAccount);
155155
}
156156
this.#setUnlocked();
157-
return this.#fullUpdate();
157+
return this.fullUpdate();
158158
}
159159

160160
/**
@@ -178,7 +178,7 @@ class KeyringController extends EventEmitter {
178178
this.keyrings = [];
179179
await this.updateMemStoreKeyrings();
180180
this.emit('lock');
181-
return this.#fullUpdate();
181+
return this.fullUpdate();
182182
}
183183

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

200200
this.#setUnlocked();
201-
return this.#fullUpdate();
201+
return this.fullUpdate();
202202
}
203203

204204
/**
@@ -222,7 +222,7 @@ class KeyringController extends EventEmitter {
222222
encryptionSalt,
223223
);
224224
this.#setUnlocked();
225-
return this.#fullUpdate();
225+
return this.fullUpdate();
226226
}
227227

228228
/**
@@ -265,7 +265,7 @@ class KeyringController extends EventEmitter {
265265
});
266266

267267
await this.persistAllKeyrings();
268-
return this.#fullUpdate();
268+
return this.fullUpdate();
269269
}
270270

271271
/**
@@ -285,7 +285,7 @@ class KeyringController extends EventEmitter {
285285
throw new Error(KeyringControllerError.UnsupportedExportAccount);
286286
}
287287

288-
return await keyring.exportAccount(normalizeToHex(address));
288+
return await keyring.exportAccount(normalizeToHex(address) as Hex);
289289
}
290290

291291
/**
@@ -314,7 +314,7 @@ class KeyringController extends EventEmitter {
314314
}
315315

316316
await this.persistAllKeyrings();
317-
return this.#fullUpdate();
317+
return this.fullUpdate();
318318
}
319319

320320
/**
@@ -388,7 +388,7 @@ class KeyringController extends EventEmitter {
388388
rawAddress: string,
389389
opts: Record<string, unknown> = {},
390390
): Promise<TxData> {
391-
const address = normalizeToHex(rawAddress);
391+
const address = normalizeToHex(rawAddress) as Hex;
392392
const keyring = await this.getKeyringForAccount(address);
393393
if (!keyring.signTransaction) {
394394
throw new Error(KeyringControllerError.UnsupportedSignTransaction);
@@ -415,7 +415,7 @@ class KeyringController extends EventEmitter {
415415
},
416416
opts: Record<string, unknown> = {},
417417
): Promise<string> {
418-
const address = normalizeToHex(msgParams.from);
418+
const address = normalizeToHex(msgParams.from) as Hex;
419419
const keyring = await this.getKeyringForAccount(address);
420420
if (!keyring.signMessage) {
421421
throw new Error(KeyringControllerError.UnsupportedSignMessage);
@@ -443,13 +443,13 @@ class KeyringController extends EventEmitter {
443443
},
444444
opts: Record<string, unknown> = {},
445445
): Promise<string> {
446-
const address = normalizeToHex(msgParams.from);
446+
const address = normalizeToHex(msgParams.from) as Hex;
447447
const keyring = await this.getKeyringForAccount(address);
448448
if (!keyring.signPersonalMessage) {
449449
throw new Error(KeyringControllerError.UnsupportedSignPersonalMessage);
450450
}
451451

452-
const normalizedData = normalizeToHex(msgParams.data);
452+
const normalizedData = normalizeToHex(msgParams.data) as Hex;
453453

454454
return await keyring.signPersonalMessage(address, normalizedData, opts);
455455
}
@@ -467,7 +467,7 @@ class KeyringController extends EventEmitter {
467467
address: string,
468468
opts: Record<string, unknown> = {},
469469
): Promise<Bytes> {
470-
const normalizedAddress = normalizeToHex(address);
470+
const normalizedAddress = normalizeToHex(address) as Hex;
471471
const keyring = await this.getKeyringForAccount(address);
472472
if (!keyring.getEncryptionPublicKey) {
473473
throw new Error(KeyringControllerError.UnsupportedGetEncryptionPublicKey);
@@ -490,7 +490,7 @@ class KeyringController extends EventEmitter {
490490
from: string;
491491
data: Eip1024EncryptedData;
492492
}): Promise<Bytes> {
493-
const address = normalizeToHex(msgParams.from);
493+
const address = normalizeToHex(msgParams.from) as Hex;
494494
const keyring = await this.getKeyringForAccount(address);
495495
if (!keyring.decryptMessage) {
496496
throw new Error(KeyringControllerError.UnsupportedDecryptMessage);
@@ -536,7 +536,7 @@ class KeyringController extends EventEmitter {
536536
* @returns The app key address.
537537
*/
538538
async getAppKeyAddress(rawAddress: string, origin: string): Promise<string> {
539-
const address = normalizeToHex(rawAddress);
539+
const address = normalizeToHex(rawAddress) as Hex;
540540
const keyring = await this.getKeyringForAccount(address);
541541
if (!keyring.getAppKeyAddress) {
542542
throw new Error(KeyringControllerError.UnsupportedGetAppKeyAddress);
@@ -556,7 +556,7 @@ class KeyringController extends EventEmitter {
556556
rawAddress: string,
557557
origin: string,
558558
): Promise<string> {
559-
const address = normalizeToHex(rawAddress);
559+
const address = normalizeToHex(rawAddress) as Hex;
560560
const keyring = await this.getKeyringForAccount(address);
561561
if (!keyring.exportAccount) {
562562
throw new Error(KeyringControllerError.UnsupportedExportAppKeyForAddress);
@@ -618,7 +618,7 @@ class KeyringController extends EventEmitter {
618618
this.keyrings.push(keyring);
619619
await this.persistAllKeyrings();
620620

621-
this.#fullUpdate();
621+
this.fullUpdate();
622622

623623
return keyring;
624624
}
@@ -756,11 +756,7 @@ class KeyringController extends EventEmitter {
756756
* @returns Keyrings matching the specified type.
757757
*/
758758
getKeyringsByType(type: string): Keyring<Json>[] {
759-
const keyrings = this.keyrings.filter((keyring) => keyring.type === type);
760-
if (!keyrings.length) {
761-
throw new Error(KeyringControllerError.NoKeyring);
762-
}
763-
return keyrings;
759+
return this.keyrings.filter((keyring) => keyring.type === type);
764760
}
765761

766762
/**
@@ -1030,10 +1026,7 @@ class KeyringController extends EventEmitter {
10301026

10311027
const keyring = keyringBuilder();
10321028

1033-
if (!isValidJson(data)) {
1034-
throw new Error(KeyringControllerError.DataType);
1035-
}
1036-
1029+
// @ts-expect-error Enforce data type after updating clients
10371030
await keyring.deserialize(data);
10381031

10391032
if (keyring.init) {
File renamed without changes.

tsconfig.build.json

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,6 @@
88
"rootDir": "src",
99
"sourceMap": true
1010
},
11-
"include": ["./src/**/*.ts", "./types"],
12-
"exclude": [
13-
"./src/test/**/*.ts",
14-
"./src/**/*.test.ts",
15-
"./src/**/*.test-d.ts",
16-
"./src/__fixtures__"
17-
]
11+
"include": ["./src/**/*.ts"],
12+
"exclude": ["./src/**/*.test.ts"]
1813
}

0 commit comments

Comments
 (0)