adding show address in ledgerSigner

This commit is contained in:
Simon Warta 2022-01-26 13:44:53 +01:00
parent 5743d55229
commit 450480791b
4 changed files with 32 additions and 0 deletions

1
.gitignore vendored
View File

@ -14,6 +14,7 @@ node_modules/
# IDE-specific
.vscode/
.idea/
# demo mnemonics from cli
*.key

View File

@ -12,6 +12,7 @@ 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.
### Changed

View File

@ -4,6 +4,7 @@ import { fromUtf8 } from "@cosmjs/encoding";
import { assert } from "@cosmjs/utils";
import Transport from "@ledgerhq/hw-transport";
import CosmosApp, {
AddressAndPublicKeyResponse,
AppInfoResponse,
PublicKeyResponse,
SignResponse,
@ -166,6 +167,16 @@ export class LaunchpadLedger {
await this.verifyCosmosAppIsOpen();
}
public async verifyAddress(hdPath?: HdPath): Promise<AddressAndPublicKeyResponse> {
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;
}
private handleLedgerErrors(
/* eslint-disable @typescript-eslint/naming-convention */
{

View File

@ -4,6 +4,7 @@ import {
encodeSecp256k1Signature,
makeCosmoshubPath,
OfflineAminoSigner,
Secp256k1Pubkey,
serializeSignDoc,
StdSignDoc,
} from "@cosmjs/amino";
@ -12,6 +13,11 @@ import Transport from "@ledgerhq/hw-transport";
import { LaunchpadLedger, LaunchpadLedgerOptions } from "./launchpadledger";
export interface AddressAndPubkey {
readonly address: string;
readonly pubkey: Secp256k1Pubkey;
}
export class LedgerSigner implements OfflineAminoSigner {
private readonly ledger: LaunchpadLedger;
private readonly hdPaths: readonly HdPath[];
@ -37,6 +43,19 @@ export class LedgerSigner implements OfflineAminoSigner {
return this.accounts;
}
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: response.compressed_pk,
};
}
public async signAmino(signerAddress: string, signDoc: StdSignDoc): Promise<AminoSignResponse> {
const accounts = this.accounts || (await this.getAccounts());
const accountIndex = accounts.findIndex((account) => account.address === signerAddress);