Check AbstractInt bitLength on construction (#1902)

* Check AbstractInt bitLength on construction

* Language cleanups
This commit is contained in:
Jaco Greeff
2020-02-20 08:00:41 +01:00
committed by GitHub
parent 0e1e0c0d37
commit 2b8ce1b3b7
3 changed files with 14 additions and 5 deletions
+3 -1
View File
@@ -6,7 +6,7 @@ import { H256 } from '../interfaces/runtime';
import { AnyNumber, Codec, Registry } from '../types';
import BN from 'bn.js';
import { bnToBn, formatNumber, hexToBn, isHex, isString, isU8a, u8aToBn } from '@polkadot/util';
import { assert, bnToBn, formatNumber, hexToBn, isHex, isString, isU8a, u8aToBn } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
import Raw from './Raw';
@@ -38,6 +38,8 @@ export default abstract class AbstractInt extends BN implements Codec {
this._bitLength = bitLength;
this._isHexJson = isHexJson;
this._isNegative = isNegative;
assert(super.bitLength() <= bitLength, `${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${bitLength}`);
}
/** @internal */
+6
View File
@@ -10,6 +10,12 @@ import UInt from './UInt';
describe('UInt', (): void => {
const registry = new TypeRegistry();
it('fails on a number that is too large for the bits specified', (): void => {
expect(
(): UInt => new UInt(registry, '12345678901234567890123456789012345678901234567890', 32)
).toThrow('u32: Input too large. Found input with 164 bits, expected 32');
});
it('provides a toBn interface', (): void => {
expect(
new UInt(registry, 987).toBn().toNumber()