Skip to content

Commit

Permalink
fix: extraneous btc policy prompt (#80)
Browse files Browse the repository at this point in the history
  • Loading branch information
meeh0w authored Oct 23, 2024
1 parent e273d06 commit 437a56f
Showing 1 changed file with 25 additions and 11 deletions.
36 changes: 25 additions & 11 deletions src/pages/Ledger/hooks/useRegisterBtcWalletPolicy.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
import { DerivationPath } from '@avalabs/core-wallets-sdk';
import { ExtensionRequest } from '@src/background/connections/extensionConnection/models';
import { AccountType } from '@src/background/services/accounts/models';
import {
AccountType,
PrimaryAccount,
} from '@src/background/services/accounts/models';
import { GetBtcWalletPolicyDetails } from '@src/background/services/wallet/handlers/getBtcWalletPolicyDetails';
import { WalletDetails } from '@src/background/services/wallet/models';
import { useAccountsContext } from '@src/contexts/AccountsProvider';
import { useConnectionContext } from '@src/contexts/ConnectionProvider';
import { LedgerAppType, useLedgerContext } from '@src/contexts/LedgerProvider';
Expand All @@ -26,11 +30,10 @@ const useRegisterBtcWalletPolicy = () => {
const activeAccount = accounts.active;

useEffect(() => {
const fetchWalletPolicyDetails = async () => {
if (activeAccount?.type !== AccountType.PRIMARY) {
return;
}

const fetchWalletPolicyDetails = async (
account: PrimaryAccount,
details: WalletDetails
) => {
const { masterFingerprint } =
(await request<GetBtcWalletPolicyDetails>({
method: ExtensionRequest.WALLET_GET_BTC_WALLET_POLICY_DETAILS,
Expand All @@ -39,10 +42,10 @@ const useRegisterBtcWalletPolicy = () => {
setMasterFingerprint(masterFingerprint);

if (!masterFingerprint) {
if (walletDetails?.derivationPath === DerivationPath.LedgerLive) {
setWalletPolicyName(`Core - ${activeAccount.name}`);
setWalletPolicyDerivationpath(`44'/60'/${activeAccount.index}'`);
} else if (walletDetails?.derivationPath === DerivationPath.BIP44) {
if (details.derivationPath === DerivationPath.LedgerLive) {
setWalletPolicyName(`Core - ${account.name}`);
setWalletPolicyDerivationpath(`44'/60'/${account.index}'`);
} else if (details.derivationPath === DerivationPath.BIP44) {
setWalletPolicyName('Core');
setWalletPolicyDerivationpath(`44'/60'/0'`);
}
Expand All @@ -51,13 +54,24 @@ const useRegisterBtcWalletPolicy = () => {
}
};

if (activeAccount?.type !== AccountType.PRIMARY || !walletDetails) {
return;
}

// This effect may be called in-between updates coming from AccountsProvider and WalletProvider
// We need to wait for those to be in-sync, otherwise we may prompt for policy registration
// when user is switching from a Ledger wallet/account to a non-Ledger wallet/account.
if (activeAccount.walletId !== walletDetails.id) {
return;
}

setMasterFingerprint(undefined);
setWalletPolicyName(undefined);
setWalletPolicyDerivationpath(undefined);
setShouldRegisterBtcWalletPolicy(false);

if (isUsingLedgerWallet && appType === LedgerAppType.BITCOIN) {
fetchWalletPolicyDetails();
fetchWalletPolicyDetails(activeAccount, walletDetails);
}
}, [
activeAccount,
Expand Down

0 comments on commit 437a56f

Please sign in to comment.