Skip to content

Commit

Permalink
feat: split proof-gen and viewing key calls, fix paths
Browse files Browse the repository at this point in the history
  • Loading branch information
jurevans committed Jan 7, 2025
1 parent 2af23fe commit 775c400
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 27 deletions.
13 changes: 9 additions & 4 deletions apps/extension/src/Setup/Ledger/LedgerConnect.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,15 @@ export const LedgerConnect: React.FC<Props> = ({ 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: {
Expand Down
3 changes: 2 additions & 1 deletion packages/sdk/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@
export { Ledger, initLedgerUSBTransport } from "./ledger";
export type {
LedgerAddressAndPublicKey,
LedgerShieldedKeys,
LedgerProofGenerationKey,
LedgerStatus,
LedgerViewingKey,
} from "./ledger";

// Export types
Expand Down
63 changes: 41 additions & 22 deletions packages/sdk/src/ledger.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -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 = {
Expand All @@ -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
*/
Expand Down Expand Up @@ -135,24 +138,45 @@ 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<LedgerShieldedKeys> {
): Promise<LedgerViewingKey> {
try {
const { xfvk }: ResponseViewKey = await this.namadaApp.retrieveKeys(
path,
NamadaKeys.ViewKey,
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<LedgerProofGenerationKey> {
try {
const { ak, nsk }: ResponseProofGenKey =
await this.namadaApp.retrieveKeys(
path,
Expand All @@ -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`);
}
}

Expand Down

0 comments on commit 775c400

Please sign in to comment.