From 02748385f2cd09b3e84e7a7646562b5ee26017ae Mon Sep 17 00:00:00 2001 From: "Justin R. Evans" Date: Thu, 1 Jun 2023 05:01:33 -0400 Subject: [PATCH] touch up ledger class --- apps/extension/src/Setup/Ledger/Ledger.tsx | 4 +- .../extension/src/background/ledger/ledger.ts | 69 ++++++++----------- .../src/background/ledger/service.ts | 39 +++++++++-- 3 files changed, 66 insertions(+), 46 deletions(-) diff --git a/apps/extension/src/Setup/Ledger/Ledger.tsx b/apps/extension/src/Setup/Ledger/Ledger.tsx index df31345289..c925ca6a36 100644 --- a/apps/extension/src/Setup/Ledger/Ledger.tsx +++ b/apps/extension/src/Setup/Ledger/Ledger.tsx @@ -44,7 +44,9 @@ const Ledger: React.FC = ({ requester: _ }) => { const queryLedger = async (ledger: LedgerApp): Promise => { const pk = await ledger.getPublicKey(); - const { appName, appVersion } = ledger.status(); + const { + info: { appName, appVersion }, + } = await ledger.status(); setAppInfo({ name: appName, version: appVersion }); setPublicKey(pk); diff --git a/apps/extension/src/background/ledger/ledger.ts b/apps/extension/src/background/ledger/ledger.ts index 087cd65926..84fa1cd2de 100644 --- a/apps/extension/src/background/ledger/ledger.ts +++ b/apps/extension/src/background/ledger/ledger.ts @@ -24,9 +24,6 @@ export const initLedgerHIDTransport = async (): Promise => { export const DEFAULT_LEDGER_BIP44_PATH = `m/44'/${bip44CoinType}'/0'/0/0`; export class Ledger { - public version: ResponseVersion | undefined; - public info: ResponseAppInfo | undefined; - constructor(public readonly namadaApp: NamadaApp | undefined = undefined) {} /** @@ -38,52 +35,25 @@ export class Ledger { const namadaApp = new NamadaApp(initializedTransport); const ledger = new Ledger(namadaApp); - ledger.version = await namadaApp.getVersion(); - ledger.info = await namadaApp.getAppInfo(); - return ledger; } /** * Return status and version info of initialized NamadaApp */ - public status(): { - appName: string; - appVersion: string; - errorMessage: string; - returnCode: number; - deviceLocked: boolean; - major: number; - minor: number; - patch: number; - targetId: string; - testMode: boolean; - } { - if (!this.version || !this.info) { + public async status(): Promise<{ + version: ResponseVersion; + info: ResponseAppInfo; + }> { + if (!this.namadaApp) { throw new Error("NamadaApp is not initialized!"); } - - const { appName, appVersion, errorMessage, returnCode } = this.info; - const { - major, - minor, - patch, - targetId, - deviceLocked = false, - testMode, - } = this.version; + const version = await this.namadaApp.getVersion(); + const info = await this.namadaApp.getAppInfo(); return { - appName, - appVersion, - errorMessage, - returnCode, - deviceLocked, - major, - minor, - patch, - targetId, - testMode, + version, + info, }; } @@ -120,8 +90,17 @@ export class Ledger { /** * Sign tx bytes with the key associated with provided (or default) path + * + * @async + * @param {ArrayBuffer} tx - tx data blob to sign + * @param {string} bip44Path (optional) - Bip44 path for signing account + * + * @returns {ResponseSign} */ - public async sign(tx: ArrayBuffer, bip44Path: string): Promise { + public async sign( + tx: ArrayBuffer, + bip44Path?: string + ): Promise { if (!this.namadaApp) { throw new Error("NamadaApp is not initialized!"); } @@ -130,4 +109,14 @@ export class Ledger { return await this.namadaApp.sign(path, Buffer.from(tx)); } + + /** + * Query status to determine if device is locked + */ + public async isLocked(): Promise { + const { + version: { deviceLocked }, + } = await this.status(); + return deviceLocked; + } } diff --git a/apps/extension/src/background/ledger/service.ts b/apps/extension/src/background/ledger/service.ts index 686c1ee0b8..6358378225 100644 --- a/apps/extension/src/background/ledger/service.ts +++ b/apps/extension/src/background/ledger/service.ts @@ -39,10 +39,8 @@ export class LedgerService { async submitTransfer( txMsg: string, address: string, - txId: string + msgId: string ): Promise { - const msgId = txId; - const tabs = await syncTabs( this.connectedTabsStore, this.requester, @@ -66,10 +64,41 @@ export class LedgerService { txMsgBuffer ); - // TODO: We need actual tx data to sign: + // TODO: We need actual tx data to sign. txMsg contains all transfer data we need to construct + // a tx, however, this needs to be constructed properly for Ledger to parse and sign (e.g., + // header, data, code): const txData = new Uint8Array(); const signature = await ledger.sign(txData, path); - console.log("TODO:", { signature, tabs, msgId, txDetails }); + + console.log("TODO:", { + msgId, + txDetails, + tabs, + }); + + const { headerSignature, dataSignature, codeSignature } = signature; + // TODO: Testing signatures + console.log( + "header", + headerSignature.hash, + headerSignature.salt, + headerSignature.pubkey, + headerSignature.signature + ); + console.log( + "data", + dataSignature.hash, + dataSignature.salt, + dataSignature.pubkey, + dataSignature.signature + ); + console.log( + "code", + codeSignature.hash, + codeSignature.salt, + codeSignature.pubkey, + codeSignature.signature + ); return; }