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` JavaScript objects and the JSON representation of `cosmwasm_std::Binary`
(base64). (base64).
- @cosmjs/cosmwasm-stargate: Export `WasmExtension` and `setupWasmExtension`. - @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 ### Changed

View File

@ -1,2 +1,2 @@
export { LaunchpadLedger } from "./launchpadledger"; export { AddressAndPubkey, LaunchpadLedger } from "./launchpadledger";
export { LedgerSigner } from "./ledgersigner"; 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 { HdPath, Secp256k1Signature } from "@cosmjs/crypto";
import { fromUtf8 } from "@cosmjs/encoding"; import { fromUtf8 } from "@cosmjs/encoding";
import { assert } from "@cosmjs/utils"; import { assert } from "@cosmjs/utils";
@ -52,6 +52,11 @@ export interface LaunchpadLedgerOptions {
readonly minLedgerAppVersion?: string; readonly minLedgerAppVersion?: string;
} }
export interface AddressAndPubkey {
readonly address: string;
readonly pubkey: Secp256k1Pubkey;
}
export class LaunchpadLedger { export class LaunchpadLedger {
private readonly testModeAllowed: boolean; private readonly testModeAllowed: boolean;
private readonly hdPaths: readonly HdPath[]; private readonly hdPaths: readonly HdPath[];
@ -167,14 +172,19 @@ export class LaunchpadLedger {
await this.verifyCosmosAppIsOpen(); await this.verifyCosmosAppIsOpen();
} }
public async verifyAddress(hdPath?: HdPath): Promise<AddressAndPublicKeyResponse> { public async showAddress(hdPath?: HdPath): Promise<AddressAndPubkey> {
await this.verifyDeviceIsReady(); await this.verifyDeviceIsReady();
const hdPathToUse = hdPath || this.hdPaths[0]; const hdPathToUse = hdPath || this.hdPaths[0];
// ledger-cosmos-js hardens the first three indices // ledger-cosmos-js hardens the first three indices
const response = await this.app.showAddressAndPubKey(unharden(hdPathToUse), this.prefix); const response = await this.app.showAddressAndPubKey(unharden(hdPathToUse), this.prefix);
this.handleLedgerErrors(response); 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( private handleLedgerErrors(

View File

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