mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-11-25 11:28:51 +01:00
Use createNewVaultAndRestore
from core KeyringController
(#19816)
* refactor: use createNewVaultAndRestore from core kc * test: use createNewVaultAndRestore from core * refactor: apply @mcmire suggestion * fix: mnemonic conversion
This commit is contained in:
parent
d28f699c57
commit
d6eecf8584
@ -89,6 +89,7 @@ import {
|
|||||||
ERC20,
|
ERC20,
|
||||||
ERC721,
|
ERC721,
|
||||||
} from '@metamask/controller-utils';
|
} from '@metamask/controller-utils';
|
||||||
|
import { wordlist } from '@metamask/scure-bip39/dist/wordlists/english';
|
||||||
|
|
||||||
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
|
///: BEGIN:ONLY_INCLUDE_IN(build-mmi)
|
||||||
import { toChecksumHexAddress } from '../../shared/modules/hexstring-utils';
|
import { toChecksumHexAddress } from '../../shared/modules/hexstring-utils';
|
||||||
@ -2905,8 +2906,6 @@ export default class MetamaskController extends EventEmitter {
|
|||||||
|
|
||||||
const seedPhraseAsBuffer = Buffer.from(encodedSeedPhrase);
|
const seedPhraseAsBuffer = Buffer.from(encodedSeedPhrase);
|
||||||
|
|
||||||
const { keyringController } = this;
|
|
||||||
|
|
||||||
// clear known identities
|
// clear known identities
|
||||||
this.preferencesController.setAddresses([]);
|
this.preferencesController.setAddresses([]);
|
||||||
|
|
||||||
@ -2930,29 +2929,22 @@ export default class MetamaskController extends EventEmitter {
|
|||||||
this.txController.txStateManager.clearUnapprovedTxs();
|
this.txController.txStateManager.clearUnapprovedTxs();
|
||||||
|
|
||||||
// create new vault
|
// create new vault
|
||||||
const vault = await keyringController.createNewVaultAndRestore(
|
const vault = await this.coreKeyringController.createNewVaultAndRestore(
|
||||||
password,
|
password,
|
||||||
seedPhraseAsBuffer,
|
this._convertMnemonicToWordlistIndices(seedPhraseAsBuffer),
|
||||||
);
|
);
|
||||||
|
|
||||||
const ethQuery = new EthQuery(this.provider);
|
const ethQuery = new EthQuery(this.provider);
|
||||||
accounts = await keyringController.getAccounts();
|
accounts = await this.coreKeyringController.getAccounts();
|
||||||
lastBalance = await this.getBalance(
|
lastBalance = await this.getBalance(
|
||||||
accounts[accounts.length - 1],
|
accounts[accounts.length - 1],
|
||||||
ethQuery,
|
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
|
// seek out the first zero balance
|
||||||
while (lastBalance !== '0x0') {
|
while (lastBalance !== '0x0') {
|
||||||
await keyringController.addNewAccount(primaryKeyring);
|
await this.coreKeyringController.addNewAccount(accounts.length);
|
||||||
accounts = await keyringController.getAccounts();
|
accounts = await this.coreKeyringController.getAccounts();
|
||||||
lastBalance = await this.getBalance(
|
lastBalance = await this.getBalance(
|
||||||
accounts[accounts.length - 1],
|
accounts[accounts.length - 1],
|
||||||
ethQuery,
|
ethQuery,
|
||||||
@ -2962,7 +2954,7 @@ export default class MetamaskController extends EventEmitter {
|
|||||||
// remove extra zero balance account potentially created from seeking ahead
|
// remove extra zero balance account potentially created from seeking ahead
|
||||||
if (accounts.length > 1 && lastBalance === '0x0') {
|
if (accounts.length > 1 && lastBalance === '0x0') {
|
||||||
await this.removeAccount(accounts[accounts.length - 1]);
|
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
|
// This must be set as soon as possible to communicate to the
|
||||||
@ -2973,8 +2965,6 @@ export default class MetamaskController extends EventEmitter {
|
|||||||
this.preferencesController.getLedgerTransportPreference();
|
this.preferencesController.getLedgerTransportPreference();
|
||||||
this.setLedgerTransportPreference(transportPreference);
|
this.setLedgerTransportPreference(transportPreference);
|
||||||
|
|
||||||
// set new identities
|
|
||||||
this.preferencesController.setAddresses(accounts);
|
|
||||||
this.selectFirstIdentity();
|
this.selectFirstIdentity();
|
||||||
|
|
||||||
return vault;
|
return vault;
|
||||||
@ -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.
|
* Get an account balance from the AccountTracker or request it directly from the network.
|
||||||
*
|
*
|
||||||
|
@ -307,7 +307,7 @@ describe('MetaMaskController', function () {
|
|||||||
'createNewVaultAndKeychain',
|
'createNewVaultAndKeychain',
|
||||||
);
|
);
|
||||||
sandbox.spy(
|
sandbox.spy(
|
||||||
metamaskController.keyringController,
|
metamaskController.coreKeyringController,
|
||||||
'createNewVaultAndRestore',
|
'createNewVaultAndRestore',
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
@ -423,7 +423,7 @@ describe('MetaMaskController', function () {
|
|||||||
await metamaskController.createNewVaultAndRestore(password, TEST_SEED);
|
await metamaskController.createNewVaultAndRestore(password, TEST_SEED);
|
||||||
|
|
||||||
assert(
|
assert(
|
||||||
metamaskController.keyringController.createNewVaultAndRestore
|
metamaskController.coreKeyringController.createNewVaultAndRestore
|
||||||
.calledTwice,
|
.calledTwice,
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user