From 89500a807095c31b275d279b4c86fc5760cee096 Mon Sep 17 00:00:00 2001 From: Anastasios Date: Tue, 9 Jul 2024 17:48:36 +0400 Subject: [PATCH] fix: choose account prevent bug, closes #5509 --- package.json | 16 +-- pnpm-lock.yaml | 110 ++++++------------ .../authentication/use-finish-auth-request.ts | 39 +++++-- src/app/common/hooks/use-key-actions.ts | 4 +- .../bitcoin/native-segwit-account.hooks.ts | 2 +- src/app/store/common/api-clients.hooks.ts | 7 +- .../software-keys/software-key.actions.ts | 9 +- 7 files changed, 83 insertions(+), 104 deletions(-) diff --git a/package.json b/package.json index 64531f12e15..5626fa6bb45 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index ed36dd35c5f..d6ba319d76e 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -36,29 +36,29 @@ importers: specifier: 1.2.0 version: 1.2.0(encoding@0.1.13) '@leather.io/bitcoin': - specifier: 0.8.2 - version: 0.8.2(encoding@0.1.13) + specifier: 0.8.4 + version: 0.8.4(encoding@0.1.13) '@leather.io/constants': - specifier: 0.8.0 - version: 0.8.0 + specifier: 0.8.1 + version: 0.8.1 '@leather.io/crypto': - specifier: 1.0.2 - version: 1.0.2 + specifier: 1.0.3 + version: 1.0.3 '@leather.io/models': - specifier: 0.10.0 - version: 0.10.0 + specifier: 0.10.1 + version: 0.10.1 '@leather.io/query': - specifier: 0.9.5 - version: 0.9.5(@stacks/network@6.13.0(encoding@0.1.13))(encoding@0.1.13)(react@18.3.1) + specifier: 0.10.0 + version: 0.10.0(@stacks/network@6.13.0(encoding@0.1.13))(encoding@0.1.13)(react@18.3.1) '@leather.io/tokens': - specifier: 0.6.0 - version: 0.6.0 + specifier: 0.6.1 + version: 0.6.1 '@leather.io/ui': - specifier: 1.6.0 - version: 1.6.0(@babel/core@7.24.4)(@babel/preset-env@7.24.6(@babel/core@7.24.4))(@swc/core@1.4.8)(@types/react-dom@18.3.0)(@types/react@18.3.3)(encoding@0.1.13)(expo-modules-autolinking@1.11.1)(postcss@8.4.38)(ts-node@10.9.2(@swc/core@1.4.8)(@types/node@20.12.12)(typescript@5.4.5))(typescript@5.4.5) + specifier: 1.6.1 + version: 1.6.1(@babel/core@7.24.4)(@babel/preset-env@7.24.6(@babel/core@7.24.4))(@swc/core@1.4.8)(@types/react-dom@18.3.0)(@types/react@18.3.3)(encoding@0.1.13)(expo-modules-autolinking@1.11.1)(postcss@8.4.38)(ts-node@10.9.2(@swc/core@1.4.8)(@types/node@20.12.12)(typescript@5.4.5))(typescript@5.4.5) '@leather.io/utils': - specifier: 0.9.2 - version: 0.9.2 + specifier: 0.9.3 + version: 0.9.3 '@ledgerhq/hw-transport-webusb': specifier: 6.27.19 version: 6.27.19 @@ -2880,20 +2880,14 @@ packages: '@jridgewell/trace-mapping@0.3.9': resolution: {integrity: sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==} - '@leather.io/bitcoin@0.8.2': - resolution: {integrity: sha512-nUI2Q/kJgIg8CPdaiM4V/uy5PoaEiwwnHhb4iFPU3VM9Iw7bHmGo0qKQSRgm2lz+oNKQ79apDklM6ldZr2e4QA==} - - '@leather.io/bitcoin@0.8.3': - resolution: {integrity: sha512-EAC/ksrqsIyxhLGzuyB5oPBnOBwywoARwZU16IPQc34FF9tPNtz7d9yZ0rnhjapNkYsgn87T37zy1xRzDIomYQ==} - - '@leather.io/constants@0.8.0': - resolution: {integrity: sha512-PmiFZG+MLqU6hi9Nr3wfb9SG57FMnV+4DCtDOr5SWB/7P1OR//qYDoaiSS9B4VwW4WRjdy32EmO/Om8z61WuMw==} + '@leather.io/bitcoin@0.8.4': + resolution: {integrity: sha512-8uf2jxKFKIih4Zel/dxVeOkR+K5hZq+d5pOHXqbdCFRFdxjQ4/b+pxx7vfMbnU+l2ZDL0xOx2brYwGAF5mpSZw==} '@leather.io/constants@0.8.1': resolution: {integrity: sha512-bw+AcJwkLaVDaXSMPVy5uVxGblSO8ocI52RmjWb2LfhKJk5ZoV75IdGeR0S59aMbQdSmUmYqmWmPkuDmkmSj0Q==} - '@leather.io/crypto@1.0.2': - resolution: {integrity: sha512-hEQZsqtmDCjWu6Z8nfpX/1zTg2T0GUL3GEi564najH+781WbvXThAD7S0Fw+QU0NEzF7ilOxMBKa38FIOd7J6Q==} + '@leather.io/crypto@1.0.3': + resolution: {integrity: sha512-HFsiV8JgQ4MzFLHJnoeQ00Y4J1JuFLvk3D4rN/r73T2Za5XjUemUYxgH/xYXZZe79warFG99QAWtJV6TDOir4Q==} '@leather.io/eslint-config@0.6.0': resolution: {integrity: sha512-d6N9lBn9u2d73QVGWO+2O2tuScwYB00HG1SIneCqBpCcg1zUf1PUddQG5SChJ9jgUZPYe7SCiwFpgi8FJmb/dw==} @@ -2910,8 +2904,8 @@ packages: '@leather.io/prettier-config@0.5.0': resolution: {integrity: sha512-Pul+4MAyBKnQvqgcKJLbZl4DHnS4kCJzSuaYFW6cfHdre7BFn/iY6Er/Dvm9F8g7VMtkdYu68jEYxQ1Xc7A0KQ==} - '@leather.io/query@0.9.5': - resolution: {integrity: sha512-A+i8dqksEzyhLc60NbPRA3rHQRHaqR50iliqlx2/Y1I9aBzjFghPsVXHcqPkDXzSlKk1L5SwSy5NmlgZ9NjTsw==} + '@leather.io/query@0.10.0': + resolution: {integrity: sha512-zzscg1VEN5ojpvCUTOdEkWAx75Z+1Puw6+5RSUcqhg1b+ngggAoVww5oKb+I6bdxht3SxLomRobJIyduBCpLfg==} peerDependencies: react: '*' @@ -2921,14 +2915,11 @@ packages: '@leather.io/rpc@2.0.1': resolution: {integrity: sha512-nmLKIcjGANhpbUo4w2USV9zZVrNHS/UzD0u8bQT3+4awHPzYvV/390E99EZTnfR4PAKfeADprex1kBkncj3SGw==} - '@leather.io/tokens@0.6.0': - resolution: {integrity: sha512-x+JIHk12UJglf+Z1gcmywZ4HqjX/om5yqeSNU7xSypiJjIjstqco3+D3vGb9XAtqBxPHevKM4pgQaJ1z9JVg8g==} - - '@leather.io/ui@1.6.0': - resolution: {integrity: sha512-fy3oLLA6Kjyyu5bTo2vh54dQ3uHPkmFci1pniqlFkeF0ptLidGlZfIwEhBrrFJujI87S9nzqXCTCb4Xj8k5CEA==} + '@leather.io/tokens@0.6.1': + resolution: {integrity: sha512-lJ3T2s/7PlszC7Bthk6SQpjdGbdfgt8TVh1m+RqIYnY0lBE7ZpmnZG8xnTYNeZLlJ8KbK3kRpPxx4JePKd5e8A==} - '@leather.io/utils@0.9.2': - resolution: {integrity: sha512-0ChJvcwjfxDJpQFJJFhEBcBTnx/umiA6EJDIZVQ+XJV/EXZet9KnY/hwl8DrhjLKyEimK9T9mK9VscUySdmhkg==} + '@leather.io/ui@1.6.1': + resolution: {integrity: sha512-yf7edsrEQ+8k7dy8ELaBtW79HKQ36dmLMo02vC6Bpijdabf+iZ0od04D1S1HoTZXkx48Gwd2rQ2vR+8MKXrUSQ==} '@leather.io/utils@0.9.3': resolution: {integrity: sha512-9KMm6C/vCSjIjDm0qPo4KOAbpig5f5GmFjl5vcl4o1/a4uC8pOCn3bMxZPTgAOJgR6Ljba7mTB09JvxWBYJB8Q==} @@ -17539,33 +17530,11 @@ snapshots: '@jridgewell/resolve-uri': 3.1.2 '@jridgewell/sourcemap-codec': 1.4.15 - '@leather.io/bitcoin@0.8.2(encoding@0.1.13)': - dependencies: - '@bitcoinerlab/secp256k1': 1.0.2 - '@leather.io/constants': 0.8.0 - '@leather.io/crypto': 1.0.2 - '@leather.io/models': 0.10.0 - '@leather.io/utils': 0.9.2 - '@noble/hashes': 1.4.0 - '@noble/secp256k1': 2.1.0 - '@scure/base': 1.1.6 - '@scure/bip32': 1.4.0 - '@scure/bip39': 1.3.0 - '@scure/btc-signer': 1.3.2 - '@stacks/common': 6.13.0 - '@stacks/transactions': 6.15.0(encoding@0.1.13) - bip32: 4.0.0 - bitcoinjs-lib: 6.1.5 - ecpair: 2.1.0 - varuint-bitcoin: 1.1.2 - transitivePeerDependencies: - - encoding - - '@leather.io/bitcoin@0.8.3(encoding@0.1.13)': + '@leather.io/bitcoin@0.8.4(encoding@0.1.13)': dependencies: '@bitcoinerlab/secp256k1': 1.0.2 '@leather.io/constants': 0.8.1 - '@leather.io/crypto': 1.0.2 + '@leather.io/crypto': 1.0.3 '@leather.io/models': 0.10.1 '@leather.io/utils': 0.9.3 '@noble/hashes': 1.4.0 @@ -17583,11 +17552,9 @@ snapshots: transitivePeerDependencies: - encoding - '@leather.io/constants@0.8.0': {} - '@leather.io/constants@0.8.1': {} - '@leather.io/crypto@1.0.2': + '@leather.io/crypto@1.0.3': dependencies: '@scure/bip32': 1.4.0 '@scure/bip39': 1.3.0 @@ -17627,11 +17594,11 @@ snapshots: - '@vue/compiler-sfc' - supports-color - '@leather.io/query@0.9.5(@stacks/network@6.13.0(encoding@0.1.13))(encoding@0.1.13)(react@18.3.1)': + '@leather.io/query@0.10.0(@stacks/network@6.13.0(encoding@0.1.13))(encoding@0.1.13)(react@18.3.1)': dependencies: '@fungible-systems/zone-file': 2.0.0 '@hirosystems/token-metadata-api-client': 1.2.0(encoding@0.1.13) - '@leather.io/bitcoin': 0.8.3(encoding@0.1.13) + '@leather.io/bitcoin': 0.8.4(encoding@0.1.13) '@leather.io/constants': 0.8.1 '@leather.io/models': 0.10.1 '@leather.io/rpc': 2.0.1 @@ -17668,13 +17635,13 @@ snapshots: dependencies: '@leather.io/models': 0.10.1 - '@leather.io/tokens@0.6.0': {} + '@leather.io/tokens@0.6.1': {} - '@leather.io/ui@1.6.0(@babel/core@7.24.4)(@babel/preset-env@7.24.6(@babel/core@7.24.4))(@swc/core@1.4.8)(@types/react-dom@18.3.0)(@types/react@18.3.3)(encoding@0.1.13)(expo-modules-autolinking@1.11.1)(postcss@8.4.38)(ts-node@10.9.2(@swc/core@1.4.8)(@types/node@20.12.12)(typescript@5.4.5))(typescript@5.4.5)': + '@leather.io/ui@1.6.1(@babel/core@7.24.4)(@babel/preset-env@7.24.6(@babel/core@7.24.4))(@swc/core@1.4.8)(@types/react-dom@18.3.0)(@types/react@18.3.3)(encoding@0.1.13)(expo-modules-autolinking@1.11.1)(postcss@8.4.38)(ts-node@10.9.2(@swc/core@1.4.8)(@types/node@20.12.12)(typescript@5.4.5))(typescript@5.4.5)': dependencies: '@expo/vector-icons': 14.0.0 - '@leather.io/tokens': 0.6.0 - '@leather.io/utils': 0.9.2 + '@leather.io/tokens': 0.6.1 + '@leather.io/utils': 0.9.3 '@radix-ui/react-accessible-icon': 1.0.3(@types/react-dom@18.3.0)(@types/react@18.3.3)(react-dom@18.2.0(react@18.2.0))(react@18.2.0) '@radix-ui/react-accordion': 1.1.2(@types/react-dom@18.3.0)(@types/react@18.3.3)(react-dom@18.2.0(react@18.2.0))(react@18.2.0) '@radix-ui/react-avatar': 1.0.4(@types/react-dom@18.3.0)(@types/react@18.3.3)(react-dom@18.2.0(react@18.2.0))(react@18.2.0) @@ -17718,13 +17685,6 @@ snapshots: - typescript - utf-8-validate - '@leather.io/utils@0.9.2': - dependencies: - '@leather.io/constants': 0.8.0 - '@leather.io/models': 0.10.0 - '@leather.io/rpc': 2.0.0 - bignumber.js: 9.1.2 - '@leather.io/utils@0.9.3': dependencies: '@leather.io/constants': 0.8.1 diff --git a/src/app/common/authentication/use-finish-auth-request.ts b/src/app/common/authentication/use-finish-auth-request.ts index 31d8d40abd5..59b15f1b73c 100644 --- a/src/app/common/authentication/use-finish-auth-request.ts +++ b/src/app/common/authentication/use-finish-auth-request.ts @@ -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'; @@ -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. @@ -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, @@ -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, @@ -112,6 +128,7 @@ export function useFinishAuthRequest() { appName, getLegacyAuthBitcoinData, keyActions, + hiroLimiter, ] ); } diff --git a/src/app/common/hooks/use-key-actions.ts b/src/app/common/hooks/use-key-actions.ts index b864e1e337c..c52aa2c1afa 100644 --- a/src/app/common/hooks/use-key-actions.ts +++ b/src/app/common/hooks/use-key-actions.ts @@ -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'; @@ -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'; diff --git a/src/app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks.ts b/src/app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks.ts index caa36bc7e3c..7b4caa88c30 100644 --- a/src/app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks.ts +++ b/src/app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks.ts @@ -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'; diff --git a/src/app/store/common/api-clients.hooks.ts b/src/app/store/common/api-clients.hooks.ts index fbe65522849..a2b07a62ca9 100644 --- a/src/app/store/common/api-clients.hooks.ts +++ b/src/app/store/common/api-clients.hooks.ts @@ -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(); diff --git a/src/app/store/software-keys/software-key.actions.ts b/src/app/store/software-keys/software-key.actions.ts index f1a1c1fedeb..d8f0b25081f 100644 --- a/src/app/store/software-keys/software-key.actions.ts +++ b/src/app/store/software-keys/software-key.actions.ts @@ -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; }