diff --git a/packages/launchpad-ledger/src/launchpadledger.ts b/packages/launchpad-ledger/src/launchpadledger.ts index 83019811..ea2aa82d 100644 --- a/packages/launchpad-ledger/src/launchpadledger.ts +++ b/packages/launchpad-ledger/src/launchpadledger.ts @@ -135,19 +135,19 @@ export class LaunchpadLedger { return `${major}.${minor}.${patch}`; } - async getPubKey(): Promise { + async getPubkey(): Promise { await this.connect(); assert(this.cosmosApp, "Cosmos Ledger App is not connected"); // ledger-cosmos-js hardens the first three indices const response = await this.cosmosApp.publicKey(unharden(this.hdPath)); this.handleLedgerErrors(response); - return (response as PublicKeyResponse).compressed_pk; + return Uint8Array.from((response as PublicKeyResponse).compressed_pk); } - async getCosmosAddress(): Promise { - const pubKey = await this.getPubKey(); - return CosmosApp.getBech32FromPK(this.prefix, pubKey); + async getCosmosAddress(pubkey?: Uint8Array): Promise { + const pubkeyToUse = pubkey || (await this.getPubkey()); + return CosmosApp.getBech32FromPK(this.prefix, Buffer.from(pubkeyToUse)); } // async verifyLedgerAddress(): Promise { diff --git a/packages/launchpad-ledger/src/ledgersigner.ts b/packages/launchpad-ledger/src/ledgersigner.ts index 145f1ddc..ef6d83d8 100644 --- a/packages/launchpad-ledger/src/ledgersigner.ts +++ b/packages/launchpad-ledger/src/ledgersigner.ts @@ -14,14 +14,18 @@ export class LedgerSigner implements OfflineSigner { public async getAccounts(): Promise { await this.ledger.connect(); - const address = (this.address = this.address || (await this.ledger.getCosmosAddress())); - const pubkey = (this.pubkey = this.pubkey || (await this.ledger.getPubKey())); + if (!this.pubkey) { + this.pubkey = await this.ledger.getPubkey(); + } + if (!this.address) { + this.address = await this.ledger.getCosmosAddress(this.pubkey); + } return [ { algo: "secp256k1", - address: address, - pubkey: pubkey, + address: this.address, + pubkey: this.pubkey, }, ]; } @@ -29,13 +33,18 @@ export class LedgerSigner implements OfflineSigner { public async sign(address: string, message: Uint8Array): Promise { await this.ledger.connect(); - const thisAddress = (this.address = this.address || (await this.ledger.getCosmosAddress())); - if (address !== thisAddress) { + if (!this.pubkey) { + this.pubkey = await this.ledger.getPubkey(); + } + if (!this.address) { + this.address = await this.ledger.getCosmosAddress(this.pubkey); + } + + if (address !== this.address) { throw new Error(`Address ${address} not found in wallet`); } const signature = await this.ledger.sign(message); - const pubkey = (this.pubkey = this.pubkey || (await this.ledger.getPubKey())); - return encodeSecp256k1Signature(pubkey, signature); + return encodeSecp256k1Signature(this.pubkey, signature); } } diff --git a/packages/launchpad-ledger/types/launchpadledger.d.ts b/packages/launchpad-ledger/types/launchpadledger.d.ts index 6dcaa64c..b4f01c24 100644 --- a/packages/launchpad-ledger/types/launchpadledger.d.ts +++ b/packages/launchpad-ledger/types/launchpadledger.d.ts @@ -1,4 +1,3 @@ -/// import { Slip10RawIndex } from "@cosmjs/crypto"; export interface LaunchpadLedgerOptions { readonly hdPath?: readonly Slip10RawIndex[]; @@ -15,8 +14,8 @@ export declare class LaunchpadLedger { constructor(options?: LaunchpadLedgerOptions); connect(timeout?: number): Promise; getCosmosAppVersion(): Promise; - getPubKey(): Promise; - getCosmosAddress(): Promise; + getPubkey(): Promise; + getCosmosAddress(pubkey?: Uint8Array): Promise; sign(message: Uint8Array): Promise; private verifyAppMode; private getOpenAppName;