Revert all recent $ renames (#5429)

* Revert "[CI Skip] bump/beta 9.11.4-6-x"

This reverts commit 7ee4f7aaf8.

* Revert "Rename Code .registry to .$registry (#5428)"

This reverts commit dd6826fadd.

* Revert "[CI Skip] bump/beta 9.11.4-5-x"

This reverts commit abc68bf2e2.

* Revert "Convert encodedLength to $encodedLength (#5426)"

This reverts commit ccab89304b.

* Revert "[CI Skip] bump/beta 9.11.4-4-x"

This reverts commit 044465eee4.

* Revert "Rename inspect() to inspectU8a() (#5425)"

This reverts commit 42af205546.

* Revert "[CI Skip] bump/beta 9.11.4-3-x"

This reverts commit 545ad50097.

* Revert "Move .isEmpty to $isEmpty (#5424)"

This reverts commit 6f19084a4d.

* Revert "[CI Skip] bump/beta 9.11.4-2-x"

This reverts commit 4688291ecf.

* Revert "Warn on deprecated getter usage (#5423)"

This reverts commit da1cf9d64e.

* Revert "[CI Skip] bump/beta 9.11.4-1-x"

This reverts commit 96a3ef51f0.

* Revert "Revert "[CI Skip] bump/beta 9.11.4-1-x""

This reverts commit 2eca0730a630d587878f12c141e21ea631dcdd19.

* Revert "Revert "[CI Skip] bump/beta 9.11.4-2-x""

This reverts commit 66284b40692071aa03a1064997c9402fc0b0987c.

* Revert "Revert "[CI Skip] bump/beta 9.11.4-3-x""

This reverts commit 9b820eaefccdb7b51d893000e5480d11e5195e22.

* Revert "Revert "[CI Skip] bump/beta 9.11.4-4-x""

This reverts commit cb5149adf98a5eefd8da61b8793707f85deb7c09.

* Revert "Revert "[CI Skip] bump/beta 9.11.4-5-x""

This reverts commit 489a64439860fefba163695ec36b319dcb1eeddc.

* Revert "Revert "[CI Skip] bump/beta 9.11.4-6-x""

This reverts commit 34a0fd4e2784a1b40ea8e06534d1f39328250e7a.

* Adjust linebreaks

* Adjust isStorageFallback naming

* Fix build
This commit is contained in:
Jaco
2023-01-19 15:06:29 +02:00
committed by GitHub
parent 7ee4f7aaf8
commit e759d32bcf
118 changed files with 488 additions and 1012 deletions
+13 -37
View File
@@ -6,7 +6,7 @@ import type { AnyJson, Codec, Inspect, IU8a, IVec, Registry } from '../types';
import { compactToU8a, u8aConcatStrict, u8aToHex } from '@polkadot/util';
import { compareArray, warnGet } from '../utils';
import { compareArray } from '../utils/compareArray';
/**
* @name AbstractArray
@@ -16,10 +16,11 @@ import { compareArray, warnGet } from '../utils';
* @noInheritDoc
*/
export abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly $registry: Registry;
readonly registry: Registry;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
/**
* @description This ensures that operators such as clice, filter, map, etc. return
@@ -32,44 +33,19 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
protected constructor (registry: Registry, length: number) {
super(length);
this.$registry = registry;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
this.registry = registry;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get $encodedLength (): number {
public get encodedLength (): number {
// We need to loop through all entries since they may have a variable length themselves,
// e.g. when a Vec or Compact is contained withing, it has a variable length based on data
let total = compactToU8a(this.length).length;
for (let i = 0; i < this.length; i++) {
total += this[i].$encodedLength;
total += this[i].encodedLength;
}
return total;
@@ -79,13 +55,13 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
* @description Checks if the value is an empty value
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
return this.length === 0;
}
@@ -107,7 +83,7 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
return {
inner: this.inspectInner(),
outer: [compactToU8a(this.length)]
@@ -118,7 +94,7 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
const inner = new Array<Inspect>(this.length);
for (let i = 0; i < this.length; i++) {
inner[i] = this[i].inspectU8a();
inner[i] = this[i].inspect();
}
return inner;
+13 -39
View File
@@ -4,55 +4,29 @@
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, BareOpts, Codec, Inspect, IU8a, Registry } from '../types';
import { warnGet } from '../utils';
/**
* @name Base
* @description A type extends the Base class, when it holds a value
*/
export abstract class AbstractBase<T extends Codec> implements Codec {
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly $registry: Registry;
readonly registry: Registry;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
readonly #raw: T;
protected constructor (registry: Registry, value: T, initialU8aLength?: number) {
this.$initialU8aLength = initialU8aLength;
this.initialU8aLength = initialU8aLength;
this.#raw = value;
this.$registry = registry;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
this.registry = registry;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get $encodedLength (): number {
public get encodedLength (): number {
return this.toU8a().length;
}
@@ -60,7 +34,7 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
public get inner (): T {
@@ -70,8 +44,8 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
/**
* @description Checks if the value is an empty value
*/
public get $isEmpty (): boolean {
return this.#raw.$isEmpty;
public get isEmpty (): boolean {
return this.#raw.isEmpty;
}
/**
@@ -84,8 +58,8 @@ export abstract class AbstractBase<T extends Codec> implements Codec {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
return this.#raw.inspectU8a();
public inspect (): Inspect {
return this.#raw.inspect();
}
/**
+20 -50
View File
@@ -6,8 +6,6 @@ import type { AnyNumber, Inspect, INumber, IU8a, Registry, UIntBitLength } from
import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isFunction, isHex, isNumber, isObject, isString, isU8a, u8aToBn, u8aToNumber } from '@polkadot/util';
import { warnGet } from '../utils';
export const DEFAULT_UINT_BITS = 64;
// Maximum allowed integer for JS is 2^53 - 1, set limit at 52
@@ -74,12 +72,13 @@ function decodeAbstractInt (value: Exclude<AnyNumber, Uint8Array> | Record<strin
* @noInheritDoc
*/
export abstract class AbstractInt extends BN implements INumber {
public $createdAtHash?: IU8a;
readonly $encodedLength: number;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly $isUnsigned: boolean;
readonly $registry: Registry;
readonly registry: Registry;
readonly encodedLength: number;
readonly isUnsigned: boolean;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
readonly #bitLength: UIntBitLength;
@@ -96,10 +95,11 @@ export abstract class AbstractInt extends BN implements INumber {
: decodeAbstractInt(value, isSigned)
);
this.$registry = registry;
this.registry = registry;
this.#bitLength = bitLength;
this.$encodedLength = this.$initialU8aLength = this.#bitLength / 8;
this.$isUnsigned = !isSigned;
this.encodedLength = this.#bitLength / 8;
this.initialU8aLength = this.#bitLength / 8;
this.isUnsigned = !isSigned;
const isNegative = this.isNeg();
const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
@@ -111,47 +111,17 @@ export abstract class AbstractInt extends BN implements INumber {
}
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $isUnsigned instead. This getter will be removed in a future version */
public get isUnsigned (): boolean {
return warnGet(this, 'isUnsigned');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
}
/**
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
* @description Checks if the value is a zero value (align elsewhere)
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
return this.isZero();
}
@@ -171,7 +141,7 @@ export abstract class AbstractInt extends BN implements INumber {
// number and BN inputs (no `.eqn` needed) - numbers will be converted
return super.eq(
isHex(other)
? hexToBn(other.toString(), { isLe: false, isNegative: !this.$isUnsigned })
? hexToBn(other.toString(), { isLe: false, isNegative: !this.isUnsigned })
: bnToBn(other as string)
);
}
@@ -179,7 +149,7 @@ export abstract class AbstractInt extends BN implements INumber {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
return {
outer: [this.toU8a()]
};
@@ -216,7 +186,7 @@ export abstract class AbstractInt extends BN implements INumber {
return bnToHex(this, {
bitLength: this.bitLength(),
isLe,
isNegative: !this.$isUnsigned
isNegative: !this.isUnsigned
});
}
@@ -232,7 +202,7 @@ export abstract class AbstractInt extends BN implements INumber {
? 'everything'
// FIXME In the case of multiples we need some way of detecting which instance this belongs
// to. as it stands we will always format (incorrectly) against the first token defined
: formatBalance(this, { decimals: this.$registry.chainDecimals[0], withSi: true, withUnit: this.$registry.chainTokens[0] });
: formatBalance(this, { decimals: this.registry.chainDecimals[0], withSi: true, withUnit: this.registry.chainTokens[0] });
}
const [, divisor] = FORMATTERS.find(([type]) => type === rawType) || [];
@@ -271,9 +241,9 @@ export abstract class AbstractInt extends BN implements INumber {
// NOTE In the case of balances, which have a special meaning on the UI
// and can be interpreted differently, return a specific value for it so
// underlying it always matches (no matter which length it actually is)
return this instanceof this.$registry.createClassUnsafe('Balance')
return this instanceof this.registry.createClassUnsafe('Balance')
? 'Balance'
: `${this.$isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
: `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
}
/**
@@ -294,7 +264,7 @@ export abstract class AbstractInt extends BN implements INumber {
return bnToU8a(this, {
bitLength: this.bitLength(),
isLe: true,
isNegative: !this.$isUnsigned
isNegative: !this.isUnsigned
});
}
}
+4 -16
View File
@@ -12,18 +12,6 @@ const CompactU32 = Compact.with(U32);
describe('Compact', (): void => {
const registry = new TypeRegistry();
describe('@deprecated', (): void => {
it('warns on createdAtHash usage', () => {
// eslint-disable-next-line deprecation/deprecation
expect(new Compact(registry, 'u128').createdAtHash).not.toBeDefined();
});
it('warns on initialU8aLength usage', () => {
// eslint-disable-next-line deprecation/deprecation
expect(new Compact(registry, 'u128').initialU8aLength).toEqual(0);
});
});
describe('constructor', (): void => {
it('fails on > MAX_SAFE_INTEGER', (): void => {
// eslint-disable-next-line @typescript-eslint/no-loss-of-precision
@@ -44,19 +32,19 @@ describe('Compact', (): void => {
it('has the correct encodedLength for constructor values (string BlockNumber)', (): void => {
expect(
new (Compact.with('BlockNumber'))(registry, 0xfffffff9).$encodedLength
new (Compact.with('BlockNumber'))(registry, 0xfffffff9).encodedLength
).toEqual(5);
});
it('has the correct encodedLength for constructor values (class BlockNumber)', (): void => {
expect(
new (Compact.with(registry.createClass('BlockNumber')))(registry, 0xfffffff9).$encodedLength
new (Compact.with(registry.createClass('BlockNumber')))(registry, 0xfffffff9).encodedLength
).toEqual(5);
});
it('has the correct encodedLength for constructor values (u32)', (): void => {
expect(
new (Compact.with(U32))(registry, 0xffff9).$encodedLength
new (Compact.with(U32))(registry, 0xffff9).encodedLength
).toEqual(4);
});
@@ -98,7 +86,7 @@ describe('Compact', (): void => {
it('has a sane inspect', (): void => {
expect(
new (Compact.with(U32))(registry, 0xffff).inspectU8a()
new (Compact.with(U32))(registry, 0xffff).inspect()
).toEqual({
outer: [new Uint8Array([254, 255, 3, 0])]
});
+14 -38
View File
@@ -7,7 +7,7 @@ import type { AnyJson, AnyNumber, CodecClass, ICompact, Inspect, INumber, IU8a,
import { compactFromU8a, compactFromU8aLim, compactToU8a, isU8a } from '@polkadot/util';
import { typeToConstructor, warnGet } from '../utils';
import { typeToConstructor } from '../utils';
interface Options<T> {
definition?: CodecClass<T>;
@@ -47,49 +47,25 @@ function decodeCompact<T extends INumber> (registry: Registry, Type: CodecClass<
* a number and making the compact representation thereof
*/
export class Compact<T extends INumber> implements ICompact<T> {
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly $registry: Registry;
readonly registry: Registry;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
readonly #Type: CodecClass<T>;
readonly #raw: T;
constructor (registry: Registry, Type: CodecClass<T> | string, value: Compact<T> | AnyNumber = 0, { definition, setDefinition = noopSetDefinition }: Options<T> = {}) {
this.$registry = registry;
this.registry = registry;
this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
const [raw, decodedLength] = decodeCompact<T>(registry, this.#Type, value);
this.$initialU8aLength = decodedLength;
this.initialU8aLength = decodedLength;
this.#raw = raw;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
}
public static with<O extends INumber> (Type: CodecClass<O> | string): CodecClass<Compact<O>> {
let definition: CodecClass<O> | undefined;
@@ -107,7 +83,7 @@ export class Compact<T extends INumber> implements ICompact<T> {
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get $encodedLength (): number {
public get encodedLength (): number {
return this.toU8a().length;
}
@@ -115,14 +91,14 @@ export class Compact<T extends INumber> implements ICompact<T> {
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
* @description Checks if the value is an empty value
*/
public get $isEmpty (): boolean {
return this.#raw.$isEmpty;
public get isEmpty (): boolean {
return this.#raw.isEmpty;
}
/**
@@ -146,7 +122,7 @@ export class Compact<T extends INumber> implements ICompact<T> {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
return {
outer: [this.toU8a()]
};
@@ -205,7 +181,7 @@ export class Compact<T extends INumber> implements ICompact<T> {
* @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()}>`;
}
/**
@@ -4,53 +4,26 @@
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, Codec, CodecClass, Inspect, IU8a, Registry } from '../types';
import { warnGet } from '../utils';
/**
* @name DoNotConstruct
* @description
* An unknown type that fails on construction with the type info
*/
export class DoNotConstruct implements Codec {
readonly $registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $isStorageFallback?: boolean;
public createdAtHash?: IU8a;
public isStorageFallback?: boolean;
#neverError: Error;
constructor (registry: Registry, typeName = 'DoNotConstruct') {
this.$registry = registry;
this.registry = registry;
this.#neverError = new Error(`DoNotConstruct: Cannot construct unknown type ${typeName}`);
throw this.#neverError;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
}
public static with (typeName?: string): CodecClass {
return class extends DoNotConstruct {
constructor (registry: Registry) {
@@ -62,7 +35,7 @@ export class DoNotConstruct implements Codec {
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get $encodedLength (): number {
public get encodedLength (): number {
throw this.#neverError;
}
@@ -76,7 +49,7 @@ export class DoNotConstruct implements Codec {
/**
* @description Checks if the value is an empty value (always true)
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
throw this.#neverError;
}
@@ -84,7 +57,7 @@ export class DoNotConstruct implements Codec {
throw this.#neverError;
}
public inspectU8a (): Inspect {
public inspect (): Inspect {
throw this.#neverError;
}
+3 -3
View File
@@ -293,7 +293,7 @@ describe('Enum', (): void => {
it('has isNone set, with correct index (i.e. no values are used)', (): void => {
const test = new Enum(registry, ['foo', 'bar'], 1);
expect(test.$isNone).toBe(true);
expect(test.isNone).toBe(true);
expect(test.index).toEqual(1);
});
@@ -303,7 +303,7 @@ describe('Enum', (): void => {
registry,
{ Text, U32 },
'0x0134120000'
).inspectU8a()
).inspect()
).toEqual({
outer: [new Uint8Array([0x01]), new Uint8Array([0x34, 0x12, 0x00, 0x00])]
});
@@ -430,7 +430,7 @@ describe('Enum', (): void => {
const test = new Test(registry, 123, 1);
expect(test.toHex()).toEqual('0x017b000000');
expect(test.$encodedLength).toEqual(1 + 4);
expect(test.encodedLength).toEqual(1 + 4);
});
it('encodes a single entry correctly', (): void => {
+33 -62
View File
@@ -6,7 +6,7 @@ import type { AnyJson, Codec, CodecClass, IEnum, Inspect, IU8a, Registry } from
import { isHex, isNumber, isObject, isString, isU8a, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
import { mapToTypeMap, typesToMap, warnGet } from '../utils';
import { mapToTypeMap, typesToMap } from '../utils';
import { Null } from './Null';
// export interface, this is used in Enum.with, so required as public by TS
@@ -183,16 +183,16 @@ function decodeEnum (registry: Registry, def: TypesDef, value?: unknown, index?:
* an extension to enum where the value type is determined by the actual index.
*/
export class Enum implements IEnum {
readonly $registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
readonly $isBasic: boolean;
public $isStorageFallback?: boolean;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
readonly #def: TypesDef;
readonly #entryIndex: number;
readonly #indexes: number[];
readonly #isBasic: boolean;
readonly #isIndexed: boolean;
readonly #raw: Codec;
@@ -204,55 +204,19 @@ export class Enum implements IEnum {
? createFromU8a(registry, def, value[0], value.subarray(1))
: decodeEnum(registry, def, value, index);
this.$isBasic = isBasic;
this.$registry = registry;
this.registry = registry;
this.#def = def;
this.#isBasic = isBasic;
this.#isIndexed = isIndexed;
this.#indexes = Object.values(def).map(({ index }) => index);
this.#entryIndex = this.#indexes.indexOf(decoded.index);
this.#raw = decoded.value;
if (this.#raw.$initialU8aLength) {
this.$initialU8aLength = 1 + this.#raw.$initialU8aLength;
if (this.#raw.initialU8aLength) {
this.initialU8aLength = 1 + this.#raw.initialU8aLength;
}
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
}
/** @deprecated Use $isBasic instead. This getter will be removed in a future version */
public get isBasic (): boolean {
return warnGet(this, 'isBasic');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $isNone instead. This getter will be removed in a future version */
public get isNone (): boolean {
return warnGet(this, 'isNone');
}
public static with (Types: Record<string, string | CodecClass> | Record<string, number> | string[]): EnumCodecClass<Enum> {
let definition: Definition | undefined;
@@ -297,15 +261,15 @@ export class Enum implements IEnum {
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get $encodedLength (): number {
return 1 + this.#raw.$encodedLength;
public get encodedLength (): number {
return 1 + this.#raw.encodedLength;
}
/**
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
@@ -322,17 +286,24 @@ export class Enum implements IEnum {
return this.#raw;
}
/**
* @description true if this is a basic enum (no values)
*/
public get isBasic (): boolean {
return this.#isBasic;
}
/**
* @description Checks if the value is an empty value
*/
public get $isEmpty (): boolean {
return this.#raw.$isEmpty;
public get isEmpty (): boolean {
return this.#raw.isEmpty;
}
/**
* @description Checks if the Enum points to a [[Null]] type
*/
public get $isNone (): boolean {
public get isNone (): boolean {
return this.#raw instanceof Null;
}
@@ -373,7 +344,7 @@ export class Enum implements IEnum {
return !this.toU8a().some((entry, index) => entry !== other[index]);
} else 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 (isHex(other)) {
return this.toHex() === other;
@@ -390,12 +361,12 @@ export class Enum implements IEnum {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
if (this.$isBasic) {
public inspect (): Inspect {
if (this.#isBasic) {
return { outer: [new Uint8Array([this.index])] };
}
const { inner, outer = [] } = this.#raw.inspectU8a();
const { inner, outer = [] } = this.#raw.inspect();
return {
inner,
@@ -414,7 +385,7 @@ export class Enum implements IEnum {
* @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 || this.$isNone
return this.#isBasic || this.isNone
? this.type
: { [this.type]: this.#raw.toHuman(isExtended) };
}
@@ -423,7 +394,7 @@ export class Enum implements IEnum {
* @description Converts the Object to JSON, typically used for RPC transfers
*/
public toJSON (): AnyJson {
return this.$isBasic
return this.#isBasic
? this.type
: { [stringCamelCase(this.type)]: this.#raw.toJSON() };
}
@@ -439,7 +410,7 @@ export class Enum implements IEnum {
* @description Converts the value in a best-fit primitive form
*/
public toPrimitive (): AnyJson {
return this.$isBasic
return this.#isBasic
? this.type
: { [stringCamelCase(this.type)]: this.#raw.toPrimitive() };
}
@@ -448,7 +419,7 @@ export class Enum implements IEnum {
* @description Returns a raw struct representation of the enum types
*/
protected _toRawStruct (): string[] | Record<string, string | number> {
if (this.$isBasic) {
if (this.#isBasic) {
return this.#isIndexed
? this.defKeys.reduce((out: Record<string, number>, key, index): Record<string, number> => {
out[key] = this.#indexes[index];
@@ -460,7 +431,7 @@ export class Enum implements IEnum {
const entries = Object.entries(this.#def);
return typesToMap(this.$registry, entries.reduce<[CodecClass[], string[]]>((out, [key, { Type }], i) => {
return typesToMap(this.registry, entries.reduce<[CodecClass[], string[]]>((out, [key, { Type }], i) => {
out[0][i] = Type;
out[1][i] = key;
@@ -479,7 +450,7 @@ export class Enum implements IEnum {
* @description Returns the string representation of the value
*/
public toString (): string {
return this.$isNone
return this.isNone
? this.type
: stringify(this.toJSON());
}
+1 -1
View File
@@ -65,7 +65,7 @@ describe('Int', (): void => {
it('has a sane inspect', (): void => {
expect(
new Int(registry, '0x12', 16).inspectU8a()
new Int(registry, '0x12', 16).inspect()
).toEqual({
outer: [new Uint8Array([0x12, 0x00])]
});
+1 -1
View File
@@ -26,7 +26,7 @@ describe('Null', (): void => {
});
it('isEmpty', (): void => {
expect(new Null(registry).$isEmpty).toBe(true);
expect(new Null(registry).isEmpty).toBe(true);
});
it('has an empty hex', (): void => {
+9 -35
View File
@@ -6,48 +6,22 @@ import type { Codec, Inspect, IU8a, Registry } from '../types';
import { isNull } from '@polkadot/util';
import { warnGet } from '../utils';
/**
* @name Null
* @description
* Implements a type that does not contain anything (apart from `null`)
*/
export class Null implements Codec {
public $createdAtHash?: IU8a;
readonly $encodedLength = 0;
public $initialU8aLength = 0;
public $isStorageFallback?: boolean;
readonly $isEmpty = true;
readonly $registry: Registry;
readonly encodedLength = 0;
readonly isEmpty = true;
readonly registry: Registry;
public createdAtHash?: IU8a;
public initialU8aLength = 0;
public isStorageFallback?: boolean;
constructor (registry: Registry) {
this.$registry = registry;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
this.registry = registry;
}
/**
@@ -67,7 +41,7 @@ export class Null implements Codec {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
return {};
}
+2 -2
View File
@@ -37,7 +37,7 @@ describe('Option', (): void => {
].forEach((test): void => {
expect(test.isSome).toBe(true);
expect(test.isNone).toBe(false);
expect(test.$isEmpty).toBe(false);
expect(test.isEmpty).toBe(false);
expect(test.toU8a()).toEqual(new Uint8Array([1]));
expect(test.unwrap().toHex()).toEqual('0x');
});
@@ -198,7 +198,7 @@ describe('Option', (): void => {
it('has a sane inspect', (): void => {
expect(
new Option(registry, U32, '1234').inspectU8a()
new Option(registry, U32, '1234').inspect()
).toEqual({
outer: [new Uint8Array([0x01]), new Uint8Array([210, 4, 0, 0])]
});
+17 -42
View File
@@ -6,7 +6,7 @@ import type { AnyJson, Codec, CodecClass, Inspect, IOption, IU8a, Registry } fro
import { isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
import { typeToConstructor, warnGet } from '../utils';
import { typeToConstructor } from '../utils';
import { Null } from './Null';
interface Options<T> {
@@ -66,11 +66,11 @@ function decodeOption (registry: Registry, Type: CodecClass, value?: unknown): C
* with a value if/as required/found.
*/
export class Option<T extends Codec> implements IOption<T> {
readonly $registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
readonly #Type: CodecClass<T>;
readonly #raw: T;
@@ -83,40 +83,15 @@ export class Option<T extends Codec> implements IOption<T> {
: new Type(registry, value.subarray(1))
: decodeOption(registry, Type, value);
this.$registry = registry;
this.registry = registry;
this.#Type = Type;
this.#raw = decoded as T;
if (decoded?.$initialU8aLength) {
this.$initialU8aLength = 1 + decoded.$initialU8aLength;
if (decoded?.initialU8aLength) {
this.initialU8aLength = 1 + decoded.initialU8aLength;
}
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
}
public static with<O extends Codec> (Type: CodecClass<O> | string): CodecClass<Option<O>> {
let definition: CodecClass<O> | undefined;
@@ -136,22 +111,22 @@ export class Option<T extends Codec> implements IOption<T> {
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get $encodedLength (): number {
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 (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
* @description Checks if the Option has no value
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
return this.isNone;
}
@@ -190,12 +165,12 @@ export class Option<T extends Codec> implements IOption<T> {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
if (this.isNone) {
return { outer: [new Uint8Array([0])] };
}
const { inner, outer = [] } = this.#raw.inspectU8a();
const { inner, outer = [] } = this.#raw.inspect();
return {
inner,
@@ -243,7 +218,7 @@ export class Option<T extends Codec> implements IOption<T> {
* @description Returns the base runtime type name for this instance
*/
public toRawType (isBare?: boolean): string {
const wrapped = this.$registry.getClassName(this.#Type) || new this.#Type(this.$registry).toRawType();
const wrapped = this.registry.getClassName(this.#Type) || new this.#Type(this.registry).toRawType();
return isBare
? wrapped
@@ -266,7 +241,7 @@ export class Option<T extends Codec> implements IOption<T> {
return this.#raw.toU8a(true);
}
const u8a = new Uint8Array(this.$encodedLength);
const u8a = new Uint8Array(this.encodedLength);
if (this.isSome) {
u8a.set([1]);
@@ -304,6 +279,6 @@ export class Option<T extends Codec> implements IOption<T> {
public unwrapOrDefault (): T {
return this.isSome
? this.unwrap()
: new this.#Type(this.$registry);
: new this.#Type(this.registry);
}
}
+1 -1
View File
@@ -51,7 +51,7 @@ export class Result<O extends Codec, E extends Codec> extends Enum implements IR
* @description Checks if the Result has no value
*/
public override get isEmpty (): boolean {
return this.isOk && this.value.$isEmpty;
return this.isOk && this.value.isEmpty;
}
/**
+2 -2
View File
@@ -50,7 +50,7 @@ describe('Tuple', (): void => {
const INPUT = '0xcc0200000000';
const test = registry.createType('(u32, [u32; 0], u16)', INPUT);
expect(test.$encodedLength).toEqual(4 + 0 + 2);
expect(test.encodedLength).toEqual(4 + 0 + 2);
expect(test.toHex()).toEqual(INPUT);
});
});
@@ -123,7 +123,7 @@ describe('Tuple', (): void => {
it('has a sane inspect', (): void => {
expect(
tuple.inspectU8a()
tuple.inspect()
).toEqual({
inner: [
{ outer: [new Uint8Array([7 << 2]), stringToU8a('bazzing')] },
+6 -6
View File
@@ -68,7 +68,7 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
super(registry, Classes[0].length);
this.$initialU8aLength = (
this.initialU8aLength = (
isU8a(value)
? decodeU8a(registry, this, value, Classes)
: decodeTuple(registry, this, value, Classes)
@@ -93,11 +93,11 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
/**
* @description The length of the value when encoded as a Uint8Array
*/
public override get $encodedLength (): number {
public override get encodedLength (): number {
let total = 0;
for (let i = 0; i < this.length; i++) {
total += this[i].$encodedLength;
total += this[i].encodedLength;
}
return total;
@@ -109,13 +109,13 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
public get Types (): string[] {
return this.#Types[1].length
? this.#Types[1]
: this.#Types[0].map((T) => new T(this.$registry).toRawType());
: this.#Types[0].map((T) => new T(this.registry).toRawType());
}
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public override inspectU8a (): Inspect {
public override inspect (): Inspect {
return {
inner: this.inspectInner()
};
@@ -126,7 +126,7 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
*/
public toRawType (): string {
const types = this.#Types[0].map((T) =>
this.$registry.getClassName(T) || new T(this.$registry).toRawType()
this.registry.getClassName(T) || new T(this.registry).toRawType()
);
return `(${types.join(',')})`;
+1 -1
View File
@@ -127,7 +127,7 @@ describe('UInt', (): void => {
it('has a sane inspect', (): void => {
expect(
new UInt(registry, '0x12', 16).inspectU8a()
new UInt(registry, '0x12', 16).inspect()
).toEqual({
outer: [new Uint8Array([0x12, 0x00])]
});
+1 -1
View File
@@ -185,7 +185,7 @@ describe('Vec', (): void => {
];
const vec = registry.createType('Vec<AccountId>', addrs);
expect(vec.inspectU8a()).toEqual({
expect(vec.inspect()).toEqual({
inner: addrs.map((a) => ({
outer: [decodeAddress(a)]
})),
+3 -3
View File
@@ -80,7 +80,7 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
this.#Type = definition || setDefinition(typeToConstructor<T>(registry, Type));
this.$initialU8aLength = (
this.initialU8aLength = (
isU8a(decodeFrom)
? decodeU8aVec(registry, this, decodeFrom, startAt, this.#Type)
: decodeVec(registry, this, decodeFrom, startAt, this.#Type)
@@ -115,7 +115,7 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
// convert type first, this removes overhead from the eq
const other = _other instanceof this.#Type
? _other
: new this.#Type(this.$registry, _other);
: new this.#Type(this.registry, _other);
for (let i = 0; i < this.length; i++) {
if (other.eq(this[i])) {
@@ -130,6 +130,6 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return `Vec<${this.$registry.getClassName(this.#Type) || new this.#Type(this.$registry).toRawType()}>`;
return `Vec<${this.registry.getClassName(this.#Type) || new this.#Type(this.registry).toRawType()}>`;
}
}
@@ -50,19 +50,19 @@ describe('VecFixed', (): void => {
it('has empty Uint8Array when length is 0', (): void => {
const test = new (VecFixed.with(Text, 0))(registry);
expect(test.$encodedLength).toEqual(0);
expect(test.encodedLength).toEqual(0);
expect(test.toU8a()).toEqual(new Uint8Array([]));
});
it('has equivalent to 1 Uint8Array when length is 1', (): void => {
const test = new (VecFixed.with(Text, 1))(registry, ['hello']);
expect(test.$encodedLength).toEqual(1 + 5);
expect(test.encodedLength).toEqual(1 + 5);
expect(test.toU8a()).toEqual(new Uint8Array([20, 104, 101, 108, 108, 111]));
});
it('has a sane inspect', (): void => {
expect(test.inspectU8a()).toEqual({
expect(test.inspect()).toEqual({
inner: [
{ outer: [new Uint8Array([1 << 2]), stringToU8a('1')] },
{ outer: [new Uint8Array([1 << 2]), stringToU8a('2')] },
+5 -5
View File
@@ -32,7 +32,7 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
this.#Type = definition || setDefinition(typeToConstructor<T>(registry, Type));
this.$initialU8aLength = (
this.initialU8aLength = (
isU8a(value)
? decodeU8aVec(registry, this, value, 0, this.#Type)
: decodeVec(registry, this, value, 0, this.#Type)
@@ -57,17 +57,17 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
* @description The type for the items
*/
public get Type (): string {
return new this.#Type(this.$registry).toRawType();
return new this.#Type(this.registry).toRawType();
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public override get $encodedLength (): number {
public override get encodedLength (): number {
let total = 0;
for (let i = 0; i < this.length; i++) {
total += this[i].$encodedLength;
total += this[i].encodedLength;
}
return total;
@@ -76,7 +76,7 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public override inspectU8a (): Inspect {
public override inspect (): Inspect {
return {
inner: this.inspectInner()
};
@@ -87,7 +87,7 @@ describe('BTreeMap', (): void => {
it('correctly encodes length', (): void => {
expect(
new (
BTreeMap.with(Text, U32))(registry, mockU32TextMap).$encodedLength
BTreeMap.with(Text, U32))(registry, mockU32TextMap).encodedLength
).toEqual(13);
});
@@ -195,7 +195,7 @@ describe('BTreeMap', (): void => {
[new Text(registry, '1'), new Text(registry, 'foo')],
[new Text(registry, '2'), new Text(registry, 'bar')],
[new Text(registry, '3'), new Text(registry, 'baz')]
])).inspectU8a()
])).inspect()
).toEqual({
inner: [
{ outer: [new Uint8Array([1 << 2]), stringToU8a('1')] },
@@ -130,7 +130,7 @@ describe('BTreeSet', (): void => {
it('correctly encodes length', (): void => {
expect(
new (
BTreeSet.with(U32))(registry, mockU32Set).$encodedLength
BTreeSet.with(U32))(registry, mockU32Set).encodedLength
).toEqual(17);
});
@@ -139,10 +139,10 @@ describe('BTreeSet', (): void => {
// only the length byte
expect(none.toHex()).toEqual('0x00');
expect(none.$encodedLength).toEqual(1);
expect(none.encodedLength).toEqual(1);
expect(
(new (BTreeSet.with(U32))(registry, none.toHex())).$initialU8aLength
).toEqual(none.$encodedLength);
(new (BTreeSet.with(U32))(registry, none.toHex())).initialU8aLength
).toEqual(none.encodedLength);
});
it('correctly encodes/decodes filled', (): void => {
@@ -150,10 +150,10 @@ describe('BTreeSet', (): void => {
// length byte + 2 values, 2 << 2 with u32 values
expect(some.toHex()).toEqual('0x080100000002000000');
expect(some.$encodedLength).toEqual(1 + (4 * 2));
expect(some.encodedLength).toEqual(1 + (4 * 2));
expect(
(new (BTreeSet.with(U32))(registry, some.toHex())).$initialU8aLength
).toEqual(some.$encodedLength);
(new (BTreeSet.with(U32))(registry, some.toHex())).initialU8aLength
).toEqual(some.encodedLength);
});
});
@@ -209,7 +209,7 @@ describe('BTreeSet', (): void => {
it('has a sane inspect', (): void => {
expect(
new (BTreeSet.with(U32))(registry, [1, 2, 3, 4]).inspectU8a()
new (BTreeSet.with(U32))(registry, [1, 2, 3, 4]).inspect()
).toEqual({
inner: [
{ outer: [new Uint8Array([1, 0, 0, 0])] },
+14 -39
View File
@@ -6,7 +6,7 @@ import type { AnyJson, Codec, CodecClass, Inspect, ISet, IU8a, Registry } from '
import { compactFromU8aLim, compactToU8a, isHex, isU8a, logger, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
import { compareSet, decodeU8aVec, sortSet, typeToConstructor, warnGet } from '../utils';
import { compareSet, decodeU8aVec, sortSet, typeToConstructor } from '../utils';
const l = logger('BTreeSet');
@@ -70,11 +70,11 @@ function decodeSet<V extends Codec> (registry: Registry, valType: CodecClass<V>
}
export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V> {
readonly $registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
readonly #ValClass: CodecClass<V>;
@@ -83,36 +83,11 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
super(sortSet(values));
this.$registry = registry;
this.$initialU8aLength = decodedLength;
this.registry = registry;
this.initialU8aLength = decodedLength;
this.#ValClass = ValClass;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
}
public static with<V extends Codec> (valType: CodecClass<V> | string): CodecClass<BTreeSet<V>> {
return class extends BTreeSet<V> {
constructor (registry: Registry, value?: Uint8Array | string | Set<any>) {
@@ -124,11 +99,11 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get $encodedLength (): number {
public get encodedLength (): number {
let len = compactToU8a(this.size).length;
for (const v of this.values()) {
len += v.$encodedLength;
len += v.encodedLength;
}
return len;
@@ -138,13 +113,13 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
* @description Returns a hash of the value
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
* @description Checks if the value is an empty value
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
return this.size === 0;
}
@@ -165,11 +140,11 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
const inner = new Array<Inspect>();
for (const v of this.values()) {
inner.push(v.inspectU8a());
inner.push(v.inspect());
}
return {
@@ -215,7 +190,7 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return `BTreeSet<${this.$registry.getClassName(this.#ValClass) || new this.#ValClass(this.$registry).toRawType()}>`;
return `BTreeSet<${this.registry.getClassName(this.#ValClass) || new this.#ValClass(this.registry).toRawType()}>`;
}
/**
@@ -19,7 +19,7 @@ describe('BitVec', (): void => {
it('has the right encodedLength', (): void => {
expect(
bitvec.$encodedLength
bitvec.encodedLength
).toEqual((test.length - 2) / 2);
});
@@ -48,7 +48,7 @@ describe('BitVec', (): void => {
it('has a sane inspect', (): void => {
expect(
new BitVec(registry, '0x0837').inspectU8a()
new BitVec(registry, '0x0837').inspect()
).toEqual({
outer: [new Uint8Array([0]), new Uint8Array([0x08, 0x37])]
});
+2 -2
View File
@@ -60,14 +60,14 @@ export class BitVec extends Raw {
/**
* @description The length of the value when encoded as a Uint8Array
*/
public override get $encodedLength (): number {
public override get encodedLength (): number {
return this.length + compactToU8a(this.#decodedLength).length;
}
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public override inspectU8a (): Inspect {
public override inspect (): Inspect {
return {
outer: [compactToU8a(this.#decodedLength), super.toU8a()]
};
@@ -65,7 +65,7 @@ describe('Bytes', (): void => {
it('has a sane inspect', (): void => {
expect(
new Bytes(registry, '0x12345678').inspectU8a()
new Bytes(registry, '0x12345678').inspect()
).toEqual({
outer: [new Uint8Array([4 << 2]), new Uint8Array([0x12, 0x34, 0x56, 0x78])]
});
+2 -2
View File
@@ -50,14 +50,14 @@ export class Bytes extends Raw {
/**
* @description The length of the value when encoded as a Uint8Array
*/
public override get $encodedLength (): number {
public override get encodedLength (): number {
return this.length + compactToU8a(this.length).length;
}
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public override inspectU8a (isBare?: boolean): Inspect {
public override inspect (isBare?: boolean): Inspect {
const clength = compactToU8a(this.length);
return {
@@ -20,7 +20,7 @@ describe('HashMap', (): void => {
new (HashMap.with(Text, Text))(registry, new Map([
[new Text(registry, '1'), new Text(registry, 'foo')],
[new Text(registry, '2'), new Text(registry, 'bar')]
])).inspectU8a()
])).inspect()
).toEqual({
inner: [
{ outer: [new Uint8Array([1 << 2]), stringToU8a('1')] },
+1 -1
View File
@@ -55,7 +55,7 @@ export class Linkage<T extends Codec> extends Struct {
public override toU8a (): Uint8Array {
// As part of a storage query (where these appear), in the case of empty, the values
// are NOT populated by the node - follow the same logic, leaving it empty
return this.$isEmpty
return this.isEmpty
? EMPTY
: super.toU8a();
}
@@ -81,10 +81,10 @@ describe('CodecMap', (): void => {
// only the length byte
expect(none.toHex()).toEqual('0x00');
expect(none.$encodedLength).toEqual(1);
expect(none.encodedLength).toEqual(1);
expect(
new CodecMap(registry, Text, Text, none.toHex()).$initialU8aLength
).toEqual(none.$encodedLength);
new CodecMap(registry, Text, Text, none.toHex()).initialU8aLength
).toEqual(none.encodedLength);
});
it('correctly encodes/decodes filled', (): void => {
@@ -95,10 +95,10 @@ describe('CodecMap', (): void => {
// length byte + 2 values, 2 << 2 with Text values
expect(some.toHex()).toEqual('0x0804310c666f6f04320c626172');
expect(some.$encodedLength).toEqual(1 + ((1 + 1) * 2) + ((1 + 3) * 2));
expect(some.encodedLength).toEqual(1 + ((1 + 1) * 2) + ((1 + 3) * 2));
expect(
new CodecMap(registry, Text, Text, some.toHex()).$initialU8aLength
).toEqual(some.$encodedLength);
new CodecMap(registry, Text, Text, some.toHex()).initialU8aLength
).toEqual(some.encodedLength);
});
});
@@ -107,7 +107,7 @@ describe('CodecMap', (): void => {
new CodecMap(registry, Text, Text, new Map([
[new Text(registry, '1'), new Text(registry, 'foo')],
[new Text(registry, '2'), new Text(registry, 'bar')]
])).inspectU8a()
])).inspect()
).toEqual({
inner: [
{ outer: [new Uint8Array([1 << 2]), stringToU8a('1')] },
+15 -40
View File
@@ -10,7 +10,7 @@ import { AbstractArray } from '../abstract/Array';
import { Enum } from '../base/Enum';
import { Raw } from '../native/Raw';
import { Struct } from '../native/Struct';
import { compareMap, decodeU8a, sortMap, typeToConstructor, warnGet } from '../utils';
import { compareMap, decodeU8a, sortMap, typeToConstructor } from '../utils';
const l = logger('Map');
@@ -94,11 +94,11 @@ function decodeMap<K extends Codec, V extends Codec> (registry: Registry, keyTyp
}
export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends Map<K, V> implements IMap<K, V> {
readonly $registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
readonly #KeyClass: CodecClass<K>;
readonly #ValClass: CodecClass<V>;
@@ -109,46 +109,21 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
super(type === 'BTreeMap' ? sortMap(decoded) : decoded);
this.$registry = registry;
this.$initialU8aLength = decodedLength;
this.registry = registry;
this.initialU8aLength = decodedLength;
this.#KeyClass = KeyClass;
this.#ValClass = ValClass;
this.#type = type;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get $encodedLength (): number {
public get encodedLength (): number {
let len = compactToU8a(this.size).length;
for (const [k, v] of this.entries()) {
len += k.$encodedLength + v.$encodedLength;
len += k.encodedLength + v.encodedLength;
}
return len;
@@ -158,13 +133,13 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
* @description Returns a hash of the value
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
* @description Checks if the value is an empty value
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
return this.size === 0;
}
@@ -178,12 +153,12 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
const inner = new Array<Inspect>();
for (const [k, v] of this.entries()) {
inner.push(k.inspectU8a());
inner.push(v.inspectU8a());
inner.push(k.inspect());
inner.push(v.inspect());
}
return {
@@ -250,7 +225,7 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return `${this.#type}<${this.$registry.getClassName(this.#KeyClass) || new this.#KeyClass(this.$registry).toRawType()},${this.$registry.getClassName(this.#ValClass) || new this.#ValClass(this.$registry).toRawType()}>`;
return `${this.#type}<${this.registry.getClassName(this.#KeyClass) || new this.#KeyClass(this.registry).toRawType()},${this.registry.getClassName(this.#ValClass) || new this.#ValClass(this.registry).toRawType()}>`;
}
/**
@@ -40,7 +40,7 @@ describe('OptionBool', (): void => {
});
it('has a sane inspect', (): void => {
expect(new OptionBool(registry, true).inspectU8a()).toEqual({
expect(new OptionBool(registry, true).inspect()).toEqual({
outer: [new Uint8Array([1])]
});
});
@@ -33,13 +33,13 @@ export class OptionBool extends Option<Bool> {
: value
);
this.$initialU8aLength = 1;
this.initialU8aLength = 1;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public override get $encodedLength (): number {
public override get encodedLength (): number {
return 1;
}
@@ -64,7 +64,7 @@ export class OptionBool extends Option<Bool> {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public override inspectU8a (): Inspect {
public override inspect (): Inspect {
return { outer: [this.toU8a()] };
}
@@ -25,7 +25,7 @@ describe('Range', (): void => {
});
it('has a sane inspect', (): void => {
expect(range.inspectU8a()).toEqual({
expect(range.inspect()).toEqual({
inner: [
{ outer: [new Uint8Array([1, 0, 0, 0])] },
{ outer: [new Uint8Array([2, 0, 0, 0])] }
@@ -106,7 +106,7 @@ describe('Type', (): void => {
const u8a = new Type(registry, original).toU8a();
const decoded = new Type(registry, u8a);
expect(decoded.$encodedLength).toEqual(original.length + 1); // extra byte for length
expect(decoded.encodedLength).toEqual(original.length + 1); // extra byte for length
expect(decoded.toString()).toEqual(expected);
});
@@ -93,7 +93,7 @@ describe('U8aFixed', (): void => {
});
it('has a sane inspect', (): void => {
expect(u8a.inspectU8a()).toEqual({
expect(u8a.inspect()).toEqual({
outer: [new Uint8Array([1, 2, 3, 4])]
});
});
@@ -52,7 +52,7 @@ describe('WrapperKeepOpaque', (): void => {
it('has a sane inspect (non-decodable)', (): void => {
expect(
new ClazzUSize(registry, u8au32).inspectU8a()
new ClazzUSize(registry, u8au32).inspect()
).toEqual({
outer: [new Uint8Array([4 << 2]), new Uint8Array([0x87, 0xd6, 0x12, 0x00])]
});
@@ -60,7 +60,7 @@ describe('WrapperKeepOpaque', (): void => {
it('has a sane inspect (decodable)', (): void => {
expect(
new ClazzU32(registry, u8au32).inspectU8a()
new ClazzU32(registry, u8au32).inspect()
).toEqual({
inner: [{ outer: [new Uint8Array([0x87, 0xd6, 0x12, 0x00])] }],
outer: [new Uint8Array([4 << 2])]
@@ -70,10 +70,10 @@ export class WrapperKeepOpaque<T extends Codec> extends Bytes {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public override inspectU8a (): Inspect {
public override inspect (): Inspect {
return this.#decoded
? {
inner: [this.#decoded.inspectU8a()],
inner: [this.#decoded.inspect()],
outer: [compactToU8a(this.length)]
}
: {
@@ -103,7 +103,7 @@ export class WrapperKeepOpaque<T extends Codec> extends Bytes {
* @description Returns the base runtime type name for this instance
*/
public override toRawType (): string {
return `${this.#opaqueName}<${this.$registry.getClassName(this.#Type) || (this.#decoded ? this.#decoded.toRawType() : new this.#Type(this.$registry).toRawType())}>`;
return `${this.#opaqueName}<${this.registry.getClassName(this.#Type) || (this.#decoded ? this.#decoded.toRawType() : new this.#Type(this.registry).toRawType())}>`;
}
/**
@@ -47,7 +47,7 @@ describe('WrapperOpaque', (): void => {
it('has a sane inspect', (): void => {
expect(
new WrapperOpaque(registry, 'u32', '0x78563412').inspectU8a()
new WrapperOpaque(registry, 'u32', '0x78563412').inspect()
).toEqual({
inner: [{ outer: [new Uint8Array([0x78, 0x56, 0x34, 0x12])] }],
outer: [new Uint8Array([4 << 2])]
+5 -5
View File
@@ -37,7 +37,7 @@ describe('Bool', (): void => {
});
it('correctly encodes length', (): void => {
expect(new Bool(registry, true).$encodedLength).toEqual(1);
expect(new Bool(registry, true).encodedLength).toEqual(1);
});
describe('utils', (): void => {
@@ -58,13 +58,13 @@ describe('Bool', (): void => {
});
it('has sane isEmpty aligning with the rest', (): void => {
expect(new Bool(registry).$isEmpty).toBe(true);
expect(new Bool(registry, false).$isEmpty).toBe(true);
expect(new Bool(registry, true).$isEmpty).toBe(false);
expect(new Bool(registry).isEmpty).toBe(true);
expect(new Bool(registry, false).isEmpty).toBe(true);
expect(new Bool(registry, true).isEmpty).toBe(false);
});
it('has a sane inspect', (): void => {
expect(new Bool(registry, true).inspectU8a()).toEqual({
expect(new Bool(registry, true).inspect()).toEqual({
outer: [new Uint8Array([1])]
});
});
+12 -33
View File
@@ -6,8 +6,6 @@ import type { AnyBool, Codec, Inspect, IU8a, Registry } from '../types';
import { isU8a, u8aToHex } from '@polkadot/util';
import { warnGet } from '../utils';
/**
* @name bool
* @description
@@ -15,12 +13,11 @@ import { warnGet } from '../utils';
* @noInheritDoc
*/
export class bool extends Boolean implements Codec {
readonly $registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
readonly $encodedLength = 1;
public $initialU8aLength = 1;
public $isStorageFallback?: boolean;
public createdAtHash?: IU8a;
public initialU8aLength = 1;
public isStorageFallback?: boolean;
constructor (registry: Registry, value: bool | AnyBool | Uint8Array | number = false) {
super(
@@ -31,45 +28,27 @@ export class bool extends Boolean implements Codec {
: !!value
);
this.$registry = registry;
this.registry = registry;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
return 1;
}
/**
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
* @description Checks if the value is an empty value (true when it wraps false/default)
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
return this.isFalse;
}
@@ -101,7 +80,7 @@ export class bool extends Boolean implements Codec {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
return {
outer: [this.toU8a()]
};
+1 -1
View File
@@ -75,7 +75,7 @@ describe('Date', (): void => {
});
it('has a sane inspect', (): void => {
expect(new CodecDate(registry, 3).inspectU8a()).toEqual({
expect(new CodecDate(registry, 3).inspect()).toEqual({
outer: [new Uint8Array([3, 0, 0, 0, 0, 0, 0, 0])]
});
});
+12 -33
View File
@@ -6,8 +6,6 @@ import type { AnyNumber, Inspect, INumber, IU8a, Registry, UIntBitLength } from
import { BN, bnToBn, bnToHex, bnToU8a, isString, isU8a, u8aToBn } from '@polkadot/util';
import { warnGet } from '../utils';
const BITLENGTH: UIntBitLength = 64;
const U8A_OPTS = { bitLength: BITLENGTH, isLe: true };
@@ -36,55 +34,36 @@ function decodeDate (value: CodecDate | Date | AnyNumber): Date {
* @noInheritDoc
*/
export class CodecDate extends Date implements INumber {
readonly $registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
readonly $encodedLength = BITLENGTH / 8;
public $initialU8aLength = BITLENGTH / 8;
public $isStorageFallback?: boolean;
public createdAtHash?: IU8a;
public initialU8aLength = BITLENGTH / 8;
public isStorageFallback?: boolean;
constructor (registry: Registry, value: CodecDate | Date | AnyNumber = 0) {
super(decodeDate(value));
this.$registry = registry;
this.registry = registry;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
return BITLENGTH / 8;
}
/**
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
* @description Checks if the value is an empty value
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
return this.getTime() === 0;
}
@@ -105,7 +84,7 @@ export class CodecDate extends Date implements INumber {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
return {
outer: [this.toU8a()]
};
@@ -22,7 +22,7 @@ describe('Float', (): void => {
it('triggers isEmpty on 0', (): void => {
expect(
new F32(registry, 0).$isEmpty
new F32(registry, 0).isEmpty
).toEqual(true);
});
+12 -38
View File
@@ -6,8 +6,6 @@ import type { AnyFloat, CodecClass, IFloat, Inspect, IU8a, Registry } from '../t
import { floatToU8a, isHex, isU8a, u8aToFloat, u8aToHex, u8aToU8a } from '@polkadot/util';
import { warnGet } from '../utils';
interface Options {
bitLength?: 32 | 64;
}
@@ -21,11 +19,12 @@ interface Options {
* in some eth_* RPCs
*/
export class Float extends Number implements IFloat {
public $createdAtHash?: IU8a;
readonly $encodedLength: number;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
readonly $registry: Registry;
readonly encodedLength: number;
readonly registry: Registry;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
readonly #bitLength: 32 | 64;
@@ -39,34 +38,9 @@ export class Float extends Number implements IFloat {
);
this.#bitLength = bitLength;
this.$encodedLength = bitLength / 8;
this.$initialU8aLength = this.$encodedLength;
this.$registry = registry;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
this.encodedLength = bitLength / 8;
this.initialU8aLength = this.encodedLength;
this.registry = registry;
}
public static with (bitLength: 32 | 64): CodecClass<Float> {
@@ -81,13 +55,13 @@ export class Float extends Number implements IFloat {
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
* @description Returns true if the type wraps an empty/default all-0 value
*/
public get $isEmpty (): boolean {
get isEmpty (): boolean {
return this.valueOf() === 0;
}
@@ -101,7 +75,7 @@ export class Float extends Number implements IFloat {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
return {
outer: [this.toU8a()]
};
+13 -32
View File
@@ -6,7 +6,7 @@ import type { AnyJson, Codec, Inspect, IU8a, Registry } from '../types';
import { isFunction, objectProperties, stringify } from '@polkadot/util';
import { compareMap, warnGet } from '../utils';
import { compareMap } from '../utils';
/** @internal */
function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
@@ -22,59 +22,40 @@ function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
* @noInheritDoc
*/
export class Json extends Map<string, any> implements Codec {
readonly $registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
readonly $encodedLength = 0;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
constructor (registry: Registry, value?: Record<string, unknown> | null) {
const decoded = decodeJson(value);
super(decoded);
this.$registry = registry;
this.registry = registry;
objectProperties(this, decoded.map(([k]) => k), (k) => this.get(k));
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
/**
* @description Always 0, never encodes as a Uint8Array
*/
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
return 0;
}
/**
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
* @description Checks if the value is an empty value
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
return [...this.keys()].length === 0;
}
@@ -95,7 +76,7 @@ export class Json extends Map<string, any> implements Codec {
/**
* @description Unimplemented, will throw
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
throw new Error('Unimplemented');
}
+2 -2
View File
@@ -45,7 +45,7 @@ describe('Raw', (): void => {
});
it('correctly encodes length', (): void => {
expect(u8a.$encodedLength).toEqual(5);
expect(u8a.encodedLength).toEqual(5);
});
it('allows wrapping of a pre-existing instance', (): void => {
@@ -98,7 +98,7 @@ describe('Raw', (): void => {
it('has a sane inspect', (): void => {
expect(
new Raw(registry, [1, 2, 3, 4, 5]).inspectU8a()
new Raw(registry, [1, 2, 3, 4, 5]).inspect()
).toEqual({
outer: [new Uint8Array([1, 2, 3, 4, 5])]
});
+10 -37
View File
@@ -6,8 +6,6 @@ import type { AnyJson, AnyU8a, Inspect, IU8a, Registry } from '../types';
import { isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util';
import { warnGet } from '../utils';
/**
* @name Raw
* @description
@@ -18,11 +16,11 @@ import { warnGet } from '../utils';
* @noInheritDoc
*/
export class Raw extends Uint8Array implements IU8a {
readonly $registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
/**
* @description This ensures that operators such as clice, filter, map, etc. return
@@ -35,39 +33,14 @@ export class Raw extends Uint8Array implements IU8a {
constructor (registry: Registry, value?: AnyU8a, initialU8aLength?: number) {
super(u8aToU8a(value));
this.$registry = registry;
this.$initialU8aLength = initialU8aLength;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
this.registry = registry;
this.initialU8aLength = initialU8aLength;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get $encodedLength (): number {
public get encodedLength (): number {
return this.length;
}
@@ -75,7 +48,7 @@ export class Raw extends Uint8Array implements IU8a {
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
@@ -88,7 +61,7 @@ export class Raw extends Uint8Array implements IU8a {
/**
* @description Returns true if the type wraps an empty/default all-0 value
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
return !this.length || isUndefined(this.find((b) => !!b));
}
@@ -121,7 +94,7 @@ export class Raw extends Uint8Array implements IU8a {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
return {
outer: [this.toU8a()]
};
+2 -2
View File
@@ -33,7 +33,7 @@ describe('Set', (): void => {
it('constructs via an string[]', (): void => {
const set = new CodecSet(registry, SET_ROLES, ['full', 'authority']);
expect(set.$isEmpty).toEqual(false);
expect(set.isEmpty).toEqual(false);
expect(set.toString()).toEqual(
'[full, authority]'
);
@@ -97,7 +97,7 @@ describe('Set', (): void => {
});
it('has a sane inspect', (): void => {
expect(set.inspectU8a()).toEqual({
expect(set.inspect()).toEqual({
outer: [new Uint8Array([SET_ROLES.full | SET_ROLES.authority])]
});
});
+10 -35
View File
@@ -6,7 +6,7 @@ import type { CodecClass, Inspect, ISet, IU8a, Registry } from '../types';
import { BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, objectProperties, stringify, stringPascalCase, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
import { compareArray, warnGet } from '../utils';
import { compareArray } from '../utils';
type SetValues = Record<string, number | BN>;
@@ -92,11 +92,11 @@ function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8A
* a bitwise representation of the values.
*/
export class CodecSet extends Set<string> implements ISet<string> {
readonly $registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
readonly #allowed: SetValues;
readonly #byteLength: number;
@@ -104,36 +104,11 @@ export class CodecSet extends Set<string> implements ISet<string> {
constructor (registry: Registry, setValues: SetValues, value?: string[] | Set<string> | Uint8Array | BN | number | string, bitLength = 8) {
super(decodeSet(setValues, value, bitLength));
this.$registry = registry;
this.registry = registry;
this.#allowed = setValues;
this.#byteLength = bitLength / 8;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
}
public static with (values: SetValues, bitLength?: number): CodecClass<CodecSet> {
return class extends CodecSet {
static {
@@ -158,7 +133,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get $encodedLength (): number {
public get encodedLength (): number {
return this.#byteLength;
}
@@ -166,13 +141,13 @@ export class CodecSet extends Set<string> implements ISet<string> {
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
* @description true is the Set contains no values
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
return this.size === 0;
}
@@ -225,7 +200,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
return {
outer: [this.toU8a()]
};
@@ -207,7 +207,7 @@ describe('Struct', (): void => {
txt: Text,
u32: U32
})
)(registry, { foo: 'bazzing', bar: 69 }).$encodedLength
)(registry, { foo: 'bazzing', bar: 69 }).encodedLength
).toEqual(5);
});
@@ -380,7 +380,7 @@ describe('Struct', (): void => {
it('has a sane inspect', (): void => {
expect(
new Struct(registry, def, val).inspectU8a()
new Struct(registry, def, val).inspect()
).toEqual({
inner: [
{
+16 -41
View File
@@ -6,7 +6,7 @@ import type { AnyJson, BareOpts, Codec, CodecClass, Inspect, IStruct, IU8a, Regi
import { isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
import { compareMap, decodeU8aStruct, mapToTypeMap, typesToMap, warnGet } from '../utils';
import { compareMap, decodeU8aStruct, mapToTypeMap, typesToMap } from '../utils';
type TypesDef<T = Codec> = Record<string, string | CodecClass<T>>;
@@ -103,11 +103,11 @@ export class Struct<
V extends { [K in keyof S]: any } = { [K in keyof S]: any },
// type names, mapped by key, name of Class in S
E extends { [K in keyof S]: string } = { [K in keyof S]: string }> extends Map<keyof S, Codec> implements IStruct<keyof S> {
readonly $registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
readonly #jsonMap: Map<keyof S, string>;
readonly #Types: Definition;
@@ -122,37 +122,12 @@ export class Struct<
super(decoded);
this.$initialU8aLength = decodedLength;
this.$registry = registry;
this.initialU8aLength = decodedLength;
this.registry = registry;
this.#jsonMap = jsonMap;
this.#Types = typeMap;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
}
public static with<S extends TypesDef> (Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>> {
let definition: Definition | undefined;
@@ -185,9 +160,9 @@ export class Struct<
/**
* @description Checks if the value is an empty value
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
for (const v of this.values()) {
if (!v.$isEmpty) {
if (!v.isEmpty) {
return false;
}
}
@@ -198,11 +173,11 @@ export class Struct<
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get $encodedLength (): number {
public get encodedLength (): number {
let total = 0;
for (const v of this.values()) {
total += v.$encodedLength;
total += v.encodedLength;
}
return total;
@@ -212,7 +187,7 @@ export class Struct<
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
@@ -223,7 +198,7 @@ export class Struct<
const [Types, keys] = this.#Types;
for (let i = 0; i < keys.length; i++) {
result[keys[i]] = new Types[i](this.$registry).toRawType();
result[keys[i]] = new Types[i](this.registry).toRawType();
}
return result as E;
@@ -261,12 +236,12 @@ export class Struct<
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (isBare?: BareOpts): Inspect {
public inspect (isBare?: BareOpts): Inspect {
const inner = new Array<Inspect>();
for (const [k, v] of this.entries()) {
inner.push({
...v.inspectU8a(
...v.inspect(
!isBare || isBoolean(isBare)
? isBare
: isBare[k]
@@ -345,7 +320,7 @@ export class Struct<
* @description Returns the base runtime type name for this instance
*/
public toRawType (): string {
return stringify(typesToMap(this.$registry, this.#Types));
return stringify(typesToMap(this.registry, this.#Types));
}
/**
+3 -3
View File
@@ -59,20 +59,20 @@ describe('Text', (): void => {
it('calulates the length & encoded length correctly for ASCII', (): void => {
const test = new Text(registry, 'abcde');
expect(test.$encodedLength).toEqual(6);
expect(test.encodedLength).toEqual(6);
expect(test).toHaveLength(5);
});
it('calulates the length & encoded length correctly for non-ASCII', (): void => {
const test = new Text(registry, '中文');
expect(test.$encodedLength).toEqual(7);
expect(test.encodedLength).toEqual(7);
expect(test).toHaveLength(2);
});
it('has a sane inspect', (): void => {
expect(
new Text(registry, 'abcde').inspectU8a()
new Text(registry, 'abcde').inspect()
).toEqual({
outer: [new Uint8Array([5 << 2]), stringToU8a('abcde')]
});
+10 -36
View File
@@ -6,7 +6,6 @@ import type { AnyString, AnyU8a, Inspect, IText, IU8a, Registry } from '../types
import { compactAddLength, compactFromU8aLim, compactToU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
import { warnGet } from '../utils';
import { Raw } from './Raw';
const MAX_LENGTH = 128 * 1024;
@@ -50,11 +49,11 @@ function decodeText (value?: null | AnyString | AnyU8a | { toString: () => strin
* @noInheritDoc
*/
export class Text extends String implements IText {
readonly $registry: Registry;
readonly registry: Registry;
public $createdAtHash?: IU8a;
public $initialU8aLength?: number;
public $isStorageFallback?: boolean;
public createdAtHash?: IU8a;
public initialU8aLength?: number;
public isStorageFallback?: boolean;
#override: string | null = null;
@@ -63,39 +62,14 @@ export class Text extends String implements IText {
super(str);
this.$registry = registry;
this.$initialU8aLength = decodedLength;
}
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
public get createdAtHash (): IU8a | undefined {
return warnGet(this, 'createdAtHash');
}
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
public get encodedLength (): number {
return warnGet(this, 'encodedLength');
}
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
public get initialU8aLength (): number | undefined {
return warnGet(this, 'initialU8aLength');
}
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version */
public get isEmpty (): boolean {
return warnGet(this, 'isEmpty');
}
/** @deprecated Use $registry instead. This getter will be removed in a future version */
public get registry (): Registry {
return warnGet(this, 'registry');
this.registry = registry;
this.initialU8aLength = decodedLength;
}
/**
* @description The length of the value when encoded as a Uint8Array
*/
public get $encodedLength (): number {
public get encodedLength (): number {
return this.toU8a().length;
}
@@ -103,13 +77,13 @@ export class Text extends String implements IText {
* @description returns a hash of the contents
*/
public get hash (): IU8a {
return this.$registry.hash(this.toU8a());
return this.registry.hash(this.toU8a());
}
/**
* @description Checks if the value is an empty value
*/
public get $isEmpty (): boolean {
public get isEmpty (): boolean {
return this.length === 0;
}
@@ -133,7 +107,7 @@ export class Text extends String implements IText {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
public inspectU8a (): Inspect {
public inspect (): Inspect {
const value = stringToU8a(super.toString());
return {
+18 -39
View File
@@ -14,23 +14,6 @@ export interface Inspect {
outer?: Uint8Array[];
}
interface CodecDeprecated {
/** @deprecated Use $createdAtHash instead. This getter will be removed in a future version. */
readonly createdAtHash: IU8a | undefined;
/** @deprecated Use $encodedLength instead. This getter will be removed in a future version. */
readonly encodedLength: number;
/** @deprecated Use $initialU8aLength instead. This getter will be removed in a future version. */
readonly initialU8aLength: number | undefined;
/** @deprecated Use $isEmpty instead. This getter will be removed in a future version. */
readonly isEmpty: boolean;
/** @deprecated Use $registry instead. This getter will be removed in a future version. */
readonly registry: Registry;
}
/**
* @name Codec
* @description
@@ -39,32 +22,20 @@ interface CodecDeprecated {
* with getters and functions. The Codec Base is however required for operating
* as an encoding/decoding layer
*/
export interface Codec extends CodecDeprecated {
export interface Codec {
/**
* @description
* The block at which this value was retrieved/created (set to non-empty when
* retrieved from storage)
*/
$createdAtHash?: IU8a;
/**
* @description
* The length of the value when encoded as a Uint8Array (which is the SCALE representation)
*/
readonly $encodedLength: number;
createdAtHash?: IU8a;
/**
* @description
* The length of the initial encoded value (Only available when the value was
* constructed from a Uint8Array input)
*/
$initialU8aLength?: number;
/**
* @description
* Checks if the value is an empty value, with the internal bytes all-zero
*/
$isEmpty: boolean;
initialU8aLength?: number;
/**
* @description
@@ -76,20 +47,28 @@ export interface Codec extends CodecDeprecated {
* any empty storage item should erturn the default. (This is the same as the
* implementation on the Substrate runtime)
*/
$isStorageFallback?: boolean;
isStorageFallback?: boolean;
/**
* @description
* The type registry associated with this object, as available when this type
* was initially created.
*/
readonly $registry: Registry;
* @description The length of the value when encoded as a Uint8Array
*/
readonly encodedLength: number;
/**
* @description Returns a hash of the value
*/
readonly hash: IU8a;
/**
* @description Checks if the value is an empty value
*/
readonly isEmpty: boolean;
/**
* @description The registry associated with this object
*/
readonly registry: Registry;
/**
* @description Compares the value of the input to see if there is a match
*/
@@ -98,7 +77,7 @@ export interface Codec extends CodecDeprecated {
/**
* @description Returns a breakdown of the hex encoding for this Codec
*/
inspectU8a (isBare?: BareOpts): Inspect;
inspect (isBare?: BareOpts): Inspect;
/**
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
+1 -2
View File
@@ -14,11 +14,10 @@ export interface ICompact<T extends INumber = INumber> extends Codec {
}
export interface IEnum extends Codec {
readonly $isBasic: boolean;
readonly defIndexes: number[];
readonly defKeys: string[];
readonly index: number;
readonly isBasic: boolean;
readonly type: string;
readonly value: Codec;
+1 -1
View File
@@ -53,7 +53,7 @@ export interface Registry {
readonly chainSS58: number | undefined;
readonly chainTokens: string[];
$createdAtHash?: IU8a;
createdAtHash?: IU8a;
findMetaCall (callIndex: Uint8Array): CallFunction;
findMetaError (errorIndex: Uint8Array | { error: BN, index: BN } | { error: BN | Uint8Array, index: BN }): RegistryError;
+3 -3
View File
@@ -41,7 +41,7 @@ export function decodeU8a <T extends Codec = Codec> (registry: Registry, result:
while (i < count) {
const value = new Types[i](registry, u8a.subarray(offset));
offset += value.$initialU8aLength || value.$encodedLength;
offset += value.initialU8aLength || value.encodedLength;
result[i] = value;
i++;
}
@@ -67,7 +67,7 @@ export function decodeU8aStruct (registry: Registry, result: [string, Codec][],
while (i < count) {
const value = new Types[i](registry, u8a.subarray(offset));
offset += value.$initialU8aLength || value.$encodedLength;
offset += value.initialU8aLength || value.encodedLength;
result[i] = [keys[i], value];
i++;
}
@@ -93,7 +93,7 @@ export function decodeU8aVec <T extends Codec = Codec> (registry: Registry, resu
while (i < count) {
const value = new Type(registry, u8a.subarray(offset));
offset += value.$initialU8aLength || value.$encodedLength;
offset += value.initialU8aLength || value.encodedLength;
result[i] = value;
i++;
}
-1
View File
@@ -10,4 +10,3 @@ export { typeToConstructor } from './typeToConstructor';
export { sanitize } from './sanitize';
export { sortAsc, sortSet, sortMap } from './sortValues';
export { typesToMap } from './typesToMap';
export { warnGet } from './warnGet';
-21
View File
@@ -1,21 +0,0 @@
// Copyright 2017-2023 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
type Getters = 'createdAtHash' | 'encodedLength' | 'hash' | 'initialU8aLength' | 'isBasic' | 'isEmpty' | 'isNone' | 'isUnsigned' | 'registry';
const hasWarned: Record<string, boolean> = {};
/** @description Warns (once, per class) on the use of a deprecated interface */
export function warnGet <T> (obj: Record<string, any>, name: Getters): T {
const clazz = `${obj.constructor?.name || 'Codec'}`;
const check = `${clazz}.${name}`;
const pass = `$${name}`;
if (!hasWarned[check]) {
hasWarned[check] = true;
console.warn(`Use ${clazz}.${pass} instead. The use of ${check} is deprecated and will be removed in a future version.`);
}
return obj[pass] as T;
}