diff --git a/packages/proto-signing/src/coins.spec.ts b/packages/proto-signing/src/coins.spec.ts deleted file mode 100644 index 5f74c4cb..00000000 --- a/packages/proto-signing/src/coins.spec.ts +++ /dev/null @@ -1,107 +0,0 @@ -import { coin, coins, parseCoins } from "./coins"; - -describe("coins", () => { - describe("coin", () => { - it("works for basic values", () => { - expect(coin(123, "utoken")).toEqual({ amount: "123", denom: "utoken" }); - expect(coin(123.0, "utoken")).toEqual({ amount: "123", denom: "utoken" }); - expect(coin(Number.MAX_SAFE_INTEGER, "utoken")).toEqual({ - amount: "9007199254740991", - denom: "utoken", - }); - expect(coin(+0, "utoken")).toEqual({ amount: "0", denom: "utoken" }); - expect(coin(-0, "utoken")).toEqual({ amount: "0", denom: "utoken" }); - }); - - it("throws for non-safe-integer values", () => { - expect(() => coin(1.23, "utoken")).toThrow(); - expect(() => coin(NaN, "utoken")).toThrow(); - expect(() => coin(Number.POSITIVE_INFINITY, "utoken")).toThrow(); - expect(() => coin(Number.MAX_SAFE_INTEGER + 1, "utoken")).toThrow(); - }); - - it("throws for negative values", () => { - expect(() => coin(-1, "utoken")).toThrow(); - expect(() => coin(Number.MIN_SAFE_INTEGER, "utoken")).toThrow(); - expect(() => coin(Number.NEGATIVE_INFINITY, "utoken")).toThrow(); - }); - }); - - describe("coins", () => { - it("returns one element array of coin", () => { - expect(coins(123, "utoken")).toEqual([{ amount: "123", denom: "utoken" }]); - }); - }); - - describe("parseCoins", () => { - it("works for empty", () => { - expect(parseCoins("")).toEqual([]); - }); - - it("works for one element", () => { - expect(parseCoins("7643ureef")).toEqual([ - { - amount: "7643", - denom: "ureef", - }, - ]); - }); - - it("works for two", () => { - expect(parseCoins("819966000ucosm,700000000ustake")).toEqual([ - { - amount: "819966000", - denom: "ucosm", - }, - { - amount: "700000000", - denom: "ustake", - }, - ]); - }); - - it("ignores empty elements", () => { - // start - expect(parseCoins(",819966000ucosm,700000000ustake")).toEqual([ - { - amount: "819966000", - denom: "ucosm", - }, - { - amount: "700000000", - denom: "ustake", - }, - ]); - // middle - expect(parseCoins("819966000ucosm,,700000000ustake")).toEqual([ - { - amount: "819966000", - denom: "ucosm", - }, - { - amount: "700000000", - denom: "ustake", - }, - ]); - // end - expect(parseCoins("819966000ucosm,700000000ustake,")).toEqual([ - { - amount: "819966000", - denom: "ucosm", - }, - { - amount: "700000000", - denom: "ustake", - }, - ]); - }); - - it("throws for invalid inputs", () => { - // denom missing - expect(() => parseCoins("3456")).toThrowError(/invalid coin string/i); - - // amount missing - expect(() => parseCoins("ucosm")).toThrowError(/invalid coin string/i); - }); - }); -}); diff --git a/packages/proto-signing/src/coins.ts b/packages/proto-signing/src/coins.ts deleted file mode 100644 index ec6881c0..00000000 --- a/packages/proto-signing/src/coins.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { Uint53, Uint64 } from "@cosmjs/math"; - -/** - * This is the same as Coin from @cosmjs/launchpad but those might diverge in the future. - */ -export interface Coin { - readonly denom: string; - readonly amount: string; -} - -/** - * Creates a coin. - * - * This is the same as coin from @cosmjs/launchpad but those might diverge in the future. - */ -export function coin(amount: number, denom: string): Coin { - return { amount: new Uint53(amount).toString(), denom: denom }; -} - -/** - * Creates a list of coins with one element - * - * This is the same as coins from @cosmjs/launchpad but those might diverge in the future. - */ -export function coins(amount: number, denom: string): Coin[] { - return [coin(amount, denom)]; -} - -/** - * Takes a coins list like "819966000ucosm,700000000ustake" and parses it - * - * This is the same as parseCoins from @cosmjs/launchpad but those might diverge in the future. - */ -export function parseCoins(input: string): Coin[] { - return input - .replace(/\s/g, "") - .split(",") - .filter(Boolean) - .map((part) => { - const match = part.match(/^([0-9]+)([a-zA-Z]+)/); - if (!match) throw new Error("Got an invalid coin string"); - return { - amount: Uint64.fromString(match[1]).toString(), - denom: match[2], - }; - }); -} diff --git a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts index 59b86612..1c6e99db 100644 --- a/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1hdwallet.spec.ts @@ -1,7 +1,7 @@ +import { coins } from "@cosmjs/amino"; import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto"; import { fromBase64, fromHex } from "@cosmjs/encoding"; -import { coins } from "./coins"; import { DirectSecp256k1HdWallet } from "./directsecp256k1hdwallet"; import { makeAuthInfoBytes, makeSignBytes, makeSignDoc } from "./signing"; import { faucet, testVectors } from "./testutils.spec"; diff --git a/packages/proto-signing/src/directsecp256k1wallet.spec.ts b/packages/proto-signing/src/directsecp256k1wallet.spec.ts index a90d123c..933403e8 100644 --- a/packages/proto-signing/src/directsecp256k1wallet.spec.ts +++ b/packages/proto-signing/src/directsecp256k1wallet.spec.ts @@ -1,8 +1,8 @@ /* eslint-disable @typescript-eslint/naming-convention */ +import { coins } from "@cosmjs/amino"; import { Secp256k1, Secp256k1Signature, sha256 } from "@cosmjs/crypto"; import { fromBase64, fromHex } from "@cosmjs/encoding"; -import { coins } from "./coins"; import { DirectSecp256k1Wallet } from "./directsecp256k1wallet"; import { makeAuthInfoBytes, makeSignBytes, makeSignDoc } from "./signing"; import { testVectors } from "./testutils.spec"; diff --git a/packages/proto-signing/src/index.ts b/packages/proto-signing/src/index.ts index a27372db..ea5182c9 100644 --- a/packages/proto-signing/src/index.ts +++ b/packages/proto-signing/src/index.ts @@ -1,4 +1,6 @@ -export { Coin, coin, coins, parseCoins } from "./coins"; +// This type happens to be shared between Amino and Direct sign modes +export { Coin, coin, coins, parseCoins } from "@cosmjs/amino"; + export { isPbjsGeneratedType, isTsProtoGeneratedType,