diff --git a/packages/proto-signing/src/index.ts b/packages/proto-signing/src/index.ts index 2041cccd..8b2788e7 100644 --- a/packages/proto-signing/src/index.ts +++ b/packages/proto-signing/src/index.ts @@ -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"; diff --git a/packages/proto-signing/src/signing.spec.ts b/packages/proto-signing/src/signing.spec.ts index 261ea5ee..36508e8d 100644 --- a/packages/proto-signing/src/signing.spec.ts +++ b/packages/proto-signing/src/signing.spec.ts @@ -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()); diff --git a/packages/proto-signing/src/signing.ts b/packages/proto-signing/src/signing.ts index 2b6be395..5bc6cc7c 100644 --- a/packages/proto-signing/src/signing.ts +++ b/packages/proto-signing/src/signing.ts @@ -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, diff --git a/packages/proto-signing/types/index.d.ts b/packages/proto-signing/types/index.d.ts index 2041cccd..8b2788e7 100644 --- a/packages/proto-signing/types/index.d.ts +++ b/packages/proto-signing/types/index.d.ts @@ -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"; diff --git a/packages/proto-signing/types/signing.d.ts b/packages/proto-signing/types/signing.d.ts index 674d9797..ebb669f4 100644 --- a/packages/proto-signing/types/signing.d.ts +++ b/packages/proto-signing/types/signing.d.ts @@ -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, diff --git a/packages/stargate/src/stargateclient.searchtx.spec.ts b/packages/stargate/src/stargateclient.searchtx.spec.ts index 791be4a2..be7c2893 100644 --- a/packages/stargate/src/stargateclient.searchtx.spec.ts +++ b/packages/stargate/src/stargateclient.searchtx.spec.ts @@ -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()); diff --git a/packages/stargate/src/stargateclient.spec.ts b/packages/stargate/src/stargateclient.spec.ts index a08ce8b0..c8682f4f 100644 --- a/packages/stargate/src/stargateclient.spec.ts +++ b/packages/stargate/src/stargateclient.spec.ts @@ -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());