Rename cosmos* types to cosmwasm*

This commit is contained in:
Simon Warta 2020-01-29 13:41:17 +01:00
parent ffda6e2baf
commit 7c8ddf8019
10 changed files with 55 additions and 51 deletions

View File

@ -1,12 +1,12 @@
import { PostableBytes, PrehashType } from "@iov/bcp";
import { Encoding } from "@iov/encoding";
import { cosmosCodec } from "./cosmoscodec";
import { cosmWasmCodec } from "./cosmwasmcodec";
import { chainId, nonce, sendTxJson, signedTxBin, signedTxJson, txId } from "./testdata.spec";
const { toUtf8 } = Encoding;
describe("cosmoscodec", () => {
describe("cosmWasmCodec", () => {
it("properly generates bytes to sign", () => {
const expected = {
bytes: toUtf8(
@ -14,36 +14,36 @@ describe("cosmoscodec", () => {
),
prehashType: PrehashType.Sha256,
};
const bytesToSign = cosmosCodec.bytesToSign(sendTxJson, nonce);
const bytesToSign = cosmWasmCodec.bytesToSign(sendTxJson, nonce);
expect(bytesToSign).toEqual(expected);
});
it("properly encodes transactions", () => {
const encoded = cosmosCodec.bytesToPost(signedTxJson);
const encoded = cosmWasmCodec.bytesToPost(signedTxJson);
expect(encoded).toEqual(signedTxBin);
});
it("throws when trying to decode a transaction without a nonce", () => {
expect(() => cosmosCodec.parseBytes(signedTxBin as PostableBytes, chainId)).toThrowError(
expect(() => cosmWasmCodec.parseBytes(signedTxBin as PostableBytes, chainId)).toThrowError(
/nonce is required/i,
);
});
it("properly decodes transactions", () => {
const decoded = cosmosCodec.parseBytes(signedTxBin as PostableBytes, chainId, nonce);
const decoded = cosmWasmCodec.parseBytes(signedTxBin as PostableBytes, chainId, nonce);
expect(decoded).toEqual(signedTxJson);
});
it("generates transaction id", () => {
const id = cosmosCodec.identifier(signedTxJson);
const id = cosmWasmCodec.identifier(signedTxJson);
expect(id).toMatch(/^[0-9A-F]{64}$/);
expect(id).toEqual(txId);
});
it("round trip works", () => {
const encoded = cosmosCodec.bytesToPost(signedTxJson);
const decoded = cosmosCodec.parseBytes(encoded, chainId, nonce);
const encoded = cosmWasmCodec.bytesToPost(signedTxJson);
const decoded = cosmWasmCodec.parseBytes(encoded, chainId, nonce);
expect(decoded).toEqual(signedTxJson);
});
});

View File

@ -44,7 +44,7 @@ function sortJson(json: any): any {
return result;
}
export class CosmosCodec implements TxCodec {
export class CosmWasmCodec implements TxCodec {
private readonly prefix: CosmosBech32Prefix;
private readonly tokens: TokenInfos;
@ -113,4 +113,4 @@ const defaultTokens: TokenInfos = [
},
];
export const cosmosCodec = new CosmosCodec(defaultPrefix, defaultTokens);
export const cosmWasmCodec = new CosmWasmCodec(defaultPrefix, defaultTokens);

View File

@ -15,8 +15,8 @@ import { Encoding } from "@iov/encoding";
import { HdPaths, Secp256k1HdWallet, UserProfile } from "@iov/keycontrol";
import { CosmosBech32Prefix } from "./address";
import { CosmosCodec, cosmosCodec } from "./cosmoscodec";
import { CosmosConnection } from "./cosmosconnection";
import { CosmWasmCodec, cosmWasmCodec } from "./cosmwasmcodec";
import { CosmWasmConnection } from "./cosmwasmconnection";
import { nonceToSequence, TokenInfos } from "./types";
const { fromBase64, toHex } = Encoding;
@ -27,7 +27,7 @@ function pendingWithoutCosmos(): void {
}
}
describe("CosmosConnection", () => {
describe("CosmWasmConnection", () => {
const cosm = "COSM" as TokenTicker;
const httpUrl = "http://localhost:1317";
const defaultChainId = "cosmos:testing" as ChainId;
@ -63,7 +63,7 @@ describe("CosmosConnection", () => {
describe("establish", () => {
it("can connect to Cosmos via http", async () => {
pendingWithoutCosmos();
const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultTokens);
expect(connection).toBeTruthy();
connection.disconnect();
});
@ -72,7 +72,7 @@ describe("CosmosConnection", () => {
describe("chainId", () => {
it("displays the chain ID", async () => {
pendingWithoutCosmos();
const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const chainId = connection.chainId();
expect(chainId).toEqual(defaultChainId);
connection.disconnect();
@ -82,7 +82,7 @@ describe("CosmosConnection", () => {
describe("height", () => {
it("displays the current height", async () => {
pendingWithoutCosmos();
const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const height = await connection.height();
expect(height).toBeGreaterThan(0);
connection.disconnect();
@ -92,7 +92,7 @@ describe("CosmosConnection", () => {
describe("getToken", () => {
it("displays a given token", async () => {
pendingWithoutCosmos();
const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const token = await connection.getToken("COSM" as TokenTicker);
expect(token).toEqual({
fractionalDigits: 6,
@ -104,7 +104,7 @@ describe("CosmosConnection", () => {
it("resolves to undefined if the token is not supported", async () => {
pendingWithoutCosmos();
const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const token = await connection.getToken("whatever" as TokenTicker);
expect(token).toBeUndefined();
connection.disconnect();
@ -114,7 +114,7 @@ describe("CosmosConnection", () => {
describe("getAllTokens", () => {
it("resolves to a list of all supported tokens", async () => {
pendingWithoutCosmos();
const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const tokens = await connection.getAllTokens();
// TODO: make this more flexible
expect(tokens).toEqual([
@ -136,7 +136,7 @@ describe("CosmosConnection", () => {
describe("getAccount", () => {
it("gets an empty account by address", async () => {
pendingWithoutCosmos();
const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const account = await connection.getAccount({ address: defaultEmptyAddress });
expect(account).toBeUndefined();
connection.disconnect();
@ -144,7 +144,7 @@ describe("CosmosConnection", () => {
it("gets an account by address", async () => {
pendingWithoutCosmos();
const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const account = await connection.getAccount({ address: defaultAddress });
if (account === undefined) {
throw new Error("Expected account not to be undefined");
@ -161,7 +161,7 @@ describe("CosmosConnection", () => {
it("gets an account by pubkey", async () => {
pendingWithoutCosmos();
const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const account = await connection.getAccount({ pubkey: defaultPubkey });
if (account === undefined) {
throw new Error("Expected account not to be undefined");
@ -180,11 +180,11 @@ describe("CosmosConnection", () => {
describe("integration tests", () => {
it("can post and get a transaction", async () => {
pendingWithoutCosmos();
const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const profile = new UserProfile();
const wallet = profile.addWallet(Secp256k1HdWallet.fromMnemonic(faucetMnemonic));
const faucet = await profile.createIdentity(wallet.id, defaultChainId, faucetPath);
const faucetAddress = cosmosCodec.identityToAddress(faucet);
const faucetAddress = cosmWasmCodec.identityToAddress(faucet);
const unsigned = await connection.withDefaultFee<SendTransaction>({
kind: "bcp/send",
@ -200,7 +200,7 @@ describe("CosmosConnection", () => {
});
const nonce = await connection.getNonce({ address: faucetAddress });
// TODO: we need to use custom codecs everywhere
const codec = new CosmosCodec(defaultPrefix, defaultTokens);
const codec = new CosmWasmCodec(defaultPrefix, defaultTokens);
const signed = await profile.signTransaction(faucet, unsigned, codec, nonce);
const postableBytes = codec.bytesToPost(signed);
const response = await connection.postTx(postableBytes);
@ -243,11 +243,11 @@ describe("CosmosConnection", () => {
it("can post and search for a transaction", async () => {
pendingWithoutCosmos();
const connection = await CosmosConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultTokens);
const profile = new UserProfile();
const wallet = profile.addWallet(Secp256k1HdWallet.fromMnemonic(faucetMnemonic));
const faucet = await profile.createIdentity(wallet.id, defaultChainId, faucetPath);
const faucetAddress = cosmosCodec.identityToAddress(faucet);
const faucetAddress = cosmWasmCodec.identityToAddress(faucet);
const unsigned = await connection.withDefaultFee<SendTransaction>({
kind: "bcp/send",
@ -263,7 +263,7 @@ describe("CosmosConnection", () => {
});
const nonce = await connection.getNonce({ address: faucetAddress });
// TODO: we need to use custom codecs everywhere
const codec = new CosmosCodec(defaultPrefix, defaultTokens);
const codec = new CosmWasmCodec(defaultPrefix, defaultTokens);
const signed = await profile.signTransaction(faucet, unsigned, codec, nonce);
const postableBytes = codec.bytesToPost(signed);
const response = await connection.postTx(postableBytes);

View File

@ -68,16 +68,16 @@ function buildQueryString({
return components.filter(Boolean).join("&");
}
export class CosmosConnection implements BlockchainConnection {
export class CosmWasmConnection implements BlockchainConnection {
// we must know prefix and tokens a priori to understand the chain
public static async establish(
url: string,
prefix: CosmosBech32Prefix,
tokenInfo: TokenInfos,
): Promise<CosmosConnection> {
): Promise<CosmWasmConnection> {
const restClient = new RestClient(url);
const chainData = await this.initialize(restClient);
return new CosmosConnection(restClient, chainData, prefix, tokenInfo);
return new CosmWasmConnection(restClient, chainData, prefix, tokenInfo);
}
private static async initialize(restClient: RestClient): Promise<ChainData> {

View File

@ -1,22 +1,22 @@
import { ChainConnector, ChainId } from "@iov/bcp";
import { CosmosBech32Prefix } from "./address";
import { cosmosCodec } from "./cosmoscodec";
import { CosmosConnection } from "./cosmosconnection";
import { cosmWasmCodec } from "./cosmwasmcodec";
import { CosmWasmConnection } from "./cosmwasmconnection";
import { TokenInfos } from "./types";
/**
* A helper to connect to a cosmos-based chain at a given url
*/
export function createCosmosConnector(
export function createCosmWasmConnector(
url: string,
prefix: CosmosBech32Prefix,
tokenInfo: TokenInfos,
expectedChainId?: ChainId,
): ChainConnector<CosmosConnection> {
): ChainConnector<CosmWasmConnection> {
return {
establishConnection: async () => CosmosConnection.establish(url, prefix, tokenInfo),
codec: cosmosCodec,
establishConnection: async () => CosmWasmConnection.establish(url, prefix, tokenInfo),
codec: cosmWasmCodec,
expectedChainId: expectedChainId,
};
}

View File

@ -1,3 +1,3 @@
export { cosmosCodec, CosmosCodec } from "./cosmoscodec";
export { CosmosConnection } from "./cosmosconnection";
export { createCosmosConnector } from "./cosmosconnector";
export { cosmWasmCodec, CosmWasmCodec } from "./cosmwasmcodec";
export { CosmWasmConnection } from "./cosmwasmconnection";
export { createCosmWasmConnector } from "./cosmwasmconnector";

View File

@ -12,7 +12,7 @@ import {
} from "@iov/bcp";
import { CosmosBech32Prefix } from "./address";
import { TokenInfos } from "./types";
export declare class CosmosCodec implements TxCodec {
export declare class CosmWasmCodec implements TxCodec {
private readonly prefix;
private readonly tokens;
constructor(prefix: CosmosBech32Prefix, tokens: TokenInfos);
@ -23,4 +23,4 @@ export declare class CosmosCodec implements TxCodec {
identityToAddress(identity: Identity): Address;
isValidAddress(address: string): boolean;
}
export declare const cosmosCodec: CosmosCodec;
export declare const cosmWasmCodec: CosmWasmCodec;

View File

@ -22,8 +22,12 @@ import {
import { Stream } from "xstream";
import { CosmosBech32Prefix } from "./address";
import { TokenInfos } from "./types";
export declare class CosmosConnection implements BlockchainConnection {
static establish(url: string, prefix: CosmosBech32Prefix, tokenInfo: TokenInfos): Promise<CosmosConnection>;
export declare class CosmWasmConnection implements BlockchainConnection {
static establish(
url: string,
prefix: CosmosBech32Prefix,
tokenInfo: TokenInfos,
): Promise<CosmWasmConnection>;
private static initialize;
private readonly restClient;
private readonly chainData;

View File

@ -1,13 +1,13 @@
import { ChainConnector, ChainId } from "@iov/bcp";
import { CosmosBech32Prefix } from "./address";
import { CosmosConnection } from "./cosmosconnection";
import { CosmWasmConnection } from "./cosmwasmconnection";
import { TokenInfos } from "./types";
/**
* A helper to connect to a cosmos-based chain at a given url
*/
export declare function createCosmosConnector(
export declare function createCosmWasmConnector(
url: string,
prefix: CosmosBech32Prefix,
tokenInfo: TokenInfos,
expectedChainId?: ChainId,
): ChainConnector<CosmosConnection>;
): ChainConnector<CosmWasmConnection>;

View File

@ -1,3 +1,3 @@
export { cosmosCodec, CosmosCodec } from "./cosmoscodec";
export { CosmosConnection } from "./cosmosconnection";
export { createCosmosConnector } from "./cosmosconnector";
export { cosmWasmCodec, CosmWasmCodec } from "./cosmwasmcodec";
export { CosmWasmConnection } from "./cosmwasmconnection";
export { createCosmWasmConnector } from "./cosmwasmconnector";