Skip to content

Commit

Permalink
refactor: do not pass NetworkService to the SecretsService
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w committed Nov 15, 2024
1 parent d67ce5a commit 38b041e
Show file tree
Hide file tree
Showing 19 changed files with 288 additions and 155 deletions.
7 changes: 7 additions & 0 deletions src/background/models.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { Avalanche } from '@avalabs/core-wallets-sdk';

export interface DomainMetadataRequest {
method: 'avalanche_sendDomainMetadata';
params: DomainMetadata;
Expand Down Expand Up @@ -80,3 +82,8 @@ export type Never<T> = {
export type ArrayElement<A> = A extends readonly (infer T)[] ? T : never;

export const ACTION_HANDLED_BY_MODULE = '__handled.via.vm.modules__';

export type AddressResolutionOptions = {
isMainnet: boolean;
providerXP: Avalanche.JsonRpcProvider;
};
41 changes: 23 additions & 18 deletions src/background/services/accounts/AccountsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ describe('background/services/accounts/AccountsService', () => {
};
};

const providerXP = { getAddress: jest.fn() } as any;

beforeEach(() => {
jest.resetAllMocks();
(storageService.load as jest.Mock).mockResolvedValue(emptyAccounts);
Expand All @@ -154,6 +156,9 @@ describe('background/services/accounts/AccountsService', () => {
});
networkService.developerModeChanged.add = jest.fn();
networkService.developerModeChanged.remove = jest.fn();
jest
.spyOn(networkService, 'getAvalanceProviderXP')
.mockResolvedValue(providerXP);
accountsService = new AccountsService(
storageService,
networkService,
Expand Down Expand Up @@ -267,13 +272,13 @@ describe('background/services/accounts/AccountsService', () => {
1,
0,
walletId,
networkService
{ isMainnet: true, providerXP }
);
expect(secretsService.getAddresses).toHaveBeenNthCalledWith(
2,
1,
walletId,
networkService
{ isMainnet: true, providerXP }
);
expect(secretsService.getImportedAddresses).toBeCalledTimes(3);

Expand Down Expand Up @@ -378,7 +383,7 @@ describe('background/services/accounts/AccountsService', () => {

expect(secretsService.getImportedAddresses).toHaveBeenCalledWith(
'fb-acc',
networkService
{ isMainnet: true, providerXP }
);
expect(secretsService.getAddresses).toHaveBeenCalledTimes(0);
expect(accountsService.getAccounts().imported['fb-acc']).toEqual({
Expand Down Expand Up @@ -500,7 +505,7 @@ describe('background/services/accounts/AccountsService', () => {
expect(secretsService.addAddress).toBeCalledWith({
index: 0,
walletId: WALLET_ID,
networkService,
options: { isMainnet: true, providerXP },
ledgerService,
});

Expand Down Expand Up @@ -552,7 +557,7 @@ describe('background/services/accounts/AccountsService', () => {
expect(secretsService.addAddress).toBeCalledWith({
index: 2,
walletId: WALLET_ID,
networkService,
options: { isMainnet: true, providerXP },
ledgerService,
});
expect(permissionsService.addWhitelistDomains).toBeCalledTimes(1);
Expand Down Expand Up @@ -593,7 +598,7 @@ describe('background/services/accounts/AccountsService', () => {
expect(secretsService.addAddress).toBeCalledWith({
index: 2,
walletId: WALLET_ID,
networkService,
options: { isMainnet: true, providerXP },
ledgerService,
});
expect(permissionsService.addWhitelistDomains).toBeCalledTimes(1);
Expand Down Expand Up @@ -680,10 +685,10 @@ describe('background/services/accounts/AccountsService', () => {
options,
});
expect(secretsService.addImportedWallet).toBeCalledTimes(1);
expect(secretsService.addImportedWallet).toBeCalledWith(
options,
networkService
);
expect(secretsService.addImportedWallet).toBeCalledWith(options, {
isMainnet: true,
providerXP,
});
expect(commitMock).toHaveBeenCalled();
expect(permissionsService.addWhitelistDomains).toBeCalledTimes(1);
expect(permissionsService.addWhitelistDomains).toBeCalledWith(
Expand Down Expand Up @@ -738,10 +743,10 @@ describe('background/services/accounts/AccountsService', () => {

await accountsService.addImportedAccount({ options });
expect(secretsService.addImportedWallet).toBeCalledTimes(1);
expect(secretsService.addImportedWallet).toBeCalledWith(
options,
networkService
);
expect(secretsService.addImportedWallet).toBeCalledWith(options, {
isMainnet: true,
providerXP,
});
expect(commitMock).toHaveBeenCalled();
expect(permissionsService.addWhitelistDomains).toBeCalledTimes(1);
expect(permissionsService.addWhitelistDomains).toBeCalledWith(
Expand Down Expand Up @@ -839,10 +844,10 @@ describe('background/services/accounts/AccountsService', () => {
'0x1'
);
expect(secretsService.addImportedWallet).toBeCalledTimes(1);
expect(secretsService.addImportedWallet).toBeCalledWith(
options,
networkService
);
expect(secretsService.addImportedWallet).toBeCalledWith(options, {
isMainnet: true,
providerXP,
});
expect(commitMock).not.toHaveBeenCalled();
expect(permissionsService.addWhitelistDomains).not.toHaveBeenCalled();
});
Expand Down
13 changes: 9 additions & 4 deletions src/background/services/accounts/AccountsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import { LedgerService } from '../ledger/LedgerService';
import { WalletConnectService } from '../walletConnect/WalletConnectService';
import { Network } from '../network/models';
import { isDevnet } from '@src/utils/isDevnet';
import { getAddressResolutionOptions } from '@src/background/utils/getAddressResolutionOptions';

type AddAccountParams = {
walletId: string;
Expand Down Expand Up @@ -224,17 +225,21 @@ export class AccountsService implements OnLock, OnUnlock {
};

async getAddressesForAccount(account: Account): Promise<DerivedAddresses> {
const addressResolutionOptions = await getAddressResolutionOptions(
this.networkService
);

if (account.type !== AccountType.PRIMARY) {
return this.secretsService.getImportedAddresses(
account.id,
this.networkService
addressResolutionOptions
);
}

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

return {
Expand Down Expand Up @@ -367,8 +372,8 @@ export class AccountsService implements OnLock, OnUnlock {
const addresses = await this.secretsService.addAddress({
index: nextIndex,
walletId,
networkService: this.networkService,
ledgerService: this.ledgerService,
options: await getAddressResolutionOptions(this.networkService),
});

const id = crypto.randomUUID();
Expand Down Expand Up @@ -413,7 +418,7 @@ export class AccountsService implements OnLock, OnUnlock {
try {
const { account, commit } = await this.secretsService.addImportedWallet(
options,
this.networkService
await getAddressResolutionOptions(this.networkService)
);

const existingAccount = this.#findAccountByAddress(account.addressC);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ describe('src/background/services/onboarding/handlers/seedlessOnboardingHandler.
addFavoriteNetwork: jest.fn(),
getAvalancheNetwork: jest.fn(),
setNetwork: jest.fn(),
isMainnet: () => false,
getAvalanceProviderXP: jest.fn(),
} as unknown as NetworkService;
const secretsServiceMock = {
getWalletAccountsSecretsById: jest.fn(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { OnboardingService } from '../OnboardingService';
import { LockService } from '../../lock/LockService';
import { finalizeOnboarding } from '../finalizeOnboarding';
import { startOnboarding } from '../startOnboarding';
import { getAddressResolutionOptions } from '@src/background/utils/getAddressResolutionOptions';

type HandlerType = ExtensionRequestHandler<
ExtensionRequest.SEEDLESS_ONBOARDING_SUBMIT,
Expand Down Expand Up @@ -72,7 +73,9 @@ export class SeedlessOnboardingHandler implements HandlerType {

const memorySessionStorage = new MemorySessionStorage(seedlessSignerToken);
const seedlessWallet = new SeedlessWallet({
networkService: this.networkService,
addressResolutionOptions: await getAddressResolutionOptions(
this.networkService
),
sessionStorage: memorySessionStorage,
});

Expand Down
Loading

0 comments on commit 38b041e

Please sign in to comment.