Augment Registry (w/o CodecRegistry & CreateRegistry) (#4463)

* Augment Registry (w/o CodecRegistry & CreateRegistry)

* Build fixes

* Final build fixes

* licence headers
This commit is contained in:
Jaco
2022-01-18 12:52:01 +02:00
committed by GitHub
parent f8c2701751
commit 500e2022c3
112 changed files with 570 additions and 537 deletions
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, Codec, CodecRegistry, IU8a, IVec } from '../types';
import type { AnyJson, Codec, IU8a, IVec, Registry } from '../types';
import { compactToU8a, u8aConcat, u8aToHex } from '@polkadot/util';
@@ -16,13 +16,13 @@ import { compareArray } from '../utils/compareArray';
* @noInheritDoc
*/
export abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
readonly initialU8aLength?: number;
protected constructor (registry: CodecRegistry, values: T[], initialU8aLength?: number) {
protected constructor (registry: Registry, values: T[], initialU8aLength?: number) {
super(values.length);
// explicitly set the values here - this removes the need for any extra allocations
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyNumber, CodecRegistry, INumber, IU8a, UIntBitLength } from '../types';
import type { AnyNumber, INumber, IU8a, Registry, UIntBitLength } from '../types';
import { assert, BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, BN_ZERO, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isHex, isNumber, isString, isU8a, u8aToBn } from '@polkadot/util';
@@ -47,7 +47,7 @@ function decodeAbstractInt (value: AnyNumber, bitLength: UIntBitLength, isNegati
* @noInheritDoc
*/
export abstract class AbstractInt extends BN implements INumber {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
@@ -57,7 +57,7 @@ export abstract class AbstractInt extends BN implements INumber {
readonly #isSigned: boolean;
constructor (registry: CodecRegistry, value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isSigned = false) {
constructor (registry: Registry, value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isSigned = false) {
// Construct via a string, which will be passed in the BN constructor.
// It would be ideal to actually return a BN, but there is an issue:
// https://github.com/indutny/bn.js/issues/206
+4 -4
View File
@@ -2,14 +2,14 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, BareOpts, Codec, CodecRegistry, IU8a } from '../types';
import type { AnyJson, BareOpts, Codec, IU8a, Registry } from '../types';
/**
* @name Base
* @description A type extends the Base class, when it holds a value
*/
export abstract class Base<T extends Codec> implements Codec {
readonly #registry: CodecRegistry;
readonly #registry: Registry;
public createdAtHash?: IU8a;
@@ -17,7 +17,7 @@ export abstract class Base<T extends Codec> implements Codec {
readonly #raw: T;
protected constructor (registry: CodecRegistry, value: T, initialU8aLength?: number) {
protected constructor (registry: Registry, value: T, initialU8aLength?: number) {
this.#registry = registry;
this.#raw = value;
this.initialU8aLength = initialU8aLength;
@@ -48,7 +48,7 @@ export abstract class Base<T extends Codec> implements Codec {
return this.#raw.isEmpty;
}
public get registry (): CodecRegistry {
public get registry (): Registry {
return this.#registry;
}
+5 -5
View File
@@ -3,7 +3,7 @@
import type { BN } from '@polkadot/util';
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, AnyNumber, CodecClass, CodecRegistry, ICompact, INumber, IU8a } from '../types';
import type { AnyJson, AnyNumber, CodecClass, ICompact, INumber, IU8a, Registry } from '../types';
import { compactFromU8a, compactToU8a, isBigInt, isBn, isNumber, isString } from '@polkadot/util';
@@ -18,7 +18,7 @@ import { typeToConstructor } from '../utils';
* a number and making the compact representation thereof
*/
export class Compact<T extends INumber> implements ICompact<T> {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
@@ -28,7 +28,7 @@ export class Compact<T extends INumber> implements ICompact<T> {
readonly #raw: T;
constructor (registry: CodecRegistry, Type: CodecClass<T> | string, value: Compact<T> | AnyNumber = 0) {
constructor (registry: Registry, Type: CodecClass<T> | string, value: Compact<T> | AnyNumber = 0) {
this.registry = registry;
this.#Type = typeToConstructor(registry, Type);
@@ -40,14 +40,14 @@ export class Compact<T extends INumber> implements ICompact<T> {
public static with<T extends INumber> (Type: CodecClass<T> | string): CodecClass<Compact<T>> {
return class extends Compact<T> {
constructor (registry: CodecRegistry, value?: Compact<T> | AnyNumber) {
constructor (registry: Registry, value?: Compact<T> | AnyNumber) {
super(registry, Type, value);
}
};
}
/** @internal */
public static decodeCompact<T extends INumber> (registry: CodecRegistry, Type: CodecClass<T>, value: Compact<T> | AnyNumber): [T, number] {
public static decodeCompact<T extends INumber> (registry: Registry, Type: CodecClass<T>, value: Compact<T> | AnyNumber): [T, number] {
if (value instanceof Compact) {
return [new Type(registry, value.#raw), 0];
} else if (value instanceof Type) {
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, Codec, CodecClass, CodecRegistry, IU8a } from '../types';
import type { AnyJson, Codec, CodecClass, IU8a, Registry } from '../types';
/**
* @name DoNotConstruct
@@ -10,13 +10,13 @@ import type { AnyJson, Codec, CodecClass, CodecRegistry, IU8a } from '../types';
* An unknown type that fails on construction with the type info
*/
export class DoNotConstruct implements Codec {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
#neverError: Error;
constructor (registry: CodecRegistry, typeName = 'DoNotConstruct') {
constructor (registry: Registry, typeName = 'DoNotConstruct') {
this.registry = registry;
this.#neverError = new Error(`DoNotConstruct: Cannot construct unknown type ${typeName}`);
@@ -25,7 +25,7 @@ export class DoNotConstruct implements Codec {
public static with (typeName?: string): CodecClass {
return class extends DoNotConstruct {
constructor (registry: CodecRegistry) {
constructor (registry: Registry) {
super(registry, typeName);
}
};
+2 -2
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { CodecRegistry } from '@polkadot/types-codec/types';
import type { Registry } from '@polkadot/types-codec/types';
import { TypeRegistry } from '@polkadot/types';
import { Enum, Null, Text, U32 } from '@polkadot/types-codec';
@@ -129,7 +129,7 @@ describe('Enum', (): void => {
class B extends Null { }
class C extends Null { }
class Test extends Enum {
constructor (registry: CodecRegistry, value?: string, index?: number) {
constructor (registry: Registry, value?: string, index?: number) {
super(registry, {
a: A,
b: B,
+9 -9
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, Codec, CodecClass, CodecRegistry, IEnum, IU8a } from '../types';
import type { AnyJson, Codec, CodecClass, IEnum, IU8a, Registry } from '../types';
import { assert, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
@@ -11,7 +11,7 @@ import { Null } from './Null';
// export interface, this is used in Enum.with, so required as public by TS
export interface EnumCodecClass<T = Codec> {
new(registry: CodecRegistry, value?: any, index?: number): T;
new(registry: Registry, value?: any, index?: number): T;
}
interface EntryDef {
@@ -38,7 +38,7 @@ function isRustEnum (def: Record<string, string | CodecClass> | Record<string, n
return true;
}
function extractDef (registry: CodecRegistry, _def: Record<string, string | CodecClass> | Record<string, number> | string[]): { def: TypesDef; isBasic: boolean; isIndexed: boolean } {
function extractDef (registry: Registry, _def: Record<string, string | CodecClass> | Record<string, number> | string[]): { def: TypesDef; isBasic: boolean; isIndexed: boolean } {
const def: TypesDef = {};
let isBasic: boolean;
let isIndexed: boolean;
@@ -81,7 +81,7 @@ function extractDef (registry: CodecRegistry, _def: Record<string, string | Code
};
}
function createFromValue (registry: CodecRegistry, def: TypesDef, index = 0, value?: unknown): Decoded {
function createFromValue (registry: Registry, def: TypesDef, index = 0, value?: unknown): Decoded {
const entry = Object.values(def).find((e) => e.index === index);
assert(!isUndefined(entry), () => `Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`);
@@ -94,7 +94,7 @@ function createFromValue (registry: CodecRegistry, def: TypesDef, index = 0, val
};
}
function decodeFromJSON (registry: CodecRegistry, def: TypesDef, key: string, value?: unknown): Decoded {
function decodeFromJSON (registry: Registry, def: TypesDef, key: string, value?: unknown): Decoded {
// JSON comes in the form of { "<type (camelCase)>": "<value for type>" }, here we
// additionally force to lower to ensure forward compat
const keys = Object.keys(def).map((k) => k.toLowerCase());
@@ -110,7 +110,7 @@ function decodeFromJSON (registry: CodecRegistry, def: TypesDef, key: string, va
}
}
function decodeEnum (registry: CodecRegistry, def: TypesDef, value?: unknown, index?: number): Decoded {
function decodeEnum (registry: Registry, def: TypesDef, value?: unknown, index?: number): Decoded {
// NOTE We check the index path first, before looking at values - this allows treating
// the optional indexes before anything else, more-specific > less-specific
if (isNumber(index)) {
@@ -145,7 +145,7 @@ function decodeEnum (registry: CodecRegistry, def: TypesDef, value?: unknown, in
* an extension to enum where the value type is determined by the actual index.
*/
export class Enum implements IEnum {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
@@ -163,7 +163,7 @@ export class Enum implements IEnum {
readonly #raw: Codec;
constructor (registry: CodecRegistry, Types: Record<string, string | CodecClass> | Record<string, number> | string[], value?: unknown, index?: number) {
constructor (registry: Registry, Types: Record<string, string | CodecClass> | Record<string, number> | string[], value?: unknown, index?: number) {
const { def, isBasic, isIndexed } = extractDef(registry, Types);
// shortcut isU8a as used in SCALE decoding
@@ -199,7 +199,7 @@ export class Enum implements IEnum {
}
return class extends Enum {
constructor (registry: CodecRegistry, value?: unknown, index?: number) {
constructor (registry: Registry, value?: unknown, index?: number) {
super(registry, Types, value, index);
objectProperties(this, isKeys, (_, i) => this.type === keys[i]);
+3 -3
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyNumber, CodecClass, CodecRegistry, UIntBitLength } from '../types';
import type { AnyNumber, CodecClass, Registry, UIntBitLength } from '../types';
import { AbstractInt } from '../abstract/AbstractInt';
@@ -16,13 +16,13 @@ import { AbstractInt } from '../abstract/AbstractInt';
* @noInheritDoc
*/
export class Int extends AbstractInt {
constructor (registry: CodecRegistry, value: AnyNumber = 0, bitLength?: UIntBitLength) {
constructor (registry: Registry, value: AnyNumber = 0, bitLength?: UIntBitLength) {
super(registry, value, bitLength, true);
}
public static with (bitLength: UIntBitLength, typeName?: string): CodecClass<Int> {
return class extends Int {
constructor (registry: CodecRegistry, value?: AnyNumber) {
constructor (registry: Registry, value?: AnyNumber) {
super(registry, value, bitLength);
}
+3 -3
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { Codec, CodecRegistry, IU8a } from '../types';
import type { Codec, IU8a, Registry } from '../types';
import { isNull } from '@polkadot/util';
@@ -12,14 +12,14 @@ import { isNull } from '@polkadot/util';
* Implements a type that does not contain anything (apart from `null`)
*/
export class Null implements Codec {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
// Added for compatibility reasons, e.g. see Option
public readonly initialU8aLength = 0;
constructor (registry: CodecRegistry) {
constructor (registry: Registry) {
this.registry = registry;
}
+5 -5
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, Codec, CodecClass, CodecRegistry, IOption, IU8a } from '../types';
import type { AnyJson, Codec, CodecClass, IOption, IU8a, Registry } from '../types';
import { assert, isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
@@ -10,7 +10,7 @@ import { typeToConstructor } from '../utils';
import { Null } from './Null';
/** @internal */
function decodeOption (registry: CodecRegistry, Type: CodecClass, value?: unknown): Codec {
function decodeOption (registry: Registry, Type: CodecClass, value?: unknown): Codec {
// In the case of an option, unwrap the inner
if (value instanceof Option) {
value = value.value;
@@ -41,7 +41,7 @@ function decodeOption (registry: CodecRegistry, Type: CodecClass, value?: unknow
* with a value if/as required/found.
*/
export class Option<T extends Codec> implements IOption<T> {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
@@ -51,7 +51,7 @@ export class Option<T extends Codec> implements IOption<T> {
readonly #raw: T;
constructor (registry: CodecRegistry, typeName: CodecClass<T> | string, value?: unknown) {
constructor (registry: Registry, typeName: CodecClass<T> | string, value?: unknown) {
const Type = typeToConstructor(registry, typeName);
const decoded = isU8a(value) && value.length && !isCodec(value)
? value[0] === 0
@@ -70,7 +70,7 @@ export class Option<T extends Codec> implements IOption<T> {
public static with<O extends Codec> (Type: CodecClass<O> | string): CodecClass<Option<O>> {
return class extends Option<O> {
constructor (registry: CodecRegistry, value?: unknown) {
constructor (registry: Registry, value?: unknown) {
super(registry, Type, value);
}
};
+3 -3
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, CodecClass, CodecRegistry, IResult } from '../types';
import type { Codec, CodecClass, IResult, Registry } from '../types';
import { assert } from '@polkadot/util';
@@ -13,7 +13,7 @@ import { Enum } from './Enum';
* A Result maps to the Rust Result type, that can either wrap a success or error value
*/
export class Result<O extends Codec, E extends Codec> extends Enum implements IResult<O, E> {
constructor (registry: CodecRegistry, Ok: CodecClass<O> | string, Err: CodecClass<E> | string, value?: unknown) {
constructor (registry: Registry, Ok: CodecClass<O> | string, Err: CodecClass<E> | string, value?: unknown) {
// NOTE This is order-dependent, Ok (with index 0) needs to be first
// eslint-disable-next-line sort-keys
super(registry, { Ok, Err }, value);
@@ -21,7 +21,7 @@ export class Result<O extends Codec, E extends Codec> extends Enum implements IR
public static override with<O extends Codec, E extends Codec> (Types: { Ok: CodecClass<O> | string; Err: CodecClass<E> | string }): CodecClass<Result<O, E>> {
return class extends Result<O, E> {
constructor (registry: CodecRegistry, value?: unknown) {
constructor (registry: Registry, value?: unknown) {
super(registry, Types.Ok, Types.Err, value);
}
};
+4 -4
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyTupleValue, Codec, CodecClass, CodecRegistry, ITuple } from '../types';
import type { AnyTupleValue, Codec, CodecClass, ITuple, Registry } from '../types';
import { isFunction, isHex, isString, isU8a, stringify, u8aConcat, u8aToU8a } from '@polkadot/util';
@@ -19,7 +19,7 @@ type TupleTypes = TupleType[] | {
};
/** @internal */
function decodeTuple (registry: CodecRegistry, Classes: TupleCodecClasss, value?: AnyTupleValue): [Codec[], number] {
function decodeTuple (registry: Registry, Classes: TupleCodecClasss, value?: AnyTupleValue): [Codec[], number] {
if (isU8a(value) || isHex(value)) {
return decodeU8a(registry, u8aToU8a(value), Classes);
}
@@ -55,7 +55,7 @@ function decodeTuple (registry: CodecRegistry, Classes: TupleCodecClasss, value?
export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
#Types: TupleCodecClasss;
constructor (registry: CodecRegistry, Types: TupleTypes | TupleType, value?: AnyTupleValue) {
constructor (registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue) {
const Classes = Array.isArray(Types)
? Types.map((t) => typeToConstructor(registry, t))
: isFunction(Types) || isString(Types)
@@ -72,7 +72,7 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
public static with (Types: TupleTypes | TupleType): CodecClass<Tuple> {
return class extends Tuple {
constructor (registry: CodecRegistry, value?: AnyTupleValue) {
constructor (registry: Registry, value?: AnyTupleValue) {
super(registry, Types, value);
}
};
+2 -2
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyNumber, CodecClass, CodecRegistry, UIntBitLength } from '../types';
import type { AnyNumber, CodecClass, Registry, UIntBitLength } from '../types';
import { AbstractInt } from '../abstract/AbstractInt';
@@ -18,7 +18,7 @@ import { AbstractInt } from '../abstract/AbstractInt';
export class UInt extends AbstractInt {
public static with (bitLength: UIntBitLength, typeName?: string): CodecClass<UInt> {
return class extends UInt {
constructor (registry: CodecRegistry, value?: AnyNumber) {
constructor (registry: Registry, value?: AnyNumber) {
super(registry, value, bitLength);
}
+4 -4
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { Codec, CodecClass, CodecRegistry } from '../types';
import type { Codec, CodecClass, Registry } from '../types';
import { assert, compactFromU8a, logger, u8aToU8a } from '@polkadot/util';
@@ -13,7 +13,7 @@ const MAX_LENGTH = 64 * 1024;
const l = logger('Vec');
export function decodeVec<T extends Codec> (registry: CodecRegistry, Type: CodecClass<T>, value: Uint8Array | HexString | unknown[], length = -1): [T[], number, number] {
export function decodeVec<T extends Codec> (registry: Registry, Type: CodecClass<T>, value: Uint8Array | HexString | unknown[], length = -1): [T[], number, number] {
if (Array.isArray(value)) {
const result = new Array<T>(value.length);
@@ -59,7 +59,7 @@ export function decodeVec<T extends Codec> (registry: CodecRegistry, Type: Codec
export class Vec<T extends Codec> extends AbstractArray<T> {
#Type: CodecClass<T>;
constructor (registry: CodecRegistry, Type: CodecClass<T> | string, value: Uint8Array | HexString | unknown[] = []) {
constructor (registry: Registry, Type: CodecClass<T> | string, value: Uint8Array | HexString | unknown[] = []) {
const Clazz = typeToConstructor<T>(registry, Type);
const [values, decodedLength] = decodeVec(registry, Clazz, value);
@@ -70,7 +70,7 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
public static with<O extends Codec> (Type: CodecClass<O> | string): CodecClass<Vec<O>> {
return class extends Vec<O> {
constructor (registry: CodecRegistry, value?: any[]) {
constructor (registry: Registry, value?: any[]) {
super(registry, Type, value);
}
};
+4 -4
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { Codec, CodecClass, CodecRegistry } from '../types';
import type { Codec, CodecClass, Registry } from '../types';
import { assert, isU8a, u8aConcat } from '@polkadot/util';
@@ -11,7 +11,7 @@ import { decodeU8aVec, typeToConstructor } from '../utils';
import { decodeVec } from './Vec';
/** @internal */
function decodeVecFixed<T extends Codec> (registry: CodecRegistry, value: Uint8Array | HexString | unknown[], Type: CodecClass<T>, length: number): [T[], number, number] {
function decodeVecFixed<T extends Codec> (registry: Registry, value: Uint8Array | HexString | unknown[], Type: CodecClass<T>, length: number): [T[], number, number] {
const [values, decodedLength, decodedLengthNoOffset] = decodeVec(registry, Type, value, length);
while (values.length < length) {
@@ -31,7 +31,7 @@ function decodeVecFixed<T extends Codec> (registry: CodecRegistry, value: Uint8A
export class VecFixed<T extends Codec> extends AbstractArray<T> {
#Type: CodecClass<T>;
constructor (registry: CodecRegistry, Type: CodecClass<T> | string, length: number, value: Uint8Array | HexString | unknown[] = [] as unknown[]) {
constructor (registry: Registry, Type: CodecClass<T> | string, length: number, value: Uint8Array | HexString | unknown[] = [] as unknown[]) {
const Clazz = typeToConstructor<T>(registry, Type);
const [values,, decodedLengthNoOffset] = isU8a(value)
? decodeU8aVec(registry, value, 0, Clazz, length)
@@ -44,7 +44,7 @@ export class VecFixed<T extends Codec> extends AbstractArray<T> {
public static with<O extends Codec> (Type: CodecClass<O> | string, length: number): CodecClass<VecFixed<O>> {
return class extends VecFixed<O> {
constructor (registry: CodecRegistry, value?: any[]) {
constructor (registry: Registry, value?: any[]) {
super(registry, Type, length, value);
}
};
@@ -1,14 +1,14 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, CodecClass, CodecRegistry } from '../types';
import type { Codec, CodecClass, Registry } from '../types';
import { CodecMap } from './Map';
export class BTreeMap<K extends Codec = Codec, V extends Codec = Codec> extends CodecMap<K, V> {
public static with<K extends Codec, V extends Codec> (keyType: CodecClass<K> | string, valType: CodecClass<V> | string): CodecClass<CodecMap<K, V>> {
return class extends BTreeMap<K, V> {
constructor (registry: CodecRegistry, value?: Uint8Array | string | Map<any, any>) {
constructor (registry: Registry, value?: Uint8Array | string | Map<any, any>) {
super(registry, keyType, valType, value, 'BTreeMap');
}
};
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, Codec, CodecClass, CodecRegistry, ISet, IU8a } from '../types';
import type { AnyJson, Codec, CodecClass, ISet, IU8a, Registry } from '../types';
import { compactFromU8a, compactToU8a, isHex, isU8a, logger, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
@@ -11,7 +11,7 @@ import { compareSet, decodeU8aVec, sortSet, typeToConstructor } from '../utils';
const l = logger('BTreeSet');
/** @internal */
function decodeSetFromU8a<V extends Codec> (registry: CodecRegistry, ValClass: CodecClass<V>, u8a: Uint8Array): [Set<V>, number] {
function decodeSetFromU8a<V extends Codec> (registry: Registry, ValClass: CodecClass<V>, u8a: Uint8Array): [Set<V>, number] {
const output = new Set<V>();
const [offset, length] = compactFromU8a(u8a);
const [values, decodedLength] = decodeU8aVec(registry, u8a, offset, ValClass, length.toNumber());
@@ -24,7 +24,7 @@ function decodeSetFromU8a<V extends Codec> (registry: CodecRegistry, ValClass: C
}
/** @internal */
function decodeSetFromSet<V extends Codec> (registry: CodecRegistry, ValClass: CodecClass<V>, value: Set<any> | string[]): [Set<V>, number] {
function decodeSetFromSet<V extends Codec> (registry: Registry, ValClass: CodecClass<V>, value: Set<any> | string[]): [Set<V>, number] {
const output = new Set<V>();
value.forEach((val: any) => {
@@ -54,7 +54,7 @@ function decodeSetFromSet<V extends Codec> (registry: CodecRegistry, ValClass: C
* @param jsonSet
* @internal
*/
function decodeSet<V extends Codec> (registry: CodecRegistry, valType: CodecClass<V> | string, value?: Uint8Array | string | string[] | Set<any>): [Set<V>, number] {
function decodeSet<V extends Codec> (registry: Registry, valType: CodecClass<V> | string, value?: Uint8Array | string | string[] | Set<any>): [Set<V>, number] {
if (!value) {
return [new Set<V>(), 0];
}
@@ -71,7 +71,7 @@ function decodeSet<V extends Codec> (registry: CodecRegistry, valType: CodecClas
}
export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V> {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
@@ -79,7 +79,7 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
readonly #ValClass: CodecClass<V>;
constructor (registry: CodecRegistry, valType: CodecClass<V> | string, rawValue?: Uint8Array | string | string[] | Set<any>) {
constructor (registry: Registry, valType: CodecClass<V> | string, rawValue?: Uint8Array | string | string[] | Set<any>) {
const [values, decodedLength] = decodeSet(registry, valType, rawValue);
super(sortSet(values));
@@ -91,7 +91,7 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
public static with<V extends Codec> (valType: CodecClass<V> | string): CodecClass<BTreeSet<V>> {
return class extends BTreeSet<V> {
constructor (registry: CodecRegistry, value?: Uint8Array | string | Set<any>) {
constructor (registry: Registry, value?: Uint8Array | string | Set<any>) {
super(registry, valType, value);
}
};
+2 -2
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyU8a, CodecRegistry } from '../types';
import type { AnyU8a, Registry } from '../types';
import { assert, compactFromU8a, compactToU8a, isString, u8aConcat, u8aToU8a } from '@polkadot/util';
@@ -42,7 +42,7 @@ function decodeBitVec (value?: AnyU8a): [number, Uint8Array] {
export class BitVec extends Raw {
readonly #decodedLength: number;
constructor (registry: CodecRegistry, value?: AnyU8a) {
constructor (registry: Registry, value?: AnyU8a) {
const [decodedLength, u8a] = decodeBitVec(value);
super(registry, u8a);
+2 -2
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyU8a, CodecRegistry } from '../types';
import type { AnyU8a, Registry } from '../types';
import { assert, compactAddLength, compactFromU8a, compactToU8a, isString, isU8a, u8aToU8a } from '@polkadot/util';
@@ -47,7 +47,7 @@ function decodeBytes (value?: AnyU8a): [Uint8Array | undefined, number] {
* as what is found in [[Text]] and [[Vec]])
*/
export class Bytes extends Raw {
constructor (registry: CodecRegistry, value?: AnyU8a) {
constructor (registry: Registry, value?: AnyU8a) {
const [u8a, decodedLength] = decodeBytes(value);
super(registry, u8a, decodedLength);
+2 -2
View File
@@ -1,14 +1,14 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, CodecClass, CodecRegistry } from '../types';
import type { Codec, CodecClass, Registry } from '../types';
import { CodecMap } from './Map';
export class HashMap<K extends Codec = Codec, V extends Codec = Codec> extends CodecMap<K, V> {
public static with<K extends Codec, V extends Codec> (keyType: CodecClass<K> | string, valType: CodecClass<V> | string): CodecClass<CodecMap<K, V>> {
return class extends HashMap<K, V> {
constructor (registry: CodecRegistry, value?: Uint8Array | string | Map<any, any>) {
constructor (registry: Registry, value?: Uint8Array | string | Map<any, any>) {
super(registry, keyType, valType, value);
}
};
+4 -4
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { Codec, CodecClass, CodecRegistry } from '../types';
import type { Codec, CodecClass, Registry } from '../types';
import { Option } from '../base/Option';
import { Tuple } from '../base/Tuple';
@@ -18,7 +18,7 @@ const EMPTY = new Uint8Array();
* @description The wrapper for the result from a LinkedMap
*/
export class Linkage<T extends Codec> extends Struct {
constructor (registry: CodecRegistry, Type: CodecClass | string, value?: unknown) {
constructor (registry: Registry, Type: CodecClass | string, value?: unknown) {
super(registry, {
previous: Option.with(Type),
// eslint-disable-next-line sort-keys
@@ -28,7 +28,7 @@ export class Linkage<T extends Codec> extends Struct {
public static withKey<O extends Codec> (Type: CodecClass | string): CodecClass<Linkage<O>> {
return class extends Linkage<O> {
constructor (registry: CodecRegistry, value?: unknown) {
constructor (registry: Registry, value?: unknown) {
super(registry, Type, value);
}
};
@@ -66,7 +66,7 @@ export class Linkage<T extends Codec> extends Struct {
* @description A Linkage keys/Values tuple
*/
export class LinkageResult extends Tuple {
constructor (registry: CodecRegistry, [TypeKey, keys]: TypeWithValues, [TypeValue, values]: TypeWithValues) {
constructor (registry: Registry, [TypeKey, keys]: TypeWithValues, [TypeValue, values]: TypeWithValues) {
super(registry, {
Keys: Vec.with(TypeKey),
Values: Vec.with(TypeValue)
+6 -6
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, Codec, CodecClass, CodecRegistry, IMap, IU8a } from '../types';
import type { AnyJson, Codec, CodecClass, IMap, IU8a, Registry } from '../types';
import { compactFromU8a, compactToU8a, isHex, isObject, isU8a, logger, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
@@ -14,7 +14,7 @@ import { compareMap, decodeU8a, sortMap, typeToConstructor } from '../utils';
const l = logger('Map');
/** @internal */
function decodeMapFromU8a<K extends Codec, V extends Codec> (registry: CodecRegistry, KeyClass: CodecClass<K>, ValClass: CodecClass<V>, u8a: Uint8Array): [Map<K, V>, number] {
function decodeMapFromU8a<K extends Codec, V extends Codec> (registry: Registry, KeyClass: CodecClass<K>, ValClass: CodecClass<V>, u8a: Uint8Array): [Map<K, V>, number] {
const output = new Map<K, V>();
const [offset, length] = compactFromU8a(u8a);
const count = length.toNumber();
@@ -34,7 +34,7 @@ function decodeMapFromU8a<K extends Codec, V extends Codec> (registry: CodecRegi
}
/** @internal */
function decodeMapFromMap<K extends Codec, V extends Codec> (registry: CodecRegistry, KeyClass: CodecClass<K>, ValClass: CodecClass<V>, value: Map<any, any>): [Map<K, V>, number] {
function decodeMapFromMap<K extends Codec, V extends Codec> (registry: Registry, KeyClass: CodecClass<K>, ValClass: CodecClass<V>, value: Map<any, any>): [Map<K, V>, number] {
const output = new Map<K, V>();
for (const [key, val] of value.entries()) {
@@ -76,7 +76,7 @@ function decodeMapFromMap<K extends Codec, V extends Codec> (registry: CodecRegi
* @param jsonMap
* @internal
*/
function decodeMap<K extends Codec, V extends Codec> (registry: CodecRegistry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, value?: Uint8Array | string | Map<any, any>): [Map<K, V>, number] {
function decodeMap<K extends Codec, V extends Codec> (registry: Registry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, value?: Uint8Array | string | Map<any, any>): [Map<K, V>, number] {
const KeyClass = typeToConstructor(registry, keyType);
const ValClass = typeToConstructor(registry, valType);
@@ -94,7 +94,7 @@ function decodeMap<K extends Codec, V extends Codec> (registry: CodecRegistry, k
}
export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends Map<K, V> implements IMap<K, V> {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
@@ -106,7 +106,7 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
readonly #type: string;
constructor (registry: CodecRegistry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, rawValue: Uint8Array | string | Map<any, any> | undefined, type: 'BTreeMap' | 'HashMap' = 'HashMap') {
constructor (registry: Registry, keyType: CodecClass<K> | string, valType: CodecClass<V> | string, rawValue: Uint8Array | string | Map<any, any> | undefined, type: 'BTreeMap' | 'HashMap' = 'HashMap') {
const [decoded, decodedLength] = decodeMap(registry, keyType, valType, rawValue);
super(type === 'BTreeMap' ? sortMap(decoded) : decoded);
+5 -5
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyTuple, CodecClass, CodecRegistry, INumber } from '../types';
import type { AnyTuple, CodecClass, INumber, Registry } from '../types';
import { Tuple } from '../base/Tuple';
@@ -15,7 +15,7 @@ type RangeType = 'Range' | 'RangeInclusive';
export class Range<T extends INumber> extends Tuple {
#rangeName: RangeType;
constructor (registry: CodecRegistry, Type: CodecClass<T> | string, value?: AnyTuple, rangeName: RangeType = 'Range') {
constructor (registry: Registry, Type: CodecClass<T> | string, value?: AnyTuple, rangeName: RangeType = 'Range') {
super(registry, {
end: Type,
start: Type
@@ -26,7 +26,7 @@ export class Range<T extends INumber> extends Tuple {
public static override with <T extends INumber> (Types: CodecClass<T> | string): CodecClass<Range<T>> {
return class extends Range<T> {
constructor (registry: CodecRegistry, value?: AnyTuple) {
constructor (registry: Registry, value?: AnyTuple) {
super(registry, Types, value);
}
};
@@ -55,13 +55,13 @@ export class Range<T extends INumber> extends Tuple {
}
export class RangeInclusive<T extends INumber = INumber> extends Range<T> {
constructor (registry: CodecRegistry, Type: CodecClass<T> | string, value?: AnyTuple) {
constructor (registry: Registry, Type: CodecClass<T> | string, value?: AnyTuple) {
super(registry, Type, value, 'RangeInclusive');
}
public static override with <T extends INumber> (Type: CodecClass<T> | string): CodecClass<RangeInclusive<T>> {
return class extends RangeInclusive<T> {
constructor (registry: CodecRegistry, value?: AnyTuple) {
constructor (registry: Registry, value?: AnyTuple) {
super(registry, Type, value);
}
};
+2 -2
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { CodecRegistry } from '../types';
import type { Registry } from '../types';
import { Text } from '../native/Text';
import { sanitize } from '../utils';
@@ -14,7 +14,7 @@ import { sanitize } from '../utils';
* i.e. we remove the `T::` prefixes found in some types for consistency across implementation.
*/
export class Type extends Text {
constructor (registry: CodecRegistry, value: Text | Uint8Array | string = '') {
constructor (registry: Registry, value: Text | Uint8Array | string = '') {
super(registry, value);
this.setOverride(sanitize(this.toString()));
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyU8a, CodecClass, CodecRegistry, U8aBitLength } from '../types';
import type { AnyU8a, CodecClass, Registry, U8aBitLength } from '../types';
import { assert, u8aToU8a } from '@polkadot/util';
@@ -28,7 +28,7 @@ function decodeU8aFixed (value: AnyU8a, bitLength: U8aBitLength): [AnyU8a, numbe
* to be used directly, rather is should be subclassed with the specific lengths.
*/
export class U8aFixed extends Raw {
constructor (registry: CodecRegistry, value: AnyU8a = new Uint8Array(), bitLength: U8aBitLength = 256) {
constructor (registry: Registry, value: AnyU8a = new Uint8Array(), bitLength: U8aBitLength = 256) {
const [u8a, decodedLength] = decodeU8aFixed(value, bitLength);
super(registry, u8a, decodedLength);
@@ -36,7 +36,7 @@ export class U8aFixed extends Raw {
public static with (bitLength: U8aBitLength, typeName?: string): CodecClass<U8aFixed> {
return class extends U8aFixed {
constructor (registry: CodecRegistry, value?: AnyU8a) {
constructor (registry: Registry, value?: AnyU8a) {
super(registry, value, bitLength);
}
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { AnyJson, AnyU8a, Codec, CodecClass, CodecRegistry } from '../types';
import type { AnyJson, AnyU8a, Codec, CodecClass, Registry } from '../types';
import { assertReturn, compactStripLength, isHex, isU8a } from '@polkadot/util';
@@ -9,7 +9,7 @@ import { Raw } from '../native/Raw';
import { typeToConstructor } from '../utils';
import { Bytes } from './Bytes';
function decodeRaw<T extends Codec> (registry: CodecRegistry, typeName: CodecClass<T> | string, value?: AnyU8a | Codec): [CodecClass<T>, T | null, AnyU8a] {
function decodeRaw<T extends Codec> (registry: Registry, typeName: CodecClass<T> | string, value?: AnyU8a | Codec): [CodecClass<T>, T | null, AnyU8a] {
const Type = typeToConstructor(registry, typeName);
if (isU8a(value) || isHex(value)) {
@@ -34,7 +34,7 @@ export class WrapperKeepOpaque<T extends Codec> extends Bytes {
readonly #decoded: T | null;
constructor (registry: CodecRegistry, typeName: CodecClass<T> | string, value?: AnyU8a | Codec) {
constructor (registry: Registry, typeName: CodecClass<T> | string, value?: AnyU8a | Codec) {
const [Type, decoded, u8a] = decodeRaw(registry, typeName, value);
super(registry, u8a);
@@ -45,7 +45,7 @@ export class WrapperKeepOpaque<T extends Codec> extends Bytes {
public static with<T extends Codec> (Type: CodecClass<T> | string): CodecClass<WrapperKeepOpaque<T>> {
return class extends WrapperKeepOpaque<T> {
constructor (registry: CodecRegistry, value?: AnyU8a | T) {
constructor (registry: Registry, value?: AnyU8a | T) {
super(registry, Type, value);
}
};
@@ -1,14 +1,14 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, CodecClass, CodecRegistry } from '../types';
import type { Codec, CodecClass, Registry } from '../types';
import { compactAddLength, compactStripLength, isU8a } from '@polkadot/util';
import { Base } from '../base/Base';
import { typeToConstructor } from '../utils';
function decodeRaw<T extends Codec> (registry: CodecRegistry, Type: CodecClass<T> | string, value?: unknown): T {
function decodeRaw<T extends Codec> (registry: Registry, Type: CodecClass<T> | string, value?: unknown): T {
const Clazz = typeToConstructor<T>(registry, Type);
if (isU8a(value)) {
@@ -21,13 +21,13 @@ function decodeRaw<T extends Codec> (registry: CodecRegistry, Type: CodecClass<T
}
export class WrapperOpaque<T extends Codec> extends Base<T> {
constructor (registry: CodecRegistry, Type: CodecClass<T> | string, value?: unknown) {
constructor (registry: Registry, Type: CodecClass<T> | string, value?: unknown) {
super(registry, decodeRaw(registry, Type, value));
}
public static with<T extends Codec> (Type: CodecClass<T> | string): CodecClass<WrapperOpaque<T>> {
return class extends WrapperOpaque<T> {
constructor (registry: CodecRegistry, value?: unknown) {
constructor (registry: Registry, value?: unknown) {
super(registry, Type, value);
}
};
+3 -3
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { Codec, CodecRegistry, IU8a } from '../types';
import type { Codec, IU8a, Registry } from '../types';
import { isU8a, u8aToHex } from '@polkadot/util';
@@ -24,12 +24,12 @@ function decodeBool (value: any): boolean {
* @noInheritDoc
*/
export class bool extends Boolean implements Codec {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
// eslint-disable-next-line @typescript-eslint/ban-types
constructor (registry: CodecRegistry, value: bool | Boolean | Uint8Array | boolean | number = false) {
constructor (registry: Registry, value: bool | Boolean | Uint8Array | boolean | number = false) {
super(decodeBool(value));
this.registry = registry;
+3 -3
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyNumber, CodecRegistry, INumber, IU8a, UIntBitLength } from '../types';
import type { AnyNumber, INumber, IU8a, Registry, UIntBitLength } from '../types';
import { BN, bnToBn, bnToHex, bnToU8a, isString, isU8a, u8aToBn } from '@polkadot/util';
@@ -33,11 +33,11 @@ function decodeDate (value: CodecDate | Date | AnyNumber): Date {
* @noInheritDoc
*/
export class CodecDate extends Date implements INumber {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
constructor (registry: CodecRegistry, value: CodecDate | Date | AnyNumber = 0) {
constructor (registry: Registry, value: CodecDate | Date | AnyNumber = 0) {
super(decodeDate(value));
this.registry = registry;
+3 -3
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, Codec, CodecRegistry, IU8a } from '../types';
import type { AnyJson, Codec, IU8a, Registry } from '../types';
import { isFunction, objectProperties, stringify } from '@polkadot/util';
@@ -22,11 +22,11 @@ function decodeJson (value?: Record<string, unknown> | null): [string, any][] {
* @noInheritDoc
*/
export class Json extends Map<string, any> implements Codec {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
constructor (registry: CodecRegistry, value?: Record<string, unknown> | null) {
constructor (registry: Registry, value?: Record<string, unknown> | null) {
const decoded = decodeJson(value);
super(decoded);
+3 -3
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, AnyU8a, CodecRegistry, IU8a } from '../types';
import type { AnyJson, AnyU8a, IU8a, Registry } from '../types';
import { assert, isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util';
@@ -16,13 +16,13 @@ import { assert, isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a }
* @noInheritDoc
*/
export class Raw extends Uint8Array implements IU8a {
readonly registry: CodecRegistry;
readonly registry: Registry;
public createdAtHash?: IU8a;
readonly initialU8aLength?: number;
constructor (registry: CodecRegistry, value?: AnyU8a, initialU8aLength?: number) {
constructor (registry: Registry, value?: AnyU8a, initialU8aLength?: number) {
super(u8aToU8a(value));
this.registry = registry;
+4 -4
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { CodecClass, CodecRegistry, ISet, IU8a } from '../types';
import type { CodecClass, ISet, IU8a, Registry } from '../types';
import { assert, BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, objectProperties, stringify, stringPascalCase, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
@@ -86,7 +86,7 @@ function decodeSet (setValues: SetValues, value: string[] | Set<string> | Uint8A
* a bitwise representation of the values.
*/
export class CodecSet extends Set<string> implements ISet<string> {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
@@ -94,7 +94,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
readonly #byteLength: number;
constructor (registry: CodecRegistry, setValues: SetValues, value?: string[] | Set<string> | Uint8Array | BN | number | string, bitLength = 8) {
constructor (registry: Registry, setValues: SetValues, value?: string[] | Set<string> | Uint8Array | BN | number | string, bitLength = 8) {
super(decodeSet(setValues, value, bitLength));
this.registry = registry;
@@ -111,7 +111,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
}
return class extends CodecSet {
constructor (registry: CodecRegistry, value?: unknown) {
constructor (registry: Registry, value?: unknown) {
super(registry, values, value as undefined, bitLength);
objectProperties(this, isKeys, (_, i) => this.strings.includes(keys[i]));
+6 -6
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyJson, BareOpts, Codec, CodecClass, CodecClassDef, CodecRegistry, IStruct, IU8a } from '../types';
import type { AnyJson, BareOpts, Codec, CodecClass, CodecClassDef, IStruct, IU8a, Registry } from '../types';
import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
@@ -11,7 +11,7 @@ import { compareMap, decodeU8a, mapToTypeMap, typesToMap } from '../utils';
type TypesDef<T = Codec> = Record<string, string | CodecClass<T>>;
/** @internal */
function decodeStructFromObject (registry: CodecRegistry, Types: CodecClassDef, value: any, jsonMap: Map<string, string>): [Iterable<[string, Codec]>, number] {
function decodeStructFromObject (registry: Registry, Types: CodecClassDef, value: any, jsonMap: Map<string, string>): [Iterable<[string, Codec]>, number] {
let jsonObj: Record<string, unknown> | undefined;
const inputKeys = Object.keys(Types);
const typeofArray = Array.isArray(value);
@@ -90,7 +90,7 @@ 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> {
#registry: CodecRegistry;
#registry: Registry;
public createdAtHash?: IU8a;
@@ -100,7 +100,7 @@ export class Struct<
readonly #Types: CodecClassDef;
constructor (registry: CodecRegistry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap = new Map<string, string>()) {
constructor (registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap = new Map<string, string>()) {
const typeMap = mapToTypeMap(registry, Types);
const [decoded, decodedLength] = isU8a(value)
? decodeU8a<Codec, [string, Codec]>(registry, value, typeMap, true)
@@ -122,7 +122,7 @@ export class Struct<
const keys = Object.keys(Types);
return class extends Struct<S> {
constructor (registry: CodecRegistry, value?: unknown) {
constructor (registry: Registry, value?: unknown) {
super(registry, Types, value as HexString, jsonMap);
objectProperties(this, keys, (k) => this.get(k));
@@ -190,7 +190,7 @@ export class Struct<
return this.registry.hash(this.toU8a());
}
public get registry (): CodecRegistry {
public get registry (): Registry {
return this.#registry;
}
+3 -3
View File
@@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0
import type { HexString } from '@polkadot/util/types';
import type { AnyU8a, Codec, CodecRegistry, IU8a } from '../types';
import type { AnyU8a, Codec, IU8a, Registry } from '../types';
import { assert, compactAddLength, compactFromU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
@@ -48,7 +48,7 @@ function decodeText (value?: null | Text | string | AnyU8a | { toString: () => s
// TODO
// - Strings should probably be trimmed (docs do come through with extra padding)
export class Text extends String implements Codec {
public readonly registry: CodecRegistry;
public readonly registry: Registry;
public createdAtHash?: IU8a;
@@ -56,7 +56,7 @@ export class Text extends String implements Codec {
#override: string | null = null;
constructor (registry: CodecRegistry, value?: null | Text | string | AnyU8a | { toString: () => string }) {
constructor (registry: Registry, value?: null | Text | string | AnyU8a | { toString: () => string }) {
const [str, decodedLength] = decodeText(value);
super(str);
+2 -2
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { CodecRegistry } from '../types';
import type { Registry } from '../types';
import { u32 } from './U32';
@@ -13,7 +13,7 @@ import { u32 } from './U32';
* It is not to be used, since it created consensus mismatches.
*/
export class usize extends u32 {
constructor (registry: CodecRegistry, value?: unknown) {
constructor (registry: Registry, value?: unknown) {
super(registry, value);
throw new Error('The `usize` type should not be used. Since it is platform-specific, it creates incompatibilities between native (generally u64) and WASM (always u32) code. Use one of the `u32` or `u64` types explicitly.');
+3 -3
View File
@@ -4,7 +4,7 @@
import type { HexString } from '@polkadot/util/types';
import type { AnyJson } from './helpers';
import type { IU8a } from './interfaces';
import type { CodecRegistry } from './registry';
import type { Registry } from './registry';
export type BareOpts = boolean | Record<string, boolean>;
@@ -39,7 +39,7 @@ export interface Codec {
/**
* @description The registry associated with this object
*/
readonly registry: CodecRegistry;
readonly registry: Registry;
/**
* @description The block at which this value was retrieved/created (set to non-empty when retrieved from storage)
@@ -91,7 +91,7 @@ export interface CodecClass<T = Codec> {
// NOTE: We need the any[] here, unknown[] does not work as expected
// eslint-disable-next-line @typescript-eslint/no-explicit-any
new(registry: CodecRegistry, ...args: any[]): T;
new(registry: Registry, ...args: any[]): T;
}
export type CodecClassDef<T = Codec> = Record<string, CodecClass<T>>;
+4 -4
View File
@@ -47,12 +47,12 @@ export interface CallFunction<A extends AnyTuple = AnyTuple, M = any> extends Ca
(...args: any[]): IMethod<A, M>;
}
export interface CodecRegistry {
export interface Registry {
readonly chainDecimals: number[];
readonly chainSS58: number | undefined;
readonly chainTokens: string[];
// readonly knownTypes: RegisteredTypes;
// readonly lookup: PortableCodecRegistry;
// readonly lookup: PortableRegistry;
// readonly metadata: MetadataLatest;
// readonly unknownTypes: string[];
// readonly signedExtensions: string[];
@@ -88,13 +88,13 @@ export interface CodecRegistry {
hasDef (name: string): boolean;
hasType (name: string): boolean;
hash (data: Uint8Array): IU8a;
// init (): CodecRegistry;
// init (): Registry;
register (type: CodecClass | RegistryTypes): void;
register (name: string, type: CodecClass): void;
register (arg1: string | CodecClass | RegistryTypes, arg2?: CodecClass): void;
// setChainProperties (properties?: ChainProperties): void;
// setHasher (hasher?: IU8aer | null): void;
// setLookup (lookup: PortableCodecRegistry): void;
// setLookup (lookup: PortableRegistry): void;
setMetadata (metadata: unknown, signedExtensions?: string[], userExtensions?: unknown): void;
// setSignedExtensions (signedExtensions?: string[], userExtensions?: ExtDef): void;
}
+4 -4
View File
@@ -1,7 +1,7 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, CodecClass, CodecRegistry } from '../types';
import type { Codec, CodecClass, Registry } from '../types';
import { u8aToHex } from '@polkadot/util';
@@ -9,7 +9,7 @@ function formatFailure (error: Error, type: string | null, u8a: Uint8Array, key?
return `decodeU8a: failed at ${u8aToHex(u8a)}…${key ? ` on ${key}` : ''}${type ? `: ${type}` : ''}:: ${error.message}`;
}
function getRawType (registry: CodecRegistry, Type: CodecClass): string | null {
function getRawType (registry: Registry, Type: CodecClass): string | null {
try {
return new Type(registry).toRawType();
} catch {
@@ -25,7 +25,7 @@ function getRawType (registry: CodecRegistry, Type: CodecClass): string | null {
* @param result - The result array (will be returned with values pushed)
* @param types - The array of CodecClass to decode the U8a against.
*/
export function decodeU8a <T extends Codec = Codec, E = T> (registry: CodecRegistry, u8a: Uint8Array, types: CodecClass[] | { [index: string]: CodecClass }, withZip?: boolean): [E[], number] {
export function decodeU8a <T extends Codec = Codec, E = T> (registry: Registry, u8a: Uint8Array, types: CodecClass[] | { [index: string]: CodecClass }, withZip?: boolean): [E[], number] {
const [Types, keys]: [CodecClass[], string[]] = Array.isArray(types)
? [types, []]
: [Object.values(types), Object.keys(types)];
@@ -56,7 +56,7 @@ export function decodeU8a <T extends Codec = Codec, E = T> (registry: CodecRegis
// Split from decodeU8a since this is specialized to 1 instance ... yes duplication, but
// since we have to do less checks (and these are intensive anyway), much faster
export function decodeU8aVec <T extends Codec = Codec> (registry: CodecRegistry, u8a: Uint8Array, start: number, Type: CodecClass<T>, count: number): [T[], number, number] {
export function decodeU8aVec <T extends Codec = Codec> (registry: Registry, u8a: Uint8Array, start: number, Type: CodecClass<T>, count: number): [T[], number, number] {
const result = new Array<T>(count);
let offset = start;
@@ -1,14 +1,14 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { CodecClass, CodecRegistry } from '../types';
import type { CodecClass, Registry } from '../types';
import { typeToConstructor } from './typeToConstructor';
/**
* @description takes an input map of the form `{ [string]: string | CodecClass }` and returns a map of `{ [string]: CodecClass }`
*/
export function mapToTypeMap (registry: CodecRegistry, input: Record<string, string | CodecClass>): Record<string, CodecClass> {
export function mapToTypeMap (registry: Registry, input: Record<string, string | CodecClass>): Record<string, CodecClass> {
const entries = Object.entries(input);
const output: Record<string, CodecClass> = {};
@@ -1,11 +1,11 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { Codec, CodecClass, CodecRegistry } from '../types';
import type { Codec, CodecClass, Registry } from '../types';
import { isString } from '@polkadot/util';
export function typeToConstructor <T extends Codec = Codec> (registry: CodecRegistry, type: string | CodecClass<T>): CodecClass<T> {
export function typeToConstructor <T extends Codec = Codec> (registry: Registry, type: string | CodecClass<T>): CodecClass<T> {
return (
isString(type)
? registry.createClassUnsafe(type)
+2 -2
View File
@@ -1,9 +1,9 @@
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
// SPDX-License-Identifier: Apache-2.0
import type { CodecClass, CodecRegistry } from '../types';
import type { CodecClass, Registry } from '../types';
export function typesToMap (registry: CodecRegistry, Types: Record<string, CodecClass>): Record<string, string> {
export function typesToMap (registry: Registry, Types: Record<string, CodecClass>): Record<string, string> {
const entries = Object.entries(Types);
const result: Record<string, string> = {};