@polkadot/types-codec 8.5.2-5 → 8.5.2-8
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 +4 -10
- package/base/Compact.d.ts +7 -2
- package/base/Compact.js +18 -3
- package/base/Enum.d.ts +16 -1
- package/base/Enum.js +29 -15
- package/base/Option.d.ts +6 -1
- package/base/Option.js +20 -3
- package/base/Tuple.d.ts +7 -1
- package/base/Tuple.js +36 -22
- package/base/Vec.d.ts +8 -2
- package/base/Vec.js +39 -22
- package/base/VecFixed.d.ts +7 -1
- package/base/VecFixed.js +20 -18
- package/cjs/abstract/AbstractArray.js +3 -9
- package/cjs/base/Compact.js +18 -3
- package/cjs/base/Enum.js +27 -12
- package/cjs/base/Option.js +20 -2
- package/cjs/base/Tuple.js +35 -21
- package/cjs/base/Vec.js +36 -21
- package/cjs/base/VecFixed.js +19 -17
- package/cjs/extended/BTreeSet.js +6 -5
- package/cjs/extended/BitVec.js +4 -4
- package/cjs/extended/Bytes.js +3 -3
- package/cjs/extended/Map.js +3 -4
- package/cjs/native/Struct.js +31 -17
- package/cjs/native/Text.js +3 -3
- 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 +7 -6
- package/extended/BitVec.js +5 -5
- package/extended/Bytes.js +4 -4
- package/extended/Map.js +4 -5
- package/native/Struct.d.ts +6 -1
- package/native/Struct.js +31 -18
- package/native/Text.js +4 -4
- package/package.json +2 -2
- 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
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
|
@@ -1,17 +1,18 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import {
|
|
3
|
+
import { compactFromU8aLim, compactToU8a, isHex, isU8a, logger, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { compareSet, decodeU8aVec, sortSet, typeToConstructor } from "../utils/index.js";
|
|
5
5
|
const l = logger('BTreeSet');
|
|
6
6
|
/** @internal */
|
|
7
7
|
|
|
8
8
|
function decodeSetFromU8a(registry, ValClass, u8a) {
|
|
9
9
|
const output = new Set();
|
|
10
|
-
const [offset,
|
|
11
|
-
const
|
|
10
|
+
const [offset, count] = compactFromU8aLim(u8a);
|
|
11
|
+
const result = new Array(count);
|
|
12
|
+
const [decodedLength] = decodeU8aVec(registry, result, u8a, offset, ValClass);
|
|
12
13
|
|
|
13
|
-
for (let i = 0; i <
|
|
14
|
-
output.add(
|
|
14
|
+
for (let i = 0; i < count; i++) {
|
|
15
|
+
output.add(result[i]);
|
|
15
16
|
}
|
|
16
17
|
|
|
17
18
|
return [ValClass, output, decodedLength];
|
|
@@ -212,7 +213,7 @@ export class BTreeSet extends Set {
|
|
|
212
213
|
encoded.push(v.toU8a(isBare));
|
|
213
214
|
}
|
|
214
215
|
|
|
215
|
-
return
|
|
216
|
+
return u8aConcatStrict(encoded);
|
|
216
217
|
}
|
|
217
218
|
|
|
218
219
|
}
|
package/extended/BitVec.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,
|
|
3
|
+
import { assert, compactFromU8aLim, compactToU8a, isString, u8aConcatStrict, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { Raw } from "../native/Raw.js";
|
|
5
5
|
/** @internal */
|
|
6
6
|
|
|
@@ -10,10 +10,10 @@ function decodeBitVecU8a(value) {
|
|
|
10
10
|
} // handle all other Uint8Array inputs, these do have a length prefix which is the number of bits encoded
|
|
11
11
|
|
|
12
12
|
|
|
13
|
-
const [offset, length] =
|
|
14
|
-
const total = offset + Math.ceil(length
|
|
13
|
+
const [offset, length] = compactFromU8aLim(value);
|
|
14
|
+
const total = offset + Math.ceil(length / 8);
|
|
15
15
|
assert(total <= value.length, () => `BitVec: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
|
16
|
-
return [length
|
|
16
|
+
return [length, value.subarray(offset, total)];
|
|
17
17
|
}
|
|
18
18
|
/** @internal */
|
|
19
19
|
|
|
@@ -84,7 +84,7 @@ export class BitVec extends Raw {
|
|
|
84
84
|
|
|
85
85
|
toU8a(isBare) {
|
|
86
86
|
const bitVec = super.toU8a();
|
|
87
|
-
return isBare ? bitVec :
|
|
87
|
+
return isBare ? bitVec : u8aConcatStrict([compactToU8a(this.#decodedLength), bitVec]);
|
|
88
88
|
}
|
|
89
89
|
|
|
90
90
|
}
|
package/extended/Bytes.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, compactAddLength,
|
|
3
|
+
import { assert, compactAddLength, compactFromU8aLim, compactToU8a, isCodec, isString, isU8a, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { Raw } from "../native/Raw.js"; // Bytes are used for things like on-chain code, so it has a healthy limit
|
|
5
5
|
|
|
6
6
|
const MAX_LENGTH = 10 * 1024 * 1024;
|
|
@@ -12,9 +12,9 @@ function decodeBytesU8a(value) {
|
|
|
12
12
|
} // handle all other Uint8Array inputs, these do have a length prefix
|
|
13
13
|
|
|
14
14
|
|
|
15
|
-
const [offset, length] =
|
|
16
|
-
const total = offset + length
|
|
17
|
-
assert(length
|
|
15
|
+
const [offset, length] = compactFromU8aLim(value);
|
|
16
|
+
const total = offset + length;
|
|
17
|
+
assert(length <= MAX_LENGTH, () => `Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
|
18
18
|
assert(total <= value.length, () => `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
|
19
19
|
return [value.subarray(offset, total), total];
|
|
20
20
|
}
|
package/extended/Map.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 { compactFromU8aLim, compactToU8a, isHex, isObject, isU8a, logger, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { AbstractArray } from "../abstract/AbstractArray.js";
|
|
5
5
|
import { Enum } from "../base/Enum.js";
|
|
6
6
|
import { Struct } from "../native/Struct.js";
|
|
@@ -10,15 +10,14 @@ const l = logger('Map');
|
|
|
10
10
|
|
|
11
11
|
function decodeMapFromU8a(registry, KeyClass, ValClass, u8a) {
|
|
12
12
|
const output = new Map();
|
|
13
|
-
const [offset,
|
|
14
|
-
const count = length.toNumber();
|
|
13
|
+
const [offset, count] = compactFromU8aLim(u8a);
|
|
15
14
|
const types = [];
|
|
16
15
|
|
|
17
16
|
for (let i = 0; i < count; i++) {
|
|
18
17
|
types.push(KeyClass, ValClass);
|
|
19
18
|
}
|
|
20
19
|
|
|
21
|
-
const [values, decodedLength] = decodeU8a(registry, u8a.subarray(offset), types);
|
|
20
|
+
const [values, decodedLength] = decodeU8a(registry, new Array(types.length), u8a.subarray(offset), [types, []]);
|
|
22
21
|
|
|
23
22
|
for (let i = 0; i < values.length; i += 2) {
|
|
24
23
|
output.set(values[i], values[i + 1]);
|
|
@@ -218,7 +217,7 @@ export class CodecMap extends Map {
|
|
|
218
217
|
encoded.push(k.toU8a(isBare), v.toU8a(isBare));
|
|
219
218
|
}
|
|
220
219
|
|
|
221
|
-
return
|
|
220
|
+
return u8aConcatStrict(encoded);
|
|
222
221
|
}
|
|
223
222
|
|
|
224
223
|
}
|
package/native/Struct.d.ts
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
import type { HexString } from '@polkadot/util/types';
|
|
2
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[]];
|
|
5
|
+
interface Options {
|
|
6
|
+
definition?: Definition;
|
|
7
|
+
setDefinition?: (d: Definition) => Definition;
|
|
8
|
+
}
|
|
4
9
|
/**
|
|
5
10
|
* @name Struct
|
|
6
11
|
* @description
|
|
@@ -23,7 +28,7 @@ export declare class Struct<S extends TypesDef = TypesDef, V extends {
|
|
|
23
28
|
#private;
|
|
24
29
|
createdAtHash?: IU8a;
|
|
25
30
|
readonly initialU8aLength?: number;
|
|
26
|
-
constructor(registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap?: Map<string, string
|
|
31
|
+
constructor(registry: Registry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap?: Map<string, string>, { definition, setDefinition }?: Options);
|
|
27
32
|
static with<S extends TypesDef>(Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>>;
|
|
28
33
|
/**
|
|
29
34
|
* @description The available keys for this struct
|
package/native/Struct.js
CHANGED
|
@@ -1,22 +1,26 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-codec authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify,
|
|
3
|
+
import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
4
4
|
import { compareMap, decodeU8a, mapToTypeMap, typesToMap } from "../utils/index.js";
|
|
5
5
|
|
|
6
|
+
function noopSetDefinition(d) {
|
|
7
|
+
return d;
|
|
8
|
+
}
|
|
6
9
|
/** @internal */
|
|
7
|
-
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
function decodeStructFromObject(registry, [Types, keys], value, jsonMap) {
|
|
8
13
|
let jsonObj;
|
|
9
|
-
const inputKeys = Object.keys(Types);
|
|
10
14
|
const typeofArray = Array.isArray(value);
|
|
11
15
|
const typeofMap = value instanceof Map;
|
|
12
16
|
assert(typeofArray || typeofMap || isObject(value), () => `Struct: Cannot decode value ${stringify(value)} (typeof ${typeof value}), expected an input object, map or array`);
|
|
13
|
-
assert(!typeofArray || value.length ===
|
|
14
|
-
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);
|
|
15
19
|
|
|
16
|
-
for (let i = 0; i <
|
|
17
|
-
const key =
|
|
20
|
+
for (let i = 0; i < keys.length; i++) {
|
|
21
|
+
const key = keys[i];
|
|
18
22
|
const jsonKey = jsonMap.get(key) || key;
|
|
19
|
-
const Type = Types[
|
|
23
|
+
const Type = Types[i];
|
|
20
24
|
let assign;
|
|
21
25
|
|
|
22
26
|
try {
|
|
@@ -73,9 +77,12 @@ export class Struct extends Map {
|
|
|
73
77
|
#jsonMap;
|
|
74
78
|
#Types;
|
|
75
79
|
|
|
76
|
-
constructor(registry, Types, value, jsonMap = new Map()
|
|
77
|
-
|
|
78
|
-
|
|
80
|
+
constructor(registry, Types, value, jsonMap = new Map(), {
|
|
81
|
+
definition,
|
|
82
|
+
setDefinition = noopSetDefinition
|
|
83
|
+
} = {}) {
|
|
84
|
+
const typeMap = definition || setDefinition(mapToTypeMap(registry, Types));
|
|
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);
|
|
79
86
|
super(decoded);
|
|
80
87
|
this.#registry = registry;
|
|
81
88
|
this.initialU8aLength = decodedLength;
|
|
@@ -85,9 +92,16 @@ export class Struct extends Map {
|
|
|
85
92
|
|
|
86
93
|
static with(Types, jsonMap) {
|
|
87
94
|
const keys = Object.keys(Types);
|
|
95
|
+
let definition; // eslint-disable-next-line no-return-assign
|
|
96
|
+
|
|
97
|
+
const setDefinition = d => definition = d;
|
|
98
|
+
|
|
88
99
|
return class extends Struct {
|
|
89
100
|
constructor(registry, value) {
|
|
90
|
-
super(registry, Types, value, jsonMap
|
|
101
|
+
super(registry, Types, value, jsonMap, {
|
|
102
|
+
definition,
|
|
103
|
+
setDefinition
|
|
104
|
+
});
|
|
91
105
|
objectProperties(this, keys, k => this.get(k));
|
|
92
106
|
}
|
|
93
107
|
|
|
@@ -99,7 +113,7 @@ export class Struct extends Map {
|
|
|
99
113
|
|
|
100
114
|
|
|
101
115
|
get defKeys() {
|
|
102
|
-
return
|
|
116
|
+
return this.#Types[1];
|
|
103
117
|
}
|
|
104
118
|
|
|
105
119
|
getT(key) {
|
|
@@ -126,11 +140,10 @@ export class Struct extends Map {
|
|
|
126
140
|
|
|
127
141
|
get Type() {
|
|
128
142
|
const result = {};
|
|
129
|
-
const
|
|
143
|
+
const [Types, keys] = this.#Types;
|
|
130
144
|
|
|
131
|
-
for (let i = 0; i <
|
|
132
|
-
|
|
133
|
-
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();
|
|
134
147
|
}
|
|
135
148
|
|
|
136
149
|
return result;
|
|
@@ -280,7 +293,7 @@ export class Struct extends Map {
|
|
|
280
293
|
}
|
|
281
294
|
}
|
|
282
295
|
|
|
283
|
-
return
|
|
296
|
+
return u8aConcatStrict(encoded);
|
|
284
297
|
}
|
|
285
298
|
|
|
286
299
|
}
|
package/native/Text.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, compactAddLength,
|
|
3
|
+
import { assert, compactAddLength, compactFromU8aLim, compactToU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
|
|
4
4
|
import { Raw } from "./Raw.js";
|
|
5
5
|
const MAX_LENGTH = 128 * 1024;
|
|
6
6
|
/** @internal */
|
|
@@ -17,9 +17,9 @@ function decodeText(value) {
|
|
|
17
17
|
return [u8aToString(value), 0];
|
|
18
18
|
}
|
|
19
19
|
|
|
20
|
-
const [offset, length] =
|
|
21
|
-
const total = offset + length
|
|
22
|
-
assert(length
|
|
20
|
+
const [offset, length] = compactFromU8aLim(value);
|
|
21
|
+
const total = offset + length;
|
|
22
|
+
assert(length <= MAX_LENGTH, () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
|
|
23
23
|
assert(total <= value.length, () => `Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
|
|
24
24
|
return [u8aToString(value.subarray(offset, total)), total];
|
|
25
25
|
} else if (isHex(value)) {
|
package/package.json
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"./cjs/detectPackage.js"
|
|
21
21
|
],
|
|
22
22
|
"type": "module",
|
|
23
|
-
"version": "8.5.2-
|
|
23
|
+
"version": "8.5.2-8",
|
|
24
24
|
"main": "./cjs/index.js",
|
|
25
25
|
"module": "./index.js",
|
|
26
26
|
"types": "./index.d.ts",
|
|
@@ -415,6 +415,6 @@
|
|
|
415
415
|
},
|
|
416
416
|
"dependencies": {
|
|
417
417
|
"@babel/runtime": "^7.18.0",
|
|
418
|
-
"@polkadot/util": "^9.2.
|
|
418
|
+
"@polkadot/util": "^9.2.2-15"
|
|
419
419
|
}
|
|
420
420
|
}
|
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-8'
|
|
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;
|