diff --git a/CHANGELOG.md b/CHANGELOG.md index 30195d5364..7c16c3e5ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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` 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) diff --git a/packages/types/src/codec/AbstractInt.ts b/packages/types/src/codec/AbstractInt.ts index 6d0326fe49..ee1e097df1 100644 --- a/packages/types/src/codec/AbstractInt.ts +++ b/packages/types/src/codec/AbstractInt.ts @@ -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 */ diff --git a/packages/types/src/codec/UInt.spec.ts b/packages/types/src/codec/UInt.spec.ts index 5310c621e1..fe45b8c13a 100644 --- a/packages/types/src/codec/UInt.spec.ts +++ b/packages/types/src/codec/UInt.spec.ts @@ -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()