Skip to content

Commit

Permalink
Bump eth-keyring-controller version to @metamask/eth-keyring-controll…
Browse files Browse the repository at this point in the history
…er v10 (#1072)

Fixes: #774  (not an initial target of this work)

Pulls in latest version - v10.0.0 - of `eth-keyring-controller` (now
`@metamask/eth-keyring-controller`) and adapts according.

**BREAKING:** `exportSeedPhrase` now returns a `Uint8Array` typed SRP
(can be converted to a string using [this
approach](https://github.com/MetaMask/eth-hd-keyring/blob/main/index.js#L40)).
It was previously a Buffer.
  • Loading branch information
adonesky1 authored and MajorLift committed Oct 11, 2023
1 parent a518334 commit 13ffc54
Show file tree
Hide file tree
Showing 6 changed files with 104 additions and 153 deletions.
2 changes: 1 addition & 1 deletion packages/keyring-controller/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,11 @@
"@keystonehq/metamask-airgapped-keyring": "^0.6.1",
"@metamask/base-controller": "workspace:^",
"@metamask/controller-utils": "workspace:^",
"@metamask/eth-keyring-controller": "^10.0.0",
"@metamask/eth-sig-util": "^5.0.2",
"@metamask/message-manager": "workspace:^",
"@metamask/preferences-controller": "workspace:^",
"async-mutex": "^0.2.6",
"eth-keyring-controller": "^7.0.2",
"ethereumjs-util": "^7.0.10",
"ethereumjs-wallet": "^1.0.1"
},
Expand Down
19 changes: 13 additions & 6 deletions packages/keyring-controller/src/KeyringController.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CryptoHDKey, ETHSignature } from '@keystonehq/bc-ur-registry-eth';
import * as uuid from 'uuid';
import { PreferencesController } from '@metamask/preferences-controller';
import { MAINNET } from '@metamask/controller-utils';
import { keyringBuilderFactory } from '@metamask/eth-keyring-controller';
import MockEncryptor from '../tests/mocks/mockEncryptor';
import {
AccountImportStrategy,
Expand Down Expand Up @@ -51,9 +52,12 @@ describe('KeyringController', () => {
keyrings: Keyring[];
};
const additionalKeyrings = [QRKeyring];
const additionalKeyringBuilders = additionalKeyrings.map((keyringType) =>
keyringBuilderFactory(keyringType),
);
const baseConfig: Partial<KeyringConfig> = {
encryptor: new MockEncryptor(),
keyringTypes: additionalKeyrings,
keyringBuilders: additionalKeyringBuilders,
};

beforeEach(async () => {
Expand Down Expand Up @@ -123,9 +127,10 @@ describe('KeyringController', () => {

it('should restore same vault if old seedWord is used', async () => {
const currentSeedWord = await keyringController.exportSeedPhrase(password);

const currentState = await keyringController.createNewVaultAndRestore(
password,
currentSeedWord.toString(),
currentSeedWord,
);
expect(initialState).toStrictEqual(currentState);
});
Expand All @@ -139,7 +144,9 @@ describe('KeyringController', () => {
it('should throw error if creating new vault and restoring without seed phrase', async () => {
await expect(
keyringController.createNewVaultAndRestore(password, ''),
).rejects.toThrow('Seed phrase is invalid');
).rejects.toThrow(
'Eth-Hd-Keyring: Deserialize method cannot be called with an opts value for numberOfAccounts and no menmonic',
);
});

it('should create new vault, mnemonic and keychain', async () => {
Expand Down Expand Up @@ -352,13 +359,13 @@ describe('KeyringController', () => {
expect(signature).not.toBe('');
});

it('should not sign message even if empty data is passed', async () => {
await expect(
it('should not sign message if empty data is passed', async () => {
await expect(() =>
keyringController.signMessage({
data: '',
from: initialState.keyrings[0].accounts[0],
}),
).rejects.toThrow('Expected message to be an Uint8Array with length 32');
).toThrow("Can't sign an empty message");
});

it('should not sign message if from account is not passed', async () => {
Expand Down
20 changes: 13 additions & 7 deletions packages/keyring-controller/src/KeyringController.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
signTypedData,
} from '@metamask/eth-sig-util';
import Wallet, { thirdparty as importers } from 'ethereumjs-wallet';
import Keyring from 'eth-keyring-controller';
import { KeyringController as EthKeyringController } from '@metamask/eth-keyring-controller';
import { Mutex } from 'async-mutex';
import {
MetaMaskKeyring as QRKeyring,
Expand Down Expand Up @@ -86,7 +86,7 @@ export interface KeyringMemState extends BaseState {
*/
export interface KeyringConfig extends BaseConfig {
encryptor?: any;
keyringTypes?: any[];
keyringBuilders?: any[];
}

/**
Expand Down Expand Up @@ -152,7 +152,7 @@ export class KeyringController extends BaseController<

private setAccountLabel?: PreferencesController['setAccountLabel'];

#keyring: typeof Keyring;
#keyring: typeof EthKeyringController;

/**
* Creates a KeyringController instance.
Expand Down Expand Up @@ -184,7 +184,9 @@ export class KeyringController extends BaseController<
state?: Partial<KeyringState>,
) {
super(config, state);
this.#keyring = new Keyring(Object.assign({ initState: state }, config));
this.#keyring = new EthKeyringController(
Object.assign({ initState: state }, config),
);

this.defaultState = {
...this.#keyring.store.getState(),
Expand Down Expand Up @@ -429,6 +431,9 @@ export class KeyringController extends BaseController<
* @returns Promise resolving to a signed message string.
*/
signMessage(messageParams: PersonalMessageParams) {
if (!messageParams.data) {
throw new Error("Can't sign an empty message");
}
return this.#keyring.signMessage(messageParams);
}

Expand Down Expand Up @@ -586,14 +591,15 @@ export class KeyringController extends BaseController<
throw new Error('Cannot verify an empty keyring.');
}

const TestKeyringClass = this.#keyring.getKeyringClassForType(
const hdKeyringBuilder = this.#keyring.getKeyringBuilderForType(
KeyringTypes.hd,
);
const testKeyring = new TestKeyringClass({
const hdKeyring = hdKeyringBuilder();
hdKeyring.deserialize({
mnemonic: seedWords,
numberOfAccounts: accounts.length,
});
const testAccounts = await testKeyring.getAccounts();
const testAccounts = await hdKeyring.getAccounts();
/* istanbul ignore if */
if (testAccounts.length !== accounts.length) {
throw new Error('Seed phrase imported incorrect number of accounts.');
Expand Down
1 change: 1 addition & 0 deletions types/@metamask/eth-keyring-controller.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
declare module '@metamask/eth-keyring-controller';
1 change: 0 additions & 1 deletion types/eth-keyring-controller.d.ts

This file was deleted.

Loading

0 comments on commit 13ffc54

Please sign in to comment.