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

Commit 977facf

Browse files
committed
Improve data types in test suite
1 parent ea8dda7 commit 977facf

File tree

3 files changed

+65
-39
lines changed

3 files changed

+65
-39
lines changed

src/KeyringController.test.ts

+35-30
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import HdKeyring from '@metamask/eth-hd-keyring';
22
import { normalize as normalizeAddress } from '@metamask/eth-sig-util';
3-
import type { Hex, Json, Keyring, KeyringClass } from '@metamask/utils';
3+
import type { Hex } from '@metamask/utils';
44
import { strict as assert } from 'assert';
55
import Wallet from 'ethereumjs-wallet';
66
import * as sinon from 'sinon';
@@ -44,11 +44,7 @@ describe('KeyringController', () => {
4444
keyringController = new KeyringController({
4545
encryptor: mockEncryptor,
4646
cacheEncryptionKey: false,
47-
keyringBuilders: [
48-
keyringBuilderFactory(
49-
KeyringMockWithInit as unknown as KeyringClass<any>,
50-
),
51-
],
47+
keyringBuilders: [keyringBuilderFactory(KeyringMockWithInit)],
5248
});
5349

5450
await keyringController.createNewVaultAndKeychain(PASSWORD);
@@ -454,7 +450,8 @@ describe('KeyringController', () => {
454450

455451
const keyring = await keyringController.restoreKeyring(mockSerialized);
456452
// eslint-disable-next-line no-unsafe-optional-chaining
457-
const { numberOfAccounts } = (await keyring?.serialize()) as any;
453+
// @ts-expect-error this value should never be undefined in this specific context.
454+
const { numberOfAccounts } = await keyring.serialize();
458455
expect(numberOfAccounts).toBe(1);
459456

460457
const accounts = await keyring?.getAccounts();
@@ -474,15 +471,17 @@ describe('KeyringController', () => {
474471
it('returns the result of getAccounts for each keyring', async () => {
475472
keyringController.keyrings = [
476473
{
474+
// @ts-expect-error there's only a need to mock the getAccounts method for this test.
477475
async getAccounts() {
478476
return Promise.resolve([1, 2, 3]);
479477
},
480-
} as unknown as Keyring<Json>,
478+
},
481479
{
480+
// @ts-expect-error there's only a need to mock the getAccounts method for this test.
482481
async getAccounts() {
483482
return Promise.resolve([4, 5, 6]);
484483
},
485-
} as unknown as Keyring<Json>,
484+
},
486485
];
487486

488487
const result = await keyringController.getAccounts();
@@ -499,7 +498,7 @@ describe('KeyringController', () => {
499498

500499
describe('removeAccount', () => {
501500
it('removes an account from the corresponding keyring', async () => {
502-
const account = {
501+
const account: { privateKey: string; publicKey: Hex } = {
503502
privateKey:
504503
'c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3',
505504
publicKey: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
@@ -514,7 +513,7 @@ describe('KeyringController', () => {
514513
expect(keyringController.keyrings).toHaveLength(2);
515514

516515
// remove that account that we just added
517-
await keyringController.removeAccount(account.publicKey as Hex);
516+
await keyringController.removeAccount(account.publicKey);
518517

519518
expect(keyringController.keyrings).toHaveLength(1);
520519
// fetch accounts after removal
@@ -523,7 +522,7 @@ describe('KeyringController', () => {
523522
});
524523

525524
it('removes the keyring if there are no accounts after removal', async () => {
526-
const account = {
525+
const account: { privateKey: string; publicKey: Hex } = {
527526
privateKey:
528527
'c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3',
529528
publicKey: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
@@ -538,7 +537,7 @@ describe('KeyringController', () => {
538537
expect(keyringController.keyrings).toHaveLength(2);
539538

540539
// remove that account that we just added
541-
await keyringController.removeAccount(account.publicKey as Hex);
540+
await keyringController.removeAccount(account.publicKey);
542541

543542
// Check that the previous keyring with only one account
544543
// was also removed after removing the account
@@ -556,7 +555,8 @@ describe('KeyringController', () => {
556555
expect(keyringController.keyrings).toHaveLength(2);
557556

558557
// remove one account from the keyring we just added
559-
await keyringController.removeAccount(walletTwoAddresses[0] as Hex);
558+
// @ts-expect-error this value should never be undefied
559+
await keyringController.removeAccount(walletTwoAddresses[0]);
560560

561561
// Check that the newly added keyring was not removed after
562562
// removing the account since it still has an account left
@@ -571,7 +571,8 @@ describe('KeyringController', () => {
571571
expect(keyrings).toHaveLength(1);
572572
await Promise.all(
573573
keyrings.map(async (keyring) => {
574-
const { numberOfAccounts } = (await keyring.serialize()) as any;
574+
// @ts-expect-error numberOfAccounts mising in Json specification.
575+
const { numberOfAccounts } = await keyring.serialize();
575576
expect(numberOfAccounts).toBe(1);
576577
}),
577578
);
@@ -592,11 +593,7 @@ describe('KeyringController', () => {
592593
describe('verifyPassword', () => {
593594
beforeEach(() => {
594595
keyringController = new KeyringController({
595-
keyringBuilders: [
596-
keyringBuilderFactory(
597-
KeyringMockWithInit as unknown as KeyringClass<any>,
598-
),
599-
],
596+
keyringBuilders: [keyringBuilderFactory(KeyringMockWithInit)],
600597
encryptor: mockEncryptor,
601598
cacheEncryptionKey: false,
602599
});
@@ -626,7 +623,8 @@ describe('KeyringController', () => {
626623
const initialAccounts = await HDKeyring?.getAccounts();
627624
expect(initialAccounts).toHaveLength(1);
628625

629-
await keyringController.addNewAccount(HDKeyring as Keyring<Json>);
626+
// @ts-expect-error this value should never be undefined in this specific context.
627+
await keyringController.addNewAccount(HDKeyring);
630628
const accountsAfterAdd = await HDKeyring?.getAccounts();
631629
expect(accountsAfterAdd).toHaveLength(2);
632630
});
@@ -718,10 +716,11 @@ describe('KeyringController', () => {
718716
it('throws error when there are no matching keyrings', async () => {
719717
keyringController.keyrings = [
720718
{
719+
// @ts-expect-error there's only a need to mock the getAccounts method for this test.
721720
async getAccounts() {
722721
return Promise.resolve([1, 2, 3]);
723722
},
724-
} as unknown as Keyring<Json>,
723+
},
725724
];
726725

727726
await expect(
@@ -808,13 +807,15 @@ describe('KeyringController', () => {
808807

809808
const [firstKeyring] = keyringController.keyrings;
810809

811-
await keyringController.addNewAccount(firstKeyring as Keyring<Json>);
810+
// @ts-expect-error this value should never be undefined in this specific context.
811+
await keyringController.addNewAccount(firstKeyring);
812812
expect(await keyringController.getAccounts()).toHaveLength(2);
813813

814-
await keyringController.addNewAccount(firstKeyring as Keyring<Json>);
814+
// @ts-expect-error this value should never be undefined in this specific context.
815+
await keyringController.addNewAccount(firstKeyring);
815816
expect(await keyringController.getAccounts()).toHaveLength(3);
816817

817-
const account = {
818+
const account: { privateKey: string; publicKey: Hex } = {
818819
privateKey:
819820
'c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3',
820821
publicKey: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
@@ -827,7 +828,7 @@ describe('KeyringController', () => {
827828
expect(await keyringController.getAccounts()).toHaveLength(4);
828829

829830
// remove that account that we just added
830-
await keyringController.removeAccount(account.publicKey as Hex);
831+
await keyringController.removeAccount(account.publicKey);
831832
expect(await keyringController.getAccounts()).toHaveLength(3);
832833
});
833834

@@ -865,7 +866,8 @@ describe('KeyringController', () => {
865866
walletOneSeedWords,
866867
);
867868
const privateKey = await keyringController.exportAccount(
868-
walletOneAddresses[0] as Hex,
869+
// @ts-expect-error this value should never be undefined in this specific context.
870+
walletOneAddresses[0],
869871
);
870872
expect(privateKey).toStrictEqual(walletOnePrivateKey[0]);
871873
});
@@ -881,10 +883,11 @@ describe('KeyringController', () => {
881883

882884
it('signMessage', async () => {
883885
const inputParams = {
884-
from: walletOneAddresses[0] as Hex,
886+
from: walletOneAddresses[0],
885887
data: '0x879a053d4800c6354e76c7985a865d2922c82fb5b3f4577b2fe08b998954f2e0',
886888
origin: 'https://metamask.github.io',
887889
};
890+
// @ts-expect-error this value should never be undefined in this specific context.
888891
const result = await keyringController.signMessage(inputParams);
889892
expect(result).toMatchInlineSnapshot(
890893
`"0x93e0035090e8144debae03f45c5339a78d24c41e38e810a82dd3387e48353db645bd77716f3b7c4fb1f07f3b97bdbd33b0d7c55f7e7eedf3a678a2081948b67f1c"`,
@@ -893,10 +896,11 @@ describe('KeyringController', () => {
893896

894897
it('signPersonalMessage', async () => {
895898
const inputParams = {
896-
from: walletOneAddresses[0] as Hex,
899+
from: walletOneAddresses[0],
897900
data: '0x4578616d706c652060706572736f6e616c5f7369676e60206d657373616765',
898901
origin: 'https://metamask.github.io',
899902
};
903+
// @ts-expect-error this value should never be undefined in this specific context.
900904
const result = await keyringController.signPersonalMessage(inputParams);
901905
expect(result).toBe(
902906
'0xfa2e5989b483e1f40a41b306f275b0009bcc07bfe5322c87682145e7d4889a3247182b4bd8138a965a7e37dea9d9b492b6f9f6d01185412f2d80466237b2805e1b',
@@ -905,7 +909,8 @@ describe('KeyringController', () => {
905909

906910
it('getEncryptionPublicKey', async () => {
907911
const result = await keyringController.getEncryptionPublicKey(
908-
walletOneAddresses[0] as Hex,
912+
// @ts-expect-error this value should never be undefined in this specific context.
913+
walletOneAddresses[0],
909914
);
910915
expect(result).toBe('SR6bQ1m3OTHvI1FLwcGzm+Uk6hffoFPxsQ0DTOeKMEc=');
911916
});

src/KeyringController.ts

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

762766
/**
@@ -1045,13 +1049,13 @@ class KeyringController extends EventEmitter {
10451049
*
10461050
* Returns a builder function for `Keyring` with a `type` property.
10471051
*
1048-
* @param Keyring - The Keyring class for the builder.
1052+
* @param KeyringConstructor - The Keyring class for the builder.
10491053
* @returns A builder function for the given Keyring.
10501054
*/
1051-
function keyringBuilderFactory(Keyring: KeyringClass<Json>) {
1052-
const builder = () => new Keyring();
1055+
function keyringBuilderFactory(KeyringConstructor: KeyringClass<Json>) {
1056+
const builder = () => new KeyringConstructor();
10531057

1054-
builder.type = Keyring.type;
1058+
builder.type = KeyringConstructor.type;
10551059

10561060
return builder;
10571061
}

src/test/keyring.mock.ts

+21-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,29 @@
1-
class KeyringMockWithInit {
2-
static type = 'Keyring Mock With Init';
1+
import type { Keyring, Json, Hex } from '@metamask/utils';
2+
3+
const TYPE = 'Keyring Mock With Init';
4+
5+
class KeyringMockWithInit implements Keyring<Json> {
6+
static type = TYPE;
7+
8+
public type = TYPE;
9+
10+
#accounts: Hex[] = [];
11+
12+
constructor(options: Record<string, unknown> | undefined = {}) {
13+
// eslint-disable-next-line @typescript-eslint/no-floating-promises, @typescript-eslint/promise-function-async
14+
this.deserialize(options);
15+
}
316

417
async init() {
518
return Promise.resolve();
619
}
720

8-
getAccounts() {
9-
return [];
21+
async addAccounts(_: number): Promise<Hex[]> {
22+
return Promise.resolve(this.#accounts);
23+
}
24+
25+
async getAccounts() {
26+
return Promise.resolve(this.#accounts);
1027
}
1128

1229
async serialize() {

0 commit comments

Comments
 (0)