launchpad: Use signer interfaces from amino

This commit is contained in:
willclarktech 2021-03-24 13:15:17 +01:00
parent f616abc4d8
commit 8ec39af787
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
10 changed files with 83 additions and 119 deletions

View File

@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { AminoMsg, StdSignDoc } from "@cosmjs/amino";
import { toUtf8 } from "@cosmjs/encoding";
import { Uint53 } from "@cosmjs/math";
import { StdFee } from "./fee";
import { Msg } from "./msgs";
function sortedObject(obj: any): any {
if (typeof obj !== "object" || obj === null) {
@ -27,22 +27,8 @@ export function sortedJsonStringify(obj: any): string {
return JSON.stringify(sortedObject(obj));
}
/**
* The document to be signed
*
* @see https://docs.cosmos.network/master/modules/auth/03_types.html#stdsigndoc
*/
export interface StdSignDoc {
readonly chain_id: string;
readonly account_number: string;
readonly sequence: string;
readonly fee: StdFee;
readonly msgs: readonly Msg[];
readonly memo: string;
}
export function makeSignDoc(
msgs: readonly Msg[],
msgs: readonly AminoMsg[],
fee: StdFee,
chainId: string,
memo: string | undefined,

View File

@ -1,5 +1,12 @@
// Re-exports for backwards compatibility
export {
AccountData,
Algo,
AminoMsg as Msg,
AminoSignResponse,
OfflineAminoSigner as OfflineSigner,
StdSignDoc,
StdSignature,
decodeAminoPubkey,
decodeBech32Pubkey,
decodeSignature,
@ -9,7 +16,6 @@ export {
encodeSecp256k1Signature,
pubkeyToAddress,
pubkeyType,
StdSignature,
} from "@cosmjs/amino";
import { SinglePubkey } from "@cosmjs/amino";
/** @deprecated PubKey is deprecated. Use `SinglePubkey` or the more general `Pubkey` from `@cosmjs/amino`. */
@ -42,7 +48,7 @@ export {
isSearchBySentFromOrToQuery,
isSearchByTagsQuery,
} from "./cosmosclient";
export { makeSignDoc, serializeSignDoc, StdSignDoc } from "./encoding";
export { makeSignDoc, serializeSignDoc } from "./encoding";
export { buildFeeTable, FeeTable, GasLimits, GasPrice, StdFee } from "./fee";
export {
AuthAccountsResponse,
@ -127,7 +133,6 @@ export {
isMsgUndelegate,
isMsgWithdrawDelegatorReward,
isMsgWithdrawValidatorCommission,
Msg,
MsgBeginRedelegate,
MsgCreateValidator,
MsgDelegate,
@ -142,7 +147,6 @@ export {
} from "./msgs";
export { makeCosmoshubPath } from "./paths";
export { findSequenceForSignedTx } from "./sequence";
export { AccountData, Algo, AminoSignResponse, OfflineSigner } from "./signer";
export { CosmosFeeTable, SigningCosmosClient } from "./signingcosmosclient";
export { isStdTx, isWrappedStdTx, makeStdTx, CosmosSdkTx, StdTx, WrappedStdTx, WrappedTx } from "./tx";
export { executeKdf, KdfConfiguration } from "./wallet";

View File

@ -1,17 +1,14 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Coin } from "./coins";
import { AminoMsg } from "@cosmjs/amino";
export interface Msg {
readonly type: string;
readonly value: any;
}
import { Coin } from "./coins";
// auth (no messages) - see https://github.com/cosmos/cosmos-sdk/blob/efa73c7/proto/cosmos/auth/auth.proto
// bank - see https://github.com/cosmos/cosmos-sdk/blob/efa73c7/proto/cosmos/bank/bank.proto
/** A high level transaction of the coin module */
export interface MsgSend extends Msg {
export interface MsgSend extends AminoMsg {
readonly type: "cosmos-sdk/MsgSend";
readonly value: {
/** Bech32 account address */
@ -22,7 +19,7 @@ export interface MsgSend extends Msg {
};
}
export function isMsgSend(msg: Msg): msg is MsgSend {
export function isMsgSend(msg: AminoMsg): msg is MsgSend {
return (msg as MsgSend).type === "cosmos-sdk/MsgSend";
}
@ -39,7 +36,7 @@ interface Output {
}
/** A high level transaction of the coin module */
export interface MsgMultiSend extends Msg {
export interface MsgMultiSend extends AminoMsg {
readonly type: "cosmos-sdk/MsgMultiSend";
readonly value: {
readonly inputs: readonly Input[];
@ -47,14 +44,14 @@ export interface MsgMultiSend extends Msg {
};
}
export function isMsgMultiSend(msg: Msg): msg is MsgMultiSend {
export function isMsgMultiSend(msg: AminoMsg): msg is MsgMultiSend {
return (msg as MsgMultiSend).type === "cosmos-sdk/MsgMultiSend";
}
// crisis - see https://github.com/cosmos/cosmos-sdk/blob/efa73c7/proto/cosmos/crisis/crisis.proto
/** Verifies a particular invariance */
export interface MsgVerifyInvariant extends Msg {
export interface MsgVerifyInvariant extends AminoMsg {
readonly type: "cosmos-sdk/MsgVerifyInvariant";
readonly value: {
/** Bech32 account address */
@ -64,14 +61,14 @@ export interface MsgVerifyInvariant extends Msg {
};
}
export function isMsgVerifyInvariant(msg: Msg): msg is MsgVerifyInvariant {
export function isMsgVerifyInvariant(msg: AminoMsg): msg is MsgVerifyInvariant {
return (msg as MsgVerifyInvariant).type === "cosmos-sdk/MsgVerifyInvariant";
}
// distribution - see https://github.com/cosmos/cosmos-sdk/blob/efa73c7/proto/cosmos/distribution/distribution.proto
/** Changes the withdraw address for a delegator (or validator self-delegation) */
export interface MsgSetWithdrawAddress extends Msg {
export interface MsgSetWithdrawAddress extends AminoMsg {
// NOTE: Type string and names diverge here!
readonly type: "cosmos-sdk/MsgModifyWithdrawAddress";
readonly value: {
@ -82,13 +79,13 @@ export interface MsgSetWithdrawAddress extends Msg {
};
}
export function isMsgSetWithdrawAddress(msg: Msg): msg is MsgSetWithdrawAddress {
export function isMsgSetWithdrawAddress(msg: AminoMsg): msg is MsgSetWithdrawAddress {
// NOTE: Type string and names diverge here!
return (msg as MsgSetWithdrawAddress).type === "cosmos-sdk/MsgModifyWithdrawAddress";
}
/** Message for delegation withdraw from a single validator */
export interface MsgWithdrawDelegatorReward extends Msg {
export interface MsgWithdrawDelegatorReward extends AminoMsg {
// NOTE: Type string and names diverge here!
readonly type: "cosmos-sdk/MsgWithdrawDelegationReward";
readonly value: {
@ -99,13 +96,13 @@ export interface MsgWithdrawDelegatorReward extends Msg {
};
}
export function isMsgWithdrawDelegatorReward(msg: Msg): msg is MsgWithdrawDelegatorReward {
export function isMsgWithdrawDelegatorReward(msg: AminoMsg): msg is MsgWithdrawDelegatorReward {
// NOTE: Type string and names diverge here!
return (msg as MsgWithdrawDelegatorReward).type === "cosmos-sdk/MsgWithdrawDelegationReward";
}
/** Message for validator withdraw */
export interface MsgWithdrawValidatorCommission extends Msg {
export interface MsgWithdrawValidatorCommission extends AminoMsg {
readonly type: "cosmos-sdk/MsgWithdrawValidatorCommission";
readonly value: {
/** Bech32 account address */
@ -113,12 +110,12 @@ export interface MsgWithdrawValidatorCommission extends Msg {
};
}
export function isMsgWithdrawValidatorCommission(msg: Msg): msg is MsgWithdrawValidatorCommission {
export function isMsgWithdrawValidatorCommission(msg: AminoMsg): msg is MsgWithdrawValidatorCommission {
return (msg as MsgWithdrawValidatorCommission).type === "cosmos-sdk/MsgWithdrawValidatorCommission";
}
/** Allows an account to directly fund the community pool. */
export interface MsgFundCommunityPool extends Msg {
export interface MsgFundCommunityPool extends AminoMsg {
readonly type: "cosmos-sdk/MsgFundCommunityPool";
readonly value: {
readonly amount: readonly Coin[];
@ -127,7 +124,7 @@ export interface MsgFundCommunityPool extends Msg {
};
}
export function isMsgFundCommunityPool(msg: Msg): msg is MsgFundCommunityPool {
export function isMsgFundCommunityPool(msg: AminoMsg): msg is MsgFundCommunityPool {
return (msg as MsgFundCommunityPool).type === "cosmos-sdk/MsgFundCommunityPool";
}
@ -139,7 +136,7 @@ interface Any {
}
/** Supports submitting arbitrary evidence */
export interface MsgSubmitEvidence extends Msg {
export interface MsgSubmitEvidence extends AminoMsg {
readonly type: "cosmos-sdk/MsgSubmitEvidence";
readonly value: {
/** Bech32 account address */
@ -148,14 +145,14 @@ export interface MsgSubmitEvidence extends Msg {
};
}
export function isMsgSubmitEvidence(msg: Msg): msg is MsgSubmitEvidence {
export function isMsgSubmitEvidence(msg: AminoMsg): msg is MsgSubmitEvidence {
return (msg as MsgSubmitEvidence).type === "cosmos-sdk/MsgSubmitEvidence";
}
// gov - https://github.com/cosmos/cosmos-sdk/blob/efa73c7edb31a7bd65786501da213b294f89267a/proto/cosmos/gov/gov.proto
/** Supports submitting arbitrary proposal content. */
export interface MsgSubmitProposal extends Msg {
export interface MsgSubmitProposal extends AminoMsg {
readonly type: "cosmos-sdk/MsgSubmitProposal";
readonly value: {
readonly content: Any;
@ -165,7 +162,7 @@ export interface MsgSubmitProposal extends Msg {
};
}
export function isMsgSubmitProposal(msg: Msg): msg is MsgSubmitProposal {
export function isMsgSubmitProposal(msg: AminoMsg): msg is MsgSubmitProposal {
return (msg as MsgSubmitProposal).type === "cosmos-sdk/MsgSubmitProposal";
}
@ -178,7 +175,7 @@ enum VoteOption {
}
/** Casts a vote */
export interface MsgVote extends Msg {
export interface MsgVote extends AminoMsg {
readonly type: "cosmos-sdk/MsgVote";
readonly value: {
readonly proposal_id: number;
@ -188,12 +185,12 @@ export interface MsgVote extends Msg {
};
}
export function isMsgVote(msg: Msg): msg is MsgVote {
export function isMsgVote(msg: AminoMsg): msg is MsgVote {
return (msg as MsgVote).type === "cosmos-sdk/MsgVote";
}
/** Submits a deposit to an existing proposal */
export interface MsgDeposit extends Msg {
export interface MsgDeposit extends AminoMsg {
readonly type: "cosmos-sdk/MsgDeposit";
readonly value: {
readonly proposal_id: number;
@ -203,7 +200,7 @@ export interface MsgDeposit extends Msg {
};
}
export function isMsgDeposit(msg: Msg): msg is MsgDeposit {
export function isMsgDeposit(msg: AminoMsg): msg is MsgDeposit {
return (msg as MsgDeposit).type === "cosmos-sdk/MsgDeposit";
}
@ -216,7 +213,7 @@ export function isMsgDeposit(msg: Msg): msg is MsgDeposit {
// slashing - see https://github.com/cosmos/cosmos-sdk/blob/efa73c7/proto/cosmos/slashing/slashing.proto
/** Unjails a jailed validator */
export interface MsgUnjail extends Msg {
export interface MsgUnjail extends AminoMsg {
readonly type: "cosmos-sdk/MsgUnjail";
readonly value: {
/** Bech32 account address */
@ -224,7 +221,7 @@ export interface MsgUnjail extends Msg {
};
}
export function isMsgUnjail(msg: Msg): msg is MsgUnjail {
export function isMsgUnjail(msg: AminoMsg): msg is MsgUnjail {
return (msg as MsgUnjail).type === "cosmos-sdk/MsgUnjail";
}
@ -247,7 +244,7 @@ interface Description {
}
/** Creates a new validator. */
export interface MsgCreateValidator extends Msg {
export interface MsgCreateValidator extends AminoMsg {
readonly type: "cosmos-sdk/MsgCreateValidator";
readonly value: {
readonly description: Description;
@ -263,12 +260,12 @@ export interface MsgCreateValidator extends Msg {
};
}
export function isMsgCreateValidator(msg: Msg): msg is MsgCreateValidator {
export function isMsgCreateValidator(msg: AminoMsg): msg is MsgCreateValidator {
return (msg as MsgCreateValidator).type === "cosmos-sdk/MsgCreateValidator";
}
/** Edits an existing validator. */
export interface MsgEditValidator extends Msg {
export interface MsgEditValidator extends AminoMsg {
readonly type: "cosmos-sdk/MsgEditValidator";
readonly value: {
readonly description: Description;
@ -279,7 +276,7 @@ export interface MsgEditValidator extends Msg {
};
}
export function isMsgEditValidator(msg: Msg): msg is MsgEditValidator {
export function isMsgEditValidator(msg: AminoMsg): msg is MsgEditValidator {
return (msg as MsgEditValidator).type === "cosmos-sdk/MsgEditValidator";
}
@ -288,7 +285,7 @@ export function isMsgEditValidator(msg: Msg): msg is MsgEditValidator {
*
* @see https://docs.cosmos.network/master/modules/staking/03_messages.html#msgdelegate
*/
export interface MsgDelegate extends Msg {
export interface MsgDelegate extends AminoMsg {
readonly type: "cosmos-sdk/MsgDelegate";
readonly value: {
/** Bech32 encoded delegator address */
@ -299,12 +296,12 @@ export interface MsgDelegate extends Msg {
};
}
export function isMsgDelegate(msg: Msg): msg is MsgDelegate {
export function isMsgDelegate(msg: AminoMsg): msg is MsgDelegate {
return (msg as MsgDelegate).type === "cosmos-sdk/MsgDelegate";
}
/** Performs a redelegation from a delegate and source validator to a destination validator */
export interface MsgBeginRedelegate extends Msg {
export interface MsgBeginRedelegate extends AminoMsg {
readonly type: "cosmos-sdk/MsgBeginRedelegate";
readonly value: {
/** Bech32 encoded delegator address */
@ -317,12 +314,12 @@ export interface MsgBeginRedelegate extends Msg {
};
}
export function isMsgBeginRedelegate(msg: Msg): msg is MsgBeginRedelegate {
export function isMsgBeginRedelegate(msg: AminoMsg): msg is MsgBeginRedelegate {
return (msg as MsgBeginRedelegate).type === "cosmos-sdk/MsgBeginRedelegate";
}
/** Performs an undelegation from a delegate and a validator */
export interface MsgUndelegate extends Msg {
export interface MsgUndelegate extends AminoMsg {
readonly type: "cosmos-sdk/MsgUndelegate";
readonly value: {
/** Bech32 encoded delegator address */
@ -333,7 +330,7 @@ export interface MsgUndelegate extends Msg {
};
}
export function isMsgUndelegate(msg: Msg): msg is MsgUndelegate {
export function isMsgUndelegate(msg: AminoMsg): msg is MsgUndelegate {
return (msg as MsgUndelegate).type === "cosmos-sdk/MsgUndelegate";
}

View File

@ -1,8 +1,9 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { StdSignDoc } from "@cosmjs/amino";
import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto";
import { fromBase64, fromHex } from "@cosmjs/encoding";
import { serializeSignDoc, StdSignDoc } from "./encoding";
import { serializeSignDoc } from "./encoding";
import { extractKdfConfiguration, Secp256k1HdWallet } from "./secp256k1hdwallet";
import { base64Matcher } from "./testutils.spec";
import { executeKdf, KdfConfiguration } from "./wallet";

View File

@ -1,4 +1,11 @@
import { encodeSecp256k1Signature, rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino";
import {
AccountData,
AminoSignResponse,
encodeSecp256k1Signature,
OfflineAminoSigner,
rawSecp256k1PubkeyToRawAddress,
StdSignDoc,
} from "@cosmjs/amino";
import {
Bip39,
EnglishMnemonic,
@ -14,9 +21,8 @@ import {
import { Bech32, fromBase64, fromUtf8, toBase64, toUtf8 } from "@cosmjs/encoding";
import { assert, isNonNullObject } from "@cosmjs/utils";
import { serializeSignDoc, StdSignDoc } from "./encoding";
import { serializeSignDoc } from "./encoding";
import { makeCosmoshubPath } from "./paths";
import { AccountData, AminoSignResponse, OfflineSigner } from "./signer";
import {
decrypt,
encrypt,
@ -106,7 +112,7 @@ interface DerivationInfo {
readonly prefix: string;
}
export class Secp256k1HdWallet implements OfflineSigner {
export class Secp256k1HdWallet implements OfflineAminoSigner {
/**
* Restores a wallet from the given BIP39 mnemonic.
*

View File

@ -1,8 +1,9 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { StdSignDoc } from "@cosmjs/amino";
import { Secp256k1, Secp256k1Signature, Sha256 } from "@cosmjs/crypto";
import { fromBase64, fromHex } from "@cosmjs/encoding";
import { serializeSignDoc, StdSignDoc } from "./encoding";
import { serializeSignDoc } from "./encoding";
import { Secp256k1Wallet } from "./secp256k1wallet";
describe("Secp256k1Wallet", () => {

View File

@ -1,16 +1,22 @@
import { encodeSecp256k1Signature, rawSecp256k1PubkeyToRawAddress } from "@cosmjs/amino";
import {
AccountData,
AminoSignResponse,
encodeSecp256k1Signature,
OfflineAminoSigner,
rawSecp256k1PubkeyToRawAddress,
StdSignDoc,
} from "@cosmjs/amino";
import { Secp256k1, Sha256 } from "@cosmjs/crypto";
import { Bech32 } from "@cosmjs/encoding";
import { serializeSignDoc, StdSignDoc } from "./encoding";
import { AccountData, AminoSignResponse, OfflineSigner } from "./signer";
import { serializeSignDoc } from "./encoding";
/**
* A wallet that holds a single secp256k1 keypair.
*
* If you want to work with BIP39 mnemonics and multiple accounts, use Secp256k1HdWallet.
*/
export class Secp256k1Wallet implements OfflineSigner {
export class Secp256k1Wallet implements OfflineAminoSigner {
/**
* Creates a Secp256k1Wallet from the given private key
*

View File

@ -1,39 +0,0 @@
import { StdSignature } from "@cosmjs/amino";
import { StdSignDoc } from "./encoding";
export type Algo = "secp256k1" | "ed25519" | "sr25519";
export interface AccountData {
/** A printable address (typically bech32 encoded) */
readonly address: string;
readonly algo: Algo;
readonly pubkey: Uint8Array;
}
export interface AminoSignResponse {
/**
* The sign doc that was signed.
* This may be different from the input signDoc when the signer modifies it as part of the signing process.
*/
readonly signed: StdSignDoc;
readonly signature: StdSignature;
}
export interface OfflineSigner {
/**
* Get AccountData array from wallet. Rejects if not enabled.
*/
readonly getAccounts: () => Promise<readonly AccountData[]>;
/**
* Request signature from whichever key corresponds to provided bech32-encoded address. Rejects if not enabled.
*
* The signer implementation may offer the user the ability to override parts of the signDoc. It must
* return the doc that was signed in the response.
*
* @param signerAddress The address of the account that should sign the transaction
* @param signDoc The content that should be signed
*/
readonly signAmino: (signerAddress: string, signDoc: StdSignDoc) => Promise<AminoSignResponse>;
}

View File

@ -1,4 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { AminoMsg, OfflineAminoSigner } from "@cosmjs/amino";
import equals from "fast-deep-equal";
import { Coin } from "./coins";
@ -6,8 +7,7 @@ import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./c
import { makeSignDoc } from "./encoding";
import { buildFeeTable, FeeTable, GasLimits, GasPrice, StdFee } from "./fee";
import { BroadcastMode } from "./lcdapi";
import { Msg, MsgSend } from "./msgs";
import { OfflineSigner } from "./signer";
import { MsgSend } from "./msgs";
import { makeStdTx, StdTx } from "./tx";
/**
@ -29,7 +29,7 @@ export class SigningCosmosClient extends CosmosClient {
public readonly fees: CosmosFeeTable;
public readonly signerAddress: string;
private readonly signer: OfflineSigner;
private readonly signer: OfflineAminoSigner;
/**
* Creates a new client with signing capability to interact with a Cosmos SDK blockchain. This is the bigger brother of CosmosClient.
@ -39,7 +39,7 @@ export class SigningCosmosClient extends CosmosClient {
*
* @param apiUrl The URL of a Cosmos SDK light client daemon API (sometimes called REST server or REST API)
* @param signerAddress The address that will sign transactions using this instance. The `signer` must be able to sign with this address.
* @param signer An implementation of OfflineSigner which can provide signatures for transactions, potentially requiring user input.
* @param signer An implementation of OfflineAminoSigner which can provide signatures for transactions, potentially requiring user input.
* @param gasPrice The price paid per unit of gas
* @param gasLimits Custom overrides for gas limits related to specific transaction types
* @param broadcastMode Defines at which point of the transaction processing the broadcastTx method returns
@ -47,7 +47,7 @@ export class SigningCosmosClient extends CosmosClient {
public constructor(
apiUrl: string,
signerAddress: string,
signer: OfflineSigner,
signer: OfflineAminoSigner,
gasPrice: GasPrice = defaultGasPrice,
gasLimits: Partial<GasLimits<CosmosFeeTable>> = {},
broadcastMode = BroadcastMode.Block,
@ -87,7 +87,11 @@ export class SigningCosmosClient extends CosmosClient {
* Gets account number and sequence from the API, creates a sign doc,
* creates a single signature, assembles the signed transaction and broadcasts it.
*/
public async signAndBroadcast(msgs: readonly Msg[], fee: StdFee, memo = ""): Promise<BroadcastTxResult> {
public async signAndBroadcast(
msgs: readonly AminoMsg[],
fee: StdFee,
memo = "",
): Promise<BroadcastTxResult> {
const signedTx = await this.sign(msgs, fee, memo);
return this.broadcastTx(signedTx);
}
@ -96,7 +100,7 @@ export class SigningCosmosClient extends CosmosClient {
* Gets account number and sequence from the API, creates a sign doc,
* creates a single signature and assembles the signed transaction.
*/
public async sign(msgs: readonly Msg[], fee: StdFee, memo = ""): Promise<StdTx> {
public async sign(msgs: readonly AminoMsg[], fee: StdFee, memo = ""): Promise<StdTx> {
const { accountNumber, sequence } = await this.getSequence();
const chainId = await this.getChainId();
const signDoc = makeSignDoc(msgs, fee, chainId, memo, accountNumber, sequence);

View File

@ -1,8 +1,6 @@
import { StdSignature } from "@cosmjs/amino";
import { AminoMsg, StdSignature, StdSignDoc } from "@cosmjs/amino";
import { StdSignDoc } from "./encoding";
import { StdFee } from "./fee";
import { Msg } from "./msgs";
/**
* A Cosmos SDK StdTx
@ -10,7 +8,7 @@ import { Msg } from "./msgs";
* @see https://docs.cosmos.network/master/modules/auth/03_types.html#stdtx
*/
export interface StdTx {
readonly msg: readonly Msg[];
readonly msg: readonly AminoMsg[];
readonly fee: StdFee;
readonly signatures: readonly StdSignature[];
readonly memo: string | undefined;