Merge pull request #313 from CosmWasm/add-missing-integer-test

Add integer check to Uint64.fromNumber
This commit is contained in:
Simon Warta 2020-07-28 11:29:13 +02:00 committed by GitHub
commit 552d4d447a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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
`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.

View File

@ -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,
);

View File

@ -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);