Make CommitSignature fields optional

This commit is contained in:
Simon Warta 2021-03-12 21:12:22 +01:00
parent a2ff422228
commit dc96a07443
4 changed files with 22 additions and 16 deletions

View File

@ -105,9 +105,9 @@ function defaultTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor, expecte
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].validatorAddress?.length).toEqual(20);
expect(response.commit.signatures[0].timestamp).toBeInstanceOf(Date);
expect(response.commit.signatures[0].signature.length).toEqual(64);
expect(response.commit.signatures[0].signature?.length).toEqual(64);
client.disconnect();
});

View File

@ -3,7 +3,7 @@ import { fromBase64, fromHex } from "@cosmjs/encoding";
import { JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
import { assert } from "@cosmjs/utils";
import { fromRfc3339WithNanoseconds } from "../../../dates";
import { DateWithNanoseconds, fromRfc3339WithNanoseconds } from "../../../dates";
import { SubscriptionEvent } from "../../../rpcclients";
import { BlockIdFlag, CommitSignature, ValidatorPubkey } from "../../../types";
import {
@ -416,17 +416,23 @@ type RpcSignature = {
readonly signature: string | null;
};
/**
* In some cases a timestamp is optional and set to the value 0 in Go.
* This can lead to strings like "0001-01-01T00:00:00Z" (see https://github.com/cosmos/cosmjs/issues/704#issuecomment-797122415).
* This decoder tries to clean up such encoding from the API and turn them
* into undefined values.
*/
function decodeOptionalTime(timestamp: string): DateWithNanoseconds | undefined {
const nonZeroTime = timestamp && !timestamp.startsWith("0001-01-01");
return nonZeroTime ? fromRfc3339WithNanoseconds(timestamp) : undefined;
}
function decodeCommitSignature(data: RpcSignature): CommitSignature {
const nonZeroTime = data.timestamp && !data.timestamp.startsWith("0001-01-01");
return {
blockIdFlag: decodeBlockIdFlag(data.block_id_flag),
// FIXME: Return an optional validatorAddress instead of an empty Uint8Array
// in the next breaking release.
validatorAddress: fromHex(data.validator_address),
timestamp: nonZeroTime ? fromRfc3339WithNanoseconds(data.timestamp) : undefined,
// FIXME: Return an optional signature instead of an empty Uint8Array
// in the next breaking release.
signature: fromBase64(data.signature || ""),
validatorAddress: data.validator_address ? fromHex(data.validator_address) : undefined,
timestamp: decodeOptionalTime(data.timestamp),
signature: data.signature ? fromBase64(data.signature) : undefined,
};
}

View File

@ -104,9 +104,9 @@ function defaultTestSuite(rpcFactory: () => RpcClient, expected: ExpectedValues)
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].validatorAddress?.length).toEqual(20);
expect(response.commit.signatures[0].timestamp).toBeInstanceOf(Date);
expect(response.commit.signatures[0].signature.length).toEqual(64);
expect(response.commit.signatures[0].signature?.length).toEqual(64);
client.disconnect();
});

View File

@ -25,7 +25,7 @@ export enum BlockIdFlag {
export interface CommitSignature {
/** If this is BlockIdFlag.Absent, all other fields are expected to be unset */
blockIdFlag: BlockIdFlag;
validatorAddress: Uint8Array;
timestamp?: ReadonlyDateWithNanoseconds;
signature: Uint8Array;
validatorAddress: Uint8Array | undefined;
timestamp: ReadonlyDateWithNanoseconds | undefined;
signature: Uint8Array | undefined;
}