@polkadot/types-codec 8.8.3-1 → 8.8.3-12

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 (84) hide show
  1. package/abstract/{AbstractArray.d.ts → Array.d.ts} +0 -0
  2. package/abstract/{AbstractArray.js → Array.js} +0 -0
  3. package/{base → abstract}/Base.d.ts +2 -2
  4. package/{base → abstract}/Base.js +2 -7
  5. package/abstract/{AbstractInt.d.ts → Int.d.ts} +1 -4
  6. package/abstract/{AbstractInt.js → Int.js} +23 -20
  7. package/abstract/index.d.ts +3 -2
  8. package/abstract/index.js +3 -2
  9. package/base/Compact.d.ts +0 -2
  10. package/base/Compact.js +15 -16
  11. package/base/Enum.js +23 -10
  12. package/base/Int.d.ts +1 -1
  13. package/base/Int.js +1 -1
  14. package/base/Null.d.ts +2 -8
  15. package/base/Null.js +2 -16
  16. package/base/Option.d.ts +1 -4
  17. package/base/Option.js +6 -12
  18. package/base/Result.js +8 -3
  19. package/base/Tuple.d.ts +1 -1
  20. package/base/Tuple.js +3 -4
  21. package/base/UInt.d.ts +1 -1
  22. package/base/UInt.js +1 -1
  23. package/base/Vec.d.ts +1 -1
  24. package/base/Vec.js +8 -5
  25. package/base/VecAny.d.ts +1 -1
  26. package/base/VecAny.js +1 -1
  27. package/base/VecFixed.d.ts +1 -1
  28. package/base/VecFixed.js +2 -3
  29. package/base/index.d.ts +0 -1
  30. package/base/index.js +0 -1
  31. package/cjs/abstract/{AbstractArray.js → Array.js} +0 -0
  32. package/cjs/{base → abstract}/Base.js +4 -9
  33. package/cjs/abstract/{AbstractInt.js → Int.js} +22 -19
  34. package/cjs/abstract/index.js +12 -4
  35. package/cjs/base/Compact.js +15 -16
  36. package/cjs/base/Enum.js +22 -9
  37. package/cjs/base/Int.js +2 -2
  38. package/cjs/base/Null.js +2 -16
  39. package/cjs/base/Option.js +5 -11
  40. package/cjs/base/Result.js +8 -4
  41. package/cjs/base/Tuple.js +4 -5
  42. package/cjs/base/UInt.js +2 -2
  43. package/cjs/base/Vec.js +8 -5
  44. package/cjs/base/VecAny.js +2 -2
  45. package/cjs/base/VecFixed.js +3 -4
  46. package/cjs/base/index.js +0 -8
  47. package/cjs/extended/BitVec.js +5 -1
  48. package/cjs/extended/Bytes.js +8 -3
  49. package/cjs/extended/Map.js +2 -2
  50. package/cjs/extended/OptionBool.js +1 -8
  51. package/cjs/extended/U8aFixed.js +4 -1
  52. package/cjs/extended/WrapperKeepOpaque.js +5 -1
  53. package/cjs/native/Raw.js +4 -1
  54. package/cjs/native/Set.js +19 -4
  55. package/cjs/native/Struct.js +9 -9
  56. package/cjs/native/Text.js +8 -12
  57. package/cjs/packageInfo.js +1 -1
  58. package/cjs/utils/decodeU8a.js +63 -20
  59. package/cjs/utils/index.js +6 -0
  60. package/cjs/utils/mapToTypeMap.js +1 -1
  61. package/cjs/utils/sanitize.js +17 -37
  62. package/extended/BitVec.js +6 -2
  63. package/extended/Bytes.js +9 -4
  64. package/extended/Map.js +1 -1
  65. package/extended/OptionBool.d.ts +0 -4
  66. package/extended/OptionBool.js +1 -8
  67. package/extended/U8aFixed.js +5 -2
  68. package/extended/WrapperKeepOpaque.js +6 -2
  69. package/native/Raw.d.ts +1 -1
  70. package/native/Raw.js +5 -2
  71. package/native/Set.js +19 -5
  72. package/native/Struct.d.ts +1 -1
  73. package/native/Struct.js +11 -11
  74. package/native/Text.d.ts +2 -5
  75. package/native/Text.js +9 -13
  76. package/package.json +15 -15
  77. package/packageInfo.js +1 -1
  78. package/utils/decodeU8a.d.ts +16 -1
  79. package/utils/decodeU8a.js +57 -19
  80. package/utils/index.d.ts +1 -1
  81. package/utils/index.js +1 -1
  82. package/utils/mapToTypeMap.js +1 -1
  83. package/utils/sanitize.d.ts +3 -5
  84. package/utils/sanitize.js +17 -37
@@ -23,8 +23,13 @@ function decodeBytesU8a(value) {
23
23
 
24
24
  const [offset, length] = (0, _util.compactFromU8aLim)(value);
25
25
  const total = offset + length;
26
- (0, _util.assert)(length <= MAX_LENGTH, () => `Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
27
- (0, _util.assert)(total <= value.length, () => `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
26
+
27
+ if (length > MAX_LENGTH) {
28
+ throw new Error(`Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
29
+ } else if (total > value.length) {
30
+ throw new Error(`Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
31
+ }
32
+
28
33
  return [value.subarray(offset, total), total];
29
34
  }
30
35
  /**
@@ -38,7 +43,7 @@ function decodeBytesU8a(value) {
38
43
 
39
44
  class Bytes extends _Raw.Raw {
40
45
  constructor(registry, value) {
41
- const [u8a, decodedLength] = (0, _util.isU8a)(value) && !((0, _util.isCodec)(value) && value instanceof _Raw.Raw) ? decodeBytesU8a(value) : Array.isArray(value) || (0, _util.isString)(value) ? [(0, _util.u8aToU8a)(value), 0] : [value, 0];
46
+ const [u8a, decodedLength] = (0, _util.isU8a)(value) && !(value instanceof _Raw.Raw) ? decodeBytesU8a(value) : Array.isArray(value) || (0, _util.isString)(value) ? [(0, _util.u8aToU8a)(value), 0] : [value, 0];
42
47
  super(registry, u8a, decodedLength);
43
48
  }
44
49
  /**
@@ -7,7 +7,7 @@ exports.CodecMap = void 0;
7
7
 
8
8
  var _util = require("@polkadot/util");
9
9
 
10
- var _AbstractArray = require("../abstract/AbstractArray");
10
+ var _Array = require("../abstract/Array");
11
11
 
12
12
  var _Enum = require("../base/Enum");
13
13
 
@@ -44,7 +44,7 @@ function decodeMapFromMap(registry, KeyClass, ValClass, value) {
44
44
  const output = new Map();
45
45
 
46
46
  for (const [key, val] of value.entries()) {
47
- const isComplex = KeyClass.prototype instanceof _AbstractArray.AbstractArray || KeyClass.prototype instanceof _Struct.Struct || KeyClass.prototype instanceof _Enum.Enum;
47
+ const isComplex = KeyClass.prototype instanceof _Array.AbstractArray || KeyClass.prototype instanceof _Struct.Struct || KeyClass.prototype instanceof _Enum.Enum;
48
48
 
49
49
  try {
50
50
  output.set(key instanceof KeyClass ? key : new KeyClass(registry, isComplex ? JSON.parse(key) : key), val instanceof ValClass ? val : new ValClass(registry, val));
@@ -29,6 +29,7 @@ function decodeU8a(registry, value) {
29
29
  class OptionBool extends _Option.Option {
30
30
  constructor(registry, value) {
31
31
  super(registry, _Bool.bool, (0, _util.isU8a)(value) || (0, _util.isHex)(value) ? decodeU8a(registry, (0, _util.u8aToU8a)(value)) : value);
32
+ this.initialU8aLength = 1;
32
33
  }
33
34
  /**
34
35
  * @description The length of the value when encoded as a Uint8Array
@@ -38,14 +39,6 @@ class OptionBool extends _Option.Option {
38
39
  get encodedLength() {
39
40
  return 1;
40
41
  }
41
- /**
42
- * @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
43
- */
44
-
45
-
46
- get initialU8aLength() {
47
- return 1;
48
- }
49
42
  /**
50
43
  * @description Checks if the value is an empty value (always false)
51
44
  */
@@ -21,7 +21,10 @@ function decodeU8aFixed(value, bitLength) {
21
21
  return [new Uint8Array(byteLength), 0];
22
22
  }
23
23
 
24
- (0, _util.assert)((0, _util.isU8a)(value) ? u8a.length >= byteLength : u8a.length === byteLength, () => `Expected input with ${byteLength} bytes (${bitLength} bits), found ${u8a.length} bytes`);
24
+ if ((0, _util.isU8a)(value) ? u8a.length < byteLength : u8a.length !== byteLength) {
25
+ throw new Error(`Expected input with ${byteLength} bytes (${bitLength} bits), found ${u8a.length} bytes`);
26
+ }
27
+
25
28
  return [u8a.subarray(0, byteLength), byteLength];
26
29
  }
27
30
  /**
@@ -106,7 +106,11 @@ class WrapperKeepOpaque extends _Bytes.Bytes {
106
106
 
107
107
 
108
108
  unwrap() {
109
- return (0, _util.assertReturn)(this.#decoded, () => `${this.#opaqueName}: unwrapping an undecodable value`);
109
+ if (!this.#decoded) {
110
+ throw new Error(`${this.#opaqueName}: unwrapping an undecodable value`);
111
+ }
112
+
113
+ return this.#decoded;
110
114
  }
111
115
 
112
116
  }
package/cjs/native/Raw.js CHANGED
@@ -190,7 +190,10 @@ class Raw extends Uint8Array {
190
190
 
191
191
 
192
192
  toUtf8() {
193
- (0, _util.assert)(this.isUtf8, 'The character sequence is not a valid Utf8 string');
193
+ if (!this.isUtf8) {
194
+ throw new Error('The character sequence is not a valid Utf8 string');
195
+ }
196
+
194
197
  return (0, _util.u8aToString)(this);
195
198
  }
196
199
 
package/cjs/native/Set.js CHANGED
@@ -28,7 +28,11 @@ function decodeSetArray(setValues, values) {
28
28
 
29
29
  for (let i = 0; i < values.length; i++) {
30
30
  const key = values[i];
31
- (0, _util.assert)(!(0, _util.isUndefined)(setValues[key]), () => `Set: Invalid key '${key}' passed to Set, allowed ${Object.keys(setValues).join(', ')}`);
31
+
32
+ if ((0, _util.isUndefined)(setValues[key])) {
33
+ throw new Error(`Set: Invalid key '${key}' passed to Set, allowed ${Object.keys(setValues).join(', ')}`);
34
+ }
35
+
32
36
  result[i] = key;
33
37
  }
34
38
 
@@ -51,7 +55,11 @@ function decodeSetNumber(setValues, _value) {
51
55
  }
52
56
 
53
57
  const computed = encodeSet(setValues, result);
54
- (0, _util.assert)(bn.eq(computed), () => `Set: Mismatch decoding '${bn.toString()}', computed as '${computed.toString()}' with ${result.join(', ')}`);
58
+
59
+ if (!bn.eq(computed)) {
60
+ throw new Error(`Set: Mismatch decoding '${bn.toString()}', computed as '${computed.toString()}' with ${result.join(', ')}`);
61
+ }
62
+
55
63
  return result;
56
64
  }
57
65
  /** @internal */
@@ -60,7 +68,11 @@ function decodeSetNumber(setValues, _value) {
60
68
  function decodeSet(setValues) {
61
69
  let value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 0;
62
70
  let bitLength = arguments.length > 2 ? arguments[2] : undefined;
63
- (0, _util.assert)(bitLength % 8 === 0, () => `Expected valid bitLength, power of 8, found ${bitLength}`);
71
+
72
+ if (bitLength % 8 !== 0) {
73
+ throw new Error(`Expected valid bitLength, power of 8, found ${bitLength}`);
74
+ }
75
+
64
76
  const byteLength = bitLength / 8;
65
77
 
66
78
  if ((0, _util.isU8a)(value)) {
@@ -161,7 +173,10 @@ class CodecSet extends Set {
161
173
  // ^^^ add = () property done to assign this instance's this, otherwise Set.add creates "some" chaos
162
174
  // we have the isUndefined(this._setValues) in here as well, add is used internally
163
175
  // in the Set constructor (so it is undefined at this point, and should allow)
164
- (0, _util.assert)((0, _util.isUndefined)(this.#allowed) || !(0, _util.isUndefined)(this.#allowed[key]), () => `Set: Invalid key '${key}' on add`);
176
+ if (this.#allowed && (0, _util.isUndefined)(this.#allowed[key])) {
177
+ throw new Error(`Set: Invalid key '${key}' on add`);
178
+ }
179
+
165
180
  super.add(key);
166
181
  return this;
167
182
  };
@@ -22,8 +22,13 @@ function decodeStructFromObject(registry, _ref, value, jsonMap) {
22
22
  let jsonObj;
23
23
  const typeofArray = Array.isArray(value);
24
24
  const typeofMap = value instanceof Map;
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 === keys.length, () => `Struct: Unable to map ${(0, _util.stringify)(value)} array to object with known keys ${keys.join(', ')}`);
25
+
26
+ if (!typeofArray && !typeofMap && !(0, _util.isObject)(value)) {
27
+ throw new Error(`Struct: Cannot decode value ${(0, _util.stringify)(value)} (typeof ${typeof value}), expected an input object, map or array`);
28
+ } else if (typeofArray && value.length !== keys.length) {
29
+ throw new Error(`Struct: Unable to map ${(0, _util.stringify)(value)} array to object with known keys ${keys.join(', ')}`);
30
+ }
31
+
27
32
  const raw = new Array(keys.length);
28
33
 
29
34
  for (let i = 0; i < keys.length; i++) {
@@ -82,7 +87,6 @@ function decodeStructFromObject(registry, _ref, value, jsonMap) {
82
87
 
83
88
 
84
89
  class Struct extends Map {
85
- #registry;
86
90
  #jsonMap;
87
91
  #Types;
88
92
 
@@ -93,10 +97,10 @@ class Struct extends Map {
93
97
  setDefinition = noopSetDefinition
94
98
  } = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : {};
95
99
  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);
100
+ const [decoded, decodedLength] = (0, _util.isU8a)(value) || (0, _util.isHex)(value) ? (0, _utils.decodeU8aStruct)(registry, new Array(typeMap[0].length), (0, _util.u8aToU8a)(value), typeMap) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
97
101
  super(decoded);
98
- this.#registry = registry;
99
102
  this.initialU8aLength = decodedLength;
103
+ this.registry = registry;
100
104
  this.#jsonMap = jsonMap;
101
105
  this.#Types = typeMap;
102
106
  }
@@ -181,10 +185,6 @@ class Struct extends Map {
181
185
  get hash() {
182
186
  return this.registry.hash(this.toU8a());
183
187
  }
184
-
185
- get registry() {
186
- return this.#registry;
187
- }
188
188
  /**
189
189
  * @description Compares the value of the input to see if there is a match
190
190
  */
@@ -28,8 +28,13 @@ function decodeText(value) {
28
28
 
29
29
  const [offset, length] = (0, _util.compactFromU8aLim)(value);
30
30
  const total = offset + length;
31
- (0, _util.assert)(length <= MAX_LENGTH, () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
32
- (0, _util.assert)(total <= value.length, () => `Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
31
+
32
+ if (length > MAX_LENGTH) {
33
+ throw new Error(`Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
34
+ } else if (total > value.length) {
35
+ throw new Error(`Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
36
+ }
37
+
33
38
  return [(0, _util.u8aToString)(value.subarray(offset, total)), total];
34
39
  } else if ((0, _util.isHex)(value)) {
35
40
  return [(0, _util.u8aToString)((0, _util.hexToU8a)(value)), 0];
@@ -48,14 +53,13 @@ function decodeText(value) {
48
53
 
49
54
 
50
55
  class Text extends String {
51
- #initialU8aLength;
52
56
  #override = null;
53
57
 
54
58
  constructor(registry, value) {
55
59
  const [str, decodedLength] = decodeText(value);
56
60
  super(str);
57
61
  this.registry = registry;
58
- this.#initialU8aLength = decodedLength;
62
+ this.initialU8aLength = decodedLength;
59
63
  }
60
64
  /**
61
65
  * @description The length of the value when encoded as a Uint8Array
@@ -65,14 +69,6 @@ class Text extends String {
65
69
  get encodedLength() {
66
70
  return this.toU8a().length;
67
71
  }
68
- /**
69
- * @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
70
- */
71
-
72
-
73
- get initialU8aLength() {
74
- return this.#initialU8aLength;
75
- }
76
72
  /**
77
73
  * @description returns a hash of the contents
78
74
  */
@@ -11,6 +11,6 @@ const packageInfo = {
11
11
  name: '@polkadot/types-codec',
12
12
  path: typeof __dirname === 'string' ? __dirname : 'auto',
13
13
  type: 'cjs',
14
- version: '8.8.3-1'
14
+ version: '8.8.3-12'
15
15
  };
16
16
  exports.packageInfo = packageInfo;
@@ -4,24 +4,31 @@ Object.defineProperty(exports, "__esModule", {
4
4
  value: true
5
5
  });
6
6
  exports.decodeU8a = decodeU8a;
7
+ exports.decodeU8aStruct = decodeU8aStruct;
7
8
  exports.decodeU8aVec = decodeU8aVec;
8
9
 
9
10
  var _util = require("@polkadot/util");
10
11
 
11
12
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
12
13
  // SPDX-License-Identifier: Apache-2.0
13
- function formatFailure(error, type, u8a, key) {
14
- return `decodeU8a: failed at ${(0, _util.u8aToHex)(u8a)}…${key ? ` on ${key}` : ''}${type ? `: ${type}` : ''}:: ${error.message}`;
15
- }
16
14
 
17
- function getRawType(registry, Type) {
15
+ /** @internal */
16
+ function formatFailure(registry, _ref, u8a, i, count, Type, key) {
17
+ let {
18
+ message
19
+ } = _ref;
20
+ let type = '';
21
+
18
22
  try {
19
- return new Type(registry).toRawType();
20
- } catch {
21
- return null;
23
+ type = `: ${new Type(registry).toRawType()}`;
24
+ } catch {// ignore
22
25
  }
26
+
27
+ return `decodeU8a: failed at ${(0, _util.u8aToHex)(u8a.subarray(0, 16))}…${key ? ` on ${key}` : ''} (index ${i}/${count})${type}:: ${message}`;
23
28
  }
24
29
  /**
30
+ * @internal
31
+ *
25
32
  * Given an u8a, and an array of Type constructors, decode the u8a against the
26
33
  * types, and return an array of decoded values.
27
34
  *
@@ -31,38 +38,74 @@ function getRawType(registry, Type) {
31
38
  */
32
39
 
33
40
 
34
- function decodeU8a(registry, result, u8a, _ref, withZip) {
35
- let [Types, keys] = _ref;
41
+ function decodeU8a(registry, result, u8a, _ref2) {
42
+ let [Types, keys] = _ref2;
36
43
  const count = result.length;
37
44
  let offset = 0;
45
+ let i = 0;
38
46
 
39
- for (let i = 0; i < count; i++) {
40
- try {
47
+ try {
48
+ while (i < count) {
41
49
  const value = new Types[i](registry, u8a.subarray(offset));
42
50
  offset += value.initialU8aLength || value.encodedLength;
43
- result[i] = withZip ? [keys[i], value] : value;
44
- } catch (error) {
45
- throw new Error(formatFailure(error, getRawType(registry, Types[i]), u8a.subarray(offset, offset + 16), keys[i]));
51
+ result[i] = value;
52
+ i++;
46
53
  }
54
+ } catch (error) {
55
+ throw new Error(formatFailure(registry, error, u8a.subarray(offset), i, count, Types[i], keys[i]));
47
56
  }
48
57
 
49
58
  return [result, offset];
50
- } // Split from decodeU8a since this is specialized to 1 instance ... yes duplication, but
51
- // since we have to do less checks (and these are intensive anyway), much faster
59
+ }
60
+ /**
61
+ * @internal
62
+ *
63
+ * Split from decodeU8a since this is specialized to zip returns ... while we duplicate, this
64
+ * is all on the hot-path, so it is not great, however there is (some) method behind the madness
65
+ */
66
+
67
+
68
+ function decodeU8aStruct(registry, result, u8a, _ref3) {
69
+ let [Types, keys] = _ref3;
70
+ const count = result.length;
71
+ let offset = 0;
72
+ let i = 0;
73
+
74
+ try {
75
+ while (i < count) {
76
+ const value = new Types[i](registry, u8a.subarray(offset));
77
+ offset += value.initialU8aLength || value.encodedLength;
78
+ result[i] = [keys[i], value];
79
+ i++;
80
+ }
81
+ } catch (error) {
82
+ throw new Error(formatFailure(registry, error, u8a.subarray(offset), i, count, Types[i], keys[i]));
83
+ }
84
+
85
+ return [result, offset];
86
+ }
87
+ /**
88
+ * @internal
89
+ *
90
+ * Split from decodeU8a since this is specialized to 1 instance ... while we duplicate, this
91
+ * is all on the hot-path, so it is not great, however there is (some) method behind the madness
92
+ */
52
93
 
53
94
 
54
95
  function decodeU8aVec(registry, result, u8a, startAt, Type) {
55
96
  const count = result.length;
56
97
  let offset = startAt;
98
+ let i = 0;
57
99
 
58
- for (let i = 0; i < count; i++) {
59
- try {
100
+ try {
101
+ while (i < count) {
60
102
  const value = new Type(registry, u8a.subarray(offset));
61
103
  offset += value.initialU8aLength || value.encodedLength;
62
104
  result[i] = value;
63
- } catch (error) {
64
- throw new Error(formatFailure(error, getRawType(registry, Type), u8a.subarray(offset, offset + 16)));
105
+ i++;
65
106
  }
107
+ } catch (error) {
108
+ throw new Error(formatFailure(registry, error, u8a.subarray(offset), i, count, Type));
66
109
  }
67
110
 
68
111
  return [offset, offset - startAt];
@@ -27,6 +27,12 @@ Object.defineProperty(exports, "decodeU8a", {
27
27
  return _decodeU8a.decodeU8a;
28
28
  }
29
29
  });
30
+ Object.defineProperty(exports, "decodeU8aStruct", {
31
+ enumerable: true,
32
+ get: function () {
33
+ return _decodeU8a.decodeU8aStruct;
34
+ }
35
+ });
30
36
  Object.defineProperty(exports, "decodeU8aVec", {
31
37
  enumerable: true,
32
38
  get: function () {
@@ -18,7 +18,7 @@ function mapToTypeMap(registry, input) {
18
18
  const count = entries.length;
19
19
  const output = [new Array(count), new Array(count)];
20
20
 
21
- for (let i = 0; i < entries.length; i++) {
21
+ for (let i = 0; i < count; i++) {
22
22
  output[1][i] = entries[i][0];
23
23
  output[0][i] = (0, _typeToConstructor.typeToConstructor)(registry, entries[i][1]);
24
24
  }
@@ -140,10 +140,7 @@ function removeExtensions(type, isSized) {
140
140
  }
141
141
 
142
142
  function removeColons() {
143
- return function (value) {
144
- let {
145
- allowNamespaces
146
- } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
143
+ return value => {
147
144
  let index = 0;
148
145
 
149
146
  while (index !== -1) {
@@ -152,10 +149,6 @@ function removeColons() {
152
149
  if (index === 0) {
153
150
  value = value.substring(2);
154
151
  } else if (index !== -1) {
155
- if (allowNamespaces) {
156
- return value;
157
- }
158
-
159
152
  let start = index;
160
153
 
161
154
  while (start !== -1 && !BOX_PRECEDING.includes(value[start])) {
@@ -177,7 +170,7 @@ function removeGenerics() {
177
170
  // check against the allowed wrappers, be it Vec<..>, Option<...> ...
178
171
  const box = ALLOWED_BOXES.find(box => {
179
172
  const start = index - box.length;
180
- return start >= 0 && value.substring(start, start + box.length) === box && ( // make sure it is stand-alone, i.e. don't catch ElectionResult<...> as Result<...>
173
+ return start >= 0 && value.substring(start, index) === box && ( // make sure it is stand-alone, i.e. don't catch ElectionResult<...> as Result<...>
181
174
  start === 0 || BOX_PRECEDING.includes(value[start - 1]));
182
175
  }); // we have not found anything, unwrap generic innards
183
176
 
@@ -190,17 +183,13 @@ function removeGenerics() {
190
183
 
191
184
  return value;
192
185
  };
193
- }
194
- /** @internal */
195
-
196
-
197
- function pairOfReplacer(v) {
198
- return `(${v},${v})`;
199
186
  } // remove the PairOf wrappers
200
187
 
201
188
 
202
189
  function removePairOf() {
203
- return value => replaceTagWith(value, 'PairOf<', pairOfReplacer);
190
+ const replacer = v => `(${v},${v})`;
191
+
192
+ return value => replaceTagWith(value, 'PairOf<', replacer);
204
193
  } // remove the type traits
205
194
 
206
195
 
@@ -226,40 +215,31 @@ function removeTraits() {
226
215
  .replace(from5, 'Lookup') // replace `<...>::Type`
227
216
  .replace(from6, '');
228
217
  };
229
- }
230
- /** @internal */
231
-
232
-
233
- function wrapReplacer(v) {
234
- return v;
235
218
  } // remove wrapping values, i.e. Box<Proposal> -> Proposal
236
219
 
237
220
 
238
221
  function removeWrap(check) {
239
- return value => replaceTagWith(value, check, wrapReplacer);
222
+ const replacer = v => v;
223
+
224
+ return value => replaceTagWith(value, check, replacer);
240
225
  }
241
226
 
242
- const sanitizeMap = new Map(); // eslint-disable-next-line @typescript-eslint/ban-types
227
+ const sanitizeMap = new Map();
243
228
 
244
- function sanitize(value, options) {
229
+ function sanitize(value) {
245
230
  const startValue = value.toString();
246
- let result = startValue;
231
+ const memoized = sanitizeMap.get(startValue);
247
232
 
248
- if (!options) {
249
- const memoized = sanitizeMap.get(startValue);
250
-
251
- if (memoized) {
252
- return memoized;
253
- }
233
+ if (memoized) {
234
+ return memoized;
254
235
  }
255
236
 
256
- for (let i = 0; i < mappings.length; i++) {
257
- result = mappings[i](result, options);
258
- }
237
+ let result = startValue;
259
238
 
260
- if (!options) {
261
- sanitizeMap.set(startValue, result);
239
+ for (let i = 0; i < mappings.length; i++) {
240
+ result = mappings[i](result);
262
241
  }
263
242
 
243
+ sanitizeMap.set(startValue, result);
264
244
  return result;
265
245
  }
@@ -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, compactFromU8aLim, compactToU8a, isString, u8aConcatStrict, u8aToU8a } from '@polkadot/util';
3
+ import { compactFromU8aLim, compactToU8a, isString, u8aConcatStrict, u8aToU8a } from '@polkadot/util';
4
4
  import { Raw } from "../native/Raw.js";
5
5
  /** @internal */
6
6
 
@@ -12,7 +12,11 @@ function decodeBitVecU8a(value) {
12
12
 
13
13
  const [offset, length] = compactFromU8aLim(value);
14
14
  const total = offset + Math.ceil(length / 8);
15
- assert(total <= value.length, () => `BitVec: required length less than remainder, expected at least ${total}, found ${value.length}`);
15
+
16
+ if (total > value.length) {
17
+ throw new Error(`BitVec: required length less than remainder, expected at least ${total}, found ${value.length}`);
18
+ }
19
+
16
20
  return [length, value.subarray(offset, total)];
17
21
  }
18
22
  /** @internal */
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, compactFromU8aLim, compactToU8a, isCodec, isString, isU8a, u8aToU8a } from '@polkadot/util';
3
+ import { compactAddLength, compactFromU8aLim, compactToU8a, 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;
@@ -14,8 +14,13 @@ function decodeBytesU8a(value) {
14
14
 
15
15
  const [offset, length] = compactFromU8aLim(value);
16
16
  const total = offset + length;
17
- assert(length <= MAX_LENGTH, () => `Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
18
- assert(total <= value.length, () => `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
17
+
18
+ if (length > MAX_LENGTH) {
19
+ throw new Error(`Bytes length ${length.toString()} exceeds ${MAX_LENGTH}`);
20
+ } else if (total > value.length) {
21
+ throw new Error(`Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
22
+ }
23
+
19
24
  return [value.subarray(offset, total), total];
20
25
  }
21
26
  /**
@@ -29,7 +34,7 @@ function decodeBytesU8a(value) {
29
34
 
30
35
  export class Bytes extends Raw {
31
36
  constructor(registry, value) {
32
- const [u8a, decodedLength] = isU8a(value) && !(isCodec(value) && value instanceof Raw) ? decodeBytesU8a(value) : Array.isArray(value) || isString(value) ? [u8aToU8a(value), 0] : [value, 0];
37
+ const [u8a, decodedLength] = isU8a(value) && !(value instanceof Raw) ? decodeBytesU8a(value) : Array.isArray(value) || isString(value) ? [u8aToU8a(value), 0] : [value, 0];
33
38
  super(registry, u8a, decodedLength);
34
39
  }
35
40
  /**
package/extended/Map.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { compactFromU8aLim, compactToU8a, isHex, isObject, isU8a, logger, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
4
- import { AbstractArray } from "../abstract/AbstractArray.js";
4
+ import { AbstractArray } from "../abstract/Array.js";
5
5
  import { Enum } from "../base/Enum.js";
6
6
  import { Struct } from "../native/Struct.js";
7
7
  import { compareMap, decodeU8a, sortMap, typeToConstructor } from "../utils/index.js";
@@ -12,10 +12,6 @@ export declare class OptionBool extends Option<Bool> {
12
12
  * @description The length of the value when encoded as a Uint8Array
13
13
  */
14
14
  get encodedLength(): number;
15
- /**
16
- * @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
17
- */
18
- get initialU8aLength(): number;
19
15
  /**
20
16
  * @description Checks if the value is an empty value (always false)
21
17
  */
@@ -20,6 +20,7 @@ function decodeU8a(registry, value) {
20
20
  export class OptionBool extends Option {
21
21
  constructor(registry, value) {
22
22
  super(registry, Bool, isU8a(value) || isHex(value) ? decodeU8a(registry, u8aToU8a(value)) : value);
23
+ this.initialU8aLength = 1;
23
24
  }
24
25
  /**
25
26
  * @description The length of the value when encoded as a Uint8Array
@@ -29,14 +30,6 @@ export class OptionBool extends Option {
29
30
  get encodedLength() {
30
31
  return 1;
31
32
  }
32
- /**
33
- * @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
34
- */
35
-
36
-
37
- get initialU8aLength() {
38
- return 1;
39
- }
40
33
  /**
41
34
  * @description Checks if the value is an empty value (always false)
42
35
  */
@@ -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, u8aToU8a } from '@polkadot/util';
3
+ import { isU8a, u8aToU8a } from '@polkadot/util';
4
4
  import { Raw } from "../native/Raw.js";
5
5
  /** @internal */
6
6
 
@@ -12,7 +12,10 @@ function decodeU8aFixed(value, bitLength) {
12
12
  return [new Uint8Array(byteLength), 0];
13
13
  }
14
14
 
15
- assert(isU8a(value) ? u8a.length >= byteLength : u8a.length === byteLength, () => `Expected input with ${byteLength} bytes (${bitLength} bits), found ${u8a.length} bytes`);
15
+ if (isU8a(value) ? u8a.length < byteLength : u8a.length !== byteLength) {
16
+ throw new Error(`Expected input with ${byteLength} bytes (${bitLength} bits), found ${u8a.length} bytes`);
17
+ }
18
+
16
19
  return [u8a.subarray(0, byteLength), byteLength];
17
20
  }
18
21
  /**
@@ -1,6 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { assertReturn, compactAddLength, compactStripLength, compactToU8a, isHex, isU8a } from '@polkadot/util';
3
+ import { compactAddLength, compactStripLength, compactToU8a, isHex, isU8a } from '@polkadot/util';
4
4
  import { Raw } from "../native/Raw.js";
5
5
  import { typeToConstructor } from "../utils/index.js";
6
6
  import { Bytes } from "./Bytes.js";
@@ -95,7 +95,11 @@ export class WrapperKeepOpaque extends Bytes {
95
95
 
96
96
 
97
97
  unwrap() {
98
- return assertReturn(this.#decoded, () => `${this.#opaqueName}: unwrapping an undecodable value`);
98
+ if (!this.#decoded) {
99
+ throw new Error(`${this.#opaqueName}: unwrapping an undecodable value`);
100
+ }
101
+
102
+ return this.#decoded;
99
103
  }
100
104
 
101
105
  }