@polkadot/types-codec 8.8.3-8 → 8.9.2-0

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/Int.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, BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, BN_ZERO, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isHex, isNumber, isString, isU8a, u8aToBn, u8aToNumber } from '@polkadot/util';
3
+ import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isHex, isNumber, isString, isU8a, u8aToBn, u8aToNumber } from '@polkadot/util';
4
4
  export const DEFAULT_UINT_BITS = 64; // Maximum allowed integer for JS is 2^53 - 1, set limit at 52
5
5
  // In this case however, we always print any >32 as hex
6
6
 
@@ -16,7 +16,10 @@ function toPercentage(value, divisor) {
16
16
 
17
17
  function decodeAbstractInt(value, isNegative) {
18
18
  if (isNumber(value)) {
19
- assert(value <= Number.MAX_SAFE_INTEGER && value >= Number.MIN_SAFE_INTEGER && Number.isInteger(value), 'Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1');
19
+ if (!Number.isInteger(value) || value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER) {
20
+ throw new Error('Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1');
21
+ }
22
+
20
23
  return value;
21
24
  } else if (isString(value)) {
22
25
  if (isHex(value, -1, true)) {
@@ -26,7 +29,10 @@ function decodeAbstractInt(value, isNegative) {
26
29
  }).toString();
27
30
  }
28
31
 
29
- assert(!(value.includes('.') || value.includes(',') || value.includes('e')), 'String should not contain decimal points or scientific notation');
32
+ if (value.includes('.') || value.includes(',') || value.includes('e')) {
33
+ throw new Error('String should not contain decimal points or scientific notation');
34
+ }
35
+
30
36
  return value;
31
37
  } else if (isBn(value)) {
32
38
  return value.toString();
@@ -59,10 +65,14 @@ export class AbstractInt extends BN {
59
65
  this.#bitLength = bitLength;
60
66
  this.encodedLength = this.#bitLength / 8;
61
67
  this.isUnsigned = !isSigned;
62
- const isPositive = this.gte(BN_ZERO);
63
- const maxBits = bitLength - (isSigned && isPositive ? 1 : 0);
64
- assert(isSigned || isPositive, () => `${this.toRawType()}: Negative number passed to unsigned type`);
65
- assert(super.bitLength() <= maxBits, () => `${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${maxBits}`);
68
+ const isNegative = this.isNeg();
69
+ const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
70
+
71
+ if (isNegative && !isSigned) {
72
+ throw new Error(`${this.toRawType()}: Negative number passed to unsigned type`);
73
+ } else if (super.bitLength() > maxBits) {
74
+ throw new Error(`${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${maxBits}`);
75
+ }
66
76
  }
67
77
  /**
68
78
  * @description returns a hash of the contents
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, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
3
+ import { isHex, isNumber, isObject, isString, isU8a, 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
 
@@ -12,7 +12,10 @@ function isRustEnum(def) {
12
12
  const defValues = Object.values(def);
13
13
 
14
14
  if (defValues.some(v => isNumber(v))) {
15
- assert(defValues.every(v => isNumber(v) && v >= 0 && v <= 255), 'Invalid number-indexed enum definition');
15
+ if (!defValues.every(v => isNumber(v) && v >= 0 && v <= 255)) {
16
+ throw new Error('Invalid number-indexed enum definition');
17
+ }
18
+
16
19
  return false;
17
20
  }
18
21
 
@@ -70,12 +73,36 @@ function extractDef(registry, _def) {
70
73
  };
71
74
  }
72
75
 
76
+ function getEntryType(def, checkIdx) {
77
+ const values = Object.values(def);
78
+
79
+ for (let i = 0; i < values.length; i++) {
80
+ const {
81
+ Type,
82
+ index
83
+ } = values[i];
84
+
85
+ if (index === checkIdx) {
86
+ return Type;
87
+ }
88
+ }
89
+
90
+ throw new Error(`Unable to create Enum via index ${checkIdx}, in ${Object.keys(def).join(', ')}`);
91
+ }
92
+
93
+ function createFromU8a(registry, def, index, value) {
94
+ const Type = getEntryType(def, index);
95
+ return {
96
+ index,
97
+ value: new Type(registry, value)
98
+ };
99
+ }
100
+
73
101
  function createFromValue(registry, def, index = 0, value) {
74
- const entry = Object.values(def).find(e => e.index === index);
75
- assert(!isUndefined(entry), () => `Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`);
102
+ const Type = getEntryType(def, index);
76
103
  return {
77
104
  index,
78
- value: value instanceof entry.Type ? value : new entry.Type(registry, value)
105
+ value: value instanceof Type ? value : new Type(registry, value)
79
106
  };
80
107
  }
81
108
 
@@ -85,7 +112,10 @@ function decodeFromJSON(registry, def, key, value) {
85
112
  const keys = Object.keys(def).map(k => k.toLowerCase());
86
113
  const keyLower = key.toLowerCase();
87
114
  const index = keys.indexOf(keyLower);
88
- assert(index !== -1, () => `Cannot map Enum JSON, unable to find '${key}' in ${keys.join(', ')}`);
115
+
116
+ if (index === -1) {
117
+ throw new Error(`Cannot map Enum JSON, unable to find '${key}' in ${keys.join(', ')}`);
118
+ }
89
119
 
90
120
  try {
91
121
  return createFromValue(registry, def, Object.values(def)[index].index, value);
@@ -103,7 +133,7 @@ function decodeEnum(registry, def, value, index) {
103
133
  const u8a = u8aToU8a(value); // nested, we don't want to match isObject below
104
134
 
105
135
  if (u8a.length) {
106
- return createFromValue(registry, def, u8a[0], u8a.subarray(1));
136
+ return createFromU8a(registry, def, u8a[0], u8a.subarray(1));
107
137
  }
108
138
  } else if (value instanceof Enum) {
109
139
  return createFromValue(registry, def, value.index, value.value);
@@ -145,7 +175,7 @@ export class Enum {
145
175
  isIndexed
146
176
  } = definition || setDefinition(extractDef(registry, Types)); // shortcut isU8a as used in SCALE decoding
147
177
 
148
- const decoded = isU8a(value) && value.length && !isNumber(index) ? createFromValue(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
178
+ const decoded = isU8a(value) && value.length && !isNumber(index) ? createFromU8a(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
149
179
  this.registry = registry;
150
180
  this.#def = def;
151
181
  this.#isBasic = isBasic;
@@ -184,7 +214,10 @@ export class Enum {
184
214
  });
185
215
  objectProperties(this, isKeys, (_, i) => this.type === keys[i]);
186
216
  objectProperties(this, asKeys, (k, i) => {
187
- assert(this[isKeys[i]], () => `Cannot convert '${this.type}' via ${k}`);
217
+ if (!this[isKeys[i]]) {
218
+ throw new Error(`Cannot convert '${this.type}' via ${k}`);
219
+ }
220
+
188
221
  return this.value;
189
222
  });
190
223
  }
package/base/Option.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, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
3
+ import { isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
4
4
  import { typeToConstructor } from "../utils/index.js";
5
5
  import { Null } from "./Null.js";
6
6
 
@@ -244,7 +244,10 @@ export class Option {
244
244
 
245
245
 
246
246
  unwrap() {
247
- assert(this.isSome, 'Option: unwrapping a None value');
247
+ if (this.isNone) {
248
+ throw new Error('Option: unwrapping a None value');
249
+ }
250
+
248
251
  return this.#raw;
249
252
  }
250
253
  /**
package/base/Result.js CHANGED
@@ -1,6 +1,5 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- import { assert } from '@polkadot/util';
4
3
  import { Enum } from "./Enum.js";
5
4
  /**
6
5
  * @name Result
@@ -32,7 +31,10 @@ export class Result extends Enum {
32
31
 
33
32
 
34
33
  get asErr() {
35
- assert(this.isErr, 'Cannot extract Err value from Ok result, check isErr first');
34
+ if (!this.isErr) {
35
+ throw new Error('Cannot extract Err value from Ok result, check isErr first');
36
+ }
37
+
36
38
  return this.value;
37
39
  }
38
40
  /**
@@ -49,7 +51,10 @@ export class Result extends Enum {
49
51
 
50
52
 
51
53
  get asOk() {
52
- assert(this.isOk, 'Cannot extract Ok value from Err result, check isOk first');
54
+ if (!this.isOk) {
55
+ throw new Error('Cannot extract Ok value from Err result, check isOk first');
56
+ }
57
+
53
58
  return this.value;
54
59
  }
55
60
  /**
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, compactFromU8aLim, isU8a, logger, u8aToU8a } from '@polkadot/util';
3
+ import { compactFromU8aLim, isU8a, logger, u8aToU8a } from '@polkadot/util';
4
4
  import { AbstractArray } from "../abstract/Array.js";
5
5
  import { decodeU8aVec, typeToConstructor } from "../utils/index.js";
6
6
  const MAX_LENGTH = 64 * 1024;
@@ -17,7 +17,11 @@ function decodeVecLength(value) {
17
17
 
18
18
  const u8a = u8aToU8a(value);
19
19
  const [startAt, length] = compactFromU8aLim(u8a);
20
- assert(length <= MAX_LENGTH, () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
20
+
21
+ if (length > MAX_LENGTH) {
22
+ throw new Error(`Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
23
+ }
24
+
21
25
  return [u8a, length, startAt];
22
26
  }
23
27
 
@@ -25,7 +25,10 @@ function toPercentage(value, divisor) {
25
25
 
26
26
  function decodeAbstractInt(value, isNegative) {
27
27
  if ((0, _util.isNumber)(value)) {
28
- (0, _util.assert)(value <= Number.MAX_SAFE_INTEGER && value >= Number.MIN_SAFE_INTEGER && Number.isInteger(value), 'Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1');
28
+ if (!Number.isInteger(value) || value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER) {
29
+ throw new Error('Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1');
30
+ }
31
+
29
32
  return value;
30
33
  } else if ((0, _util.isString)(value)) {
31
34
  if ((0, _util.isHex)(value, -1, true)) {
@@ -35,7 +38,10 @@ function decodeAbstractInt(value, isNegative) {
35
38
  }).toString();
36
39
  }
37
40
 
38
- (0, _util.assert)(!(value.includes('.') || value.includes(',') || value.includes('e')), 'String should not contain decimal points or scientific notation');
41
+ if (value.includes('.') || value.includes(',') || value.includes('e')) {
42
+ throw new Error('String should not contain decimal points or scientific notation');
43
+ }
44
+
39
45
  return value;
40
46
  } else if ((0, _util.isBn)(value)) {
41
47
  return value.toString();
@@ -71,10 +77,14 @@ class AbstractInt extends _util.BN {
71
77
  this.#bitLength = bitLength;
72
78
  this.encodedLength = this.#bitLength / 8;
73
79
  this.isUnsigned = !isSigned;
74
- const isPositive = this.gte(_util.BN_ZERO);
75
- const maxBits = bitLength - (isSigned && isPositive ? 1 : 0);
76
- (0, _util.assert)(isSigned || isPositive, () => `${this.toRawType()}: Negative number passed to unsigned type`);
77
- (0, _util.assert)(super.bitLength() <= maxBits, () => `${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${maxBits}`);
80
+ const isNegative = this.isNeg();
81
+ const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
82
+
83
+ if (isNegative && !isSigned) {
84
+ throw new Error(`${this.toRawType()}: Negative number passed to unsigned type`);
85
+ } else if (super.bitLength() > maxBits) {
86
+ throw new Error(`${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${maxBits}`);
87
+ }
78
88
  }
79
89
  /**
80
90
  * @description returns a hash of the contents
package/cjs/base/Enum.js CHANGED
@@ -21,7 +21,10 @@ function isRustEnum(def) {
21
21
  const defValues = Object.values(def);
22
22
 
23
23
  if (defValues.some(v => (0, _util.isNumber)(v))) {
24
- (0, _util.assert)(defValues.every(v => (0, _util.isNumber)(v) && v >= 0 && v <= 255), 'Invalid number-indexed enum definition');
24
+ if (!defValues.every(v => (0, _util.isNumber)(v) && v >= 0 && v <= 255)) {
25
+ throw new Error('Invalid number-indexed enum definition');
26
+ }
27
+
25
28
  return false;
26
29
  }
27
30
 
@@ -82,14 +85,38 @@ function extractDef(registry, _def) {
82
85
  };
83
86
  }
84
87
 
88
+ function getEntryType(def, checkIdx) {
89
+ const values = Object.values(def);
90
+
91
+ for (let i = 0; i < values.length; i++) {
92
+ const {
93
+ Type,
94
+ index
95
+ } = values[i];
96
+
97
+ if (index === checkIdx) {
98
+ return Type;
99
+ }
100
+ }
101
+
102
+ throw new Error(`Unable to create Enum via index ${checkIdx}, in ${Object.keys(def).join(', ')}`);
103
+ }
104
+
105
+ function createFromU8a(registry, def, index, value) {
106
+ const Type = getEntryType(def, index);
107
+ return {
108
+ index,
109
+ value: new Type(registry, value)
110
+ };
111
+ }
112
+
85
113
  function createFromValue(registry, def) {
86
114
  let index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
87
115
  let value = arguments.length > 3 ? arguments[3] : undefined;
88
- const entry = Object.values(def).find(e => e.index === index);
89
- (0, _util.assert)(!(0, _util.isUndefined)(entry), () => `Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`);
116
+ const Type = getEntryType(def, index);
90
117
  return {
91
118
  index,
92
- value: value instanceof entry.Type ? value : new entry.Type(registry, value)
119
+ value: value instanceof Type ? value : new Type(registry, value)
93
120
  };
94
121
  }
95
122
 
@@ -99,7 +126,10 @@ function decodeFromJSON(registry, def, key, value) {
99
126
  const keys = Object.keys(def).map(k => k.toLowerCase());
100
127
  const keyLower = key.toLowerCase();
101
128
  const index = keys.indexOf(keyLower);
102
- (0, _util.assert)(index !== -1, () => `Cannot map Enum JSON, unable to find '${key}' in ${keys.join(', ')}`);
129
+
130
+ if (index === -1) {
131
+ throw new Error(`Cannot map Enum JSON, unable to find '${key}' in ${keys.join(', ')}`);
132
+ }
103
133
 
104
134
  try {
105
135
  return createFromValue(registry, def, Object.values(def)[index].index, value);
@@ -117,7 +147,7 @@ function decodeEnum(registry, def, value, index) {
117
147
  const u8a = (0, _util.u8aToU8a)(value); // nested, we don't want to match isObject below
118
148
 
119
149
  if (u8a.length) {
120
- return createFromValue(registry, def, u8a[0], u8a.subarray(1));
150
+ return createFromU8a(registry, def, u8a[0], u8a.subarray(1));
121
151
  }
122
152
  } else if (value instanceof Enum) {
123
153
  return createFromValue(registry, def, value.index, value.value);
@@ -160,7 +190,7 @@ class Enum {
160
190
  isIndexed
161
191
  } = definition || setDefinition(extractDef(registry, Types)); // shortcut isU8a as used in SCALE decoding
162
192
 
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);
193
+ const decoded = (0, _util.isU8a)(value) && value.length && !(0, _util.isNumber)(index) ? createFromU8a(registry, def, value[0], value.subarray(1)) : decodeEnum(registry, def, value, index);
164
194
  this.registry = registry;
165
195
  this.#def = def;
166
196
  this.#isBasic = isBasic;
@@ -202,7 +232,10 @@ class Enum {
202
232
  });
203
233
  (0, _util.objectProperties)(this, isKeys, (_, i) => this.type === keys[i]);
204
234
  (0, _util.objectProperties)(this, asKeys, (k, i) => {
205
- (0, _util.assert)(this[isKeys[i]], () => `Cannot convert '${this.type}' via ${k}`);
235
+ if (!this[isKeys[i]]) {
236
+ throw new Error(`Cannot convert '${this.type}' via ${k}`);
237
+ }
238
+
206
239
  return this.value;
207
240
  });
208
241
  }
@@ -254,7 +254,10 @@ class Option {
254
254
 
255
255
 
256
256
  unwrap() {
257
- (0, _util.assert)(this.isSome, 'Option: unwrapping a None value');
257
+ if (this.isNone) {
258
+ throw new Error('Option: unwrapping a None value');
259
+ }
260
+
258
261
  return this.#raw;
259
262
  }
260
263
  /**
@@ -5,8 +5,6 @@ Object.defineProperty(exports, "__esModule", {
5
5
  });
6
6
  exports.Result = void 0;
7
7
 
8
- var _util = require("@polkadot/util");
9
-
10
8
  var _Enum = require("./Enum");
11
9
 
12
10
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
@@ -41,7 +39,10 @@ class Result extends _Enum.Enum {
41
39
 
42
40
 
43
41
  get asErr() {
44
- (0, _util.assert)(this.isErr, 'Cannot extract Err value from Ok result, check isErr first');
42
+ if (!this.isErr) {
43
+ throw new Error('Cannot extract Err value from Ok result, check isErr first');
44
+ }
45
+
45
46
  return this.value;
46
47
  }
47
48
  /**
@@ -58,7 +59,10 @@ class Result extends _Enum.Enum {
58
59
 
59
60
 
60
61
  get asOk() {
61
- (0, _util.assert)(this.isOk, 'Cannot extract Ok value from Err result, check isOk first');
62
+ if (!this.isOk) {
63
+ throw new Error('Cannot extract Ok value from Err result, check isOk first');
64
+ }
65
+
62
66
  return this.value;
63
67
  }
64
68
  /**
package/cjs/base/Vec.js CHANGED
@@ -28,7 +28,11 @@ function decodeVecLength(value) {
28
28
 
29
29
  const u8a = (0, _util.u8aToU8a)(value);
30
30
  const [startAt, length] = (0, _util.compactFromU8aLim)(u8a);
31
- (0, _util.assert)(length <= MAX_LENGTH, () => `Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
31
+
32
+ if (length > MAX_LENGTH) {
33
+ throw new Error(`Vec length ${length.toString()} exceeds ${MAX_LENGTH}`);
34
+ }
35
+
32
36
  return [u8a, length, startAt];
33
37
  }
34
38
 
@@ -21,7 +21,11 @@ function decodeBitVecU8a(value) {
21
21
 
22
22
  const [offset, length] = (0, _util.compactFromU8aLim)(value);
23
23
  const total = offset + Math.ceil(length / 8);
24
- (0, _util.assert)(total <= value.length, () => `BitVec: required length less than remainder, expected at least ${total}, found ${value.length}`);
24
+
25
+ if (total > value.length) {
26
+ throw new Error(`BitVec: required length less than remainder, expected at least ${total}, found ${value.length}`);
27
+ }
28
+
25
29
  return [length, value.subarray(offset, total)];
26
30
  }
27
31
  /** @internal */
@@ -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
  /**
@@ -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++) {
@@ -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];
@@ -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-8'
14
+ version: '8.9.2-0'
15
15
  };
16
16
  exports.packageInfo = packageInfo;
@@ -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, 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
  /**
@@ -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
  }
package/native/Raw.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, isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util';
3
+ import { isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util';
4
4
  /**
5
5
  * @name Raw
6
6
  * @description
@@ -182,7 +182,10 @@ export class Raw extends Uint8Array {
182
182
 
183
183
 
184
184
  toUtf8() {
185
- assert(this.isUtf8, 'The character sequence is not a valid Utf8 string');
185
+ if (!this.isUtf8) {
186
+ throw new Error('The character sequence is not a valid Utf8 string');
187
+ }
188
+
186
189
  return u8aToString(this);
187
190
  }
188
191
 
package/native/Set.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, BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, objectProperties, stringify, stringPascalCase, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
3
+ import { BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, objectProperties, stringify, stringPascalCase, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
4
4
  import { compareArray } from "../utils/index.js";
5
5
 
6
6
  function encodeSet(setValues, values) {
@@ -20,7 +20,11 @@ function decodeSetArray(setValues, values) {
20
20
 
21
21
  for (let i = 0; i < values.length; i++) {
22
22
  const key = values[i];
23
- assert(!isUndefined(setValues[key]), () => `Set: Invalid key '${key}' passed to Set, allowed ${Object.keys(setValues).join(', ')}`);
23
+
24
+ if (isUndefined(setValues[key])) {
25
+ throw new Error(`Set: Invalid key '${key}' passed to Set, allowed ${Object.keys(setValues).join(', ')}`);
26
+ }
27
+
24
28
  result[i] = key;
25
29
  }
26
30
 
@@ -43,14 +47,21 @@ function decodeSetNumber(setValues, _value) {
43
47
  }
44
48
 
45
49
  const computed = encodeSet(setValues, result);
46
- assert(bn.eq(computed), () => `Set: Mismatch decoding '${bn.toString()}', computed as '${computed.toString()}' with ${result.join(', ')}`);
50
+
51
+ if (!bn.eq(computed)) {
52
+ throw new Error(`Set: Mismatch decoding '${bn.toString()}', computed as '${computed.toString()}' with ${result.join(', ')}`);
53
+ }
54
+
47
55
  return result;
48
56
  }
49
57
  /** @internal */
50
58
 
51
59
 
52
60
  function decodeSet(setValues, value = 0, bitLength) {
53
- assert(bitLength % 8 === 0, () => `Expected valid bitLength, power of 8, found ${bitLength}`);
61
+ if (bitLength % 8 !== 0) {
62
+ throw new Error(`Expected valid bitLength, power of 8, found ${bitLength}`);
63
+ }
64
+
54
65
  const byteLength = bitLength / 8;
55
66
 
56
67
  if (isU8a(value)) {
@@ -150,7 +161,10 @@ export class CodecSet extends Set {
150
161
  // ^^^ add = () property done to assign this instance's this, otherwise Set.add creates "some" chaos
151
162
  // we have the isUndefined(this._setValues) in here as well, add is used internally
152
163
  // in the Set constructor (so it is undefined at this point, and should allow)
153
- assert(isUndefined(this.#allowed) || !isUndefined(this.#allowed[key]), () => `Set: Invalid key '${key}' on add`);
164
+ if (this.#allowed && isUndefined(this.#allowed[key])) {
165
+ throw new Error(`Set: Invalid key '${key}' on add`);
166
+ }
167
+
154
168
  super.add(key);
155
169
  return this;
156
170
  };
package/native/Struct.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, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
3
+ import { isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
4
4
  import { compareMap, decodeU8aStruct, mapToTypeMap, typesToMap } from "../utils/index.js";
5
5
 
6
6
  function noopSetDefinition(d) {
@@ -13,8 +13,13 @@ function decodeStructFromObject(registry, [Types, keys], value, jsonMap) {
13
13
  let jsonObj;
14
14
  const typeofArray = Array.isArray(value);
15
15
  const typeofMap = value instanceof Map;
16
- assert(typeofArray || typeofMap || isObject(value), () => `Struct: Cannot decode value ${stringify(value)} (typeof ${typeof value}), expected an input object, map or array`);
17
- assert(!typeofArray || value.length === keys.length, () => `Struct: Unable to map ${stringify(value)} array to object with known keys ${keys.join(', ')}`);
16
+
17
+ if (!typeofArray && !typeofMap && !isObject(value)) {
18
+ throw new Error(`Struct: Cannot decode value ${stringify(value)} (typeof ${typeof value}), expected an input object, map or array`);
19
+ } else if (typeofArray && value.length !== keys.length) {
20
+ throw new Error(`Struct: Unable to map ${stringify(value)} array to object with known keys ${keys.join(', ')}`);
21
+ }
22
+
18
23
  const raw = new Array(keys.length);
19
24
 
20
25
  for (let i = 0; i < keys.length; i++) {
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, compactFromU8aLim, compactToU8a, hexToU8a, isHex, isString, isU8a, stringToU8a, u8aToHex, u8aToString } from '@polkadot/util';
3
+ import { 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 */
@@ -19,8 +19,13 @@ function decodeText(value) {
19
19
 
20
20
  const [offset, length] = compactFromU8aLim(value);
21
21
  const total = offset + length;
22
- assert(length <= MAX_LENGTH, () => `Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
23
- assert(total <= value.length, () => `Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
22
+
23
+ if (length > MAX_LENGTH) {
24
+ throw new Error(`Text: length ${length.toString()} exceeds ${MAX_LENGTH}`);
25
+ } else if (total > value.length) {
26
+ throw new Error(`Text: required length less than remainder, expected at least ${total}, found ${value.length}`);
27
+ }
28
+
24
29
  return [u8aToString(value.subarray(offset, total)), total];
25
30
  } else if (isHex(value)) {
26
31
  return [u8aToString(hexToU8a(value)), 0];
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./cjs/detectPackage.js"
21
21
  ],
22
22
  "type": "module",
23
- "version": "8.8.3-8",
23
+ "version": "8.9.2-0",
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.3",
418
- "@polkadot/util": "^9.4.2-22"
418
+ "@polkadot/util": "^9.5.1"
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.8.3-8'
8
+ version: '8.9.2-0'
9
9
  };