Skip to content

Commit

Permalink
fix: ledger btc policy issues (#76)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w authored Oct 21, 2024
1 parent 50c351f commit 4063c4f
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/background/services/secrets/SecretsService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -883,7 +883,7 @@ describe('src/background/services/secrets/SecretsService.ts', () => {
mockMnemonicWallet();

await expect(
secretsService.getBtcWalletPolicyDetails()
secretsService.getBtcWalletPolicyDetails(activeAccountData)
).resolves.toBeUndefined();
});

Expand Down
6 changes: 3 additions & 3 deletions src/background/services/secrets/SecretsService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,14 +413,14 @@ export class SecretsService {
}

async getBtcWalletPolicyDetails(
activeAccount?: Account
account: Account
): Promise<
{ accountIndex: number; details?: BtcWalletPolicyDetails } | undefined
> {
if (!activeAccount) {
if (!account) {
return undefined;
}
const secrets = await this.getAccountSecrets(activeAccount);
const secrets = await this.getAccountSecrets(account);

if (secrets.secretType === SecretType.LedgerLive && secrets.account) {
const accountIndex = secrets.account.index;
Expand Down
10 changes: 5 additions & 5 deletions src/background/services/wallet/WalletService.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1466,9 +1466,9 @@ describe('background/services/wallet/WalletService.ts', () => {
it('throws if it fails to find policy details', async () => {
secretsService.getBtcWalletPolicyDetails.mockResolvedValueOnce(undefined);

await expect(walletService['parseWalletPolicyDetails']()).rejects.toThrow(
'Error while parsing wallet policy: missing data.'
);
await expect(
walletService['parseWalletPolicyDetails']({} as Account)
).rejects.toThrow('Error while parsing wallet policy: missing data.');
});

it('returns the correct data for Ledger Live', async () => {
Expand All @@ -1485,7 +1485,7 @@ describe('background/services/wallet/WalletService.ts', () => {
(createWalletPolicy as jest.Mock).mockReturnValueOnce(walletPolicy);

await expect(
walletService['parseWalletPolicyDetails']()
walletService['parseWalletPolicyDetails']({} as Account)
).resolves.toStrictEqual({
hmac: Buffer.from(hmacHex, 'hex'),
policy: walletPolicy,
Expand Down Expand Up @@ -1513,7 +1513,7 @@ describe('background/services/wallet/WalletService.ts', () => {
(createWalletPolicy as jest.Mock).mockReturnValueOnce(walletPolicy);

await expect(
walletService['parseWalletPolicyDetails']()
walletService['parseWalletPolicyDetails']({} as Account)
).resolves.toStrictEqual({
hmac: Buffer.from(hmacHex, 'hex'),
policy: walletPolicy,
Expand Down
13 changes: 9 additions & 4 deletions src/background/services/wallet/WalletService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import { getProviderForNetwork } from '@src/utils/network/getProviderForNetwork'
import { Network } from '../network/models';
import { AccountsService } from '../accounts/AccountsService';
import { utils } from '@avalabs/avalanchejs';
import { Account } from '../accounts/models';

@singleton()
export class WalletService implements OnLock, OnUnlock {
Expand Down Expand Up @@ -327,7 +328,9 @@ export class WalletService implements OnLock, OnUnlock {
throw new Error('Ledger transport not available');
}

const walletPolicy = await this.parseWalletPolicyDetails();
const walletPolicy = await this.parseWalletPolicyDetails(
this.accountsService.activeAccount
);
const accountIndexToUse =
accountIndex === undefined ? secrets.account.index : accountIndex;

Expand Down Expand Up @@ -358,7 +361,9 @@ export class WalletService implements OnLock, OnUnlock {
throw new Error('Account public key not available');
}

const walletPolicy = await this.parseWalletPolicyDetails();
const walletPolicy = await this.parseWalletPolicyDetails(
secrets.account
);

return new BitcoinLedgerWallet(
Buffer.from(addressPublicKey.evm, 'hex'),
Expand Down Expand Up @@ -835,9 +840,9 @@ export class WalletService implements OnLock, OnUnlock {
);
}

private async parseWalletPolicyDetails() {
private async parseWalletPolicyDetails(account: Account) {
const policyInfo = await this.secretService.getBtcWalletPolicyDetails(
this.accountsService.activeAccount
account
);

if (!policyInfo || !policyInfo.details) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export class GetBtcWalletPolicyDetails implements HandlerType {

handle: HandlerType['handle'] = async ({ request }) => {
try {
const activeAccount = this.accountService.activeAccount;
const { activeAccount } = this.accountService;

if (!activeAccount) {
throw new Error('no account selected');
Expand All @@ -31,7 +31,9 @@ export class GetBtcWalletPolicyDetails implements HandlerType {
throw new Error('incorrect account type');
}

const policyInfo = await this.secretsService.getBtcWalletPolicyDetails();
const policyInfo = await this.secretsService.getBtcWalletPolicyDetails(
activeAccount
);

return {
...request,
Expand Down
33 changes: 16 additions & 17 deletions src/contexts/LedgerProvider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ export function LedgerContextProvider({ children }: { children: any }) {
* Get the extended public key for the given path (m/44'/60'/0' by default)
* @returns Promise<extended public key>
*/
async function getExtendedPublicKey(path?: string) {
const getExtendedPublicKey = useCallback(async (path?: string) => {
if (!transportRef.current) {
throw new Error('no device detected');
}
Expand All @@ -303,23 +303,22 @@ export function LedgerContextProvider({ children }: { children: any }) {
throw new Error(pubKeyError);
}
return pubKey;
}
}, []);

async function getPublicKey(
accountIndex: number,
pathType: DerivationPath,
vm: VM = 'EVM'
) {
if (!transportRef.current) {
throw new Error('no device detected');
}
return getPubKeyFromTransport(
transportRef.current,
accountIndex,
pathType,
vm
);
}
const getPublicKey = useCallback(
async (accountIndex: number, pathType: DerivationPath, vm: VM = 'EVM') => {
if (!transportRef.current) {
throw new Error('no device detected');
}
return getPubKeyFromTransport(
transportRef.current,
accountIndex,
pathType,
vm
);
},
[]
);

/**
* When the user plugs-in/connects their ledger for the first time a
Expand Down

0 comments on commit 4063c4f

Please sign in to comment.