@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
File without changes
File without changes
@@ -4,10 +4,11 @@ import type { AnyJson, BareOpts, Codec, Inspect, IU8a, Registry } from '../types
4
4
  * @name Base
5
5
  * @description A type extends the Base class, when it holds a value
6
6
  */
7
- export declare abstract class Base<T extends Codec> implements Codec {
7
+ export declare abstract class AbstractBase<T extends Codec> implements Codec {
8
8
  #private;
9
9
  createdAtHash?: IU8a;
10
10
  readonly initialU8aLength?: number;
11
+ readonly registry: Registry;
11
12
  protected constructor(registry: Registry, value: T, initialU8aLength?: number);
12
13
  /**
13
14
  * @description The length of the value when encoded as a Uint8Array
@@ -22,7 +23,6 @@ export declare abstract class Base<T extends Codec> implements Codec {
22
23
  * @description Checks if the value is an empty value
23
24
  */
24
25
  get isEmpty(): boolean;
25
- get registry(): Registry;
26
26
  /**
27
27
  * @description Compares the value of the input to see if there is a match
28
28
  */
@@ -5,14 +5,13 @@
5
5
  * @name Base
6
6
  * @description A type extends the Base class, when it holds a value
7
7
  */
8
- export class Base {
9
- #registry;
8
+ export class AbstractBase {
10
9
  #raw;
11
10
 
12
11
  constructor(registry, value, initialU8aLength) {
13
- this.#registry = registry;
14
12
  this.#raw = value;
15
13
  this.initialU8aLength = initialU8aLength;
14
+ this.registry = registry;
16
15
  }
17
16
  /**
18
17
  * @description The length of the value when encoded as a Uint8Array
@@ -42,10 +41,6 @@ export class Base {
42
41
  get isEmpty() {
43
42
  return this.#raw.isEmpty;
44
43
  }
45
-
46
- get registry() {
47
- return this.#registry;
48
- }
49
44
  /**
50
45
  * @description Compares the value of the input to see if there is a match
51
46
  */
@@ -13,6 +13,7 @@ export declare abstract class AbstractInt extends BN implements INumber {
13
13
  readonly registry: Registry;
14
14
  createdAtHash?: IU8a;
15
15
  readonly encodedLength: number;
16
+ readonly isUnsigned: boolean;
16
17
  constructor(registry: Registry, value?: AnyNumber, bitLength?: UIntBitLength, isSigned?: boolean);
17
18
  /**
18
19
  * @description returns a hash of the contents
@@ -22,10 +23,6 @@ export declare abstract class AbstractInt extends BN implements INumber {
22
23
  * @description Checks if the value is a zero value (align elsewhere)
23
24
  */
24
25
  get isEmpty(): boolean;
25
- /**
26
- * @description Checks if the value is an unsigned type
27
- */
28
- get isUnsigned(): boolean;
29
26
  /**
30
27
  * @description Returns the number of bits in the value
31
28
  */
@@ -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 } 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();
@@ -43,25 +49,30 @@ function decodeAbstractInt(value, isNegative) {
43
49
 
44
50
  export class AbstractInt extends BN {
45
51
  #bitLength;
46
- #isSigned;
47
52
 
48
53
  constructor(registry, value = 0, bitLength = DEFAULT_UINT_BITS, isSigned = false) {
49
- // Construct via a string, which will be passed in the BN constructor.
54
+ // Construct via a string/number, which will be passed in the BN constructor.
50
55
  // It would be ideal to actually return a BN, but there is an issue:
51
56
  // https://github.com/indutny/bn.js/issues/206
52
57
  super( // shortcut isU8a as used in SCALE decoding
53
- isU8a(value) ? u8aToBn(value.subarray(0, bitLength / 8), {
58
+ isU8a(value) ? bitLength <= 48 ? u8aToNumber(value.subarray(0, bitLength / 8), {
59
+ isNegative: isSigned
60
+ }) : u8aToBn(value.subarray(0, bitLength / 8), {
54
61
  isLe: true,
55
62
  isNegative: isSigned
56
63
  }).toString() : decodeAbstractInt(value, isSigned));
57
64
  this.registry = registry;
58
65
  this.#bitLength = bitLength;
59
66
  this.encodedLength = this.#bitLength / 8;
60
- this.#isSigned = isSigned;
61
- const isPositive = this.gte(BN_ZERO);
62
- const maxBits = bitLength - (isSigned && isPositive ? 1 : 0);
63
- assert(isSigned || isPositive, () => `${this.toRawType()}: Negative number passed to unsigned type`);
64
- assert(super.bitLength() <= maxBits, () => `${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${maxBits}`);
67
+ this.isUnsigned = !isSigned;
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
+ }
65
76
  }
66
77
  /**
67
78
  * @description returns a hash of the contents
@@ -79,14 +90,6 @@ export class AbstractInt extends BN {
79
90
  get isEmpty() {
80
91
  return this.isZero();
81
92
  }
82
- /**
83
- * @description Checks if the value is an unsigned type
84
- */
85
-
86
-
87
- get isUnsigned() {
88
- return !this.#isSigned;
89
- }
90
93
  /**
91
94
  * @description Returns the number of bits in the value
92
95
  */
@@ -106,7 +109,7 @@ export class AbstractInt extends BN {
106
109
  // number and BN inputs (no `.eqn` needed) - numbers will be converted
107
110
  return super.eq(isHex(other) ? hexToBn(other.toString(), {
108
111
  isLe: false,
109
- isNegative: this.#isSigned
112
+ isNegative: !this.isUnsigned
110
113
  }) : bnToBn(other));
111
114
  }
112
115
  /**
@@ -1,2 +1,3 @@
1
- export { AbstractArray } from './AbstractArray';
2
- export { AbstractInt } from './AbstractInt';
1
+ export { AbstractArray } from './Array';
2
+ export { AbstractBase } from './Base';
3
+ export { AbstractInt } from './Int';
package/abstract/index.js CHANGED
@@ -1,4 +1,5 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
- export { AbstractArray } from "./AbstractArray.js";
4
- export { AbstractInt } from "./AbstractInt.js";
3
+ export { AbstractArray } from "./Array.js";
4
+ export { AbstractBase } from "./Base.js";
5
+ export { AbstractInt } from "./Int.js";
package/base/Compact.d.ts CHANGED
@@ -21,8 +21,6 @@ export declare class Compact<T extends INumber> implements ICompact<T> {
21
21
  readonly initialU8aLength?: number;
22
22
  constructor(registry: Registry, Type: CodecClass<T> | string, value?: Compact<T> | AnyNumber, { definition, setDefinition }?: Options<T>);
23
23
  static with<O extends INumber>(Type: CodecClass<O> | string): CodecClass<Compact<O>>;
24
- /** @internal */
25
- static decodeCompact<T extends INumber>(registry: Registry, Type: CodecClass<T>, value: Compact<T> | AnyNumber): [T, number];
26
24
  /**
27
25
  * @description The length of the value when encoded as a Uint8Array
28
26
  */
package/base/Compact.js CHANGED
@@ -6,6 +6,20 @@ import { typeToConstructor } from "../utils/index.js";
6
6
  function noopSetDefinition(d) {
7
7
  return d;
8
8
  }
9
+
10
+ function decodeCompact(registry, Type, value) {
11
+ if (isU8a(value)) {
12
+ const [decodedLength, bn] = (value[0] & 0b11) < 0b11 ? compactFromU8aLim(value) : compactFromU8a(value);
13
+ return [new Type(registry, bn), decodedLength];
14
+ } else if (value instanceof Compact) {
15
+ const raw = value.unwrap();
16
+ return raw instanceof Type ? [raw, 0] : [new Type(registry, raw), 0];
17
+ } else if (value instanceof Type) {
18
+ return [value, 0];
19
+ }
20
+
21
+ return [new Type(registry, value), 0];
22
+ }
9
23
  /**
10
24
  * @name Compact
11
25
  * @description
@@ -26,7 +40,7 @@ export class Compact {
26
40
  } = {}) {
27
41
  this.registry = registry;
28
42
  this.#Type = definition || setDefinition(typeToConstructor(registry, Type));
29
- const [raw, decodedLength] = Compact.decodeCompact(registry, this.#Type, value);
43
+ const [raw, decodedLength] = decodeCompact(registry, this.#Type, value);
30
44
  this.initialU8aLength = decodedLength;
31
45
  this.#raw = raw;
32
46
  }
@@ -46,21 +60,6 @@ export class Compact {
46
60
 
47
61
  };
48
62
  }
49
- /** @internal */
50
-
51
-
52
- static decodeCompact(registry, Type, value) {
53
- if (isU8a(value)) {
54
- const [decodedLength, bn] = (value[0] & 0b11) < 0b11 ? compactFromU8aLim(value) : compactFromU8a(value);
55
- return [new Type(registry, bn), decodedLength];
56
- } else if (value instanceof Compact) {
57
- return value.#raw instanceof Type ? [value.#raw, 0] : [new Type(registry, value.#raw), 0];
58
- } else if (value instanceof Type) {
59
- return [value, 0];
60
- }
61
-
62
- return [new Type(registry, value), 0];
63
- }
64
63
  /**
65
64
  * @description The length of the value when encoded as a Uint8Array
66
65
  */
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, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
3
+ import { 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
 
@@ -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
 
@@ -72,10 +75,14 @@ function extractDef(registry, _def) {
72
75
 
73
76
  function createFromValue(registry, def, index = 0, value) {
74
77
  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(', ')}`);
78
+
79
+ if (isUndefined(entry)) {
80
+ throw new Error(`Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`);
81
+ }
82
+
76
83
  return {
77
84
  index,
78
- value: isCodec(value) && value instanceof entry.Type ? value : new entry.Type(registry, value)
85
+ value: value instanceof entry.Type ? value : new entry.Type(registry, value)
79
86
  };
80
87
  }
81
88
 
@@ -85,7 +92,10 @@ function decodeFromJSON(registry, def, key, value) {
85
92
  const keys = Object.keys(def).map(k => k.toLowerCase());
86
93
  const keyLower = key.toLowerCase();
87
94
  const index = keys.indexOf(keyLower);
88
- assert(index !== -1, () => `Cannot map Enum JSON, unable to find '${key}' in ${keys.join(', ')}`);
95
+
96
+ if (index === -1) {
97
+ throw new Error(`Cannot map Enum JSON, unable to find '${key}' in ${keys.join(', ')}`);
98
+ }
89
99
 
90
100
  try {
91
101
  return createFromValue(registry, def, Object.values(def)[index].index, value);
@@ -153,7 +163,7 @@ export class Enum {
153
163
  this.#indexes = Object.values(def).map(({
154
164
  index
155
165
  }) => index);
156
- this.#entryIndex = this.#indexes.indexOf(decoded.index) || 0;
166
+ this.#entryIndex = this.#indexes.indexOf(decoded.index);
157
167
  this.#raw = decoded.value;
158
168
 
159
169
  if (this.#raw.initialU8aLength) {
@@ -167,7 +177,7 @@ export class Enum {
167
177
  const isKeys = new Array(keys.length);
168
178
 
169
179
  for (let i = 0; i < keys.length; i++) {
170
- const name = stringPascalCase(keys[i].replace(' ', '_'));
180
+ const name = stringPascalCase(keys[i]);
171
181
  asKeys[i] = `as${name}`;
172
182
  isKeys[i] = `is${name}`;
173
183
  }
@@ -184,7 +194,10 @@ export class Enum {
184
194
  });
185
195
  objectProperties(this, isKeys, (_, i) => this.type === keys[i]);
186
196
  objectProperties(this, asKeys, (k, i) => {
187
- assert(this[isKeys[i]], () => `Cannot convert '${this.type}' via ${k}`);
197
+ if (!this[isKeys[i]]) {
198
+ throw new Error(`Cannot convert '${this.type}' via ${k}`);
199
+ }
200
+
188
201
  return this.value;
189
202
  });
190
203
  }
@@ -303,7 +316,7 @@ export class Enum {
303
316
  return this.type === other;
304
317
  } else if (isHex(other)) {
305
318
  return this.toHex() === other;
306
- } else if (isCodec(other) && other instanceof Enum) {
319
+ } else if (other instanceof Enum) {
307
320
  return this.index === other.index && this.value.eq(other.value);
308
321
  } else if (isObject(other)) {
309
322
  return this.value.eq(other[this.type]);
@@ -318,7 +331,7 @@ export class Enum {
318
331
 
319
332
 
320
333
  inspect() {
321
- if (this.isBasic) {
334
+ if (this.#isBasic) {
322
335
  return {
323
336
  outer: [new Uint8Array([this.index])]
324
337
  };
package/base/Int.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { AnyNumber, CodecClass, Registry, UIntBitLength } from '../types';
2
- import { AbstractInt } from '../abstract/AbstractInt';
2
+ import { AbstractInt } from '../abstract/Int';
3
3
  /**
4
4
  * @name Int
5
5
  * @description
package/base/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 { AbstractInt } from "../abstract/AbstractInt.js";
3
+ import { AbstractInt } from "../abstract/Int.js";
4
4
  /**
5
5
  * @name Int
6
6
  * @description
package/base/Null.d.ts CHANGED
@@ -6,22 +6,16 @@ import type { Codec, Inspect, IU8a, Registry } from '../types';
6
6
  * Implements a type that does not contain anything (apart from `null`)
7
7
  */
8
8
  export declare class Null implements Codec {
9
+ readonly encodedLength = 0;
10
+ readonly isEmpty = true;
9
11
  readonly registry: Registry;
10
12
  createdAtHash?: IU8a;
11
13
  readonly initialU8aLength = 0;
12
14
  constructor(registry: Registry);
13
- /**
14
- * @description The length of the value when encoded as a Uint8Array
15
- */
16
- get encodedLength(): number;
17
15
  /**
18
16
  * @description returns a hash of the contents
19
17
  */
20
18
  get hash(): IU8a;
21
- /**
22
- * @description Checks if the value is an empty value (always true)
23
- */
24
- get isEmpty(): boolean;
25
19
  /**
26
20
  * @description Compares the value of the input to see if there is a match
27
21
  */
package/base/Null.js CHANGED
@@ -8,20 +8,14 @@ import { isNull } from '@polkadot/util';
8
8
  */
9
9
 
10
10
  export class Null {
11
+ encodedLength = 0;
12
+ isEmpty = true;
11
13
  // Added for compatibility reasons, e.g. see Option
12
14
  initialU8aLength = 0;
13
15
 
14
16
  constructor(registry) {
15
17
  this.registry = registry;
16
18
  }
17
- /**
18
- * @description The length of the value when encoded as a Uint8Array
19
- */
20
-
21
-
22
- get encodedLength() {
23
- return 0;
24
- }
25
19
  /**
26
20
  * @description returns a hash of the contents
27
21
  */
@@ -30,14 +24,6 @@ export class Null {
30
24
  get hash() {
31
25
  throw new Error('.hash is not implemented on Null');
32
26
  }
33
- /**
34
- * @description Checks if the value is an empty value (always true)
35
- */
36
-
37
-
38
- get isEmpty() {
39
- return true;
40
- }
41
27
  /**
42
28
  * @description Compares the value of the input to see if there is a match
43
29
  */
package/base/Option.d.ts CHANGED
@@ -16,16 +16,13 @@ export declare class Option<T extends Codec> implements IOption<T> {
16
16
  #private;
17
17
  readonly registry: Registry;
18
18
  createdAtHash?: IU8a;
19
+ initialU8aLength?: number;
19
20
  constructor(registry: Registry, typeName: CodecClass<T> | string, value?: unknown, { definition, setDefinition }?: Options<T>);
20
21
  static with<O extends Codec>(Type: CodecClass<O> | string): CodecClass<Option<O>>;
21
22
  /**
22
23
  * @description The length of the value when encoded as a Uint8Array
23
24
  */
24
25
  get encodedLength(): number;
25
- /**
26
- * @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
27
- */
28
- get initialU8aLength(): number | undefined;
29
26
  /**
30
27
  * @description returns a hash of the contents
31
28
  */
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
 
@@ -58,7 +58,6 @@ function decodeOption(registry, Type, value) {
58
58
 
59
59
  export class Option {
60
60
  #Type;
61
- #initialU8aLength;
62
61
  #raw;
63
62
 
64
63
  constructor(registry, typeName, value, {
@@ -72,7 +71,7 @@ export class Option {
72
71
  this.#raw = decoded;
73
72
 
74
73
  if (decoded && decoded.initialU8aLength) {
75
- this.#initialU8aLength = 1 + decoded.initialU8aLength;
74
+ this.initialU8aLength = 1 + decoded.initialU8aLength;
76
75
  }
77
76
  }
78
77
 
@@ -103,14 +102,6 @@ export class Option {
103
102
  // boolean byte (has value, doesn't have) along with wrapped length
104
103
  return 1 + this.#raw.encodedLength;
105
104
  }
106
- /**
107
- * @description The length of the initial encoded value (Only available when constructed from a Uint8Array)
108
- */
109
-
110
-
111
- get initialU8aLength() {
112
- return this.#initialU8aLength;
113
- }
114
105
  /**
115
106
  * @description returns a hash of the contents
116
107
  */
@@ -253,7 +244,10 @@ export class Option {
253
244
 
254
245
 
255
246
  unwrap() {
256
- assert(this.isSome, 'Option: unwrapping a None value');
247
+ if (this.isNone) {
248
+ throw new Error('Option: unwrapping a None value');
249
+ }
250
+
257
251
  return this.#raw;
258
252
  }
259
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/Tuple.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { AnyTupleValue, Codec, CodecClass, Inspect, ITuple, Registry } from '../types';
2
- import { AbstractArray } from '../abstract/AbstractArray';
2
+ import { AbstractArray } from '../abstract/Array';
3
3
  declare type TupleType = (CodecClass | string);
4
4
  declare type TupleTypes = TupleType[] | {
5
5
  [index: string]: CodecClass | string;
package/base/Tuple.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 { isFunction, isHex, isString, isU8a, stringify, u8aConcatStrict, u8aToU8a } from '@polkadot/util';
4
- import { AbstractArray } from "../abstract/AbstractArray.js";
4
+ import { AbstractArray } from "../abstract/Array.js";
5
5
  import { decodeU8a, mapToTypeMap, typeToConstructor } from "../utils/index.js";
6
6
 
7
7
  function noopSetDefinition(d) {
@@ -10,7 +10,7 @@ function noopSetDefinition(d) {
10
10
  /** @internal */
11
11
 
12
12
 
13
- function decodeTuple(registry, result, Classes, value) {
13
+ function decodeTuple(registry, result, value, Classes) {
14
14
  if (isU8a(value) || isHex(value)) {
15
15
  return decodeU8a(registry, result, u8aToU8a(value), Classes);
16
16
  }
@@ -45,8 +45,7 @@ export class Tuple extends AbstractArray {
45
45
  } = {}) {
46
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
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;
48
+ this.initialU8aLength = (isU8a(value) ? decodeU8a(registry, this, value, Classes) : decodeTuple(registry, this, value, Classes))[1];
50
49
  this.#Types = Classes;
51
50
  }
52
51
 
package/base/UInt.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { CodecClass, UIntBitLength } from '../types';
2
- import { AbstractInt } from '../abstract/AbstractInt';
2
+ import { AbstractInt } from '../abstract/Int';
3
3
  /**
4
4
  * @name UInt
5
5
  * @description
package/base/UInt.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 { AbstractInt } from "../abstract/AbstractInt.js";
3
+ import { AbstractInt } from "../abstract/Int.js";
4
4
  /**
5
5
  * @name UInt
6
6
  * @description
package/base/Vec.d.ts CHANGED
@@ -1,6 +1,6 @@
1
1
  import type { HexString } from '@polkadot/util/types';
2
2
  import type { Codec, CodecClass, Registry } from '../types';
3
- import { AbstractArray } from '../abstract/AbstractArray';
3
+ import { AbstractArray } from '../abstract/Array';
4
4
  interface Options<T> {
5
5
  definition?: CodecClass<T>;
6
6
  setDefinition?: (d: CodecClass<T>) => CodecClass<T>;
package/base/Vec.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
- import { assert, compactFromU8aLim, isU8a, logger, u8aToU8a } from '@polkadot/util';
4
- import { AbstractArray } from "../abstract/AbstractArray.js";
3
+ import { compactFromU8aLim, isU8a, logger, u8aToU8a } from '@polkadot/util';
4
+ import { AbstractArray } from "../abstract/Array.js";
5
5
  import { decodeU8aVec, typeToConstructor } from "../utils/index.js";
6
6
  const MAX_LENGTH = 64 * 1024;
7
7
  const l = logger('Vec');
@@ -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
 
@@ -59,8 +63,7 @@ export class Vec extends AbstractArray {
59
63
  const [decodeFrom, length, startAt] = decodeVecLength(value);
60
64
  super(registry, length);
61
65
  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;
66
+ this.initialU8aLength = (isU8a(decodeFrom) ? decodeU8aVec(registry, this, decodeFrom, startAt, this.#Type) : decodeVec(registry, this, decodeFrom, startAt, this.#Type))[0];
64
67
  }
65
68
 
66
69
  static with(Type) {
package/base/VecAny.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { Codec } from '../types';
2
- import { AbstractArray } from '../abstract/AbstractArray';
2
+ import { AbstractArray } from '../abstract/Array';
3
3
  /**
4
4
  * @name VecAny
5
5
  * @description
package/base/VecAny.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 { AbstractArray } from "../abstract/AbstractArray.js";
3
+ import { AbstractArray } from "../abstract/Array.js";
4
4
  /**
5
5
  * @name VecAny
6
6
  * @description
@@ -1,6 +1,6 @@
1
1
  import type { HexString } from '@polkadot/util/types';
2
2
  import type { Codec, CodecClass, Inspect, Registry } from '../types';
3
- import { AbstractArray } from '../abstract/AbstractArray';
3
+ import { AbstractArray } from '../abstract/Array';
4
4
  interface Options<T> {
5
5
  definition?: CodecClass<T>;
6
6
  setDefinition?: (d: CodecClass<T>) => CodecClass<T>;