Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use createNewVaultAndRestore from core KeyringController #19816

Merged
merged 4 commits into from
Aug 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
38 changes: 21 additions & 17 deletions app/scripts/metamask-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -2905,8 +2906,6 @@ export default class MetamaskController extends EventEmitter {

const seedPhraseAsBuffer = Buffer.from(encodedSeedPhrase);

const { keyringController } = this;

// clear known identities
this.preferencesController.setAddresses([]);

Expand All @@ -2930,29 +2929,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._convertMnemonicToWordlistIndices(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] = this.coreKeyringController.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,
Expand All @@ -2962,7 +2954,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
Expand All @@ -2973,8 +2965,6 @@ export default class MetamaskController extends EventEmitter {
this.preferencesController.getLedgerTransportPreference();
this.setLedgerTransportPreference(transportPreference);

// set new identities
this.preferencesController.setAddresses(accounts);
this.selectFirstIdentity();

return vault;
Expand All @@ -2983,6 +2973,20 @@ export default class MetamaskController extends EventEmitter {
}
}

/**
* Encodes a BIP-39 mnemonic as the indices of words in the English BIP-39 wordlist.
*
* @param {Buffer} mnemonic - The BIP-39 mnemonic.
* @returns {Buffer} The Unicode code points for the seed phrase formed from the words in the wordlist.
*/
_convertMnemonicToWordlistIndices(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.
*
Expand Down
4 changes: 2 additions & 2 deletions app/scripts/metamask-controller.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ describe('MetaMaskController', function () {
'createNewVaultAndKeychain',
);
sandbox.spy(
metamaskController.keyringController,
metamaskController.coreKeyringController,
'createNewVaultAndRestore',
);
});
Expand Down Expand Up @@ -423,7 +423,7 @@ describe('MetaMaskController', function () {
await metamaskController.createNewVaultAndRestore(password, TEST_SEED);

assert(
metamaskController.keyringController.createNewVaultAndRestore
metamaskController.coreKeyringController.createNewVaultAndRestore
.calledTwice,
);
});
Expand Down
Loading