Skip to content

Commit

Permalink
Add Ledger approval/rejection handling (anoma#461)
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans authored Nov 17, 2023
1 parent ed1c31e commit faf638b
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
} from "@namada/components";
import { LedgerError } from "@namada/ledger-namada";
import { formatRouterPath } from "@namada/utils";
import { initLedgerHIDTransport, Ledger as LedgerApp } from "background/ledger";
import { Ledger as LedgerApp } from "background/ledger";
import { LedgerConnectRoute, TopLevelRoute } from "Setup/types";
import {
ButtonContainer,
Expand All @@ -24,9 +24,11 @@ import {
export const LedgerConnect: React.FC = () => {
const navigate = useNavigate();
const [error, setError] = useState<string>();
const [isLedgerConnecting, setIsLedgerConnecting] = useState(false);
const [ledger, setLedger] = useState<LedgerApp>();

const queryLedger = async (ledger: LedgerApp): Promise<void> => {
setError(undefined);
try {
const {
version: { errorMessage, returnCode },
Expand All @@ -36,7 +38,11 @@ export const LedgerConnect: React.FC = () => {
throw new Error(errorMessage);
}

const { address, publicKey } = await ledger.getAddressAndPublicKey();
setIsLedgerConnecting(true);

const { address, publicKey } = await ledger.showAddressAndPublicKey();

setIsLedgerConnecting(false);

navigate(
formatRouterPath([TopLevelRoute.Ledger, LedgerConnectRoute.Import]),
Expand All @@ -48,6 +54,7 @@ export const LedgerConnect: React.FC = () => {
}
);
} catch (e) {
setIsLedgerConnecting(false);
handleError(e);
} finally {
await ledger.closeTransport();
Expand Down Expand Up @@ -84,19 +91,6 @@ export const LedgerConnect: React.FC = () => {
setError(`${e}`);
};

/**
* Connect using HID transport
*/
const _handleConnectHID = async (): Promise<void> => {
const transport = await initLedgerHIDTransport();
try {
const ledger = await LedgerApp.init(transport);
queryLedger(ledger);
} catch (e) {
setError(`${e}`);
}
};

return (
<>
<Stack gap={12}>
Expand All @@ -110,6 +104,9 @@ export const LedgerConnect: React.FC = () => {
{error}
</Alert>
)}
{isLedgerConnecting && (
<Alert type="warning">Review on your Ledger</Alert>
)}
<LedgerListItem active={!ledger} complete={!!ledger}>
<LedgerIcon>
<Image
Expand Down Expand Up @@ -148,7 +145,7 @@ export const LedgerConnect: React.FC = () => {
<Text>Open the Namada App on your ledger device</Text>
<ButtonContainer>
<ActionButton
disabled={!ledger}
disabled={!ledger || isLedgerConnecting}
size="xs"
onClick={() => connectNamadaApp()}
>
Expand Down
24 changes: 24 additions & 0 deletions apps/extension/src/background/ledger/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,30 @@ export class Ledger {
};
}

public async showAddressAndPublicKey(
bip44Path?: string
): Promise<{ address: string; publicKey: string }> {
if (!this.namadaApp) {
throw new Error("NamadaApp is not initialized!");
}

const path = bip44Path || DEFAULT_LEDGER_BIP44_PATH;

try {
const { address, publicKey } = await this.namadaApp.showAddressAndPubKey(
path
);

return {
// Return address as bech32-encoded string
address: address.toString(),
// Return public key as hex-encoded string
publicKey: toHex(publicKey),
};
} catch (e) {
throw new Error("Connect Ledger rejected by user");
}
}
/**
* Sign tx bytes with the key associated with provided (or default) path
*
Expand Down

0 comments on commit faf638b

Please sign in to comment.