Skip to content

Commit

Permalink
refactor: types
Browse files Browse the repository at this point in the history
  • Loading branch information
vvava committed Sep 19, 2024
1 parent 366c372 commit f406ae3
Show file tree
Hide file tree
Showing 19 changed files with 819 additions and 653 deletions.
101 changes: 45 additions & 56 deletions src/background/services/accounts/AccountsService.test.ts

Large diffs are not rendered by default.

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.getActiveAccountSecrets(
this.accountsService.activeAccount
);

if (secrets.secretType !== SecretType.Fireblocks) {
throw new FireblocksBtcAccessError(
Expand Down
15 changes: 14 additions & 1 deletion src/background/services/fireblocks/FireblocksService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,8 +60,20 @@ const mockResponsesByPath =
};

describe('src/background/services/fireblocks/FireblocksService', () => {
const accountsService = new AccountsService(
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} 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(() => {
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
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { SecretsService } from '../../secrets/SecretsService';
import { LedgerTransport } from '../LedgerTransport';
import { MigrateMissingPublicKeysFromLedgerHandler } from './migrateMissingPublicKeysFromLedger';
import { buildRpcCall } from '@src/tests/test-utils';
import { AccountsService } from '../../accounts/AccountsService';

jest.mock('../../secrets/SecretsService');
jest.mock('@avalabs/core-wallets-sdk');
Expand All @@ -19,13 +20,23 @@ describe('src/background/services/ledger/handlers/migrateMissingPublicKeysFromLe
id: '123',
method: ExtensionRequest.LEDGER_MIGRATE_MISSING_PUBKEYS,
} as any;
const accountsService = new AccountsService(
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any,
{} as any
);

const secretsService = jest.mocked(new SecretsService({} as any));
const ledgerService = {} as any;
const handleRequest = async () => {
const handler = new MigrateMissingPublicKeysFromLedgerHandler(
secretsService,
ledgerService
ledgerService,
accountsService
);
return handler.handle(buildRpcCall(request));
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { SecretType } from '../../secrets/models';
import { SecretsService } from '../../secrets/SecretsService';
import { PubKeyType } from '../../wallet/models';
import { LedgerService } from '../LedgerService';
import { AccountsService } from '../../accounts/AccountsService';

type HandlerType = ExtensionRequestHandler<
ExtensionRequest.LEDGER_MIGRATE_MISSING_PUBKEYS,
Expand All @@ -23,12 +24,18 @@ export class MigrateMissingPublicKeysFromLedgerHandler implements HandlerType {

constructor(
private secretsService: SecretsService,
private ledgerService: LedgerService
private ledgerService: LedgerService,
private accountsService: AccountsService
) {}

handle: HandlerType['handle'] = async ({ request }) => {
try {
const secrets = await this.secretsService.getActiveAccountSecrets();
if (!this.accountsService.activeAccount) {
throw new Error('There is no active account');
}
const secrets = await this.secretsService.getActiveAccountSecrets(
this.accountsService.activeAccount
);
if (
secrets.secretType !== SecretType.Ledger &&
secrets.secretType !== SecretType.LedgerLive
Expand Down
Loading

0 comments on commit f406ae3

Please sign in to comment.