proto-signing: Update tests for new codec
This commit is contained in:
parent
b06dedbc2e
commit
887850c4ad
@ -7,7 +7,7 @@ import { cosmos, google } from "./codec";
|
||||
import { cosmosField, registered } from "./decorator";
|
||||
import { Registry } from "./registry";
|
||||
|
||||
const { TxBody } = cosmos.tx;
|
||||
const { TxBody } = cosmos.tx.v1beta1;
|
||||
const { Any } = google.protobuf;
|
||||
|
||||
describe("decorator demo", () => {
|
||||
|
||||
@ -12,9 +12,9 @@ type MsgDemo = {
|
||||
readonly example: string;
|
||||
};
|
||||
|
||||
const { Coin } = cosmos;
|
||||
const { TxBody } = cosmos.tx;
|
||||
const { MsgSend } = cosmos.bank;
|
||||
const { Coin } = cosmos.base.v1beta1;
|
||||
const { TxBody } = cosmos.tx.v1beta1;
|
||||
const { MsgSend } = cosmos.bank.v1beta1;
|
||||
const { Any } = google.protobuf;
|
||||
|
||||
function getTypeName(typeUrl: string): string {
|
||||
@ -29,13 +29,13 @@ describe("protobuf demo", () => {
|
||||
amount: "1234567890",
|
||||
});
|
||||
const msgSend = MsgSend.create({
|
||||
fromAddress: Uint8Array.from(Array.from({ length: 20 }, () => 1)),
|
||||
toAddress: Uint8Array.from(Array.from({ length: 20 }, () => 2)),
|
||||
fromAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
|
||||
toAddress: "cosmos1qypqxpq9qcrsszg2pvxq6rs0zqg3yyc5lzv7xu",
|
||||
amount: [coin],
|
||||
});
|
||||
const msgSendBytes = MsgSend.encode(msgSend).finish();
|
||||
const msgSendWrapped = Any.create({
|
||||
type_url: "/cosmos.bank.MsgSend",
|
||||
type_url: "/cosmos.bank.v1beta1.MsgSend",
|
||||
value: msgSendBytes,
|
||||
});
|
||||
const txBody = TxBody.create({
|
||||
@ -53,8 +53,8 @@ describe("protobuf demo", () => {
|
||||
const msgSendDecoded = MsgSend.decode(msg.value);
|
||||
|
||||
// fromAddress and toAddress are now Buffers
|
||||
expect(Uint8Array.from(msgSendDecoded.fromAddress)).toEqual(msgSend.fromAddress);
|
||||
expect(Uint8Array.from(msgSendDecoded.toAddress)).toEqual(msgSend.toAddress);
|
||||
expect(msgSendDecoded.fromAddress).toEqual(msgSend.fromAddress);
|
||||
expect(msgSendDecoded.toAddress).toEqual(msgSend.toAddress);
|
||||
expect(msgSendDecoded.amount).toEqual(msgSend.amount);
|
||||
});
|
||||
|
||||
|
||||
@ -60,12 +60,12 @@ describe("registry magic demo", () => {
|
||||
extensionOptions: [],
|
||||
};
|
||||
const txBodyBytes = myRegistry.encode({
|
||||
typeUrl: "/cosmos.tx.TxBody",
|
||||
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
||||
value: txBodyFields,
|
||||
});
|
||||
|
||||
const txBodyDecoded = myRegistry.decode({
|
||||
typeUrl: "/cosmos.tx.TxBody",
|
||||
typeUrl: "/cosmos.tx.v1beta1.TxBody",
|
||||
value: txBodyBytes,
|
||||
});
|
||||
expect(txBodyDecoded.memo).toEqual(txBodyFields.memo);
|
||||
|
||||
@ -1,29 +1,27 @@
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
import { fromHex } from "@cosmjs/encoding";
|
||||
|
||||
import { cosmos } from "./codec";
|
||||
import { Coin, MsgSend } from "./msgs";
|
||||
|
||||
describe("msgs", () => {
|
||||
it("encodes decorated MsgSend equally to static code", () => {
|
||||
const alice = fromHex("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA");
|
||||
const bob = fromHex("BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB");
|
||||
const alice = "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6";
|
||||
const bob = "cosmos1qypqxpq9qcrsszg2pvxq6rs0zqg3yyc5lzv7xu";
|
||||
const amount = [
|
||||
new Coin({ denom: "utoken", amount: "123" }),
|
||||
new Coin({ denom: "ustake", amount: "654" }),
|
||||
];
|
||||
const donation = new MsgSend({ from_address: alice, to_address: bob, amount });
|
||||
|
||||
const expected = cosmos.bank.MsgSend.encode(
|
||||
cosmos.bank.MsgSend.create({
|
||||
const expected = cosmos.bank.v1beta1.MsgSend.encode(
|
||||
cosmos.bank.v1beta1.MsgSend.create({
|
||||
fromAddress: alice,
|
||||
toAddress: bob,
|
||||
amount: [
|
||||
cosmos.Coin.create({
|
||||
cosmos.base.v1beta1.Coin.create({
|
||||
denom: "utoken",
|
||||
amount: "123",
|
||||
}),
|
||||
cosmos.Coin.create({
|
||||
cosmos.base.v1beta1.Coin.create({
|
||||
denom: "ustake",
|
||||
amount: "654",
|
||||
}),
|
||||
|
||||
@ -6,27 +6,27 @@ import { cosmos, google } from "./codec";
|
||||
import { MsgDemo as MsgDemoType } from "./demo";
|
||||
import { Registry } from "./registry";
|
||||
|
||||
const { TxBody } = cosmos.tx;
|
||||
const { TxBody } = cosmos.tx.v1beta1;
|
||||
const { Any } = google.protobuf;
|
||||
|
||||
describe("registry demo", () => {
|
||||
it("works with a default msg", () => {
|
||||
const registry = new Registry();
|
||||
const Coin = registry.lookupType("/cosmos.Coin")!;
|
||||
const MsgSend = registry.lookupType("/cosmos.bank.MsgSend")!;
|
||||
const Coin = registry.lookupType("/cosmos.base.v1beta1.Coin")!;
|
||||
const MsgSend = registry.lookupType("/cosmos.bank.v1beta1.MsgSend")!;
|
||||
|
||||
const coin = Coin.create({
|
||||
denom: "ucosm",
|
||||
amount: "1234567890",
|
||||
});
|
||||
const msgSend = (MsgSend.create({
|
||||
fromAddress: Uint8Array.from(Array.from({ length: 20 }, () => 1)),
|
||||
toAddress: Uint8Array.from(Array.from({ length: 20 }, () => 2)),
|
||||
fromAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
|
||||
toAddress: "cosmos1qypqxpq9qcrsszg2pvxq6rs0zqg3yyc5lzv7xu",
|
||||
amount: [coin],
|
||||
}) as unknown) as cosmos.bank.MsgSend;
|
||||
}) as unknown) as cosmos.bank.v1beta1.MsgSend;
|
||||
const msgSendBytes = MsgSend.encode(msgSend).finish();
|
||||
const msgSendWrapped = Any.create({
|
||||
type_url: "/cosmos.bank.MsgSend",
|
||||
type_url: "/cosmos.bank.v1beta1.MsgSend",
|
||||
value: msgSendBytes,
|
||||
});
|
||||
const txBody = TxBody.create({
|
||||
@ -46,8 +46,8 @@ describe("registry demo", () => {
|
||||
const msgSendDecoded = decoder.decode(msg.value);
|
||||
|
||||
// fromAddress and toAddress are now Buffers
|
||||
expect(Uint8Array.from(msgSendDecoded.fromAddress)).toEqual(msgSend.fromAddress);
|
||||
expect(Uint8Array.from(msgSendDecoded.toAddress)).toEqual(msgSend.toAddress);
|
||||
expect(msgSendDecoded.fromAddress).toEqual(msgSend.fromAddress);
|
||||
expect(msgSendDecoded.toAddress).toEqual(msgSend.toAddress);
|
||||
expect(msgSendDecoded.amount).toEqual(msgSend.amount);
|
||||
});
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user