Remove unneeded base extends (#2407)

* Remove unneeded base extends

* CHANGELOG

* Result with value on enum
This commit is contained in:
Jaco Greeff
2020-07-03 11:55:39 +02:00
committed by GitHub
parent 05600c0cd0
commit 63096cc238
7 changed files with 174 additions and 70 deletions
+1 -1
View File
@@ -162,7 +162,7 @@ export default abstract class AbstractInt extends BN implements Codec {
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public toHuman (isExpanded?: boolean): any {
public toHuman (isExpanded?: boolean): string {
// FIXME we need proper expansion here
return this instanceof this.registry.createClass('Balance')
? this.isMax()
+71 -20
View File
@@ -2,15 +2,17 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { AnyNumber, Codec, Constructor, ICompact, InterfaceTypes, Registry } from '../types';
import { H256 } from '@polkadot/types/interfaces';
import { AnyJson, AnyNumber, Codec, Constructor, ICompact, InterfaceTypes, Registry } from '../types';
import BN from 'bn.js';
import { compactAddLength, compactFromU8a, compactStripLength, compactToU8a, isBigInt, isBn, isNumber, isString } from '@polkadot/util';
import { DEFAULT_BITLENGTH } from '@polkadot/util/compact/defaults';
import { blake2AsU8a } from '@polkadot/util-crypto';
import typeToConstructor from './utils/typeToConstructor';
import { UIntBitLength } from './AbstractInt';
import Base from './Base';
import Raw from './Raw';
export interface CompactEncodable extends Codec {
bitLength (): number;
@@ -26,17 +28,17 @@ export interface CompactEncodable extends Codec {
* used by other types to add length-prefixed encoding, or in the case of wrapped types, taking
* a number and making the compact representation thereof
*/
export default class Compact<T extends CompactEncodable> extends Base<T> implements ICompact<T> {
private readonly _Type: Constructor<T>;
export default class Compact<T extends CompactEncodable> implements ICompact<T> {
public readonly registry: Registry;
readonly #Type: Constructor<T>;
readonly #raw: T;
constructor (registry: Registry, Type: Constructor<T> | keyof InterfaceTypes, value: Compact<T> | AnyNumber = 0) {
const _Type = typeToConstructor(registry, Type);
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
super(registry, Compact.decodeCompact<T>(registry, _Type, value));
this._Type = _Type;
this.registry = registry;
this.#Type = typeToConstructor(registry, Type);
this.#raw = Compact.decodeCompact<T>(registry, this.#Type, value) as T;
}
public static with<T extends CompactEncodable> (Type: Constructor<T> | keyof InterfaceTypes): Constructor<Compact<T>> {
@@ -67,7 +69,7 @@ export default class Compact<T extends CompactEncodable> extends Base<T> impleme
/** @internal */
public static decodeCompact<T extends CompactEncodable> (registry: Registry, Type: Constructor<T>, value: Compact<T> | AnyNumber): CompactEncodable {
if (value instanceof Compact) {
return new Type(registry, value._raw);
return new Type(registry, value.#raw);
} else if (isString(value) || isNumber(value) || isBn(value) || isBigInt(value)) {
return new Type(registry, value);
}
@@ -77,20 +79,41 @@ export default class Compact<T extends CompactEncodable> extends Base<T> impleme
return new Type(registry, _value);
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return this.toU8a().length;
}
/**
* @description returns a hash of the contents
*/
public get hash (): H256 {
return new Raw(this.registry, blake2AsU8a(this.toU8a(), 256));
}
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
return this.#raw.isEmpty;
}
/**
* @description Returns the number of bits in the value
*/
public bitLength (): number {
return this._raw.bitLength();
return this.#raw.bitLength();
}
/**
* @description Compares the value of the input to see if there is a match
*/
public eq (other?: unknown): boolean {
return this._raw.eq(
return this.#raw.eq(
other instanceof Compact
? other._raw
? other.#raw
: other
);
}
@@ -99,21 +122,49 @@ export default class Compact<T extends CompactEncodable> extends Base<T> impleme
* @description Returns the BN representation of the number
*/
public toBn (): BN {
return this._raw.toBn();
return this.#raw.toBn();
}
/**
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
*/
public toHex (isLe?: boolean): string {
return this.#raw.toHex(isLe);
}
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public toHuman (isExtended?: boolean): AnyJson {
return this.#raw.toHuman(isExtended);
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): AnyJson {
return this.#raw.toJSON();
}
/**
* @description Returns the number representation for the value
*/
public toNumber (): number {
return this._raw.toNumber();
return this.#raw.toNumber();
}
/**
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return `Compact<${this.registry.getClassName(this._Type) || this._raw.toRawType()}>`;
return `Compact<${this.registry.getClassName(this.#Type) || this.#raw.toRawType()}>`;
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
return this.#raw.toString();
}
/**
@@ -122,13 +173,13 @@ export default class Compact<T extends CompactEncodable> extends Base<T> impleme
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
public toU8a (isBare?: boolean): Uint8Array {
return Compact.encodeU8a(this._raw.toBn());
return Compact.encodeU8a(this.#raw.toBn());
}
/**
* @description Returns the embedded [[UInt]] or [[Moment]] value
*/
public unwrap (): T {
return this._raw;
return this.#raw;
}
}
+51 -31
View File
@@ -2,13 +2,15 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { H256 } from '@polkadot/types/interfaces';
import { AnyJson, Codec, Constructor, InterfaceTypes, Registry } from '../types';
import { assert, hexToU8a, isHex, isNumber, isObject, isString, isU8a, isUndefined, stringCamelCase, stringUpperFirst, u8aConcat, u8aToHex } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
import Null from '../primitive/Null';
import { mapToTypeMap } from './utils';
import Base from './Base';
import Raw from './Raw';
import Struct from './Struct';
// export interface, this is used in Enum.with, so required as public by TS
@@ -114,25 +116,29 @@ function decodeEnum (registry: Registry, def: TypesDef, value?: any, index?: num
// TODO:
// - As per Enum, actually use TS enum
// - It should rather probably extend Enum instead of copying code
export default class Enum extends Base<Codec> {
private _def: TypesDef;
export default class Enum implements Codec {
public readonly registry: Registry;
private _index: number;
readonly #def: TypesDef;
private _indexes: number[];
readonly #index: number;
private _isBasic: boolean;
readonly #indexes: number[];
readonly #isBasic: boolean;
readonly #raw: Codec;
constructor (registry: Registry, def: Record<string, keyof InterfaceTypes | Constructor> | string[], value?: unknown, index?: number) {
const defInfo = extractDef(registry, def);
const decoded = decodeEnum(registry, defInfo.def, value, index);
super(registry, decoded.value);
this._def = defInfo.def;
this._isBasic = defInfo.isBasic;
this._indexes = Object.keys(defInfo.def).map((_, index): number => index);
this._index = this._indexes.indexOf(decoded.index) || 0;
this.registry = registry;
this.#def = defInfo.def;
this.#isBasic = defInfo.isBasic;
this.#indexes = Object.keys(defInfo.def).map((_, index): number => index);
this.#index = this.#indexes.indexOf(decoded.index) || 0;
this.#raw = decoded.value;
}
public static with (Types: Record<string, keyof InterfaceTypes | Constructor> | string[]): EnumConstructor<Enum> {
@@ -140,7 +146,7 @@ export default class Enum extends Base<Codec> {
constructor (registry: Registry, value?: unknown, index?: number) {
super(registry, Types, value, index);
Object.keys(this._def).forEach((_key): void => {
Object.keys(this.#def).forEach((_key): void => {
const name = stringUpperFirst(stringCamelCase(_key.replace(' ', '_')));
const askey = `as${name}`;
const iskey = `is${name}`;
@@ -175,21 +181,35 @@ export default class Enum extends Base<Codec> {
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return 1 + this._raw.encodedLength;
return 1 + this.#raw.encodedLength;
}
/**
* @description returns a hash of the contents
*/
public get hash (): H256 {
return new Raw(this.registry, blake2AsU8a(this.toU8a(), 256));
}
/**
* @description The index of the metadata value
*/
public get index (): number {
return this._index;
return this.#index;
}
/**
* @description true if this is a basic enum (no values)
*/
public get isBasic (): boolean {
return this._isBasic;
return this.#isBasic;
}
/**
* @description Checks if the value is an empty value
*/
public get isEmpty (): boolean {
return this.#raw.isEmpty;
}
/**
@@ -203,35 +223,35 @@ export default class Enum extends Base<Codec> {
* @description Checks if the Enum points to a [[Null]] type (deprecated, use isNone)
*/
public get isNull (): boolean {
return this._raw instanceof Null;
return this.#raw instanceof Null;
}
/**
* @description The available keys for this enum
*/
public get defEntries (): string[] {
return Object.keys(this._def);
return Object.keys(this.#def);
}
/**
* @description The available keys for this enum
*/
public get defKeys (): string[] {
return Object.keys(this._def);
return Object.keys(this.#def);
}
/**
* @description The name of the type this enum value represents
*/
public get type (): string {
return this.defKeys[this._index];
return this.defKeys[this.#index];
}
/**
* @description The value of the enum
*/
public get value (): Codec {
return this._raw;
return this.#raw;
}
/**
@@ -241,7 +261,7 @@ export default class Enum extends Base<Codec> {
// cater for the case where we only pass the enum index
if (isNumber(other)) {
return this.toNumber() === other;
} else if (this._isBasic && isString(other)) {
} else if (this.#isBasic && isString(other)) {
return this.type === other;
} else if (isU8a(other)) {
return !this.toU8a().some((entry, index): boolean => entry !== other[index]);
@@ -268,34 +288,34 @@ export default class Enum extends Base<Codec> {
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public toHuman (isExtended?: boolean): AnyJson {
return this._isBasic
return this.#isBasic
? this.type
: { [this.type]: this._raw.toHuman(isExtended) };
: { [this.type]: this.#raw.toHuman(isExtended) };
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): AnyJson {
return this._isBasic
return this.#isBasic
? this.type
: { [this.type]: this._raw.toJSON() };
: { [this.type]: this.#raw.toJSON() };
}
/**
* @description Returns the number representation for the value
*/
public toNumber (): number {
return this._index;
return this.#index;
}
/**
* @description Returns a raw struct representation of the enum types
*/
protected _toRawStruct (): string[] | Record<string, string> {
return this._isBasic
return this.#isBasic
? this.defKeys
: Struct.typesToMap(this.registry, this._def);
: Struct.typesToMap(this.registry, this.#def);
}
/**
@@ -319,11 +339,11 @@ export default class Enum extends Base<Codec> {
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8a (isBare?: boolean): Uint8Array {
const index = this._indexes[this._index];
const index = this.#indexes[this.#index];
return u8aConcat(
new Uint8Array([index]),
this._raw.toU8a(isBare)
this.#raw.toU8a(isBare)
);
}
}
+45 -13
View File
@@ -2,13 +2,15 @@
// This software may be modified and distributed under the terms
// of the Apache-2.0 license. See the LICENSE file for details.
import { Codec, Constructor, InterfaceTypes, Registry } from '../types';
import { H256 } from '@polkadot/types/interfaces';
import { AnyJson, Codec, Constructor, InterfaceTypes, Registry } from '../types';
import { isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
import { blake2AsU8a } from '@polkadot/util-crypto';
import Null from '../primitive/Null';
import { typeToConstructor } from './utils';
import Base from './Base';
import Raw from './Raw';
/** @internal */
function decodeOptionU8a (registry: Registry, Type: Constructor, value: Uint8Array): Codec {
@@ -48,15 +50,17 @@ function decodeOption (registry: Registry, typeName: Constructor | keyof Interfa
* implements that - decodes, checks for optionality and wraps the required structure
* with a value if/as required/found.
*/
export default class Option<T extends Codec> extends Base<T> {
export default class Option<T extends Codec> implements Codec {
public readonly registry: Registry;
readonly #Type: Constructor<T>;
constructor (registry: Registry, typeName: Constructor<T> | keyof InterfaceTypes, value?: unknown) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
super(registry, decodeOption(registry, typeName, value));
readonly #raw: T;
constructor (registry: Registry, typeName: Constructor<T> | keyof InterfaceTypes, value?: unknown) {
this.registry = registry;
this.#Type = typeToConstructor(registry, typeName);
this.#raw = decodeOption(registry, typeName, value) as T;
}
public static with<O extends Codec> (Type: Constructor<O> | keyof InterfaceTypes): Constructor<Option<O>> {
@@ -72,7 +76,14 @@ export default class Option<T extends Codec> extends Base<T> {
*/
public get encodedLength (): number {
// boolean byte (has value, doesn't have) along with wrapped length
return 1 + this._raw.encodedLength;
return 1 + this.#raw.encodedLength;
}
/**
* @description returns a hash of the contents
*/
public get hash (): H256 {
return new Raw(this.registry, blake2AsU8a(this.toU8a(), 256));
}
/**
@@ -86,7 +97,7 @@ export default class Option<T extends Codec> extends Base<T> {
* @description Checks if the Option has no value
*/
public get isNone (): boolean {
return this._raw instanceof Null;
return this.#raw instanceof Null;
}
/**
@@ -100,7 +111,7 @@ export default class Option<T extends Codec> extends Base<T> {
* @description The actual value for the Option
*/
public get value (): Codec {
return this._raw;
return this.#raw;
}
/**
@@ -125,6 +136,20 @@ export default class Option<T extends Codec> extends Base<T> {
: u8aToHex(this.toU8a().subarray(1));
}
/**
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
*/
public toHuman (isExtended?: boolean): AnyJson {
return this.#raw.toHuman(isExtended);
}
/**
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): AnyJson {
return this.#raw.toJSON();
}
/**
* @description Returns the base runtime type name for this instance
*/
@@ -136,20 +161,27 @@ export default class Option<T extends Codec> extends Base<T> {
: `Option<${wrapped}>`;
}
/**
* @description Returns the string representation of the value
*/
public toString (): string {
return this.#raw.toString();
}
/**
* @description Encodes the value as a Uint8Array as per the SCALE specifications
* @param isBare true when the value has none of the type-specific prefixes (internal)
*/
public toU8a (isBare?: boolean): Uint8Array {
if (isBare) {
return this._raw.toU8a(true);
return this.#raw.toU8a(true);
}
const u8a = new Uint8Array(this.encodedLength);
if (this.isSome) {
u8a.set([1]);
u8a.set(this._raw.toU8a(), 1);
u8a.set(this.#raw.toU8a(), 1);
}
return u8a;
@@ -163,7 +195,7 @@ export default class Option<T extends Codec> extends Base<T> {
throw new Error('Option: unwrapping a None value');
}
return this._raw;
return this.#raw;
}
/**
+1 -1
View File
@@ -50,7 +50,7 @@ export default class Result<O extends Codec, E extends Codec> extends Enum {
* @description Checks if the Result has no value
*/
public get isEmpty (): boolean {
return this.isOk && this._raw.isEmpty;
return this.isOk && this.value.isEmpty;
}
/**
+4 -4
View File
@@ -67,11 +67,11 @@ export default class Data extends Enum {
}
get asRaw (): Bytes {
return this._raw as Bytes;
return this.value as Bytes;
}
get asSha256 (): H256 {
return this._raw as H256;
return this.value as H256;
}
get isRaw (): boolean {
@@ -97,7 +97,7 @@ export default class Data extends Enum {
return new Uint8Array(1);
} else if (this.index === 1) {
// don't add the length, just the data
const data = this._raw.toU8a(true);
const data = this.value.toU8a(true);
const length = Math.min(data.length, 32);
const u8a = new Uint8Array(length + 1);
@@ -111,7 +111,7 @@ export default class Data extends Enum {
const u8a = new Uint8Array(33);
u8a.set([this.index + 32], 0);
u8a.set(this._raw.toU8a(), 1);
u8a.set(this.value.toU8a(), 1);
return u8a;
}