From e3069e35a125bede8006a939644e60e517e729b0 Mon Sep 17 00:00:00 2001 From: willclarktech Date: Wed, 21 Oct 2020 16:23:54 +0200 Subject: [PATCH] proto-signing: Add makeSignDoc helper function --- .../src/directsecp256k1wallet.spec.ts | 16 +++++++++------- packages/proto-signing/src/index.ts | 2 +- packages/proto-signing/src/signing.spec.ts | 9 ++------- packages/proto-signing/src/signing.ts | 14 ++++++++++++++ packages/proto-signing/types/index.d.ts | 2 +- packages/proto-signing/types/signing.d.ts | 6 ++++++ 6 files changed, 33 insertions(+), 16 deletions(-) diff --git a/packages/proto-signing/src/directsecp256k1wallet.spec.ts b/packages/proto-signing/src/directsecp256k1wallet.spec.ts index 732875f1..cd199b99 100644 --- a/packages/proto-signing/src/directsecp256k1wallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1wallet.spec.ts @@ -4,7 +4,7 @@ import { coins } from "@cosmjs/launchpad"; import Long from "long"; import { DirectSecp256k1Wallet } from "./directsecp256k1wallet"; -import { makeAuthInfoBytes, makeSignBytes } from "./signing"; +import { makeAuthInfoBytes, makeSignBytes, makeSignDoc } from "./signing"; import { faucet, testVectors } from "./testutils.spec"; describe("DirectSecp256k1Wallet", () => { @@ -68,12 +68,14 @@ describe("DirectSecp256k1Wallet", () => { }; const fee = coins(2000, "ucosm"); const gasLimit = 200000; - const signDoc = { - bodyBytes: fromHex(bodyBytes), - authInfoBytes: makeAuthInfoBytes([pubkey], fee, gasLimit, sequence), - accountNumber: Long.fromNumber(1), - chainId: "simd-testing", - }; + const chainId = "simd-testing"; + const accountNumber = 1; + const signDoc = makeSignDoc( + fromHex(bodyBytes), + makeAuthInfoBytes([pubkey], fee, gasLimit, sequence), + chainId, + accountNumber, + ); const signDocBytes = makeSignBytes(signDoc); const signResponse = await wallet.signDirect(faucet.address, signDoc); const valid = await Secp256k1.verifySignature( diff --git a/packages/proto-signing/src/index.ts b/packages/proto-signing/src/index.ts index b8112a32..ebc91f1a 100644 --- a/packages/proto-signing/src/index.ts +++ b/packages/proto-signing/src/index.ts @@ -4,4 +4,4 @@ export { Registry } from "./registry"; export { DirectSecp256k1Wallet } from "./directsecp256k1wallet"; export { decodePubkey, encodePubkey } from "./pubkey"; export { OfflineDirectSigner, OfflineSigner } from "./signer"; -export { makeAuthInfoBytes, makeSignBytes } from "./signing"; +export { makeAuthInfoBytes, makeSignBytes, makeSignDoc } from "./signing"; diff --git a/packages/proto-signing/src/signing.spec.ts b/packages/proto-signing/src/signing.spec.ts index 08b04fe9..26e0c284 100644 --- a/packages/proto-signing/src/signing.spec.ts +++ b/packages/proto-signing/src/signing.spec.ts @@ -6,7 +6,7 @@ import { cosmos, google } from "./codec"; import { DirectSecp256k1Wallet } from "./directsecp256k1wallet"; import { defaultRegistry } from "./msgs"; import { Registry, TxBodyValue } from "./registry"; -import { makeAuthInfoBytes, makeSignBytes } from "./signing"; +import { makeAuthInfoBytes, makeSignBytes, makeSignDoc } from "./signing"; import { faucet, testVectors } from "./testutils.spec"; const { Tx, TxRaw } = cosmos.tx.v1beta1; @@ -97,12 +97,7 @@ describe("signing", () => { await Promise.all( testVectors.map(async ({ sequence, signBytes, signedTxBytes }) => { const authInfoBytes = makeAuthInfoBytes([publicKeyAny], feeAmount, gasLimit, sequence); - const signDoc = { - bodyBytes: txBodyBytes, - authInfoBytes: authInfoBytes, - chainId: chainId, - accountNumber: Long.fromNumber(accountNumber), - }; + const signDoc = makeSignDoc(txBodyBytes, authInfoBytes, chainId, accountNumber); const signDocBytes = makeSignBytes(signDoc); expect(toHex(signDocBytes)).toEqual(signBytes); diff --git a/packages/proto-signing/src/signing.ts b/packages/proto-signing/src/signing.ts index 6213360d..ae152779 100644 --- a/packages/proto-signing/src/signing.ts +++ b/packages/proto-signing/src/signing.ts @@ -30,6 +30,20 @@ export function makeAuthInfoBytes( return Uint8Array.from(AuthInfo.encode(authInfo).finish()); } +export function makeSignDoc( + bodyBytes: Uint8Array, + authInfoBytes: Uint8Array, + chainId: string, + accountNumber: number, +): cosmos.tx.v1beta1.ISignDoc { + return { + bodyBytes: bodyBytes, + authInfoBytes: authInfoBytes, + chainId: chainId, + accountNumber: Long.fromNumber(accountNumber), + }; +} + export function makeSignBytes({ accountNumber, authInfoBytes, diff --git a/packages/proto-signing/types/index.d.ts b/packages/proto-signing/types/index.d.ts index b8112a32..ebc91f1a 100644 --- a/packages/proto-signing/types/index.d.ts +++ b/packages/proto-signing/types/index.d.ts @@ -4,4 +4,4 @@ export { Registry } from "./registry"; export { DirectSecp256k1Wallet } from "./directsecp256k1wallet"; export { decodePubkey, encodePubkey } from "./pubkey"; export { OfflineDirectSigner, OfflineSigner } from "./signer"; -export { makeAuthInfoBytes, makeSignBytes } from "./signing"; +export { makeAuthInfoBytes, makeSignBytes, makeSignDoc } from "./signing"; diff --git a/packages/proto-signing/types/signing.d.ts b/packages/proto-signing/types/signing.d.ts index e7676b3b..e3cc389d 100644 --- a/packages/proto-signing/types/signing.d.ts +++ b/packages/proto-signing/types/signing.d.ts @@ -8,6 +8,12 @@ export declare function makeAuthInfoBytes( gasLimit: number, sequence: number, ): Uint8Array; +export declare function makeSignDoc( + bodyBytes: Uint8Array, + authInfoBytes: Uint8Array, + chainId: string, + accountNumber: number, +): cosmos.tx.v1beta1.ISignDoc; export declare function makeSignBytes({ accountNumber, authInfoBytes,