Skip to content

Commit

Permalink
refactor: remove accountsService dependency from seretsSerivce
Browse files Browse the repository at this point in the history
  • Loading branch information
vvava committed Sep 18, 2024
1 parent d3d972e commit 366c372
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 36 deletions.
6 changes: 1 addition & 5 deletions src/background/services/accounts/AccountsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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(
Expand Down
70 changes: 44 additions & 26 deletions src/background/services/secrets/SecretsService.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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');
Expand Down Expand Up @@ -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 &&
Expand Down Expand Up @@ -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;
Expand All @@ -428,13 +443,16 @@ export class SecretsService {
}
}

async loadSecrets() {
return await this.#loadSecrets(true);
}

async #loadSecrets(strict: true): Promise<WalletSecretInStorage | never>;
async #loadSecrets(strict: false): Promise<WalletSecretInStorage | null>;
async #loadSecrets(strict: boolean): Promise<WalletSecretInStorage | null> {
const walletKeys = await this.storageService.load<WalletSecretInStorage>(
WALLET_STORAGE_KEY
);
console.log('walletKeys: ', walletKeys);

if (!walletKeys && strict) {
throw new Error('Wallet is not initialized');
Expand Down
26 changes: 21 additions & 5 deletions src/background/services/wallet/WalletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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[] {
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<PubKeyType> {
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,
Expand Down Expand Up @@ -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 [];
Expand All @@ -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.');
Expand Down

0 comments on commit 366c372

Please sign in to comment.