Merge pull request #414 from CosmWasm/extract-makeAuthInfo

Pull out makeAuthInfo
This commit is contained in:
Simon Warta 2020-09-08 10:59:33 +02:00 committed by GitHub
commit fb2067baf5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 45 additions and 63 deletions

View File

@ -1,4 +1,4 @@
export { Coin } from "./msgs";
export { cosmosField } from "./decorator";
export { Registry } from "./registry";
export { makeSignBytes } from "./signing";
export { makeAuthInfo, makeSignBytes } from "./signing";

View File

@ -1,12 +1,11 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Bech32, fromBase64, fromHex, toHex } from "@cosmjs/encoding";
import { Secp256k1Wallet } from "@cosmjs/launchpad";
import Long from "long";
import { cosmos } from "./codec";
import { defaultRegistry } from "./msgs";
import { Registry, TxBodyValue } from "./registry";
import { makeSignBytes } from "./signing";
import { makeAuthInfo, makeSignBytes } from "./signing";
const { AuthInfo, Tx, TxBody } = cosmos.tx;
const { PublicKey } = cosmos.crypto;
@ -61,13 +60,13 @@ const testVectors = [
},
];
describe("signing demo", () => {
describe("signing", () => {
const chainId = "simd-testing";
const toAddress = Uint8Array.from({ length: 20 }, (_, i) => i + 1);
const sendAmount = "1234567";
const sendDenom = "ucosm";
const gasLimit = Long.fromNumber(200000);
const gasLimit = 200000;
it("correctly parses test vectors", async () => {
const wallet = await Secp256k1Wallet.fromMnemonic(faucet.mnemonic);
@ -130,22 +129,7 @@ describe("signing demo", () => {
});
const txBody = TxBody.decode(txBodyBytes);
const authInfo = {
signerInfos: [
{
publicKey: publicKey,
modeInfo: {
single: {
mode: cosmos.tx.signing.SignMode.SIGN_MODE_DIRECT,
},
},
},
],
fee: {
gasLimit: gasLimit,
},
};
const authInfoBytes = Uint8Array.from(AuthInfo.encode(authInfo).finish());
const authInfoBytes = makeAuthInfo([publicKey], gasLimit);
const accountNumber = 1;
await Promise.all(
@ -154,9 +138,10 @@ describe("signing demo", () => {
expect(toHex(signDocBytes)).toEqual(signBytes);
const signature = await wallet.sign(address, signDocBytes);
// TODO: Why is this not a TxRaw? https://github.com/CosmWasm/cosmjs/issues/383
const txRaw = Tx.create({
body: txBody,
authInfo: authInfo,
authInfo: AuthInfo.decode(authInfoBytes),
signatures: [fromBase64(signature.signature)],
});
const txRawBytes = Uint8Array.from(Tx.encode(txRaw).finish());

View File

@ -1,8 +1,28 @@
/* eslint-disable @typescript-eslint/naming-convention */
import Long from "long";
import { omitDefaults } from "./adr27";
import { cosmos } from "./codec";
const { SignDoc } = cosmos.tx;
const { SignDoc, AuthInfo } = cosmos.tx;
/**
* Creates and serializes an AuthInfo document using SIGN_MODE_DIRECT.
*/
export function makeAuthInfo(pubkeys: readonly cosmos.crypto.IPublicKey[], gasLimit: number): Uint8Array {
const authInfo = {
signerInfos: pubkeys.map(
(pubkey): cosmos.tx.ISignerInfo => ({
publicKey: pubkey,
modeInfo: {
single: { mode: cosmos.tx.signing.SignMode.SIGN_MODE_DIRECT },
},
}),
),
fee: { gasLimit: Long.fromNumber(gasLimit) },
};
return Uint8Array.from(AuthInfo.encode(authInfo).finish());
}
export function makeSignBytes(
txBody: Uint8Array,

View File

@ -1,4 +1,4 @@
export { Coin } from "./msgs";
export { cosmosField } from "./decorator";
export { Registry } from "./registry";
export { makeSignBytes } from "./signing";
export { makeAuthInfo, makeSignBytes } from "./signing";

View File

@ -1,3 +1,11 @@
import { cosmos } from "./codec";
/**
* Creates and serializes an AuthInfo document using SIGN_MODE_DIRECT.
*/
export declare function makeAuthInfo(
pubkeys: readonly cosmos.crypto.IPublicKey[],
gasLimit: number,
): Uint8Array;
export declare function makeSignBytes(
txBody: Uint8Array,
authInfo: Uint8Array,

View File

@ -1,9 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Bech32, fromBase64 } from "@cosmjs/encoding";
import { Coin, coins, Secp256k1Wallet } from "@cosmjs/launchpad";
import { makeSignBytes, Registry } from "@cosmjs/proto-signing";
import { makeAuthInfo, makeSignBytes, Registry } from "@cosmjs/proto-signing";
import { assert, sleep } from "@cosmjs/utils";
import Long from "long";
import { cosmos } from "./codec";
import {
@ -56,31 +55,16 @@ async function sendTokens(
};
const txBodyBytes = registry.encode(txBodyFields);
const txBody = TxBody.decode(txBodyBytes);
const authInfo = {
signerInfos: [
{
publicKey: publicKey,
modeInfo: {
single: {
mode: cosmos.tx.signing.SignMode.SIGN_MODE_DIRECT,
},
},
},
],
fee: {
gasLimit: Long.fromNumber(200000),
},
};
const authInfoBytes = Uint8Array.from(AuthInfo.encode(authInfo).finish());
const authInfoBytes = makeAuthInfo([publicKey], 200000);
const { accountNumber, sequence } = (await client.getSequence(walletAddress))!;
const chainId = await client.getChainId();
const signDocBytes = makeSignBytes(txBodyBytes, authInfoBytes, chainId, accountNumber, sequence);
const signature = await wallet.sign(walletAddress, signDocBytes);
// TODO: Why is this not a TxRaw? https://github.com/CosmWasm/cosmjs/issues/383
const txRaw = Tx.create({
body: txBody,
authInfo: authInfo,
authInfo: AuthInfo.decode(authInfoBytes),
signatures: [fromBase64(signature.signature)],
});
const txRawBytes = Uint8Array.from(Tx.encode(txRaw).finish());

View File

@ -1,9 +1,8 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { Bech32, fromBase64 } from "@cosmjs/encoding";
import { Secp256k1Wallet } from "@cosmjs/launchpad";
import { makeSignBytes, Registry } from "@cosmjs/proto-signing";
import { makeAuthInfo, makeSignBytes, Registry } from "@cosmjs/proto-signing";
import { assert, sleep } from "@cosmjs/utils";
import Long from "long";
import { ReadonlyDate } from "readonly-date";
import { cosmos } from "./codec";
@ -279,30 +278,16 @@ describe("StargateClient", () => {
};
const txBodyBytes = registry.encode(txBodyFields);
const txBody = TxBody.decode(txBodyBytes);
const authInfo = {
signerInfos: [
{
publicKey: publicKey,
modeInfo: {
single: {
mode: cosmos.tx.signing.SignMode.SIGN_MODE_DIRECT,
},
},
},
],
fee: {
gasLimit: Long.fromNumber(200000),
},
};
const authInfoBytes = Uint8Array.from(AuthInfo.encode(authInfo).finish());
const authInfoBytes = makeAuthInfo([publicKey], 200000);
const chainId = await client.getChainId();
const { accountNumber, sequence } = (await client.getSequence(address))!;
const signDocBytes = makeSignBytes(txBodyBytes, authInfoBytes, chainId, accountNumber, sequence);
const signature = await wallet.sign(address, signDocBytes);
// TODO: Why is this not a TxRaw? https://github.com/CosmWasm/cosmjs/issues/383
const txRaw = Tx.create({
body: txBody,
authInfo: authInfo,
authInfo: AuthInfo.decode(authInfoBytes),
signatures: [fromBase64(signature.signature)],
});
const txRawBytes = Uint8Array.from(Tx.encode(txRaw).finish());