mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
launchpad-ledger: Improve account number handling
This commit is contained in:
parent
fd99625516
commit
5d426ac35c
@ -6,19 +6,28 @@ import {
|
||||
StdSignature,
|
||||
} from "@cosmjs/launchpad";
|
||||
|
||||
import { LaunchpadLedger, LaunchpadLedgerOptions } from "./launchpadledger";
|
||||
import { LaunchpadLedger } from "./launchpadledger";
|
||||
|
||||
export interface LedgerSignerOptions {
|
||||
readonly accountNumbers?: readonly number[];
|
||||
readonly prefix?: string;
|
||||
readonly testModeAllowed?: boolean;
|
||||
}
|
||||
|
||||
export class LedgerSigner implements OfflineSigner {
|
||||
private readonly ledger: LaunchpadLedger;
|
||||
private readonly accountNumbers: readonly number[];
|
||||
private accounts?: readonly AccountData[];
|
||||
|
||||
constructor(options?: LaunchpadLedgerOptions) {
|
||||
this.ledger = new LaunchpadLedger(options);
|
||||
constructor({ accountNumbers = [0], ...restOptions }: LedgerSignerOptions = {}) {
|
||||
this.accountNumbers = accountNumbers;
|
||||
this.ledger = new LaunchpadLedger({
|
||||
...restOptions,
|
||||
hdPaths: accountNumbers.map(makeCosmoshubPath),
|
||||
});
|
||||
}
|
||||
|
||||
public async getAccounts(): Promise<readonly AccountData[]> {
|
||||
await this.ledger.connect();
|
||||
|
||||
if (!this.accounts) {
|
||||
const pubkeys = await this.ledger.getPubkeys();
|
||||
this.accounts = await Promise.all(
|
||||
@ -33,21 +42,16 @@ export class LedgerSigner implements OfflineSigner {
|
||||
return this.accounts;
|
||||
}
|
||||
|
||||
public async sign(
|
||||
address: string,
|
||||
message: Uint8Array,
|
||||
_prehashType?: "sha256" | "sha512" | null,
|
||||
accountNumber = 0,
|
||||
): Promise<StdSignature> {
|
||||
await this.ledger.connect();
|
||||
|
||||
public async sign(address: string, message: Uint8Array): Promise<StdSignature> {
|
||||
const accounts = this.accounts || (await this.getAccounts());
|
||||
const accountForAddress = accounts.find((account) => account.address === address);
|
||||
const accountIndex = accounts.findIndex((account) => account.address === address);
|
||||
|
||||
if (!accountForAddress) {
|
||||
if (accountIndex === -1) {
|
||||
throw new Error(`Address ${address} not found in wallet`);
|
||||
}
|
||||
|
||||
const accountForAddress = accounts[accountIndex];
|
||||
const accountNumber = this.accountNumbers[accountIndex];
|
||||
const hdPath = makeCosmoshubPath(accountNumber);
|
||||
const signature = await this.ledger.sign(message, hdPath);
|
||||
return encodeSecp256k1Signature(accountForAddress.pubkey, signature);
|
||||
|
@ -1,14 +1,14 @@
|
||||
import { AccountData, OfflineSigner, StdSignature } from "@cosmjs/launchpad";
|
||||
import { LaunchpadLedgerOptions } from "./launchpadledger";
|
||||
export interface LedgerSignerOptions {
|
||||
readonly accountNumbers?: readonly number[];
|
||||
readonly prefix?: string;
|
||||
readonly testModeAllowed?: boolean;
|
||||
}
|
||||
export declare class LedgerSigner implements OfflineSigner {
|
||||
private readonly ledger;
|
||||
private readonly accountNumbers;
|
||||
private accounts?;
|
||||
constructor(options?: LaunchpadLedgerOptions);
|
||||
constructor({ accountNumbers, ...restOptions }?: LedgerSignerOptions);
|
||||
getAccounts(): Promise<readonly AccountData[]>;
|
||||
sign(
|
||||
address: string,
|
||||
message: Uint8Array,
|
||||
_prehashType?: "sha256" | "sha512" | null,
|
||||
accountNumber?: number,
|
||||
): Promise<StdSignature>;
|
||||
sign(address: string, message: Uint8Array): Promise<StdSignature>;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user