From 92a99ed6c4ded046f0cb85dc3d55a561d4761fe2 Mon Sep 17 00:00:00 2001 From: kyranjamie Date: Thu, 15 Aug 2024 11:12:11 +0200 Subject: [PATCH] fix(ledger): support stx_signMessage, closes #5756 --- src/app/common/rpc-helpers.ts | 23 ------------------- .../use-rpc-sign-stacks-transaction.ts | 12 ++++------ .../compliance-checker.query.ts | 14 +++++------ src/app/routes/rpc-routes.tsx | 2 ++ .../bitcoin/native-segwit-account.hooks.ts | 4 ++-- 5 files changed, 15 insertions(+), 40 deletions(-) diff --git a/src/app/common/rpc-helpers.ts b/src/app/common/rpc-helpers.ts index 307e2eaf466..8d04d19a814 100644 --- a/src/app/common/rpc-helpers.ts +++ b/src/app/common/rpc-helpers.ts @@ -1,13 +1,7 @@ import { useMemo } from 'react'; -import { RpcErrorCode } from '@btckit/types'; - -import { WalletMethodMap, makeRpcErrorResponse } from '@shared/rpc/rpc-methods'; -import { closeWindow } from '@shared/utils'; - import { useDefaultRequestParams } from './hooks/use-default-request-search-params'; import { initialSearchParams } from './initial-search-params'; -import { useWalletType } from './use-wallet-type'; export function useRpcRequestParams() { const defaultParams = useDefaultRequestParams(); @@ -19,20 +13,3 @@ export function useRpcRequestParams() { [defaultParams] ); } - -export function useRejectIfLedgerWallet(request: keyof WalletMethodMap) { - const { walletType } = useWalletType(); - const { tabId, requestId } = useRpcRequestParams(); - if (walletType !== 'ledger' || !tabId) return; - chrome.tabs.sendMessage( - tabId, - makeRpcErrorResponse(request, { - id: requestId, - error: { - code: RpcErrorCode.INTERNAL_ERROR, - message: 'Ledger wallet is not supported', - }, - }) - ); - closeWindow(); -} diff --git a/src/app/pages/rpc-sign-stacks-transaction/use-rpc-sign-stacks-transaction.ts b/src/app/pages/rpc-sign-stacks-transaction/use-rpc-sign-stacks-transaction.ts index 9bc1b175267..903cb634650 100644 --- a/src/app/pages/rpc-sign-stacks-transaction/use-rpc-sign-stacks-transaction.ts +++ b/src/app/pages/rpc-sign-stacks-transaction/use-rpc-sign-stacks-transaction.ts @@ -1,5 +1,4 @@ import { useMemo } from 'react'; -import { useSearchParams } from 'react-router-dom'; import { RpcErrorCode } from '@btckit/types'; import { bytesToHex } from '@stacks/common'; @@ -9,18 +8,15 @@ import { makeRpcErrorResponse, makeRpcSuccessResponse } from '@shared/rpc/rpc-me import { closeWindow } from '@shared/utils'; import { useDefaultRequestParams } from '@app/common/hooks/use-default-request-search-params'; -import { useRejectIfLedgerWallet } from '@app/common/rpc-helpers'; +import { initialSearchParams } from '@app/common/initial-search-params'; import { getTxSenderAddress } from '@app/common/transactions/stacks/transaction.utils'; import { useSignStacksTransaction } from '@app/store/transactions/transaction.hooks'; function useRpcSignStacksTransactionParams() { - useRejectIfLedgerWallet('stx_signTransaction'); - - const [searchParams] = useSearchParams(); const { origin, tabId } = useDefaultRequestParams(); - const requestId = searchParams.get('requestId'); - const txHex = searchParams.get('txHex'); - const isMultisig = searchParams.get('isMultisig'); + const requestId = initialSearchParams.get('requestId'); + const txHex = initialSearchParams.get('txHex'); + const isMultisig = initialSearchParams.get('isMultisig'); if (!requestId || !txHex || !origin) throw new Error('Invalid params'); diff --git a/src/app/query/common/compliance-checker/compliance-checker.query.ts b/src/app/query/common/compliance-checker/compliance-checker.query.ts index 9ac822583be..8ec8432cd46 100644 --- a/src/app/query/common/compliance-checker/compliance-checker.query.ts +++ b/src/app/query/common/compliance-checker/compliance-checker.query.ts @@ -2,11 +2,11 @@ import { type UseQueryOptions, useQueries } from '@tanstack/react-query'; import axios from 'axios'; import type { BitcoinNetworkModes } from '@leather.io/models'; -import { ensureArray } from '@leather.io/utils'; +import { ensureArray, isEmptyString } from '@leather.io/utils'; import { analytics } from '@shared/utils/analytics'; -import { useCurrentAccountNativeSegwitIndexZeroSigner } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks'; +import { useCurrentAccountNativeSegwitIndexZeroSignerNullable } from '@app/store/accounts/blockchain/bitcoin/native-segwit-account.hooks'; import { useCurrentNetwork } from '@app/store/networks/networks.selectors'; const checkApi = 'https://api.chainalysis.com/api/risk/v2/entities'; @@ -67,19 +67,19 @@ function makeComplianceQuery( function useCheckAddressComplianceQueries(addresses: string[]) { const network = useCurrentNetwork(); return useQueries({ - queries: addresses.map(address => - makeComplianceQuery(address, network.chain.bitcoin.bitcoinNetwork) - ), + queries: addresses + .filter(address => !isEmptyString(address)) + .map(address => makeComplianceQuery(address, network.chain.bitcoin.bitcoinNetwork)), }); } export const compliantErrorBody = 'Unable to handle request, errorCode: 1398'; export function useBreakOnNonCompliantEntity(address: string | string[]) { - const nativeSegwitSigner = useCurrentAccountNativeSegwitIndexZeroSigner(); + const nativeSegwitSigner = useCurrentAccountNativeSegwitIndexZeroSignerNullable(); const complianceReports = useCheckAddressComplianceQueries([ - nativeSegwitSigner.address, + nativeSegwitSigner?.address ?? '', ...ensureArray(address), ]); diff --git a/src/app/routes/rpc-routes.tsx b/src/app/routes/rpc-routes.tsx index 0f2a1f98c36..5e026314c3a 100644 --- a/src/app/routes/rpc-routes.tsx +++ b/src/app/routes/rpc-routes.tsx @@ -6,6 +6,7 @@ import { RouteUrls } from '@shared/route-urls'; import { EditNonceDialog } from '@app/features/dialogs/edit-nonce-dialog/edit-nonce-dialog'; import { ledgerBitcoinTxSigningRoutes } from '@app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container'; import { ledgerStacksMessageSigningRoutes } from '@app/features/ledger/flows/stacks-message-signing/ledger-stacks-sign-msg.routes'; +import { ledgerStacksTxSigningRoutes } from '@app/features/ledger/flows/stacks-tx-signing/ledger-sign-stacks-tx-container'; import { RpcGetAddresses } from '@app/pages/rpc-get-addresses/rpc-get-addresses'; import { rpcSendTransferRoutes } from '@app/pages/rpc-send-transfer/rpc-send-transfer.routes'; import { RpcSignPsbt } from '@app/pages/rpc-sign-psbt/rpc-sign-psbt'; @@ -77,6 +78,7 @@ export const rpcRequestRoutes = ( } > + {ledgerStacksTxSigningRoutes} } /> 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 059ff8f82e8..5722df807ca 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 @@ -8,8 +8,8 @@ import { deriveAddressIndexZeroFromAccount, deriveNativeSegwitAccountFromRootKeychain, getNativeSegWitPaymentFromAddressIndex, - getNativeSegwitAccountDerivationPath, lookUpLedgerKeysByPath, + makeNativeSegwitAccountDerivationPath, } from '@leather.io/bitcoin'; import { extractAddressIndexFromPath } from '@leather.io/crypto'; import { useBitcoinClient } from '@leather.io/query'; @@ -34,7 +34,7 @@ import { const selectNativeSegwitAccountBuilder = bitcoinAccountBuilderFactory( deriveNativeSegwitAccountFromRootKeychain, - lookUpLedgerKeysByPath(getNativeSegwitAccountDerivationPath) + lookUpLedgerKeysByPath(makeNativeSegwitAccountDerivationPath) ); const selectCurrentNetworkNativeSegwitAccountBuilder = createSelector(