Refactor to LaunchpadLedger.showAddress

This commit is contained in:
Simon Warta 2022-01-26 13:45:15 +01:00
parent 08bb7e4e73
commit e48101f8a7
4 changed files with 18 additions and 23 deletions

View File

@ -12,7 +12,8 @@ and this project adheres to
JavaScript objects and the JSON representation of `cosmwasm_std::Binary`
(base64).
- @cosmjs/cosmwasm-stargate: Export `WasmExtension` and `setupWasmExtension`.
- @cosmjs/ledger-amino: Added `showAddress` in LedgerSigner.
- @cosmjs/ledger-amino: Added `LedgerSigner.showAddress` and
`LaunchpadLedger.showAddress` to show the user's address in the Ledger screen.
### Changed

View File

@ -1,2 +1,2 @@
export { LaunchpadLedger } from "./launchpadledger";
export { AddressAndPubkey, LaunchpadLedger } from "./launchpadledger";
export { LedgerSigner } from "./ledgersigner";

View File

@ -1,4 +1,4 @@
import { makeCosmoshubPath } from "@cosmjs/amino";
import { encodeSecp256k1Pubkey, makeCosmoshubPath, Secp256k1Pubkey } from "@cosmjs/amino";
import { HdPath, Secp256k1Signature } from "@cosmjs/crypto";
import { fromUtf8 } from "@cosmjs/encoding";
import { assert } from "@cosmjs/utils";
@ -52,6 +52,11 @@ export interface LaunchpadLedgerOptions {
readonly minLedgerAppVersion?: string;
}
export interface AddressAndPubkey {
readonly address: string;
readonly pubkey: Secp256k1Pubkey;
}
export class LaunchpadLedger {
private readonly testModeAllowed: boolean;
private readonly hdPaths: readonly HdPath[];
@ -167,14 +172,19 @@ export class LaunchpadLedger {
await this.verifyCosmosAppIsOpen();
}
public async verifyAddress(hdPath?: HdPath): Promise<AddressAndPublicKeyResponse> {
public async showAddress(hdPath?: HdPath): Promise<AddressAndPubkey> {
await this.verifyDeviceIsReady();
const hdPathToUse = hdPath || this.hdPaths[0];
// ledger-cosmos-js hardens the first three indices
const response = await this.app.showAddressAndPubKey(unharden(hdPathToUse), this.prefix);
this.handleLedgerErrors(response);
return response as AddressAndPublicKeyResponse;
// eslint-disable-next-line @typescript-eslint/naming-convention
const { address, compressed_pk } = response as AddressAndPublicKeyResponse;
return {
address: address,
pubkey: encodeSecp256k1Pubkey(compressed_pk),
};
}
private handleLedgerErrors(

View File

@ -1,23 +1,16 @@
import {
AccountData,
AminoSignResponse,
encodeSecp256k1Pubkey,
encodeSecp256k1Signature,
makeCosmoshubPath,
OfflineAminoSigner,
Secp256k1Pubkey,
serializeSignDoc,
StdSignDoc,
} from "@cosmjs/amino";
import { HdPath } from "@cosmjs/crypto";
import Transport from "@ledgerhq/hw-transport";
import { LaunchpadLedger, LaunchpadLedgerOptions } from "./launchpadledger";
export interface AddressAndPubkey {
readonly address: string;
readonly pubkey: Secp256k1Pubkey;
}
import { AddressAndPubkey, LaunchpadLedger, LaunchpadLedgerOptions } from "./launchpadledger";
export class LedgerSigner implements OfflineAminoSigner {
private readonly ledger: LaunchpadLedger;
@ -45,16 +38,7 @@ export class LedgerSigner implements OfflineAminoSigner {
}
public async showAddress(path: HdPath): Promise<AddressAndPubkey> {
const response = await this.ledger.verifyAddress(path);
if (response.error_message) {
throw new Error(response.error_message);
}
return {
address: response.address,
pubkey: encodeSecp256k1Pubkey(response.compressed_pk),
};
return this.ledger.showAddress(path);
}
public async signAmino(signerAddress: string, signDoc: StdSignDoc): Promise<AminoSignResponse> {