launchpad-ledger: Store HD paths in LedgerSigner

This commit is contained in:
willclarktech 2020-09-17 15:27:42 +02:00
parent 917da1bb2a
commit bd774b3eb8
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
4 changed files with 14 additions and 27 deletions

View File

@ -1,6 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { toBase64 } from "@cosmjs/encoding";
import { makeSignBytes, StdFee, StdSignature } from "@cosmjs/launchpad";
import { makeCosmoshubPath, makeSignBytes, StdFee, StdSignature } from "@cosmjs/launchpad";
import { LedgerSigner } from "../ledgersigner";
@ -14,7 +14,7 @@ const defaultSequence = "0";
const signer = new LedgerSigner({
testModeAllowed: true,
accountNumbers: [0, 1, 2, 10],
hdPaths: [makeCosmoshubPath(0), makeCosmoshubPath(1), makeCosmoshubPath(2), makeCosmoshubPath(10)],
});
export async function getAccounts(): Promise<

View File

@ -1,5 +1,5 @@
import { toBase64, toUtf8 } from "@cosmjs/encoding";
import { AccountData } from "@cosmjs/launchpad";
import { AccountData, makeCosmoshubPath } from "@cosmjs/launchpad";
import { LedgerSigner } from "../ledgersigner";
@ -34,7 +34,7 @@ function createMessage(accountNumber: number, address: string): string {
const signer = new LedgerSigner({
testModeAllowed: true,
accountNumbers: [0, 1, 2],
hdPaths: [makeCosmoshubPath(0), makeCosmoshubPath(1), makeCosmoshubPath(2)],
});
window.updateMessage = (accountNumber: number) => {

View File

@ -1,3 +1,4 @@
import { HdPath } from "@cosmjs/crypto";
import {
AccountData,
encodeSecp256k1Signature,
@ -6,25 +7,16 @@ import {
StdSignature,
} from "@cosmjs/launchpad";
import { LaunchpadLedger } from "./launchpadledger";
export interface LedgerSignerOptions {
readonly accountNumbers?: readonly number[];
readonly prefix?: string;
readonly testModeAllowed?: boolean;
}
import { LaunchpadLedger, LaunchpadLedgerOptions } from "./launchpadledger";
export class LedgerSigner implements OfflineSigner {
private readonly ledger: LaunchpadLedger;
private readonly accountNumbers: readonly number[];
private readonly hdPaths: readonly HdPath[];
private accounts?: readonly AccountData[];
constructor({ accountNumbers = [0], ...restOptions }: LedgerSignerOptions = {}) {
this.accountNumbers = accountNumbers;
this.ledger = new LaunchpadLedger({
...restOptions,
hdPaths: accountNumbers.map(makeCosmoshubPath),
});
constructor(options: LaunchpadLedgerOptions = {}) {
this.hdPaths = options.hdPaths || [makeCosmoshubPath(0)];
this.ledger = new LaunchpadLedger(options);
}
public async getAccounts(): Promise<readonly AccountData[]> {
@ -51,8 +43,7 @@ export class LedgerSigner implements OfflineSigner {
}
const accountForAddress = accounts[accountIndex];
const accountNumber = this.accountNumbers[accountIndex];
const hdPath = makeCosmoshubPath(accountNumber);
const hdPath = this.hdPaths[accountIndex];
const signature = await this.ledger.sign(message, hdPath);
return encodeSecp256k1Signature(accountForAddress.pubkey, signature);
}

View File

@ -1,14 +1,10 @@
import { AccountData, OfflineSigner, StdSignature } from "@cosmjs/launchpad";
export interface LedgerSignerOptions {
readonly accountNumbers?: readonly number[];
readonly prefix?: string;
readonly testModeAllowed?: boolean;
}
import { LaunchpadLedgerOptions } from "./launchpadledger";
export declare class LedgerSigner implements OfflineSigner {
private readonly ledger;
private readonly accountNumbers;
private readonly hdPaths;
private accounts?;
constructor({ accountNumbers, ...restOptions }?: LedgerSignerOptions);
constructor(options?: LaunchpadLedgerOptions);
getAccounts(): Promise<readonly AccountData[]>;
sign(address: string, message: Uint8Array): Promise<StdSignature>;
}