Merge pull request #880 from cosmos/decodeTxRaw-testing

Add test for decodeTxRaw
This commit is contained in:
Simon Warta 2021-09-29 13:20:44 +02:00 committed by GitHub
commit 68a21d55f4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 2 deletions

View File

@ -0,0 +1,69 @@
import { fromBase64, fromHex } from "@cosmjs/encoding";
import { MsgSend } from "cosmjs-types/cosmos/bank/v1beta1/tx";
import { PubKey } from "cosmjs-types/cosmos/crypto/secp256k1/keys";
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
import { Any } from "cosmjs-types/google/protobuf/any";
import Long from "long";
import { decodeTxRaw } from "./decode";
import { faucet, testVectors } from "./testutils.spec";
describe("decode", () => {
describe("decodeTxRaw", () => {
it("works", () => {
const pubkeyBytes = fromBase64(faucet.pubkey.value);
const prefixedPubkeyBytes = Uint8Array.from(PubKey.encode({ key: pubkeyBytes }).finish());
const testVector = testVectors[0];
const expectedMsg: Any = {
typeUrl: "/cosmos.bank.v1beta1.MsgSend",
value: Uint8Array.from(
MsgSend.encode({
fromAddress: "cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6",
toAddress: "cosmos1qypqxpq9qcrsszg2pvxq6rs0zqg3yyc5lzv7xu",
amount: [
{
denom: "ucosm",
amount: "1234567",
},
],
}).finish(),
),
};
const decoded = decodeTxRaw(fromHex(testVector.outputs.signedTxBytes));
expect(decoded).toEqual({
authInfo: {
signerInfos: [
{
publicKey: {
typeUrl: "/cosmos.crypto.secp256k1.PubKey",
value: prefixedPubkeyBytes,
},
modeInfo: {
single: {
mode: SignMode.SIGN_MODE_DIRECT,
},
},
sequence: Long.fromNumber(0, true),
},
],
fee: {
gasLimit: Long.fromNumber(200000, true),
payer: "",
granter: "",
amount: [{ amount: "2000", denom: "ucosm" }],
},
},
body: {
memo: "",
timeoutHeight: Long.UZERO,
messages: [expectedMsg],
extensionOptions: [],
nonCriticalExtensionOptions: [],
},
signatures: [fromHex(testVector.outputs.signature)],
});
});
});
});

View File

@ -1,5 +1,6 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { fromBase64, fromHex, toHex } from "@cosmjs/encoding";
import { PubKey } from "cosmjs-types/cosmos/crypto/secp256k1/keys";
import { SignMode } from "cosmjs-types/cosmos/tx/signing/v1beta1/signing";
import { TxRaw } from "cosmjs-types/cosmos/tx/v1beta1/tx";
@ -20,13 +21,13 @@ describe("signing", () => {
it("correctly parses signed transactions from test vectors", async () => {
const wallet = await DirectSecp256k1HdWallet.fromMnemonic(faucet.mnemonic);
const [{ address, pubkey: pubkeyBytes }] = await wallet.getAccounts();
const prefixedPubkeyBytes = Uint8Array.from([0x0a, pubkeyBytes.length, ...pubkeyBytes]);
const prefixedPubkeyBytes = Uint8Array.from(PubKey.encode({ key: pubkeyBytes }).finish());
testVectors.forEach(({ outputs: { signedTxBytes } }) => {
const parsedTestTx = decodeTxRaw(fromHex(signedTxBytes));
expect(parsedTestTx.signatures.length).toEqual(1);
expect(parsedTestTx.authInfo.signerInfos.length).toEqual(1);
expect(Uint8Array.from(parsedTestTx.authInfo.signerInfos[0].publicKey!.value ?? [])).toEqual(
expect(Uint8Array.from(parsedTestTx.authInfo.signerInfos[0].publicKey!.value)).toEqual(
prefixedPubkeyBytes,
);
expect(parsedTestTx.authInfo.signerInfos[0].modeInfo!.single!.mode).toEqual(SignMode.SIGN_MODE_DIRECT);