From e23ac8a5961f7056d6b823042bc8faadc7f86ef1 Mon Sep 17 00:00:00 2001 From: Michele Esposito Date: Tue, 25 Jul 2023 10:57:32 +0200 Subject: [PATCH] refactor: use createNewVaultAndRestore from core kc --- app/scripts/metamask-controller.js | 38 +++++++++++++++++------------- 1 file changed, 21 insertions(+), 17 deletions(-) diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index d58adf26c62d..f5e793b5dcb6 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -89,6 +89,7 @@ import { ERC20, ERC721, } from '@metamask/controller-utils'; +import { wordlist } from '@metamask/scure-bip39/dist/wordlists/english'; ///: BEGIN:ONLY_INCLUDE_IN(build-mmi) import { toChecksumHexAddress } from '../../shared/modules/hexstring-utils'; @@ -2880,8 +2881,6 @@ export default class MetamaskController extends EventEmitter { const seedPhraseAsBuffer = Buffer.from(encodedSeedPhrase); - const { keyringController } = this; - // clear known identities this.preferencesController.setAddresses([]); @@ -2905,29 +2904,22 @@ export default class MetamaskController extends EventEmitter { this.txController.txStateManager.clearUnapprovedTxs(); // create new vault - const vault = await keyringController.createNewVaultAndRestore( + const vault = await this.coreKeyringController.createNewVaultAndRestore( password, - seedPhraseAsBuffer, + this._bufferToUint8Array(seedPhraseAsBuffer), ); const ethQuery = new EthQuery(this.provider); - accounts = await keyringController.getAccounts(); + accounts = await this.coreKeyringController.getAccounts(); lastBalance = await this.getBalance( accounts[accounts.length - 1], ethQuery, ); - const [primaryKeyring] = keyringController.getKeyringsByType( - KeyringType.hdKeyTree, - ); - if (!primaryKeyring) { - throw new Error('MetamaskController - No HD Key Tree found'); - } - // seek out the first zero balance while (lastBalance !== '0x0') { - await keyringController.addNewAccount(primaryKeyring); - accounts = await keyringController.getAccounts(); + await this.coreKeyringController.addNewAccount(accounts.length); + accounts = await this.coreKeyringController.getAccounts(); lastBalance = await this.getBalance( accounts[accounts.length - 1], ethQuery, @@ -2937,7 +2929,7 @@ export default class MetamaskController extends EventEmitter { // remove extra zero balance account potentially created from seeking ahead if (accounts.length > 1 && lastBalance === '0x0') { await this.removeAccount(accounts[accounts.length - 1]); - accounts = await keyringController.getAccounts(); + accounts = await this.coreKeyringController.getAccounts(); } // This must be set as soon as possible to communicate to the @@ -2948,8 +2940,6 @@ export default class MetamaskController extends EventEmitter { this.preferencesController.getLedgerTransportPreference(); this.setLedgerTransportPreference(transportPreference); - // set new identities - this.preferencesController.setAddresses(accounts); this.selectFirstIdentity(); return vault; @@ -2958,6 +2948,20 @@ export default class MetamaskController extends EventEmitter { } } + /** + * Get a Uint8Array mnemonic from Buffer. + * + * @param {Buffer} mnemonic - The mnemonic phrase as a Buffer + * @returns {Uint8Array} The mnemonic phrase as a Uint8Array + */ + _bufferToUint8Array(mnemonic) { + const indices = mnemonic + .toString() + .split(' ') + .map((word) => wordlist.indexOf(word)); + return new Uint8Array(new Uint16Array(indices).buffer); + } + /** * Get an account balance from the AccountTracker or request it directly from the network. *