launchpad-ledger: Refactor ledger pubkey/address methods

This commit is contained in:
willclarktech 2020-09-15 11:18:42 +02:00
parent 738f0b3f65
commit 2ef93a0b26
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
3 changed files with 24 additions and 16 deletions

View File

@ -135,19 +135,19 @@ export class LaunchpadLedger {
return `${major}.${minor}.${patch}`;
}
async getPubKey(): Promise<Buffer> {
async getPubkey(): Promise<Uint8Array> {
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<string> {
const pubKey = await this.getPubKey();
return CosmosApp.getBech32FromPK(this.prefix, pubKey);
async getCosmosAddress(pubkey?: Uint8Array): Promise<string> {
const pubkeyToUse = pubkey || (await this.getPubkey());
return CosmosApp.getBech32FromPK(this.prefix, Buffer.from(pubkeyToUse));
}
// async verifyLedgerAddress(): Promise<void> {

View File

@ -14,14 +14,18 @@ export class LedgerSigner implements OfflineSigner {
public async getAccounts(): Promise<readonly AccountData[]> {
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<StdSignature> {
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);
}
}

View File

@ -1,4 +1,3 @@
/// <reference types="node" />
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<LaunchpadLedger>;
getCosmosAppVersion(): Promise<string>;
getPubKey(): Promise<Buffer>;
getCosmosAddress(): Promise<string>;
getPubkey(): Promise<Uint8Array>;
getCosmosAddress(pubkey?: Uint8Array): Promise<string>;
sign(message: Uint8Array): Promise<Uint8Array>;
private verifyAppMode;
private getOpenAppName;