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
+5 -4
View File
@@ -2,19 +2,20 @@
- Add support for `api.rpc.chain.subscribeAllHeads` (not just best) (Thanks to https://github.com/jak-pan)
- Add support for `api.rpc.engine.*` for manual seal support
- Expand `.toHuman` on ExtrinsicEra, SubmittableResult & Vote
- Expand `.toHuman` on `ExtrinsicEra`, `SubmittableResult` & `Vote`
- Remove `GenericDigestItem`, the `DigestItem` is now a type interface via `@polkadot/types/interfaces`
- Move `H160`, `H256` & `H512` to interfaces in `@polkadot/types/interfaces`
- Align construction of unknown types via `registry.get` e.g. in Events, warn on detection, throw on use
- Expose static with on `UInt`, `Int` & `U8aFixed` classes with optional type name override
- Expose static `.with` on `UInt`, `Int` & `U8aFixed` classes with optional type name override
- `UInt` & `Int` now does source bitLength checks upon construction
- Support for arbitrary UInt types via `UInt<bitLength>` definitions
- Add `api.injectMetadata(metadata)` to initialize with a specific version
- The api now checks for runtime upgrades, decorating with new metadata
- One-lners are normally not mentioned, however the [11 Jan 2019](https://github.com/polkadot-js/api/issues/574) is fixed
- Remove old [11 Jan 2019](https://github.com/polkadot-js/api/issues/574) extrinsic length hack
# 1.3.1 Feb 18, 2020
- Add `@types/bn.js` as a normal dependency, exposed in `*.d.ts` ((Thanks to https://github.com/monitz87)
- Add `@types/bn.js` as a normal dependency, exposed in `*.d.ts` (Thanks to https://github.com/monitz87)
- Type generation scripts (including `polkadot-types-from-{chain,defs}`) has been moved to the `@polkadot/typegen` package
- The `@polkadot/types/codec/create` has been moved to `@polkadot/types/create`, imports from `@polkadot/types` are unaffected
- Expose chain properties on registry (and use for API type formatting)
+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()