launchpad-ledger: Fully support multiple accounts

This commit is contained in:
willclarktech 2020-09-16 12:44:48 +02:00
parent 55602522ab
commit 7b005b4244
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
3 changed files with 26 additions and 5 deletions

View File

@ -122,7 +122,11 @@ export class LaunchpadLedger {
}
async getPubkeys(): Promise<readonly Uint8Array[]> {
return Promise.all(this.hdPaths.map(async (hdPath) => this.getPubkey(hdPath)));
return this.hdPaths.reduce(
(promise: Promise<readonly Uint8Array[]>, hdPath) =>
promise.then(async (pubkeys) => [...pubkeys, await this.getPubkey(hdPath)]),
Promise.resolve([]),
);
}
async getCosmosAddress(pubkey?: Uint8Array): Promise<string> {

View File

@ -1,4 +1,10 @@
import { AccountData, encodeSecp256k1Signature, OfflineSigner, StdSignature } from "@cosmjs/launchpad";
import {
AccountData,
encodeSecp256k1Signature,
makeCosmoshubPath,
OfflineSigner,
StdSignature,
} from "@cosmjs/launchpad";
import { LaunchpadLedger, LaunchpadLedgerOptions } from "./launchpadledger";
@ -27,7 +33,12 @@ export class LedgerSigner implements OfflineSigner {
return this.accounts;
}
public async sign(address: string, message: Uint8Array): Promise<StdSignature> {
public async sign(
address: string,
message: Uint8Array,
_prehashType?: "sha256" | "sha512" | null,
accountNumber = 0,
): Promise<StdSignature> {
await this.ledger.connect();
const accounts = this.accounts || (await this.getAccounts());
@ -37,7 +48,8 @@ export class LedgerSigner implements OfflineSigner {
throw new Error(`Address ${address} not found in wallet`);
}
const signature = await this.ledger.sign(message);
const hdPath = makeCosmoshubPath(accountNumber);
const signature = await this.ledger.sign(message, hdPath);
return encodeSecp256k1Signature(accountForAddress.pubkey, signature);
}
}

View File

@ -5,5 +5,10 @@ export declare class LedgerSigner implements OfflineSigner {
private accounts?;
constructor(options?: LaunchpadLedgerOptions);
getAccounts(): Promise<readonly AccountData[]>;
sign(address: string, message: Uint8Array): Promise<StdSignature>;
sign(
address: string,
message: Uint8Array,
_prehashType?: "sha256" | "sha512" | null,
accountNumber?: number,
): Promise<StdSignature>;
}