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
Next Next commit
Solve lint issues
  • Loading branch information
gantunesr committed Feb 23, 2023
commit a605febbc345960af4cac6f1ed2c9ed061d902ae
91 changes: 47 additions & 44 deletions src/KeyringController.test.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
import HdKeyring from '@metamask/eth-hd-keyring';
import { normalize as normalizeAddress } from '@metamask/eth-sig-util';
import type { Hex } from '@metamask/utils';
import { strict as assert } from 'assert';
import Wallet from 'ethereumjs-wallet';
import { restore, spy, stub, assert as sinonAssert } from 'sinon';

import { KeyringController, keyringBuilderFactory } from '.';
import mockEncryptor from './mocks/mock-encryptor';
import KeyringMockWithInit from './mocks/mock-keyring';
import { ExtendedKeyring, KeyringType } from './types';

const password = 'password123';

Expand All @@ -31,7 +33,7 @@ const walletTwoAddresses = [
];

describe('KeyringController', function () {
let keyringController;
let keyringController: KeyringController;

beforeEach(async function () {
keyringController = new KeyringController({
Expand Down Expand Up @@ -101,7 +103,7 @@ describe('KeyringController', function () {
describe('persistAllKeyrings', function () {
it('should persist keyrings in _unsupportedKeyrings array', async function () {
const unsupportedKeyring = 'DUMMY_KEYRING';
keyringController._unsupportedKeyrings = [unsupportedKeyring];
keyringController.#unsupportedKeyrings = [unsupportedKeyring];
await keyringController.persistAllKeyrings();

const { vault } = keyringController.store.getState();
Expand Down Expand Up @@ -141,7 +143,7 @@ describe('KeyringController', function () {
const { vault } = keyringController.store.getState();
// eslint-disable-next-line jest/no-restricted-matchers
expect(vault).toBeTruthy();
keyringController.encryptor.encrypt.args.forEach(([actualPassword]) => {
keyringController.encryptor.encrypt.args.forEach(([actualPassword]: string[]) => {
expect(actualPassword).toBe(password);
});
});
Expand Down Expand Up @@ -193,7 +195,7 @@ describe('KeyringController', function () {
);

await expect(() =>
keyringController.createNewVaultAndRestore(password, 1234),
keyringController.createNewVaultAndRestore(password, '1234'),
).rejects.toThrow(
'Eth-Hd-Keyring: Invalid secret recovery phrase provided',
);
Expand Down Expand Up @@ -238,7 +240,7 @@ describe('KeyringController', function () {
const keyring = await keyringController.addNewKeyring('Simple Key Pair', [
privateKey,
]);
const keyringAccounts = await keyring.getAccounts();
const keyringAccounts = await keyring?.getAccounts();
const expectedKeyringAccounts = [
'0x627306090abab3a6e1400e9345bc60c78a8bef57',
];
Expand All @@ -255,7 +257,7 @@ describe('KeyringController', function () {
const previousAllAccounts = await keyringController.getAccounts();
expect(previousAllAccounts).toHaveLength(1);
const keyring = await keyringController.addNewKeyring('HD Key Tree');
const keyringAccounts = await keyring.getAccounts();
const keyringAccounts = await keyring?.getAccounts();
expect(keyringAccounts).toHaveLength(1);
const allAccounts = await keyringController.getAccounts();
expect(allAccounts).toHaveLength(2);
Expand All @@ -268,10 +270,10 @@ describe('KeyringController', function () {
numberOfAccounts: 2,
mnemonic: walletTwoSeedWords,
});
const keyringAccounts = await keyring.getAccounts();
const keyringAccounts = await keyring?.getAccounts();
expect(keyringAccounts).toHaveLength(2);
expect(keyringAccounts[0]).toStrictEqual(walletTwoAddresses[0]);
expect(keyringAccounts[1]).toStrictEqual(walletTwoAddresses[1]);
expect(keyringAccounts![0]).toStrictEqual(walletTwoAddresses[0]);
expect(keyringAccounts![1]).toStrictEqual(walletTwoAddresses[1]);
const allAccounts = await keyringController.getAccounts();
expect(allAccounts).toHaveLength(3);
});
Expand Down Expand Up @@ -300,7 +302,7 @@ describe('KeyringController', function () {

const keyring = await keyringController.addNewKeyring('HD Key Tree');

const keyringAccounts = await keyring.getAccounts();
const keyringAccounts = await keyring?.getAccounts();
expect(keyringAccounts).toHaveLength(1);
});
});
Expand All @@ -316,11 +318,11 @@ describe('KeyringController', function () {
};

const keyring = await keyringController.restoreKeyring(mockSerialized);
const wallet = await keyring.serialize();
const wallet = await keyring?.serialize();
expect(wallet.numberOfAccounts).toBe(1);

const accounts = await keyring.getAccounts();
expect(accounts[0]).toBe(walletOneAddresses[0]);
const accounts = await keyring?.getAccounts();
expect(accounts![0]).toBe(walletOneAddresses[0]);
});

it('should return undefined if keyring type is not supported.', async function () {
Expand Down Expand Up @@ -376,7 +378,7 @@ describe('KeyringController', function () {
expect(keyringController.keyrings).toHaveLength(2);

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

expect(keyringController.keyrings).toHaveLength(1);
// fetch accounts after removal
Expand All @@ -400,7 +402,7 @@ describe('KeyringController', function () {
expect(keyringController.keyrings).toHaveLength(2);

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

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

// remove one account from the keyring we just added
await keyringController.removeAccount(walletTwoAddresses[0]);
await keyringController.removeAccount(walletTwoAddresses[0] as Hex);

// Check that the newly added keyring was not removed after
// removing the account since it still has an account left
Expand All @@ -440,13 +442,13 @@ describe('KeyringController', function () {
});

it('add serialized keyring to _unsupportedKeyrings array if keyring type is not known', async function () {
const _unsupportedKeyrings = [{ type: 'Ledger Keyring', data: 'DUMMY' }];
mockEncryptor.encrypt(password, _unsupportedKeyrings);
const unsupportedKeyrings = [{ type: 'Ledger Keyring', data: 'DUMMY' }];
mockEncryptor.encrypt(password, unsupportedKeyrings);
await keyringController.setLocked();
const keyrings = await keyringController.unlockKeyrings(password);
expect(keyrings).toHaveLength(0);
expect(keyringController._unsupportedKeyrings).toStrictEqual(
_unsupportedKeyrings,
expect(keyringController.unsupportedKeyrings).toStrictEqual(
unsupportedKeyrings,
);
});
});
Expand All @@ -470,22 +472,20 @@ describe('KeyringController', function () {
walletOneSeedWords,
);

expect(() =>
keyringController.verifyPassword(password),
).not.toThrow();
expect(() => keyringController.verifyPassword(password)).not.toThrow();
});
});

describe('addNewAccount', function () {
it('adds a new account to the keyring it receives as an argument', async function () {
const [HDKeyring] = await keyringController.getKeyringsByType(
'HD Key Tree',
KeyringType.HD
);
const initialAccounts = await HDKeyring.getAccounts();
const initialAccounts = await HDKeyring?.getAccounts();
expect(initialAccounts).toHaveLength(1);

await keyringController.addNewAccount(HDKeyring);
const accountsAfterAdd = await HDKeyring.getAccounts();
await keyringController.addNewAccount(HDKeyring!);
const accountsAfterAdd = await HDKeyring?.getAccounts();
expect(accountsAfterAdd).toHaveLength(2);
});
});
Expand All @@ -496,23 +496,25 @@ describe('KeyringController', function () {
const privateKey =
'0xb8a9c05beeedb25df85f8d641538cbffedf67216048de9c678ee26260eb91952';

const keyring = await keyringController.addNewKeyring('Simple Key Pair', [
const keyring = await keyringController.addNewKeyring(KeyringType.Simple, [
privateKey,
]);
keyring.getAppKeyAddress = spy();

const getAppKeyAddressSpy = spy();
keyringController.on('getAppKeyAddress', getAppKeyAddressSpy);
/* eslint-disable-next-line require-atomic-updates */
keyringController.getKeyringForAccount = stub().returns(
Promise.resolve(keyring),
);

await keyringController.getAppKeyAddress(address, 'someapp.origin.io');

expect(keyringController.getKeyringForAccount.calledOnce).toBe(true);
expect(keyringController.getKeyringForAccount.getCall(0).args[0]).toBe(
expect(getAppKeyAddressSpy.calledOnce).toBe(true);
expect(getAppKeyAddressSpy.getCall(0).args[0]).toBe(
normalizeAddress(address),
);
expect(keyring.getAppKeyAddress.calledOnce).toBe(true);
expect(keyring.getAppKeyAddress.getCall(0).args).toStrictEqual([
expect(getAppKeyAddressSpy.calledOnce).toBe(true);
expect(getAppKeyAddressSpy.getCall(0).args).toStrictEqual([
normalizeAddress(address),
'someapp.origin.io',
]);
Expand Down Expand Up @@ -559,10 +561,11 @@ describe('KeyringController', function () {
});

it('forget hardware device', async function () {
const hdKeyring = keyringController.getKeyringsByType('HD Key Tree');
hdKeyring.forgetDevice = spy();
const hdKeyring = keyringController.getKeyringsByType(KeyringType.HD);
const forgetDeviceSpy = spy();
keyringController.on('forgetDevice', forgetDeviceSpy);
keyringController.forgetKeyring(hdKeyring);
expect(hdKeyring.forgetDevice.calledOnce).toBe(true);
expect(forgetDeviceSpy.calledOnce).toBe(true);
});
});

Expand Down Expand Up @@ -681,10 +684,10 @@ describe('KeyringController', function () {

const [firstKeyring] = keyringController.keyrings;

await keyringController.addNewAccount(firstKeyring);
await keyringController.addNewAccount(firstKeyring as ExtendedKeyring);
expect(await keyringController.getAccounts()).toHaveLength(2);

await keyringController.addNewAccount(firstKeyring);
await keyringController.addNewAccount(firstKeyring as ExtendedKeyring);
expect(await keyringController.getAccounts()).toHaveLength(3);

const account = {
Expand All @@ -700,7 +703,7 @@ describe('KeyringController', function () {
expect(await keyringController.getAccounts()).toHaveLength(4);

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

Expand Down Expand Up @@ -738,7 +741,7 @@ describe('KeyringController', function () {
walletOneSeedWords,
);
const privateKey = await keyringController.exportAccount(
walletOneAddresses[0],
walletOneAddresses[0] as Hex,
);
expect(privateKey).toStrictEqual(walletOnePrivateKey[0]);
});
Expand All @@ -754,7 +757,7 @@ describe('KeyringController', function () {

it('signMessage', async () => {
const inputParams = {
from: walletOneAddresses[0],
from: walletOneAddresses[0] as Hex,
data: '0x879a053d4800c6354e76c7985a865d2922c82fb5b3f4577b2fe08b998954f2e0',
origin: 'https://metamask.github.io',
};
Expand All @@ -766,7 +769,7 @@ describe('KeyringController', function () {

it('signPersonalMessage', async () => {
const inputParams = {
from: walletOneAddresses[0],
from: walletOneAddresses[0] as Hex,
data: '0x4578616d706c652060706572736f6e616c5f7369676e60206d657373616765',
origin: 'https://metamask.github.io',
};
Expand All @@ -778,14 +781,14 @@ describe('KeyringController', function () {

it('getEncryptionPublicKey', async () => {
const result = await keyringController.getEncryptionPublicKey(
walletOneAddresses[0],
walletOneAddresses[0] as Hex,
);
expect(result).toBe('SR6bQ1m3OTHvI1FLwcGzm+Uk6hffoFPxsQ0DTOeKMEc=');
});

it('signTypedMessage', async () => {
const inputParams = {
from: walletOneAddresses[0],
from: walletOneAddresses[0] as Hex,
data: [
{
type: 'string',
Expand Down
Loading