proto-signing: Add makeSignDoc helper function

This commit is contained in:
willclarktech 2020-10-21 16:23:54 +02:00
parent f1c081a7d0
commit e3069e35a1
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
6 changed files with 33 additions and 16 deletions

View File

@ -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(

View File

@ -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";

View File

@ -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);

View File

@ -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,

View File

@ -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";

View File

@ -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,