Test wasm upload in RestServer
This commit is contained in:
parent
8034ae7bc6
commit
28a4518edf
@ -42,5 +42,7 @@
|
||||
"axios": "^0.19.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
"@iov/bcp": "^2.0.0-alpha.7",
|
||||
"@iov/keycontrol": "^2.0.0-alpha.7"
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,14 +1,23 @@
|
||||
/* eslint-disable @typescript-eslint/camelcase */
|
||||
import { ChainId, PrehashType, SignableBytes } from "@iov/bcp";
|
||||
import { Secp256k1 } from "@iov/crypto";
|
||||
import { Encoding } from "@iov/encoding";
|
||||
import { HdPaths, Secp256k1HdWallet } from "@iov/keycontrol";
|
||||
import { types } from "src";
|
||||
|
||||
import { marshalTx, sortJson } from "./encoding";
|
||||
import { RestClient } from "./restclient";
|
||||
import contract from "./testdata/contract.json";
|
||||
import data from "./testdata/cosmoshub.json";
|
||||
import { StdTx } from "./types";
|
||||
import { StdSignature, StdTx } from "./types";
|
||||
|
||||
const { fromBase64 } = Encoding;
|
||||
const { fromBase64, toBase64, toUtf8 } = Encoding;
|
||||
|
||||
const httpUrl = "http://localhost:1317";
|
||||
const defaultNetworkId = "testing";
|
||||
const faucetMnemonic =
|
||||
"economy stock theory fatal elder harbor betray wasp final emotion task crumble siren bottom lizard educate guess current outdoor pair theory focus wife stone";
|
||||
const faucetPath = HdPaths.cosmos(0);
|
||||
const faucetAddress = "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6";
|
||||
|
||||
function pendingWithoutCosmos(): void {
|
||||
@ -51,4 +60,73 @@ describe("RestClient", () => {
|
||||
expect(await client.encodeTx(tx)).toEqual(fromBase64(data.tx_data));
|
||||
});
|
||||
});
|
||||
|
||||
describe("post", () => {
|
||||
it("can upload wasm", async () => {
|
||||
pendingWithoutCosmos();
|
||||
const wallet = Secp256k1HdWallet.fromMnemonic(faucetMnemonic);
|
||||
const signer = await wallet.createIdentity("abc" as ChainId, faucetPath);
|
||||
|
||||
const memo = "My first contract on chain";
|
||||
const theMsg: types.Msg = {
|
||||
type: "wasm/store-code",
|
||||
value: {
|
||||
sender: faucetAddress,
|
||||
wasm_byte_code: contract.data,
|
||||
source: "https://mycoderepo.example/134",
|
||||
builder: "v0.0.1",
|
||||
},
|
||||
};
|
||||
|
||||
const unsigned: StdTx = {
|
||||
msg: [theMsg],
|
||||
memo: memo,
|
||||
signatures: [],
|
||||
fee: {
|
||||
amount: [
|
||||
{
|
||||
amount: "5000",
|
||||
denom: "ucosm",
|
||||
},
|
||||
],
|
||||
gas: "200000",
|
||||
},
|
||||
};
|
||||
|
||||
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 signature = await wallet.createTransactionSignature(signer, signBytes, PrehashType.Sha256);
|
||||
const fullSignature: StdSignature = {
|
||||
pub_key: {
|
||||
type: "tendermint/PubKeySecp256k1",
|
||||
value: toBase64(Secp256k1.compressPubkey(signer.pubkey.data)),
|
||||
},
|
||||
// Recovery seems to be unused
|
||||
signature: toBase64(Secp256k1.trimRecoveryByte(signature)),
|
||||
};
|
||||
|
||||
const tx: StdTx = {
|
||||
msg: unsigned.msg,
|
||||
fee: unsigned.fee,
|
||||
memo: memo,
|
||||
signatures: [fullSignature],
|
||||
};
|
||||
|
||||
const postableBytes = marshalTx(tx);
|
||||
const result = await client.postTx(postableBytes);
|
||||
// console.log("Raw log:", result.raw_log);
|
||||
expect(result.code).toBeFalsy();
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
4
packages/sdk/src/testdata/contract.json
vendored
Normal file
4
packages/sdk/src/testdata/contract.json
vendored
Normal file
File diff suppressed because one or more lines are too long
@ -25,7 +25,7 @@ export function isAminoStdTx(txValue: unknown): txValue is StdTx {
|
||||
export interface Msg {
|
||||
readonly type: string;
|
||||
// TODO: make better union type
|
||||
readonly value: MsgSend | unknown;
|
||||
readonly value: MsgSend | MsgStoreCode | unknown;
|
||||
}
|
||||
|
||||
export interface MsgSend {
|
||||
@ -36,6 +36,17 @@ export interface MsgSend {
|
||||
readonly amount: ReadonlyArray<Coin>;
|
||||
}
|
||||
|
||||
export interface MsgStoreCode {
|
||||
/** Bech32 account address */
|
||||
readonly sender: string;
|
||||
/** Base64 encoded Wasm */
|
||||
readonly wasm_byte_code: string;
|
||||
/** A valid URI reference to the contract's source code, optional */
|
||||
readonly source?: string;
|
||||
/** A docker tag, optional */
|
||||
readonly builder?: string;
|
||||
}
|
||||
|
||||
export interface StdFee {
|
||||
readonly amount: ReadonlyArray<Coin>;
|
||||
readonly gas: string;
|
||||
|
||||
12
packages/sdk/types/types.d.ts
vendored
12
packages/sdk/types/types.d.ts
vendored
@ -14,7 +14,7 @@ export declare type AminoTx = Tx & {
|
||||
export declare function isAminoStdTx(txValue: unknown): txValue is StdTx;
|
||||
export interface Msg {
|
||||
readonly type: string;
|
||||
readonly value: MsgSend | unknown;
|
||||
readonly value: MsgSend | MsgStoreCode | unknown;
|
||||
}
|
||||
export interface MsgSend {
|
||||
/** Bech32 account address */
|
||||
@ -23,6 +23,16 @@ export interface MsgSend {
|
||||
readonly to_address: string;
|
||||
readonly amount: ReadonlyArray<Coin>;
|
||||
}
|
||||
export interface MsgStoreCode {
|
||||
/** Bech32 account address */
|
||||
readonly sender: string;
|
||||
/** Base64 encoded Wasm */
|
||||
readonly wasm_byte_code: string;
|
||||
/** A valid URI reference to the contract's source code, optional */
|
||||
readonly source?: string;
|
||||
/** A docker tag, optional */
|
||||
readonly builder?: string;
|
||||
}
|
||||
export interface StdFee {
|
||||
readonly amount: ReadonlyArray<Coin>;
|
||||
readonly gas: string;
|
||||
|
||||
Loading…
Reference in New Issue
Block a user