diff --git a/apps/extension/src/Setup/Ledger/LedgerConnect.tsx b/apps/extension/src/Setup/Ledger/LedgerConnect.tsx index 012285c0b..b765afb17 100644 --- a/apps/extension/src/Setup/Ledger/LedgerConnect.tsx +++ b/apps/extension/src/Setup/Ledger/LedgerConnect.tsx @@ -40,10 +40,15 @@ export const LedgerConnect: React.FC = ({ path, setPath }) => { const { address, publicKey } = await ledger.showAddressAndPublicKey( makeBip44Path(chains.namada.bip44.coinType, path) ); - const { viewingKey, proofGenerationKey } = await ledger.getShieldedKeys( - makeSaplingPath(chains.namada.bip44.coinType, path) - ); - console.log("TODO", { viewingKey, proofGenerationKey }); + + // Shielded Keys + const zip32Path = makeSaplingPath(chains.namada.bip44.coinType, { + account: path.account, + }); + const { xfvk } = await ledger.getViewingKey(zip32Path); + const { ak, nsk } = await ledger.getProofGenerationKey(zip32Path); + + console.log("TODO", { zip32Path, xfvk, ak, nsk }); setIsLedgerConnecting(false); navigate(routes.ledgerImport(), { state: { diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index 27e28c38b..7360d56c9 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -2,8 +2,9 @@ export { Ledger, initLedgerUSBTransport } from "./ledger"; export type { LedgerAddressAndPublicKey, - LedgerShieldedKeys, + LedgerProofGenerationKey, LedgerStatus, + LedgerViewingKey, } from "./ledger"; // Export types diff --git a/packages/sdk/src/ledger.ts b/packages/sdk/src/ledger.ts index f9100a59b..32aeed63c 100644 --- a/packages/sdk/src/ledger.ts +++ b/packages/sdk/src/ledger.ts @@ -1,3 +1,4 @@ +import { toHex } from "@cosmjs/encoding"; import Transport from "@ledgerhq/hw-transport"; import TransportUSB from "@ledgerhq/hw-transport-webusb"; import { chains } from "@namada/chains"; @@ -11,19 +12,17 @@ import { ResponseVersion, ResponseViewKey, } from "@zondax/ledger-namada"; -import { makeBip44Path } from "./utils"; +import { makeBip44Path, makeSaplingPath } from "./utils"; const { coinType } = chains.namada.bip44; export type LedgerAddressAndPublicKey = { address: string; publicKey: string }; -export type LedgerShieldedKeys = { - viewingKey: { - xfvk?: string; - }; - proofGenerationKey: { - ak?: string; - nsk?: string; - }; +export type LedgerViewingKey = { + xfvk?: string; +}; +export type LedgerProofGenerationKey = { + ak?: string; + nsk?: string; }; export type LedgerStatus = { @@ -46,6 +45,10 @@ export const DEFAULT_LEDGER_BIP44_PATH = makeBip44Path(coinType, { index: 0, }); +export const DEFAULT_LEDGER_ZIP32_PATH = makeSaplingPath(coinType, { + account: 0, +}); + /** * Functionality for interacting with NamadaApp for Ledger Hardware Wallets */ @@ -135,17 +138,17 @@ export class Ledger { } /** - * Prompt user to get viewing and proof gen key associated with optional path, otherwise, use default path. + * Prompt user to get viewing key associated with optional path, otherwise, use default path. * Throw exception if app is not initialized. * @async - * @param [path] Bip44 path for deriving key + * @param [path] Zip32 path for deriving key * @param [promptUser] boolean to determine whether to display on Ledger device and require approval * @returns ShieldedKeys */ - public async getShieldedKeys( - path: string = DEFAULT_LEDGER_BIP44_PATH, + public async getViewingKey( + path: string = DEFAULT_LEDGER_ZIP32_PATH, promptUser = true - ): Promise { + ): Promise { try { const { xfvk }: ResponseViewKey = await this.namadaApp.retrieveKeys( path, @@ -153,6 +156,27 @@ export class Ledger { promptUser ); + return { + xfvk: xfvk ? toHex(xfvk) : undefined, + }; + } catch (_) { + throw new Error(`Could not derive viewing key`); + } + } + + /** + * Prompt user to get proof generation key associated with optional path, otherwise, use default path. + * Throw exception if app is not initialized. + * @async + * @param [path] Zip32 path for deriving key + * @param [promptUser] boolean to determine whether to display on Ledger device and require approval + * @returns ShieldedKeys + */ + public async getProofGenerationKey( + path: string = DEFAULT_LEDGER_ZIP32_PATH, + promptUser = true + ): Promise { + try { const { ak, nsk }: ResponseProofGenKey = await this.namadaApp.retrieveKeys( path, @@ -161,16 +185,11 @@ export class Ledger { ); return { - viewingKey: { - xfvk: xfvk?.toString(), - }, - proofGenerationKey: { - ak: ak?.toString(), - nsk: nsk?.toString(), - }, + ak: ak ? toHex(ak) : undefined, + nsk: nsk ? toHex(nsk) : undefined, }; } catch (_) { - throw new Error(`Could not retrieve Viewing Key`); + throw new Error(`Could not derive proof generation key`); } }