Check AbstractInt bitLength on construction (#1902)
* Check AbstractInt bitLength on construction * Language cleanups
This commit is contained in:
@@ -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 */
|
||||
|
||||
@@ -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()
|
||||
|
||||
Reference in New Issue
Block a user