From d69df217bf0589a6ea087a5d4a9730fba4330a58 Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Tue, 18 Jul 2023 15:55:39 +0200 Subject: [PATCH] refactor: use @metamask/eth-keyring-controller v13 --- packages/keyring-controller/package.json | 4 +- .../src/KeyringController.ts | 103 +++++------------- yarn.lock | 74 +++++-------- 3 files changed, 55 insertions(+), 126 deletions(-) diff --git a/packages/keyring-controller/package.json b/packages/keyring-controller/package.json index 8b5e7a23b46..d4f7ffb7c75 100644 --- a/packages/keyring-controller/package.json +++ b/packages/keyring-controller/package.json @@ -31,8 +31,7 @@ "dependencies": { "@keystonehq/metamask-airgapped-keyring": "^0.13.1", "@metamask/base-controller": "workspace:^", - "@metamask/eth-keyring-controller": "^12.0.1", - "@metamask/eth-sig-util": "^6.0.0", + "@metamask/eth-keyring-controller": "^13.0.0", "@metamask/message-manager": "workspace:^", "@metamask/preferences-controller": "workspace:^", "@metamask/utils": "^6.2.0", @@ -46,6 +45,7 @@ "@ethereumjs/tx": "^4.2.0", "@keystonehq/bc-ur-registry-eth": "^0.9.0", "@metamask/auto-changelog": "^3.1.0", + "@metamask/eth-sig-util": "^6.0.0", "@metamask/scure-bip39": "^2.1.0", "@types/jest": "^27.4.1", "deepmerge": "^4.2.2", diff --git a/packages/keyring-controller/src/KeyringController.ts b/packages/keyring-controller/src/KeyringController.ts index 0cdbba46d4a..30813b09611 100644 --- a/packages/keyring-controller/src/KeyringController.ts +++ b/packages/keyring-controller/src/KeyringController.ts @@ -6,10 +6,6 @@ import { import type { RestrictedControllerMessenger } from '@metamask/base-controller'; import { BaseControllerV2 } from '@metamask/base-controller'; import { KeyringController as EthKeyringController } from '@metamask/eth-keyring-controller'; -import { - normalize as normalizeAddress, - signTypedData, -} from '@metamask/eth-sig-util'; import type { PersonalMessageParams, TypedMessageParams, @@ -18,11 +14,9 @@ import type { PreferencesController } from '@metamask/preferences-controller'; import { assertIsStrictHexString, hasProperty, - isValidHexAddress, type Hex, type Keyring, type Json, - type Bytes, } from '@metamask/utils'; import { Mutex } from 'async-mutex'; import { @@ -61,7 +55,6 @@ export enum KeyringTypes { export type KeyringControllerState = { vault?: string; isUnlocked: boolean; - keyringTypes: string[]; keyrings: KeyringObject[]; encryptionKey?: string; encryptionSalt?: string; @@ -123,7 +116,7 @@ export type KeyringControllerOptions = { keyringBuilders?: { (): Keyring; type: string }[]; cacheEncryptionKey?: boolean; messenger: KeyringControllerMessenger; - state?: { vault: string }; + state?: { vault?: string }; }; /** @@ -159,7 +152,6 @@ export enum SignTypedDataVersion { const defaultState: KeyringControllerState = { isUnlocked: false, - keyringTypes: [], keyrings: [], }; @@ -235,14 +227,13 @@ export class KeyringController extends BaseControllerV2< keyringBuilders = [], cacheEncryptionKey = false, messenger, - state, + state = {}, }: KeyringControllerOptions) { super({ name, metadata: { vault: { persist: true, anonymous: false }, isUnlocked: { persist: false, anonymous: true }, - keyringTypes: { persist: false, anonymous: false }, keyrings: { persist: false, anonymous: false }, encryptionKey: { persist: false, anonymous: false }, encryptionSalt: { persist: false, anonymous: false }, @@ -255,9 +246,6 @@ export class KeyringController extends BaseControllerV2< }); this.#keyring = new EthKeyringController({ - // @ts-expect-error @metamask/eth-keyring-controller uses initState to - // initialize the persistent store, which is an object - // with a `vault` property initState: state, encryptor, keyringBuilders, @@ -522,9 +510,9 @@ export class KeyringController extends BaseControllerV2< default: throw new Error(`Unexpected import strategy: '${strategy}'`); } - const newKeyring = await this.#keyring.addNewKeyring(KeyringTypes.simple, { - privateKeys: [privateKey], - }); + const newKeyring = await this.#keyring.addNewKeyring(KeyringTypes.simple, [ + privateKey, + ]); const accounts = await newKeyring.getAccounts(); const allAccounts = await this.#keyring.getAccounts(); this.updateIdentities(allAccounts); @@ -592,69 +580,33 @@ export class KeyringController extends BaseControllerV2< async signTypedMessage( messageParams: TypedMessageParams, version: SignTypedDataVersion, - ): Promise { + ): Promise { try { if (!this.isUnlocked() || !this.#keyring.password) { throw new Error('Keyring must be unlocked to sign a message.'); } - const address = normalizeAddress(messageParams.from); - if (!address || !isValidHexAddress(address as Hex)) { - throw new Error( - `Missing or invalid address ${JSON.stringify(messageParams.from)}`, - ); + if ( + ![ + SignTypedDataVersion.V1, + SignTypedDataVersion.V3, + SignTypedDataVersion.V4, + ].includes(version) + ) { + throw new Error(`Unexpected signTypedMessage version: '${version}'`); } - if ((await this.getAccountKeyringType(address)) === KeyringTypes.qr) { - const qrKeyring = await this.getOrAddQRKeyring(); - const qrAccounts = await qrKeyring.getAccounts(); - if ( - qrAccounts.findIndex( - (qrAddress: string) => - qrAddress.toLowerCase() === address.toLowerCase(), - ) !== -1 - ) { - return this.#keyring.signTypedMessage( - { - from: messageParams.from, - data: - version !== SignTypedDataVersion.V1 && - typeof messageParams.data === 'string' - ? JSON.parse(messageParams.data) - : messageParams.data, - }, - { version }, - ); - } - } - - const privateKey = await this.exportAccount( - this.#keyring.password, - address, + return await this.#keyring.signTypedMessage( + { + from: messageParams.from, + data: + version !== SignTypedDataVersion.V1 && + typeof messageParams.data === 'string' + ? JSON.parse(messageParams.data) + : messageParams.data, + }, + { version }, ); - const privateKeyBuffer = toBuffer(addHexPrefix(privateKey)); - switch (version) { - case SignTypedDataVersion.V1: - return signTypedData({ - privateKey: privateKeyBuffer, - data: messageParams.data as any, - version: SignTypedDataVersion.V1, - }); - case SignTypedDataVersion.V3: - return signTypedData({ - privateKey: privateKeyBuffer, - data: JSON.parse(messageParams.data as string), - version: SignTypedDataVersion.V3, - }); - case SignTypedDataVersion.V4: - return signTypedData({ - privateKey: privateKeyBuffer, - data: JSON.parse(messageParams.data as string), - version: SignTypedDataVersion.V4, - }); - default: - throw new Error(`Unexpected signTypedMessage version: '${version}'`); - } } catch (error) { throw new Error(`Keyring Controller signTypedMessage: ${error}`); } @@ -764,11 +716,7 @@ export class KeyringController extends BaseControllerV2< * when initializing the controller */ private async addQRKeyring(): Promise> { - // we need to pass an empty array of accounts as options - // otherwise `eth-keyring-controller` will apply `{}` as default and - // pass it to `QRKeyring.deserialize` method, that will cause - // `QRKeyring.accounts` var to be set to `undefined`. - return this.#keyring.addNewKeyring(KeyringTypes.qr, { accounts: [] }); + return this.#keyring.addNewKeyring(KeyringTypes.qr); } /** @@ -924,7 +872,6 @@ export class KeyringController extends BaseControllerV2< return { isUnlocked: this.state.isUnlocked, keyrings: this.state.keyrings, - keyringTypes: this.state.keyringTypes, }; } } diff --git a/yarn.lock b/yarn.lock index 700c5a96186..02e760ed0c2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1668,18 +1668,18 @@ __metadata: languageName: node linkType: hard -"@metamask/eth-keyring-controller@npm:^12.0.1": - version: 12.0.1 - resolution: "@metamask/eth-keyring-controller@npm:12.0.1" +"@metamask/eth-keyring-controller@npm:^13.0.0": + version: 13.0.0 + resolution: "@metamask/eth-keyring-controller@npm:13.0.0" dependencies: "@ethereumjs/tx": ^4.2.0 "@metamask/browser-passworder": ^4.1.0 "@metamask/eth-hd-keyring": ^6.0.0 "@metamask/eth-sig-util": ^6.0.0 "@metamask/eth-simple-keyring": ^5.0.0 + "@metamask/obs-store": ^8.1.0 "@metamask/utils": ^6.2.0 - obs-store: ^4.0.3 - checksum: b3fa08f370a5c8542abbdb37c955f5f05fe55587f56f6b27bf611299c5bb50a0b5dfdec294b09ed8b939a2a23f1b02fc19139c78ebc0bb5163ea45bc97643d78 + checksum: 28ee9e844698d6fc9718eb9f434bb3472ed3e4a3875099df3daddb649618d9d0618b9bbf3cae07e54481378b7935242cf452b2d366982456956b5140a2aa3da3 languageName: node linkType: hard @@ -1764,7 +1764,7 @@ __metadata: "@keystonehq/metamask-airgapped-keyring": ^0.13.1 "@metamask/auto-changelog": ^3.1.0 "@metamask/base-controller": "workspace:^" - "@metamask/eth-keyring-controller": ^12.0.1 + "@metamask/eth-keyring-controller": ^13.0.0 "@metamask/eth-sig-util": ^6.0.0 "@metamask/message-manager": "workspace:^" "@metamask/preferences-controller": "workspace:^" @@ -1901,6 +1901,16 @@ __metadata: languageName: node linkType: hard +"@metamask/obs-store@npm:^8.1.0": + version: 8.1.0 + resolution: "@metamask/obs-store@npm:8.1.0" + dependencies: + "@metamask/safe-event-emitter": ^2.0.0 + through2: ^2.0.3 + checksum: 92356067fa3517526d656f2f0bdfbc4d39f65e27fb30d84240cfc9c1aa9cd5d743498952df18ed8efbb8887b6cc1bc1fab37bde3fb0fc059539e0dfcc67ff86f + languageName: node + linkType: hard + "@metamask/permission-controller@workspace:packages/permission-controller": version: 0.0.0-use.local resolution: "@metamask/permission-controller@workspace:packages/permission-controller" @@ -4918,13 +4928,6 @@ __metadata: languageName: node linkType: hard -"events@npm:^3.0.0": - version: 3.3.0 - resolution: "events@npm:3.3.0" - checksum: f6f487ad2198aa41d878fa31452f1a3c00958f46e9019286ff4787c84aac329332ab45c9cdc8c445928fc6d7ded294b9e005a7fce9426488518017831b272780 - languageName: node - linkType: hard - "evp_bytestokey@npm:^1.0.3": version: 1.0.3 resolution: "evp_bytestokey@npm:1.0.3" @@ -7617,18 +7620,6 @@ __metadata: languageName: node linkType: hard -"obs-store@npm:^4.0.3": - version: 4.0.3 - resolution: "obs-store@npm:4.0.3" - dependencies: - readable-stream: ^2.2.2 - safe-event-emitter: ^1.0.1 - through2: ^2.0.3 - xtend: ^4.0.1 - checksum: a3c05dad7483489f2c083059256f24838b106e10012dd296c7d3e8066869bbc7313dc90775354b519cbeb7aa4c230b0f66cc87ef1414189bad6d03adb0b00b75 - languageName: node - linkType: hard - "once@npm:^1.3.0": version: 1.4.0 resolution: "once@npm:1.4.0" @@ -8061,7 +8052,18 @@ __metadata: languageName: node linkType: hard -"readable-stream@npm:^2.2.2, readable-stream@npm:~2.3.6": +"readable-stream@npm:^3.6.0": + version: 3.6.2 + resolution: "readable-stream@npm:3.6.2" + dependencies: + inherits: ^2.0.3 + string_decoder: ^1.1.1 + util-deprecate: ^1.0.1 + checksum: bdcbe6c22e846b6af075e32cf8f4751c2576238c5043169a1c221c92ee2878458a816a4ea33f4c67623c0b6827c8a400409bfb3cf0bf3381392d0b1dfb52ac8d + languageName: node + linkType: hard + +"readable-stream@npm:~2.3.6": version: 2.3.8 resolution: "readable-stream@npm:2.3.8" dependencies: @@ -8076,17 +8078,6 @@ __metadata: languageName: node linkType: hard -"readable-stream@npm:^3.6.0": - version: 3.6.2 - resolution: "readable-stream@npm:3.6.2" - dependencies: - inherits: ^2.0.3 - string_decoder: ^1.1.1 - util-deprecate: ^1.0.1 - checksum: bdcbe6c22e846b6af075e32cf8f4751c2576238c5043169a1c221c92ee2878458a816a4ea33f4c67623c0b6827c8a400409bfb3cf0bf3381392d0b1dfb52ac8d - languageName: node - linkType: hard - "regenerator-runtime@npm:^0.11.0": version: 0.11.1 resolution: "regenerator-runtime@npm:0.11.1" @@ -8281,15 +8272,6 @@ __metadata: languageName: node linkType: hard -"safe-event-emitter@npm:^1.0.1": - version: 1.0.1 - resolution: "safe-event-emitter@npm:1.0.1" - dependencies: - events: ^3.0.0 - checksum: 2a15094bd28b0966571693f219b5a846949ae24f7ba87c6024f0ed552bef63ebe72970a784b85b77b1f03f1c95e78fabe19306d44538dbc4a3a685bed31c18c4 - languageName: node - linkType: hard - "safe-regex-test@npm:^1.0.0": version: 1.0.0 resolution: "safe-regex-test@npm:1.0.0"