Test instantiate with Amino JSON signing and fix message encoding

This commit is contained in:
Simon Warta 2021-10-12 11:26:17 +02:00
parent 22e0d32aa3
commit a6a5d8e2fd
3 changed files with 47 additions and 12 deletions

View File

@ -1,5 +1,5 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { fromBase64, toUtf8 } from "@cosmjs/encoding";
import { fromBase64, toBase64, toUtf8 } from "@cosmjs/encoding";
import { AminoTypes, coins } from "@cosmjs/stargate";
import {
MsgClearAdmin,
@ -64,7 +64,7 @@ describe("AminoTypes", () => {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: { foo: "bar" },
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
funds: coins(1234, "ucosm"),
admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
},
@ -92,7 +92,7 @@ describe("AminoTypes", () => {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: { foo: "bar" },
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
funds: coins(1234, "ucosm"),
admin: undefined,
},
@ -230,7 +230,7 @@ describe("AminoTypes", () => {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: { foo: "bar" },
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
funds: coins(1234, "ucosm"),
admin: "cosmos10dyr9899g6t0pelew4nvf4j5c3jcgv0r73qga5",
},
@ -258,7 +258,7 @@ describe("AminoTypes", () => {
sender: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
code_id: "12345",
label: "sticky",
msg: { foo: "bar" },
msg: toBase64(toUtf8(`{"foo":"bar"}`)),
funds: coins(1234, "ucosm"),
},
};

View File

@ -65,8 +65,8 @@ export interface AminoMsgInstantiateContract {
readonly code_id: string;
/** Human-readable label for this contract */
readonly label: string;
/** Instantiate message as JavaScript object */
readonly msg: any;
/** Instantiate message as base64 encoded JSON */
readonly msg: string;
readonly funds: readonly Coin[];
/** Bech32-encoded admin address */
readonly admin?: string;
@ -150,7 +150,7 @@ export const cosmWasmTypes: Record<string, AminoConverter> = {
sender: sender,
code_id: codeId.toString(),
label: label,
msg: JSON.parse(fromUtf8(msg)),
msg: toBase64(msg),
funds: funds,
admin: admin || undefined,
}),
@ -165,7 +165,7 @@ export const cosmWasmTypes: Record<string, AminoConverter> = {
sender: sender,
codeId: Long.fromString(code_id),
label: label,
msg: toUtf8(JSON.stringify(msg)),
msg: fromBase64(msg),
funds: [...funds],
admin: admin ?? "",
}),

View File

@ -34,6 +34,7 @@ import {
defaultSigningClientOptions,
defaultUpdateAdminFee,
defaultUploadFee,
deployedHackatom,
getHackatom,
makeRandomAddress,
makeWasmClient,
@ -150,7 +151,7 @@ describe("SigningCosmWasmClient", () => {
const options = { ...defaultSigningClientOptions, prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
const { codeId } = await client.upload(alice.address0, getHackatom().data, defaultUploadFee);
const contractAddress1 = await client.instantiate(
const { contractAddress: address1 } = await client.instantiate(
alice.address0,
codeId,
{
@ -160,7 +161,7 @@ describe("SigningCosmWasmClient", () => {
"contract 1",
defaultInstantiateFee,
);
const contractAddress2 = await client.instantiate(
const { contractAddress: address2 } = await client.instantiate(
alice.address0,
codeId,
{
@ -170,7 +171,41 @@ describe("SigningCosmWasmClient", () => {
"contract 2",
defaultInstantiateFee,
);
expect(contractAddress1).not.toEqual(contractAddress2);
expect(address1).not.toEqual(address2);
client.disconnect();
});
it("works with legacy Amino signer", async () => {
pendingWithoutWasmd();
const wallet = await Secp256k1HdWallet.fromMnemonic(alice.mnemonic, { prefix: wasmd.prefix });
const options = { ...defaultSigningClientOptions, prefix: wasmd.prefix };
const client = await SigningCosmWasmClient.connectWithSigner(wasmd.endpoint, wallet, options);
// With admin
await client.instantiate(
alice.address0,
deployedHackatom.codeId,
{
verifier: alice.address0,
beneficiary: makeRandomAddress(),
},
"contract 1",
defaultInstantiateFee,
{ admin: makeRandomAddress() },
);
// Without admin
await client.instantiate(
alice.address0,
deployedHackatom.codeId,
{
verifier: alice.address0,
beneficiary: makeRandomAddress(),
},
"contract 1",
defaultInstantiateFee,
);
client.disconnect();
});
});