diff --git a/src/background/services/accounts/AccountsService.ts b/src/background/services/accounts/AccountsService.ts index c7ff7557..d6264d9c 100644 --- a/src/background/services/accounts/AccountsService.ts +++ b/src/background/services/accounts/AccountsService.ts @@ -291,12 +291,10 @@ export class AccountsService implements OnLock, OnUnlock { } getAccounts(): Accounts { - console.log('this.accounts: ', this.accounts); return this.accounts; } getAccountList(): Account[] { - console.log('getAccountList this.accounts: ', this.accounts); return [ ...Object.values(this.accounts.primary).flat(), ...Object.values(this.accounts.imported), @@ -328,7 +326,6 @@ export class AccountsService implements OnLock, OnUnlock { return this.getAccountList().flatMap(getAllAddressesForAccount); } - //TODO async addPrimaryAccount({ walletId }: AddAccountParams) { const selectedWalletAccounts = this.accounts.primary[walletId] ?? []; const lastAccount = selectedWalletAccounts.at(-1); @@ -492,11 +489,10 @@ export class AccountsService implements OnLock, OnUnlock { } async deleteAccounts(ids: string[]) { - console.log('ids: ', ids); const { active } = this.accounts; const walletIds = Object.keys(this.accounts.primary); - console.log('walletIds: ', walletIds); + const accountsCount = Object.values(this.accounts.primary).flat().length; const importedAccountIds = ids.filter((id) => id in this.accounts.imported); const primaryAccountIds = ids.filter( diff --git a/src/background/services/secrets/SecretsService.ts b/src/background/services/secrets/SecretsService.ts index 85d6b5ce..5d0b2e4e 100644 --- a/src/background/services/secrets/SecretsService.ts +++ b/src/background/services/secrets/SecretsService.ts @@ -1,8 +1,12 @@ import { omit, pick } from 'lodash'; -import { container, singleton } from 'tsyringe'; +import { singleton } from 'tsyringe'; -import { AccountsService } from '../accounts/AccountsService'; -import { AccountType, ImportData, ImportType } from '../accounts/models'; +import { + Account, + AccountType, + ImportData, + ImportType, +} from '../accounts/models'; import { StorageService } from '../storage/StorageService'; import { BtcWalletPolicyDetails, @@ -163,24 +167,33 @@ export class SecretsService { } } - //TODO: move to accountsSerivce - getActiveWalletSecrets(walletKeys: WalletSecretInStorage) { - const accountsService = container.resolve(AccountsService); - - const activeWalletId = isPrimaryAccount(accountsService.activeAccount) - ? accountsService.activeAccount.walletId - : accountsService.activeAccount?.id; + getActiveWalletSecrets( + walletKeys: WalletSecretInStorage, + activeAccount?: Account + ) { + if (!activeAccount) { + return null; + } + const activeWalletId = isPrimaryAccount(activeAccount) + ? activeAccount.walletId + : activeAccount?.id; return walletKeys.wallets.find((wallet) => wallet.id === activeWalletId); } - async getPrimaryAccountSecrets() { + async getPrimaryAccountSecrets(activeAccount?: Account) { + if (!activeAccount) { + return null; + } const walletKeys = await this.#loadSecrets(false); if (!walletKeys) { return null; } - const activeWalletSecrets = this.getActiveWalletSecrets(walletKeys); + const activeWalletSecrets = this.getActiveWalletSecrets( + walletKeys, + activeAccount + ); if (!activeWalletSecrets) { return null; @@ -227,18 +240,14 @@ export class SecretsService { throw new Error('Unsupported import type'); } - //TODO: move to accountsService - async getActiveAccountSecrets() { + async getActiveAccountSecrets(activeAccount: Account) { const walletKeys = await this.#loadSecrets(true); - // But later on, we rely on the active account only. - // To resolve circular dependencies we are getting accounts service on the fly instead of via constructor - const accountsService = container.resolve(AccountsService); - - const { activeAccount } = accountsService; - if (!activeAccount || activeAccount.type === AccountType.PRIMARY) { - const activeWalletSecrets = this.getActiveWalletSecrets(walletKeys); + const activeWalletSecrets = this.getActiveWalletSecrets( + walletKeys, + activeAccount + ); if (!activeWalletSecrets) { throw new Error('There is no values for this account'); @@ -332,9 +341,10 @@ export class SecretsService { masterFingerprint: string, hmacHex: string, name: string, - walletId: string + walletId: string, + activeAccount: Account ) { - const secrets = await this.getActiveAccountSecrets(); + const secrets = await this.getActiveAccountSecrets(activeAccount); if ( secrets.secretType !== SecretType.Ledger && @@ -405,10 +415,15 @@ export class SecretsService { ); } - async getBtcWalletPolicyDetails(): Promise< + async getBtcWalletPolicyDetails( + activeAccount?: Account + ): Promise< { accountIndex: number; details?: BtcWalletPolicyDetails } | undefined > { - const secrets = await this.getActiveAccountSecrets(); + if (!activeAccount) { + return undefined; + } + const secrets = await this.getActiveAccountSecrets(activeAccount); if (secrets.secretType === SecretType.LedgerLive && secrets.account) { const accountIndex = secrets.account.index; @@ -428,13 +443,16 @@ export class SecretsService { } } + async loadSecrets() { + return await this.#loadSecrets(true); + } + async #loadSecrets(strict: true): Promise; async #loadSecrets(strict: false): Promise; async #loadSecrets(strict: boolean): Promise { const walletKeys = await this.storageService.load( WALLET_STORAGE_KEY ); - console.log('walletKeys: ', walletKeys); if (!walletKeys && strict) { throw new Error('Wallet is not initialized'); diff --git a/src/background/services/wallet/WalletService.ts b/src/background/services/wallet/WalletService.ts index 66ad89ca..d36b888c 100644 --- a/src/background/services/wallet/WalletService.ts +++ b/src/background/services/wallet/WalletService.ts @@ -59,6 +59,7 @@ import { SeedlessTokenStorage } from '../seedless/SeedlessTokenStorage'; import { SeedlessSessionManager } from '../seedless/SeedlessSessionManager'; import { getProviderForNetwork } from '@src/utils/network/getProviderForNetwork'; import { Network } from '../network/models'; +import { AccountsService } from '../accounts/AccountsService'; @singleton() export class WalletService implements OnLock, OnUnlock { @@ -71,7 +72,8 @@ export class WalletService implements OnLock, OnUnlock { private keystoneService: KeystoneService, private walletConnectService: WalletConnectService, private fireblocksService: FireblocksService, - private secretService: SecretsService + private secretService: SecretsService, + private accountsService: AccountsService ) {} public get wallets(): WalletDetails[] { @@ -168,7 +170,12 @@ export class WalletService implements OnLock, OnUnlock { tabId?: number; accountIndex?: number; }) { - const secrets = await this.secretService.getActiveAccountSecrets(); + if (!this.accountsService.activeAccount) { + return; + } + const secrets = await this.secretService.getActiveAccountSecrets( + this.accountsService.activeAccount + ); if (!secrets.account) { // wallet is not initialized return; @@ -562,7 +569,12 @@ export class WalletService implements OnLock, OnUnlock { * @throws Will throw error for LedgerLive accounts that have not been added yet. */ async getActiveAccountPublicKey(): Promise { - const secrets = await this.secretService.getActiveAccountSecrets(); + if (!this.accountsService.activeAccount) { + throw new Error('There is no active account'); + } + const secrets = await this.secretService.getActiveAccountSecrets( + this.accountsService.activeAccount + ); if (secrets.secretType === SecretType.Fireblocks) { // TODO: We technically can fetch some public keys using the API, @@ -797,7 +809,9 @@ export class WalletService implements OnLock, OnUnlock { isChange: boolean ) { const provXP = await this.networkService.getAvalanceProviderXP(); - const secrets = await this.secretService.getPrimaryAccountSecrets(); + const secrets = await this.secretService.getPrimaryAccountSecrets( + this.accountsService.activeAccount + ); if (!secrets || !secrets.xpubXP) { return []; @@ -819,7 +833,9 @@ export class WalletService implements OnLock, OnUnlock { } private async parseWalletPolicyDetails() { - const policyInfo = await this.secretService.getBtcWalletPolicyDetails(); + const policyInfo = await this.secretService.getBtcWalletPolicyDetails( + this.accountsService.activeAccount + ); if (!policyInfo || !policyInfo.details) { throw new Error('Error while parsing wallet policy: missing data.');