launchpad: Use SignDoc helpers from amino

This commit is contained in:
willclarktech 2021-03-24 15:39:15 +01:00
parent 651af7d653
commit 772a23c5ae
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
16 changed files with 16 additions and 198 deletions

View File

@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { makeSignDoc } from "@cosmjs/amino";
import { assert, sleep } from "@cosmjs/utils";
import { coins } from "./coins";
import { CosmosClient, isBroadcastTxFailure } from "./cosmosclient";
import { makeSignDoc } from "./encoding";
import { LcdClient } from "./lcdapi";
import { isMsgSend, MsgSend } from "./msgs";
import { Secp256k1HdWallet } from "./secp256k1hdwallet";

View File

@ -1,10 +1,9 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { StdFee } from "@cosmjs/amino";
import { makeSignDoc, StdFee } from "@cosmjs/amino";
import { assert, sleep } from "@cosmjs/utils";
import { ReadonlyDate } from "readonly-date";
import { assertIsBroadcastTxSuccess, CosmosClient, PrivateCosmosClient } from "./cosmosclient";
import { makeSignDoc } from "./encoding";
import { findAttribute } from "./logs";
import { MsgSend } from "./msgs";
import { Secp256k1HdWallet } from "./secp256k1hdwallet";

View File

@ -1,129 +0,0 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { coin, coins } from "./coins";
import { makeSignDoc, sortedJsonStringify } from "./encoding";
import { MsgDelegate, MsgSend } from "./msgs";
import { faucet, launchpad, makeRandomAddress } from "./testutils.spec";
describe("encoding", () => {
describe("sortedJsonStringify", () => {
it("leaves non-objects unchanged", () => {
expect(sortedJsonStringify(true)).toEqual(`true`);
expect(sortedJsonStringify(false)).toEqual(`false`);
expect(sortedJsonStringify("aabbccdd")).toEqual(`"aabbccdd"`);
expect(sortedJsonStringify(75)).toEqual(`75`);
expect(sortedJsonStringify(null)).toEqual(`null`);
expect(sortedJsonStringify([5, 6, 7, 1])).toEqual(`[5,6,7,1]`);
expect(sortedJsonStringify([5, ["a", "b"], true, null, 1])).toEqual(`[5,["a","b"],true,null,1]`);
});
it("sorts objects by key", () => {
// already sorted
expect(sortedJsonStringify({})).toEqual(`{}`);
expect(sortedJsonStringify({ a: 3 })).toEqual(`{"a":3}`);
expect(sortedJsonStringify({ a: 3, b: 2, c: 1 })).toEqual(`{"a":3,"b":2,"c":1}`);
// not yet sorted
expect(sortedJsonStringify({ b: 2, a: 3, c: 1 })).toEqual(`{"a":3,"b":2,"c":1}`);
expect(sortedJsonStringify({ aaa: true, aa: true, a: true })).toEqual(
`{"a":true,"aa":true,"aaa":true}`,
);
});
it("sorts nested objects", () => {
// already sorted
expect(sortedJsonStringify({ x: { y: { z: null } } })).toEqual(`{"x":{"y":{"z":null}}}`);
// not yet sorted
expect(sortedJsonStringify({ b: { z: true, x: true, y: true }, a: true, c: true })).toEqual(
`{"a":true,"b":{"x":true,"y":true,"z":true},"c":true}`,
);
});
it("sorts objects in arrays", () => {
// already sorted
expect(sortedJsonStringify([1, 2, { x: { y: { z: null } } }, 4])).toEqual(
`[1,2,{"x":{"y":{"z":null}}},4]`,
);
// not yet sorted
expect(sortedJsonStringify([1, 2, { b: { z: true, x: true, y: true }, a: true, c: true }, 4])).toEqual(
`[1,2,{"a":true,"b":{"x":true,"y":true,"z":true},"c":true},4]`,
);
});
});
describe("makeSignDoc", () => {
it("works", () => {
const chainId = "testspace-12";
const msg1: MsgDelegate = {
type: "cosmos-sdk/MsgDelegate",
value: {
delegator_address: faucet.address0,
validator_address: launchpad.validator.address,
amount: coin(1234, "ustake"),
},
};
const msg2: MsgSend = {
type: "cosmos-sdk/MsgSend",
value: {
from_address: faucet.address0,
to_address: makeRandomAddress(),
amount: coins(1234567, "ucosm"),
},
};
const fee = {
amount: coins(2000, "ucosm"),
gas: "180000", // 180k
};
const memo = "Use your power wisely";
const accountNumber = 15;
const sequence = 16;
const signDoc = makeSignDoc([msg1, msg2], fee, chainId, memo, accountNumber, sequence);
expect(signDoc).toEqual({
msgs: [msg1, msg2],
fee: fee,
chain_id: chainId,
account_number: accountNumber.toString(),
sequence: sequence.toString(),
memo: memo,
});
});
it("works with undefined memo", () => {
const chainId = "testspace-12";
const msg1: MsgDelegate = {
type: "cosmos-sdk/MsgDelegate",
value: {
delegator_address: faucet.address0,
validator_address: launchpad.validator.address,
amount: coin(1234, "ustake"),
},
};
const msg2: MsgSend = {
type: "cosmos-sdk/MsgSend",
value: {
from_address: faucet.address0,
to_address: makeRandomAddress(),
amount: coins(1234567, "ucosm"),
},
};
const fee = {
amount: coins(2000, "ucosm"),
gas: "180000", // 180k
};
const accountNumber = 15;
const sequence = 16;
const signDoc = makeSignDoc([msg1, msg2], fee, chainId, undefined, accountNumber, sequence);
expect(signDoc).toEqual({
msgs: [msg1, msg2],
fee: fee,
chain_id: chainId,
account_number: accountNumber.toString(),
sequence: sequence.toString(),
memo: "",
});
});
});
});

View File

@ -1,48 +0,0 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { AminoMsg, StdFee, StdSignDoc } from "@cosmjs/amino";
import { toUtf8 } from "@cosmjs/encoding";
import { Uint53 } from "@cosmjs/math";
function sortedObject(obj: any): any {
if (typeof obj !== "object" || obj === null) {
return obj;
}
if (Array.isArray(obj)) {
return obj.map(sortedObject);
}
const sortedKeys = Object.keys(obj).sort();
const result: Record<string, any> = {};
// NOTE: Use forEach instead of reduce for performance with large objects eg Wasm code
sortedKeys.forEach((key) => {
result[key] = sortedObject(obj[key]);
});
return result;
}
/** Returns a JSON string with objects sorted by key */
// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function sortedJsonStringify(obj: any): string {
return JSON.stringify(sortedObject(obj));
}
export function makeSignDoc(
msgs: readonly AminoMsg[],
fee: StdFee,
chainId: string,
memo: string | undefined,
accountNumber: number | string,
sequence: number | string,
): StdSignDoc {
return {
chain_id: chainId,
account_number: Uint53.fromString(accountNumber.toString()).toString(),
sequence: Uint53.fromString(sequence.toString()).toString(),
fee: fee,
msgs: msgs,
memo: memo || "",
};
}
export function serializeSignDoc(signDoc: StdSignDoc): Uint8Array {
return toUtf8(sortedJsonStringify(signDoc));
}

View File

@ -16,8 +16,10 @@ export {
encodeBech32Pubkey,
encodeSecp256k1Pubkey,
encodeSecp256k1Signature,
makeSignDoc,
pubkeyToAddress,
pubkeyType,
serializeSignDoc,
} from "@cosmjs/amino";
import { SinglePubkey } from "@cosmjs/amino";
/** @deprecated PubKey is deprecated. Use `SinglePubkey` or the more general `Pubkey` from `@cosmjs/amino`. */
@ -49,7 +51,6 @@ export {
isSearchBySentFromOrToQuery,
isSearchByTagsQuery,
} from "./cosmosclient";
export { makeSignDoc, serializeSignDoc } from "./encoding";
export { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./fee";
export {
AuthAccountsResponse,

View File

@ -1,10 +1,10 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { makeSignDoc } from "@cosmjs/amino";
import { Bech32 } from "@cosmjs/encoding";
import { sleep } from "@cosmjs/utils";
import { coin, coins } from "../coins";
import { assertIsBroadcastTxSuccess } from "../cosmosclient";
import { makeSignDoc } from "../encoding";
import { MsgDelegate } from "../msgs";
import { Secp256k1HdWallet } from "../secp256k1hdwallet";
import { SigningCosmosClient } from "../signingcosmosclient";

View File

@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { makeSignDoc } from "@cosmjs/amino";
import { sleep } from "@cosmjs/utils";
import { coins } from "../coins";
import { assertIsBroadcastTxSuccess } from "../cosmosclient";
import { makeSignDoc } from "../encoding";
import { Secp256k1HdWallet } from "../secp256k1hdwallet";
import { SigningCosmosClient } from "../signingcosmosclient";
import {

View File

@ -1,9 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Coin, StdFee } from "@cosmjs/amino";
import { Coin, makeSignDoc, StdFee } from "@cosmjs/amino";
import { assert, sleep } from "@cosmjs/utils";
import { isBroadcastTxFailure } from "../cosmosclient";
import { makeSignDoc } from "../encoding";
import { parseLogs } from "../logs";
import { MsgSend } from "../msgs";
import { makeCosmoshubPath } from "../paths";

View File

@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { makeSignDoc } from "@cosmjs/amino";
import { assert, sleep } from "@cosmjs/utils";
import { coin, coins } from "../coins";
import { assertIsBroadcastTxSuccess } from "../cosmosclient";
import { makeSignDoc } from "../encoding";
import { MsgDelegate, MsgUndelegate } from "../msgs";
import { Secp256k1HdWallet } from "../secp256k1hdwallet";
import { SigningCosmosClient } from "../signingcosmosclient";

View File

@ -1,9 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { StdSignDoc } from "@cosmjs/amino";
import { serializeSignDoc, StdSignDoc } from "@cosmjs/amino";
import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto";
import { fromBase64, fromHex } from "@cosmjs/encoding";
import { serializeSignDoc } from "./encoding";
import { extractKdfConfiguration, Secp256k1HdWallet } from "./secp256k1hdwallet";
import { base64Matcher } from "./testutils.spec";
import { executeKdf, KdfConfiguration } from "./wallet";

View File

@ -4,6 +4,7 @@ import {
encodeSecp256k1Signature,
OfflineAminoSigner,
rawSecp256k1PubkeyToRawAddress,
serializeSignDoc,
StdSignDoc,
} from "@cosmjs/amino";
import {
@ -21,7 +22,6 @@ import {
import { Bech32, fromBase64, fromUtf8, toBase64, toUtf8 } from "@cosmjs/encoding";
import { assert, isNonNullObject } from "@cosmjs/utils";
import { serializeSignDoc } from "./encoding";
import { makeCosmoshubPath } from "./paths";
import {
decrypt,

View File

@ -1,9 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { StdSignDoc } from "@cosmjs/amino";
import { serializeSignDoc, StdSignDoc } from "@cosmjs/amino";
import { Secp256k1, Secp256k1Signature, Sha256 } from "@cosmjs/crypto";
import { fromBase64, fromHex } from "@cosmjs/encoding";
import { serializeSignDoc } from "./encoding";
import { Secp256k1Wallet } from "./secp256k1wallet";
describe("Secp256k1Wallet", () => {

View File

@ -4,13 +4,12 @@ import {
encodeSecp256k1Signature,
OfflineAminoSigner,
rawSecp256k1PubkeyToRawAddress,
serializeSignDoc,
StdSignDoc,
} from "@cosmjs/amino";
import { Secp256k1, Sha256 } from "@cosmjs/crypto";
import { Bech32 } from "@cosmjs/encoding";
import { serializeSignDoc } from "./encoding";
/**
* A wallet that holds a single secp256k1 keypair.
*

View File

@ -1,7 +1,6 @@
import { decodeSignature } from "@cosmjs/amino";
import { decodeSignature, makeSignDoc, serializeSignDoc } from "@cosmjs/amino";
import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto";
import { makeSignDoc, serializeSignDoc } from "./encoding";
import { WrappedStdTx } from "./tx";
/**

View File

@ -1,9 +1,9 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { AminoMsg, Coin, OfflineAminoSigner, StdFee } from "@cosmjs/amino";
import { AminoMsg, Coin, makeSignDoc, OfflineAminoSigner, StdFee } from "@cosmjs/amino";
import {} from "@cosmjs/amino/build/signdoc";
import equals from "fast-deep-equal";
import { Account, BroadcastTxResult, CosmosClient, GetSequenceResult } from "./cosmosclient";
import { makeSignDoc } from "./encoding";
import { buildFeeTable, FeeTable, GasLimits, GasPrice } from "./fee";
import { BroadcastMode } from "./lcdapi";
import { MsgSend } from "./msgs";

View File

@ -1,8 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { StdFee, StdSignature } from "@cosmjs/amino";
import { makeSignDoc } from "@cosmjs/amino/build/signdoc";
import { coins } from "./coins";
import { makeSignDoc } from "./encoding";
import { makeStdTx } from "./tx";
describe("tx", () => {