diff --git a/CHANGELOG.md b/CHANGELOG.md index 1cae3f29a4..b15d26cb89 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,10 @@ - @cosmjs/proto-signing: Add new package for handling transaction signing with protobuf encoding. - @cosmjs/stargate: Add new package for Cosmos SDK Stargate support. +- @cosmjs/tendermint-rpc: Make `Client.detectVersion` private and let it return + a version instead of a client. +- @cosmjs/tendermint-rpc: Add `Client.create` for creating a Tendermint client + given an RPC client. ## 0.23.1 (2020-10-27) diff --git a/packages/tendermint-rpc/src/client.spec.ts b/packages/tendermint-rpc/src/client.spec.ts index 8c1c1a37b6..b8d0235398 100644 --- a/packages/tendermint-rpc/src/client.spec.ts +++ b/packages/tendermint-rpc/src/client.spec.ts @@ -42,6 +42,16 @@ function randomString(): string { } function defaultTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor, expected: ExpectedValues): void { + describe("create", () => { + it("can auto-discover Tendermint version and communicate", async () => { + pendingWithoutTendermint(); + const client = await Client.create(rpcFactory()); + const info = await client.abciInfo(); + expect(info).toBeTruthy(); + client.disconnect(); + }); + }); + it("can connect to tendermint with known version", async () => { pendingWithoutTendermint(); const client = new Client(rpcFactory(), adaptor); @@ -49,14 +59,6 @@ function defaultTestSuite(rpcFactory: () => RpcClient, adaptor: Adaptor, expecte client.disconnect(); }); - it("can auto-discover tendermint version and connect", async () => { - pendingWithoutTendermint(); - const client = await Client.detectVersion(rpcFactory()); - const info = await client.abciInfo(); - expect(info).toBeTruthy(); - client.disconnect(); - }); - it("can get genesis", async () => { pendingWithoutTendermint(); const client = new Client(rpcFactory(), adaptor); diff --git a/packages/tendermint-rpc/src/client.ts b/packages/tendermint-rpc/src/client.ts index b5845a751c..0b158c1e85 100644 --- a/packages/tendermint-rpc/src/client.ts +++ b/packages/tendermint-rpc/src/client.ts @@ -14,13 +14,26 @@ import { } from "./rpcclients"; export class Client { + /** + * Creates a new Tendermint client for the given endpoint. + * + * Uses HTTP when the URL schema is http or https. Uses WebSockets otherwise. + */ public static async connect(url: string): Promise { const useHttp = url.startsWith("http://") || url.startsWith("https://"); - const client = useHttp ? new HttpClient(url) : new WebsocketClient(url); - return this.detectVersion(client); + const rpcClient = useHttp ? new HttpClient(url) : new WebsocketClient(url); + return Client.create(rpcClient); } - public static async detectVersion(client: RpcClient): Promise { + /** + * Creates a new Tendermint client given an RPC client. + */ + public static async create(rpcClient: RpcClient): Promise { + const version = await this.detectVersion(rpcClient); + return new Client(rpcClient, adaptorForVersion(version)); + } + + private static async detectVersion(client: RpcClient): Promise { const req = createJsonRpcRequest(requests.Method.Status); const response = await client.execute(req); const result = response.result; @@ -33,8 +46,7 @@ export class Client { if (typeof version !== "string") { throw new Error("Unrecognized version format: must be string"); } - - return new Client(client, adaptorForVersion(version)); + return version; } private readonly client: RpcClient; diff --git a/packages/tendermint-rpc/types/client.d.ts b/packages/tendermint-rpc/types/client.d.ts index 92606f944f..5612c85392 100644 --- a/packages/tendermint-rpc/types/client.d.ts +++ b/packages/tendermint-rpc/types/client.d.ts @@ -4,8 +4,17 @@ import * as requests from "./requests"; import * as responses from "./responses"; import { RpcClient } from "./rpcclients"; export declare class Client { + /** + * Creates a new Tendermint client for the given endpoint. + * + * Uses HTTP when the URL schema is http or https. Uses WebSockets otherwise. + */ static connect(url: string): Promise; - static detectVersion(client: RpcClient): Promise; + /** + * Creates a new Tendermint client given an RPC client. + */ + static create(rpcClient: RpcClient): Promise; + private static detectVersion; private readonly client; private readonly p; private readonly r;