From e84c2a5180bb8b935cd9696b9a1eb01678a9bcd9 Mon Sep 17 00:00:00 2001 From: Jaco Date: Tue, 11 Jan 2022 06:33:25 +0100 Subject: [PATCH] Asserts for number formats (#4439) * Asserts for number formats * Test for Compact (used base format asserts) * Lint fixes --- CHANGELOG.md | 1 + packages/types-codec/src/abstract/AbstractInt.ts | 6 +++++- packages/types-codec/src/base/Compact.spec.ts | 5 +++++ packages/types-codec/src/base/UInt.spec.ts | 13 +++++++++++++ 4 files changed, 24 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8838c26252..afd25d5da8 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ Changes: +- Asserts for invalid number format inputs (MAX_SAFE_INTEGER, float) - Ensure `Range/RangeInclusive` variant check does not fail on encoding - Ensure non-option calls in api-contract are marked as `@deprecated` diff --git a/packages/types-codec/src/abstract/AbstractInt.ts b/packages/types-codec/src/abstract/AbstractInt.ts index b5ea82b716..be178d0da5 100644 --- a/packages/types-codec/src/abstract/AbstractInt.ts +++ b/packages/types-codec/src/abstract/AbstractInt.ts @@ -4,7 +4,7 @@ import type { HexString } from '@polkadot/util/types'; import type { AnyNumber, CodecRegistry, INumber, IU8a, UIntBitLength } from '../types'; -import { assert, BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, BN_ZERO, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isHex, isU8a, u8aToBn } from '@polkadot/util'; +import { assert, BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, BN_ZERO, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isHex, isNumber, isString, isU8a, u8aToBn } from '@polkadot/util'; export const DEFAULT_UINT_BITS = 64; @@ -32,6 +32,10 @@ function decodeAbstractInt (value: AnyNumber, bitLength: UIntBitLength, isNegati return value.toString(); } else if (isHex(value, -1, true)) { return hexToBn(value, { isLe: false, isNegative }).toString(); + } else if (isNumber(value)) { + assert(value <= Number.MAX_SAFE_INTEGER && value >= Number.MIN_SAFE_INTEGER && Math.floor(value) === value, 'Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1'); + } else if (isString(value)) { + assert(!(value.includes('.') || value.includes(',') || value.includes('e')), 'String should not contain decimal points or scientific notation'); } return bnToBn(value).toString(); diff --git a/packages/types-codec/src/base/Compact.spec.ts b/packages/types-codec/src/base/Compact.spec.ts index 3010f30361..0b4ee58fe7 100644 --- a/packages/types-codec/src/base/Compact.spec.ts +++ b/packages/types-codec/src/base/Compact.spec.ts @@ -9,6 +9,11 @@ describe('Compact', (): void => { const registry = new TypeRegistry(); describe('constructor', (): void => { + it('fails on > MAX_SAFE_INTEGER', (): void => { + // eslint-disable-next-line @typescript-eslint/no-loss-of-precision + expect(() => new Compact(registry, 'u128', 9007199254740999)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/); + }); + it('has support for BigInt', (): void => { expect( new Compact(registry, 'u128', 123456789000123456789n).toHuman() diff --git a/packages/types-codec/src/base/UInt.spec.ts b/packages/types-codec/src/base/UInt.spec.ts index 108e600cbe..860c1736c3 100644 --- a/packages/types-codec/src/base/UInt.spec.ts +++ b/packages/types-codec/src/base/UInt.spec.ts @@ -8,6 +8,19 @@ import { BN, BN_TWO, isBn } from '@polkadot/util'; describe('UInt', (): void => { const registry = new TypeRegistry(); + it('fails on > MAX_SAFE_INTEGER and float', (): void => { + // eslint-disable-next-line @typescript-eslint/no-loss-of-precision + expect(() => new UInt(registry, 9007199254740999)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/); + // eslint-disable-next-line @typescript-eslint/no-loss-of-precision + expect(() => new UInt(registry, -9007199254740999)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/); + expect(() => new UInt(registry, 9.123)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/); + }); + + it('fails on strings with decimal points & scientific notation', (): void => { + expect(() => new UInt(registry, '123.4')).toThrow(/not contain decimal points/); + expect(() => new UInt(registry, '9e10')).toThrow(/not contain decimal points/); + }); + it('decodes an empty Uint8array correctly', (): void => { expect( new UInt(registry, new Uint8Array()).toNumber()