From f3a413f62a6ea111c74a2de6e70c1e43e00dd6ce Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 4 Feb 2020 15:28:14 +0100 Subject: [PATCH] Cleanup test code --- packages/sdk/src/encoding.ts | 2 +- packages/sdk/src/restclient.spec.ts | 60 ++++++++++------------------- packages/sdk/types/encoding.d.ts | 1 - 3 files changed, 21 insertions(+), 42 deletions(-) diff --git a/packages/sdk/src/encoding.ts b/packages/sdk/src/encoding.ts index 62c17d32..88844d26 100644 --- a/packages/sdk/src/encoding.ts +++ b/packages/sdk/src/encoding.ts @@ -5,7 +5,7 @@ import { Msg, NonceInfo, StdFee, StdSignature, StdTx } from "./types"; const { toBase64, toUtf8 } = Encoding; -export function sortJson(json: any): any { +function sortJson(json: any): any { if (typeof json !== "object" || json === null) { return json; } diff --git a/packages/sdk/src/restclient.spec.ts b/packages/sdk/src/restclient.spec.ts index 6bcff98a..ade652b4 100644 --- a/packages/sdk/src/restclient.spec.ts +++ b/packages/sdk/src/restclient.spec.ts @@ -3,13 +3,13 @@ import { ChainId, PrehashType, SignableBytes } from "@iov/bcp"; import { Encoding } from "@iov/encoding"; import { HdPaths, Secp256k1HdWallet } from "@iov/keycontrol"; -import { encodeSecp256k1Signature, makeSignBytes, marshalTx, sortJson } from "./encoding"; +import { encodeSecp256k1Signature, makeSignBytes, marshalTx } from "./encoding"; import { RestClient } from "./restclient"; import contract from "./testdata/contract.json"; import data from "./testdata/cosmoshub.json"; import { MsgSend, MsgStoreCode, StdFee, StdTx } from "./types"; -const { fromBase64, toUtf8 } = Encoding; +const { fromBase64 } = Encoding; const httpUrl = "http://localhost:1317"; const defaultNetworkId = "testing"; @@ -81,45 +81,31 @@ describe("RestClient", () => { }, }; - const unsigned: StdTx = { - msg: [theMsg], - memo: memo, - signatures: [], - fee: { - amount: [ - { - amount: "5000", - denom: "ucosm", - }, - ], - gas: "890000", - }, + const fee: StdFee = { + amount: [ + { + amount: "5000", + denom: "ucosm", + }, + ], + gas: "890000", }; const client = new RestClient(httpUrl); const account = (await client.authAccounts(faucetAddress)).result.value; - const signMsg = sortJson({ - account_number: account.account_number.toString(), - chain_id: defaultNetworkId, - fee: unsigned.fee, - memo: memo, - msgs: unsigned.msg, - sequence: account.sequence.toString(), - }); - - const signBytes = toUtf8(JSON.stringify(signMsg)) as SignableBytes; + const signBytes = makeSignBytes([theMsg], fee, defaultNetworkId, memo, account) as SignableBytes; const rawSignature = await wallet.createTransactionSignature(signer, signBytes, PrehashType.Sha256); const signature = encodeSecp256k1Signature(signer.pubkey.data, rawSignature); - const tx: StdTx = { - msg: unsigned.msg, - fee: unsigned.fee, + const signedTx: StdTx = { + msg: [theMsg], + fee: fee, memo: memo, signatures: [signature], }; - const postableBytes = marshalTx(tx); + const postableBytes = marshalTx(signedTx); const result = await client.postTx(postableBytes); // console.log("Raw log:", result.raw_log); expect(result.code).toBeFalsy(); @@ -136,14 +122,14 @@ describe("RestClient", () => { value: { sender: faucetAddress, wasm_byte_code: contract.data, - source: "https://mycoderepo.example/134", - builder: "v0.0.1", + source: "https://github.com/confio/cosmwasm/raw/0.7/lib/vm/testdata/contract_0.6.wasm", + builder: "cosmwasm-opt:0.6.2", }, }; const fee: StdFee = { amount: [ { - amount: "5000", + amount: "5000000", denom: "ucosm", }, ], @@ -156,20 +142,14 @@ describe("RestClient", () => { const rawSignature = await wallet.createTransactionSignature(signer, signBytes, PrehashType.Sha256); const signature = encodeSecp256k1Signature(signer.pubkey.data, rawSignature); - const tx: StdTx = { + const signedTx: StdTx = { msg: [theMsg], fee: fee, memo: memo, signatures: [signature], }; - // make sure this is a valid encoding/decoding - const aminoBytes = await client.encodeTx(tx); - expect(aminoBytes).toBeTruthy(); - expect(aminoBytes.length).toEqual(63084); - - // now submit it - const postableBytes = marshalTx(tx); + const postableBytes = marshalTx(signedTx); const result = await client.postTx(postableBytes); // console.log("Raw log:", result.raw_log); expect(result.code).toBeFalsy(); diff --git a/packages/sdk/types/encoding.d.ts b/packages/sdk/types/encoding.d.ts index 69a1f7a6..2ed63061 100644 --- a/packages/sdk/types/encoding.d.ts +++ b/packages/sdk/types/encoding.d.ts @@ -1,5 +1,4 @@ import { Msg, NonceInfo, StdFee, StdSignature, StdTx } from "./types"; -export declare function sortJson(json: any): any; export declare function marshalTx(tx: StdTx): Uint8Array; export declare function makeSignBytes( msgs: readonly Msg[],