Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: close ledger sheet in request keys flow #6024

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useNavigate } from 'react-router-dom';
import { useLocation, useNavigate } from 'react-router-dom';

import { HStack, styled } from 'leather-styles/jsx';

Expand All @@ -15,13 +15,15 @@ interface ConnectLedgerButtonProps {
}
export function ConnectLedgerButton({ chain }: ConnectLedgerButtonProps) {
const navigate = useNavigate();
const location = useLocation();

const onClick = () => {
navigate(`${chain}/connect-your-ledger`, {
replace: true,
state: {
[immediatelyAttemptLedgerConnection]: true,
backgroundLocation: { pathname: RouteUrls.Home },
fromLocation: location,
},
});
};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useEffect, useState } from 'react';
import { Route, useLocation } from 'react-router-dom';

import { bytesToHex } from '@noble/hashes/utils';
import * as btc from '@scure/btc-signer';
import { hexToBytes } from '@stacks/common';
import BitcoinApp from 'ledger-bitcoin';
Expand Down Expand Up @@ -63,7 +64,7 @@

useEffect(() => () => setUnsignedTransaction(null), []);

const chain = 'bitcoin' as const;

Check warning on line 67 in src/app/features/ledger/flows/bitcoin-tx-signing/ledger-bitcoin-sign-tx-container.tsx

View workflow job for this annotation

GitHub Actions / lint-eslint

This assertion is unnecessary since it does not change the type of the expression

const { signTransaction, latestDeviceResponse, awaitingDeviceConnection } =
useLedgerSignTx<BitcoinApp>({
Expand Down Expand Up @@ -106,6 +107,13 @@
},
});

function closeAction() {
appEvents.publish('ledgerBitcoinTxSigningCancelled', {
unsignedPsbt: unsignedTransaction ? bytesToHex(unsignedTransaction.toPSBT()) : '',
});
Comment on lines +111 to +113
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the action is cancelled, why do we return anything at all with the action? Do we need this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need for abort handler

    function signingAbortedHandler(msg: GlobalAppEvents['ledgerBitcoinTxSigningCancelled']) {
      if (msg.unsignedPsbt === psbt) {
        appEvents.unsubscribe('ledgerBitcoinTxSigningCancelled', signingAbortedHandler);
        appEvents.unsubscribe('ledgerBitcoinTxSigned', txSignedHandler);
        reject(new Error('User cancelled the signing operation'));
      }
    }

ledgerNavigate.cancelLedgerAction();
}

const ledgerContextValue: LedgerTxSigningContext = {
chain,
transaction: unsignedTransaction,
Expand All @@ -118,7 +126,7 @@
return (
<TxSigningFlow
context={ledgerContextValue}
closeAction={canCancelLedgerAction ? ledgerNavigate.cancelLedgerAction : undefined}
closeAction={canCancelLedgerAction ? closeAction : undefined}
/>
);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useNavigate } from 'react-router-dom';
import { useLocation, useNavigate } from 'react-router-dom';

import { Sheet, SheetHeader } from '@leather.io/ui';

Expand All @@ -13,10 +13,15 @@ import { ConnectLedger } from './connect-ledger';

export function ConnectLedgerStart() {
const navigate = useNavigate();
const location = useLocation();

const pageModeRoutingAction = (url: string) =>
whenPageMode({
full() {
navigate(url, { replace: true, state: { [immediatelyAttemptLedgerConnection]: true } });
navigate(url, {
replace: true,
state: { [immediatelyAttemptLedgerConnection]: true, fromLocation: location },
});
},
popup() {
void openIndexPageInNewTab(url);
Expand Down
18 changes: 13 additions & 5 deletions src/app/features/ledger/hooks/use-ledger-navigate.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { useMemo } from 'react';
import { useLocation, useNavigate } from 'react-router-dom';
import { resolvePath, useLocation, useNavigate } from 'react-router-dom';

import { bytesToHex } from '@stacks/common';
import { StacksTransaction } from '@stacks/transactions';
Expand Down Expand Up @@ -38,7 +38,8 @@ export function useLedgerNavigate() {

toConnectAndSignBitcoinTransactionStep(
psbt: Uint8Array,
inputsToSign?: BitcoinInputSigningConfig[]
inputsToSign?: BitcoinInputSigningConfig[],
fromLocation?: typeof location
) {
return navigate(RouteUrls.ConnectLedger, {
replace: true,
Expand All @@ -47,6 +48,7 @@ export function useLedgerNavigate() {
tx: bytesToHex(psbt),
inputsToSign,
backgroundLocation: { pathname: RouteUrls.Home },
fromLocation,
},
});
},
Expand Down Expand Up @@ -83,6 +85,7 @@ export function useLedgerNavigate() {
latestLedgerError: errorMessage,
chain,
backgroundLocation: { pathname: RouteUrls.Home },
fromLocation: location.state.fromLocation,
},
});
},
Expand Down Expand Up @@ -145,10 +148,15 @@ export function useLedgerNavigate() {
},

cancelLedgerAction() {
// Use baseUrl to determine where to go on close
const baseUrl = `/${location.pathname.split('/')[1]}`;
const fromLocation = location.state.fromLocation ?? undefined;

return navigate(baseUrl, {
if (fromLocation) {
return navigate(fromLocation, { state: { ...fromLocation.state, wentBack: true } });
}

const resolvedPath = resolvePath('..', location.pathname);

return navigate(resolvedPath, {
relative: 'path',
replace: true,
state: { ...location.state, wentBack: true },
Expand Down
1 change: 0 additions & 1 deletion src/app/features/ledger/utils/generic-ledger-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,6 @@ function useIsLedgerActionCancellable(): boolean {
const { pathname } = useLocation();
return (
pathname.includes(RouteUrls.ConnectLedger) ||
pathname.includes(RouteUrls.ConnectLedgerSuccess) ||
pathname.includes(RouteUrls.ConnectLedgerError) ||
pathname.includes(RouteUrls.AwaitingDeviceUserAction)
);
Expand Down
5 changes: 3 additions & 2 deletions src/app/pages/legacy-account-auth/legacy-account-auth.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Outlet, useNavigate } from 'react-router-dom';
import { Outlet, useLocation, useNavigate } from 'react-router-dom';

import { RouteUrls } from '@shared/route-urls';
import { closeWindow } from '@shared/utils';
Expand Down Expand Up @@ -30,6 +30,7 @@ export function LegacyAccountAuth() {
const { toggleSwitchAccount } = useSwitchAccountSheet();
const { whenWallet } = useWalletType();
const navigate = useNavigate();
const location = useLocation();

useOnOriginTabClose(() => closeWindow());

Expand All @@ -44,7 +45,7 @@ export function LegacyAccountAuth() {
await finishSignIn(index);
},
async ledger() {
navigate(RouteUrls.ConnectLedger, { state: { index } });
navigate(RouteUrls.ConnectLedger, { state: { index, fromLocation: location } });
await listenForJwtSigningComplete();
},
})();
Expand Down
9 changes: 8 additions & 1 deletion src/app/store/accounts/blockchain/bitcoin/bitcoin.hooks.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useLocation } from 'react-router-dom';

import { bytesToHex } from '@noble/hashes/utils';
import * as btc from '@scure/btc-signer';
import { Psbt } from 'bitcoinjs-lib';
Expand Down Expand Up @@ -257,6 +259,7 @@ export function useGetAssumedZeroIndexSigningConfig() {
export function useSignBitcoinTx() {
const { whenWallet } = useWalletType();
const ledgerNavigate = useLedgerNavigate();
const location = useLocation();
const signSoftwareTx = useSignBitcoinSoftwareTx();
const getDefaultSigningConfig = useGetAssumedZeroIndexSigningConfig();

Expand All @@ -277,7 +280,11 @@ export function useSignBitcoinTx() {
// many routes, in order to achieve a consistent API between
// Ledger/software, we subscribe to the event that occurs when the
// unsigned tx is signed
ledgerNavigate.toConnectAndSignBitcoinTransactionStep(psbt, getSigningConfig(inputsToSign));
ledgerNavigate.toConnectAndSignBitcoinTransactionStep(
psbt,
getSigningConfig(inputsToSign),
location
);
return listenForBitcoinTxLedgerSigning(bytesToHex(psbt));
},
async software() {
Expand Down
Loading