Optimize internal u8a decoding (#4131)
* Optimize internal u8a decoding * linting * tsc fixes & HexString usage * yet more... it passes now (well... tsc & lint)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { AnyJson, Codec, IVec, Registry } from '../types';
|
||||
|
||||
@@ -89,7 +90,7 @@ export abstract class AbstractArray<T extends Codec> extends Array<T> implements
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public toHex (): string {
|
||||
public toHex (): HexString {
|
||||
return u8aToHex(this.toU8a());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { AnyNumber, INumber, Registry } from '../types';
|
||||
import type { UIntBitLength } from './types';
|
||||
@@ -161,7 +162,7 @@ export abstract class AbstractInt extends BN implements INumber {
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public toHex (isLe = false): string {
|
||||
public toHex (isLe = false): HexString {
|
||||
// For display/JSON, this is BE, for compare, use isLe
|
||||
return bnToHex(this, {
|
||||
bitLength: this.bitLength(),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { AnyJson, Codec, Constructor, ISet, Registry } from '../types';
|
||||
|
||||
@@ -148,7 +149,7 @@ export class BTreeSet<V extends Codec = Codec> extends Set<V> implements ISet<V>
|
||||
/**
|
||||
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
||||
*/
|
||||
public toHex (): string {
|
||||
public toHex (): HexString {
|
||||
return u8aToHex(this.toU8a());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { AnyJson, BareOpts, Codec, Registry } from '../types';
|
||||
|
||||
@@ -61,7 +62,7 @@ export abstract class Base<T extends Codec> implements Codec {
|
||||
/**
|
||||
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
||||
*/
|
||||
public toHex (isLe?: boolean): string {
|
||||
public toHex (isLe?: boolean): HexString {
|
||||
return this._raw.toHex(isLe);
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces';
|
||||
import type { AnyJson, AnyNumber, Constructor, ICompact, INumber, Registry } from '../types';
|
||||
|
||||
@@ -122,7 +123,7 @@ export class Compact<T extends INumber> implements ICompact<T> {
|
||||
/**
|
||||
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
||||
*/
|
||||
public toHex (isLe?: boolean): string {
|
||||
public toHex (isLe?: boolean): HexString {
|
||||
return this.#raw.toHex(isLe);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { AnyNumber, INumber, Registry } from '../types';
|
||||
import type { UIntBitLength } from './types';
|
||||
@@ -96,7 +97,7 @@ export class CodecDate extends Date implements INumber {
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public toHex (isLe = false): string {
|
||||
public toHex (isLe = false): HexString {
|
||||
return bnToHex(this.toBn(), {
|
||||
bitLength: BITLENGTH,
|
||||
isLe,
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { AnyJson, Codec, Constructor, Registry } from '../types';
|
||||
|
||||
@@ -56,7 +57,7 @@ export class DoNotConstruct implements Codec {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
toHex (): string {
|
||||
toHex (): HexString {
|
||||
throw this.#neverError;
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces';
|
||||
import type { AnyJson, Codec, Constructor, IEnum, Registry } from '../types';
|
||||
|
||||
@@ -337,7 +338,7 @@ export class Enum implements IEnum {
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public toHex (): string {
|
||||
public toHex (): HexString {
|
||||
return u8aToHex(this.toU8a());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { AnyJson, Codec, Registry } from '../types';
|
||||
|
||||
@@ -73,7 +74,7 @@ export class Json extends Map<string, any> implements Codec {
|
||||
/**
|
||||
* @description Unimplemented, will throw
|
||||
*/
|
||||
public toHex (): string {
|
||||
public toHex (): HexString {
|
||||
throw new Error('Unimplemented');
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Codec, Constructor, Registry } from '../types';
|
||||
|
||||
import { Option } from './Option';
|
||||
@@ -22,7 +23,7 @@ export class Linkage<T extends Codec> extends Struct {
|
||||
previous: Option.with(Type),
|
||||
// eslint-disable-next-line sort-keys
|
||||
next: Option.with(Type)
|
||||
}, value as string);
|
||||
}, value as HexString);
|
||||
}
|
||||
|
||||
public static withKey<O extends Codec> (Type: Constructor | string): Constructor<Linkage<O>> {
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { AnyJson, Codec, Constructor, IMap, Registry } from '../types';
|
||||
|
||||
@@ -162,7 +163,7 @@ export class CodecMap<K extends Codec = Codec, V extends Codec = Codec> extends
|
||||
/**
|
||||
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
||||
*/
|
||||
public toHex (): string {
|
||||
public toHex (): HexString {
|
||||
return u8aToHex(this.toU8a());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces';
|
||||
import type { AnyJson, Codec, Constructor, IOption, Registry } from '../types';
|
||||
|
||||
@@ -140,7 +141,7 @@ export class Option<T extends Codec> implements IOption<T> {
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public toHex (): string {
|
||||
public toHex (): HexString {
|
||||
// This attempts to align with the JSON encoding - actually in this case
|
||||
// the isSome value is correct, however the `isNone` may be problematic
|
||||
return this.isNone
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { AnyJson, AnyU8a, IU8a, Registry } from '../types';
|
||||
|
||||
@@ -121,7 +122,7 @@ export class Raw extends Uint8Array implements IU8a {
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public toHex (): string {
|
||||
public toHex (): HexString {
|
||||
return u8aToHex(this);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { Constructor, ISet, Registry } from '../types';
|
||||
|
||||
@@ -177,7 +178,7 @@ export class CodecSet extends Set<string> implements ISet<string> {
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public toHex (): string {
|
||||
public toHex (): HexString {
|
||||
return u8aToHex(this.toU8a());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { AnyJson, BareOpts, Codec, Constructor, ConstructorDef, IStruct, Registry } from '../types';
|
||||
|
||||
@@ -10,38 +11,33 @@ import { compareMap, decodeU8a, mapToTypeMap } from './utils';
|
||||
|
||||
type TypesDef<T = Codec> = Record<string, string | Constructor<T>>;
|
||||
|
||||
function newEntry (registry: Registry, Type: Constructor, key: string, value: unknown): [string, Codec] {
|
||||
return [
|
||||
key,
|
||||
value instanceof Type
|
||||
? value
|
||||
: new Type(registry, value)
|
||||
];
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
function decodeStructFromObject (registry: Registry, Types: ConstructorDef, value: any, jsonMap: Map<string, string>): Iterable<[string, Codec]> {
|
||||
let jsonObj: Record<string, unknown> | undefined;
|
||||
const inputKeys = Object.keys(Types);
|
||||
const typeofArray = Array.isArray(value);
|
||||
const typeofMap = value instanceof Map;
|
||||
|
||||
assert(!Array.isArray(value) || value.length === inputKeys.length, () => `Struct: Unable to map ${stringify(value)} array to object with known keys ${inputKeys.join(', ')}`);
|
||||
assert(typeofArray || typeofMap || isObject(value), () => `Struct: Cannot decode value ${stringify(value)} (typeof ${typeof value}), expected an input object, map or array`);
|
||||
assert(!typeofArray || value.length === inputKeys.length, () => `Struct: Unable to map ${stringify(value)} array to object with known keys ${inputKeys.join(', ')}`);
|
||||
|
||||
return inputKeys.reduce<[string, Codec][]>((raw, key, index) => {
|
||||
const jsonKey = jsonMap.get(key) || key;
|
||||
const Type = Types[key];
|
||||
let assign: unknown;
|
||||
|
||||
try {
|
||||
if (Array.isArray(value)) {
|
||||
raw.push(newEntry(registry, Type, key, value[index]));
|
||||
} else if (value instanceof Map) {
|
||||
raw.push(newEntry(registry, Type, key, jsonKey && (value as Map<string, unknown>).get(jsonKey)));
|
||||
} else if (isObject(value)) {
|
||||
let assign = jsonKey && value[jsonKey] as unknown;
|
||||
if (typeofArray) {
|
||||
assign = value[index] as unknown;
|
||||
} else if (typeofMap) {
|
||||
assign = jsonKey && value.get(jsonKey);
|
||||
} else {
|
||||
assign = jsonKey && value[jsonKey] as unknown;
|
||||
|
||||
if (isUndefined(assign)) {
|
||||
if (isUndefined(jsonObj)) {
|
||||
jsonObj = Object.entries(value).reduce((all: Record<string, unknown>, [key, value]): Record<string, unknown> => {
|
||||
all[stringCamelCase(key)] = value;
|
||||
jsonObj = Object.entries(value).reduce((all: Record<string, unknown>, [k, v]): Record<string, unknown> => {
|
||||
all[stringCamelCase(k)] = v;
|
||||
|
||||
return all;
|
||||
}, {});
|
||||
@@ -49,11 +45,14 @@ function decodeStructFromObject (registry: Registry, Types: ConstructorDef, valu
|
||||
|
||||
assign = jsonKey && jsonObj[jsonKey];
|
||||
}
|
||||
|
||||
raw.push(newEntry(registry, Type, key, assign));
|
||||
} else {
|
||||
throw new Error(`Cannot decode value ${stringify(value)} (typeof ${typeof value}), expected an input object with known keys`);
|
||||
}
|
||||
|
||||
raw.push([
|
||||
key,
|
||||
assign instanceof Type
|
||||
? assign
|
||||
: new Type(registry, assign)
|
||||
]);
|
||||
} catch (error) {
|
||||
let type = Type.name;
|
||||
|
||||
@@ -90,18 +89,7 @@ function decodeStruct (registry: Registry, Types: ConstructorDef, value: unknown
|
||||
if (isHex(value)) {
|
||||
return decodeStruct(registry, Types, hexToU8a(value), jsonMap);
|
||||
} else if (isU8a(value)) {
|
||||
const keys = Object.keys(Types);
|
||||
const [values, decodedLength] = decodeU8a(registry, value, Object.values(Types), keys);
|
||||
|
||||
// Transform array of values to {key: value} mapping
|
||||
return [
|
||||
keys.reduce<[string, Codec][]>((raw, key, index) => {
|
||||
raw.push([key, values[index]]);
|
||||
|
||||
return raw;
|
||||
}, []),
|
||||
decodedLength
|
||||
];
|
||||
return decodeU8a<Codec, [string, Codec]>(registry, value, Types, 0, (k, v) => [k, v]);
|
||||
} else if (value instanceof Struct) {
|
||||
return [value as Iterable<[string, Codec]>, 0];
|
||||
}
|
||||
@@ -138,7 +126,7 @@ export class Struct<
|
||||
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<string, string> = new Map()) {
|
||||
constructor (registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap = new Map<string, string>()) {
|
||||
const [decoded, decodedLength] = decodeStruct(registry, mapToTypeMap(registry, Types), value, jsonMap);
|
||||
|
||||
super(decoded);
|
||||
@@ -152,7 +140,7 @@ export class Struct<
|
||||
public static with<S extends TypesDef> (Types: S, jsonMap?: Map<string, string>): Constructor<Struct<S>> {
|
||||
return class extends Struct<S> {
|
||||
constructor (registry: Registry, value?: unknown) {
|
||||
super(registry, Types, value as string, jsonMap);
|
||||
super(registry, Types, value as HexString, jsonMap);
|
||||
|
||||
Object.keys(Types).forEach((key): void => {
|
||||
isUndefined(this[key as keyof this]) &&
|
||||
@@ -186,6 +174,7 @@ export class Struct<
|
||||
public get isEmpty (): boolean {
|
||||
const items = this.toArray();
|
||||
|
||||
// More code, but it _should_ be more performand that this.toArray().every((e) => e.isEmpty)
|
||||
for (let i = 0; i < items.length; i++) {
|
||||
if (!items[i].isEmpty) {
|
||||
return false;
|
||||
@@ -203,7 +192,7 @@ export class Struct<
|
||||
.entries(this.#Types))
|
||||
.reduce((result: E, [key, Type]): E => {
|
||||
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
||||
(result as any)[key] = new Type(this.registry).toRawType();
|
||||
(result as any)[key as keyof E] = new Type(this.registry).toRawType();
|
||||
|
||||
return result;
|
||||
}, {} as E);
|
||||
@@ -262,7 +251,7 @@ export class Struct<
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public toHex (): string {
|
||||
public toHex (): HexString {
|
||||
return u8aToHex(this.toU8a());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Codec, Constructor, Registry } from '../types';
|
||||
|
||||
import { assert, compactFromU8a, logger, u8aToU8a } from '@polkadot/util';
|
||||
@@ -22,7 +23,7 @@ const l = logger('Vec');
|
||||
export class Vec<T extends Codec> extends AbstractArray<T> {
|
||||
#Type: Constructor<T>;
|
||||
|
||||
constructor (registry: Registry, Type: Constructor<T> | string, value: Vec<Codec> | Uint8Array | string | unknown[] = []) {
|
||||
constructor (registry: Registry, Type: Constructor<T> | string, value: Uint8Array | HexString | unknown[] = []) {
|
||||
const Clazz = typeToConstructor<T>(registry, Type);
|
||||
const [values, decodedLength] = Vec.decodeVec(registry, Clazz, value);
|
||||
|
||||
@@ -32,11 +33,10 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
|
||||
}
|
||||
|
||||
/** @internal */
|
||||
public static decodeVec<T extends Codec> (registry: Registry, Type: Constructor<T>, value: Vec<Codec> | Uint8Array | string | unknown[]): [T[], number, number] {
|
||||
public static decodeVec<T extends Codec> (registry: Registry, Type: Constructor<T>, value: Uint8Array | HexString | unknown[], length = -1): [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 => {
|
||||
value.map((entry: unknown, index: number): T => {
|
||||
try {
|
||||
return entry instanceof Type
|
||||
? entry
|
||||
@@ -52,11 +52,18 @@ export class Vec<T extends Codec> extends AbstractArray<T> {
|
||||
}
|
||||
|
||||
const u8a = u8aToU8a(value);
|
||||
const [offset, length] = compactFromU8a(u8a);
|
||||
let offset = 0;
|
||||
|
||||
assert(length.lten(MAX_LENGTH), () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
if (length === -1) {
|
||||
const [_offset, _length] = compactFromU8a(u8a);
|
||||
|
||||
const [decoded, decodedLength] = decodeU8a(registry, u8a.subarray(offset), new Array(length.toNumber()).fill(Type));
|
||||
assert(_length.lten(MAX_LENGTH), () => `Vec length ${_length.toString()} exceeds ${MAX_LENGTH}`);
|
||||
|
||||
length = _length.toNumber();
|
||||
offset = _offset;
|
||||
}
|
||||
|
||||
const [decoded, decodedLength] = decodeU8a(registry, u8a.subarray(offset), Type, length);
|
||||
|
||||
return [decoded as T[], decodedLength + offset, decodedLength];
|
||||
}
|
||||
|
||||
@@ -1,29 +1,24 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Codec, Constructor, Registry } from '../types';
|
||||
|
||||
import { assert, compactToU8a, isHex, isU8a, u8aConcat } from '@polkadot/util';
|
||||
import { assert, u8aConcat } from '@polkadot/util';
|
||||
|
||||
import { AbstractArray } from './AbstractArray';
|
||||
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[], number] {
|
||||
const [values,, decodedLengthNoOffset] = Vec.decodeVec(
|
||||
registry,
|
||||
Type,
|
||||
isU8a(value) || isHex(value)
|
||||
? u8aConcat(compactToU8a(allocLength), value)
|
||||
: value
|
||||
);
|
||||
function decodeVecFixed<T extends Codec> (registry: Registry, Type: Constructor<T>, length: number, value: Uint8Array | HexString | unknown[]): [T[], number] {
|
||||
const [values,, decodedLengthNoOffset] = Vec.decodeVec(registry, Type, value, length);
|
||||
|
||||
while (values.length < allocLength) {
|
||||
while (values.length < length) {
|
||||
values.push(new Type(registry));
|
||||
}
|
||||
|
||||
assert(values.length === allocLength, () => `Expected a length of exactly ${allocLength} entries`);
|
||||
assert(values.length === length, () => `Expected a length of exactly ${length} entries`);
|
||||
|
||||
return [values, decodedLengthNoOffset];
|
||||
}
|
||||
@@ -36,7 +31,7 @@ function decodeVecFixed<T extends Codec> (registry: Registry, Type: Constructor<
|
||||
export class VecFixed<T extends Codec> extends AbstractArray<T> {
|
||||
#Type: Constructor<T>;
|
||||
|
||||
constructor (registry: Registry, Type: Constructor<T> | string, length: number, value: VecFixed<any> | Uint8Array | string | any[] = [] as any[]) {
|
||||
constructor (registry: Registry, Type: Constructor<T> | string, length: number, value: Uint8Array | HexString | unknown[] = [] as unknown[]) {
|
||||
const Clazz = typeToConstructor<T>(registry, Type);
|
||||
const [values, decodedLength] = decodeVecFixed(registry, Clazz, length, value);
|
||||
|
||||
|
||||
@@ -3,7 +3,11 @@
|
||||
|
||||
import type { Codec, Constructor, Registry } from '../../types';
|
||||
|
||||
import { u8aToHex } from '@polkadot/util';
|
||||
import { isFunction, u8aToHex } from '@polkadot/util';
|
||||
|
||||
function identityZip <T, E> (key: string, value: T): E {
|
||||
return value as unknown as E;
|
||||
}
|
||||
|
||||
/**
|
||||
* Given an u8a, and an array of Type constructors, decode the u8a against the
|
||||
@@ -13,33 +17,34 @@ 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[], number] {
|
||||
const result: T[] = [];
|
||||
const [types, keys]: [Constructor[], string[]] = Array.isArray(_types)
|
||||
? [_types, _keys || []]
|
||||
: [Object.values(_types), Object.keys(_types)];
|
||||
export function decodeU8a <T extends Codec = Codec, E = T> (registry: Registry, u8a: Uint8Array, types: Constructor | Constructor[] | { [index: string]: Constructor }, count?: number, zip: (key: string, value: T) => E = identityZip): [E[], number] {
|
||||
const [Type, Types, keys]: [Constructor | null, Constructor[], string[]] = isFunction(types)
|
||||
? [types, [], []]
|
||||
: Array.isArray(types)
|
||||
? [null, types, []]
|
||||
: [null, Object.values(types), Object.keys(types)];
|
||||
|
||||
count = count || Types.length;
|
||||
|
||||
const result = new Array<E>(count);
|
||||
let decodedLength = 0;
|
||||
|
||||
for (let i = 0; i < types.length; i++) {
|
||||
const Type = types[i];
|
||||
|
||||
for (let i = 0; i < count; i++) {
|
||||
try {
|
||||
const value = new Type(registry, u8a.subarray(decodedLength));
|
||||
const value = new (Type || Types[i])(registry, u8a.subarray(decodedLength));
|
||||
|
||||
result.push(value as T);
|
||||
|
||||
// use the initial length if available, this bypasses an extra serialization
|
||||
result[i] = zip(keys[i], value as T);
|
||||
decodedLength += value.initialU8aLength || value.encodedLength;
|
||||
} catch (error) {
|
||||
let rawType: string;
|
||||
|
||||
try {
|
||||
rawType = new Type(registry).toRawType();
|
||||
rawType = new (Type || Types[i])(registry).toRawType();
|
||||
} catch {
|
||||
rawType = '';
|
||||
}
|
||||
|
||||
throw new Error(`decodeU8a: failed at ${u8aToHex(u8a.subarray(decodedLength).slice(0, 8))}… on ${keys[i] ? `${keys[i]}` : ''}${rawType ? `: ${rawType}` : ''}:: ${(error as Error).message}`);
|
||||
throw new Error(`decodeU8a: failed at ${u8aToHex(u8a.subarray(decodedLength, decodedLength + 8))}…${keys[i] ? ` on ${keys[i]}` : ''}${rawType ? `: ${rawType}` : ''}:: ${(error as Error).message}`);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Registry } from '../types';
|
||||
|
||||
import { isBigInt, isBn, isHex, isNumber, isU8a, u8aConcat, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
@@ -92,7 +93,7 @@ export class GenericEthereumLookupSource extends Base<GenericEthereumAccountId |
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public override toHex (): string {
|
||||
public override toHex (): HexString {
|
||||
return u8aToHex(this.toU8a());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { EcdsaSignature, Ed25519Signature, ExtrinsicUnknown, ExtrinsicV4, Sr25519Signature } from '../interfaces/extrinsics';
|
||||
import type { FunctionMetadataLatest } from '../interfaces/metadata';
|
||||
import type { Address, Balance, Call, Index } from '../interfaces/runtime';
|
||||
@@ -216,7 +217,7 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
|
||||
/**
|
||||
* @description Injects an already-generated signature into the extrinsic
|
||||
*/
|
||||
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): GenericExtrinsic<A> {
|
||||
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: ExtrinsicPayloadValue | Uint8Array | HexString): GenericExtrinsic<A> {
|
||||
(this._raw as ExtrinsicVx).addSignature(signer, signature, payload);
|
||||
|
||||
return this;
|
||||
@@ -243,7 +244,7 @@ export class GenericExtrinsic<A extends AnyTuple = AnyTuple> extends ExtrinsicBa
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public override toHex (isBare?: boolean): string {
|
||||
public override toHex (isBare?: boolean): HexString {
|
||||
return u8aToHex(this.toU8a(isBare));
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { ExtrinsicPayloadV4 } from '../interfaces/extrinsics';
|
||||
import type { Balance, Hash, Index } from '../interfaces/runtime';
|
||||
import type { AnyJson, BareOpts, ExtrinsicPayloadValue, IKeyringPair, Registry } from '../types';
|
||||
@@ -119,7 +120,7 @@ export class GenericExtrinsicPayload extends Base<ExtrinsicPayloadVx> {
|
||||
/**
|
||||
* @description Sign the payload with the keypair
|
||||
*/
|
||||
public sign (signerPair: IKeyringPair): { signature: string } {
|
||||
public sign (signerPair: IKeyringPair): { signature: HexString } {
|
||||
const signature = this._raw.sign(signerPair);
|
||||
|
||||
// This is extensible, so we could quite readily extend to send back extra
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Address, Balance, BlockNumber, Call, ExtrinsicEra, Hash, Index, RuntimeVersion } from '../interfaces';
|
||||
import type { Codec, ISignerPayload, Registry, SignerPayloadJSON, SignerPayloadRaw } from '../types';
|
||||
|
||||
@@ -49,7 +50,7 @@ const knownTypes: Record<string, string> = {
|
||||
export class GenericSignerPayload extends Struct implements ISignerPayload, SignerPayloadType {
|
||||
private readonly _extraTypes: Record<string, string>;
|
||||
|
||||
constructor (registry: Registry, value?: string | { [x: string]: any; } | Map<unknown, unknown> | unknown[]) {
|
||||
constructor (registry: Registry, value?: HexString | { [x: string]: unknown; } | Map<unknown, unknown> | unknown[]) {
|
||||
const extensionTypes = {
|
||||
...registry.getSignedExtensionTypes(),
|
||||
...registry.getSignedExtensionExtra()
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { ExtrinsicSignatureV4 } from '../../interfaces/extrinsics';
|
||||
import type { Address, Call } from '../../interfaces/runtime';
|
||||
import type { ExtrinsicPayloadValue, IExtrinsicImpl, IKeyringPair, Registry, SignatureOptions } from '../../types';
|
||||
@@ -82,7 +83,7 @@ export class GenericExtrinsicV4 extends Struct implements IExtrinsicImpl {
|
||||
/**
|
||||
* @description Add an [[ExtrinsicSignatureV4]] to the extrinsic (already generated)
|
||||
*/
|
||||
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): GenericExtrinsicV4 {
|
||||
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: ExtrinsicPayloadValue | Uint8Array | HexString): GenericExtrinsicV4 {
|
||||
this.signature.addSignature(signer, signature, payload);
|
||||
|
||||
return this;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { SignOptions } from '@polkadot/keyring/types';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { ExtrinsicEra } from '../../interfaces/extrinsics';
|
||||
import type { AssetId, Balance, Hash, Index } from '../../interfaces/runtime';
|
||||
import type { ExtrinsicPayloadValue, IKeyringPair, Registry } from '../../types';
|
||||
@@ -23,7 +24,7 @@ import { sign } from '../util';
|
||||
export class GenericExtrinsicPayloadV4 extends Struct {
|
||||
#signOptions: SignOptions;
|
||||
|
||||
constructor (registry: Registry, value?: ExtrinsicPayloadValue | Uint8Array | string) {
|
||||
constructor (registry: Registry, value?: ExtrinsicPayloadValue | Uint8Array | HexString) {
|
||||
super(registry, {
|
||||
method: 'Bytes',
|
||||
...registry.getSignedExtensionTypes(),
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { EcdsaSignature, Ed25519Signature, ExtrinsicEra, ExtrinsicSignature, Sr25519Signature } from '../../interfaces/extrinsics';
|
||||
import type { Address, AssetId, Balance, Call, Index } from '../../interfaces/runtime';
|
||||
import type { ExtrinsicPayloadValue, IExtrinsicSignature, IKeyringPair, Registry, SignatureOptions } from '../../types';
|
||||
@@ -137,7 +138,7 @@ export class GenericExtrinsicSignatureV4 extends Struct implements IExtrinsicSig
|
||||
/**
|
||||
* @description Adds a raw signature
|
||||
*/
|
||||
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): IExtrinsicSignature {
|
||||
public addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: ExtrinsicPayloadValue | Uint8Array | HexString): IExtrinsicSignature {
|
||||
return this._injectSignature(
|
||||
toAddress(this.registry, signer),
|
||||
this.registry.createType('ExtrinsicSignature', signature),
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Registry } from '../types';
|
||||
|
||||
import { isBigInt, isBn, isHex, isNumber, isU8a, u8aConcat, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
|
||||
@@ -92,7 +93,7 @@ export class GenericLookupSource extends Base<GenericAccountId | GenericAccountI
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public override toHex (): string {
|
||||
public override toHex (): HexString {
|
||||
return u8aToHex(this.toU8a());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { Codec, Registry } from '../types';
|
||||
|
||||
@@ -84,7 +85,7 @@ export class bool extends Boolean implements Codec {
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public toHex (): string {
|
||||
public toHex (): HexString {
|
||||
return u8aToHex(this.toU8a());
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { Codec, Registry } from '../types';
|
||||
|
||||
@@ -51,7 +52,7 @@ export class Null implements Codec {
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public toHex (): string {
|
||||
public toHex (): HexString {
|
||||
return '0x';
|
||||
}
|
||||
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { CodecHash, Hash } from '../interfaces/runtime';
|
||||
import type { AnyU8a, Codec, Registry } from '../types';
|
||||
|
||||
@@ -120,7 +121,7 @@ export class Text extends String implements Codec {
|
||||
/**
|
||||
* @description Returns a hex string representation of the value
|
||||
*/
|
||||
public toHex (): string {
|
||||
public toHex (): HexString {
|
||||
// like with Vec<u8>, when we are encoding to hex, we don't actually add
|
||||
// the length prefix (it is already implied by the actual string length)
|
||||
return u8aToHex(this.toU8a(true));
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { BN } from '@polkadot/util';
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { Hash } from '../interfaces/runtime';
|
||||
import type { InterfaceTypes, Registry } from './registry';
|
||||
|
||||
@@ -76,7 +77,7 @@ export interface Codec {
|
||||
/**
|
||||
* @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
|
||||
*/
|
||||
toHex (isLe?: boolean): string;
|
||||
toHex (isLe?: boolean): HexString;
|
||||
|
||||
/**
|
||||
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
||||
@@ -116,6 +117,6 @@ export interface Constructor<T = Codec> {
|
||||
new(registry: Registry, ...args: any[]): T;
|
||||
}
|
||||
|
||||
export type ConstructorDef<T = Codec> = Record<string, Constructor<T> | Constructor<T>>;
|
||||
export type ConstructorDef<T = Codec> = Record<string, Constructor<T>>;
|
||||
|
||||
export type ArgsDef = Record<string, Constructor | keyof InterfaceTypes>;
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
// Copyright 2017-2021 @polkadot/types authors & contributors
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
import type { HexString } from '@polkadot/util/types';
|
||||
import type { ExtrinsicStatus } from '../interfaces/author';
|
||||
import type { EcdsaSignature, Ed25519Signature, Sr25519Signature } from '../interfaces/extrinsics';
|
||||
import type { Address, Balance, Call, H256, Index } from '../interfaces/runtime';
|
||||
@@ -125,7 +126,7 @@ export interface SignerResult {
|
||||
/**
|
||||
* @description The resulting signature in hex
|
||||
*/
|
||||
signature: string;
|
||||
signature: HexString;
|
||||
}
|
||||
|
||||
export interface Signer {
|
||||
@@ -184,13 +185,13 @@ export interface ExtrinsicPayloadValue {
|
||||
}
|
||||
|
||||
export interface IExtrinsicSignature extends ExtrinsicSignatureBase, Codec {
|
||||
addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: Uint8Array | string): IExtrinsicSignature;
|
||||
addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: Uint8Array | HexString): IExtrinsicSignature;
|
||||
sign (method: Call, account: IKeyringPair, options: SignatureOptions): IExtrinsicSignature;
|
||||
signFake (method: Call, address: Address | Uint8Array | string, options: SignatureOptions): IExtrinsicSignature;
|
||||
}
|
||||
|
||||
interface IExtrinsicSignable<T> {
|
||||
addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | string, payload: ExtrinsicPayloadValue | Uint8Array | string): T;
|
||||
addSignature (signer: Address | Uint8Array | string, signature: Uint8Array | HexString, payload: ExtrinsicPayloadValue | Uint8Array | HexString): T;
|
||||
sign (account: IKeyringPair, options: SignatureOptions): T;
|
||||
signFake (address: Address | Uint8Array | string, options: SignatureOptions): T;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user