mirror of
https://github.com/cosmos/cosmjs.git
synced 2025-03-11 14:09:15 +00:00
launchpad-ledger: Refactor ledger pubkey/address methods
This commit is contained in:
parent
738f0b3f65
commit
2ef93a0b26
@ -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> {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
@ -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;
|
||||
|
Loading…
x
Reference in New Issue
Block a user