From 787fc13f193df5a1fd176cc9a9b5720abb3869b0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Albert=20Oliv=C3=A9?= Date: Tue, 22 Aug 2023 17:50:33 +0200 Subject: [PATCH] Fixed bug that was causing to not show the correct account name and instead was displaying the default "Account x" one (#20555) --- app/scripts/controllers/mmi-controller.js | 37 ++++++++++++++++++----- 1 file changed, 29 insertions(+), 8 deletions(-) diff --git a/app/scripts/controllers/mmi-controller.js b/app/scripts/controllers/mmi-controller.js index f5fcc339c6af..bfeca6a24f9f 100644 --- a/app/scripts/controllers/mmi-controller.js +++ b/app/scripts/controllers/mmi-controller.js @@ -292,9 +292,9 @@ export default class MMIController extends EventEmitter { })), ); - newAccounts.forEach( - async () => await this.keyringController.addNewAccount(keyring), - ); + for (let i = 0; i < newAccounts.length; i++) { + await this.keyringController.addNewAccount(keyring); + } const allAccounts = await this.keyringController.getAccounts(); @@ -303,12 +303,33 @@ export default class MMIController extends EventEmitter { ...new Set(oldAccounts.concat(allAccounts.map((a) => a.toLowerCase()))), ]; + // Create a Set of lowercased addresses from oldAccounts for efficient existence checks + const oldAccountsSet = new Set( + oldAccounts.map((address) => address.toLowerCase()), + ); + + // Create a map of lowercased addresses to names from newAccounts for efficient lookups + const accountNameMap = newAccounts.reduce((acc, item) => { + // For each account in newAccounts, add an entry to the map with the lowercased address as the key and the name as the value + acc[item.toLowerCase()] = accounts[item].name; + return acc; + }, {}); + + // Iterate over all accounts allAccounts.forEach((address) => { - if (!oldAccounts.includes(address.toLowerCase())) { - const label = newAccounts - .filter((item) => item.toLowerCase() === address) - .map((item) => accounts[item].name)[0]; - this.preferencesController.setAccountLabel(address, label); + // Convert the address to lowercase for consistent comparisons + const lowercasedAddress = address.toLowerCase(); + + // If the address is not in oldAccounts + if (!oldAccountsSet.has(lowercasedAddress)) { + // Look up the label in the map + const label = accountNameMap[lowercasedAddress]; + + // If the label is defined + if (label) { + // Set the label for the address + this.preferencesController.setAccountLabel(address, label); + } } });