Adjust AbstractArray & Struct decoders (#4127)
* Adjust AbstractArray & Struct decoders * Fixes, add Text & Bytes/Raw to shortcuts * Additional * Additional * Remove debug log * Revert extra arg, make CC happy * BTreeSet
This commit is contained in:
@@ -4,6 +4,10 @@
|
||||
|
||||
Changes:
|
||||
|
||||
- Fix construction for `palletVersion` keys with correct prefix
|
||||
- Add Polkadot `EthereumAddress` as a known `PortableRegistry` override
|
||||
- De-dupe `PortableRegistry` namespace splitting operations
|
||||
- Adjust internal `AbstractArray` & `Struct` decoders with inital u8a lengths
|
||||
- Use internal `lookupName` in `createClass` (allows deeply recursive lookups)
|
||||
- Update known upgrade blocks for Westend & Kusama
|
||||
- Bump static metadata for latest Substrate, Polkadot & Kusama
|
||||
|
||||
@@ -20,21 +20,34 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
|
||||
public createdAtHash?: Hash;
|
||||
|
||||
protected constructor (registry: Registry, values: T[]) {
|
||||
readonly #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
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
this[i] = values[i];
|
||||
}
|
||||
|
||||
this.registry = registry;
|
||||
this.#initialU8aLength = initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return this.reduce((total, entry) => total + entry.encodedLength, compactToU8a(this.length).length);
|
||||
// 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
|
||||
return this.reduce((total, e) => total + e.encodedLength, compactToU8a(this.length).length);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
||||
*/
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return this.#initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -67,6 +67,8 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
|
||||
readonly #bitLength: UIntBitLength;
|
||||
|
||||
readonly #byteLength: number;
|
||||
|
||||
readonly #isSigned: boolean;
|
||||
|
||||
constructor (registry: Registry, value: AnyNumber = 0, bitLength: UIntBitLength = DEFAULT_UINT_BITS, isSigned = false) {
|
||||
@@ -74,6 +76,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
|
||||
this.registry = registry;
|
||||
this.#bitLength = bitLength;
|
||||
this.#byteLength = this.#bitLength / 8;
|
||||
this.#isSigned = isSigned;
|
||||
|
||||
const isPositive = this.gte(BN_ZERO);
|
||||
@@ -87,7 +90,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return this.#bitLength / 8;
|
||||
return this.#byteLength;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,26 +11,20 @@ import { compareSet, decodeU8a, sortSet, typeToConstructor } from './utils';
|
||||
const l = logger('BTreeSet');
|
||||
|
||||
/** @internal */
|
||||
function decodeSetFromU8a<V extends Codec> (registry: Registry, ValClass: Constructor<V>, u8a: Uint8Array): Set<V> {
|
||||
function decodeSetFromU8a<V extends Codec> (registry: Registry, ValClass: Constructor<V>, u8a: Uint8Array): [Set<V>, number] {
|
||||
const output = new Set<V>();
|
||||
const [offset, length] = compactFromU8a(u8a);
|
||||
const types = [];
|
||||
|
||||
for (let i = 0; i < length.toNumber(); i++) {
|
||||
types.push(ValClass);
|
||||
}
|
||||
|
||||
const values = decodeU8a<V>(registry, u8a.subarray(offset), types);
|
||||
const [values, decodedLength] = decodeU8a<V>(registry, u8a.subarray(offset), new Array(length.toNumber()).fill(ValClass));
|
||||
|
||||
for (let i = 0; i < values.length; i++) {
|
||||
output.add(values[i]);
|
||||
}
|
||||
|
||||
return output;
|
||||
return [output, decodedLength + offset];
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function decodeSetFromSet<V extends Codec> (registry: Registry, ValClass: Constructor<V>, value: Set<any> | string[]): Set<V> {
|
||||
function decodeSetFromSet<V extends Codec> (registry: Registry, ValClass: Constructor<V>, value: Set<any> | string[]): [Set<V>, number] {
|
||||
const output = new Set<V>();
|
||||
|
||||
value.forEach((val: any) => {
|
||||
@@ -43,7 +37,7 @@ function decodeSetFromSet<V extends Codec> (registry: Registry, ValClass: Constr
|
||||
}
|
||||
});
|
||||
|
||||
return output;
|
||||
return [output, 0];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -60,9 +54,9 @@ function decodeSetFromSet<V extends Codec> (registry: Registry, ValClass: Constr
|
||||
* @param jsonSet
|
||||
* @internal
|
||||
*/
|
||||
function decodeSet<V extends Codec> (registry: Registry, valType: Constructor<V> | string, value?: Uint8Array | string | string[] | Set<any>): Set<V> {
|
||||
function decodeSet<V extends Codec> (registry: Registry, valType: Constructor<V> | string, value?: Uint8Array | string | string[] | Set<any>): [Set<V>, number] {
|
||||
if (!value) {
|
||||
return new Set<V>();
|
||||
return [new Set<V>(), 0];
|
||||
}
|
||||
|
||||
const ValClass = typeToConstructor(registry, valType);
|
||||
@@ -81,12 +75,17 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
|
||||
public createdAtHash?: Hash;
|
||||
|
||||
readonly #initialU8aLength?: number;
|
||||
|
||||
readonly #ValClass: Constructor<V>;
|
||||
|
||||
constructor (registry: Registry, valType: Constructor<V> | string, rawValue?: Uint8Array | string | string[] | Set<any>) {
|
||||
super(sortSet(decodeSet(registry, valType, rawValue)));
|
||||
const [values, decodedLength] = decodeSet(registry, valType, rawValue);
|
||||
|
||||
super(sortSet(values));
|
||||
|
||||
this.registry = registry;
|
||||
this.#initialU8aLength = decodedLength;
|
||||
this.#ValClass = typeToConstructor(registry, valType);
|
||||
}
|
||||
|
||||
@@ -111,6 +110,13 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
||||
*/
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return this.#initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hash of the value
|
||||
*/
|
||||
|
||||
@@ -13,11 +13,14 @@ export abstract class Base<T extends Codec> implements Codec {
|
||||
|
||||
public createdAtHash?: Hash;
|
||||
|
||||
readonly #initialU8aLength?: number;
|
||||
|
||||
protected readonly _raw: T;
|
||||
|
||||
protected constructor (registry: Registry, value: T) {
|
||||
protected constructor (registry: Registry, value: T, initialU8aLength?: number) {
|
||||
this.registry = registry;
|
||||
this._raw = value;
|
||||
this.#initialU8aLength = initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -27,6 +30,13 @@ export abstract class Base<T extends Codec> implements Codec {
|
||||
return this.toU8a().length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
||||
*/
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return this.#initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
|
||||
@@ -24,12 +24,18 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
|
||||
readonly #Type: Constructor<T>;
|
||||
|
||||
readonly #initialU8aLength?: number;
|
||||
|
||||
readonly #raw: T;
|
||||
|
||||
constructor (registry: Registry, Type: Constructor<T> | string, value: Compact<T> | AnyNumber = 0) {
|
||||
this.registry = registry;
|
||||
this.#Type = typeToConstructor(registry, Type);
|
||||
this.#raw = Compact.decodeCompact<T>(registry, this.#Type, value) as T;
|
||||
|
||||
const [raw, decodedLength] = Compact.decodeCompact<T>(registry, this.#Type, value);
|
||||
|
||||
this.#initialU8aLength = decodedLength;
|
||||
this.#raw = raw;
|
||||
}
|
||||
|
||||
public static with<T extends INumber> (Type: Constructor<T> | string): Constructor<Compact<T>> {
|
||||
@@ -41,14 +47,16 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
public static decodeCompact<T extends INumber> (registry: Registry, Type: Constructor<T>, value: Compact<T> | AnyNumber): INumber {
|
||||
public static decodeCompact<T extends INumber> (registry: Registry, Type: Constructor<T>, value: Compact<T> | AnyNumber): [T, number] {
|
||||
if (value instanceof Compact) {
|
||||
return new Type(registry, value.#raw);
|
||||
return [new Type(registry, value.#raw), 0];
|
||||
} else if (isString(value) || isNumber(value) || isBn(value) || isBigInt(value)) {
|
||||
return new Type(registry, value);
|
||||
return [new Type(registry, value), 0];
|
||||
}
|
||||
|
||||
return new Type(registry, compactFromU8a(value)[1]);
|
||||
const [decodedLength, bn] = compactFromU8a(value);
|
||||
|
||||
return [new Type(registry, bn), decodedLength];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -58,6 +66,13 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
return this.toU8a().length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
||||
*/
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return this.#initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
|
||||
@@ -169,6 +169,8 @@ export class Enum implements IEnum {
|
||||
|
||||
readonly #entryIndex: number;
|
||||
|
||||
readonly #initialU8aLength?: number;
|
||||
|
||||
readonly #indexes: number[];
|
||||
|
||||
readonly #isBasic: boolean;
|
||||
@@ -188,6 +190,10 @@ export class Enum implements IEnum {
|
||||
this.#indexes = Object.values(defInfo.def).map(({ index }) => index);
|
||||
this.#entryIndex = this.#indexes.indexOf(decoded.index) || 0;
|
||||
this.#raw = decoded.value;
|
||||
|
||||
if (this.#raw.initialU8aLength) {
|
||||
this.#initialU8aLength = 1 + this.#raw.initialU8aLength;
|
||||
}
|
||||
}
|
||||
|
||||
public static with (Types: Record<string, string | Constructor> | Record<string, number> | string[]): EnumConstructor<Enum> {
|
||||
@@ -227,6 +233,13 @@ export class Enum implements IEnum {
|
||||
return 1 + this.#raw.encodedLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
||||
*/
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return this.#initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
|
||||
@@ -14,26 +14,27 @@ import { compareMap, decodeU8a, sortMap, typeToConstructor } from './utils';
|
||||
const l = logger('Map');
|
||||
|
||||
/** @internal */
|
||||
function decodeMapFromU8a<K extends Codec, V extends Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, u8a: Uint8Array): Map<K, V> {
|
||||
function decodeMapFromU8a<K extends Codec, V extends Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, u8a: Uint8Array): [Map<K, V>, number] {
|
||||
const output = new Map<K, V>();
|
||||
const [offset, length] = compactFromU8a(u8a);
|
||||
const count = length.toNumber();
|
||||
const types = [];
|
||||
|
||||
for (let i = 0; i < length.toNumber(); i++) {
|
||||
for (let i = 0; i < count; i++) {
|
||||
types.push(KeyClass, ValClass);
|
||||
}
|
||||
|
||||
const values = decodeU8a(registry, u8a.subarray(offset), types);
|
||||
const [values, decodedLength] = decodeU8a(registry, u8a.subarray(offset), types);
|
||||
|
||||
for (let i = 0; i < values.length; i += 2) {
|
||||
output.set(values[i] as K, values[i + 1] as V);
|
||||
}
|
||||
|
||||
return output;
|
||||
return [output, offset + decodedLength];
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function decodeMapFromMap<K extends Codec, V extends Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, value: Map<any, any>): Map<K, V> {
|
||||
function decodeMapFromMap<K extends Codec, V extends Codec> (registry: Registry, KeyClass: Constructor<K>, ValClass: Constructor<V>, value: Map<any, any>): [Map<K, V>, number] {
|
||||
const output = new Map<K, V>();
|
||||
|
||||
value.forEach((val: unknown, key: unknown) => {
|
||||
@@ -57,7 +58,7 @@ function decodeMapFromMap<K extends Codec, V extends Codec> (registry: Registry,
|
||||
}
|
||||
});
|
||||
|
||||
return output;
|
||||
return [output, 0];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,12 +76,12 @@ function decodeMapFromMap<K extends Codec, V extends Codec> (registry: Registry,
|
||||
* @param jsonMap
|
||||
* @internal
|
||||
*/
|
||||
function decodeMap<K extends Codec, V extends Codec> (registry: Registry, keyType: Constructor<K> | string, valType: Constructor<V> | string, value?: Uint8Array | string | Map<any, any>): Map<K, V> {
|
||||
function decodeMap<K extends Codec, V extends Codec> (registry: Registry, keyType: Constructor<K> | string, valType: Constructor<V> | string, value?: Uint8Array | string | Map<any, any>): [Map<K, V>, number] {
|
||||
const KeyClass = typeToConstructor(registry, keyType);
|
||||
const ValClass = typeToConstructor(registry, valType);
|
||||
|
||||
if (!value) {
|
||||
return new Map<K, V>();
|
||||
return [new Map<K, V>(), 0];
|
||||
} else if (isU8a(value) || isHex(value)) {
|
||||
return decodeMapFromU8a<K, V>(registry, KeyClass, ValClass, u8aToU8a(value));
|
||||
} else if (value instanceof Map) {
|
||||
@@ -101,16 +102,19 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
|
||||
readonly #ValClass: Constructor<V>;
|
||||
|
||||
readonly #initialU8aLength?: number;
|
||||
|
||||
readonly #type: string;
|
||||
|
||||
constructor (registry: Registry, keyType: Constructor<K> | string, valType: Constructor<V> | string, rawValue: Uint8Array | string | Map<any, any> | undefined, type: 'BTreeMap' | 'HashMap' = 'HashMap') {
|
||||
const decoded = decodeMap(registry, keyType, valType, rawValue);
|
||||
const [decoded, decodedLength] = decodeMap(registry, keyType, valType, rawValue);
|
||||
|
||||
super(type === 'BTreeMap' ? sortMap(decoded) : decoded);
|
||||
|
||||
this.registry = registry;
|
||||
this.#KeyClass = typeToConstructor(registry, keyType);
|
||||
this.#ValClass = typeToConstructor(registry, valType);
|
||||
this.#initialU8aLength = decodedLength;
|
||||
this.#type = type;
|
||||
}
|
||||
|
||||
@@ -127,6 +131,13 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
||||
*/
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return this.#initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description Returns a hash of the value
|
||||
*/
|
||||
|
||||
@@ -54,12 +54,18 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
|
||||
readonly #Type: Constructor<T>;
|
||||
|
||||
readonly #initialU8aLength?: number;
|
||||
|
||||
readonly #raw: T;
|
||||
|
||||
constructor (registry: Registry, typeName: Constructor<T> | string, value?: unknown) {
|
||||
this.registry = registry;
|
||||
this.#Type = typeToConstructor(registry, typeName);
|
||||
this.#raw = decodeOption(registry, typeName, value) as T;
|
||||
|
||||
if (this.#raw.initialU8aLength) {
|
||||
this.#initialU8aLength = 1 + this.#raw.initialU8aLength;
|
||||
}
|
||||
}
|
||||
|
||||
public static with<O extends Codec> (Type: Constructor<O> | string): Constructor<Option<O>> {
|
||||
@@ -78,6 +84,13 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
return 1 + this.#raw.encodedLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
||||
*/
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return this.#initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
|
||||
@@ -20,10 +20,13 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
|
||||
public createdAtHash?: Hash;
|
||||
|
||||
constructor (registry: Registry, value?: AnyU8a) {
|
||||
readonly #initialU8aLength?: number;
|
||||
|
||||
constructor (registry: Registry, value?: AnyU8a, initialU8aLength?: number) {
|
||||
super(u8aToU8a(value));
|
||||
|
||||
this.registry = registry;
|
||||
this.#initialU8aLength = initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -33,6 +36,13 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
return this.length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
||||
*/
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return this.#initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
|
||||
@@ -11,13 +11,13 @@ import { compareMap, decodeU8a, mapToTypeMap } from './utils';
|
||||
type TypesDef<T = Codec> = Record<string, string | Constructor<T>>;
|
||||
|
||||
/** @internal */
|
||||
function decodeStructFromObject <T> (registry: Registry, Types: ConstructorDef, value: any, jsonMap: Map<any, string>): T {
|
||||
let jsonObj: Record<string, any> | undefined;
|
||||
function decodeStructFromObject (registry: Registry, Types: ConstructorDef, value: any, jsonMap: Map<any, string>): Record<string, Codec> {
|
||||
let jsonObj: Record<string, unknown> | undefined;
|
||||
const inputKeys = Object.keys(Types);
|
||||
|
||||
assert(!Array.isArray(value) || value.length === inputKeys.length, () => `Struct: Unable to map ${stringify(value)} array to object with known keys ${inputKeys.join(', ')}`);
|
||||
|
||||
return inputKeys.reduce((raw, key, index): T => {
|
||||
return inputKeys.reduce<Record<string, Codec>>((raw, key, index) => {
|
||||
// The key in the JSON can be snake_case (or other cases), but in our
|
||||
// Types, result or any other maps, it's camelCase
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
@@ -79,7 +79,7 @@ function decodeStructFromObject <T> (registry: Registry, Types: ConstructorDef,
|
||||
}
|
||||
|
||||
return raw;
|
||||
}, {} as T);
|
||||
}, {});
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -98,25 +98,26 @@ function decodeStructFromObject <T> (registry: Registry, Types: ConstructorDef,
|
||||
* @param jsonMap
|
||||
* @internal
|
||||
*/
|
||||
function decodeStruct <T> (registry: Registry, Types: ConstructorDef, value: unknown, jsonMap: Map<any, string>): T {
|
||||
function decodeStruct (registry: Registry, Types: ConstructorDef, value: unknown, jsonMap: Map<any, string>): [Record<string, Codec>, number] {
|
||||
if (isHex(value)) {
|
||||
return decodeStruct(registry, Types, hexToU8a(value), jsonMap);
|
||||
} else if (isU8a(value)) {
|
||||
const keys = Object.keys(Types);
|
||||
const values = decodeU8a(registry, value, Object.values(Types), keys);
|
||||
const [values, decodedLength] = decodeU8a(registry, value, Object.values(Types), keys);
|
||||
|
||||
// Transform array of values to {key: value} mapping
|
||||
return keys.reduce((raw, key, index): T => {
|
||||
// TS2322: Type 'Codec' is not assignable to type 'T[keyof S]'.
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
(raw as any)[key] = values[index];
|
||||
return [
|
||||
keys.reduce<Record<string, Codec>>((raw, key, index) => {
|
||||
raw[key] = values[index];
|
||||
|
||||
return raw;
|
||||
}, {} as T);
|
||||
return raw;
|
||||
}, {}),
|
||||
decodedLength
|
||||
];
|
||||
}
|
||||
|
||||
// We assume from here that value is a JS object (Array, Map, Object)
|
||||
return decodeStructFromObject(registry, Types, value || {}, jsonMap);
|
||||
return [decodeStructFromObject(registry, Types, value || {}, jsonMap), 0];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -140,15 +141,20 @@ export class Struct<
|
||||
|
||||
public createdAtHash?: Hash;
|
||||
|
||||
readonly #initialU8aLength?: number;
|
||||
|
||||
readonly #jsonMap: Map<keyof S, string>;
|
||||
|
||||
readonly #Types: ConstructorDef;
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
|
||||
constructor (registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | string | null, jsonMap: Map<keyof S, string> = new Map()) {
|
||||
super(Object.entries(decodeStruct(registry, mapToTypeMap(registry, Types), value, jsonMap)));
|
||||
const [decoded, decodedLength] = decodeStruct(registry, mapToTypeMap(registry, Types), value, jsonMap);
|
||||
|
||||
super(Object.entries(decoded) as [keyof S, Codec][]);
|
||||
|
||||
this.registry = registry;
|
||||
this.#initialU8aLength = decodedLength;
|
||||
this.#jsonMap = jsonMap;
|
||||
this.#Types = mapToTypeMap(registry, Types);
|
||||
}
|
||||
@@ -217,11 +223,14 @@ export class Struct<
|
||||
* @description The length of the value when encoded as a Uint8Array
|
||||
*/
|
||||
public get encodedLength (): number {
|
||||
return this.toArray().reduce((length, entry): number => {
|
||||
length += entry.encodedLength;
|
||||
return this.toArray().reduce((length, e) => length + e.encodedLength, 0);
|
||||
}
|
||||
|
||||
return length;
|
||||
}, 0);
|
||||
/**
|
||||
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
||||
*/
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return this.#initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -21,7 +21,7 @@ type TupleTypes = TupleType[] | {
|
||||
};
|
||||
|
||||
/** @internal */
|
||||
function decodeTuple (registry: Registry, _Types: TupleConstructors, value?: AnyTupleValue): Codec[] {
|
||||
function decodeTuple (registry: Registry, _Types: TupleConstructors, value?: AnyTupleValue): [Codec[], number] {
|
||||
if (isU8a(value) || isHex(value)) {
|
||||
return decodeU8a(registry, u8aToU8a(value), _Types);
|
||||
}
|
||||
@@ -30,19 +30,22 @@ function decodeTuple (registry: Registry, _Types: TupleConstructors, value?: Any
|
||||
? _Types
|
||||
: Object.values(_Types);
|
||||
|
||||
return Types.map((Type, index): Codec => {
|
||||
try {
|
||||
const entry = value?.[index];
|
||||
return [
|
||||
Types.map((Type, index): Codec => {
|
||||
try {
|
||||
const entry = value?.[index];
|
||||
|
||||
if (entry instanceof Type) {
|
||||
return entry;
|
||||
if (entry instanceof Type) {
|
||||
return entry;
|
||||
}
|
||||
|
||||
return new Type(registry, entry);
|
||||
} catch (error) {
|
||||
throw new Error(`Tuple: failed on ${index}:: ${(error as Error).message}`);
|
||||
}
|
||||
|
||||
return new Type(registry, entry);
|
||||
} catch (error) {
|
||||
throw new Error(`Tuple: failed on ${index}:: ${(error as Error).message}`);
|
||||
}
|
||||
});
|
||||
}),
|
||||
0
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,7 +55,7 @@ function decodeTuple (registry: Registry, _Types: TupleConstructors, value?: Any
|
||||
* own type. It extends the base JS `Array` object.
|
||||
*/
|
||||
export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||
private _Types: TupleConstructors;
|
||||
#Types: TupleConstructors;
|
||||
|
||||
constructor (registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue) {
|
||||
const Clazzes = Array.isArray(Types)
|
||||
@@ -60,10 +63,11 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||
: isFunction(Types) || isString(Types)
|
||||
? [typeToConstructor(registry, Types)]
|
||||
: mapToTypeMap(registry, Types);
|
||||
const [values, decodedLength] = decodeTuple(registry, Clazzes, value);
|
||||
|
||||
super(registry, decodeTuple(registry, Clazzes, value));
|
||||
super(registry, values, decodedLength);
|
||||
|
||||
this._Types = Clazzes;
|
||||
this.#Types = Clazzes;
|
||||
}
|
||||
|
||||
public static with (Types: TupleTypes | TupleType): Constructor<Tuple> {
|
||||
@@ -78,16 +82,16 @@ 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 {
|
||||
return this.reduce((total, entry) => total + entry.encodedLength, 0);
|
||||
return this.reduce((total, e) => total + e.encodedLength, 0);
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The types definition of the tuple
|
||||
*/
|
||||
public get Types (): string[] {
|
||||
return Array.isArray(this._Types)
|
||||
? this._Types.map((Type) => new Type(this.registry).toRawType())
|
||||
: Object.keys(this._Types);
|
||||
return Array.isArray(this.#Types)
|
||||
? this.#Types.map((T) => new T(this.registry).toRawType())
|
||||
: Object.keys(this.#Types);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -95,11 +99,11 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||
*/
|
||||
public toRawType (): string {
|
||||
const types = (
|
||||
Array.isArray(this._Types)
|
||||
? this._Types
|
||||
: Object.values(this._Types)
|
||||
).map((Type) =>
|
||||
this.registry.getClassName(Type) || new Type(this.registry).toRawType()
|
||||
Array.isArray(this.#Types)
|
||||
? this.#Types
|
||||
: Object.values(this.#Types)
|
||||
).map((T) =>
|
||||
this.registry.getClassName(T) || new T(this.registry).toRawType()
|
||||
);
|
||||
|
||||
return `(${types.join(',')})`;
|
||||
@@ -119,8 +123,8 @@ export class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
||||
*/
|
||||
public override toU8a (isBare?: boolean): Uint8Array {
|
||||
return u8aConcat(
|
||||
...this.map((entry): Uint8Array =>
|
||||
entry.toU8a(isBare)
|
||||
...this.map((e): Uint8Array =>
|
||||
e.toU8a(isBare)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import { assert, isString, u8aToU8a } from '@polkadot/util';
|
||||
import { Raw } from './Raw';
|
||||
|
||||
/** @internal */
|
||||
function decodeU8aFixed (value: AnyU8a, bitLength: U8aBitLength): AnyU8a {
|
||||
function decodeU8aFixed (value: AnyU8a, bitLength: U8aBitLength): [AnyU8a, number] {
|
||||
if (Array.isArray(value) || isString(value)) {
|
||||
return decodeU8aFixed(u8aToU8a(value), bitLength);
|
||||
}
|
||||
@@ -18,12 +18,12 @@ function decodeU8aFixed (value: AnyU8a, bitLength: U8aBitLength): AnyU8a {
|
||||
const u8a = new Uint8Array(byteLength);
|
||||
|
||||
if (!value || !value.length) {
|
||||
return u8a;
|
||||
return [u8a, 0];
|
||||
}
|
||||
|
||||
assert(value.length >= byteLength, () => `Expected at least ${byteLength} bytes (${bitLength} bits), found ${value.length} bytes`);
|
||||
|
||||
return value.subarray(0, byteLength);
|
||||
return [value.subarray(0, byteLength), byteLength];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,7 +34,9 @@ function decodeU8aFixed (value: AnyU8a, bitLength: U8aBitLength): AnyU8a {
|
||||
*/
|
||||
export class U8aFixed extends Raw {
|
||||
constructor (registry: Registry, value: AnyU8a = new Uint8Array(), bitLength: U8aBitLength = 256) {
|
||||
super(registry, decodeU8aFixed(value, bitLength));
|
||||
const [u8a, decodedLength] = decodeU8aFixed(value, bitLength);
|
||||
|
||||
super(registry, u8a, decodedLength);
|
||||
}
|
||||
|
||||
public static with (bitLength: U8aBitLength, typeName?: string): Constructor<U8aFixed> {
|
||||
|
||||
@@ -20,31 +20,35 @@ const l = logger('Vec');
|
||||
* specific encoding/decoding on top of the base type.
|
||||
*/
|
||||
export class Vec<T extends Codec> extends AbstractArray<T> {
|
||||
private _Type: Constructor<T>;
|
||||
#Type: Constructor<T>;
|
||||
|
||||
constructor (registry: Registry, Type: Constructor<T> | string, value: Vec<Codec> | Uint8Array | string | unknown[] = []) {
|
||||
const Clazz = typeToConstructor<T>(registry, Type);
|
||||
const [values, decodedLength] = Vec.decodeVec(registry, Clazz, value);
|
||||
|
||||
super(registry, Vec.decodeVec(registry, Clazz, value));
|
||||
super(registry, values, decodedLength);
|
||||
|
||||
this._Type = Clazz;
|
||||
this.#Type = Clazz;
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
public static decodeVec<T extends Codec> (registry: Registry, Type: Constructor<T>, value: Vec<Codec> | Uint8Array | string | unknown[]): T[] {
|
||||
public static decodeVec<T extends Codec> (registry: Registry, Type: Constructor<T>, value: Vec<Codec> | Uint8Array | string | unknown[]): [T[], number, number] {
|
||||
if (Array.isArray(value)) {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return
|
||||
return (value as unknown[]).map((entry: unknown, index: number): T => {
|
||||
try {
|
||||
return entry instanceof Type
|
||||
? entry
|
||||
: new Type(registry, entry);
|
||||
} catch (error) {
|
||||
l.error(`Unable to decode on index ${index}`, (error as Error).message);
|
||||
return [
|
||||
(value as unknown[]).map((entry: unknown, index: number): T => {
|
||||
try {
|
||||
return entry instanceof Type
|
||||
? entry
|
||||
: new Type(registry, entry);
|
||||
} catch (error) {
|
||||
l.error(`Unable to decode on index ${index}`, (error as Error).message);
|
||||
|
||||
throw error;
|
||||
}
|
||||
});
|
||||
throw error;
|
||||
}
|
||||
}),
|
||||
0, 0
|
||||
];
|
||||
}
|
||||
|
||||
const u8a = u8aToU8a(value);
|
||||
@@ -52,7 +56,9 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
|
||||
|
||||
assert(length.lten(MAX_LENGTH), () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
|
||||
return decodeU8a(registry, u8a.subarray(offset), new Array(length.toNumber()).fill(Type));
|
||||
const [decoded, decodedLength] = decodeU8a(registry, u8a.subarray(offset), new Array(length.toNumber()).fill(Type));
|
||||
|
||||
return [decoded as T[], decodedLength + offset, decodedLength];
|
||||
}
|
||||
|
||||
public static with<O extends Codec> (Type: Constructor<O> | string): Constructor<Vec<O>> {
|
||||
@@ -67,7 +73,7 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
|
||||
* @description The type for the items
|
||||
*/
|
||||
public get Type (): string {
|
||||
return this._Type.name;
|
||||
return this.#Type.name;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -75,9 +81,9 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
|
||||
*/
|
||||
public override indexOf (_other?: unknown): number {
|
||||
// convert type first, this removes overhead from the eq
|
||||
const other = _other instanceof this._Type
|
||||
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])) {
|
||||
@@ -92,6 +98,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()}>`;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -10,8 +10,8 @@ import { typeToConstructor } from './utils';
|
||||
import { Vec } from './Vec';
|
||||
|
||||
/** @internal */
|
||||
function decodeVecFixed<T extends Codec> (registry: Registry, Type: Constructor<T>, allocLength: number, value: VecFixed<any> | Uint8Array | string | any[]): T[] {
|
||||
const values = Vec.decodeVec(
|
||||
function decodeVecFixed<T extends Codec> (registry: Registry, Type: Constructor<T>, allocLength: number, value: VecFixed<any> | Uint8Array | string | any[]): [T[], number] {
|
||||
const [values,, decodedLengthNoOffset] = Vec.decodeVec(
|
||||
registry,
|
||||
Type,
|
||||
isU8a(value) || isHex(value)
|
||||
@@ -25,7 +25,7 @@ function decodeVecFixed<T extends Codec> (registry: Registry, Type: Constructor<
|
||||
|
||||
assert(values.length === allocLength, () => `Expected a length of exactly ${allocLength} entries`);
|
||||
|
||||
return values;
|
||||
return [values, decodedLengthNoOffset];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -34,14 +34,15 @@ function decodeVecFixed<T extends Codec> (registry: Registry, Type: Constructor<
|
||||
* This manages codec arrays of a fixed length
|
||||
*/
|
||||
export class VecFixed<T extends Codec> extends AbstractArray<T> {
|
||||
private _Type: Constructor<T>;
|
||||
#Type: Constructor<T>;
|
||||
|
||||
constructor (registry: Registry, Type: Constructor<T> | string, length: number, value: VecFixed<any> | Uint8Array | string | any[] = [] as any[]) {
|
||||
const Clazz = typeToConstructor<T>(registry, Type);
|
||||
const [values, decodedLength] = decodeVecFixed(registry, Clazz, length, value);
|
||||
|
||||
super(registry, decodeVecFixed(registry, Clazz, length, value));
|
||||
super(registry, values, decodedLength);
|
||||
|
||||
this._Type = Clazz;
|
||||
this.#Type = Clazz;
|
||||
}
|
||||
|
||||
public static with<O extends Codec> (Type: Constructor<O> | string, length: number): Constructor<VecFixed<O>> {
|
||||
@@ -56,20 +57,20 @@ 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 {
|
||||
return this.reduce((total, entry) => total + entry.encodedLength, 0);
|
||||
return this.reduce((total, e) => total + e.encodedLength, 0);
|
||||
}
|
||||
|
||||
public override toU8a (): Uint8Array {
|
||||
// we override, we don't add the length prefix for ourselves, and at the same time we
|
||||
// ignore isBare on entries, since they should be properly encoded at all times
|
||||
const encoded = this.map((entry) => entry.toU8a());
|
||||
const encoded = this.map((e) => e.toU8a());
|
||||
|
||||
return encoded.length
|
||||
? u8aConcat(...encoded)
|
||||
|
||||
@@ -10,10 +10,10 @@ import { hasEq } from './util';
|
||||
export function compareArray (a: unknown[], b?: unknown): boolean {
|
||||
if (Array.isArray(b)) {
|
||||
return (a.length === b.length) && isUndefined(
|
||||
a.find((value, index): boolean =>
|
||||
hasEq(value)
|
||||
? !value.eq(b[index])
|
||||
: value !== b[index]
|
||||
a.find((v, index): boolean =>
|
||||
hasEq(v)
|
||||
? !v.eq(b[index])
|
||||
: v !== b[index]
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
@@ -19,9 +19,9 @@ function notEntry (value: unknown): boolean {
|
||||
|
||||
function compareMapArray (a: Map<unknown, unknown>, b: [unknown, unknown][]): boolean {
|
||||
// equal number of entries and each entry in the array should match
|
||||
return (a.size === b.length) && !b.some((entry) =>
|
||||
notEntry(entry) ||
|
||||
hasMismatch(a.get(entry[0]), entry[1])
|
||||
return (a.size === b.length) && !b.some((e) =>
|
||||
notEntry(e) ||
|
||||
hasMismatch(a.get(e[0]), e[1])
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ import { isObject } from '@polkadot/util';
|
||||
|
||||
function compareSetArray (a: Set<unknown>, b: any[]): boolean {
|
||||
// equal number of entries and each entry in the array should match
|
||||
return (a.size === b.length) && !b.some((entry): boolean => !a.has(entry));
|
||||
return (a.size === b.length) && !b.some((e) => !a.has(e));
|
||||
}
|
||||
|
||||
// NOTE These are used internally and when comparing objects, expects that
|
||||
|
||||
@@ -13,21 +13,23 @@ import { u8aToHex } from '@polkadot/util';
|
||||
* @param result - The result array (will be returned with values pushed)
|
||||
* @param types - The array of Constructor to decode the U8a against.
|
||||
*/
|
||||
export function decodeU8a <T extends Codec = Codec> (registry: Registry, u8a: Uint8Array, _types: Constructor[] | { [index: string]: Constructor }, _keys?: string[]): T[] {
|
||||
export function decodeU8a <T extends Codec = Codec> (registry: Registry, u8a: Uint8Array, _types: Constructor[] | { [index: string]: Constructor }, _keys?: string[]): [T[], number] {
|
||||
const result: T[] = [];
|
||||
const [types, keys]: [Constructor[], string[]] = Array.isArray(_types)
|
||||
? [_types, _keys || []]
|
||||
: [Object.values(_types), Object.keys(_types)];
|
||||
let offset = 0;
|
||||
let decodedLength = 0;
|
||||
|
||||
for (let i = 0; i < types.length; i++) {
|
||||
const Type = types[i];
|
||||
|
||||
try {
|
||||
const value = new Type(registry, u8a.subarray(offset));
|
||||
const value = new Type(registry, u8a.subarray(decodedLength));
|
||||
|
||||
result.push(value as T);
|
||||
offset += value.encodedLength;
|
||||
|
||||
// use the initial length if available, this bypasses an extra serialization
|
||||
decodedLength += value.initialU8aLength || value.encodedLength;
|
||||
} catch (error) {
|
||||
let rawType: string;
|
||||
|
||||
@@ -37,9 +39,9 @@ export function decodeU8a <T extends Codec = Codec> (registry: Registry, u8a: Ui
|
||||
rawType = '';
|
||||
}
|
||||
|
||||
throw new Error(`decodeU8a: failed at ${u8aToHex(u8a.subarray(offset).slice(0, 8))}… on ${keys[i] ? `${keys[i]}` : ''}${rawType ? `: ${rawType}` : ''}:: ${(error as Error).message}`);
|
||||
throw new Error(`decodeU8a: failed at ${u8aToHex(u8a.subarray(decodedLength).slice(0, 8))}… on ${keys[i] ? `${keys[i]}` : ''}${rawType ? `: ${rawType}` : ''}:: ${(error as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
return result;
|
||||
return [result, decodedLength];
|
||||
}
|
||||
|
||||
@@ -11,9 +11,9 @@ import { Raw } from '../codec/Raw';
|
||||
const MAX_LENGTH = 10 * 1024 * 1024;
|
||||
|
||||
/** @internal */
|
||||
function decodeBytesU8a (value: Uint8Array): Uint8Array {
|
||||
function decodeBytesU8a (value: Uint8Array): [Uint8Array, number] {
|
||||
if (!value.length) {
|
||||
return new Uint8Array();
|
||||
return [new Uint8Array(), 0];
|
||||
}
|
||||
|
||||
// handle all other Uint8Array inputs, these do have a length prefix
|
||||
@@ -23,20 +23,20 @@ function decodeBytesU8a (value: Uint8Array): Uint8Array {
|
||||
assert(length.lten(MAX_LENGTH), () => `Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
assert(total <= value.length, () => `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
||||
|
||||
return value.subarray(offset, total);
|
||||
return [value.subarray(offset, total), total];
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function decodeBytes (value?: AnyU8a): Uint8Array | undefined {
|
||||
function decodeBytes (value?: AnyU8a): [Uint8Array | undefined, number] {
|
||||
if (Array.isArray(value) || isString(value)) {
|
||||
return u8aToU8a(value);
|
||||
return [u8aToU8a(value), 0];
|
||||
} else if (!(value instanceof Raw) && isU8a(value)) {
|
||||
// We are ensuring we are not a Raw instance. In the case of a Raw we already have gotten
|
||||
// rid of the length, i.e. new Bytes(new Bytes(...)) will work as expected
|
||||
return decodeBytesU8a(value);
|
||||
}
|
||||
|
||||
return value;
|
||||
return [value, 0];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -48,7 +48,9 @@ function decodeBytes (value?: AnyU8a): Uint8Array | undefined {
|
||||
*/
|
||||
export class Bytes extends Raw {
|
||||
constructor (registry: Registry, value?: AnyU8a) {
|
||||
super(registry, decodeBytes(value));
|
||||
const [u8a, decodedLength] = decodeBytes(value);
|
||||
|
||||
super(registry, u8a, decodedLength);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -11,18 +11,18 @@ import { Raw } from '../codec/Raw';
|
||||
const MAX_LENGTH = 128 * 1024;
|
||||
|
||||
/** @internal */
|
||||
function decodeText (value?: null | Text | string | AnyU8a | { toString: () => string }): string {
|
||||
function decodeText (value?: null | Text | string | AnyU8a | { toString: () => string }): [string, number] {
|
||||
if (isHex(value)) {
|
||||
return u8aToString(hexToU8a(value));
|
||||
return [u8aToString(hexToU8a(value)), 0];
|
||||
} else if (value instanceof Uint8Array) {
|
||||
if (!value.length) {
|
||||
return '';
|
||||
return ['', 0];
|
||||
}
|
||||
|
||||
// for Raw, the internal buffer does not have an internal length
|
||||
// (the same applies in e.g. Bytes, where length is added at encoding-time)
|
||||
if (value instanceof Raw) {
|
||||
return u8aToString(value);
|
||||
return [u8aToString(value), 0];
|
||||
}
|
||||
|
||||
const [offset, length] = compactFromU8a(value);
|
||||
@@ -31,10 +31,10 @@ function decodeText (value?: null | Text | string | AnyU8a | { toString: () => s
|
||||
assert(length.lten(MAX_LENGTH), () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
assert(total <= value.length, () => `Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
||||
|
||||
return u8aToString(value.subarray(offset, total));
|
||||
return [u8aToString(value.subarray(offset, total)), total];
|
||||
}
|
||||
|
||||
return value ? value.toString() : '';
|
||||
return [value ? value.toString() : '', 0];
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -52,12 +52,17 @@ export class Text extends String implements Codec {
|
||||
|
||||
public createdAtHash?: Hash;
|
||||
|
||||
readonly #initialU8aLength?: number;
|
||||
|
||||
#override: string | null = null;
|
||||
|
||||
constructor (registry: Registry, value?: null | Text | string | AnyU8a | { toString: () => string }) {
|
||||
super(decodeText(value));
|
||||
const [str, decodedLength] = decodeText(value);
|
||||
|
||||
super(str);
|
||||
|
||||
this.registry = registry;
|
||||
this.#initialU8aLength = decodedLength;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -67,6 +72,13 @@ export class Text extends String implements Codec {
|
||||
return this.toU8a().length;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
||||
*/
|
||||
public get initialU8aLength (): number | undefined {
|
||||
return this.#initialU8aLength;
|
||||
}
|
||||
|
||||
/**
|
||||
* @description returns a hash of the contents
|
||||
*/
|
||||
|
||||
@@ -48,6 +48,11 @@ export interface Codec {
|
||||
*/
|
||||
readonly hash: Hash;
|
||||
|
||||
/**
|
||||
* @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
|
||||
*/
|
||||
readonly initialU8aLength?: number;
|
||||
|
||||
/**
|
||||
* @description Checks if the value is an empty value
|
||||
*/
|
||||
|
||||
Reference in New Issue
Block a user