From 772a23c5ae201906ec4c4076c6799f9b147d8e13 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Wed, 24 Mar 2021 15:39:15 +0100 Subject: [PATCH] launchpad: Use SignDoc helpers from amino --- .../src/cosmosclient.searchtx.spec.ts | 2 +- packages/launchpad/src/cosmosclient.spec.ts | 3 +- packages/launchpad/src/encoding.spec.ts | 129 ------------------ packages/launchpad/src/encoding.ts | 48 ------- packages/launchpad/src/index.ts | 3 +- .../launchpad/src/lcdapi/distribution.spec.ts | 2 +- packages/launchpad/src/lcdapi/gov.spec.ts | 2 +- .../launchpad/src/lcdapi/lcdclient.spec.ts | 3 +- packages/launchpad/src/lcdapi/staking.spec.ts | 2 +- .../launchpad/src/secp256k1hdwallet.spec.ts | 3 +- packages/launchpad/src/secp256k1hdwallet.ts | 2 +- .../launchpad/src/secp256k1wallet.spec.ts | 3 +- packages/launchpad/src/secp256k1wallet.ts | 3 +- packages/launchpad/src/sequence.ts | 3 +- packages/launchpad/src/signingcosmosclient.ts | 4 +- packages/launchpad/src/tx.spec.ts | 2 +- 16 files changed, 16 insertions(+), 198 deletions(-) delete mode 100644 packages/launchpad/src/encoding.spec.ts delete mode 100644 packages/launchpad/src/encoding.ts diff --git a/packages/launchpad/src/cosmosclient.searchtx.spec.ts b/packages/launchpad/src/cosmosclient.searchtx.spec.ts index c127c166..dc643b12 100644 --- a/packages/launchpad/src/cosmosclient.searchtx.spec.ts +++ b/packages/launchpad/src/cosmosclient.searchtx.spec.ts @@ -1,9 +1,9 @@ /* eslint-disable @typescript-eslint/naming-convention */ +import { makeSignDoc } from "@cosmjs/amino"; import { assert, sleep } from "@cosmjs/utils"; import { coins } from "./coins"; import { CosmosClient, isBroadcastTxFailure } from "./cosmosclient"; -import { makeSignDoc } from "./encoding"; import { LcdClient } from "./lcdapi"; import { isMsgSend, MsgSend } from "./msgs"; import { Secp256k1HdWallet } from "./secp256k1hdwallet"; diff --git a/packages/launchpad/src/cosmosclient.spec.ts b/packages/launchpad/src/cosmosclient.spec.ts index 56d7933f..db00aa23 100644 --- a/packages/launchpad/src/cosmosclient.spec.ts +++ b/packages/launchpad/src/cosmosclient.spec.ts @@ -1,10 +1,9 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { StdFee } from "@cosmjs/amino"; +import { makeSignDoc, StdFee } from "@cosmjs/amino"; import { assert, sleep } from "@cosmjs/utils"; import { ReadonlyDate } from "readonly-date"; import { assertIsBroadcastTxSuccess, CosmosClient, PrivateCosmosClient } from "./cosmosclient"; -import { makeSignDoc } from "./encoding"; import { findAttribute } from "./logs"; import { MsgSend } from "./msgs"; import { Secp256k1HdWallet } from "./secp256k1hdwallet"; diff --git a/packages/launchpad/src/encoding.spec.ts b/packages/launchpad/src/encoding.spec.ts deleted file mode 100644 index e58bb654..00000000 --- a/packages/launchpad/src/encoding.spec.ts +++ /dev/null @@ -1,129 +0,0 @@ -/* eslint-disable @typescript-eslint/naming-convention */ -import { coin, coins } from "./coins"; -import { makeSignDoc, sortedJsonStringify } from "./encoding"; -import { MsgDelegate, MsgSend } from "./msgs"; -import { faucet, launchpad, makeRandomAddress } from "./testutils.spec"; - -describe("encoding", () => { - describe("sortedJsonStringify", () => { - it("leaves non-objects unchanged", () => { - expect(sortedJsonStringify(true)).toEqual(`true`); - expect(sortedJsonStringify(false)).toEqual(`false`); - expect(sortedJsonStringify("aabbccdd")).toEqual(`"aabbccdd"`); - expect(sortedJsonStringify(75)).toEqual(`75`); - expect(sortedJsonStringify(null)).toEqual(`null`); - expect(sortedJsonStringify([5, 6, 7, 1])).toEqual(`[5,6,7,1]`); - expect(sortedJsonStringify([5, ["a", "b"], true, null, 1])).toEqual(`[5,["a","b"],true,null,1]`); - }); - - it("sorts objects by key", () => { - // already sorted - expect(sortedJsonStringify({})).toEqual(`{}`); - expect(sortedJsonStringify({ a: 3 })).toEqual(`{"a":3}`); - expect(sortedJsonStringify({ a: 3, b: 2, c: 1 })).toEqual(`{"a":3,"b":2,"c":1}`); - - // not yet sorted - expect(sortedJsonStringify({ b: 2, a: 3, c: 1 })).toEqual(`{"a":3,"b":2,"c":1}`); - expect(sortedJsonStringify({ aaa: true, aa: true, a: true })).toEqual( - `{"a":true,"aa":true,"aaa":true}`, - ); - }); - - it("sorts nested objects", () => { - // already sorted - expect(sortedJsonStringify({ x: { y: { z: null } } })).toEqual(`{"x":{"y":{"z":null}}}`); - - // not yet sorted - expect(sortedJsonStringify({ b: { z: true, x: true, y: true }, a: true, c: true })).toEqual( - `{"a":true,"b":{"x":true,"y":true,"z":true},"c":true}`, - ); - }); - - it("sorts objects in arrays", () => { - // already sorted - expect(sortedJsonStringify([1, 2, { x: { y: { z: null } } }, 4])).toEqual( - `[1,2,{"x":{"y":{"z":null}}},4]`, - ); - - // not yet sorted - expect(sortedJsonStringify([1, 2, { b: { z: true, x: true, y: true }, a: true, c: true }, 4])).toEqual( - `[1,2,{"a":true,"b":{"x":true,"y":true,"z":true},"c":true},4]`, - ); - }); - }); - - describe("makeSignDoc", () => { - it("works", () => { - const chainId = "testspace-12"; - const msg1: MsgDelegate = { - type: "cosmos-sdk/MsgDelegate", - value: { - delegator_address: faucet.address0, - validator_address: launchpad.validator.address, - amount: coin(1234, "ustake"), - }, - }; - const msg2: MsgSend = { - type: "cosmos-sdk/MsgSend", - value: { - from_address: faucet.address0, - to_address: makeRandomAddress(), - amount: coins(1234567, "ucosm"), - }, - }; - const fee = { - amount: coins(2000, "ucosm"), - gas: "180000", // 180k - }; - const memo = "Use your power wisely"; - const accountNumber = 15; - const sequence = 16; - - const signDoc = makeSignDoc([msg1, msg2], fee, chainId, memo, accountNumber, sequence); - expect(signDoc).toEqual({ - msgs: [msg1, msg2], - fee: fee, - chain_id: chainId, - account_number: accountNumber.toString(), - sequence: sequence.toString(), - memo: memo, - }); - }); - - it("works with undefined memo", () => { - const chainId = "testspace-12"; - const msg1: MsgDelegate = { - type: "cosmos-sdk/MsgDelegate", - value: { - delegator_address: faucet.address0, - validator_address: launchpad.validator.address, - amount: coin(1234, "ustake"), - }, - }; - const msg2: MsgSend = { - type: "cosmos-sdk/MsgSend", - value: { - from_address: faucet.address0, - to_address: makeRandomAddress(), - amount: coins(1234567, "ucosm"), - }, - }; - const fee = { - amount: coins(2000, "ucosm"), - gas: "180000", // 180k - }; - const accountNumber = 15; - const sequence = 16; - - const signDoc = makeSignDoc([msg1, msg2], fee, chainId, undefined, accountNumber, sequence); - expect(signDoc).toEqual({ - msgs: [msg1, msg2], - fee: fee, - chain_id: chainId, - account_number: accountNumber.toString(), - sequence: sequence.toString(), - memo: "", - }); - }); - }); -}); diff --git a/packages/launchpad/src/encoding.ts b/packages/launchpad/src/encoding.ts deleted file mode 100644 index 00690962..00000000 --- a/packages/launchpad/src/encoding.ts +++ /dev/null @@ -1,48 +0,0 @@ -/* eslint-disable @typescript-eslint/naming-convention */ -import { AminoMsg, StdFee, StdSignDoc } from "@cosmjs/amino"; -import { toUtf8 } from "@cosmjs/encoding"; -import { Uint53 } from "@cosmjs/math"; - -function sortedObject(obj: any): any { - if (typeof obj !== "object" || obj === null) { - return obj; - } - if (Array.isArray(obj)) { - return obj.map(sortedObject); - } - const sortedKeys = Object.keys(obj).sort(); - const result: Record = {}; - // NOTE: Use forEach instead of reduce for performance with large objects eg Wasm code - sortedKeys.forEach((key) => { - result[key] = sortedObject(obj[key]); - }); - return result; -} - -/** Returns a JSON string with objects sorted by key */ -// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types -export function sortedJsonStringify(obj: any): string { - return JSON.stringify(sortedObject(obj)); -} - -export function makeSignDoc( - msgs: readonly AminoMsg[], - fee: StdFee, - chainId: string, - memo: string | undefined, - accountNumber: number | string, - sequence: number | string, -): StdSignDoc { - return { - chain_id: chainId, - account_number: Uint53.fromString(accountNumber.toString()).toString(), - sequence: Uint53.fromString(sequence.toString()).toString(), - fee: fee, - msgs: msgs, - memo: memo || "", - }; -} - -export function serializeSignDoc(signDoc: StdSignDoc): Uint8Array { - return toUtf8(sortedJsonStringify(signDoc)); -} diff --git a/packages/launchpad/src/index.ts b/packages/launchpad/src/index.ts index 0cd39b82..7d6219fb 100644 --- a/packages/launchpad/src/index.ts +++ b/packages/launchpad/src/index.ts @@ -16,8 +16,10 @@ export { encodeBech32Pubkey, encodeSecp256k1Pubkey, encodeSecp256k1Signature, + makeSignDoc, pubkeyToAddress, pubkeyType, + serializeSignDoc, } from "@cosmjs/amino"; import { SinglePubkey } from "@cosmjs/amino"; /** @deprecated PubKey is deprecated. Use `SinglePubkey` or the more general `Pubkey` from `@cosmjs/amino`. */ @@ -49,7 +51,6 @@ export { isSearchBySentFromOrToQuery, isSearchByTagsQuery, } from "./cosmosclient"; -export { makeSignDoc, serializeSignDoc } from "./encoding"; export { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./fee"; export { AuthAccountsResponse, diff --git a/packages/launchpad/src/lcdapi/distribution.spec.ts b/packages/launchpad/src/lcdapi/distribution.spec.ts index 039417b5..667219ad 100644 --- a/packages/launchpad/src/lcdapi/distribution.spec.ts +++ b/packages/launchpad/src/lcdapi/distribution.spec.ts @@ -1,10 +1,10 @@ /* eslint-disable @typescript-eslint/naming-convention */ +import { makeSignDoc } from "@cosmjs/amino"; import { Bech32 } from "@cosmjs/encoding"; import { sleep } from "@cosmjs/utils"; import { coin, coins } from "../coins"; import { assertIsBroadcastTxSuccess } from "../cosmosclient"; -import { makeSignDoc } from "../encoding"; import { MsgDelegate } from "../msgs"; import { Secp256k1HdWallet } from "../secp256k1hdwallet"; import { SigningCosmosClient } from "../signingcosmosclient"; diff --git a/packages/launchpad/src/lcdapi/gov.spec.ts b/packages/launchpad/src/lcdapi/gov.spec.ts index bc28eade..f907c888 100644 --- a/packages/launchpad/src/lcdapi/gov.spec.ts +++ b/packages/launchpad/src/lcdapi/gov.spec.ts @@ -1,9 +1,9 @@ /* eslint-disable @typescript-eslint/naming-convention */ +import { makeSignDoc } from "@cosmjs/amino"; import { sleep } from "@cosmjs/utils"; import { coins } from "../coins"; import { assertIsBroadcastTxSuccess } from "../cosmosclient"; -import { makeSignDoc } from "../encoding"; import { Secp256k1HdWallet } from "../secp256k1hdwallet"; import { SigningCosmosClient } from "../signingcosmosclient"; import { diff --git a/packages/launchpad/src/lcdapi/lcdclient.spec.ts b/packages/launchpad/src/lcdapi/lcdclient.spec.ts index bc3e7d3c..d10cc24d 100644 --- a/packages/launchpad/src/lcdapi/lcdclient.spec.ts +++ b/packages/launchpad/src/lcdapi/lcdclient.spec.ts @@ -1,9 +1,8 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { Coin, StdFee } from "@cosmjs/amino"; +import { Coin, makeSignDoc, StdFee } from "@cosmjs/amino"; import { assert, sleep } from "@cosmjs/utils"; import { isBroadcastTxFailure } from "../cosmosclient"; -import { makeSignDoc } from "../encoding"; import { parseLogs } from "../logs"; import { MsgSend } from "../msgs"; import { makeCosmoshubPath } from "../paths"; diff --git a/packages/launchpad/src/lcdapi/staking.spec.ts b/packages/launchpad/src/lcdapi/staking.spec.ts index cd8c3d0c..6ec3ab1f 100644 --- a/packages/launchpad/src/lcdapi/staking.spec.ts +++ b/packages/launchpad/src/lcdapi/staking.spec.ts @@ -1,9 +1,9 @@ /* eslint-disable @typescript-eslint/naming-convention */ +import { makeSignDoc } from "@cosmjs/amino"; import { assert, sleep } from "@cosmjs/utils"; import { coin, coins } from "../coins"; import { assertIsBroadcastTxSuccess } from "../cosmosclient"; -import { makeSignDoc } from "../encoding"; import { MsgDelegate, MsgUndelegate } from "../msgs"; import { Secp256k1HdWallet } from "../secp256k1hdwallet"; import { SigningCosmosClient } from "../signingcosmosclient"; diff --git a/packages/launchpad/src/secp256k1hdwallet.spec.ts b/packages/launchpad/src/secp256k1hdwallet.spec.ts index 5ad11121..e64e5aaf 100644 --- a/packages/launchpad/src/secp256k1hdwallet.spec.ts +++ b/packages/launchpad/src/secp256k1hdwallet.spec.ts @@ -1,9 +1,8 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { StdSignDoc } from "@cosmjs/amino"; +import { serializeSignDoc, StdSignDoc } from "@cosmjs/amino"; import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto"; import { fromBase64, fromHex } from "@cosmjs/encoding"; -import { serializeSignDoc } from "./encoding"; import { extractKdfConfiguration, Secp256k1HdWallet } from "./secp256k1hdwallet"; import { base64Matcher } from "./testutils.spec"; import { executeKdf, KdfConfiguration } from "./wallet"; diff --git a/packages/launchpad/src/secp256k1hdwallet.ts b/packages/launchpad/src/secp256k1hdwallet.ts index 460ce990..956ad6aa 100644 --- a/packages/launchpad/src/secp256k1hdwallet.ts +++ b/packages/launchpad/src/secp256k1hdwallet.ts @@ -4,6 +4,7 @@ import { encodeSecp256k1Signature, OfflineAminoSigner, rawSecp256k1PubkeyToRawAddress, + serializeSignDoc, StdSignDoc, } from "@cosmjs/amino"; import { @@ -21,7 +22,6 @@ import { import { Bech32, fromBase64, fromUtf8, toBase64, toUtf8 } from "@cosmjs/encoding"; import { assert, isNonNullObject } from "@cosmjs/utils"; -import { serializeSignDoc } from "./encoding"; import { makeCosmoshubPath } from "./paths"; import { decrypt, diff --git a/packages/launchpad/src/secp256k1wallet.spec.ts b/packages/launchpad/src/secp256k1wallet.spec.ts index d074bbad..57f20687 100644 --- a/packages/launchpad/src/secp256k1wallet.spec.ts +++ b/packages/launchpad/src/secp256k1wallet.spec.ts @@ -1,9 +1,8 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { StdSignDoc } from "@cosmjs/amino"; +import { serializeSignDoc, StdSignDoc } from "@cosmjs/amino"; import { Secp256k1, Secp256k1Signature, Sha256 } from "@cosmjs/crypto"; import { fromBase64, fromHex } from "@cosmjs/encoding"; -import { serializeSignDoc } from "./encoding"; import { Secp256k1Wallet } from "./secp256k1wallet"; describe("Secp256k1Wallet", () => { diff --git a/packages/launchpad/src/secp256k1wallet.ts b/packages/launchpad/src/secp256k1wallet.ts index 4fbbcf48..3280189d 100644 --- a/packages/launchpad/src/secp256k1wallet.ts +++ b/packages/launchpad/src/secp256k1wallet.ts @@ -4,13 +4,12 @@ import { encodeSecp256k1Signature, OfflineAminoSigner, rawSecp256k1PubkeyToRawAddress, + serializeSignDoc, StdSignDoc, } from "@cosmjs/amino"; import { Secp256k1, Sha256 } from "@cosmjs/crypto"; import { Bech32 } from "@cosmjs/encoding"; -import { serializeSignDoc } from "./encoding"; - /** * A wallet that holds a single secp256k1 keypair. * diff --git a/packages/launchpad/src/sequence.ts b/packages/launchpad/src/sequence.ts index c833107a..c413f1c4 100644 --- a/packages/launchpad/src/sequence.ts +++ b/packages/launchpad/src/sequence.ts @@ -1,7 +1,6 @@ -import { decodeSignature } from "@cosmjs/amino"; +import { decodeSignature, makeSignDoc, serializeSignDoc } from "@cosmjs/amino"; import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto"; -import { makeSignDoc, serializeSignDoc } from "./encoding"; import { WrappedStdTx } from "./tx"; /** diff --git a/packages/launchpad/src/signingcosmosclient.ts b/packages/launchpad/src/signingcosmosclient.ts index 3572d748..1326e375 100644 --- a/packages/launchpad/src/signingcosmosclient.ts +++ b/packages/launchpad/src/signingcosmosclient.ts @@ -1,9 +1,9 @@ /* eslint-disable @typescript-eslint/naming-convention */ -import { AminoMsg, Coin, OfflineAminoSigner, StdFee } from "@cosmjs/amino"; +import { AminoMsg, Coin, makeSignDoc, OfflineAminoSigner, StdFee } from "@cosmjs/amino"; +import {} from "@cosmjs/amino/build/signdoc"; import equals from "fast-deep-equal"; import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient"; -import { makeSignDoc } from "./encoding"; import { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./fee"; import { BroadcastMode } from "./lcdapi"; import { MsgSend } from "./msgs"; diff --git a/packages/launchpad/src/tx.spec.ts b/packages/launchpad/src/tx.spec.ts index b79da7ca..bb6d11f7 100644 --- a/packages/launchpad/src/tx.spec.ts +++ b/packages/launchpad/src/tx.spec.ts @@ -1,8 +1,8 @@ /* eslint-disable @typescript-eslint/naming-convention */ import { StdFee, StdSignature } from "@cosmjs/amino"; +import { makeSignDoc } from "@cosmjs/amino/build/signdoc"; import { coins } from "./coins"; -import { makeSignDoc } from "./encoding"; import { makeStdTx } from "./tx"; describe("tx", () => {