@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.
Files changed (47) hide show
  1. package/abstract/AbstractArray.d.ts +1 -2
  2. package/abstract/AbstractArray.js +4 -10
  3. package/base/Compact.d.ts +7 -2
  4. package/base/Compact.js +18 -3
  5. package/base/Enum.d.ts +16 -1
  6. package/base/Enum.js +29 -15
  7. package/base/Option.d.ts +6 -1
  8. package/base/Option.js +20 -3
  9. package/base/Tuple.d.ts +7 -1
  10. package/base/Tuple.js +36 -22
  11. package/base/Vec.d.ts +8 -2
  12. package/base/Vec.js +39 -22
  13. package/base/VecFixed.d.ts +7 -1
  14. package/base/VecFixed.js +20 -18
  15. package/cjs/abstract/AbstractArray.js +3 -9
  16. package/cjs/base/Compact.js +18 -3
  17. package/cjs/base/Enum.js +27 -12
  18. package/cjs/base/Option.js +20 -2
  19. package/cjs/base/Tuple.js +35 -21
  20. package/cjs/base/Vec.js +36 -21
  21. package/cjs/base/VecFixed.js +19 -17
  22. package/cjs/extended/BTreeSet.js +6 -5
  23. package/cjs/extended/BitVec.js +4 -4
  24. package/cjs/extended/Bytes.js +3 -3
  25. package/cjs/extended/Map.js +3 -4
  26. package/cjs/native/Struct.js +31 -17
  27. package/cjs/native/Text.js +3 -3
  28. package/cjs/packageInfo.js +1 -1
  29. package/cjs/utils/decodeU8a.js +8 -8
  30. package/cjs/utils/mapToTypeMap.js +4 -2
  31. package/cjs/utils/typesToMap.js +4 -5
  32. package/extended/BTreeSet.js +7 -6
  33. package/extended/BitVec.js +5 -5
  34. package/extended/Bytes.js +4 -4
  35. package/extended/Map.js +4 -5
  36. package/native/Struct.d.ts +6 -1
  37. package/native/Struct.js +31 -18
  38. package/native/Text.js +4 -4
  39. package/package.json +2 -2
  40. package/packageInfo.js +1 -1
  41. package/types/codec.d.ts +0 -1
  42. package/utils/decodeU8a.d.ts +2 -4
  43. package/utils/decodeU8a.js +7 -8
  44. package/utils/mapToTypeMap.d.ts +1 -1
  45. package/utils/mapToTypeMap.js +4 -2
  46. package/utils/typesToMap.d.ts +1 -1
  47. package/utils/typesToMap.js +3 -5
@@ -20,15 +20,9 @@ var _compareArray = require("../utils/compareArray");
20
20
  * @noInheritDoc
21
21
  */
22
22
  class AbstractArray extends Array {
23
- constructor(registry, values, initialU8aLength) {
24
- super(values.length); // explicitly set the values here - this removes the need for any extra allocations
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
@@ -169,7 +163,7 @@ class AbstractArray extends Array {
169
163
 
170
164
  toU8a(isBare) {
171
165
  const encoded = this.toU8aInner();
172
- return isBare ? (0, _util.u8aConcat)(...encoded) : (0, _util.u8aConcat)((0, _util.compactToU8a)(this.length), ...encoded);
166
+ return isBare ? (0, _util.u8aConcatStrict)(encoded) : (0, _util.u8aConcatStrict)([(0, _util.compactToU8a)(this.length), ...encoded]);
173
167
  }
174
168
 
175
169
  toU8aInner(isBare) {
@@ -11,7 +11,9 @@ var _utils = require("../utils");
11
11
 
12
12
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
13
13
  // SPDX-License-Identifier: Apache-2.0
14
-
14
+ function noopSetDefinition(d) {
15
+ return d;
16
+ }
15
17
  /**
16
18
  * @name Compact
17
19
  * @description
@@ -20,23 +22,36 @@ var _utils = require("../utils");
20
22
  * used by other types to add length-prefixed encoding, or in the case of wrapped types, taking
21
23
  * a number and making the compact representation thereof
22
24
  */
25
+
26
+
23
27
  class Compact {
24
28
  #Type;
25
29
  #raw;
26
30
 
27
31
  constructor(registry, Type) {
28
32
  let value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
33
+ let {
34
+ definition,
35
+ setDefinition = noopSetDefinition
36
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
29
37
  this.registry = registry;
30
- this.#Type = (0, _utils.typeToConstructor)(registry, Type);
38
+ this.#Type = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
31
39
  const [raw, decodedLength] = Compact.decodeCompact(registry, this.#Type, value);
32
40
  this.initialU8aLength = decodedLength;
33
41
  this.#raw = raw;
34
42
  }
35
43
 
36
44
  static with(Type) {
45
+ let definition; // eslint-disable-next-line no-return-assign
46
+
47
+ const setDefinition = d => definition = d;
48
+
37
49
  return class extends Compact {
38
50
  constructor(registry, value) {
39
- super(registry, Type, value);
51
+ super(registry, Type, value, {
52
+ definition,
53
+ setDefinition
54
+ });
40
55
  }
41
56
 
42
57
  };
package/cjs/base/Enum.js CHANGED
@@ -13,6 +13,10 @@ var _Null = require("./Null");
13
13
 
14
14
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
15
15
  // SPDX-License-Identifier: Apache-2.0
16
+ function noopSetDefinition(d) {
17
+ return d;
18
+ }
19
+
16
20
  function isRustEnum(def) {
17
21
  const defValues = Object.values(def);
18
22
 
@@ -40,12 +44,11 @@ function extractDef(registry, _def) {
40
44
  isBasic = true;
41
45
  isIndexed = false;
42
46
  } else if (isRustEnum(_def)) {
43
- const entries = Object.entries((0, _utils.mapToTypeMap)(registry, _def));
47
+ const [Types, keys] = (0, _utils.mapToTypeMap)(registry, _def);
44
48
 
45
- for (let i = 0; i < entries.length; i++) {
46
- const [key, Type] = entries[i];
47
- def[key] = {
48
- Type,
49
+ for (let i = 0; i < keys.length; i++) {
50
+ def[keys[i]] = {
51
+ Type: Types[i],
49
52
  index: i
50
53
  };
51
54
  }
@@ -147,11 +150,15 @@ class Enum {
147
150
  #raw;
148
151
 
149
152
  constructor(registry, Types, value, index) {
153
+ let {
154
+ definition,
155
+ setDefinition = noopSetDefinition
156
+ } = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
150
157
  const {
151
158
  def,
152
159
  isBasic,
153
160
  isIndexed
154
- } = extractDef(registry, Types); // shortcut isU8a as used in SCALE decoding
161
+ } = definition || setDefinition(extractDef(registry, Types)); // shortcut isU8a as used in SCALE decoding
155
162
 
156
163
  const decoded = (0, _util.isU8a)(value) && value.length && !(0, _util.isNumber)(index) ? createFromValue(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
157
164
  this.registry = registry;
@@ -183,9 +190,16 @@ class Enum {
183
190
  isKeys[i] = `is${name}`;
184
191
  }
185
192
 
193
+ let definition; // eslint-disable-next-line no-return-assign
194
+
195
+ const setDefinition = d => definition = d;
196
+
186
197
  return class extends Enum {
187
198
  constructor(registry, value, index) {
188
- super(registry, Types, value, index);
199
+ super(registry, Types, value, index, {
200
+ definition,
201
+ setDefinition
202
+ });
189
203
  (0, _util.objectProperties)(this, isKeys, (_, i) => this.type === keys[i]);
190
204
  (0, _util.objectProperties)(this, asKeys, (k, i) => {
191
205
  (0, _util.assert)(this[isKeys[i]], () => `Cannot convert '${this.type}' via ${k}`);
@@ -386,14 +400,15 @@ class Enum {
386
400
  }, {}) : this.defKeys;
387
401
  }
388
402
 
389
- const typeMap = Object.entries(this.#def).reduce((out, _ref3) => {
403
+ const entries = Object.entries(this.#def);
404
+ return (0, _utils.typesToMap)(this.registry, entries.reduce((out, _ref3, i) => {
390
405
  let [key, {
391
406
  Type
392
407
  }] = _ref3;
393
- out[key] = Type;
408
+ out[0][i] = Type;
409
+ out[1][i] = key;
394
410
  return out;
395
- }, {});
396
- return (0, _utils.typesToMap)(this.registry, typeMap);
411
+ }, [new Array(entries.length), new Array(entries.length)]));
397
412
  }
398
413
  /**
399
414
  * @description Returns the base runtime type name for this instance
@@ -420,7 +435,7 @@ class Enum {
420
435
 
421
436
 
422
437
  toU8a(isBare) {
423
- return (0, _util.u8aConcat)(new Uint8Array(isBare ? [] : [this.index]), this.#raw.toU8a(isBare));
438
+ return isBare ? this.#raw.toU8a(isBare) : (0, _util.u8aConcatStrict)([new Uint8Array([this.index]), this.#raw.toU8a(isBare)]);
424
439
  }
425
440
 
426
441
  }
@@ -13,6 +13,10 @@ var _Null = require("./Null");
13
13
 
14
14
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
15
15
  // SPDX-License-Identifier: Apache-2.0
16
+ function noopSetDefinition(d) {
17
+ return d;
18
+ }
19
+
16
20
  class None extends _Null.Null {
17
21
  /**
18
22
  * @description Returns the base runtime type name for this instance
@@ -60,7 +64,11 @@ class Option {
60
64
  #raw;
61
65
 
62
66
  constructor(registry, typeName, value) {
63
- const Type = (0, _utils.typeToConstructor)(registry, typeName);
67
+ let {
68
+ definition,
69
+ setDefinition = noopSetDefinition
70
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
71
+ const Type = definition || setDefinition((0, _utils.typeToConstructor)(registry, typeName));
64
72
  const decoded = (0, _util.isU8a)(value) && value.length && !(0, _util.isCodec)(value) ? value[0] === 0 ? new None(registry) : new Type(registry, value.subarray(1)) : decodeOption(registry, Type, value);
65
73
  this.registry = registry;
66
74
  this.#Type = Type;
@@ -72,9 +80,19 @@ class Option {
72
80
  }
73
81
 
74
82
  static with(Type) {
83
+ let definition;
84
+
85
+ const setDefinition = d => {
86
+ definition = d;
87
+ return d;
88
+ };
89
+
75
90
  return class extends Option {
76
91
  constructor(registry, value) {
77
- super(registry, Type, value);
92
+ super(registry, Type, value, {
93
+ definition,
94
+ setDefinition
95
+ });
78
96
  }
79
97
 
80
98
  };
package/cjs/base/Tuple.js CHANGED
@@ -13,27 +13,29 @@ var _utils = require("../utils");
13
13
 
14
14
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
15
15
  // SPDX-License-Identifier: Apache-2.0
16
-
16
+ function noopSetDefinition(d) {
17
+ return d;
18
+ }
17
19
  /** @internal */
18
- function decodeTuple(registry, Classes, value) {
20
+
21
+
22
+ function decodeTuple(registry, result, Classes, value) {
19
23
  if ((0, _util.isU8a)(value) || (0, _util.isHex)(value)) {
20
- return (0, _utils.decodeU8a)(registry, (0, _util.u8aToU8a)(value), Classes);
24
+ return (0, _utils.decodeU8a)(registry, result, (0, _util.u8aToU8a)(value), Classes);
21
25
  }
22
26
 
23
- const Types = Array.isArray(Classes) ? Classes : Object.values(Classes);
24
- return [Types.map((Type, index) => {
25
- try {
26
- const entry = value === null || value === void 0 ? void 0 : value[index];
27
-
28
- if (entry instanceof Type) {
29
- return entry;
30
- }
27
+ const Types = Classes[0];
31
28
 
32
- return new Type(registry, entry);
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);
33
33
  } catch (error) {
34
- throw new Error(`Tuple: failed on ${index}:: ${error.message}`);
34
+ throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
35
35
  }
36
- }), 0];
36
+ }
37
+
38
+ return [result, 0];
37
39
  }
38
40
  /**
39
41
  * @name Tuple
@@ -47,16 +49,28 @@ class Tuple extends _AbstractArray.AbstractArray {
47
49
  #Types;
48
50
 
49
51
  constructor(registry, Types, value) {
50
- const Classes = 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);
51
- const [values, decodedLength] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8a)(registry, value, Classes) : decodeTuple(registry, Classes, value);
52
- super(registry, values, decodedLength);
52
+ let {
53
+ definition,
54
+ setDefinition = noopSetDefinition
55
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
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;
53
60
  this.#Types = Classes;
54
61
  }
55
62
 
56
63
  static with(Types) {
64
+ let definition; // eslint-disable-next-line no-return-assign
65
+
66
+ const setDefinition = d => definition = d;
67
+
57
68
  return class extends Tuple {
58
69
  constructor(registry, value) {
59
- super(registry, Types, value);
70
+ super(registry, Types, value, {
71
+ definition,
72
+ setDefinition
73
+ });
60
74
  }
61
75
 
62
76
  };
@@ -81,7 +95,7 @@ class Tuple extends _AbstractArray.AbstractArray {
81
95
 
82
96
 
83
97
  get Types() {
84
- return Array.isArray(this.#Types) ? this.#Types.map(T => new T(this.registry).toRawType()) : Object.keys(this.#Types);
98
+ return this.#Types[1].length ? this.#Types[1] : this.#Types[0].map(T => new T(this.registry).toRawType());
85
99
  }
86
100
  /**
87
101
  * @description Returns a breakdown of the hex encoding for this Codec
@@ -99,7 +113,7 @@ class Tuple extends _AbstractArray.AbstractArray {
99
113
 
100
114
 
101
115
  toRawType() {
102
- const types = (Array.isArray(this.#Types) ? this.#Types : Object.values(this.#Types)).map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
116
+ const types = this.#Types[0].map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
103
117
  return `(${types.join(',')})`;
104
118
  }
105
119
  /**
@@ -118,7 +132,7 @@ class Tuple extends _AbstractArray.AbstractArray {
118
132
 
119
133
 
120
134
  toU8a(isBare) {
121
- return (0, _util.u8aConcat)(...this.toU8aInner(isBare));
135
+ return (0, _util.u8aConcatStrict)(this.toU8aInner(isBare));
122
136
  }
123
137
 
124
138
  }
package/cjs/base/Vec.js CHANGED
@@ -17,13 +17,26 @@ var _utils = require("../utils");
17
17
  const MAX_LENGTH = 64 * 1024;
18
18
  const l = (0, _util.logger)('Vec');
19
19
 
20
- function decodeVec(registry, Type, value) {
21
- let length = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : -1;
20
+ function noopSetDefinition(d) {
21
+ return d;
22
+ }
23
+
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.compactFromU8aLim)(u8a);
31
+ (0, _util.assert)(length <= MAX_LENGTH, () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
32
+ return [u8a, length, startAt];
33
+ }
22
34
 
35
+ function decodeVec(registry, result, value, startAt, Type) {
23
36
  if (Array.isArray(value)) {
24
- const result = new Array(value.length);
37
+ const count = result.length;
25
38
 
26
- for (let i = 0; i < value.length; i++) {
39
+ for (let i = 0; i < count; i++) {
27
40
  const entry = value[i];
28
41
 
29
42
  try {
@@ -34,20 +47,10 @@ function decodeVec(registry, Type, value) {
34
47
  }
35
48
  }
36
49
 
37
- return [result, 0, 0];
50
+ return [0, 0];
38
51
  }
39
52
 
40
- const u8a = (0, _util.u8aToU8a)(value);
41
- let offset = 0;
42
-
43
- if (length === -1) {
44
- const [_offset, _length] = (0, _util.compactFromU8a)(u8a);
45
- (0, _util.assert)(_length.lten(MAX_LENGTH), () => `Vec length ${_length.toString()} exceeds ${MAX_LENGTH}`);
46
- length = _length.toNumber();
47
- offset = _offset;
48
- }
49
-
50
- return (0, _utils.decodeU8aVec)(registry, u8a, offset, Type, length);
53
+ return (0, _utils.decodeU8aVec)(registry, result, (0, _util.u8aToU8a)(value), startAt, Type);
51
54
  }
52
55
  /**
53
56
  * @name Vec
@@ -63,16 +66,28 @@ class Vec extends _AbstractArray.AbstractArray {
63
66
 
64
67
  constructor(registry, Type) {
65
68
  let value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
66
- const Clazz = (0, _utils.typeToConstructor)(registry, Type);
67
- const [values, decodedLength] = decodeVec(registry, Clazz, value);
68
- super(registry, values, decodedLength);
69
- this.#Type = Clazz;
69
+ let {
70
+ definition,
71
+ setDefinition = noopSetDefinition
72
+ } = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
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;
70
78
  }
71
79
 
72
80
  static with(Type) {
81
+ let definition; // eslint-disable-next-line no-return-assign
82
+
83
+ const setDefinition = d => definition = d;
84
+
73
85
  return class extends Vec {
74
86
  constructor(registry, value) {
75
- super(registry, Type, value);
87
+ super(registry, Type, value, {
88
+ definition,
89
+ setDefinition
90
+ });
76
91
  }
77
92
 
78
93
  };
@@ -15,17 +15,8 @@ var _Vec = require("./Vec");
15
15
 
16
16
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
17
17
  // SPDX-License-Identifier: Apache-2.0
18
-
19
- /** @internal */
20
- function decodeVecFixed(registry, value, Type, length) {
21
- const [values, decodedLength, decodedLengthNoOffset] = (0, _Vec.decodeVec)(registry, Type, value, length);
22
-
23
- while (values.length < length) {
24
- values.push(new Type(registry));
25
- }
26
-
27
- (0, _util.assert)(values.length === length, () => `Expected a length of exactly ${length} entries`);
28
- return [values, decodedLength, decodedLengthNoOffset];
18
+ function noopSetDefinition(d) {
19
+ return d;
29
20
  }
30
21
  /**
31
22
  * @name VecFixed
@@ -39,16 +30,27 @@ class VecFixed extends _AbstractArray.AbstractArray {
39
30
 
40
31
  constructor(registry, Type, length) {
41
32
  let value = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
42
- const Clazz = (0, _utils.typeToConstructor)(registry, Type);
43
- const [values,, decodedLengthNoOffset] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8aVec)(registry, value, 0, Clazz, length) : decodeVecFixed(registry, value, Clazz, length);
44
- super(registry, values, decodedLengthNoOffset);
45
- this.#Type = Clazz;
33
+ let {
34
+ definition,
35
+ setDefinition = noopSetDefinition
36
+ } = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
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;
46
41
  }
47
42
 
48
43
  static with(Type, length) {
44
+ let definition; // eslint-disable-next-line no-return-assign
45
+
46
+ const setDefinition = d => definition = d;
47
+
49
48
  return class extends VecFixed {
50
49
  constructor(registry, value) {
51
- super(registry, Type, length, value);
50
+ super(registry, Type, length, value, {
51
+ definition,
52
+ setDefinition
53
+ });
52
54
  }
53
55
 
54
56
  };
@@ -90,7 +92,7 @@ class VecFixed extends _AbstractArray.AbstractArray {
90
92
  // we override, we don't add the length prefix for ourselves, and at the same time we
91
93
  // ignore isBare on entries, since they should be properly encoded at all times
92
94
  const encoded = this.toU8aInner();
93
- return encoded.length ? (0, _util.u8aConcat)(...encoded) : new Uint8Array([]);
95
+ return encoded.length ? (0, _util.u8aConcatStrict)(encoded) : new Uint8Array([]);
94
96
  }
95
97
  /**
96
98
  * @description Returns the base runtime type name for this instance
@@ -16,11 +16,12 @@ const l = (0, _util.logger)('BTreeSet');
16
16
 
17
17
  function decodeSetFromU8a(registry, ValClass, u8a) {
18
18
  const output = new Set();
19
- const [offset, length] = (0, _util.compactFromU8a)(u8a);
20
- const [values, decodedLength] = (0, _utils.decodeU8aVec)(registry, u8a, offset, ValClass, length.toNumber());
19
+ const [offset, count] = (0, _util.compactFromU8aLim)(u8a);
20
+ const result = new Array(count);
21
+ const [decodedLength] = (0, _utils.decodeU8aVec)(registry, result, u8a, offset, ValClass);
21
22
 
22
- for (let i = 0; i < values.length; i++) {
23
- output.add(values[i]);
23
+ for (let i = 0; i < count; i++) {
24
+ output.add(result[i]);
24
25
  }
25
26
 
26
27
  return [ValClass, output, decodedLength];
@@ -221,7 +222,7 @@ class BTreeSet extends Set {
221
222
  encoded.push(v.toU8a(isBare));
222
223
  }
223
224
 
224
- return (0, _util.u8aConcat)(...encoded);
225
+ return (0, _util.u8aConcatStrict)(encoded);
225
226
  }
226
227
 
227
228
  }
@@ -19,10 +19,10 @@ function decodeBitVecU8a(value) {
19
19
  } // handle all other Uint8Array inputs, these do have a length prefix which is the number of bits encoded
20
20
 
21
21
 
22
- const [offset, length] = (0, _util.compactFromU8a)(value);
23
- const total = offset + Math.ceil(length.toNumber() / 8);
22
+ const [offset, length] = (0, _util.compactFromU8aLim)(value);
23
+ const total = offset + Math.ceil(length / 8);
24
24
  (0, _util.assert)(total <= value.length, () => `BitVec: required length less than remainder, expected at least ${total}, found ${value.length}`);
25
- return [length.toNumber(), value.subarray(offset, total)];
25
+ return [length, value.subarray(offset, total)];
26
26
  }
27
27
  /** @internal */
28
28
 
@@ -94,7 +94,7 @@ class BitVec extends _Raw.Raw {
94
94
 
95
95
  toU8a(isBare) {
96
96
  const bitVec = super.toU8a();
97
- return isBare ? bitVec : (0, _util.u8aConcat)((0, _util.compactToU8a)(this.#decodedLength), bitVec);
97
+ return isBare ? bitVec : (0, _util.u8aConcatStrict)([(0, _util.compactToU8a)(this.#decodedLength), bitVec]);
98
98
  }
99
99
 
100
100
  }
@@ -21,9 +21,9 @@ function decodeBytesU8a(value) {
21
21
  } // handle all other Uint8Array inputs, these do have a length prefix
22
22
 
23
23
 
24
- const [offset, length] = (0, _util.compactFromU8a)(value);
25
- const total = offset + length.toNumber();
26
- (0, _util.assert)(length.lten(MAX_LENGTH), () => `Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
24
+ const [offset, length] = (0, _util.compactFromU8aLim)(value);
25
+ const total = offset + length;
26
+ (0, _util.assert)(length <= MAX_LENGTH, () => `Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
27
27
  (0, _util.assert)(total <= value.length, () => `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
28
28
  return [value.subarray(offset, total), total];
29
29
  }
@@ -22,15 +22,14 @@ const l = (0, _util.logger)('Map');
22
22
 
23
23
  function decodeMapFromU8a(registry, KeyClass, ValClass, u8a) {
24
24
  const output = new Map();
25
- const [offset, length] = (0, _util.compactFromU8a)(u8a);
26
- const count = length.toNumber();
25
+ const [offset, count] = (0, _util.compactFromU8aLim)(u8a);
27
26
  const types = [];
28
27
 
29
28
  for (let i = 0; i < count; i++) {
30
29
  types.push(KeyClass, ValClass);
31
30
  }
32
31
 
33
- const [values, decodedLength] = (0, _utils.decodeU8a)(registry, u8a.subarray(offset), types);
32
+ const [values, decodedLength] = (0, _utils.decodeU8a)(registry, new Array(types.length), u8a.subarray(offset), [types, []]);
34
33
 
35
34
  for (let i = 0; i < values.length; i += 2) {
36
35
  output.set(values[i], values[i + 1]);
@@ -231,7 +230,7 @@ class CodecMap extends Map {
231
230
  encoded.push(k.toU8a(isBare), v.toU8a(isBare));
232
231
  }
233
232
 
234
- return (0, _util.u8aConcat)(...encoded);
233
+ return (0, _util.u8aConcatStrict)(encoded);
235
234
  }
236
235
 
237
236
  }
@@ -11,21 +11,25 @@ var _utils = require("../utils");
11
11
 
12
12
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
13
13
  // SPDX-License-Identifier: Apache-2.0
14
-
14
+ function noopSetDefinition(d) {
15
+ return d;
16
+ }
15
17
  /** @internal */
16
- function decodeStructFromObject(registry, Types, value, jsonMap) {
18
+
19
+
20
+ function decodeStructFromObject(registry, _ref, value, jsonMap) {
21
+ let [Types, keys] = _ref;
17
22
  let jsonObj;
18
- const inputKeys = Object.keys(Types);
19
23
  const typeofArray = Array.isArray(value);
20
24
  const typeofMap = value instanceof Map;
21
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`);
22
- (0, _util.assert)(!typeofArray || value.length === inputKeys.length, () => `Struct: Unable to map ${(0, _util.stringify)(value)} array to object with known keys ${inputKeys.join(', ')}`);
23
- const raw = new Array(inputKeys.length);
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);
24
28
 
25
- for (let i = 0; i < inputKeys.length; i++) {
26
- const key = inputKeys[i];
29
+ for (let i = 0; i < keys.length; i++) {
30
+ const key = keys[i];
27
31
  const jsonKey = jsonMap.get(key) || key;
28
- const Type = Types[key];
32
+ const Type = Types[i];
29
33
  let assign;
30
34
 
31
35
  try {
@@ -84,8 +88,12 @@ class Struct extends Map {
84
88
 
85
89
  constructor(registry, Types, value) {
86
90
  let jsonMap = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new Map();
87
- const typeMap = (0, _utils.mapToTypeMap)(registry, Types);
88
- const [decoded, decodedLength] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8a)(registry, value, typeMap, true) : (0, _util.isHex)(value) ? (0, _utils.decodeU8a)(registry, (0, _util.u8aToU8a)(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
91
+ let {
92
+ definition,
93
+ setDefinition = noopSetDefinition
94
+ } = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
95
+ const typeMap = definition || setDefinition((0, _utils.mapToTypeMap)(registry, Types));
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);
89
97
  super(decoded);
90
98
  this.#registry = registry;
91
99
  this.initialU8aLength = decodedLength;
@@ -95,9 +103,16 @@ class Struct extends Map {
95
103
 
96
104
  static with(Types, jsonMap) {
97
105
  const keys = Object.keys(Types);
106
+ let definition; // eslint-disable-next-line no-return-assign
107
+
108
+ const setDefinition = d => definition = d;
109
+
98
110
  return class extends Struct {
99
111
  constructor(registry, value) {
100
- super(registry, Types, value, jsonMap);
112
+ super(registry, Types, value, jsonMap, {
113
+ definition,
114
+ setDefinition
115
+ });
101
116
  (0, _util.objectProperties)(this, keys, k => this.get(k));
102
117
  }
103
118
 
@@ -109,7 +124,7 @@ class Struct extends Map {
109
124
 
110
125
 
111
126
  get defKeys() {
112
- return Object.keys(this.#Types);
127
+ return this.#Types[1];
113
128
  }
114
129
 
115
130
  getT(key) {
@@ -136,11 +151,10 @@ class Struct extends Map {
136
151
 
137
152
  get Type() {
138
153
  const result = {};
139
- const defs = Object.entries(this.#Types);
154
+ const [Types, keys] = this.#Types;
140
155
 
141
- for (let i = 0; i < defs.length; i++) {
142
- const [key, Type] = defs[i];
143
- 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();
144
158
  }
145
159
 
146
160
  return result;
@@ -290,7 +304,7 @@ class Struct extends Map {
290
304
  }
291
305
  }
292
306
 
293
- return (0, _util.u8aConcat)(...encoded);
307
+ return (0, _util.u8aConcatStrict)(encoded);
294
308
  }
295
309
 
296
310
  }
@@ -26,9 +26,9 @@ function decodeText(value) {
26
26
  return [(0, _util.u8aToString)(value), 0];
27
27
  }
28
28
 
29
- const [offset, length] = (0, _util.compactFromU8a)(value);
30
- const total = offset + length.toNumber();
31
- (0, _util.assert)(length.lten(MAX_LENGTH), () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
29
+ const [offset, length] = (0, _util.compactFromU8aLim)(value);
30
+ const total = offset + length;
31
+ (0, _util.assert)(length <= MAX_LENGTH, () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
32
32
  (0, _util.assert)(total <= value.length, () => `Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
33
33
  return [(0, _util.u8aToString)(value.subarray(offset, total)), total];
34
34
  } else if ((0, _util.isHex)(value)) {