Skip to content

Commit b8f9aaa

Browse files
committed
refactor: remove try-ception
1 parent aceac7b commit b8f9aaa

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

packages/keyring-controller/jest.config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ module.exports = merge(baseConfig, {
1919
global: {
2020
branches: 93.71,
2121
functions: 100,
22-
lines: 98.95,
23-
statements: 98.95,
22+
lines: 98.8,
23+
statements: 98.8,
2424
},
2525
},
2626

packages/keyring-controller/src/KeyringController.ts

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2408,13 +2408,18 @@ export class KeyringController extends BaseController<
24082408
* Retrieves all the accounts from keyrings instances
24092409
* that are currently in memory.
24102410
*
2411+
* @param additionalKeyrings - Additional keyrings to include in the search.
24112412
* @returns A promise resolving to an array of accounts.
24122413
*/
2413-
async #getAccountsFromKeyrings(): Promise<string[]> {
2414+
async #getAccountsFromKeyrings(
2415+
additionalKeyrings: EthKeyring[] = [],
2416+
): Promise<string[]> {
24142417
const keyrings = this.#keyrings;
24152418

24162419
const keyringArrays = await Promise.all(
2417-
keyrings.map(async (keyring) => keyring.getAccounts()),
2420+
[...keyrings, ...additionalKeyrings].map(async (keyring) =>
2421+
keyring.getAccounts(),
2422+
),
24182423
);
24192424
const addresses = keyringArrays.reduce((res, arr) => {
24202425
return res.concat(arr);
@@ -2559,6 +2564,7 @@ export class KeyringController extends BaseController<
25592564
try {
25602565
const { type, data } = serialized;
25612566
const keyring = await this.#createKeyring(type, data);
2567+
await this.#assertNoDuplicateAccounts([keyring]);
25622568
// If metadata is missing, assume the data is from an installation before
25632569
// we had keyring metadata.
25642570
if (this.#keyringsMetadata.length <= this.#keyrings.length) {
@@ -2568,14 +2574,6 @@ export class KeyringController extends BaseController<
25682574
// The keyring is added to the keyrings array only if it's successfully restored
25692575
// and the metadata is successfully added to the controller
25702576
this.#keyrings.push(keyring);
2571-
try {
2572-
await this.#assertNoDuplicateAccounts();
2573-
} catch (error) {
2574-
// If the keyring includes duplicated accounts, we need to remove it
2575-
// from the keyrings array
2576-
this.#keyrings.pop();
2577-
throw error;
2578-
}
25792577
return keyring;
25802578
} catch (error) {
25812579
console.error(error);
@@ -2635,10 +2633,13 @@ export class KeyringController extends BaseController<
26352633
/**
26362634
* Assert that there are no duplicate accounts in the keyrings.
26372635
*
2636+
* @param additionalKeyrings - Additional keyrings to include in the check.
26382637
* @throws If there are duplicate accounts.
26392638
*/
2640-
async #assertNoDuplicateAccounts(): Promise<void> {
2641-
const accounts = await this.#getAccountsFromKeyrings();
2639+
async #assertNoDuplicateAccounts(
2640+
additionalKeyrings: EthKeyring[] = [],
2641+
): Promise<void> {
2642+
const accounts = await this.#getAccountsFromKeyrings(additionalKeyrings);
26422643

26432644
if (new Set(accounts).size !== accounts.length) {
26442645
throw new Error(KeyringControllerError.DuplicatedAccount);

0 commit comments

Comments
 (0)