Skip to content
This repository was archived by the owner on Oct 7, 2024. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
aa6526d
Refactor to TS
gantunesr Feb 22, 2023
192c48a
Move files and update structure
gantunesr Feb 23, 2023
ae34346
Update dependencies and configuration
gantunesr Feb 23, 2023
4d8cf0d
Update test and mock files
gantunesr Feb 23, 2023
b7cc77c
Solve yarn issues
gantunesr Feb 23, 2023
7eae2b3
Update configuration and dependencies
gantunesr Feb 23, 2023
d014dbc
Bump jest
gantunesr Feb 23, 2023
98769cd
Create types file
gantunesr Feb 23, 2023
a605feb
Solve lint issues
gantunesr Feb 23, 2023
f51af43
Remove private member
gantunesr Feb 23, 2023
d9e1de5
Solve lint issues
gantunesr Feb 24, 2023
601cb3c
Add typings
gantunesr Feb 28, 2023
fd02507
Update mock-encryptor
gantunesr Feb 28, 2023
46bc491
Update types
gantunesr Feb 28, 2023
d1401a2
Update .eslintrc.js
gantunesr Feb 28, 2023
3ce7a6e
Update test
gantunesr Feb 28, 2023
6933b78
Update KeyringController
gantunesr Feb 28, 2023
3b5ea22
Add build command (not working properly)
gantunesr Mar 1, 2023
fdff1ec
Move declarations to new file
gantunesr Mar 1, 2023
ab03e00
Update jest.config
gantunesr Mar 1, 2023
734c7fb
Update TS config
gantunesr Mar 1, 2023
b90bb01
Fix tests
gantunesr Mar 1, 2023
41f4045
Remove log
gantunesr Mar 1, 2023
c9d7857
Disable TS warning
gantunesr Mar 1, 2023
c95429a
Remove forgetKeyring
gantunesr Mar 1, 2023
c833be6
Change type any to Json
gantunesr Mar 1, 2023
e7b8a5f
Prettier
gantunesr Mar 1, 2023
473c047
Remove log
gantunesr Mar 1, 2023
258c349
Typecast serialize result to any
gantunesr Mar 1, 2023
6fa1e73
Remove Json
gantunesr Mar 1, 2023
a7905a7
Rename privateKeyArray to privateKeys
gantunesr Mar 1, 2023
37f0d37
Update mocks dir structure
gantunesr Mar 1, 2023
1ba064e
Install depcheck and rv @metamask/eslint-config-commonjs
gantunesr Mar 1, 2023
4d8bb84
Revert "Install depcheck and rv @metamask/eslint-config-commonjs"
gantunesr Mar 1, 2023
d43832b
Revert "Update mocks dir structure"
gantunesr Mar 1, 2023
9fe79f0
Update mocks dir structure
gantunesr Mar 1, 2023
921bcb4
Install depcheck and rv @metamask/eslint-config-commonjs
gantunesr Mar 1, 2023
9ccd731
Update dependencies
gantunesr Mar 2, 2023
0dbb136
Update dependencies
gantunesr Mar 2, 2023
84b4e2e
Improve types
gantunesr Mar 2, 2023
a319df4
Improve types
gantunesr Mar 2, 2023
9dd451b
Update errors
gantunesr Mar 2, 2023
85aa914
Add constants file
gantunesr Mar 2, 2023
c80f3d3
Update error messages
gantunesr Mar 6, 2023
8b930ff
Update State
gantunesr Mar 6, 2023
4ed4fe2
Update types, constants, and errors
gantunesr Mar 6, 2023
2973637
Fix tests and lint
gantunesr Mar 6, 2023
e4898e4
Update dependencies
gantunesr Mar 7, 2023
5832e7d
Update types
gantunesr Mar 7, 2023
7d6a833
Rebase
gantunesr Mar 7, 2023
979774b
Lint
gantunesr Mar 7, 2023
2ec20ff
Update data type
gantunesr Mar 8, 2023
c85a7bd
Update syntax to arrow function
gantunesr Mar 8, 2023
0638515
Add KeyringControllerArgs
gantunesr Mar 9, 2023
526182c
Add default to opts arg in addNewKeyring
gantunesr Mar 9, 2023
acd549e
Remove string casting
gantunesr Mar 9, 2023
1d38cfc
Declare backfilled types
gantunesr Mar 9, 2023
8aa5ba0
Rename arg from _address to rawAddress
gantunesr Mar 9, 2023
c59a832
Remove noImplicitAny
gantunesr Mar 9, 2023
f609772
Remove unneed optionals
gantunesr Mar 9, 2023
17b6354
Update data type for arg in newKeyring method
gantunesr Mar 9, 2023
70d5d8c
Fix style
gantunesr Mar 9, 2023
d892064
Improve KeyringControllerArgs
gantunesr Mar 9, 2023
13240e6
Update logic in addNewKeyring for switch-case statement
gantunesr Mar 9, 2023
1b460f3
Update import
gantunesr Mar 9, 2023
05c1920
Update logic for newKeyring to throw an error instead of returning un…
gantunesr Mar 9, 2023
8915012
Update types in tests
gantunesr Mar 9, 2023
3550b93
Update KeyringControllerArgs in types file
gantunesr Mar 9, 2023
b257e31
Update mocks
gantunesr Mar 9, 2023
a96cbca
Add backfill for normalize method and remove type casting
gantunesr Mar 9, 2023
9b48af0
Fix data type
gantunesr Mar 9, 2023
ce7d126
Add SerializedKeyring
gantunesr Mar 9, 2023
b620d2a
Lint
gantunesr Mar 9, 2023
ded3cef
Move mocks to test dir
gantunesr Mar 9, 2023
352b11c
Update build config
gantunesr Mar 9, 2023
476124b
Improve keyring mock
gantunesr Mar 9, 2023
7f29186
Improve error messages
gantunesr Mar 12, 2023
c1843f5
Update tests
gantunesr Mar 12, 2023
5f4d845
Export KeyringControllerError enum
gantunesr Mar 12, 2023
2272c18
Update exports
gantunesr Mar 12, 2023
d0f6e63
Improve stripHexPrefix
gantunesr Mar 12, 2023
1b8c42b
Sort methods
gantunesr Mar 14, 2023
f054c3b
Replace State with Json
gantunesr Mar 14, 2023
5a1df64
Improve types
gantunesr Mar 14, 2023
b76b3db
Update dependencies
gantunesr Mar 17, 2023
850cb00
Bump @typescript-eslint/parser from 5.53.0 to 5.55.0 for TypeScript 5
leotm Mar 17, 2023
b4a3005
Upgrade TypeScript from 4.9.3-5 to 5.0.2 stable
leotm Mar 17, 2023
cfe3e44
Bump remaining harmless ESLint deps
leotm Mar 17, 2023
b618965
Add prettierignore and revert .yarnrc.yml file
gantunesr Mar 21, 2023
a12ce45
Update sinon import
gantunesr Mar 21, 2023
91fd437
Remove type cast in submitPassword test
gantunesr Mar 21, 2023
5b17f35
Remove type cast in persistAllKeyrings test
gantunesr Mar 21, 2023
5dc6cb0
Add @ts-expect-error to remove type casts
gantunesr Mar 21, 2023
9c59391
Set password to only string
gantunesr Mar 21, 2023
6eff4a1
Remove type from forEach
gantunesr Mar 21, 2023
e36c134
Update hex methods
gantunesr Mar 21, 2023
4fba438
Remove KeyringType as data type in methods
gantunesr Mar 22, 2023
7044d64
Move displayForKeyring outside class
gantunesr Mar 22, 2023
7c871f4
Merge branch 'refactor-typescript' of https://github.com/MetaMask/Key…
gantunesr Mar 22, 2023
f722305
Clean up code
gantunesr Mar 22, 2023
3edc176
Update @metamask/eth-sig-util type file
gantunesr Mar 23, 2023
10bb8c8
Validate that data is Json in newKeyring method
gantunesr Mar 23, 2023
a344342
Console.error in restoreKeyring
gantunesr Mar 23, 2023
1c76cf0
Remove unnecessary comment
gantunesr Mar 23, 2023
c87885c
Refactor removeAccount method
gantunesr Mar 23, 2023
ee755fd
Change data type from Hex to string
gantunesr Mar 23, 2023
c7afbff
Change data type from Hex to string
gantunesr Mar 23, 2023
ee12163
Update test name
gantunesr Mar 23, 2023
1ae48d3
Sort methods
gantunesr Mar 23, 2023
ea8dda7
Sort methods
gantunesr Mar 24, 2023
977facf
Improve data types in test suite
gantunesr Mar 27, 2023
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
Prev Previous commit
Improve data types in test suite
  • Loading branch information
gantunesr committed Mar 27, 2023
commit 977facf11c37f5418c5303954037d7ecf125b734
65 changes: 35 additions & 30 deletions src/KeyringController.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import HdKeyring from '@metamask/eth-hd-keyring';
import { normalize as normalizeAddress } from '@metamask/eth-sig-util';
import type { Hex, Json, Keyring, KeyringClass } from '@metamask/utils';
import type { Hex } from '@metamask/utils';
import { strict as assert } from 'assert';
import Wallet from 'ethereumjs-wallet';
import * as sinon from 'sinon';
Expand Down Expand Up @@ -44,11 +44,7 @@ describe('KeyringController', () => {
keyringController = new KeyringController({
encryptor: mockEncryptor,
cacheEncryptionKey: false,
keyringBuilders: [
keyringBuilderFactory(
KeyringMockWithInit as unknown as KeyringClass<any>,
),
],
keyringBuilders: [keyringBuilderFactory(KeyringMockWithInit)],
});

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

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

const accounts = await keyring?.getAccounts();
Expand All @@ -474,15 +471,17 @@ describe('KeyringController', () => {
it('returns the result of getAccounts for each keyring', async () => {
keyringController.keyrings = [
{
// @ts-expect-error there's only a need to mock the getAccounts method for this test.
async getAccounts() {
return Promise.resolve([1, 2, 3]);
},
} as unknown as Keyring<Json>,
},
{
// @ts-expect-error there's only a need to mock the getAccounts method for this test.
async getAccounts() {
return Promise.resolve([4, 5, 6]);
},
} as unknown as Keyring<Json>,
},
];

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

describe('removeAccount', () => {
it('removes an account from the corresponding keyring', async () => {
const account = {
const account: { privateKey: string; publicKey: Hex } = {
privateKey:
'c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3',
publicKey: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
Expand All @@ -514,7 +513,7 @@ describe('KeyringController', () => {
expect(keyringController.keyrings).toHaveLength(2);

// remove that account that we just added
await keyringController.removeAccount(account.publicKey as Hex);
await keyringController.removeAccount(account.publicKey);

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

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

// remove that account that we just added
await keyringController.removeAccount(account.publicKey as Hex);
await keyringController.removeAccount(account.publicKey);

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

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

// Check that the newly added keyring was not removed after
// removing the account since it still has an account left
Expand All @@ -571,7 +571,8 @@ describe('KeyringController', () => {
expect(keyrings).toHaveLength(1);
await Promise.all(
keyrings.map(async (keyring) => {
const { numberOfAccounts } = (await keyring.serialize()) as any;
// @ts-expect-error numberOfAccounts mising in Json specification.
const { numberOfAccounts } = await keyring.serialize();
expect(numberOfAccounts).toBe(1);
}),
);
Expand All @@ -592,11 +593,7 @@ describe('KeyringController', () => {
describe('verifyPassword', () => {
beforeEach(() => {
keyringController = new KeyringController({
keyringBuilders: [
keyringBuilderFactory(
KeyringMockWithInit as unknown as KeyringClass<any>,
),
],
keyringBuilders: [keyringBuilderFactory(KeyringMockWithInit)],
encryptor: mockEncryptor,
cacheEncryptionKey: false,
});
Expand Down Expand Up @@ -626,7 +623,8 @@ describe('KeyringController', () => {
const initialAccounts = await HDKeyring?.getAccounts();
expect(initialAccounts).toHaveLength(1);

await keyringController.addNewAccount(HDKeyring as Keyring<Json>);
// @ts-expect-error this value should never be undefined in this specific context.
await keyringController.addNewAccount(HDKeyring);
const accountsAfterAdd = await HDKeyring?.getAccounts();
expect(accountsAfterAdd).toHaveLength(2);
});
Expand Down Expand Up @@ -718,10 +716,11 @@ describe('KeyringController', () => {
it('throws error when there are no matching keyrings', async () => {
keyringController.keyrings = [
{
// @ts-expect-error there's only a need to mock the getAccounts method for this test.
async getAccounts() {
return Promise.resolve([1, 2, 3]);
},
} as unknown as Keyring<Json>,
},
];

await expect(
Expand Down Expand Up @@ -808,13 +807,15 @@ describe('KeyringController', () => {

const [firstKeyring] = keyringController.keyrings;

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

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

const account = {
const account: { privateKey: string; publicKey: Hex } = {
privateKey:
'c87509a1c067bbde78beb793e6fa76530b6382a4c0241e5e4a9ec0a0f44dc0d3',
publicKey: '0x627306090abab3a6e1400e9345bc60c78a8bef57',
Expand All @@ -827,7 +828,7 @@ describe('KeyringController', () => {
expect(await keyringController.getAccounts()).toHaveLength(4);

// remove that account that we just added
await keyringController.removeAccount(account.publicKey as Hex);
await keyringController.removeAccount(account.publicKey);
expect(await keyringController.getAccounts()).toHaveLength(3);
});

Expand Down Expand Up @@ -865,7 +866,8 @@ describe('KeyringController', () => {
walletOneSeedWords,
);
const privateKey = await keyringController.exportAccount(
walletOneAddresses[0] as Hex,
// @ts-expect-error this value should never be undefined in this specific context.
walletOneAddresses[0],
);
expect(privateKey).toStrictEqual(walletOnePrivateKey[0]);
});
Expand All @@ -881,10 +883,11 @@ describe('KeyringController', () => {

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

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

it('getEncryptionPublicKey', async () => {
const result = await keyringController.getEncryptionPublicKey(
walletOneAddresses[0] as Hex,
// @ts-expect-error this value should never be undefined in this specific context.
walletOneAddresses[0],
);
expect(result).toBe('SR6bQ1m3OTHvI1FLwcGzm+Uk6hffoFPxsQ0DTOeKMEc=');
});
Expand Down
14 changes: 9 additions & 5 deletions src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -756,7 +756,11 @@ class KeyringController extends EventEmitter {
* @returns Keyrings matching the specified type.
*/
getKeyringsByType(type: string): Keyring<Json>[] {
return this.keyrings.filter((keyring) => keyring.type === type);
const keyrings = this.keyrings.filter((keyring) => keyring.type === type);
if (!keyrings.length) {
throw new Error(KeyringControllerError.NoKeyring);
}
return keyrings;
}

/**
Expand Down Expand Up @@ -1045,13 +1049,13 @@ class KeyringController extends EventEmitter {
*
* Returns a builder function for `Keyring` with a `type` property.
*
* @param Keyring - The Keyring class for the builder.
* @param KeyringConstructor - The Keyring class for the builder.
* @returns A builder function for the given Keyring.
*/
function keyringBuilderFactory(Keyring: KeyringClass<Json>) {
const builder = () => new Keyring();
function keyringBuilderFactory(KeyringConstructor: KeyringClass<Json>) {
const builder = () => new KeyringConstructor();

builder.type = Keyring.type;
builder.type = KeyringConstructor.type;

return builder;
}
Expand Down
25 changes: 21 additions & 4 deletions src/test/keyring.mock.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,29 @@
class KeyringMockWithInit {
static type = 'Keyring Mock With Init';
import type { Keyring, Json, Hex } from '@metamask/utils';

const TYPE = 'Keyring Mock With Init';

class KeyringMockWithInit implements Keyring<Json> {
static type = TYPE;

public type = TYPE;

#accounts: Hex[] = [];

constructor(options: Record<string, unknown> | undefined = {}) {
// eslint-disable-next-line @typescript-eslint/no-floating-promises, @typescript-eslint/promise-function-async
this.deserialize(options);
}

async init() {
return Promise.resolve();
}

getAccounts() {
return [];
async addAccounts(_: number): Promise<Hex[]> {
return Promise.resolve(this.#accounts);
}

async getAccounts() {
return Promise.resolve(this.#accounts);
}

async serialize() {
Expand Down