diff --git a/CHANGELOG.md b/CHANGELOG.md index aa57b47b..7665af03 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/packages/amino/src/index.ts b/packages/amino/src/index.ts index 42d9720c..6eca28c0 100644 --- a/packages/amino/src/index.ts +++ b/packages/amino/src/index.ts @@ -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"; diff --git a/packages/amino/src/stdtx.spec.ts b/packages/amino/src/stdtx.spec.ts new file mode 100644 index 00000000..4f194e89 --- /dev/null +++ b/packages/amino/src/stdtx.spec.ts @@ -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], + }); + }); +}); diff --git a/packages/amino/src/stdtx.ts b/packages/amino/src/stdtx.ts new file mode 100644 index 00000000..2948e1fa --- /dev/null +++ b/packages/amino/src/stdtx.ts @@ -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, + signatures: StdSignature | readonly StdSignature[], +): StdTx { + return { + msg: content.msgs, + fee: content.fee, + memo: content.memo, + signatures: Array.isArray(signatures) ? signatures : [signatures], + }; +} diff --git a/packages/launchpad/src/cosmosclient.searchtx.spec.ts b/packages/launchpad/src/cosmosclient.searchtx.spec.ts index d4e4c197..0262b561 100644 --- a/packages/launchpad/src/cosmosclient.searchtx.spec.ts +++ b/packages/launchpad/src/cosmosclient.searchtx.spec.ts @@ -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; diff --git a/packages/launchpad/src/cosmosclient.spec.ts b/packages/launchpad/src/cosmosclient.spec.ts index e19b848f..0f955cd7 100644 --- a/packages/launchpad/src/cosmosclient.spec.ts +++ b/packages/launchpad/src/cosmosclient.spec.ts @@ -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 diff --git a/packages/launchpad/src/cosmosclient.ts b/packages/launchpad/src/cosmosclient.ts index 25fcce92..79e0f93c 100644 --- a/packages/launchpad/src/cosmosclient.ts +++ b/packages/launchpad/src/cosmosclient.ts @@ -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; diff --git a/packages/launchpad/src/index.ts b/packages/launchpad/src/index.ts index 210f8e40..28b62910 100644 --- a/packages/launchpad/src/index.ts +++ b/packages/launchpad/src/index.ts @@ -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"; diff --git a/packages/launchpad/src/lcdapi/distribution.spec.ts b/packages/launchpad/src/lcdapi/distribution.spec.ts index c2d320e8..95a487db 100644 --- a/packages/launchpad/src/lcdapi/distribution.spec.ts +++ b/packages/launchpad/src/lcdapi/distribution.spec.ts @@ -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"; diff --git a/packages/launchpad/src/lcdapi/lcdclient.spec.ts b/packages/launchpad/src/lcdapi/lcdclient.spec.ts index a06086fe..8499ec1d 100644 --- a/packages/launchpad/src/lcdapi/lcdclient.spec.ts +++ b/packages/launchpad/src/lcdapi/lcdclient.spec.ts @@ -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"; diff --git a/packages/launchpad/src/lcdapi/lcdclient.ts b/packages/launchpad/src/lcdapi/lcdclient.ts index 0ed7f40e..69913362 100644 --- a/packages/launchpad/src/lcdapi/lcdclient.ts +++ b/packages/launchpad/src/lcdapi/lcdclient.ts @@ -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, diff --git a/packages/launchpad/src/lcdapi/staking.spec.ts b/packages/launchpad/src/lcdapi/staking.spec.ts index 90ff26f9..6db3e4f5 100644 --- a/packages/launchpad/src/lcdapi/staking.spec.ts +++ b/packages/launchpad/src/lcdapi/staking.spec.ts @@ -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"; diff --git a/packages/launchpad/src/signingcosmosclient.ts b/packages/launchpad/src/signingcosmosclient.ts index b1e5f877..42c6e4c9 100644 --- a/packages/launchpad/src/signingcosmosclient.ts +++ b/packages/launchpad/src/signingcosmosclient.ts @@ -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 diff --git a/packages/launchpad/src/tx.spec.ts b/packages/launchpad/src/tx.spec.ts deleted file mode 100644 index 9fec53b4..00000000 --- a/packages/launchpad/src/tx.spec.ts +++ /dev/null @@ -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], - }); - }); - }); -}); diff --git a/packages/launchpad/src/tx.ts b/packages/launchpad/src/tx.ts index 57bf5232..16f97072 100644 --- a/packages/launchpad/src/tx.ts +++ b/packages/launchpad/src/tx.ts @@ -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, - 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