diff --git a/CHANGELOG.md b/CHANGELOG.md index 053f063d..5d39c116 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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) diff --git a/packages/tendermint-rpc/src/adaptor.ts b/packages/tendermint-rpc/src/adaptor.ts index 4f188d7c..27c9e4f3 100644 --- a/packages/tendermint-rpc/src/adaptor.ts +++ b/packages/tendermint-rpc/src/adaptor.ts @@ -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 diff --git a/packages/tendermint-rpc/src/adaptors/v0-33/hasher.spec.ts b/packages/tendermint-rpc/src/adaptors/v0-33/hasher.spec.ts index 6793f8b3..1933923f 100644 --- a/packages/tendermint-rpc/src/adaptors/v0-33/hasher.spec.ts +++ b/packages/tendermint-rpc/src/adaptors/v0-33/hasher.spec.ts @@ -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); }); diff --git a/packages/tendermint-rpc/src/adaptors/v0-33/hasher.ts b/packages/tendermint-rpc/src/adaptors/v0-33/hasher.ts index 3fc0c6ce..00525d2e 100644 --- a/packages/tendermint-rpc/src/adaptors/v0-33/hasher.ts +++ b/packages/tendermint-rpc/src/adaptors/v0-33/hasher.ts @@ -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); } diff --git a/packages/tendermint-rpc/src/adaptors/v0-33/responses.ts b/packages/tendermint-rpc/src/adaptors/v0-33/responses.ts index 7946826d..79982127 100644 --- a/packages/tendermint-rpc/src/adaptors/v0-33/responses.ts +++ b/packages/tendermint-rpc/src/adaptors/v0-33/responses.ts @@ -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), diff --git a/packages/tendermint-rpc/src/client.spec.ts b/packages/tendermint-rpc/src/client.spec.ts index 4e0322ac..40ae694d 100644 --- a/packages/tendermint-rpc/src/client.spec.ts +++ b/packages/tendermint-rpc/src/client.spec.ts @@ -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 { 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 { diff --git a/packages/tendermint-rpc/src/index.ts b/packages/tendermint-rpc/src/index.ts index 3afb8913..46d92f4c 100644 --- a/packages/tendermint-rpc/src/index.ts +++ b/packages/tendermint-rpc/src/index.ts @@ -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, diff --git a/packages/tendermint-rpc/src/responses.ts b/packages/tendermint-rpc/src/responses.ts index 4cea1c9d..6b940fcb 100644 --- a/packages/tendermint-rpc/src/responses.ts +++ b/packages/tendermint-rpc/src/responses.ts @@ -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; diff --git a/packages/tendermint-rpc/src/types.ts b/packages/tendermint-rpc/src/types.ts index f5d55e5d..bc4d62fb 100644 --- a/packages/tendermint-rpc/src/types.ts +++ b/packages/tendermint-rpc/src/types.ts @@ -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; diff --git a/packages/tendermint-rpc/types/adaptor.d.ts b/packages/tendermint-rpc/types/adaptor.d.ts index 9dde507b..4adad234 100644 --- a/packages/tendermint-rpc/types/adaptor.d.ts +++ b/packages/tendermint-rpc/types/adaptor.d.ts @@ -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 = (req: T) => JsonRpcRequest; export declare type Decoder = (res: JsonRpcSuccessResponse) => T; diff --git a/packages/tendermint-rpc/types/adaptors/v0-33/hasher.d.ts b/packages/tendermint-rpc/types/adaptors/v0-33/hasher.d.ts index f89426d8..2104d509 100644 --- a/packages/tendermint-rpc/types/adaptors/v0-33/hasher.d.ts +++ b/packages/tendermint-rpc/types/adaptors/v0-33/hasher.d.ts @@ -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; diff --git a/packages/tendermint-rpc/types/index.d.ts b/packages/tendermint-rpc/types/index.d.ts index 7c0edb71..c4de41bc 100644 --- a/packages/tendermint-rpc/types/index.d.ts +++ b/packages/tendermint-rpc/types/index.d.ts @@ -74,8 +74,6 @@ export { } from "./responses"; export { HttpClient, WebsocketClient } from "./rpcclients"; export { - TxBytes, - TxHash, ValidatorEd25519Pubkey, ValidatorEd25519Signature, ValidatorPubkey, diff --git a/packages/tendermint-rpc/types/responses.d.ts b/packages/tendermint-rpc/types/responses.d.ts index 52024db5..c9fbcaaf 100644 --- a/packages/tendermint-rpc/types/responses.d.ts +++ b/packages/tendermint-rpc/types/responses.d.ts @@ -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; diff --git a/packages/tendermint-rpc/types/types.d.ts b/packages/tendermint-rpc/types/types.d.ts index 412bee15..784cbe78 100644 --- a/packages/tendermint-rpc/types/types.d.ts +++ b/packages/tendermint-rpc/types/types.d.ts @@ -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;