From 19882e68497dcce169d00b98ea0952932128479b Mon Sep 17 00:00:00 2001 From: Simon Warta Date: Sat, 25 Jul 2020 15:00:39 +0200 Subject: [PATCH] Add integer check to Uint64.fromNumber --- CHANGELOG.md | 2 ++ packages/math/src/integers.spec.ts | 11 +++++------ packages/math/src/integers.ts | 4 ++++ 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f8ea468d1b..720d38c5c4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,3 +11,5 @@ - @cosmjs/sdk38: Remove `Pen` type in favour of `OfflineSigner` and remove `Secp256k1Pen` class in favour of `Secp256k1Wallet` which takes an `OfflineSigner` instead of a `SigningCallback`. +- @cosmjs/math: Add missing integer check to `Uint64.fromNumber`. Before + `Uint64.fromNumber(1.1)` produced some result. diff --git a/packages/math/src/integers.spec.ts b/packages/math/src/integers.spec.ts index 2b3a5b5aa3..28ed202cdb 100644 --- a/packages/math/src/integers.spec.ts +++ b/packages/math/src/integers.spec.ts @@ -380,12 +380,11 @@ describe("Integers", () => { expect(() => Uint64.fromNumber(Number.NaN)).toThrowError(/input is not a number/i); // not an integer - expect(() => Uint64.fromNumber(Number.NEGATIVE_INFINITY)).toThrowError( - /input is not a safe integer/i, - ); - expect(() => Uint64.fromNumber(Number.POSITIVE_INFINITY)).toThrowError( - /input is not a safe integer/i, - ); + expect(() => Uint64.fromNumber(1.1)).toThrowError(/input is not an integer/i); + expect(() => Uint64.fromNumber(Number.NEGATIVE_INFINITY)).toThrowError(/input is not an integer/i); + expect(() => Uint64.fromNumber(Number.POSITIVE_INFINITY)).toThrowError(/input is not an integer/i); + + // not a safe integer expect(() => Uint64.fromNumber(Number.MAX_SAFE_INTEGER + 1)).toThrowError( /input is not a safe integer/i, ); diff --git a/packages/math/src/integers.ts b/packages/math/src/integers.ts index b63813d043..155252d374 100644 --- a/packages/math/src/integers.ts +++ b/packages/math/src/integers.ts @@ -173,6 +173,10 @@ export class Uint64 implements Integer, WithByteConverters { throw new Error("Input is not a number"); } + if (!Number.isInteger(input)) { + throw new Error("Input is not an integer"); + } + let bigint: BN; try { bigint = new BN(input);