Skip to content

Commit

Permalink
wip wiop iwp
Browse files Browse the repository at this point in the history
  • Loading branch information
mateuszjasiuk committed Dec 30, 2024
1 parent 753529f commit ab972b7
Show file tree
Hide file tree
Showing 10 changed files with 69 additions and 40 deletions.
2 changes: 1 addition & 1 deletion apps/extension/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
"@ledgerhq/hw-transport": "^6.31.4",
"@ledgerhq/hw-transport-webhid": "^6.29.4",
"@ledgerhq/hw-transport-webusb": "^6.29.4",
"@zondax/ledger-namada": "^1.0.0",
"@zondax/ledger-namada": "^2.0.0",
"bignumber.js": "^9.1.1",
"buffer": "^6.0.3",
"dompurify": "^3.0.2",
Expand Down
4 changes: 3 additions & 1 deletion apps/extension/src/Approvals/ConfirmSignLedgerTx.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import sdkInit from "@namada/sdk/web-init";
import clsx from "clsx";
import { ReactNode, useCallback, useEffect, useState } from "react";

Expand Down Expand Up @@ -97,7 +98,8 @@ export const ConfirmSignLedgerTx: React.FC<Props> = ({ details }) => {
setStatusInfo("Connecting Ledger...");
setStatus(Status.Pending);

const ledger = await Ledger.init().catch((e) => {
const { cryptoMemory } = await sdkInit();
const ledger = await Ledger.init(cryptoMemory).catch((e) => {
setError(`${e}`);
setStatus(Status.Failed);
});
Expand Down
7 changes: 6 additions & 1 deletion apps/extension/src/Setup/Ledger/LedgerConnect.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { chains } from "@namada/chains";
import { ActionButton, Alert, Image, Stack } from "@namada/components";
import { Ledger as LedgerApp, makeBip44Path } from "@namada/sdk/web";
import sdkInit from "@namada/sdk/web-init";
import { Bip44Path } from "@namada/types";
import { LedgerError } from "@zondax/ledger-namada";
import { LedgerStep } from "Setup/Common";
Expand Down Expand Up @@ -33,6 +34,9 @@ export const LedgerConnect: React.FC<Props> = ({ path, setPath }) => {
}

setIsLedgerConnecting(true);

const asd = await ledger.getShieldedKeys();
console.log(asd);
const { address, publicKey } = await ledger.showAddressAndPublicKey(
makeBip44Path(chains.namada.bip44.coinType, path)
);
Expand All @@ -53,7 +57,8 @@ export const LedgerConnect: React.FC<Props> = ({ path, setPath }) => {

const connectUSB = async (): Promise<void> => {
try {
const ledger = await LedgerApp.init();
const { cryptoMemory } = await sdkInit();
const ledger = await LedgerApp.init(cryptoMemory);
setLedger(ledger);
} catch (e) {
handleError(e);
Expand Down
2 changes: 1 addition & 1 deletion packages/sdk/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@
"@ledgerhq/hw-transport": "^6.31.4",
"@ledgerhq/hw-transport-webhid": "^6.29.4",
"@ledgerhq/hw-transport-webusb": "^6.29.4",
"@zondax/ledger-namada": "^1.0.0",
"@zondax/ledger-namada": "^2.0.0",
"bignumber.js": "^9.1.1",
"buffer": "^6.0.3",
"slip44": "^3.0.18"
Expand Down
65 changes: 39 additions & 26 deletions packages/sdk/src/ledger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,14 @@ import {
ResponseVersion,
ResponseViewKey,
} from "@zondax/ledger-namada";
import { makeBip44Path } from "./utils";
import { ExtendedViewingKey, ProofGenerationKey } from "../../shared/src";
import { makeBip44Path, makeSaplingPath } from "./utils";

const { coinType } = chains.namada.bip44;

export type LedgerAddressAndPublicKey = { address: string; publicKey: string };
export type LedgerShieldedKeys = {
viewingKey: {
viewKey?: string;
ivk?: string;
ovk?: string;
};
xfvk?: string;
proofGenerationKey: {
ak?: string;
nsk?: string;
Expand Down Expand Up @@ -58,27 +55,41 @@ export const DEFAULT_LEDGER_BIP44_PATH = makeBip44Path(coinType, {
index: 0,
});

export const DEFAULT_LEDGER_ZIP32_PATH = makeSaplingPath(coinType, {
account: 0,
change: 0,
index: 0,
});

/**
* Functionality for interacting with NamadaApp for Ledger Hardware Wallets
*/
export class Ledger {
/**
* @param namadaApp - Inititalized NamadaApp class from Zondax package
* @param cryptoMemory - Memory accessor for crypto lib
*/
private constructor(public readonly namadaApp: NamadaApp) {}
private constructor(
public readonly namadaApp: NamadaApp,
protected readonly cryptoMemory: WebAssembly.Memory
) { }

/**
* Initialize and return Ledger class instance with initialized Transport
* @async
* @param [cryptoMemory] memory accessor for crypto lib
* @param [transport] Ledger transport
* @returns Ledger class instance
*/
static async init(transport?: Transport): Promise<Ledger> {
static async init(
cryptoMemory: WebAssembly.Memory,
transport?: Transport
): Promise<Ledger> {
const initializedTransport = transport ?? (await initLedgerUSBTransport());

try {
const namadaApp = new NamadaApp(initializedTransport);
const ledger = new Ledger(namadaApp);
const ledger = new Ledger(namadaApp, cryptoMemory);
return ledger;
} catch (e) {
throw new Error(`${e}`);
Expand Down Expand Up @@ -155,32 +166,34 @@ export class Ledger {
* @returns ShieldedKeys
*/
public async getShieldedKeys(
path: string = DEFAULT_LEDGER_BIP44_PATH,
path: string = DEFAULT_LEDGER_ZIP32_PATH,
promptUser = true
): Promise<LedgerShieldedKeys> {
try {
const { viewKey, ivk, ovk }: ResponseViewKey =
await this.namadaApp.retrieveKeys(path, NamadaKeys.ViewKey, promptUser);
const { xfvk }: ResponseViewKey = await this.namadaApp.retrieveKeys(
path,
NamadaKeys.ViewKey,
promptUser
);
const asd = new ExtendedViewingKey(xfvk!);

const www: ResponseProofGenKey = await this.namadaApp.retrieveKeys(
path,
NamadaKeys.ProofGenerationKey,
promptUser
);

const { ak, nsk }: ResponseProofGenKey =
await this.namadaApp.retrieveKeys(
path,
NamadaKeys.ProofGenerationKey,
promptUser
);
const xxx = ProofGenerationKey.from_bytes(www.ak!, www.nsk!);

return {
viewingKey: {
viewKey: viewKey?.toString(),
ivk: ivk?.toString(),
ovk: ovk?.toString(),
},
xfvk: xfvk?.toString(),
proofGenerationKey: {
ak: ak?.toString(),
nsk: nsk?.toString(),
ak: www?.ak?.toString(),
nsk: www?.nsk?.toString(),
},
};
} catch (_) {
} catch (e) {
console.error(e);
throw new Error(`Could not retrieve Viewing Key`);
}
}
Expand Down
4 changes: 2 additions & 2 deletions packages/sdk/src/sdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export class Sdk {
public readonly cryptoMemory: WebAssembly.Memory,
public readonly url: string,
public readonly nativeToken: string
) {}
) { }

/**
* Re-initialize wasm instances and return this instance
Expand Down Expand Up @@ -111,7 +111,7 @@ export class Sdk {
* @returns Class for interacting with NamadaApp for Ledger Hardware Wallets
*/
async initLedger(transport?: Transport): Promise<Ledger> {
return await Ledger.init(transport);
return await Ledger.init(this.cryptoMemory, transport);
}

/**
Expand Down
5 changes: 3 additions & 2 deletions packages/sdk/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,11 @@ export const makeSaplingPath = (
zip32Path: Zip32Path,
fullPath = true
): string => {
const { account, index } = zip32Path;
const { account, change, index } = zip32Path;
const prefix = `m/32'/${coinType}'`;
let path = `/${account}'`;
if (typeof index === "number") path += `/${index}`;
if (typeof change === "number") path += `/${change}'`;
if (typeof index === "number") path += `/${index}'`;
return fullPath ? `${prefix}${path}` : path;
};

Expand Down
7 changes: 7 additions & 0 deletions packages/shared/lib/src/types/masp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,13 @@ pub struct ProofGenerationKey(pub(crate) sapling::ProofGenerationKey);

#[wasm_bindgen]
impl ProofGenerationKey {
pub fn from_bytes(ak: Vec<u8>, nsk: Vec<u8>) -> ProofGenerationKey {
let concatenated: Vec<u8> = ak.iter().chain(nsk.iter()).cloned().collect();
let www = sapling::ProofGenerationKey::try_from_slice(concatenated.as_slice())
.expect("Deserializing ProofGenerationKey should not fail!");

ProofGenerationKey(www)
}
pub fn encode(&self) -> String {
hex::encode(
borsh::to_vec(&self.0).expect("Serializing ProofGenerationKey should not fail!"),
Expand Down
1 change: 1 addition & 0 deletions packages/types/src/account.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export type Bip44Path = {

export type Zip32Path = {
account: number;
change?: number;
index?: number;
};

Expand Down
12 changes: 6 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3487,7 +3487,7 @@ __metadata:
"@types/w3c-web-usb": "npm:^1.0.10"
"@types/webextension-polyfill": "npm:^0.10.6"
"@types/zxcvbn": "npm:^4.4.1"
"@zondax/ledger-namada": "npm:^1.0.0"
"@zondax/ledger-namada": "npm:^2.0.0"
bignumber.js: "npm:^9.1.1"
buffer: "npm:^6.0.3"
copy-webpack-plugin: "npm:^11.0.0"
Expand Down Expand Up @@ -3757,7 +3757,7 @@ __metadata:
"@release-it/conventional-changelog": "npm:^8.0.1"
"@types/jest": "npm:^29.5.12"
"@types/node": "npm:^20.11.4"
"@zondax/ledger-namada": "npm:^1.0.0"
"@zondax/ledger-namada": "npm:^2.0.0"
babel-jest: "npm:^29.0.3"
bignumber.js: "npm:^9.1.1"
buffer: "npm:^6.0.3"
Expand Down Expand Up @@ -6065,12 +6065,12 @@ __metadata:
languageName: node
linkType: hard

"@zondax/ledger-namada@npm:^1.0.0":
version: 1.0.0
resolution: "@zondax/ledger-namada@npm:1.0.0"
"@zondax/ledger-namada@npm:^2.0.0":
version: 2.0.0
resolution: "@zondax/ledger-namada@npm:2.0.0"
dependencies:
"@ledgerhq/hw-transport": "npm:^6.30.6"
checksum: f7490964ccd41f9a63f2bc8d89ea9f01e7d6a58be796db0b454a3f1455dcc8dc8dd578a8642aa0f0799c93f838e8e3afd69f59f7e9947816b9471338c2b9dd63
checksum: 1fa2a9a537bc42df01444332529a606ed77f608a2cc1dbb029915ed854ff447976930a4338c2d68d50d98869828cd76b1a4f4b5c2c989fd84af7b66d55dc51fc
languageName: node
linkType: hard

Expand Down

0 comments on commit ab972b7

Please sign in to comment.