@polkadot/types-codec 8.5.2-6 → 8.5.2-7
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/abstract/AbstractArray.d.ts +1 -2
- package/abstract/AbstractArray.js +2 -8
- package/base/Enum.js +10 -10
- package/base/Tuple.d.ts +4 -5
- package/base/Tuple.js +17 -18
- package/base/Vec.d.ts +2 -1
- package/base/Vec.js +22 -20
- package/base/VecFixed.d.ts +1 -0
- package/base/VecFixed.js +5 -18
- package/cjs/abstract/AbstractArray.js +2 -8
- package/cjs/base/Enum.js +9 -9
- package/cjs/base/Tuple.js +17 -18
- package/cjs/base/Vec.js +20 -20
- package/cjs/base/VecFixed.js +4 -17
- package/cjs/extended/BTreeSet.js +5 -3
- package/cjs/extended/Map.js +1 -1
- package/cjs/native/Struct.js +12 -13
- package/cjs/packageInfo.js +1 -1
- package/cjs/utils/decodeU8a.js +8 -8
- package/cjs/utils/mapToTypeMap.js +4 -2
- package/cjs/utils/typesToMap.js +4 -5
- package/extended/BTreeSet.js +5 -3
- package/extended/Map.js +1 -1
- package/native/Struct.d.ts +4 -3
- package/native/Struct.js +11 -13
- package/package.json +1 -1
- package/packageInfo.js +1 -1
- package/types/codec.d.ts +0 -1
- package/utils/decodeU8a.d.ts +2 -4
- package/utils/decodeU8a.js +7 -8
- package/utils/mapToTypeMap.d.ts +1 -1
- package/utils/mapToTypeMap.js +4 -2
- package/utils/typesToMap.d.ts +1 -1
- package/utils/typesToMap.js +3 -5
|
@@ -10,8 +10,7 @@ import type { AnyJson, Codec, Inspect, IU8a, IVec, Registry } from '../types';
|
|
|
10
10
|
export declare abstract class AbstractArray<T extends Codec> extends Array<T> implements IVec<T> {
|
|
11
11
|
readonly registry: Registry;
|
|
12
12
|
createdAtHash?: IU8a;
|
|
13
|
-
|
|
14
|
-
protected constructor(registry: Registry, values: T[], initialU8aLength?: number);
|
|
13
|
+
protected constructor(registry: Registry, length: number);
|
|
15
14
|
/**
|
|
16
15
|
* @description The length of the value when encoded as a Uint8Array
|
|
17
16
|
*/
|
|
@@ -11,15 +11,9 @@ import { compareArray } from "../utils/compareArray.js";
|
|
|
11
11
|
*/
|
|
12
12
|
|
|
13
13
|
export class AbstractArray extends Array {
|
|
14
|
-
constructor(registry,
|
|
15
|
-
super(
|
|
16
|
-
|
|
17
|
-
for (let i = 0; i < values.length; i++) {
|
|
18
|
-
this[i] = values[i];
|
|
19
|
-
}
|
|
20
|
-
|
|
14
|
+
constructor(registry, length) {
|
|
15
|
+
super(length);
|
|
21
16
|
this.registry = registry;
|
|
22
|
-
this.initialU8aLength = initialU8aLength;
|
|
23
17
|
}
|
|
24
18
|
/**
|
|
25
19
|
* @description The length of the value when encoded as a Uint8Array
|
package/base/Enum.js
CHANGED
|
@@ -35,12 +35,11 @@ function extractDef(registry, _def) {
|
|
|
35
35
|
isBasic = true;
|
|
36
36
|
isIndexed = false;
|
|
37
37
|
} else if (isRustEnum(_def)) {
|
|
38
|
-
const
|
|
38
|
+
const [Types, keys] = mapToTypeMap(registry, _def);
|
|
39
39
|
|
|
40
|
-
for (let i = 0; i <
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
Type,
|
|
40
|
+
for (let i = 0; i < keys.length; i++) {
|
|
41
|
+
def[keys[i]] = {
|
|
42
|
+
Type: Types[i],
|
|
44
43
|
index: i
|
|
45
44
|
};
|
|
46
45
|
}
|
|
@@ -383,13 +382,14 @@ export class Enum {
|
|
|
383
382
|
}, {}) : this.defKeys;
|
|
384
383
|
}
|
|
385
384
|
|
|
386
|
-
const
|
|
385
|
+
const entries = Object.entries(this.#def);
|
|
386
|
+
return typesToMap(this.registry, entries.reduce((out, [key, {
|
|
387
387
|
Type
|
|
388
|
-
}]) => {
|
|
389
|
-
out[
|
|
388
|
+
}], i) => {
|
|
389
|
+
out[0][i] = Type;
|
|
390
|
+
out[1][i] = key;
|
|
390
391
|
return out;
|
|
391
|
-
},
|
|
392
|
-
return typesToMap(this.registry, typeMap);
|
|
392
|
+
}, [new Array(entries.length), new Array(entries.length)]));
|
|
393
393
|
}
|
|
394
394
|
/**
|
|
395
395
|
* @description Returns the base runtime type name for this instance
|
package/base/Tuple.d.ts
CHANGED
|
@@ -1,15 +1,13 @@
|
|
|
1
1
|
import type { AnyTupleValue, Codec, CodecClass, Inspect, ITuple, Registry } from '../types';
|
|
2
2
|
import { AbstractArray } from '../abstract/AbstractArray';
|
|
3
|
-
declare type TupleCodecClass = CodecClass[] | {
|
|
4
|
-
[index: string]: CodecClass;
|
|
5
|
-
};
|
|
6
3
|
declare type TupleType = (CodecClass | string);
|
|
7
4
|
declare type TupleTypes = TupleType[] | {
|
|
8
5
|
[index: string]: CodecClass | string;
|
|
9
6
|
};
|
|
7
|
+
declare type Definition = [CodecClass[], string[]];
|
|
10
8
|
interface Options {
|
|
11
|
-
definition?:
|
|
12
|
-
setDefinition?: (d:
|
|
9
|
+
definition?: Definition;
|
|
10
|
+
setDefinition?: (d: Definition) => Definition;
|
|
13
11
|
}
|
|
14
12
|
/**
|
|
15
13
|
* @name Tuple
|
|
@@ -19,6 +17,7 @@ interface Options {
|
|
|
19
17
|
*/
|
|
20
18
|
export declare class Tuple extends AbstractArray<Codec> implements ITuple<Codec[]> {
|
|
21
19
|
#private;
|
|
20
|
+
readonly initialU8aLength?: number;
|
|
22
21
|
constructor(registry: Registry, Types: TupleTypes | TupleType, value?: AnyTupleValue, { definition, setDefinition }?: Options);
|
|
23
22
|
static with(Types: TupleTypes | TupleType): CodecClass<Tuple>;
|
|
24
23
|
/**
|
package/base/Tuple.js
CHANGED
|
@@ -10,25 +10,23 @@ function noopSetDefinition(d) {
|
|
|
10
10
|
/** @internal */
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
function decodeTuple(registry, Classes, value) {
|
|
13
|
+
function decodeTuple(registry, result, Classes, value) {
|
|
14
14
|
if (isU8a(value) || isHex(value)) {
|
|
15
|
-
return decodeU8a(registry, u8aToU8a(value), Classes);
|
|
15
|
+
return decodeU8a(registry, result, u8aToU8a(value), Classes);
|
|
16
16
|
}
|
|
17
17
|
|
|
18
|
-
const Types =
|
|
19
|
-
return [Types.map((Type, index) => {
|
|
20
|
-
try {
|
|
21
|
-
const entry = value === null || value === void 0 ? void 0 : value[index];
|
|
22
|
-
|
|
23
|
-
if (entry instanceof Type) {
|
|
24
|
-
return entry;
|
|
25
|
-
}
|
|
18
|
+
const Types = Classes[0];
|
|
26
19
|
|
|
27
|
-
|
|
20
|
+
for (let i = 0; i < Types.length; i++) {
|
|
21
|
+
try {
|
|
22
|
+
const entry = value === null || value === void 0 ? void 0 : value[i];
|
|
23
|
+
result[i] = entry instanceof Types[i] ? entry : new Types[i](registry, entry);
|
|
28
24
|
} catch (error) {
|
|
29
|
-
throw new Error(`Tuple: failed on ${
|
|
25
|
+
throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
|
|
30
26
|
}
|
|
31
|
-
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
return [result, 0];
|
|
32
30
|
}
|
|
33
31
|
/**
|
|
34
32
|
* @name Tuple
|
|
@@ -45,9 +43,10 @@ export class Tuple extends AbstractArray {
|
|
|
45
43
|
definition,
|
|
46
44
|
setDefinition = noopSetDefinition
|
|
47
45
|
} = {}) {
|
|
48
|
-
const Classes = definition || setDefinition(Array.isArray(Types) ? Types.map(t => typeToConstructor(registry, t)) : isFunction(Types) || isString(Types) ? [typeToConstructor(registry, Types)] : mapToTypeMap(registry, Types));
|
|
49
|
-
|
|
50
|
-
|
|
46
|
+
const Classes = definition || setDefinition(Array.isArray(Types) ? [Types.map(t => typeToConstructor(registry, t)), []] : isFunction(Types) || isString(Types) ? [[typeToConstructor(registry, Types)], []] : mapToTypeMap(registry, Types));
|
|
47
|
+
super(registry, Classes[0].length);
|
|
48
|
+
const [, decodedLength] = isU8a(value) ? decodeU8a(registry, this, value, Classes, false) : decodeTuple(registry, this, Classes, value);
|
|
49
|
+
this.initialU8aLength = decodedLength;
|
|
51
50
|
this.#Types = Classes;
|
|
52
51
|
}
|
|
53
52
|
|
|
@@ -86,7 +85,7 @@ export class Tuple extends AbstractArray {
|
|
|
86
85
|
|
|
87
86
|
|
|
88
87
|
get Types() {
|
|
89
|
-
return
|
|
88
|
+
return this.#Types[1].length ? this.#Types[1] : this.#Types[0].map(T => new T(this.registry).toRawType());
|
|
90
89
|
}
|
|
91
90
|
/**
|
|
92
91
|
* @description Returns a breakdown of the hex encoding for this Codec
|
|
@@ -104,7 +103,7 @@ export class Tuple extends AbstractArray {
|
|
|
104
103
|
|
|
105
104
|
|
|
106
105
|
toRawType() {
|
|
107
|
-
const types =
|
|
106
|
+
const types = this.#Types[0].map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
|
|
108
107
|
return `(${types.join(',')})`;
|
|
109
108
|
}
|
|
110
109
|
/**
|
package/base/Vec.d.ts
CHANGED
|
@@ -5,7 +5,7 @@ interface Options<T> {
|
|
|
5
5
|
definition?: CodecClass<T>;
|
|
6
6
|
setDefinition?: (d: CodecClass<T>) => CodecClass<T>;
|
|
7
7
|
}
|
|
8
|
-
export declare function decodeVec<T extends Codec>(registry: Registry,
|
|
8
|
+
export declare function decodeVec<T extends Codec>(registry: Registry, result: T[], value: Uint8Array | HexString | unknown[], startAt: number, Type: CodecClass<T>): [number, number];
|
|
9
9
|
/**
|
|
10
10
|
* @name Vec
|
|
11
11
|
* @description
|
|
@@ -15,6 +15,7 @@ export declare function decodeVec<T extends Codec>(registry: Registry, Type: Cod
|
|
|
15
15
|
*/
|
|
16
16
|
export declare class Vec<T extends Codec> extends AbstractArray<T> {
|
|
17
17
|
#private;
|
|
18
|
+
readonly initialU8aLength?: number;
|
|
18
19
|
constructor(registry: Registry, Type: CodecClass<T> | string, value?: Uint8Array | HexString | unknown[], { definition, setDefinition }?: Options<T>);
|
|
19
20
|
static with<O extends Codec>(Type: CodecClass<O> | string): CodecClass<Vec<O>>;
|
|
20
21
|
/**
|
package/base/Vec.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { assert, compactFromU8a, logger, u8aToU8a } from '@polkadot/util';
|
|
3
|
+
import { assert, compactFromU8a, isU8a, logger, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { AbstractArray } from "../abstract/AbstractArray.js";
|
|
5
5
|
import { decodeU8aVec, typeToConstructor } from "../utils/index.js";
|
|
6
6
|
const MAX_LENGTH = 64 * 1024;
|
|
@@ -10,11 +10,22 @@ function noopSetDefinition(d) {
|
|
|
10
10
|
return d;
|
|
11
11
|
}
|
|
12
12
|
|
|
13
|
-
|
|
13
|
+
function decodeVecLength(value) {
|
|
14
14
|
if (Array.isArray(value)) {
|
|
15
|
-
|
|
15
|
+
return [value, value.length, 0];
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const u8a = u8aToU8a(value);
|
|
19
|
+
const [startAt, length] = compactFromU8a(u8a);
|
|
20
|
+
assert(length.lten(MAX_LENGTH), () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
|
21
|
+
return [u8a, length.toNumber(), startAt];
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
export function decodeVec(registry, result, value, startAt, Type) {
|
|
25
|
+
if (Array.isArray(value)) {
|
|
26
|
+
const count = result.length;
|
|
16
27
|
|
|
17
|
-
for (let i = 0; i <
|
|
28
|
+
for (let i = 0; i < count; i++) {
|
|
18
29
|
const entry = value[i];
|
|
19
30
|
|
|
20
31
|
try {
|
|
@@ -25,20 +36,10 @@ export function decodeVec(registry, Type, value, length = -1) {
|
|
|
25
36
|
}
|
|
26
37
|
}
|
|
27
38
|
|
|
28
|
-
return [
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const u8a = u8aToU8a(value);
|
|
32
|
-
let offset = 0;
|
|
33
|
-
|
|
34
|
-
if (length === -1) {
|
|
35
|
-
const [_offset, _length] = compactFromU8a(u8a);
|
|
36
|
-
assert(_length.lten(MAX_LENGTH), () => `Vec length ${_length.toString()} exceeds ${MAX_LENGTH}`);
|
|
37
|
-
length = _length.toNumber();
|
|
38
|
-
offset = _offset;
|
|
39
|
+
return [0, 0];
|
|
39
40
|
}
|
|
40
41
|
|
|
41
|
-
return decodeU8aVec(registry,
|
|
42
|
+
return decodeU8aVec(registry, result, u8aToU8a(value), startAt, Type);
|
|
42
43
|
}
|
|
43
44
|
/**
|
|
44
45
|
* @name Vec
|
|
@@ -55,10 +56,11 @@ export class Vec extends AbstractArray {
|
|
|
55
56
|
definition,
|
|
56
57
|
setDefinition = noopSetDefinition
|
|
57
58
|
} = {}) {
|
|
58
|
-
const
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
this.#Type
|
|
59
|
+
const [decodeFrom, length, startAt] = decodeVecLength(value);
|
|
60
|
+
super(registry, length);
|
|
61
|
+
this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
|
|
62
|
+
const [decodedLength] = isU8a(decodeFrom) ? decodeU8aVec(registry, this, decodeFrom, startAt, this.#Type) : decodeVec(registry, this, decodeFrom, startAt, this.#Type);
|
|
63
|
+
this.initialU8aLength = decodedLength;
|
|
62
64
|
}
|
|
63
65
|
|
|
64
66
|
static with(Type) {
|
package/base/VecFixed.d.ts
CHANGED
|
@@ -12,6 +12,7 @@ interface Options<T> {
|
|
|
12
12
|
*/
|
|
13
13
|
export declare class VecFixed<T extends Codec> extends AbstractArray<T> {
|
|
14
14
|
#private;
|
|
15
|
+
readonly initialU8aLength?: number;
|
|
15
16
|
constructor(registry: Registry, Type: CodecClass<T> | string, length: number, value?: Uint8Array | HexString | unknown[], { definition, setDefinition }?: Options<T>);
|
|
16
17
|
static with<O extends Codec>(Type: CodecClass<O> | string, length: number): CodecClass<VecFixed<O>>;
|
|
17
18
|
/**
|
package/base/VecFixed.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import {
|
|
3
|
+
import { isU8a, u8aConcat } from '@polkadot/util';
|
|
4
4
|
import { AbstractArray } from "../abstract/AbstractArray.js";
|
|
5
5
|
import { decodeU8aVec, typeToConstructor } from "../utils/index.js";
|
|
6
6
|
import { decodeVec } from "./Vec.js";
|
|
@@ -8,19 +8,6 @@ import { decodeVec } from "./Vec.js";
|
|
|
8
8
|
function noopSetDefinition(d) {
|
|
9
9
|
return d;
|
|
10
10
|
}
|
|
11
|
-
/** @internal */
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
function decodeVecFixed(registry, value, Type, length) {
|
|
15
|
-
const [values, decodedLength, decodedLengthNoOffset] = decodeVec(registry, Type, value, length);
|
|
16
|
-
|
|
17
|
-
while (values.length < length) {
|
|
18
|
-
values.push(new Type(registry));
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
assert(values.length === length, () => `Expected a length of exactly ${length} entries`);
|
|
22
|
-
return [values, decodedLength, decodedLengthNoOffset];
|
|
23
|
-
}
|
|
24
11
|
/**
|
|
25
12
|
* @name VecFixed
|
|
26
13
|
* @description
|
|
@@ -35,10 +22,10 @@ export class VecFixed extends AbstractArray {
|
|
|
35
22
|
definition,
|
|
36
23
|
setDefinition = noopSetDefinition
|
|
37
24
|
} = {}) {
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
this
|
|
25
|
+
super(registry, length);
|
|
26
|
+
this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
|
|
27
|
+
const [, decodedLengthNoOffset] = isU8a(value) ? decodeU8aVec(registry, this, value, 0, this.#Type) : decodeVec(registry, this, value, 0, this.#Type);
|
|
28
|
+
this.initialU8aLength = decodedLengthNoOffset;
|
|
42
29
|
}
|
|
43
30
|
|
|
44
31
|
static with(Type, length) {
|
|
@@ -20,15 +20,9 @@ var _compareArray = require("../utils/compareArray");
|
|
|
20
20
|
* @noInheritDoc
|
|
21
21
|
*/
|
|
22
22
|
class AbstractArray extends Array {
|
|
23
|
-
constructor(registry,
|
|
24
|
-
super(
|
|
25
|
-
|
|
26
|
-
for (let i = 0; i < values.length; i++) {
|
|
27
|
-
this[i] = values[i];
|
|
28
|
-
}
|
|
29
|
-
|
|
23
|
+
constructor(registry, length) {
|
|
24
|
+
super(length);
|
|
30
25
|
this.registry = registry;
|
|
31
|
-
this.initialU8aLength = initialU8aLength;
|
|
32
26
|
}
|
|
33
27
|
/**
|
|
34
28
|
* @description The length of the value when encoded as a Uint8Array
|
package/cjs/base/Enum.js
CHANGED
|
@@ -44,12 +44,11 @@ function extractDef(registry, _def) {
|
|
|
44
44
|
isBasic = true;
|
|
45
45
|
isIndexed = false;
|
|
46
46
|
} else if (isRustEnum(_def)) {
|
|
47
|
-
const
|
|
47
|
+
const [Types, keys] = (0, _utils.mapToTypeMap)(registry, _def);
|
|
48
48
|
|
|
49
|
-
for (let i = 0; i <
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
Type,
|
|
49
|
+
for (let i = 0; i < keys.length; i++) {
|
|
50
|
+
def[keys[i]] = {
|
|
51
|
+
Type: Types[i],
|
|
53
52
|
index: i
|
|
54
53
|
};
|
|
55
54
|
}
|
|
@@ -401,14 +400,15 @@ class Enum {
|
|
|
401
400
|
}, {}) : this.defKeys;
|
|
402
401
|
}
|
|
403
402
|
|
|
404
|
-
const
|
|
403
|
+
const entries = Object.entries(this.#def);
|
|
404
|
+
return (0, _utils.typesToMap)(this.registry, entries.reduce((out, _ref3, i) => {
|
|
405
405
|
let [key, {
|
|
406
406
|
Type
|
|
407
407
|
}] = _ref3;
|
|
408
|
-
out[
|
|
408
|
+
out[0][i] = Type;
|
|
409
|
+
out[1][i] = key;
|
|
409
410
|
return out;
|
|
410
|
-
},
|
|
411
|
-
return (0, _utils.typesToMap)(this.registry, typeMap);
|
|
411
|
+
}, [new Array(entries.length), new Array(entries.length)]));
|
|
412
412
|
}
|
|
413
413
|
/**
|
|
414
414
|
* @description Returns the base runtime type name for this instance
|
package/cjs/base/Tuple.js
CHANGED
|
@@ -19,25 +19,23 @@ function noopSetDefinition(d) {
|
|
|
19
19
|
/** @internal */
|
|
20
20
|
|
|
21
21
|
|
|
22
|
-
function decodeTuple(registry, Classes, value) {
|
|
22
|
+
function decodeTuple(registry, result, Classes, value) {
|
|
23
23
|
if ((0, _util.isU8a)(value) || (0, _util.isHex)(value)) {
|
|
24
|
-
return (0, _utils.decodeU8a)(registry, (0, _util.u8aToU8a)(value), Classes);
|
|
24
|
+
return (0, _utils.decodeU8a)(registry, result, (0, _util.u8aToU8a)(value), Classes);
|
|
25
25
|
}
|
|
26
26
|
|
|
27
|
-
const Types =
|
|
28
|
-
return [Types.map((Type, index) => {
|
|
29
|
-
try {
|
|
30
|
-
const entry = value === null || value === void 0 ? void 0 : value[index];
|
|
31
|
-
|
|
32
|
-
if (entry instanceof Type) {
|
|
33
|
-
return entry;
|
|
34
|
-
}
|
|
27
|
+
const Types = Classes[0];
|
|
35
28
|
|
|
36
|
-
|
|
29
|
+
for (let i = 0; i < Types.length; i++) {
|
|
30
|
+
try {
|
|
31
|
+
const entry = value === null || value === void 0 ? void 0 : value[i];
|
|
32
|
+
result[i] = entry instanceof Types[i] ? entry : new Types[i](registry, entry);
|
|
37
33
|
} catch (error) {
|
|
38
|
-
throw new Error(`Tuple: failed on ${
|
|
34
|
+
throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
|
|
39
35
|
}
|
|
40
|
-
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return [result, 0];
|
|
41
39
|
}
|
|
42
40
|
/**
|
|
43
41
|
* @name Tuple
|
|
@@ -55,9 +53,10 @@ class Tuple extends _AbstractArray.AbstractArray {
|
|
|
55
53
|
definition,
|
|
56
54
|
setDefinition = noopSetDefinition
|
|
57
55
|
} = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
58
|
-
const Classes = definition || setDefinition(Array.isArray(Types) ? Types.map(t => (0, _utils.typeToConstructor)(registry, t)) : (0, _util.isFunction)(Types) || (0, _util.isString)(Types) ? [(0, _utils.typeToConstructor)(registry, Types)] : (0, _utils.mapToTypeMap)(registry, Types));
|
|
59
|
-
|
|
60
|
-
|
|
56
|
+
const Classes = definition || setDefinition(Array.isArray(Types) ? [Types.map(t => (0, _utils.typeToConstructor)(registry, t)), []] : (0, _util.isFunction)(Types) || (0, _util.isString)(Types) ? [[(0, _utils.typeToConstructor)(registry, Types)], []] : (0, _utils.mapToTypeMap)(registry, Types));
|
|
57
|
+
super(registry, Classes[0].length);
|
|
58
|
+
const [, decodedLength] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8a)(registry, this, value, Classes, false) : decodeTuple(registry, this, Classes, value);
|
|
59
|
+
this.initialU8aLength = decodedLength;
|
|
61
60
|
this.#Types = Classes;
|
|
62
61
|
}
|
|
63
62
|
|
|
@@ -96,7 +95,7 @@ class Tuple extends _AbstractArray.AbstractArray {
|
|
|
96
95
|
|
|
97
96
|
|
|
98
97
|
get Types() {
|
|
99
|
-
return
|
|
98
|
+
return this.#Types[1].length ? this.#Types[1] : this.#Types[0].map(T => new T(this.registry).toRawType());
|
|
100
99
|
}
|
|
101
100
|
/**
|
|
102
101
|
* @description Returns a breakdown of the hex encoding for this Codec
|
|
@@ -114,7 +113,7 @@ class Tuple extends _AbstractArray.AbstractArray {
|
|
|
114
113
|
|
|
115
114
|
|
|
116
115
|
toRawType() {
|
|
117
|
-
const types =
|
|
116
|
+
const types = this.#Types[0].map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
|
|
118
117
|
return `(${types.join(',')})`;
|
|
119
118
|
}
|
|
120
119
|
/**
|
package/cjs/base/Vec.js
CHANGED
|
@@ -21,13 +21,22 @@ function noopSetDefinition(d) {
|
|
|
21
21
|
return d;
|
|
22
22
|
}
|
|
23
23
|
|
|
24
|
-
function
|
|
25
|
-
|
|
24
|
+
function decodeVecLength(value) {
|
|
25
|
+
if (Array.isArray(value)) {
|
|
26
|
+
return [value, value.length, 0];
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const u8a = (0, _util.u8aToU8a)(value);
|
|
30
|
+
const [startAt, length] = (0, _util.compactFromU8a)(u8a);
|
|
31
|
+
(0, _util.assert)(length.lten(MAX_LENGTH), () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
|
32
|
+
return [u8a, length.toNumber(), startAt];
|
|
33
|
+
}
|
|
26
34
|
|
|
35
|
+
function decodeVec(registry, result, value, startAt, Type) {
|
|
27
36
|
if (Array.isArray(value)) {
|
|
28
|
-
const
|
|
37
|
+
const count = result.length;
|
|
29
38
|
|
|
30
|
-
for (let i = 0; i <
|
|
39
|
+
for (let i = 0; i < count; i++) {
|
|
31
40
|
const entry = value[i];
|
|
32
41
|
|
|
33
42
|
try {
|
|
@@ -38,20 +47,10 @@ function decodeVec(registry, Type, value) {
|
|
|
38
47
|
}
|
|
39
48
|
}
|
|
40
49
|
|
|
41
|
-
return [
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
const u8a = (0, _util.u8aToU8a)(value);
|
|
45
|
-
let offset = 0;
|
|
46
|
-
|
|
47
|
-
if (length === -1) {
|
|
48
|
-
const [_offset, _length] = (0, _util.compactFromU8a)(u8a);
|
|
49
|
-
(0, _util.assert)(_length.lten(MAX_LENGTH), () => `Vec length ${_length.toString()} exceeds ${MAX_LENGTH}`);
|
|
50
|
-
length = _length.toNumber();
|
|
51
|
-
offset = _offset;
|
|
50
|
+
return [0, 0];
|
|
52
51
|
}
|
|
53
52
|
|
|
54
|
-
return (0, _utils.decodeU8aVec)(registry,
|
|
53
|
+
return (0, _utils.decodeU8aVec)(registry, result, (0, _util.u8aToU8a)(value), startAt, Type);
|
|
55
54
|
}
|
|
56
55
|
/**
|
|
57
56
|
* @name Vec
|
|
@@ -71,10 +70,11 @@ class Vec extends _AbstractArray.AbstractArray {
|
|
|
71
70
|
definition,
|
|
72
71
|
setDefinition = noopSetDefinition
|
|
73
72
|
} = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
74
|
-
const
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
this.#Type
|
|
73
|
+
const [decodeFrom, length, startAt] = decodeVecLength(value);
|
|
74
|
+
super(registry, length);
|
|
75
|
+
this.#Type = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
|
|
76
|
+
const [decodedLength] = (0, _util.isU8a)(decodeFrom) ? (0, _utils.decodeU8aVec)(registry, this, decodeFrom, startAt, this.#Type) : decodeVec(registry, this, decodeFrom, startAt, this.#Type);
|
|
77
|
+
this.initialU8aLength = decodedLength;
|
|
78
78
|
}
|
|
79
79
|
|
|
80
80
|
static with(Type) {
|
package/cjs/base/VecFixed.js
CHANGED
|
@@ -18,19 +18,6 @@ var _Vec = require("./Vec");
|
|
|
18
18
|
function noopSetDefinition(d) {
|
|
19
19
|
return d;
|
|
20
20
|
}
|
|
21
|
-
/** @internal */
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
function decodeVecFixed(registry, value, Type, length) {
|
|
25
|
-
const [values, decodedLength, decodedLengthNoOffset] = (0, _Vec.decodeVec)(registry, Type, value, length);
|
|
26
|
-
|
|
27
|
-
while (values.length < length) {
|
|
28
|
-
values.push(new Type(registry));
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
(0, _util.assert)(values.length === length, () => `Expected a length of exactly ${length} entries`);
|
|
32
|
-
return [values, decodedLength, decodedLengthNoOffset];
|
|
33
|
-
}
|
|
34
21
|
/**
|
|
35
22
|
* @name VecFixed
|
|
36
23
|
* @description
|
|
@@ -47,10 +34,10 @@ class VecFixed extends _AbstractArray.AbstractArray {
|
|
|
47
34
|
definition,
|
|
48
35
|
setDefinition = noopSetDefinition
|
|
49
36
|
} = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
this
|
|
37
|
+
super(registry, length);
|
|
38
|
+
this.#Type = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
|
|
39
|
+
const [, decodedLengthNoOffset] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8aVec)(registry, this, value, 0, this.#Type) : (0, _Vec.decodeVec)(registry, this, value, 0, this.#Type);
|
|
40
|
+
this.initialU8aLength = decodedLengthNoOffset;
|
|
54
41
|
}
|
|
55
42
|
|
|
56
43
|
static with(Type, length) {
|
package/cjs/extended/BTreeSet.js
CHANGED
|
@@ -17,10 +17,12 @@ const l = (0, _util.logger)('BTreeSet');
|
|
|
17
17
|
function decodeSetFromU8a(registry, ValClass, u8a) {
|
|
18
18
|
const output = new Set();
|
|
19
19
|
const [offset, length] = (0, _util.compactFromU8a)(u8a);
|
|
20
|
-
const
|
|
20
|
+
const count = length.toNumber();
|
|
21
|
+
const result = new Array(count);
|
|
22
|
+
const [decodedLength] = (0, _utils.decodeU8aVec)(registry, result, u8a, offset, ValClass);
|
|
21
23
|
|
|
22
|
-
for (let i = 0; i <
|
|
23
|
-
output.add(
|
|
24
|
+
for (let i = 0; i < count; i++) {
|
|
25
|
+
output.add(result[i]);
|
|
24
26
|
}
|
|
25
27
|
|
|
26
28
|
return [ValClass, output, decodedLength];
|
package/cjs/extended/Map.js
CHANGED
|
@@ -30,7 +30,7 @@ function decodeMapFromU8a(registry, KeyClass, ValClass, u8a) {
|
|
|
30
30
|
types.push(KeyClass, ValClass);
|
|
31
31
|
}
|
|
32
32
|
|
|
33
|
-
const [values, decodedLength] = (0, _utils.decodeU8a)(registry, u8a.subarray(offset), types);
|
|
33
|
+
const [values, decodedLength] = (0, _utils.decodeU8a)(registry, new Array(types.length), u8a.subarray(offset), [types, []]);
|
|
34
34
|
|
|
35
35
|
for (let i = 0; i < values.length; i += 2) {
|
|
36
36
|
output.set(values[i], values[i + 1]);
|
package/cjs/native/Struct.js
CHANGED
|
@@ -17,19 +17,19 @@ function noopSetDefinition(d) {
|
|
|
17
17
|
/** @internal */
|
|
18
18
|
|
|
19
19
|
|
|
20
|
-
function decodeStructFromObject(registry,
|
|
20
|
+
function decodeStructFromObject(registry, _ref, value, jsonMap) {
|
|
21
|
+
let [Types, keys] = _ref;
|
|
21
22
|
let jsonObj;
|
|
22
|
-
const inputKeys = Object.keys(Types);
|
|
23
23
|
const typeofArray = Array.isArray(value);
|
|
24
24
|
const typeofMap = value instanceof Map;
|
|
25
25
|
(0, _util.assert)(typeofArray || typeofMap || (0, _util.isObject)(value), () => `Struct: Cannot decode value ${(0, _util.stringify)(value)} (typeof ${typeof value}), expected an input object, map or array`);
|
|
26
|
-
(0, _util.assert)(!typeofArray || value.length ===
|
|
27
|
-
const raw = new Array(
|
|
26
|
+
(0, _util.assert)(!typeofArray || value.length === keys.length, () => `Struct: Unable to map ${(0, _util.stringify)(value)} array to object with known keys ${keys.join(', ')}`);
|
|
27
|
+
const raw = new Array(keys.length);
|
|
28
28
|
|
|
29
|
-
for (let i = 0; i <
|
|
30
|
-
const key =
|
|
29
|
+
for (let i = 0; i < keys.length; i++) {
|
|
30
|
+
const key = keys[i];
|
|
31
31
|
const jsonKey = jsonMap.get(key) || key;
|
|
32
|
-
const Type = Types[
|
|
32
|
+
const Type = Types[i];
|
|
33
33
|
let assign;
|
|
34
34
|
|
|
35
35
|
try {
|
|
@@ -93,7 +93,7 @@ class Struct extends Map {
|
|
|
93
93
|
setDefinition = noopSetDefinition
|
|
94
94
|
} = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
|
|
95
95
|
const typeMap = definition || setDefinition((0, _utils.mapToTypeMap)(registry, Types));
|
|
96
|
-
const [decoded, decodedLength] = (0, _util.isU8a)(value)
|
|
96
|
+
const [decoded, decodedLength] = (0, _util.isU8a)(value) || (0, _util.isHex)(value) ? (0, _utils.decodeU8a)(registry, new Array(typeMap[0].length), (0, _util.u8aToU8a)(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
|
|
97
97
|
super(decoded);
|
|
98
98
|
this.#registry = registry;
|
|
99
99
|
this.initialU8aLength = decodedLength;
|
|
@@ -124,7 +124,7 @@ class Struct extends Map {
|
|
|
124
124
|
|
|
125
125
|
|
|
126
126
|
get defKeys() {
|
|
127
|
-
return
|
|
127
|
+
return this.#Types[1];
|
|
128
128
|
}
|
|
129
129
|
|
|
130
130
|
getT(key) {
|
|
@@ -151,11 +151,10 @@ class Struct extends Map {
|
|
|
151
151
|
|
|
152
152
|
get Type() {
|
|
153
153
|
const result = {};
|
|
154
|
-
const
|
|
154
|
+
const [Types, keys] = this.#Types;
|
|
155
155
|
|
|
156
|
-
for (let i = 0; i <
|
|
157
|
-
|
|
158
|
-
result[key] = new Type(this.registry).toRawType();
|
|
156
|
+
for (let i = 0; i < keys.length; i++) {
|
|
157
|
+
result[keys[i]] = new Types[i](this.registry).toRawType();
|
|
159
158
|
}
|
|
160
159
|
|
|
161
160
|
return result;
|
package/cjs/packageInfo.js
CHANGED
package/cjs/utils/decodeU8a.js
CHANGED
|
@@ -31,12 +31,12 @@ function getRawType(registry, Type) {
|
|
|
31
31
|
*/
|
|
32
32
|
|
|
33
33
|
|
|
34
|
-
function decodeU8a(registry, u8a,
|
|
35
|
-
|
|
36
|
-
const
|
|
34
|
+
function decodeU8a(registry, result, u8a, _ref, withZip) {
|
|
35
|
+
let [Types, keys] = _ref;
|
|
36
|
+
const count = result.length;
|
|
37
37
|
let offset = 0;
|
|
38
38
|
|
|
39
|
-
for (let i = 0; i <
|
|
39
|
+
for (let i = 0; i < count; i++) {
|
|
40
40
|
try {
|
|
41
41
|
const value = new Types[i](registry, u8a.subarray(offset));
|
|
42
42
|
offset += value.initialU8aLength || value.encodedLength;
|
|
@@ -51,9 +51,9 @@ function decodeU8a(registry, u8a, types, withZip) {
|
|
|
51
51
|
// since we have to do less checks (and these are intensive anyway), much faster
|
|
52
52
|
|
|
53
53
|
|
|
54
|
-
function decodeU8aVec(registry, u8a,
|
|
55
|
-
const
|
|
56
|
-
let offset =
|
|
54
|
+
function decodeU8aVec(registry, result, u8a, startAt, Type) {
|
|
55
|
+
const count = result.length;
|
|
56
|
+
let offset = startAt;
|
|
57
57
|
|
|
58
58
|
for (let i = 0; i < count; i++) {
|
|
59
59
|
try {
|
|
@@ -65,5 +65,5 @@ function decodeU8aVec(registry, u8a, start, Type, count) {
|
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
|
|
68
|
-
return [
|
|
68
|
+
return [offset, offset - startAt];
|
|
69
69
|
}
|
|
@@ -15,10 +15,12 @@ var _typeToConstructor = require("./typeToConstructor");
|
|
|
15
15
|
*/
|
|
16
16
|
function mapToTypeMap(registry, input) {
|
|
17
17
|
const entries = Object.entries(input);
|
|
18
|
-
const
|
|
18
|
+
const count = entries.length;
|
|
19
|
+
const output = [new Array(count), new Array(count)];
|
|
19
20
|
|
|
20
21
|
for (let i = 0; i < entries.length; i++) {
|
|
21
|
-
output[
|
|
22
|
+
output[1][i] = entries[i][0];
|
|
23
|
+
output[0][i] = (0, _typeToConstructor.typeToConstructor)(registry, entries[i][1]);
|
|
22
24
|
}
|
|
23
25
|
|
|
24
26
|
return output;
|
package/cjs/utils/typesToMap.js
CHANGED
|
@@ -7,13 +7,12 @@ exports.typesToMap = typesToMap;
|
|
|
7
7
|
|
|
8
8
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
9
9
|
// SPDX-License-Identifier: Apache-2.0
|
|
10
|
-
function typesToMap(registry,
|
|
11
|
-
|
|
10
|
+
function typesToMap(registry, _ref) {
|
|
11
|
+
let [Types, keys] = _ref;
|
|
12
12
|
const result = {};
|
|
13
13
|
|
|
14
|
-
for (let i = 0; i <
|
|
15
|
-
|
|
16
|
-
result[key] = registry.getClassName(Type) || new Type(registry).toRawType();
|
|
14
|
+
for (let i = 0; i < keys.length; i++) {
|
|
15
|
+
result[keys[i]] = registry.getClassName(Types[i]) || new Types[i](registry).toRawType();
|
|
17
16
|
}
|
|
18
17
|
|
|
19
18
|
return result;
|
package/extended/BTreeSet.js
CHANGED
|
@@ -8,10 +8,12 @@ const l = logger('BTreeSet');
|
|
|
8
8
|
function decodeSetFromU8a(registry, ValClass, u8a) {
|
|
9
9
|
const output = new Set();
|
|
10
10
|
const [offset, length] = compactFromU8a(u8a);
|
|
11
|
-
const
|
|
11
|
+
const count = length.toNumber();
|
|
12
|
+
const result = new Array(count);
|
|
13
|
+
const [decodedLength] = decodeU8aVec(registry, result, u8a, offset, ValClass);
|
|
12
14
|
|
|
13
|
-
for (let i = 0; i <
|
|
14
|
-
output.add(
|
|
15
|
+
for (let i = 0; i < count; i++) {
|
|
16
|
+
output.add(result[i]);
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
return [ValClass, output, decodedLength];
|
package/extended/Map.js
CHANGED
|
@@ -18,7 +18,7 @@ function decodeMapFromU8a(registry, KeyClass, ValClass, u8a) {
|
|
|
18
18
|
types.push(KeyClass, ValClass);
|
|
19
19
|
}
|
|
20
20
|
|
|
21
|
-
const [values, decodedLength] = decodeU8a(registry, u8a.subarray(offset), types);
|
|
21
|
+
const [values, decodedLength] = decodeU8a(registry, new Array(types.length), u8a.subarray(offset), [types, []]);
|
|
22
22
|
|
|
23
23
|
for (let i = 0; i < values.length; i += 2) {
|
|
24
24
|
output.set(values[i], values[i + 1]);
|
package/native/Struct.d.ts
CHANGED
|
@@ -1,9 +1,10 @@
|
|
|
1
1
|
import type { HexString } from '@polkadot/util/types';
|
|
2
|
-
import type { AnyJson, BareOpts, Codec, CodecClass,
|
|
2
|
+
import type { AnyJson, BareOpts, Codec, CodecClass, Inspect, IStruct, IU8a, Registry } from '../types';
|
|
3
3
|
declare type TypesDef<T = Codec> = Record<string, string | CodecClass<T>>;
|
|
4
|
+
declare type Definition = [CodecClass[], string[]];
|
|
4
5
|
interface Options {
|
|
5
|
-
definition?:
|
|
6
|
-
setDefinition?: (d:
|
|
6
|
+
definition?: Definition;
|
|
7
|
+
setDefinition?: (d: Definition) => Definition;
|
|
7
8
|
}
|
|
8
9
|
/**
|
|
9
10
|
* @name Struct
|
package/native/Struct.js
CHANGED
|
@@ -9,19 +9,18 @@ function noopSetDefinition(d) {
|
|
|
9
9
|
/** @internal */
|
|
10
10
|
|
|
11
11
|
|
|
12
|
-
function decodeStructFromObject(registry, Types, value, jsonMap) {
|
|
12
|
+
function decodeStructFromObject(registry, [Types, keys], value, jsonMap) {
|
|
13
13
|
let jsonObj;
|
|
14
|
-
const inputKeys = Object.keys(Types);
|
|
15
14
|
const typeofArray = Array.isArray(value);
|
|
16
15
|
const typeofMap = value instanceof Map;
|
|
17
16
|
assert(typeofArray || typeofMap || isObject(value), () => `Struct: Cannot decode value ${stringify(value)} (typeof ${typeof value}), expected an input object, map or array`);
|
|
18
|
-
assert(!typeofArray || value.length ===
|
|
19
|
-
const raw = new Array(
|
|
17
|
+
assert(!typeofArray || value.length === keys.length, () => `Struct: Unable to map ${stringify(value)} array to object with known keys ${keys.join(', ')}`);
|
|
18
|
+
const raw = new Array(keys.length);
|
|
20
19
|
|
|
21
|
-
for (let i = 0; i <
|
|
22
|
-
const key =
|
|
20
|
+
for (let i = 0; i < keys.length; i++) {
|
|
21
|
+
const key = keys[i];
|
|
23
22
|
const jsonKey = jsonMap.get(key) || key;
|
|
24
|
-
const Type = Types[
|
|
23
|
+
const Type = Types[i];
|
|
25
24
|
let assign;
|
|
26
25
|
|
|
27
26
|
try {
|
|
@@ -83,7 +82,7 @@ export class Struct extends Map {
|
|
|
83
82
|
setDefinition = noopSetDefinition
|
|
84
83
|
} = {}) {
|
|
85
84
|
const typeMap = definition || setDefinition(mapToTypeMap(registry, Types));
|
|
86
|
-
const [decoded, decodedLength] = isU8a(value)
|
|
85
|
+
const [decoded, decodedLength] = isU8a(value) || isHex(value) ? decodeU8a(registry, new Array(typeMap[0].length), u8aToU8a(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
|
|
87
86
|
super(decoded);
|
|
88
87
|
this.#registry = registry;
|
|
89
88
|
this.initialU8aLength = decodedLength;
|
|
@@ -114,7 +113,7 @@ export class Struct extends Map {
|
|
|
114
113
|
|
|
115
114
|
|
|
116
115
|
get defKeys() {
|
|
117
|
-
return
|
|
116
|
+
return this.#Types[1];
|
|
118
117
|
}
|
|
119
118
|
|
|
120
119
|
getT(key) {
|
|
@@ -141,11 +140,10 @@ export class Struct extends Map {
|
|
|
141
140
|
|
|
142
141
|
get Type() {
|
|
143
142
|
const result = {};
|
|
144
|
-
const
|
|
143
|
+
const [Types, keys] = this.#Types;
|
|
145
144
|
|
|
146
|
-
for (let i = 0; i <
|
|
147
|
-
|
|
148
|
-
result[key] = new Type(this.registry).toRawType();
|
|
145
|
+
for (let i = 0; i < keys.length; i++) {
|
|
146
|
+
result[keys[i]] = new Types[i](this.registry).toRawType();
|
|
149
147
|
}
|
|
150
148
|
|
|
151
149
|
return result;
|
package/package.json
CHANGED
package/packageInfo.js
CHANGED
|
@@ -5,5 +5,5 @@ export const packageInfo = {
|
|
|
5
5
|
name: '@polkadot/types-codec',
|
|
6
6
|
path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
|
|
7
7
|
type: 'esm',
|
|
8
|
-
version: '8.5.2-
|
|
8
|
+
version: '8.5.2-7'
|
|
9
9
|
};
|
package/types/codec.d.ts
CHANGED
|
@@ -81,6 +81,5 @@ export interface CodecClass<T = Codec> {
|
|
|
81
81
|
readonly __fallbackType?: string;
|
|
82
82
|
new (registry: Registry, ...args: any[]): T;
|
|
83
83
|
}
|
|
84
|
-
export declare type CodecClassDef<T = Codec> = Record<string, CodecClass<T>>;
|
|
85
84
|
export declare type CodecTo = 'toHex' | 'toJSON' | 'toString' | 'toU8a';
|
|
86
85
|
export declare type ArgsDef = Record<string, CodecClass | string>;
|
package/utils/decodeU8a.d.ts
CHANGED
|
@@ -7,7 +7,5 @@ import type { Codec, CodecClass, Registry } from '../types';
|
|
|
7
7
|
* @param result - The result array (will be returned with values pushed)
|
|
8
8
|
* @param types - The array of CodecClass to decode the U8a against.
|
|
9
9
|
*/
|
|
10
|
-
export declare function decodeU8a<T extends Codec = Codec, E = T>(registry: Registry, u8a: Uint8Array,
|
|
11
|
-
|
|
12
|
-
}, withZip?: boolean): [E[], number];
|
|
13
|
-
export declare function decodeU8aVec<T extends Codec = Codec>(registry: Registry, u8a: Uint8Array, start: number, Type: CodecClass<T>, count: number): [T[], number, number];
|
|
10
|
+
export declare function decodeU8a<T extends Codec = Codec, E = T>(registry: Registry, result: unknown[], u8a: Uint8Array, [Types, keys]: [CodecClass[], string[]], withZip?: boolean): [E[], number];
|
|
11
|
+
export declare function decodeU8aVec<T extends Codec = Codec>(registry: Registry, result: unknown[], u8a: Uint8Array, startAt: number, Type: CodecClass<T>): [number, number];
|
package/utils/decodeU8a.js
CHANGED
|
@@ -23,12 +23,11 @@ function getRawType(registry, Type) {
|
|
|
23
23
|
*/
|
|
24
24
|
|
|
25
25
|
|
|
26
|
-
export function decodeU8a(registry, u8a,
|
|
27
|
-
const
|
|
28
|
-
const result = new Array(Types.length);
|
|
26
|
+
export function decodeU8a(registry, result, u8a, [Types, keys], withZip) {
|
|
27
|
+
const count = result.length;
|
|
29
28
|
let offset = 0;
|
|
30
29
|
|
|
31
|
-
for (let i = 0; i <
|
|
30
|
+
for (let i = 0; i < count; i++) {
|
|
32
31
|
try {
|
|
33
32
|
const value = new Types[i](registry, u8a.subarray(offset));
|
|
34
33
|
offset += value.initialU8aLength || value.encodedLength;
|
|
@@ -42,9 +41,9 @@ export function decodeU8a(registry, u8a, types, withZip) {
|
|
|
42
41
|
} // Split from decodeU8a since this is specialized to 1 instance ... yes duplication, but
|
|
43
42
|
// since we have to do less checks (and these are intensive anyway), much faster
|
|
44
43
|
|
|
45
|
-
export function decodeU8aVec(registry, u8a,
|
|
46
|
-
const
|
|
47
|
-
let offset =
|
|
44
|
+
export function decodeU8aVec(registry, result, u8a, startAt, Type) {
|
|
45
|
+
const count = result.length;
|
|
46
|
+
let offset = startAt;
|
|
48
47
|
|
|
49
48
|
for (let i = 0; i < count; i++) {
|
|
50
49
|
try {
|
|
@@ -56,5 +55,5 @@ export function decodeU8aVec(registry, u8a, start, Type, count) {
|
|
|
56
55
|
}
|
|
57
56
|
}
|
|
58
57
|
|
|
59
|
-
return [
|
|
58
|
+
return [offset, offset - startAt];
|
|
60
59
|
}
|
package/utils/mapToTypeMap.d.ts
CHANGED
|
@@ -2,4 +2,4 @@ import type { CodecClass, Registry } from '../types';
|
|
|
2
2
|
/**
|
|
3
3
|
* @description takes an input map of the form `{ [string]: string | CodecClass }` and returns a map of `{ [string]: CodecClass }`
|
|
4
4
|
*/
|
|
5
|
-
export declare function mapToTypeMap(registry: Registry, input: Record<string, string | CodecClass>):
|
|
5
|
+
export declare function mapToTypeMap(registry: Registry, input: Record<string, string | CodecClass>): [CodecClass[], string[]];
|
package/utils/mapToTypeMap.js
CHANGED
|
@@ -7,10 +7,12 @@ import { typeToConstructor } from "./typeToConstructor.js";
|
|
|
7
7
|
|
|
8
8
|
export function mapToTypeMap(registry, input) {
|
|
9
9
|
const entries = Object.entries(input);
|
|
10
|
-
const
|
|
10
|
+
const count = entries.length;
|
|
11
|
+
const output = [new Array(count), new Array(count)];
|
|
11
12
|
|
|
12
13
|
for (let i = 0; i < entries.length; i++) {
|
|
13
|
-
output[
|
|
14
|
+
output[1][i] = entries[i][0];
|
|
15
|
+
output[0][i] = typeToConstructor(registry, entries[i][1]);
|
|
14
16
|
}
|
|
15
17
|
|
|
16
18
|
return output;
|
package/utils/typesToMap.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import type { CodecClass, Registry } from '../types';
|
|
2
|
-
export declare function typesToMap(registry: Registry, Types:
|
|
2
|
+
export declare function typesToMap(registry: Registry, [Types, keys]: [CodecClass[], string[]]): Record<string, string>;
|
package/utils/typesToMap.js
CHANGED
|
@@ -1,12 +1,10 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
export function typesToMap(registry, Types) {
|
|
4
|
-
const entries = Object.entries(Types);
|
|
3
|
+
export function typesToMap(registry, [Types, keys]) {
|
|
5
4
|
const result = {};
|
|
6
5
|
|
|
7
|
-
for (let i = 0; i <
|
|
8
|
-
|
|
9
|
-
result[key] = registry.getClassName(Type) || new Type(registry).toRawType();
|
|
6
|
+
for (let i = 0; i < keys.length; i++) {
|
|
7
|
+
result[keys[i]] = registry.getClassName(Types[i]) || new Types[i](registry).toRawType();
|
|
10
8
|
}
|
|
11
9
|
|
|
12
10
|
return result;
|