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

View File

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

View File

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

View File

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