From 4d6d59f1c534adfd768bcfc5c2e044ba7b33d661 Mon Sep 17 00:00:00 2001 From: Jaco Date: Thu, 13 May 2021 18:23:46 +0300 Subject: [PATCH] Adjust Compact toBigInt() (#3525) --- CHANGELOG.md | 1 + packages/types/src/codec/AbstractInt.ts | 4 +-- packages/types/src/codec/Compact.ts | 2 +- packages/types/src/codec/Date.ts | 33 ++++++++++++------------- packages/types/src/codec/Json.ts | 4 +-- packages/types/src/codec/types.ts | 1 + packages/types/src/create/createType.ts | 6 ++--- 7 files changed, 24 insertions(+), 27 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ddff578748..b93c7c33ac 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -21,6 +21,7 @@ Changes: - Remove support for the old-style V2 contracts from `@polkadot/api-contract` - Adjust historic decoration, return as-at registry via `getHeader`, `getMetadata` & `getRuntimeVersion` - Adjust `VecFixed` to allow for the handling of hex inputs +- Adjust `Compact<*>` to allow for wrapped `toBigInt()` conversions - Fix `isPedantic` decoding to correctly handle `Text` diff --git a/packages/types/src/codec/AbstractInt.ts b/packages/types/src/codec/AbstractInt.ts index c2718fc70a..8cd32fbd3e 100644 --- a/packages/types/src/codec/AbstractInt.ts +++ b/packages/types/src/codec/AbstractInt.ts @@ -62,8 +62,6 @@ function decodeAbstractInt (value: AnyNumber, bitLength: UIntBitLength, isNegati * @ignore * @noInheritDoc */ -// TODO: -// - Apart from encoding/decoding we don't actually keep check on the sizes, is this good enough? export abstract class AbstractInt extends BN implements Codec { public readonly registry: Registry; @@ -140,7 +138,7 @@ export abstract class AbstractInt extends BN implements Codec { * @description True if this value is the max of the type */ public isMax (): boolean { - const u8a = this.toU8a().filter((byte): boolean => byte === 0xff); + const u8a = this.toU8a().filter((b) => b === 0xff); return u8a.length === (this.#bitLength / 8); } diff --git a/packages/types/src/codec/Compact.ts b/packages/types/src/codec/Compact.ts index 9fd62902d7..4ba2477592 100644 --- a/packages/types/src/codec/Compact.ts +++ b/packages/types/src/codec/Compact.ts @@ -96,7 +96,7 @@ export class Compact implements ICompact { * @description Returns a BigInt representation of the number */ public toBigInt (): bigint { - return BigInt(this.toString()); + return this.#raw.toBigInt(); } /** diff --git a/packages/types/src/codec/Date.ts b/packages/types/src/codec/Date.ts index 5631c9e838..ea5cdf0301 100644 --- a/packages/types/src/codec/Date.ts +++ b/packages/types/src/codec/Date.ts @@ -11,6 +11,20 @@ import { bnToBn, bnToHex, bnToU8a, isString, isU8a, u8aToBn } from '@polkadot/ut const BITLENGTH: UIntBitLength = 64; +function decodeDate (value: CodecDate | Date | AnyNumber): Date { + if (value instanceof Date) { + return value; + } else if (isU8a(value)) { + value = u8aToBn(value.subarray(0, BITLENGTH / 8), true); + } else if (isString(value)) { + value = new BN(value.toString(), 10, 'le'); + } + + return new Date( + bnToBn(value as BN).toNumber() * 1000 + ); +} + /** * @name Date * @description @@ -27,26 +41,11 @@ export class CodecDate extends Date implements Codec { public createdAtHash?: Hash; constructor (registry: Registry, value: CodecDate | Date | AnyNumber = 0) { - super(CodecDate.decodeDate(value)); + super(decodeDate(value)); this.registry = registry; } - /** @internal */ - public static decodeDate (value: CodecDate | Date | AnyNumber | unknown): Date { - if (value instanceof Date) { - return value; - } else if (isU8a(value)) { - value = u8aToBn(value.subarray(0, BITLENGTH / 8), true); - } else if (isString(value)) { - value = new BN(value.toString(), 10, 'le'); - } - - return new Date( - bnToBn(value as BN).toNumber() * 1000 - ); - } - /** * @description The length of the value when encoded as a Uint8Array */ @@ -72,7 +71,7 @@ export class CodecDate extends Date implements Codec { * @description Compares the value of the input to see if there is a match */ public eq (other?: unknown): boolean { - return CodecDate.decodeDate(other).getTime() === this.getTime(); + return decodeDate(other as AnyNumber).getTime() === this.getTime(); } /** diff --git a/packages/types/src/codec/Json.ts b/packages/types/src/codec/Json.ts index 20ba30f16c..2a63a400f1 100644 --- a/packages/types/src/codec/Json.ts +++ b/packages/types/src/codec/Json.ts @@ -81,7 +81,7 @@ export class Json extends Map implements Codec { * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information */ public toHuman (): Record { - return [...this.entries()].reduce((json: Record, [key, value]): Record => { + return [...this.entries()].reduce>((json, [key, value]): Record => { json[key] = isFunction((value as Codec).toHuman) ? (value as Codec).toHuman() : value as AnyJson; @@ -94,7 +94,7 @@ export class Json extends Map implements Codec { * @description Converts the Object to JSON, typically used for RPC transfers */ public toJSON (): Record { - return [...this.entries()].reduce((json: Record, [key, value]): Record => { + return [...this.entries()].reduce>((json, [key, value]): Record => { json[key] = value as AnyJson; return json; diff --git a/packages/types/src/codec/types.ts b/packages/types/src/codec/types.ts index e5ecb0a7fd..0d66439096 100644 --- a/packages/types/src/codec/types.ts +++ b/packages/types/src/codec/types.ts @@ -6,6 +6,7 @@ import type { Codec } from '../types'; export interface CompactEncodable extends Codec { bitLength (): number; + toBigInt (): bigint; toBn (): BN; toNumber (): number; } diff --git a/packages/types/src/create/createType.ts b/packages/types/src/create/createType.ts index c1afec5788..45d989434a 100644 --- a/packages/types/src/create/createType.ts +++ b/packages/types/src/create/createType.ts @@ -54,10 +54,8 @@ function initType (registry: // An unsafe version of the `createType` below. It's unsafe because the `type` // argument here can be any string, which, when it cannot parse, will yield a // runtime error. -export function createTypeUnsafe (registry: Registry, type: K, params: any[] = [], options: CreateOptions = {}): T { +export function createTypeUnsafe (registry: Registry, type: K, params: unknown[] = [], options: CreateOptions = {}): T { try { - // Circle back to isPedantic when it handles all cases 100% - as of now, - // it provides false warning which is more hinderance than help return initType(registry, createClass(registry, type), params, options); } catch (error) { throw new Error(`createType(${type}):: ${(error as Error).message}`); @@ -70,6 +68,6 @@ export function createTypeUnsafe (registry: Registry, type: K, ...params: any[]): InterfaceTypes[K] { +export function createType (registry: Registry, type: K, ...params: unknown[]): InterfaceTypes[K] { return createTypeUnsafe(registry, type, params); }