Skip to content

Commit

Permalink
refactor: use withKeyring to batch account restore operation
Browse files Browse the repository at this point in the history
  • Loading branch information
mikesposito committed Sep 24, 2024
1 parent 1272a4a commit 2e16358
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions app/util/importAdditionalAccounts.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { BNToHex } from '../util/number';
import Logger from '../util/Logger';
import ExtendedKeyringTypes from '../../app/constants/keyringTypes';

const HD_KEY_TREE_ERROR = 'MetamaskController - No HD Key Tree found';
const ZERO_BALANCE = '0x0';
const MAX = 20;

Expand All @@ -30,31 +29,33 @@ const getBalance = async (address, ethQuery) =>
*/
export default async () => {
const { KeyringController } = Engine.context;

const ethQuery = Engine.getGlobalEthQuery();
let accounts = await KeyringController.getAccounts();
let lastBalance = await getBalance(accounts[accounts.length - 1], ethQuery);

const { keyrings } = KeyringController.state;
const filteredKeyrings = keyrings.filter(
(keyring) => keyring.type === ExtendedKeyringTypes.hd,
);
const primaryKeyring = filteredKeyrings[0];
if (!primaryKeyring) throw new Error(HD_KEY_TREE_ERROR);

let i = 0;
// seek out the first zero balance
while (lastBalance !== ZERO_BALANCE) {
if (i === MAX) break;
await KeyringController.addNewAccountWithoutUpdate(primaryKeyring);
accounts = await KeyringController.getAccounts();
lastBalance = await getBalance(accounts[accounts.length - 1], ethQuery);
i++;
}
await KeyringController.withKeyring(
{ type: ExtendedKeyringTypes.hd },
async (primaryKeyring) => {
const existingAccounts = await primaryKeyring.getAccounts();
let lastBalance = await getBalance(
existingAccounts[existingAccounts.length - 1],
ethQuery,
);
let i = 0;
// seek out the first zero balance
while (lastBalance !== ZERO_BALANCE && i < MAX) {
lastBalance = await getBalance(
await primaryKeyring.addAccounts(1),
ethQuery,
);
i++;
}

// remove extra zero balance account potentially created from seeking ahead
if (accounts.length > 1 && lastBalance === ZERO_BALANCE) {
await KeyringController.removeAccount(accounts[accounts.length - 1]);
accounts = await KeyringController.getAccounts();
}
// remove extra zero balance account potentially created from seeking ahead
const currentAccounts = await primaryKeyring.getAccounts();
if (currentAccounts.length > 1 && lastBalance === ZERO_BALANCE) {
primaryKeyring.removeAccount(
currentAccounts[currentAccounts.length - 1],
);
}
},
);
};

0 comments on commit 2e16358

Please sign in to comment.