Add integer check to Uint64.fromNumber

This commit is contained in:
Simon Warta 2020-07-25 15:00:39 +02:00
parent 87f17685ac
commit 19882e6849
3 changed files with 11 additions and 6 deletions

View File

@ -11,3 +11,5 @@
- @cosmjs/sdk38: Remove `Pen` type in favour of `OfflineSigner` and remove - @cosmjs/sdk38: Remove `Pen` type in favour of `OfflineSigner` and remove
`Secp256k1Pen` class in favour of `Secp256k1Wallet` which takes an `Secp256k1Pen` class in favour of `Secp256k1Wallet` which takes an
`OfflineSigner` instead of a `SigningCallback`. `OfflineSigner` instead of a `SigningCallback`.
- @cosmjs/math: Add missing integer check to `Uint64.fromNumber`. Before
`Uint64.fromNumber(1.1)` produced some result.

View File

@ -380,12 +380,11 @@ describe("Integers", () => {
expect(() => Uint64.fromNumber(Number.NaN)).toThrowError(/input is not a number/i); expect(() => Uint64.fromNumber(Number.NaN)).toThrowError(/input is not a number/i);
// not an integer // not an integer
expect(() => Uint64.fromNumber(Number.NEGATIVE_INFINITY)).toThrowError( expect(() => Uint64.fromNumber(1.1)).toThrowError(/input is not an integer/i);
/input is not a safe 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);
expect(() => Uint64.fromNumber(Number.POSITIVE_INFINITY)).toThrowError(
/input is not a safe integer/i, // not a safe integer
);
expect(() => Uint64.fromNumber(Number.MAX_SAFE_INTEGER + 1)).toThrowError( expect(() => Uint64.fromNumber(Number.MAX_SAFE_INTEGER + 1)).toThrowError(
/input is not a safe integer/i, /input is not a safe integer/i,
); );

View File

@ -173,6 +173,10 @@ export class Uint64 implements Integer, WithByteConverters {
throw new Error("Input is not a number"); throw new Error("Input is not a number");
} }
if (!Number.isInteger(input)) {
throw new Error("Input is not an integer");
}
let bigint: BN; let bigint: BN;
try { try {
bigint = new BN(input); bigint = new BN(input);