From 03835af2d4a0c08d8c7f8a4654e6b39aa6be7875 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Wed, 22 Jun 2022 15:42:25 +0200 Subject: [PATCH] Add apiToBigInt --- .../tendermint-rpc/src/inthelpers.spec.ts | 40 +++++++++++++++++++ packages/tendermint-rpc/src/inthelpers.ts | 16 ++++++++ 2 files changed, 56 insertions(+) create mode 100644 packages/tendermint-rpc/src/inthelpers.spec.ts diff --git a/packages/tendermint-rpc/src/inthelpers.spec.ts b/packages/tendermint-rpc/src/inthelpers.spec.ts new file mode 100644 index 00000000..3c2a065e --- /dev/null +++ b/packages/tendermint-rpc/src/inthelpers.spec.ts @@ -0,0 +1,40 @@ +import { apiToBigInt } from "./inthelpers"; + +describe("inthelpers", () => { + describe("apiToBigInt", () => { + it("works for positive an negative ints", () => { + expect(apiToBigInt("0")).toEqual(BigInt(0)); + expect(apiToBigInt("1")).toEqual(BigInt(1)); + expect(apiToBigInt("100")).toEqual(BigInt(100)); + expect(apiToBigInt("-1")).toEqual(BigInt(-1)); + expect(apiToBigInt("-100")).toEqual(BigInt(-100)); + expect(apiToBigInt("9007199254740991")).toEqual(BigInt(9007199254740991)); + expect(apiToBigInt("-9007199254740991")).toEqual(BigInt(-9007199254740991)); + // uint64 max + expect(apiToBigInt("18446744073709551615")).toEqual(BigInt("18446744073709551615")); + // int64 min/max + expect(apiToBigInt("-9223372036854775808")).toEqual(BigInt("-9223372036854775808")); + expect(apiToBigInt("9223372036854775807")).toEqual(BigInt("9223372036854775807")); + }); + + it("throws for ill-formatted inputs", () => { + // empty + expect(() => apiToBigInt("")).toThrowError(/invalid string format/i); + expect(() => apiToBigInt("-")).toThrowError(/invalid string format/i); + + // non decimal representation + expect(() => apiToBigInt("0x0")).toThrowError(/invalid string format/i); + expect(() => apiToBigInt("0x01")).toThrowError(/invalid string format/i); + expect(() => apiToBigInt("0x")).toThrowError(/invalid string format/i); + + // decimal points + expect(() => apiToBigInt("1.0")).toThrowError(/invalid string format/i); + + // Invalid dashes + expect(() => apiToBigInt("1-")).toThrowError(/invalid string format/i); + expect(() => apiToBigInt("--1")).toThrowError(/invalid string format/i); + expect(() => apiToBigInt("1-1")).toThrowError(/invalid string format/i); + expect(() => apiToBigInt("-1-1")).toThrowError(/invalid string format/i); + }); + }); +}); diff --git a/packages/tendermint-rpc/src/inthelpers.ts b/packages/tendermint-rpc/src/inthelpers.ts index 0219da53..77be2e2e 100644 --- a/packages/tendermint-rpc/src/inthelpers.ts +++ b/packages/tendermint-rpc/src/inthelpers.ts @@ -1,5 +1,7 @@ import { Int53 } from "@cosmjs/math"; +import { assertString } from "./tendermint34/encodings"; + /** * Takes an integer value from the Tendermint RPC API and * returns it as number. @@ -11,6 +13,20 @@ export function apiToSmallInt(input: string | number): number { return asInt.toNumber(); } +/** + * Takes an integer value from the Tendermint RPC API and + * returns it as BigInt. + * + * This supports the full uint64 and int64 ranges. + */ +export function apiToBigInt(input: string): bigint { + assertString(input); // Runtime check on top of TypeScript just to be safe for semi-trusted API types + if (!input.match(/^-?[0-9]+$/)) { + throw new Error("Invalid string format"); + } + return BigInt(input); +} + /** * Takes an integer in the safe integer range and returns * a string representation to be used in the Tendermint RPC API.