Skip to content

Commit

Permalink
Fixed bug that was causing to not show the correct account name and i…
Browse files Browse the repository at this point in the history
…nstead was displaying the default "Account x" one (#20555)
  • Loading branch information
albertolive authored Aug 22, 2023
1 parent e6cd452 commit 787fc13
Showing 1 changed file with 29 additions and 8 deletions.
37 changes: 29 additions & 8 deletions app/scripts/controllers/mmi-controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -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);
}
}
});

Expand Down

0 comments on commit 787fc13

Please sign in to comment.