diff --git a/src/core/keychain/KeychainManager.ts b/src/core/keychain/KeychainManager.ts index 2c4da0ebf8..15d5c0619f 100644 --- a/src/core/keychain/KeychainManager.ts +++ b/src/core/keychain/KeychainManager.ts @@ -149,6 +149,7 @@ class KeychainManager { default: throw new Error('Keychain type not recognized.'); } + await this.overrideReadOnlyKeychains(keychain); await this.checkForDuplicateInKeychain(keychain); this.state.keychains.push(keychain as Keychain); return keychain; @@ -257,6 +258,22 @@ class KeychainManager { return false; } + async overrideReadOnlyKeychains(incomingKeychain: Keychain) { + if (incomingKeychain.type === KeychainType.ReadOnlyKeychain) return; + const currentAccounts = await this.getAccounts(); + const incomingAccounts = await incomingKeychain.getAccounts(); + const conflictingAccounts = incomingAccounts.filter((acc) => + currentAccounts.includes(acc), + ); + await Promise.all( + conflictingAccounts.map(async (acc) => { + const wallet = await this.getWallet(acc); + const isReadOnly = wallet.type === KeychainType.ReadOnlyKeychain; + if (isReadOnly) this.removeAccount(acc); + }), + ); + } + async checkForDuplicateInKeychain(keychain: Keychain) { const existingAccounts = await this.getAccounts(); const newAccounts = await keychain.getAccounts();