Skip to content

Commit e84c2a5

Browse files
authored
Asserts for number formats (#4439)
* Asserts for number formats * Test for Compact (used base format asserts) * Lint fixes
1 parent aabf4b3 commit e84c2a5

File tree

4 files changed

+24
-1
lines changed

4 files changed

+24
-1
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
Changes:
66

7+
- Asserts for invalid number format inputs (MAX_SAFE_INTEGER, float)
78
- Ensure `Range/RangeInclusive` variant check does not fail on encoding
89
- Ensure non-option calls in api-contract are marked as `@deprecated`
910

packages/types-codec/src/abstract/AbstractInt.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
import type { HexString } from '@polkadot/util/types';
55
import type { AnyNumber, CodecRegistry, INumber, IU8a, UIntBitLength } from '../types';
66

7-
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';
7+
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';
88

99
export const DEFAULT_UINT_BITS = 64;
1010

@@ -32,6 +32,10 @@ function decodeAbstractInt (value: AnyNumber, bitLength: UIntBitLength, isNegati
3232
return value.toString();
3333
} else if (isHex(value, -1, true)) {
3434
return hexToBn(value, { isLe: false, isNegative }).toString();
35+
} else if (isNumber(value)) {
36+
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');
37+
} else if (isString(value)) {
38+
assert(!(value.includes('.') || value.includes(',') || value.includes('e')), 'String should not contain decimal points or scientific notation');
3539
}
3640

3741
return bnToBn(value).toString();

packages/types-codec/src/base/Compact.spec.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ describe('Compact', (): void => {
99
const registry = new TypeRegistry();
1010

1111
describe('constructor', (): void => {
12+
it('fails on > MAX_SAFE_INTEGER', (): void => {
13+
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
14+
expect(() => new Compact(registry, 'u128', 9007199254740999)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/);
15+
});
16+
1217
it('has support for BigInt', (): void => {
1318
expect(
1419
new Compact(registry, 'u128', 123456789000123456789n).toHuman()

packages/types-codec/src/base/UInt.spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,19 @@ import { BN, BN_TWO, isBn } from '@polkadot/util';
88
describe('UInt', (): void => {
99
const registry = new TypeRegistry();
1010

11+
it('fails on > MAX_SAFE_INTEGER and float', (): void => {
12+
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
13+
expect(() => new UInt(registry, 9007199254740999)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/);
14+
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
15+
expect(() => new UInt(registry, -9007199254740999)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/);
16+
expect(() => new UInt(registry, 9.123)).toThrow(/integer <= Number.MAX_SAFE_INTEGER/);
17+
});
18+
19+
it('fails on strings with decimal points & scientific notation', (): void => {
20+
expect(() => new UInt(registry, '123.4')).toThrow(/not contain decimal points/);
21+
expect(() => new UInt(registry, '9e10')).toThrow(/not contain decimal points/);
22+
});
23+
1124
it('decodes an empty Uint8array correctly', (): void => {
1225
expect(
1326
new UInt(registry, new Uint8Array()).toNumber()

0 commit comments

Comments
 (0)