1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Avoid using indexes to reference first item in array (#15732)

This commit is contained in:
David Walsh 2022-09-27 10:52:01 -05:00 committed by GitHub
parent 1295fabfb5
commit a64f82e8a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 18 deletions

View File

@ -263,7 +263,7 @@ export default class PreferencesController {
// If the selected account is no longer valid,
// select an arbitrary other account:
if (address === this.getSelectedAddress()) {
const selected = Object.keys(identities)[0];
const [selected] = Object.keys(identities);
this.setSelectedAddress(selected);
}
return address;
@ -326,7 +326,7 @@ export default class PreferencesController {
// select an arbitrary other account:
let selected = this.getSelectedAddress();
if (!addresses.includes(selected)) {
selected = addresses[0];
[selected] = addresses;
this.setSelectedAddress(selected);
}

View File

@ -2161,8 +2161,8 @@ export default class MetamaskController extends EventEmitter {
ethQuery,
);
const primaryKeyring =
keyringController.getKeyringsByType('HD Key Tree')[0];
const [primaryKeyring] =
keyringController.getKeyringsByType('HD Key Tree');
if (!primaryKeyring) {
throw new Error('MetamaskController - No HD Key Tree found');
}
@ -2287,8 +2287,7 @@ export default class MetamaskController extends EventEmitter {
});
// Accounts
const hdKeyring =
this.keyringController.getKeyringsByType('HD Key Tree')[0];
const [hdKeyring] = this.keyringController.getKeyringsByType('HD Key Tree');
const simpleKeyPairKeyrings =
this.keyringController.getKeyringsByType('Simple Key Pair');
const hdAccounts = await hdKeyring.getAccounts();
@ -2394,7 +2393,7 @@ export default class MetamaskController extends EventEmitter {
*/
selectFirstIdentity() {
const { identities } = this.preferencesController.store.getState();
const address = Object.keys(identities)[0];
const [address] = Object.keys(identities);
this.preferencesController.setSelectedAddress(address);
}
@ -2402,7 +2401,7 @@ export default class MetamaskController extends EventEmitter {
* Gets the mnemonic of the user's primary keyring.
*/
getPrimaryKeyringMnemonic() {
const keyring = this.keyringController.getKeyringsByType('HD Key Tree')[0];
const [keyring] = this.keyringController.getKeyringsByType('HD Key Tree');
if (!keyring.mnemonic) {
throw new Error('Primary keyring mnemonic unavailable.');
}
@ -2433,9 +2432,7 @@ export default class MetamaskController extends EventEmitter {
'MetamaskController:getKeyringForDevice - Unknown device',
);
}
let keyring = await this.keyringController.getKeyringsByType(
keyringName,
)[0];
let [keyring] = await this.keyringController.getKeyringsByType(keyringName);
if (!keyring) {
keyring = await this.keyringController.addNewKeyring(keyringName);
}
@ -2635,8 +2632,8 @@ export default class MetamaskController extends EventEmitter {
* @returns {} keyState
*/
async addNewAccount(accountCount) {
const primaryKeyring =
this.keyringController.getKeyringsByType('HD Key Tree')[0];
const [primaryKeyring] =
this.keyringController.getKeyringsByType('HD Key Tree');
if (!primaryKeyring) {
throw new Error('MetamaskController - No HD Key Tree found');
}
@ -2679,8 +2676,8 @@ export default class MetamaskController extends EventEmitter {
* encoded as an array of UTF-8 bytes.
*/
async verifySeedPhrase() {
const primaryKeyring =
this.keyringController.getKeyringsByType('HD Key Tree')[0];
const [primaryKeyring] =
this.keyringController.getKeyringsByType('HD Key Tree');
if (!primaryKeyring) {
throw new Error('MetamaskController - No HD Key Tree found');
}
@ -2804,12 +2801,12 @@ export default class MetamaskController extends EventEmitter {
'Simple Key Pair',
[privateKey],
);
const accounts = await keyring.getAccounts();
const [firstAccount] = await keyring.getAccounts();
// update accounts in preferences controller
const allAccounts = await this.keyringController.getAccounts();
this.preferencesController.setAddresses(allAccounts);
// set new account as selected
await this.preferencesController.setSelectedAddress(accounts[0]);
await this.preferencesController.setSelectedAddress(firstAccount);
}
// ---------------------------------------------------------------------------
@ -2875,7 +2872,7 @@ export default class MetamaskController extends EventEmitter {
if (requestedAccount) {
account = requestedAccount;
} else {
account = (await this.keyringController.getAccounts())[0];
[account] = await this.keyringController.getAccounts();
}
return this.keyringController.exportAppKeyForAddress(account, subject);