Move StdTx, isStdTx and makeStdTx to @cosmjs/amino

This commit is contained in:
Simon Warta 2021-07-21 15:37:23 +02:00
parent a56de626e5
commit d6fa413e61
15 changed files with 116 additions and 102 deletions

View File

@ -14,6 +14,9 @@ and this project adheres to
([#932]).
- @cosmjs/stargate: Merge `DeliverTxFailure` and `DeliverTxSuccess` into a
single `DeliverTxResponse` ([#878], [#949]). Add `assertIsDeliverTxFailure`.
- @cosmjs/amino: Added `StdTx`, `isStdTx` and `makeStdTx` and removed them from
@cosmjs/launchpad. They are re-exported in @cosmjs/launchpad for backwards
compatibility.
[#938]: https://github.com/cosmos/cosmjs/issues/938
[#932]: https://github.com/cosmos/cosmjs/issues/932

View File

@ -31,4 +31,5 @@ export { Secp256k1Wallet } from "./secp256k1wallet";
export { decodeSignature, encodeSecp256k1Signature, StdSignature } from "./signature";
export { AminoMsg, makeSignDoc, serializeSignDoc, StdFee, StdSignDoc } from "./signdoc";
export { AccountData, Algo, AminoSignResponse, OfflineAminoSigner } from "./signer";
export { StdTx, isStdTx, makeStdTx } from "./stdtx";
export { executeKdf, KdfConfiguration } from "./wallet";

View File

@ -0,0 +1,52 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { coins } from "./coins";
import { StdSignature } from "./signature";
import { makeSignDoc, StdFee } from "./signdoc";
import { makeStdTx } from "./stdtx";
describe("makeStdTx", () => {
it("can make an StdTx from a SignDoc and one signature", () => {
const fee: StdFee = { amount: coins(123, "ucosm"), gas: "22" };
const signDoc = makeSignDoc([], fee, "chain-xy", "hello", 3, 4);
const signature: StdSignature = {
pub_key: {
type: "tendermint/PubKeySecp256k1",
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",
},
signature: "1nUcIH0CLT0/nQ0mBTDrT6kMG20NY/PsH7P2gc4bpYNGLEYjBmdWevXUJouSE/9A/60QG9cYeqyTe5kFDeIPxQ==",
};
const signedTx = makeStdTx(signDoc, signature);
expect(signedTx).toEqual({
msg: [],
memo: "hello",
fee: fee,
signatures: [signature],
});
});
it("can make an StdTx from a SignDoc and multiple signatures", () => {
const fee: StdFee = { amount: coins(123, "ucosm"), gas: "22" };
const signDoc = makeSignDoc([], fee, "chain-xy", "hello", 3, 4);
const signature1: StdSignature = {
pub_key: {
type: "tendermint/PubKeySecp256k1",
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",
},
signature: "1nUcIH0CLT0/nQ0mBTDrT6kMG20NY/PsH7P2gc4bpYNGLEYjBmdWevXUJouSE/9A/60QG9cYeqyTe5kFDeIPxQ==",
};
const signature2: StdSignature = {
pub_key: {
type: "tendermint/PubKeySecp256k1",
value: "A5qFcJBJvEK/fOmEAY0DHNWwSRZ9TEfNZyH8VoVvDtAq",
},
signature: "NK1Oy4EUGAsoC03c1wi9GG03JC/39LEdautC5Jk643oIbEPqeXHMwaqbdvO/Jws0X/NAXaN8SAy2KNY5Qml+5Q==",
};
const signedTx = makeStdTx(signDoc, [signature1, signature2]);
expect(signedTx).toEqual({
msg: [],
memo: "hello",
fee: fee,
signatures: [signature1, signature2],
});
});
});

View File

@ -0,0 +1,33 @@
import { StdSignature } from "./signature";
import { AminoMsg, StdFee, StdSignDoc } from "./signdoc";
/**
* A Cosmos SDK StdTx
*
* @see https://docs.cosmos.network/master/modules/auth/03_types.html#stdtx
*/
export interface StdTx {
readonly msg: readonly AminoMsg[];
readonly fee: StdFee;
readonly signatures: readonly StdSignature[];
readonly memo: string | undefined;
}
export function isStdTx(txValue: unknown): txValue is StdTx {
const { memo, msg, fee, signatures } = txValue as StdTx;
return (
typeof memo === "string" && Array.isArray(msg) && typeof fee === "object" && Array.isArray(signatures)
);
}
export function makeStdTx(
content: Pick<StdSignDoc, "msgs" | "fee" | "memo">,
signatures: StdSignature | readonly StdSignature[],
): StdTx {
return {
msg: content.msgs,
fee: content.fee,
memo: content.memo,
signatures: Array.isArray(signatures) ? signatures : [signatures],
};
}

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { coins, makeSignDoc, Secp256k1HdWallet } from "@cosmjs/amino";
import { coins, makeSignDoc, makeStdTx, Secp256k1HdWallet } from "@cosmjs/amino";
import { assert, sleep } from "@cosmjs/utils";
import { CosmosClient, isBroadcastTxFailure } from "./cosmosclient";
@ -14,7 +14,7 @@ import {
makeRandomAddress,
pendingWithoutLaunchpad,
} from "./testutils.spec";
import { makeStdTx, WrappedStdTx } from "./tx";
import { WrappedStdTx } from "./tx";
interface TestTxSend {
readonly sender: string;

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { makeSignDoc, Secp256k1HdWallet, StdFee } from "@cosmjs/amino";
import { makeSignDoc, makeStdTx, Secp256k1HdWallet, StdFee } from "@cosmjs/amino";
import { assert, sleep } from "@cosmjs/utils";
import { ReadonlyDate } from "readonly-date";
@ -15,7 +15,7 @@ import {
tendermintIdMatcher,
unused,
} from "./testutils.spec";
import { isWrappedStdTx, makeStdTx } from "./tx";
import { isWrappedStdTx } from "./tx";
const blockTime = 1_000; // ms

View File

@ -1,4 +1,4 @@
import { Coin, Pubkey } from "@cosmjs/amino";
import { Coin, Pubkey, StdTx } from "@cosmjs/amino";
import { sha256 } from "@cosmjs/crypto";
import { fromBase64, fromHex, toHex } from "@cosmjs/encoding";
import { Uint53 } from "@cosmjs/math";
@ -12,7 +12,7 @@ import {
uint64ToNumber,
} from "./lcdapi";
import { Log, parseLogs } from "./logs";
import { StdTx, WrappedStdTx } from "./tx";
import { WrappedStdTx } from "./tx";
export interface GetSequenceResult {
readonly accountNumber: number;

View File

@ -29,6 +29,9 @@ export {
pubkeyToAddress,
pubkeyType,
serializeSignDoc,
isStdTx,
makeStdTx,
StdTx,
} from "@cosmjs/amino";
import { SinglePubkey } from "@cosmjs/amino";
/** @deprecated PubKey is deprecated. Use `SinglePubkey` or the more general `Pubkey` from `@cosmjs/amino`. */
@ -157,4 +160,4 @@ export {
} from "./msgs";
export { findSequenceForSignedTx } from "./sequence";
export { CosmosFeeTable, SigningCosmosClient } from "./signingcosmosclient";
export { isStdTx, isWrappedStdTx, makeStdTx, CosmosSdkTx, StdTx, WrappedStdTx, WrappedTx } from "./tx";
export { isWrappedStdTx, CosmosSdkTx, WrappedStdTx, WrappedTx } from "./tx";

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { coin, coins, makeSignDoc, Secp256k1HdWallet } from "@cosmjs/amino";
import { coin, coins, makeSignDoc, makeStdTx, Secp256k1HdWallet } from "@cosmjs/amino";
import { Bech32 } from "@cosmjs/encoding";
import { sleep } from "@cosmjs/utils";
@ -14,7 +14,6 @@ import {
nonNegativeIntegerMatcher,
pendingWithoutLaunchpad,
} from "../testutils.spec";
import { makeStdTx } from "../tx";
import { DistributionExtension, setupDistributionExtension } from "./distribution";
import { LcdClient } from "./lcdclient";

View File

@ -1,5 +1,14 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Coin, coins, makeCosmoshubPath, makeSignDoc, Secp256k1HdWallet, StdFee } from "@cosmjs/amino";
import {
Coin,
coins,
makeCosmoshubPath,
makeSignDoc,
makeStdTx,
Secp256k1HdWallet,
StdFee,
StdTx,
} from "@cosmjs/amino";
import { assert, sleep } from "@cosmjs/utils";
import { isBroadcastTxFailure } from "../cosmosclient";
@ -17,7 +26,7 @@ import {
tendermintIdMatcher,
unused,
} from "../testutils.spec";
import { isWrappedStdTx, makeStdTx, StdTx } from "../tx";
import { isWrappedStdTx } from "../tx";
import { setupAuthExtension } from "./auth";
import { TxsResponse } from "./base";
import { LcdApiArray, LcdClient } from "./lcdclient";

View File

@ -1,8 +1,9 @@
/* eslint-disable no-dupe-class-members, @typescript-eslint/ban-types, @typescript-eslint/naming-convention */
import { StdTx } from "@cosmjs/amino";
import { assert, isNonNullObject } from "@cosmjs/utils";
import axios, { AxiosError, AxiosInstance } from "axios";
import { StdTx, WrappedStdTx } from "../tx";
import { WrappedStdTx } from "../tx";
import {
BlockResponse,
BroadcastMode,

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { coin, coins, makeSignDoc, Secp256k1HdWallet } from "@cosmjs/amino";
import { coin, coins, makeSignDoc, makeStdTx, Secp256k1HdWallet } from "@cosmjs/amino";
import { assert, sleep } from "@cosmjs/utils";
import { assertIsBroadcastTxSuccess } from "../cosmosclient";
@ -14,7 +14,6 @@ import {
nonNegativeIntegerMatcher,
pendingWithoutLaunchpad,
} from "../testutils.spec";
import { makeStdTx } from "../tx";
import { LcdClient } from "./lcdclient";
import { BondStatus, setupStakingExtension, StakingExtension } from "./staking";

View File

@ -1,12 +1,11 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { AminoMsg, Coin, makeSignDoc, OfflineAminoSigner, StdFee } from "@cosmjs/amino";
import { AminoMsg, Coin, makeSignDoc, makeStdTx, OfflineAminoSigner, StdFee, StdTx } from "@cosmjs/amino";
import equals from "fast-deep-equal";
import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient";
import { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./fee";
import { BroadcastMode } from "./lcdapi";
import { MsgSend } from "./msgs";
import { makeStdTx, StdTx } from "./tx";
/**
* These fees are used by the higher level methods of SigningCosmosClient

View File

@ -1,54 +0,0 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { coins, StdFee, StdSignature } from "@cosmjs/amino";
import { makeSignDoc } from "@cosmjs/amino/build/signdoc";
import { makeStdTx } from "./tx";
describe("tx", () => {
describe("makeStdTx", () => {
it("can make an StdTx from a SignDoc and one signature", () => {
const fee: StdFee = { amount: coins(123, "ucosm"), gas: "22" };
const signDoc = makeSignDoc([], fee, "chain-xy", "hello", 3, 4);
const signature: StdSignature = {
pub_key: {
type: "tendermint/PubKeySecp256k1",
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",
},
signature: "1nUcIH0CLT0/nQ0mBTDrT6kMG20NY/PsH7P2gc4bpYNGLEYjBmdWevXUJouSE/9A/60QG9cYeqyTe5kFDeIPxQ==",
};
const signedTx = makeStdTx(signDoc, signature);
expect(signedTx).toEqual({
msg: [],
memo: "hello",
fee: fee,
signatures: [signature],
});
});
it("can make an StdTx from a SignDoc and multiple signatures", () => {
const fee: StdFee = { amount: coins(123, "ucosm"), gas: "22" };
const signDoc = makeSignDoc([], fee, "chain-xy", "hello", 3, 4);
const signature1: StdSignature = {
pub_key: {
type: "tendermint/PubKeySecp256k1",
value: "AtQaCqFnshaZQp6rIkvAPyzThvCvXSDO+9AzbxVErqJP",
},
signature: "1nUcIH0CLT0/nQ0mBTDrT6kMG20NY/PsH7P2gc4bpYNGLEYjBmdWevXUJouSE/9A/60QG9cYeqyTe5kFDeIPxQ==",
};
const signature2: StdSignature = {
pub_key: {
type: "tendermint/PubKeySecp256k1",
value: "A5qFcJBJvEK/fOmEAY0DHNWwSRZ9TEfNZyH8VoVvDtAq",
},
signature: "NK1Oy4EUGAsoC03c1wi9GG03JC/39LEdautC5Jk643oIbEPqeXHMwaqbdvO/Jws0X/NAXaN8SAy2KNY5Qml+5Q==",
};
const signedTx = makeStdTx(signDoc, [signature1, signature2]);
expect(signedTx).toEqual({
msg: [],
memo: "hello",
fee: fee,
signatures: [signature1, signature2],
});
});
});
});

View File

@ -1,35 +1,4 @@
import { AminoMsg, StdFee, StdSignature, StdSignDoc } from "@cosmjs/amino";
/**
* A Cosmos SDK StdTx
*
* @see https://docs.cosmos.network/master/modules/auth/03_types.html#stdtx
*/
export interface StdTx {
readonly msg: readonly AminoMsg[];
readonly fee: StdFee;
readonly signatures: readonly StdSignature[];
readonly memo: string | undefined;
}
export function isStdTx(txValue: unknown): txValue is StdTx {
const { memo, msg, fee, signatures } = txValue as StdTx;
return (
typeof memo === "string" && Array.isArray(msg) && typeof fee === "object" && Array.isArray(signatures)
);
}
export function makeStdTx(
content: Pick<StdSignDoc, "msgs" | "fee" | "memo">,
signatures: StdSignature | readonly StdSignature[],
): StdTx {
return {
msg: content.msgs,
fee: content.fee,
memo: content.memo,
signatures: Array.isArray(signatures) ? signatures : [signatures],
};
}
import { isStdTx, StdTx } from "@cosmjs/amino";
/**
* An Amino JSON wrapper around the Tx interface