Skip to content

Commit

Permalink
feat: remove walletService from accountsService (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
vvava authored Oct 7, 2024
1 parent 5b9e239 commit a8ca274
Show file tree
Hide file tree
Showing 33 changed files with 1,557 additions and 1,198 deletions.
142 changes: 81 additions & 61 deletions src/background/services/accounts/AccountsService.test.ts

Large diffs are not rendered by default.

41 changes: 29 additions & 12 deletions src/background/services/accounts/AccountsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
WalletId,
} from './models';
import { OnLock, OnUnlock } from '@src/background/runtime/lifecycleCallbacks';
import { WalletService } from '../wallet/WalletService';
import { NetworkService } from '../network/NetworkService';
import { NetworkVMType } from '@avalabs/core-chains-sdk';
import { PermissionsService } from '../permissions/PermissionsService';
Expand All @@ -24,6 +23,9 @@ import { DerivedAddresses, SecretType } from '../secrets/models';
import { isPrimaryAccount } from './utils/typeGuards';
import { AnalyticsServicePosthog } from '../analytics/AnalyticsServicePosthog';
import getAllAddressesForAccount from '@src/utils/getAllAddressesForAccount';
import { SecretsService } from '../secrets/SecretsService';
import { LedgerService } from '../ledger/LedgerService';
import { WalletConnectService } from '../walletConnect/WalletConnectService';

type AddAccountParams = {
walletId: string;
Expand Down Expand Up @@ -82,10 +84,12 @@ export class AccountsService implements OnLock, OnUnlock {

constructor(
private storageService: StorageService,
private walletService: WalletService,
private networkService: NetworkService,
private permissionsService: PermissionsService,
private analyticsServicePosthog: AnalyticsServicePosthog
private analyticsServicePosthog: AnalyticsServicePosthog,
private secretsService: SecretsService,
private ledgerService: LedgerService,
private walletConnectService: WalletConnectService
) {}

async onUnlock(): Promise<void> {
Expand Down Expand Up @@ -197,12 +201,14 @@ export class AccountsService implements OnLock, OnUnlock {

async getAddressesForAccount(account: Account): Promise<DerivedAddresses> {
if (account.type !== AccountType.PRIMARY) {
return this.walletService.getImportedAddresses(account.id);
const isMainnet = this.networkService.isMainnet();
return this.secretsService.getImportedAddresses(account.id, isMainnet);
}

const addresses = await this.walletService.getAddresses(
const addresses = await this.secretsService.getAddresses(
account.index,
account.walletId
account.walletId,
this.networkService
);

return {
Expand Down Expand Up @@ -332,7 +338,12 @@ export class AccountsService implements OnLock, OnUnlock {
walletId: walletId,
};

const addresses = await this.walletService.addAddress(nextIndex, walletId);
const addresses = await this.secretsService.addAddress({
index: nextIndex,
walletId,
networkService: this.networkService,
ledgerService: this.ledgerService,
});

const id = crypto.randomUUID();

Expand Down Expand Up @@ -374,8 +385,10 @@ export class AccountsService implements OnLock, OnUnlock {
name?: string;
}) {
try {
const { account, commit } = await this.walletService.addImportedWallet(
options
const isMainnet = this.networkService.isMainnet();
const { account, commit } = await this.secretsService.addImportedWallet(
options,
isMainnet
);

const existingAccount = this.#findAccountByAddress(account.addressC);
Expand Down Expand Up @@ -479,6 +492,7 @@ export class AccountsService implements OnLock, OnUnlock {
const { active } = this.accounts;

const walletIds = Object.keys(this.accounts.primary);

const accountsCount = Object.values(this.accounts.primary).flat().length;
const importedAccountIds = ids.filter((id) => id in this.accounts.imported);
const primaryAccountIds = ids.filter(
Expand All @@ -501,7 +515,7 @@ export class AccountsService implements OnLock, OnUnlock {
if (!walletAccounts) {
continue;
}
const walletType = await this.walletService.getWalletType(walletId);
const walletType = await this.secretsService.getWalletType(walletId);

const filteredWalletAccounts = walletAccounts.filter((account) => {
return !primaryAccountIds.includes(account.id);
Expand All @@ -515,7 +529,7 @@ export class AccountsService implements OnLock, OnUnlock {
}

if (!filteredWalletAccounts.length && walletIds.length > 1) {
await this.walletService.deletePrimaryWallets([walletId]);
await this.secretsService.deletePrimaryWallets([walletId]);
continue;
}

Expand Down Expand Up @@ -551,7 +565,10 @@ export class AccountsService implements OnLock, OnUnlock {
throw new Error('There is no new active account!');
}

await this.walletService.deleteImportedWallets(ids);
await this.secretsService.deleteImportedWallets(
ids,
this.walletConnectService
);

this.accounts = {
primary: newPrimaryAccounts,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DAppProviderRequest } from '@src/background/connections/dAppConnection/
import { ethErrors } from 'eth-rpc-errors';
import { AvalancheGetAddressesInRangeHandler } from './avalanche_getAddressesInRange';
import { buildRpcCall } from '@src/tests/test-utils';
import { AccountsService } from '../AccountsService';

jest.mock('@avalabs/core-wallets-sdk');

Expand All @@ -17,10 +18,13 @@ describe('background/services/accounts/handlers/avalanche_getAddressesInRange.ts
getAvalanceProviderXP: jest.fn(),
} as any;

const accountsService = jest.mocked<AccountsService>({} as any);

const handleRequest = async (request) => {
const handler = new AvalancheGetAddressesInRangeHandler(
secretsServiceMock,
networkServiceMock
networkServiceMock,
accountsService
);

return handler.handleAuthenticated(request);
Expand Down Expand Up @@ -134,7 +138,8 @@ describe('background/services/accounts/handlers/avalanche_getAddressesInRange.ts
it('handleUnauthenticated', async () => {
const handler = new AvalancheGetAddressesInRangeHandler(
secretsServiceMock,
networkServiceMock
networkServiceMock,
accountsService
);
const request = {
id: '123',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,16 @@ import { DAppProviderRequest } from '@src/background/connections/dAppConnection/
import { Avalanche } from '@avalabs/core-wallets-sdk';
import { SecretsService } from '../../secrets/SecretsService';
import { NetworkService } from '../../network/NetworkService';
import { AccountsService } from '../AccountsService';

@injectable()
export class AvalancheGetAddressesInRangeHandler extends DAppRequestHandler {
methods = [DAppProviderRequest.AVALANCHE_GET_ADDRESSES_IN_RANGE];

constructor(
private secretsService: SecretsService,
private networkService: NetworkService
private networkService: NetworkService,
private accountsService: AccountsService
) {
super();
}
Expand Down Expand Up @@ -49,7 +51,9 @@ export class AvalancheGetAddressesInRangeHandler extends DAppRequestHandler {
const correctedExternalLimit = getCorrectedLimit(externalLimit);
const correctedInternalLimit = getCorrectedLimit(internalLimit);
const provXP = await this.networkService.getAvalanceProviderXP();
const secrets = await this.secretsService.getPrimaryAccountSecrets();
const secrets = await this.secretsService.getPrimaryAccountSecrets(
this.accountsService.activeAccount
);

const addresses: { external: string[]; internal: string[] } = {
external: [],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { LockService } from '../../lock/LockService';
import { SecretType } from '../../secrets/models';
import { getWalletFromMnemonic } from '@avalabs/core-wallets-sdk';
import { buildRpcCall } from '@src/tests/test-utils';
import { AccountsService } from '../AccountsService';

jest.mock('@avalabs/core-wallets-sdk', () => ({
...jest.requireActual('@avalabs/core-wallets-sdk'),
Expand All @@ -20,6 +21,7 @@ describe('background/services/accounts/handlers/getPrivateKey.ts', () => {
const lockServiceMock: jest.Mocked<LockService> = {
verifyPassword: jest.fn(),
} as any;
const accountsServiceMock: jest.Mocked<AccountsService> = {} as any;

const request = {
id: '123',
Expand All @@ -28,7 +30,11 @@ describe('background/services/accounts/handlers/getPrivateKey.ts', () => {
} as any;

const getHandler = () =>
new GetPrivateKeyHandler(sercretServiceMock, lockServiceMock);
new GetPrivateKeyHandler(
sercretServiceMock,
lockServiceMock,
accountsServiceMock
);

beforeEach(() => {
jest.resetAllMocks();
Expand Down
9 changes: 6 additions & 3 deletions src/background/services/accounts/handlers/getPrivateKey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { AccountType, GetPrivateKeyErrorTypes } from '../models';
import { utils } from '@avalabs/avalanchejs';
import { LockService } from '../../lock/LockService';
import { SecretType } from '../../secrets/models';
import { AccountsService } from '../AccountsService';

interface GetPrivateKeyHandlerParamsProps {
type: SecretType.Mnemonic | AccountType.IMPORTED;
Expand All @@ -27,7 +28,8 @@ export class GetPrivateKeyHandler implements HandlerType {

constructor(
private secretService: SecretsService,
private lockService: LockService
private lockService: LockService,
private accountsService: AccountsService
) {}

handle: HandlerType['handle'] = async ({ request }) => {
Expand Down Expand Up @@ -85,8 +87,9 @@ export class GetPrivateKeyHandler implements HandlerType {
}
}

const primaryAccount =
await this.secretService.getPrimaryAccountSecrets();
const primaryAccount = await this.secretService.getPrimaryAccountSecrets(
this.accountsService.activeAccount
);

if (
!primaryAccount ||
Expand Down
13 changes: 11 additions & 2 deletions src/background/services/fireblocks/FireblocksSecretsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,23 @@ import {
FireblocksBtcAccessErrorCode,
FireblocksSecretsProvider,
} from './models';
import { AccountsService } from '../accounts/AccountsService';

@singleton()
export class FireblocksSecretsService implements FireblocksSecretsProvider {
constructor(private secretsService: SecretsService) {}
constructor(
private secretsService: SecretsService,
private accountsService: AccountsService
) {}

async getSecrets(): Promise<{ apiKey: string; privateKey: KeyLike }> {
if (!this.accountsService.activeAccount) {
throw new Error('There is no active account!');
}
// By default thought, we'll get the credentials directly from SecretsService
const secrets = await this.secretsService.getActiveAccountSecrets();
const secrets = await this.secretsService.getAccountSecrets(
this.accountsService.activeAccount
);

if (secrets.secretType !== SecretType.Fireblocks) {
throw new FireblocksBtcAccessError(
Expand Down
13 changes: 10 additions & 3 deletions src/background/services/fireblocks/FireblocksService.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { sha256 } from 'ethers';
import { PeerType } from 'fireblocks-sdk';
import { AccountType, FireblocksAccount } from '../accounts/models';
import { Account, AccountType, FireblocksAccount } from '../accounts/models';
import { SecretType } from '../secrets/models';
import { SecretsService } from '../secrets/SecretsService';
import { FireblocksSecretsService } from './FireblocksSecretsService';
Expand All @@ -11,6 +11,7 @@ import sentryCaptureException, {
} from '@src/monitoring/sentryCaptureException';
import { CommonError } from '@src/utils/errors';
import { ethErrors } from 'eth-rpc-errors';
import { AccountsService } from '../accounts/AccountsService';

jest.mock('ethers');
jest.mock('../accounts/AccountsService');
Expand Down Expand Up @@ -59,15 +60,21 @@ const mockResponsesByPath =
};

describe('src/background/services/fireblocks/FireblocksService', () => {
const accountsService: jest.Mocked<AccountsService> = {
activeAccount: {} as unknown as Account,
} as any;
const secretsService = jest.mocked(new SecretsService({} as any));
const secretsProvider = new FireblocksSecretsService(secretsService);
const secretsProvider = new FireblocksSecretsService(
secretsService,
accountsService
);
let service: FireblocksService;

beforeEach(() => {
jest.resetAllMocks();

jest.mocked(sha256).mockReturnValue('0x1234');
secretsService.getActiveAccountSecrets.mockResolvedValue({
secretsService.getAccountSecrets.mockResolvedValue({
secretType: SecretType.Fireblocks,
addresses: {
addressC: 'addressC',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,12 @@ describe('src/background/services/fireblocks/handlers/fireblocksUpdateApiCredent
const networkServiceMock = new NetworkService({} as any, {} as any);
const secretsServiceMock = new SecretsService({} as any);
const accountServiceMock = new AccountsService(
{} as any,
{} as any,
networkServiceMock,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any
);
const fireblocksServiceMock = new FireblocksService({} as any);
Expand Down
Loading

0 comments on commit a8ca274

Please sign in to comment.