@polkadot/types-codec 8.5.2-6 → 8.5.2-9

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.
@@ -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
- readonly initialU8aLength?: number;
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
  */
@@ -1,6 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { compactToU8a, u8aConcat, u8aToHex } from '@polkadot/util';
3
+ import { compactToU8a, u8aConcatStrict, u8aToHex } from '@polkadot/util';
4
4
  import { compareArray } from "../utils/compareArray.js";
5
5
  /**
6
6
  * @name AbstractArray
@@ -11,15 +11,9 @@ import { compareArray } from "../utils/compareArray.js";
11
11
  */
12
12
 
13
13
  export class AbstractArray extends Array {
14
- constructor(registry, values, initialU8aLength) {
15
- super(values.length); // explicitly set the values here - this removes the need for any extra allocations
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
@@ -160,7 +154,7 @@ export class AbstractArray extends Array {
160
154
 
161
155
  toU8a(isBare) {
162
156
  const encoded = this.toU8aInner();
163
- return isBare ? u8aConcat(...encoded) : u8aConcat(compactToU8a(this.length), ...encoded);
157
+ return isBare ? u8aConcatStrict(encoded) : u8aConcatStrict([compactToU8a(this.length), ...encoded]);
164
158
  }
165
159
 
166
160
  toU8aInner(isBare) {
package/base/Enum.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, isCodec, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
3
+ import { assert, isCodec, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
4
4
  import { mapToTypeMap, typesToMap } from "../utils/index.js";
5
5
  import { Null } from "./Null.js"; // export interface, this is used in Enum.with, so required as public by TS
6
6
 
@@ -35,12 +35,11 @@ function extractDef(registry, _def) {
35
35
  isBasic = true;
36
36
  isIndexed = false;
37
37
  } else if (isRustEnum(_def)) {
38
- const entries = Object.entries(mapToTypeMap(registry, _def));
38
+ const [Types, keys] = mapToTypeMap(registry, _def);
39
39
 
40
- for (let i = 0; i < entries.length; i++) {
41
- const [key, Type] = entries[i];
42
- def[key] = {
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 typeMap = Object.entries(this.#def).reduce((out, [key, {
385
+ const entries = Object.entries(this.#def);
386
+ return typesToMap(this.registry, entries.reduce((out, [key, {
387
387
  Type
388
- }]) => {
389
- out[key] = Type;
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
@@ -416,7 +416,7 @@ export class Enum {
416
416
 
417
417
 
418
418
  toU8a(isBare) {
419
- return u8aConcat(new Uint8Array(isBare ? [] : [this.index]), this.#raw.toU8a(isBare));
419
+ return isBare ? this.#raw.toU8a(isBare) : u8aConcatStrict([new Uint8Array([this.index]), this.#raw.toU8a(isBare)]);
420
420
  }
421
421
 
422
422
  }
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?: TupleCodecClass;
12
- setDefinition?: (d: TupleCodecClass) => TupleCodecClass;
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
@@ -1,6 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { isFunction, isHex, isString, isU8a, stringify, u8aConcat, u8aToU8a } from '@polkadot/util';
3
+ import { isFunction, isHex, isString, isU8a, stringify, u8aConcatStrict, u8aToU8a } from '@polkadot/util';
4
4
  import { AbstractArray } from "../abstract/AbstractArray.js";
5
5
  import { decodeU8a, mapToTypeMap, typeToConstructor } from "../utils/index.js";
6
6
 
@@ -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 = Array.isArray(Classes) ? Classes : Object.values(Classes);
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
- return new Type(registry, entry);
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 ${index}:: ${error.message}`);
25
+ throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
30
26
  }
31
- }), 0];
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
- const [values, decodedLength] = isU8a(value) ? decodeU8a(registry, value, Classes) : decodeTuple(registry, Classes, value);
50
- super(registry, values, decodedLength);
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 Array.isArray(this.#Types) ? this.#Types.map(T => new T(this.registry).toRawType()) : Object.keys(this.#Types);
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 = (Array.isArray(this.#Types) ? this.#Types : Object.values(this.#Types)).map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
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
  /**
@@ -123,7 +122,7 @@ export class Tuple extends AbstractArray {
123
122
 
124
123
 
125
124
  toU8a(isBare) {
126
- return u8aConcat(...this.toU8aInner(isBare));
125
+ return u8aConcatStrict(this.toU8aInner(isBare));
127
126
  }
128
127
 
129
128
  }
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, Type: CodecClass<T>, value: Uint8Array | HexString | unknown[], length?: number): [T[], number, number];
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, compactFromU8aLim, 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
- export function decodeVec(registry, Type, value, length = -1) {
13
+ function decodeVecLength(value) {
14
14
  if (Array.isArray(value)) {
15
- const result = new Array(value.length);
15
+ return [value, value.length, 0];
16
+ }
17
+
18
+ const u8a = u8aToU8a(value);
19
+ const [startAt, length] = compactFromU8aLim(u8a);
20
+ assert(length <= MAX_LENGTH, () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
21
+ return [u8a, length, 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 < value.length; 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 [result, 0, 0];
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, u8a, offset, Type, length);
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 Clazz = definition || setDefinition(typeToConstructor(registry, Type));
59
- const [values, decodedLength] = decodeVec(registry, Clazz, value);
60
- super(registry, values, decodedLength);
61
- this.#Type = Clazz;
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) {
@@ -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 { assert, isU8a, u8aConcat } from '@polkadot/util';
3
+ import { isU8a, u8aConcatStrict } 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
- const Clazz = definition || setDefinition(typeToConstructor(registry, Type));
39
- const [values,, decodedLengthNoOffset] = isU8a(value) ? decodeU8aVec(registry, value, 0, Clazz, length) : decodeVecFixed(registry, value, Clazz, length);
40
- super(registry, values, decodedLengthNoOffset);
41
- this.#Type = Clazz;
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) {
@@ -93,7 +80,7 @@ export class VecFixed extends AbstractArray {
93
80
  // we override, we don't add the length prefix for ourselves, and at the same time we
94
81
  // ignore isBare on entries, since they should be properly encoded at all times
95
82
  const encoded = this.toU8aInner();
96
- return encoded.length ? u8aConcat(...encoded) : new Uint8Array([]);
83
+ return encoded.length ? u8aConcatStrict(encoded) : new Uint8Array([]);
97
84
  }
98
85
  /**
99
86
  * @description Returns the base runtime type name for this instance
@@ -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) {
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 entries = Object.entries((0, _utils.mapToTypeMap)(registry, _def));
47
+ const [Types, keys] = (0, _utils.mapToTypeMap)(registry, _def);
48
48
 
49
- for (let i = 0; i < entries.length; i++) {
50
- const [key, Type] = entries[i];
51
- def[key] = {
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 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) => {
405
405
  let [key, {
406
406
  Type
407
407
  }] = _ref3;
408
- out[key] = Type;
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
@@ -435,7 +435,7 @@ class Enum {
435
435
 
436
436
 
437
437
  toU8a(isBare) {
438
- 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)]);
439
439
  }
440
440
 
441
441
  }
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 = Array.isArray(Classes) ? Classes : Object.values(Classes);
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
- 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);
37
33
  } catch (error) {
38
- throw new Error(`Tuple: failed on ${index}:: ${error.message}`);
34
+ throw new Error(`Tuple: failed on ${i}:: ${error.message}`);
39
35
  }
40
- }), 0];
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
- const [values, decodedLength] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8a)(registry, value, Classes) : decodeTuple(registry, Classes, value);
60
- super(registry, values, decodedLength);
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 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());
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 = (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());
118
117
  return `(${types.join(',')})`;
119
118
  }
120
119
  /**
@@ -133,7 +132,7 @@ class Tuple extends _AbstractArray.AbstractArray {
133
132
 
134
133
 
135
134
  toU8a(isBare) {
136
- return (0, _util.u8aConcat)(...this.toU8aInner(isBare));
135
+ return (0, _util.u8aConcatStrict)(this.toU8aInner(isBare));
137
136
  }
138
137
 
139
138
  }
package/cjs/base/Vec.js CHANGED
@@ -21,13 +21,22 @@ function noopSetDefinition(d) {
21
21
  return d;
22
22
  }
23
23
 
24
- function decodeVec(registry, Type, value) {
25
- let length = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : -1;
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
+ }
26
34
 
35
+ function decodeVec(registry, result, value, startAt, Type) {
27
36
  if (Array.isArray(value)) {
28
- const result = new Array(value.length);
37
+ const count = result.length;
29
38
 
30
- for (let i = 0; i < value.length; 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 [result, 0, 0];
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, u8a, offset, Type, length);
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 Clazz = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
75
- const [values, decodedLength] = decodeVec(registry, Clazz, value);
76
- super(registry, values, decodedLength);
77
- this.#Type = Clazz;
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) {
@@ -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
- const Clazz = definition || setDefinition((0, _utils.typeToConstructor)(registry, Type));
51
- const [values,, decodedLengthNoOffset] = (0, _util.isU8a)(value) ? (0, _utils.decodeU8aVec)(registry, value, 0, Clazz, length) : decodeVecFixed(registry, value, Clazz, length);
52
- super(registry, values, decodedLengthNoOffset);
53
- this.#Type = Clazz;
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) {
@@ -105,7 +92,7 @@ class VecFixed extends _AbstractArray.AbstractArray {
105
92
  // we override, we don't add the length prefix for ourselves, and at the same time we
106
93
  // ignore isBare on entries, since they should be properly encoded at all times
107
94
  const encoded = this.toU8aInner();
108
- return encoded.length ? (0, _util.u8aConcat)(...encoded) : new Uint8Array([]);
95
+ return encoded.length ? (0, _util.u8aConcatStrict)(encoded) : new Uint8Array([]);
109
96
  }
110
97
  /**
111
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
  }