tendermint-rpc: Fix commit signature types

This commit is contained in:
willclarktech 2021-02-10 13:11:19 +00:00
parent 466e31e5fd
commit 61934ada2f
No known key found for this signature in database
GPG Key ID: 551A86E2E398ADF7
5 changed files with 50 additions and 23 deletions

View File

@ -1,6 +1,7 @@
/* eslint-disable @typescript-eslint/naming-convention */
import { fromBase64, fromHex } from "@cosmjs/encoding";
import { JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
import { assert } from "@cosmjs/utils";
import {
assertArray,
@ -18,7 +19,7 @@ import {
} from "../../encodings";
import * as responses from "../../responses";
import { SubscriptionEvent } from "../../rpcclients";
import { ValidatorPubkey, ValidatorSignature } from "../../types";
import { BlockIdFlag, CommitSignature, ValidatorPubkey } from "../../types";
import { hashTx } from "./hasher";
interface AbciInfoResult {
@ -396,6 +397,11 @@ function decodeBroadcastTxCommit(data: RpcBroadcastTxCommitResponse): responses.
};
}
function decodeBlockIdFlag(blockIdFlag: number): BlockIdFlag {
assert(blockIdFlag in BlockIdFlag);
return blockIdFlag;
}
type RpcSignature = {
readonly block_id_flag: number;
/** hex encoded */
@ -405,22 +411,28 @@ type RpcSignature = {
readonly signature: string;
};
function decodeSignature(data: RpcSignature): ValidatorSignature {
function decodeCommitSignature(data: RpcSignature): CommitSignature {
return {
algorithm: "ed25519",
data: fromBase64(assertNotEmpty(data.signature)),
blockIdFlag: decodeBlockIdFlag(data.block_id_flag),
validatorAddress: fromHex(data.validator_address),
timestamp: new Date(assertNotEmpty(data.timestamp)),
signature: fromBase64(assertNotEmpty(data.signature)),
};
}
interface RpcCommit {
readonly block_id: RpcBlockId;
readonly height: string;
readonly round: string;
readonly signatures: readonly RpcSignature[];
}
function decodeCommit(data: RpcCommit): responses.Commit {
return {
blockId: decodeBlockId(assertObject(data.block_id)),
signatures: assertArray(data.signatures).map(decodeSignature),
height: Integer.parse(assertNotEmpty(data.height)),
round: Integer.parse(data.round),
signatures: assertArray(data.signatures).map(decodeCommitSignature),
};
}

View File

@ -115,12 +115,26 @@ function defaultTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor, expecte
client.disconnect();
});
it("can get a commit", async () => {
pendingWithoutTendermint();
const client = await Client.create(rpcFactory(), adaptor);
const response = await client.commit(4);
expect(response).toBeTruthy();
expect(response.commit.signatures.length).toBeGreaterThanOrEqual(1);
expect(response.commit.signatures[0].blockIdFlag).toEqual(2);
expect(response.commit.signatures[0].validatorAddress.length).toEqual(20);
expect(response.commit.signatures[0].timestamp).toBeInstanceOf(Date);
expect(response.commit.signatures[0].signature.length).toEqual(64);
client.disconnect();
});
it("can call a bunch of methods", async () => {
pendingWithoutTendermint();
const client = await Client.create(rpcFactory(), adaptor);
expect(await client.block()).toBeTruthy();
expect(await client.commit(4)).toBeTruthy();
expect(await client.genesis()).toBeTruthy();
expect(await client.health()).toBeNull();
expect(await client.validators()).toBeTruthy();

View File

@ -73,9 +73,4 @@ export {
VoteType,
} from "./responses";
export { HttpClient, WebsocketClient } from "./rpcclients"; // TODO: Why do we export those outside of this package?
export {
ValidatorEd25519Pubkey,
ValidatorEd25519Signature,
ValidatorPubkey,
ValidatorSignature,
} from "./types";
export { BlockIdFlag, CommitSignature, ValidatorEd25519Pubkey, ValidatorPubkey } from "./types";

View File

@ -1,6 +1,6 @@
import { ReadonlyDate } from "readonly-date";
import { ValidatorPubkey, ValidatorSignature } from "./types";
import { CommitSignature, ValidatorPubkey } from "./types";
export type Response =
| AbciInfoResponse
@ -222,7 +222,9 @@ export interface Evidence {
export interface Commit {
readonly blockId: BlockId;
readonly signatures: readonly ValidatorSignature[];
readonly height: number;
readonly round: number;
readonly signatures: readonly CommitSignature[];
}
/**
@ -241,7 +243,7 @@ export interface Vote {
readonly round: number;
readonly timestamp: ReadonlyDate;
readonly blockId: BlockId;
readonly signature: ValidatorSignature;
readonly signature: Uint8Array;
}
export interface Version {

View File

@ -12,13 +12,17 @@ export interface ValidatorEd25519Pubkey {
*/
export type ValidatorPubkey = ValidatorEd25519Pubkey;
export interface ValidatorEd25519Signature {
readonly algorithm: "ed25519";
readonly data: Uint8Array;
export enum BlockIdFlag {
Unknown = 0,
Absent = 1,
Commit = 2,
Nil = 3,
Unrecognized = -1,
}
/**
* Union type for different possible voting signatures.
* Currently only Ed25519 supported.
*/
export type ValidatorSignature = ValidatorEd25519Signature;
export interface CommitSignature {
blockIdFlag: BlockIdFlag;
validatorAddress: Uint8Array;
timestamp?: Date;
signature: Uint8Array;
}