From d902160c51e02a61ca9521e324cdbb3afc82171e Mon Sep 17 00:00:00 2001 From: "Justin R. Evans" Date: Fri, 15 Nov 2024 09:13:45 -0500 Subject: [PATCH 1/3] feat: prompt user for shielded keys --- apps/extension/src/Setup/Ledger/LedgerConnect.tsx | 6 ++++++ packages/sdk/src/ledger.ts | 12 +++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/apps/extension/src/Setup/Ledger/LedgerConnect.tsx b/apps/extension/src/Setup/Ledger/LedgerConnect.tsx index 538984b8e..6c88eb1b2 100644 --- a/apps/extension/src/Setup/Ledger/LedgerConnect.tsx +++ b/apps/extension/src/Setup/Ledger/LedgerConnect.tsx @@ -36,6 +36,12 @@ export const LedgerConnect: React.FC = ({ path, setPath }) => { const { address, publicKey } = await ledger.showAddressAndPublicKey( makeBip44Path(chains.namada.bip44.coinType, path) ); + + const response = await ledger.getShieldedKeys( + makeBip44Path(chains.namada.bip44.coinType, path), + true + ); + console.log({ shieldedResponse: response }); setIsLedgerConnecting(false); navigate(routes.ledgerImport(), { state: { diff --git a/packages/sdk/src/ledger.ts b/packages/sdk/src/ledger.ts index 030b6be31..b52747b14 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 TransportHID from "@ledgerhq/hw-transport-webhid"; import TransportUSB from "@ledgerhq/hw-transport-webusb"; @@ -162,6 +163,7 @@ export class Ledger { const { viewKey, ivk, ovk }: ResponseViewKey = await this.namadaApp.retrieveKeys(path, NamadaKeys.ViewKey, promptUser); + // NOTE: If promptUser is false, the proof generation keys will not be defined const { ak, nsk }: ResponseProofGenKey = await this.namadaApp.retrieveKeys( path, @@ -171,13 +173,13 @@ export class Ledger { return { viewingKey: { - viewKey: viewKey?.toString(), - ivk: ivk?.toString(), - ovk: ovk?.toString(), + viewKey: viewKey ? toHex(viewKey) : undefined, + ivk: ivk ? toHex(ivk) : undefined, + ovk: ovk ? toHex(ovk) : undefined, }, proofGenerationKey: { - ak: ak?.toString(), - nsk: nsk?.toString(), + ak: ak ? toHex(ak) : undefined, + nsk: nsk ? toHex(nsk) : undefined, }, }; } catch (e) { From 5fb65566947ff8f5e1314671733c9254cda97e29 Mon Sep 17 00:00:00 2001 From: "Justin R. Evans" Date: Tue, 19 Nov 2024 06:01:10 -0500 Subject: [PATCH 2/3] fix: clean up --- packages/shared/lib/src/types/masp.rs | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/packages/shared/lib/src/types/masp.rs b/packages/shared/lib/src/types/masp.rs index 3274b0305..2be675e03 100644 --- a/packages/shared/lib/src/types/masp.rs +++ b/packages/shared/lib/src/types/masp.rs @@ -1,8 +1,11 @@ //! PaymentAddress - Provide wasm_bindgen bindings for shielded addresses //! See @namada/crypto for zip32 HD wallet functionality. use namada_sdk::borsh::BorshDeserialize; -use namada_sdk::{ExtendedViewingKey as NamadaExtendedViewingKey, ExtendedSpendingKey as NamadaExtendedSpendingKey, PaymentAddress as NamadaPaymentAddress}; use namada_sdk::masp_primitives::{sapling, zip32}; +use namada_sdk::{ + ExtendedSpendingKey as NamadaExtendedSpendingKey, + ExtendedViewingKey as NamadaExtendedViewingKey, PaymentAddress as NamadaPaymentAddress, +}; use thiserror::Error; use wasm_bindgen::prelude::*; @@ -23,13 +26,13 @@ pub struct ExtendedViewingKey(pub(crate) NamadaExtendedViewingKey); impl ExtendedViewingKey { /// Instantiate ExtendedViewingKey from serialized vector #[wasm_bindgen(constructor)] - pub fn new(key: &[u8]) -> Result { - let xfvk: zip32::ExtendedFullViewingKey = BorshDeserialize::try_from_slice(key) + pub fn new(xfvk_bytes: &[u8]) -> Result { + let xfvk: zip32::ExtendedFullViewingKey = BorshDeserialize::try_from_slice(xfvk_bytes) .map_err(|err| format!("{}: {:?}", MaspError::BorshDeserialize, err))?; - let vk = NamadaExtendedViewingKey::from(xfvk); + let xvk = NamadaExtendedViewingKey::from(xfvk); - Ok(ExtendedViewingKey(vk)) + Ok(ExtendedViewingKey(xvk)) } /// Return ExtendedViewingKey as Bech32-encoded String @@ -47,8 +50,8 @@ pub struct ExtendedSpendingKey(pub(crate) NamadaExtendedSpendingKey); impl ExtendedSpendingKey { /// Instantiate ExtendedSpendingKey from serialized vector #[wasm_bindgen(constructor)] - pub fn new(key: &[u8]) -> Result { - let xsk: zip32::ExtendedSpendingKey = BorshDeserialize::try_from_slice(key) + pub fn new(xsk_bytes: &[u8]) -> Result { + let xsk: zip32::ExtendedSpendingKey = BorshDeserialize::try_from_slice(xsk_bytes) .map_err(|err| format!("{}: {:?}", MaspError::BorshDeserialize, err))?; let xsk = NamadaExtendedSpendingKey::from(xsk); From ca7d1b02155f6f8ed7d59dbfb975aa57b722ea75 Mon Sep 17 00:00:00 2001 From: "Justin R. Evans" Date: Wed, 20 Nov 2024 10:03:13 -0500 Subject: [PATCH 3/3] feat: additional review prompts --- .../src/Setup/Ledger/LedgerConnect.tsx | 44 ++++++++----- packages/sdk/src/index.ts | 2 +- packages/sdk/src/ledger.ts | 65 +++++++++++-------- 3 files changed, 68 insertions(+), 43 deletions(-) diff --git a/apps/extension/src/Setup/Ledger/LedgerConnect.tsx b/apps/extension/src/Setup/Ledger/LedgerConnect.tsx index 6c88eb1b2..03c4fbbe3 100644 --- a/apps/extension/src/Setup/Ledger/LedgerConnect.tsx +++ b/apps/extension/src/Setup/Ledger/LedgerConnect.tsx @@ -15,10 +15,16 @@ type Props = { setPath: (path: Bip44Path) => void; }; +enum LedgerReviewPrompt { + AddressAndPublicKey = "address and public key", + ViewingKey = "viewing key", + ProofGenerationKeys = "proof generation keys", +} + export const LedgerConnect: React.FC = ({ path, setPath }) => { const navigate = useNavigate(); const [error, setError] = useState(); - const [isLedgerConnecting, setIsLedgerConnecting] = useState(false); + const [reviewPrompt, setReviewPrompt] = useState(); const [ledger, setLedger] = useState(); const queryLedger = async (ledger: LedgerApp): Promise => { @@ -32,17 +38,23 @@ export const LedgerConnect: React.FC = ({ path, setPath }) => { throw new Error(errorMessage); } - setIsLedgerConnecting(true); - const { address, publicKey } = await ledger.showAddressAndPublicKey( - makeBip44Path(chains.namada.bip44.coinType, path) - ); - - const response = await ledger.getShieldedKeys( - makeBip44Path(chains.namada.bip44.coinType, path), - true - ); - console.log({ shieldedResponse: response }); - setIsLedgerConnecting(false); + const bip44Path = makeBip44Path(chains.namada.bip44.coinType, path); + + setReviewPrompt(LedgerReviewPrompt.AddressAndPublicKey); + const { address, publicKey } = + await ledger.showAddressAndPublicKey(bip44Path); + + setReviewPrompt(LedgerReviewPrompt.ViewingKey); + const viewingKey = await ledger.getViewingKey(bip44Path, true); + console.log({ viewingKey }); + + setReviewPrompt(LedgerReviewPrompt.ProofGenerationKeys); + const proofGenerationKeys = + await ledger.getProofGenerationKeys(bip44Path); + + console.log({ proofGenerationKeys }); + setReviewPrompt(undefined); + navigate(routes.ledgerImport(), { state: { address, @@ -50,7 +62,7 @@ export const LedgerConnect: React.FC = ({ path, setPath }) => { }, }); } catch (e) { - setIsLedgerConnecting(false); + setReviewPrompt(undefined); handleError(e); } finally { await ledger.closeTransport(); @@ -100,8 +112,8 @@ export const LedgerConnect: React.FC = ({ path, setPath }) => { )} - {isLedgerConnecting && ( - Review on your Ledger + {reviewPrompt && ( + Review {reviewPrompt} on your Ledger )} = ({ path, setPath }) => { active={!!ledger} complete={false} onClick={() => connectNamadaApp()} - buttonDisabled={!ledger || isLedgerConnecting} + buttonDisabled={!ledger || Boolean(reviewPrompt)} image={ } diff --git a/packages/sdk/src/index.ts b/packages/sdk/src/index.ts index dd9b9c5a6..188dc09b8 100644 --- a/packages/sdk/src/index.ts +++ b/packages/sdk/src/index.ts @@ -6,8 +6,8 @@ export { } from "./ledger"; export type { LedgerAddressAndPublicKey, - LedgerShieldedKeys, LedgerStatus, + LedgerViewingKey, } from "./ledger"; // Export types diff --git a/packages/sdk/src/ledger.ts b/packages/sdk/src/ledger.ts index b52747b14..820132824 100644 --- a/packages/sdk/src/ledger.ts +++ b/packages/sdk/src/ledger.ts @@ -18,16 +18,15 @@ import { makeBip44Path } from "./utils"; const { coinType } = chains.namada.bip44; export type LedgerAddressAndPublicKey = { address: string; publicKey: string }; -export type LedgerShieldedKeys = { - viewingKey: { - viewKey?: string; - ivk?: string; - ovk?: string; - }; - proofGenerationKey: { - ak?: string; - nsk?: string; - }; +// TODO: This should be xfvk, awaiting an updated version! +export type LedgerViewingKey = { + viewKey?: string; + ivk?: string; + ovk?: string; +}; +export type LedgerProofGenerationKey = { + ak?: string; + nsk?: string; }; export type LedgerStatus = { @@ -148,42 +147,56 @@ 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 [promptUser] boolean to determine whether to display on Ledger device and require approval - * @returns ShieldedKeys + * @returns LedgerViewingKey */ - public async getShieldedKeys( + public async getViewingKey( path: string = DEFAULT_LEDGER_BIP44_PATH, promptUser = true - ): Promise { + ): Promise { try { const { viewKey, ivk, ovk }: ResponseViewKey = await this.namadaApp.retrieveKeys(path, NamadaKeys.ViewKey, promptUser); - // NOTE: If promptUser is false, the proof generation keys will not be defined + return { + viewKey: viewKey ? toHex(new Uint8Array(viewKey)) : undefined, + ivk: ivk ? toHex(new Uint8Array(ivk)) : undefined, + ovk: ovk ? toHex(new Uint8Array(ovk)) : undefined, + }; + } catch (e) { + throw new Error(`Could not retrieve Viewing Key: ${e}`); + } + } + + /** + * Prompt user to get proof generation keys associated with optional path, otherwise, use default path. + * Throw exception if app is not initialized. + * @async + * @param [path] Bip44 path for deriving key + * @returns LedgerProofGenerationKey + */ + public async getProofGenerationKeys( + path: string = DEFAULT_LEDGER_BIP44_PATH + ): Promise { + try { const { ak, nsk }: ResponseProofGenKey = await this.namadaApp.retrieveKeys( path, NamadaKeys.ProofGenerationKey, - promptUser + // NOTE: Setting this to false will result in undefined values + true ); return { - viewingKey: { - viewKey: viewKey ? toHex(viewKey) : undefined, - ivk: ivk ? toHex(ivk) : undefined, - ovk: ovk ? toHex(ovk) : undefined, - }, - proofGenerationKey: { - ak: ak ? toHex(ak) : undefined, - nsk: nsk ? toHex(nsk) : undefined, - }, + ak: ak ? toHex(new Uint8Array(ak)) : undefined, + nsk: nsk ? toHex(new Uint8Array(nsk)) : undefined, }; } catch (e) { - throw new Error(`Could not retrieve Viewing Key`); + throw new Error(`Could not retrive Proof Generation Keys: ${e}`); } }