Skip to content

Commit

Permalink
fix(ledger): support stx_signMessage, closes #5756
Browse files Browse the repository at this point in the history
  • Loading branch information
kyranjamie committed Aug 15, 2024
1 parent ab973fa commit 92a99ed
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 40 deletions.
23 changes: 0 additions & 23 deletions src/app/common/rpc-helpers.ts
Original file line number Diff line number Diff line change
@@ -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();
Expand All @@ -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();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useMemo } from 'react';
import { useSearchParams } from 'react-router-dom';

import { RpcErrorCode } from '@btckit/types';
import { bytesToHex } from '@stacks/common';
Expand All @@ -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');

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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),
]);

Expand Down
2 changes: 2 additions & 0 deletions src/app/routes/rpc-routes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -77,6 +78,7 @@ export const rpcRequestRoutes = (
</AccountGate>
}
>
{ledgerStacksTxSigningRoutes}
<Route path={RouteUrls.EditNonce} element={<EditNonceDialog />} />
</Route>
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -34,7 +34,7 @@ import {

const selectNativeSegwitAccountBuilder = bitcoinAccountBuilderFactory(
deriveNativeSegwitAccountFromRootKeychain,
lookUpLedgerKeysByPath(getNativeSegwitAccountDerivationPath)
lookUpLedgerKeysByPath(makeNativeSegwitAccountDerivationPath)
);

const selectCurrentNetworkNativeSegwitAccountBuilder = createSelector(
Expand Down

0 comments on commit 92a99ed

Please sign in to comment.