From ae84b0cb6d67817ac54575fd4cb105599392dfde Mon Sep 17 00:00:00 2001 From: Muin Momin Date: Thu, 16 Sep 2021 16:20:57 -0500 Subject: [PATCH] Fixes #12073: Extra account with zero balance always added when importing wallet with seed and when first account's balance is non-zero (#12074) * add command to debug unit tests * remove extra zero balance account potentially created from seeking ahead * Add PR 12074 to CHANGELOG * Revert "Add PR 12074 to CHANGELOG" This reverts commit 9f6f7eec1cac163c0ce1c711b1c205322aa5b2af. * Remove test debug command --- app/scripts/metamask-controller.js | 6 ++++++ app/scripts/metamask-controller.test.js | 4 +--- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 7f426fc0b..7e045668c 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -1303,6 +1303,12 @@ 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(); + } + // set new identities this.preferencesController.setAddresses(accounts); this.selectFirstIdentity(); diff --git a/app/scripts/metamask-controller.test.js b/app/scripts/metamask-controller.test.js index f75bf7b50..61995363e 100644 --- a/app/scripts/metamask-controller.test.js +++ b/app/scripts/metamask-controller.test.js @@ -91,7 +91,6 @@ const MetaMaskController = proxyquire('./metamask-controller', { const currentNetworkId = '42'; const DEFAULT_LABEL = 'Account 1'; -const DEFAULT_LABEL_2 = 'Account 2'; const TEST_SEED = 'debris dizzy just program just float decrease vacant alarm reduce speak stadium'; const TEST_ADDRESS = '0x0dcd5d886577d5081b0c52e242ef29e70be3e7bc'; @@ -360,7 +359,7 @@ describe('MetaMaskController', function () { }); }); - it('should restore any consecutive accounts with balances', async function () { + it('should restore any consecutive accounts with balances without extra zero balance accounts', async function () { sandbox.stub(metamaskController, 'getBalance'); metamaskController.getBalance.withArgs(TEST_ADDRESS).callsFake(() => { return Promise.resolve('0x14ced5122ce0a000'); @@ -386,7 +385,6 @@ describe('MetaMaskController', function () { delete identities[TEST_ADDRESS].lastSelected; assert.deepEqual(identities, { [TEST_ADDRESS]: { address: TEST_ADDRESS, name: DEFAULT_LABEL }, - [TEST_ADDRESS_2]: { address: TEST_ADDRESS_2, name: DEFAULT_LABEL_2 }, }); }); });