Skip to content

Commit

Permalink
fix: choose account prevent bug, closes #5509
Browse files Browse the repository at this point in the history
  • Loading branch information
alter-eggo committed Jul 10, 2024
1 parent cf35f60 commit 89500a8
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 104 deletions.
16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,14 @@
"@dlc-link/dlc-tools": "1.1.1",
"@fungible-systems/zone-file": "2.0.0",
"@hirosystems/token-metadata-api-client": "1.2.0",
"@leather.io/bitcoin": "0.8.2",
"@leather.io/constants": "0.8.0",
"@leather.io/crypto": "1.0.2",
"@leather.io/models": "0.10.0",
"@leather.io/query": "0.9.5",
"@leather.io/tokens": "0.6.0",
"@leather.io/ui": "1.6.0",
"@leather.io/utils": "0.9.2",
"@leather.io/bitcoin": "0.8.4",
"@leather.io/constants": "0.8.1",
"@leather.io/crypto": "1.0.3",
"@leather.io/models": "0.10.1",
"@leather.io/query": "0.10.0",
"@leather.io/tokens": "0.6.1",
"@leather.io/ui": "1.6.1",
"@leather.io/utils": "0.9.3",
"@ledgerhq/hw-transport-webusb": "6.27.19",
"@noble/hashes": "1.4.0",
"@noble/secp256k1": "2.1.0",
Expand Down
110 changes: 35 additions & 75 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 28 additions & 11 deletions src/app/common/authentication/use-finish-auth-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@stacks/wallet-sdk';

import { gaiaUrl } from '@leather.io/constants';
import { useHiroApiRateLimiter } from '@leather.io/query';

import { finalizeAuthResponse } from '@shared/actions/finalize-auth-response';
import { logger } from '@shared/logger';
Expand All @@ -32,6 +33,7 @@ export function useFinishAuthRequest() {
const { origin, tabId } = useAuthRequestParams();

const wallet = useLegacyStacksWallet();
const hiroLimiter = useHiroApiRateLimiter();

// TODO: It would be good to separate out finishing auth by the wallet vs an app
// so that the additional data we provide apps can be removed from our onboarding.
Expand All @@ -55,12 +57,20 @@ export function useFinishAuthRequest() {
if (!wallet) return;

try {
const gaiaHubConfig = await createWalletGaiaConfig({ gaiaHubUrl: gaiaUrl, wallet });
const gaiaHubConfig = await hiroLimiter.add(
() => createWalletGaiaConfig({ gaiaHubUrl: gaiaUrl, wallet }),
{
priority: 5,
throwOnTimeout: true,
}
);

const walletConfig = await getOrCreateWalletConfig({
wallet,
gaiaHubConfig,
skipUpload: true,
});

await updateWalletConfigWithApp({
wallet,
walletConfig,
Expand All @@ -75,17 +85,23 @@ export function useFinishAuthRequest() {
},
});

const authResponse = await makeAuthResponse({
gaiaHubUrl: gaiaUrl,
appDomain: appURL.origin,
transitPublicKey: decodedAuthRequest.public_keys[0],
scopes: decodedAuthRequest.scopes,
account: account,
additionalData: {
...getLegacyAuthBitcoinData(accountIndex),
walletProvider: 'leather',
const authResponse = await hiroLimiter.add(
() => {
return makeAuthResponse({
gaiaHubUrl: gaiaUrl,
appDomain: appURL.origin,
transitPublicKey: decodedAuthRequest.public_keys[0],
scopes: decodedAuthRequest.scopes,
account: account,
additionalData: {
...getLegacyAuthBitcoinData(accountIndex),
walletProvider: 'leather',
},
});
},
});
{ priority: 5, throwOnTimeout: true }
);

keyActions.switchAccount(accountIndex);
finalizeAuthResponse({
decodedAuthRequest,
Expand All @@ -112,6 +128,7 @@ export function useFinishAuthRequest() {
appName,
getLegacyAuthBitcoinData,
keyActions,
hiroLimiter,
]
);
}
4 changes: 3 additions & 1 deletion src/app/common/hooks/use-key-actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ import { useMemo } from 'react';

import { generateSecretKey } from '@stacks/wallet-sdk';

import { useBitcoinClient } from '@leather.io/query';

import { logger } from '@shared/logger';
import { clearChromeStorage } from '@shared/storage/redux-pesist';
import { analytics } from '@shared/utils/analytics';
Expand All @@ -10,7 +12,7 @@ import { queryClient } from '@app/common/persistence';
import { partiallyClearLocalStorage } from '@app/common/store-utils';
import { useAppDispatch } from '@app/store';
import { createNewAccount, stxChainActions } from '@app/store/chains/stx-chain.actions';
import { useBitcoinClient, useStacksClient } from '@app/store/common/api-clients.hooks';
import { useStacksClient } from '@app/store/common/api-clients.hooks';
import { inMemoryKeyActions } from '@app/store/in-memory-key/in-memory-key.actions';
import { bitcoinKeysSlice } from '@app/store/ledger/bitcoin/bitcoin-key.slice';
import { stacksKeysSlice } from '@app/store/ledger/stacks/stacks-key.slice';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import {
lookUpLedgerKeysByPath,
} from '@leather.io/bitcoin';
import { extractAddressIndexFromPath } from '@leather.io/crypto';
import { useBitcoinClient } from '@leather.io/query';
import { reverseBytes } from '@leather.io/utils';

import { BitcoinInputSigningConfig } from '@shared/crypto/bitcoin/signer-config';
import { analytics } from '@shared/utils/analytics';

import { mnemonicToRootNode } from '@app/common/keychain/keychain';
import { useBitcoinClient } from '@app/store/common/api-clients.hooks';
import { selectCurrentNetwork } from '@app/store/networks/networks.selectors';
import { selectCurrentAccountIndex } from '@app/store/software-keys/software-key.selectors';

Expand Down
7 changes: 1 addition & 6 deletions src/app/store/common/api-clients.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
import { useMemo } from 'react';

import { bitcoinClient, stacksClient } from '@leather.io/query';
import { stacksClient } from '@leather.io/query';

import { useCurrentNetworkState } from '../networks/networks.hooks';

export function useBitcoinClient() {
const network = useCurrentNetworkState();
return bitcoinClient(network.chain.bitcoin.bitcoinUrl);
}

export function useStacksClient() {
const network = useCurrentNetworkState();

Expand Down
9 changes: 7 additions & 2 deletions src/app/store/software-keys/software-key.actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,17 @@ function setWalletEncryptionPassword(args: {
await checkForLegacyGaiaConfigWithKnownGeneratedAccountIndex(secretKey);

async function doesStacksAddressHaveBalance(address: string) {
const resp = await stxClient.getAccountBalance(address);
const resp = await stxClient.getAccountBalance(address, new AbortSignal());
return Number(resp.stx.balance) > 0;
}

async function doesStacksAddressHaveBnsName(address: string) {
const resp = await fetchNamesForAddress({ client: stxClient, address, isTestnet: false });
const resp = await fetchNamesForAddress({
client: stxClient,
address,
isTestnet: false,
signal: new AbortSignal(),
});
return resp.names.length > 0;
}

Expand Down

0 comments on commit 89500a8

Please sign in to comment.