Remove types BlockHash, TxBytes and TxHash

This commit is contained in:
Simon Warta 2020-12-21 03:02:19 +01:00
parent 0855c56fdc
commit fb49a6b4f5
14 changed files with 36 additions and 71 deletions

View File

@ -35,6 +35,8 @@
- @cosmjs/tendermint-rpc: Export `DateTime` class.
- @cosmjs/tendermint-rpc: Remove types `QueryString`, `Base64String`,
`HexString`, `IntegerString` and `IpPortString`. Use `string` instead.
- @cosmjs/tendermint-rpc: Remove types `BlockHash`, `TxBytes` and `TxHash`. Use
`Uint8Array` instead.
## 0.23.1 (2020-10-27)

View File

@ -3,13 +3,12 @@ import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
import * as requests from "./requests";
import * as responses from "./responses";
import { SubscriptionEvent } from "./rpcclients";
import { BlockHash, TxBytes, TxHash } from "./types";
export interface Adaptor {
readonly params: Params;
readonly responses: Responses;
readonly hashTx: (tx: TxBytes) => TxHash;
readonly hashBlock: (header: responses.Header) => BlockHash;
readonly hashTx: (tx: Uint8Array) => Uint8Array;
readonly hashBlock: (header: responses.Header) => Uint8Array;
}
// Encoder is a generic that matches all methods of Params

View File

@ -2,7 +2,6 @@ import { fromBase64, fromHex } from "@cosmjs/encoding";
import { ReadonlyDate } from "readonly-date";
import { ReadonlyDateWithNanoseconds } from "../../responses";
import { TxBytes } from "../../types";
import { hashBlock, hashTx } from "./hasher";
describe("Hasher", () => {
@ -10,7 +9,7 @@ describe("Hasher", () => {
// This was taken from a result from /tx_search of some random test transaction
// curl "http://localhost:11127/tx_search?query=\"tx.hash='5CB2CF94A1097A4BC19258BC2353C3E76102B6D528458BE45C855DC5563C1DB2'\""
const txId = fromHex("5CB2CF94A1097A4BC19258BC2353C3E76102B6D528458BE45C855DC5563C1DB2");
const txData = fromBase64("YUpxZDY2NURaUDMxPWd2TzBPdnNrVWFWYg==") as TxBytes;
const txData = fromBase64("YUpxZDY2NURaUDMxPWd2TzBPdnNrVWFWYg==");
expect(hashTx(txData)).toEqual(txId);
});

View File

@ -1,4 +1,4 @@
import { Sha256 } from "@cosmjs/crypto";
import { Sha256, sha256 } from "@cosmjs/crypto";
import {
encodeBlockId,
@ -9,13 +9,11 @@ import {
encodeVersion,
} from "../../encodings";
import { Header } from "../../responses";
import { BlockHash, TxBytes, TxHash } from "../../types";
// hash is sha256
// https://github.com/tendermint/tendermint/blob/master/UPGRADING.md#v0260
export function hashTx(tx: TxBytes): TxHash {
const hash = new Sha256(tx).digest();
return hash as TxHash;
export function hashTx(tx: Uint8Array): Uint8Array {
return sha256(tx);
}
function getSplitPoint(n: number): number {
@ -54,7 +52,7 @@ function hashTree(hashes: readonly Uint8Array[]): Uint8Array {
}
}
export function hashBlock(header: Header): BlockHash {
export function hashBlock(header: Header): Uint8Array {
const encodedFields: readonly Uint8Array[] = [
encodeVersion(header.version),
encodeString(header.chainId),
@ -72,5 +70,5 @@ export function hashBlock(header: Header): BlockHash {
encodeBytes(header.evidenceHash),
encodeBytes(header.proposerAddress),
];
return hashTree(encodedFields) as BlockHash;
return hashTree(encodedFields);
}

View File

@ -18,7 +18,7 @@ import {
} from "../../encodings";
import * as responses from "../../responses";
import { SubscriptionEvent } from "../../rpcclients";
import { TxBytes, TxHash, ValidatorPubkey, ValidatorSignature } from "../../types";
import { ValidatorPubkey, ValidatorSignature } from "../../types";
import { hashTx } from "./hasher";
interface AbciInfoResult {
@ -375,7 +375,7 @@ interface RpcBroadcastTxSyncResponse extends RpcTxData {
function decodeBroadcastTxSync(data: RpcBroadcastTxSyncResponse): responses.BroadcastTxSyncResponse {
return {
...decodeTxData(data),
hash: fromHex(assertNotEmpty(data.hash)) as TxHash,
hash: fromHex(assertNotEmpty(data.hash)),
};
}
@ -390,7 +390,7 @@ interface RpcBroadcastTxCommitResponse {
function decodeBroadcastTxCommit(data: RpcBroadcastTxCommitResponse): responses.BroadcastTxCommitResponse {
return {
height: Integer.parse(data.height),
hash: fromHex(assertNotEmpty(data.hash)) as TxHash,
hash: fromHex(assertNotEmpty(data.hash)),
checkTx: decodeTxData(assertObject(data.check_tx)),
deliverTx: may(decodeTxData, data.deliver_tx),
};
@ -626,11 +626,11 @@ interface RpcTxResponse {
function decodeTxResponse(data: RpcTxResponse): responses.TxResponse {
return {
tx: fromBase64(assertNotEmpty(data.tx)) as TxBytes,
tx: fromBase64(assertNotEmpty(data.tx)),
result: decodeTxData(assertObject(data.tx_result)),
height: Integer.parse(assertNotEmpty(data.height)),
index: Integer.parse(assertNumber(data.index)),
hash: fromHex(assertNotEmpty(data.hash)) as TxHash,
hash: fromHex(assertNotEmpty(data.hash)),
proof: may(decodeTxProof, data.proof),
};
}
@ -657,7 +657,7 @@ interface RpcTxEvent {
}
function decodeTxEvent(data: RpcTxEvent): responses.TxEvent {
const tx = fromBase64(assertNotEmpty(data.tx)) as TxBytes;
const tx = fromBase64(assertNotEmpty(data.tx));
return {
tx: tx,
hash: hashTx(tx),

View File

@ -13,7 +13,6 @@ import { buildQuery } from "./requests";
import * as responses from "./responses";
import { HttpClient, RpcClient, WebsocketClient } from "./rpcclients";
import { chainIdMatcher } from "./testutil.spec";
import { TxBytes } from "./types";
function tendermintEnabled(): boolean {
return !!process.env.TENDERMINT_ENABLED;
@ -30,8 +29,8 @@ async function tendermintSearchIndexUpdated(): Promise<void> {
return sleep(75);
}
function buildKvTx(k: string, v: string): TxBytes {
return toAscii(`${k}=${v}`) as TxBytes;
function buildKvTx(k: string, v: string): Uint8Array {
return toAscii(`${k}=${v}`);
}
function randomString(): string {

View File

@ -74,8 +74,6 @@ export {
} from "./responses";
export { HttpClient, WebsocketClient } from "./rpcclients"; // TODO: Why do we export those outside of this package?
export {
TxBytes,
TxHash,
ValidatorEd25519Pubkey,
ValidatorEd25519Signature,
ValidatorPubkey,

View File

@ -1,6 +1,6 @@
import { ReadonlyDate } from "readonly-date";
import { TxBytes, TxHash, ValidatorPubkey, ValidatorSignature } from "./types";
import { ValidatorPubkey, ValidatorSignature } from "./types";
export type Response =
| AbciInfoResponse
@ -68,7 +68,7 @@ export interface BlockchainResponse {
export interface BroadcastTxAsyncResponse {}
export interface BroadcastTxSyncResponse extends TxData {
readonly hash: TxHash;
readonly hash: Uint8Array;
}
/**
@ -81,7 +81,7 @@ export function broadcastTxSyncSuccess(res: BroadcastTxSyncResponse): boolean {
export interface BroadcastTxCommitResponse {
readonly height: number;
readonly hash: TxHash;
readonly hash: Uint8Array;
readonly checkTx: TxData;
readonly deliverTx?: TxData;
}
@ -125,8 +125,8 @@ export interface StatusResponse {
* Try to keep this compatible to TxEvent
*/
export interface TxResponse {
readonly tx: TxBytes;
readonly hash: TxHash;
readonly tx: Uint8Array;
readonly hash: Uint8Array;
readonly height: number;
readonly index: number;
readonly result: TxData;
@ -150,8 +150,8 @@ export interface NewBlockEvent extends Block {}
export interface NewBlockHeaderEvent extends Header {}
export interface TxEvent {
readonly tx: TxBytes;
readonly hash: TxHash;
readonly tx: Uint8Array;
readonly hash: Uint8Array;
readonly height: number;
/** @deprecated this value is not set in Tendermint 0.34+ */
readonly index?: number;

View File

@ -1,21 +1,6 @@
// Types in this file are exported outside of the @cosmjs/tendermint-rpc package,
// e.g. as part of a request or response
import { As } from "type-tagger";
/**
* Merkle root
*/
export type BlockHash = Uint8Array & As<"block-hash">;
/** Raw transaction bytes */
export type TxBytes = Uint8Array & As<"tx-bytes">;
/**
* A raw tendermint transaction hash, currently 20 bytes
*/
export type TxHash = Uint8Array & As<"tx-hash">;
export interface ValidatorEd25519Pubkey {
readonly algorithm: "ed25519";
readonly data: Uint8Array;

View File

@ -2,12 +2,11 @@ import { JsonRpcRequest, JsonRpcSuccessResponse } from "@cosmjs/json-rpc";
import * as requests from "./requests";
import * as responses from "./responses";
import { SubscriptionEvent } from "./rpcclients";
import { BlockHash, TxBytes, TxHash } from "./types";
export interface Adaptor {
readonly params: Params;
readonly responses: Responses;
readonly hashTx: (tx: TxBytes) => TxHash;
readonly hashBlock: (header: responses.Header) => BlockHash;
readonly hashTx: (tx: Uint8Array) => Uint8Array;
readonly hashBlock: (header: responses.Header) => Uint8Array;
}
export declare type Encoder<T extends requests.Request> = (req: T) => JsonRpcRequest;
export declare type Decoder<T extends responses.Response> = (res: JsonRpcSuccessResponse) => T;

View File

@ -1,4 +1,3 @@
import { Header } from "../../responses";
import { BlockHash, TxBytes, TxHash } from "../../types";
export declare function hashTx(tx: TxBytes): TxHash;
export declare function hashBlock(header: Header): BlockHash;
export declare function hashTx(tx: Uint8Array): Uint8Array;
export declare function hashBlock(header: Header): Uint8Array;

View File

@ -74,8 +74,6 @@ export {
} from "./responses";
export { HttpClient, WebsocketClient } from "./rpcclients";
export {
TxBytes,
TxHash,
ValidatorEd25519Pubkey,
ValidatorEd25519Signature,
ValidatorPubkey,

View File

@ -1,5 +1,5 @@
import { ReadonlyDate } from "readonly-date";
import { TxBytes, TxHash, ValidatorPubkey, ValidatorSignature } from "./types";
import { ValidatorPubkey, ValidatorSignature } from "./types";
export declare type Response =
| AbciInfoResponse
| AbciQueryResponse
@ -57,7 +57,7 @@ export interface BlockchainResponse {
/** No data in here because RPC method BroadcastTxAsync "returns right away, with no response" */
export interface BroadcastTxAsyncResponse {}
export interface BroadcastTxSyncResponse extends TxData {
readonly hash: TxHash;
readonly hash: Uint8Array;
}
/**
* Returns true iff transaction made it sucessfully into the transaction pool
@ -65,7 +65,7 @@ export interface BroadcastTxSyncResponse extends TxData {
export declare function broadcastTxSyncSuccess(res: BroadcastTxSyncResponse): boolean;
export interface BroadcastTxCommitResponse {
readonly height: number;
readonly hash: TxHash;
readonly hash: Uint8Array;
readonly checkTx: TxData;
readonly deliverTx?: TxData;
}
@ -99,8 +99,8 @@ export interface StatusResponse {
* Try to keep this compatible to TxEvent
*/
export interface TxResponse {
readonly tx: TxBytes;
readonly hash: TxHash;
readonly tx: Uint8Array;
readonly hash: Uint8Array;
readonly height: number;
readonly index: number;
readonly result: TxData;
@ -117,8 +117,8 @@ export interface ValidatorsResponse {
export interface NewBlockEvent extends Block {}
export interface NewBlockHeaderEvent extends Header {}
export interface TxEvent {
readonly tx: TxBytes;
readonly hash: TxHash;
readonly tx: Uint8Array;
readonly hash: Uint8Array;
readonly height: number;
/** @deprecated this value is not set in Tendermint 0.34+ */
readonly index?: number;

View File

@ -1,14 +1,3 @@
import { As } from "type-tagger";
/**
* Merkle root
*/
export declare type BlockHash = Uint8Array & As<"block-hash">;
/** Raw transaction bytes */
export declare type TxBytes = Uint8Array & As<"tx-bytes">;
/**
* A raw tendermint transaction hash, currently 20 bytes
*/
export declare type TxHash = Uint8Array & As<"tx-hash">;
export interface ValidatorEd25519Pubkey {
readonly algorithm: "ed25519";
readonly data: Uint8Array;