From ab972b73afd7befad186d51df795d7cee77b2c7d Mon Sep 17 00:00:00 2001 From: Mateusz Jasiuk Date: Mon, 30 Dec 2024 15:32:32 +0100 Subject: [PATCH] wip wiop iwp --- apps/extension/package.json | 2 +- .../src/Approvals/ConfirmSignLedgerTx.tsx | 4 +- .../src/Setup/Ledger/LedgerConnect.tsx | 7 +- packages/sdk/package.json | 2 +- packages/sdk/src/ledger.ts | 65 +++++++++++-------- packages/sdk/src/sdk.ts | 4 +- packages/sdk/src/utils.ts | 5 +- packages/shared/lib/src/types/masp.rs | 7 ++ packages/types/src/account.ts | 1 + yarn.lock | 12 ++-- 10 files changed, 69 insertions(+), 40 deletions(-) diff --git a/apps/extension/package.json b/apps/extension/package.json index 87f3ee5267..5aa07bea48 100644 --- a/apps/extension/package.json +++ b/apps/extension/package.json @@ -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", diff --git a/apps/extension/src/Approvals/ConfirmSignLedgerTx.tsx b/apps/extension/src/Approvals/ConfirmSignLedgerTx.tsx index ff7152d309..447bc7a322 100644 --- a/apps/extension/src/Approvals/ConfirmSignLedgerTx.tsx +++ b/apps/extension/src/Approvals/ConfirmSignLedgerTx.tsx @@ -1,3 +1,4 @@ +import sdkInit from "@namada/sdk/web-init"; import clsx from "clsx"; import { ReactNode, useCallback, useEffect, useState } from "react"; @@ -97,7 +98,8 @@ export const ConfirmSignLedgerTx: React.FC = ({ 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); }); diff --git a/apps/extension/src/Setup/Ledger/LedgerConnect.tsx b/apps/extension/src/Setup/Ledger/LedgerConnect.tsx index 8dd0d176a4..387038e70f 100644 --- a/apps/extension/src/Setup/Ledger/LedgerConnect.tsx +++ b/apps/extension/src/Setup/Ledger/LedgerConnect.tsx @@ -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"; @@ -33,6 +34,9 @@ export const LedgerConnect: React.FC = ({ path, setPath }) => { } setIsLedgerConnecting(true); + + const asd = await ledger.getShieldedKeys(); + console.log(asd); const { address, publicKey } = await ledger.showAddressAndPublicKey( makeBip44Path(chains.namada.bip44.coinType, path) ); @@ -53,7 +57,8 @@ export const LedgerConnect: React.FC = ({ path, setPath }) => { const connectUSB = async (): Promise => { try { - const ledger = await LedgerApp.init(); + const { cryptoMemory } = await sdkInit(); + const ledger = await LedgerApp.init(cryptoMemory); setLedger(ledger); } catch (e) { handleError(e); diff --git a/packages/sdk/package.json b/packages/sdk/package.json index 12219bdc43..3851aa5ded 100644 --- a/packages/sdk/package.json +++ b/packages/sdk/package.json @@ -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" diff --git a/packages/sdk/src/ledger.ts b/packages/sdk/src/ledger.ts index e4ad28ee40..f334b77a17 100644 --- a/packages/sdk/src/ledger.ts +++ b/packages/sdk/src/ledger.ts @@ -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; @@ -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 { + static async init( + cryptoMemory: WebAssembly.Memory, + transport?: Transport + ): Promise { 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}`); @@ -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 { 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`); } } diff --git a/packages/sdk/src/sdk.ts b/packages/sdk/src/sdk.ts index fc61a9089b..59d40a953c 100644 --- a/packages/sdk/src/sdk.ts +++ b/packages/sdk/src/sdk.ts @@ -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 @@ -111,7 +111,7 @@ export class Sdk { * @returns Class for interacting with NamadaApp for Ledger Hardware Wallets */ async initLedger(transport?: Transport): Promise { - return await Ledger.init(transport); + return await Ledger.init(this.cryptoMemory, transport); } /** diff --git a/packages/sdk/src/utils.ts b/packages/sdk/src/utils.ts index bf3758ee52..a28aeadb13 100644 --- a/packages/sdk/src/utils.ts +++ b/packages/sdk/src/utils.ts @@ -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; }; diff --git a/packages/shared/lib/src/types/masp.rs b/packages/shared/lib/src/types/masp.rs index d353979798..ec55fa7758 100644 --- a/packages/shared/lib/src/types/masp.rs +++ b/packages/shared/lib/src/types/masp.rs @@ -54,6 +54,13 @@ pub struct ProofGenerationKey(pub(crate) sapling::ProofGenerationKey); #[wasm_bindgen] impl ProofGenerationKey { + pub fn from_bytes(ak: Vec, nsk: Vec) -> ProofGenerationKey { + let concatenated: Vec = 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!"), diff --git a/packages/types/src/account.ts b/packages/types/src/account.ts index f5f5a9ee53..48993c11ab 100644 --- a/packages/types/src/account.ts +++ b/packages/types/src/account.ts @@ -6,6 +6,7 @@ export type Bip44Path = { export type Zip32Path = { account: number; + change?: number; index?: number; }; diff --git a/yarn.lock b/yarn.lock index cbe90c8c7d..dc3755d999 100644 --- a/yarn.lock +++ b/yarn.lock @@ -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" @@ -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" @@ -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