From 1c2f4344af4e70c0a851fd6a270b7568fb58c482 Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Tue, 28 Jun 2022 14:03:17 +0200 Subject: [PATCH] Add {Uint32,Int53,Uint53,Uint64}.toBigInt --- CHANGELOG.md | 1 + packages/math/src/integers.spec.ts | 51 ++++++++++++++++++++++++++++++ packages/math/src/integers.ts | 17 ++++++++++ 3 files changed, 69 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 41e0d930..3cb8302c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,6 +14,7 @@ and this project adheres to `AbciQueryResponse`. - @cosmjs/cosmwasm-stargate: Add `SigningCosmWasmClient.executeMultiple` ([#1072]). +- @cosmjs/math: Add `{Uint32,Int53,Uint53,Uint64}.toBigInt` converter methods. [#1072]: https://github.com/cosmos/cosmjs/issues/1072 [#1176]: https://github.com/cosmos/cosmjs/pull/1176 diff --git a/packages/math/src/integers.spec.ts b/packages/math/src/integers.spec.ts index dd109782..87d50da8 100644 --- a/packages/math/src/integers.spec.ts +++ b/packages/math/src/integers.spec.ts @@ -120,6 +120,16 @@ describe("Integers", () => { expect(new Uint32(4294967295).toNumber()).toEqual(4294967295); }); + it("can convert to BigInt", () => { + expect(new Uint32(0).toBigInt()).toEqual(BigInt(0)); + expect(new Uint32(1).toBigInt()).toEqual(BigInt(1)); + expect(new Uint32(42).toBigInt()).toEqual(BigInt(42)); + expect(new Uint32(1000000000).toBigInt()).toEqual(BigInt(1000000000)); + expect(new Uint32(2147483647).toBigInt()).toEqual(BigInt(2147483647)); + expect(new Uint32(2147483648).toBigInt()).toEqual(BigInt(2147483648)); + expect(new Uint32(4294967295).toBigInt()).toEqual(BigInt(4294967295)); + }); + it("can convert to string", () => { expect(new Uint32(0).toString()).toEqual("0"); expect(new Uint32(1).toString()).toEqual("1"); @@ -209,6 +219,20 @@ describe("Integers", () => { expect(new Int53(-9007199254740991).toNumber()).toEqual(-9007199254740991); }); + it("can convert to BigInt", () => { + expect(new Int53(0).toBigInt()).toEqual(BigInt(0)); + expect(new Int53(1).toBigInt()).toEqual(BigInt(1)); + expect(new Int53(42).toBigInt()).toEqual(BigInt(42)); + expect(new Int53(1000000000).toBigInt()).toEqual(BigInt(1000000000)); + expect(new Int53(2147483647).toBigInt()).toEqual(BigInt(2147483647)); + expect(new Int53(2147483648).toBigInt()).toEqual(BigInt(2147483648)); + expect(new Int53(4294967295).toBigInt()).toEqual(BigInt(4294967295)); + expect(new Int53(9007199254740991).toBigInt()).toEqual(BigInt(9007199254740991)); + + expect(new Int53(-1).toBigInt()).toEqual(BigInt(-1)); + expect(new Int53(-9007199254740991).toBigInt()).toEqual(BigInt(-9007199254740991)); + }); + it("can convert to string", () => { expect(new Int53(0).toString()).toEqual("0"); expect(new Int53(1).toString()).toEqual("1"); @@ -287,6 +311,17 @@ describe("Integers", () => { expect(new Uint53(9007199254740991).toNumber()).toEqual(9007199254740991); }); + it("can convert to BigInt", () => { + expect(new Uint53(0).toBigInt()).toEqual(BigInt(0)); + expect(new Uint53(1).toBigInt()).toEqual(BigInt(1)); + expect(new Uint53(42).toBigInt()).toEqual(BigInt(42)); + expect(new Uint53(1000000000).toBigInt()).toEqual(BigInt(1000000000)); + expect(new Uint53(2147483647).toBigInt()).toEqual(BigInt(2147483647)); + expect(new Uint53(2147483648).toBigInt()).toEqual(BigInt(2147483648)); + expect(new Uint53(4294967295).toBigInt()).toEqual(BigInt(4294967295)); + expect(new Uint53(9007199254740991).toBigInt()).toEqual(BigInt(9007199254740991)); + }); + it("can convert to string", () => { expect(new Uint53(0).toString()).toEqual("0"); expect(new Uint53(1).toString()).toEqual("1"); @@ -505,5 +540,21 @@ describe("Integers", () => { expect(() => a.toNumber()).toThrowError(/number can only safely store up to 53 bits/i); } }); + + it("can export to BigInt", () => { + const a = Uint64.fromBytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); + expect(a.toBigInt()).toEqual(BigInt(0)); + + const b = Uint64.fromBytes([0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01]); + expect(b.toBigInt()).toEqual(BigInt(1)); + + // value too large for 53 bit integer + const c = Uint64.fromBytes([0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff]); + expect(c.toBigInt()).toEqual(BigInt("0xffffffffffffffff")); + + // Number.MAX_SAFE_INTEGER + 1 + const d = Uint64.fromBytes([0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]); + expect(d.toBigInt()).toEqual(BigInt(Number.MAX_SAFE_INTEGER) + BigInt(1)); + }); }); }); diff --git a/packages/math/src/integers.ts b/packages/math/src/integers.ts index 573efefa..ee4a9dfe 100644 --- a/packages/math/src/integers.ts +++ b/packages/math/src/integers.ts @@ -6,6 +6,7 @@ const uint64MaxValue = new BN("18446744073709551615", 10, "be"); /** Internal interface to ensure all integer types can be used equally */ interface Integer { readonly toNumber: () => number; + readonly toBigInt: () => bigint; readonly toString: () => string; } @@ -103,6 +104,10 @@ export class Uint32 implements Integer, WithByteConverters { return this.data; } + public toBigInt(): bigint { + return BigInt(this.toNumber()); + } + public toString(): string { return this.data.toString(); } @@ -139,6 +144,10 @@ export class Int53 implements Integer { return this.data; } + public toBigInt(): bigint { + return BigInt(this.toNumber()); + } + public toString(): string { return this.data.toString(); } @@ -164,6 +173,10 @@ export class Uint53 implements Integer { return this.data.toNumber(); } + public toBigInt(): bigint { + return BigInt(this.toNumber()); + } + public toString(): string { return this.data.toString(); } @@ -245,6 +258,10 @@ export class Uint64 implements Integer, WithByteConverters { return this.data.toString(10); } + public toBigInt(): bigint { + return BigInt(this.toString()); + } + public toNumber(): number { return this.data.toNumber(); }