diff --git a/packages/bcp/src/cosmwasmcodec.spec.ts b/packages/bcp/src/cosmwasmcodec.spec.ts index f3c29b4b..a061f7f3 100644 --- a/packages/bcp/src/cosmwasmcodec.spec.ts +++ b/packages/bcp/src/cosmwasmcodec.spec.ts @@ -1,30 +1,40 @@ +import { CosmosAddressBech32Prefix } from "@cosmwasm/sdk"; import { PostableBytes, PrehashType } from "@iov/bcp"; import { Encoding } from "@iov/encoding"; -import { cosmWasmCodec } from "./cosmwasmcodec"; +import { CosmWasmCodec } from "./cosmwasmcodec"; import { chainId, nonce, sendTxJson, signedTxBin, signedTxEncodedJson, signedTxJson } from "./testdata.spec"; +import { BankTokens } from "./types"; const { toUtf8 } = Encoding; -describe("cosmWasmCodec", () => { +const defaultPrefix = "cosmos" as CosmosAddressBech32Prefix; + +const defaultTokens: BankTokens = [ + { + fractionalDigits: 6, + ticker: "ATOM", + denom: "uatom", + }, +]; + +describe("CosmWasmCodec", () => { + const codec = new CosmWasmCodec(defaultPrefix, defaultTokens); + describe("isValidAddress", () => { it("accepts valid addresses", () => { - expect(cosmWasmCodec.isValidAddress("cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6")).toEqual(true); - expect(cosmWasmCodec.isValidAddress("cosmosvalcons10q82zkzzmaku5lazhsvxv7hsg4ntpuhdwadmss")).toEqual( - true, - ); - expect(cosmWasmCodec.isValidAddress("cosmosvaloper17mggn4znyeyg25wd7498qxl7r2jhgue8u4qjcq")).toEqual( - true, - ); + expect(codec.isValidAddress("cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs6")).toEqual(true); + expect(codec.isValidAddress("cosmosvalcons10q82zkzzmaku5lazhsvxv7hsg4ntpuhdwadmss")).toEqual(true); + expect(codec.isValidAddress("cosmosvaloper17mggn4znyeyg25wd7498qxl7r2jhgue8u4qjcq")).toEqual(true); }); it("rejects invalid addresses", () => { // Bad size - expect(cosmWasmCodec.isValidAddress("cosmos10q82zkzzmaku5lazhsvxv7hsg4ntpuhh8289f")).toEqual(false); + expect(codec.isValidAddress("cosmos10q82zkzzmaku5lazhsvxv7hsg4ntpuhh8289f")).toEqual(false); // Bad checksum - expect(cosmWasmCodec.isValidAddress("cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs7")).toEqual(false); + expect(codec.isValidAddress("cosmos1pkptre7fdkl6gfrzlesjjvhxhlc3r4gmmk8rs7")).toEqual(false); // Bad prefix - expect(cosmWasmCodec.isValidAddress("cosmot10q82zkzzmaku5lazhsvxv7hsg4ntpuhd8j5266")).toEqual(false); + expect(codec.isValidAddress("cosmot10q82zkzzmaku5lazhsvxv7hsg4ntpuhd8j5266")).toEqual(false); }); }); @@ -36,32 +46,32 @@ describe("cosmWasmCodec", () => { ), prehashType: PrehashType.Sha256, }; - expect(cosmWasmCodec.bytesToSign(sendTxJson, nonce)).toEqual(expected); + expect(codec.bytesToSign(sendTxJson, nonce)).toEqual(expected); }); }); describe("bytesToPost", () => { it("works for SendTransaction via bank module", () => { - const encoded = cosmWasmCodec.bytesToPost(signedTxJson); + const encoded = codec.bytesToPost(signedTxJson); expect(encoded).toEqual(signedTxEncodedJson); }); }); describe("parseBytes", () => { it("throws when trying to decode a transaction without a nonce", () => { - expect(() => cosmWasmCodec.parseBytes(signedTxBin as PostableBytes, chainId)).toThrowError( + expect(() => codec.parseBytes(signedTxBin as PostableBytes, chainId)).toThrowError( /nonce is required/i, ); }); it("properly decodes transactions", () => { - const decoded = cosmWasmCodec.parseBytes(signedTxEncodedJson as PostableBytes, chainId, nonce); + const decoded = codec.parseBytes(signedTxEncodedJson as PostableBytes, chainId, nonce); expect(decoded).toEqual(signedTxJson); }); it("round trip works", () => { - const encoded = cosmWasmCodec.bytesToPost(signedTxJson); - const decoded = cosmWasmCodec.parseBytes(encoded, chainId, nonce); + const encoded = codec.bytesToPost(signedTxJson); + const decoded = codec.parseBytes(encoded, chainId, nonce); expect(decoded).toEqual(signedTxJson); }); }); diff --git a/packages/bcp/src/cosmwasmcodec.ts b/packages/bcp/src/cosmwasmcodec.ts index 2df8f0ef..447694c6 100644 --- a/packages/bcp/src/cosmwasmcodec.ts +++ b/packages/bcp/src/cosmwasmcodec.ts @@ -101,5 +101,9 @@ const defaultTokens: BankTokens = [ }, ]; -/** Unconfigured codec is useful for testing only */ +/** + * Unconfigured codec is useful for testing only + * + * @deprecated use CosmWasmCodec constructor + */ export const cosmWasmCodec = new CosmWasmCodec(defaultPrefix, defaultTokens); diff --git a/packages/bcp/src/cosmwasmconnection.spec.ts b/packages/bcp/src/cosmwasmconnection.spec.ts index dee2d922..9bf71d95 100644 --- a/packages/bcp/src/cosmwasmconnection.spec.ts +++ b/packages/bcp/src/cosmwasmconnection.spec.ts @@ -184,6 +184,7 @@ describe("CosmWasmConnection", () => { it("calculates tx hash from PostableBytes", async () => { pendingWithoutCosmos(); const connection = await CosmWasmConnection.establish(httpUrl, defaultPrefix, defaultConfig); + // tslint:disable-next-line: deprecation const postable = cosmWasmCodec.bytesToPost(signedTxJson); const id = await connection.identifier(postable); expect(id).toMatch(/^[0-9A-F]{64}$/); @@ -257,6 +258,7 @@ describe("CosmWasmConnection", () => { const profile = new UserProfile(); const wallet = profile.addWallet(Secp256k1HdWallet.fromMnemonic(faucetMnemonic)); const faucet = await profile.createIdentity(wallet.id, defaultChainId, faucetPath); + // tslint:disable-next-line: deprecation const faucetAddress = cosmWasmCodec.identityToAddress(faucet); const unsigned = await connection.withDefaultFee({ @@ -322,6 +324,7 @@ describe("CosmWasmConnection", () => { const profile = new UserProfile(); const wallet = profile.addWallet(Secp256k1HdWallet.fromMnemonic(faucetMnemonic)); const faucet = await profile.createIdentity(wallet.id, defaultChainId, faucetPath); + // tslint:disable-next-line: deprecation const faucetAddress = cosmWasmCodec.identityToAddress(faucet); const unsigned = await connection.withDefaultFee({ diff --git a/packages/bcp/types/cosmwasmcodec.d.ts b/packages/bcp/types/cosmwasmcodec.d.ts index 6161b65d..e9429a00 100644 --- a/packages/bcp/types/cosmwasmcodec.d.ts +++ b/packages/bcp/types/cosmwasmcodec.d.ts @@ -23,5 +23,9 @@ export declare class CosmWasmCodec implements TxCodec { identityToAddress(identity: Identity): Address; isValidAddress(address: string): boolean; } -/** Unconfigured codec is useful for testing only */ +/** + * Unconfigured codec is useful for testing only + * + * @deprecated use CosmWasmCodec constructor + */ export declare const cosmWasmCodec: CosmWasmCodec;