@polkadot/types-codec 8.3.3-13 → 8.3.3-16

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.
@@ -14,13 +14,8 @@ function toPercentage(value, divisor) {
14
14
  /** @internal */
15
15
 
16
16
 
17
- function decodeAbstractInt(value, bitLength, isNegative) {
18
- if (isU8a(value)) {
19
- return u8aToBn(value.subarray(0, bitLength / 8), {
20
- isLe: true,
21
- isNegative
22
- }).toString();
23
- } else if (isBn(value)) {
17
+ function decodeAbstractInt(value, isNegative) {
18
+ if (isBn(value)) {
24
19
  return value.toString();
25
20
  } else if (isHex(value, -1, true)) {
26
21
  return hexToBn(value, {
@@ -54,7 +49,7 @@ export class AbstractInt extends BN {
54
49
  isU8a(value) ? u8aToBn(value.subarray(0, bitLength / 8), {
55
50
  isLe: true,
56
51
  isNegative: isSigned
57
- }).toString() : decodeAbstractInt(value, bitLength, isSigned));
52
+ }).toString() : decodeAbstractInt(value, isSigned));
58
53
  this.registry = registry;
59
54
  this.#bitLength = bitLength;
60
55
  this.encodedLength = this.#bitLength / 8;
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, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
3
+ import { assert, isCodec, isHex, isNumber, isObject, isString, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcat, 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
 
@@ -72,7 +72,7 @@ function createFromValue(registry, def, index = 0, value) {
72
72
  assert(!isUndefined(entry), () => `Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`);
73
73
  return {
74
74
  index,
75
- value: value instanceof entry.Type ? value : new entry.Type(registry, value)
75
+ value: isCodec(value) && value instanceof entry.Type ? value : new entry.Type(registry, value)
76
76
  };
77
77
  }
78
78
 
@@ -241,7 +241,7 @@ export class Enum {
241
241
 
242
242
 
243
243
  get isNull() {
244
- return this.#raw instanceof Null;
244
+ return this.isNone;
245
245
  }
246
246
  /**
247
247
  * @description The available keys for this enum
@@ -290,7 +290,7 @@ export class Enum {
290
290
  return this.type === other;
291
291
  } else if (isHex(other)) {
292
292
  return this.toHex() === other;
293
- } else if (other instanceof Enum) {
293
+ } else if (isCodec(other) && other instanceof Enum) {
294
294
  return this.index === other.index && this.value.eq(other.value);
295
295
  } else if (isObject(other)) {
296
296
  return this.value.eq(other[this.type]);
@@ -393,7 +393,7 @@ export class Enum {
393
393
 
394
394
 
395
395
  toString() {
396
- return this.isNull ? this.type : stringify(this.toJSON());
396
+ return this.isNone ? this.type : stringify(this.toJSON());
397
397
  }
398
398
  /**
399
399
  * @description Encodes the value as a Uint8Array as per the SCALE specifications
@@ -23,13 +23,8 @@ function toPercentage(value, divisor) {
23
23
  /** @internal */
24
24
 
25
25
 
26
- function decodeAbstractInt(value, bitLength, isNegative) {
27
- if ((0, _util.isU8a)(value)) {
28
- return (0, _util.u8aToBn)(value.subarray(0, bitLength / 8), {
29
- isLe: true,
30
- isNegative
31
- }).toString();
32
- } else if ((0, _util.isBn)(value)) {
26
+ function decodeAbstractInt(value, isNegative) {
27
+ if ((0, _util.isBn)(value)) {
33
28
  return value.toString();
34
29
  } else if ((0, _util.isHex)(value, -1, true)) {
35
30
  return (0, _util.hexToBn)(value, {
@@ -66,7 +61,7 @@ class AbstractInt extends _util.BN {
66
61
  (0, _util.isU8a)(value) ? (0, _util.u8aToBn)(value.subarray(0, bitLength / 8), {
67
62
  isLe: true,
68
63
  isNegative: isSigned
69
- }).toString() : decodeAbstractInt(value, bitLength, isSigned));
64
+ }).toString() : decodeAbstractInt(value, isSigned));
70
65
  this.registry = registry;
71
66
  this.#bitLength = bitLength;
72
67
  this.encodedLength = this.#bitLength / 8;
package/cjs/base/Enum.js CHANGED
@@ -86,7 +86,7 @@ function createFromValue(registry, def) {
86
86
  (0, _util.assert)(!(0, _util.isUndefined)(entry), () => `Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`);
87
87
  return {
88
88
  index,
89
- value: value instanceof entry.Type ? value : new entry.Type(registry, value)
89
+ value: (0, _util.isCodec)(value) && value instanceof entry.Type ? value : new entry.Type(registry, value)
90
90
  };
91
91
  }
92
92
 
@@ -258,7 +258,7 @@ class Enum {
258
258
 
259
259
 
260
260
  get isNull() {
261
- return this.#raw instanceof _Null.Null;
261
+ return this.isNone;
262
262
  }
263
263
  /**
264
264
  * @description The available keys for this enum
@@ -307,7 +307,7 @@ class Enum {
307
307
  return this.type === other;
308
308
  } else if ((0, _util.isHex)(other)) {
309
309
  return this.toHex() === other;
310
- } else if (other instanceof Enum) {
310
+ } else if ((0, _util.isCodec)(other) && other instanceof Enum) {
311
311
  return this.index === other.index && this.value.eq(other.value);
312
312
  } else if ((0, _util.isObject)(other)) {
313
313
  return this.value.eq(other[this.type]);
@@ -411,7 +411,7 @@ class Enum {
411
411
 
412
412
 
413
413
  toString() {
414
- return this.isNull ? this.type : (0, _util.stringify)(this.toJSON());
414
+ return this.isNone ? this.type : (0, _util.stringify)(this.toJSON());
415
415
  }
416
416
  /**
417
417
  * @description Encodes the value as a Uint8Array as per the SCALE specifications
@@ -23,7 +23,7 @@ function decodeSetFromU8a(registry, ValClass, u8a) {
23
23
  output.add(values[i]);
24
24
  }
25
25
 
26
- return [output, decodedLength];
26
+ return [ValClass, output, decodedLength];
27
27
  }
28
28
  /** @internal */
29
29
 
@@ -38,7 +38,7 @@ function decodeSetFromSet(registry, ValClass, value) {
38
38
  throw error;
39
39
  }
40
40
  });
41
- return [output, 0];
41
+ return [ValClass, output, 0];
42
42
  }
43
43
  /**
44
44
  * Decode input to pass into constructor.
@@ -57,13 +57,11 @@ function decodeSetFromSet(registry, ValClass, value) {
57
57
 
58
58
 
59
59
  function decodeSet(registry, valType, value) {
60
- if (!value) {
61
- return [new Set(), 0];
62
- }
63
-
64
60
  const ValClass = (0, _utils.typeToConstructor)(registry, valType);
65
61
 
66
- if ((0, _util.isU8a)(value) || (0, _util.isHex)(value)) {
62
+ if (!value) {
63
+ return [ValClass, new Set(), 0];
64
+ } else if ((0, _util.isU8a)(value) || (0, _util.isHex)(value)) {
67
65
  return decodeSetFromU8a(registry, ValClass, (0, _util.u8aToU8a)(value));
68
66
  } else if (Array.isArray(value) || value instanceof Set) {
69
67
  return decodeSetFromSet(registry, ValClass, value);
@@ -76,11 +74,11 @@ class BTreeSet extends Set {
76
74
  #ValClass;
77
75
 
78
76
  constructor(registry, valType, rawValue) {
79
- const [values, decodedLength] = decodeSet(registry, valType, rawValue);
77
+ const [ValClass, values, decodedLength] = decodeSet(registry, valType, rawValue);
80
78
  super((0, _utils.sortSet)(values));
81
79
  this.registry = registry;
82
80
  this.initialU8aLength = decodedLength;
83
- this.#ValClass = (0, _utils.typeToConstructor)(registry, valType);
81
+ this.#ValClass = ValClass;
84
82
  }
85
83
 
86
84
  static with(valType) {
@@ -27,20 +27,6 @@ function decodeBytesU8a(value) {
27
27
  (0, _util.assert)(total <= value.length, () => `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
28
28
  return [value.subarray(offset, total), total];
29
29
  }
30
- /** @internal */
31
-
32
-
33
- function decodeBytes(value) {
34
- if (Array.isArray(value) || (0, _util.isString)(value)) {
35
- return [(0, _util.u8aToU8a)(value), 0];
36
- } else if (!(value instanceof _Raw.Raw) && (0, _util.isU8a)(value)) {
37
- // We are ensuring we are not a Raw instance. In the case of a Raw we already have gotten
38
- // rid of the length, i.e. new Bytes(new Bytes(...)) will work as expected
39
- return decodeBytesU8a(value);
40
- }
41
-
42
- return [value, 0];
43
- }
44
30
  /**
45
31
  * @name Bytes
46
32
  * @description
@@ -52,7 +38,7 @@ function decodeBytes(value) {
52
38
 
53
39
  class Bytes extends _Raw.Raw {
54
40
  constructor(registry, value) {
55
- const [u8a, decodedLength] = decodeBytes(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];
56
42
  super(registry, u8a, decodedLength);
57
43
  }
58
44
  /**
@@ -36,7 +36,7 @@ function decodeMapFromU8a(registry, KeyClass, ValClass, u8a) {
36
36
  output.set(values[i], values[i + 1]);
37
37
  }
38
38
 
39
- return [output, offset + decodedLength];
39
+ return [KeyClass, ValClass, output, offset + decodedLength];
40
40
  }
41
41
  /** @internal */
42
42
 
@@ -55,7 +55,7 @@ function decodeMapFromMap(registry, KeyClass, ValClass, value) {
55
55
  }
56
56
  }
57
57
 
58
- return [output, 0];
58
+ return [KeyClass, ValClass, output, 0];
59
59
  }
60
60
  /**
61
61
  * Decode input to pass into constructor.
@@ -79,7 +79,7 @@ function decodeMap(registry, keyType, valType, value) {
79
79
  const ValClass = (0, _utils.typeToConstructor)(registry, valType);
80
80
 
81
81
  if (!value) {
82
- return [new Map(), 0];
82
+ return [KeyClass, ValClass, new Map(), 0];
83
83
  } else if ((0, _util.isU8a)(value) || (0, _util.isHex)(value)) {
84
84
  return decodeMapFromU8a(registry, KeyClass, ValClass, (0, _util.u8aToU8a)(value));
85
85
  } else if (value instanceof Map) {
@@ -98,12 +98,12 @@ class CodecMap extends Map {
98
98
 
99
99
  constructor(registry, keyType, valType, rawValue) {
100
100
  let type = arguments.length > 4 && arguments[4] !== undefined ? arguments[4] : 'HashMap';
101
- const [decoded, decodedLength] = decodeMap(registry, keyType, valType, rawValue);
101
+ const [KeyClass, ValClass, decoded, decodedLength] = decodeMap(registry, keyType, valType, rawValue);
102
102
  super(type === 'BTreeMap' ? (0, _utils.sortMap)(decoded) : decoded);
103
103
  this.registry = registry;
104
104
  this.initialU8aLength = decodedLength;
105
- this.#KeyClass = (0, _utils.typeToConstructor)(registry, keyType);
106
- this.#ValClass = (0, _utils.typeToConstructor)(registry, valType);
105
+ this.#KeyClass = KeyClass;
106
+ this.#ValClass = ValClass;
107
107
  this.#type = type;
108
108
  }
109
109
  /**
@@ -10,29 +10,17 @@ var _util = require("@polkadot/util");
10
10
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
11
11
  // SPDX-License-Identifier: Apache-2.0
12
12
 
13
- /** @internal */
14
- function decodeBool(value) {
15
- if ((0, _util.isU8a)(value)) {
16
- return value[0] === 1;
17
- } else if (value instanceof Boolean) {
18
- return value.valueOf();
19
- }
20
-
21
- return !!value;
22
- }
23
13
  /**
24
14
  * @name bool
25
15
  * @description
26
16
  * Representation for a boolean value in the system. It extends the base JS `Boolean` class
27
17
  * @noInheritDoc
28
18
  */
29
-
30
-
31
19
  class bool extends Boolean {
32
20
  // eslint-disable-next-line @typescript-eslint/ban-types
33
21
  constructor(registry) {
34
22
  let value = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : false;
35
- super(decodeBool(value));
23
+ super((0, _util.isU8a)(value) ? value[0] === 1 : value instanceof Boolean ? value.valueOf() : !!value);
36
24
  this.registry = registry;
37
25
  }
38
26
  /**
@@ -13,7 +13,7 @@ const BITLENGTH = 64;
13
13
 
14
14
  function decodeDate(value) {
15
15
  if ((0, _util.isU8a)(value)) {
16
- value = (0, _util.u8aToBn)(value.subarray(0, BITLENGTH / 8), true);
16
+ value = (0, _util.u8aToBn)(value.subarray(0, BITLENGTH / 8));
17
17
  } else if (value instanceof Date) {
18
18
  return value;
19
19
  } else if ((0, _util.isString)(value)) {
@@ -45,8 +45,6 @@ function decodeText(value) {
45
45
  * object, inheriting all methods exposed from `String`.
46
46
  * @noInheritDoc
47
47
  */
48
- // TODO
49
- // - Strings should probably be trimmed (docs do come through with extra padding)
50
48
 
51
49
 
52
50
  class Text extends String {
@@ -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.3.3-13'
14
+ version: '8.3.3-16'
15
15
  };
16
16
  exports.packageInfo = packageInfo;
@@ -14,7 +14,7 @@ function decodeSetFromU8a(registry, ValClass, u8a) {
14
14
  output.add(values[i]);
15
15
  }
16
16
 
17
- return [output, decodedLength];
17
+ return [ValClass, output, decodedLength];
18
18
  }
19
19
  /** @internal */
20
20
 
@@ -29,7 +29,7 @@ function decodeSetFromSet(registry, ValClass, value) {
29
29
  throw error;
30
30
  }
31
31
  });
32
- return [output, 0];
32
+ return [ValClass, output, 0];
33
33
  }
34
34
  /**
35
35
  * Decode input to pass into constructor.
@@ -48,13 +48,11 @@ function decodeSetFromSet(registry, ValClass, value) {
48
48
 
49
49
 
50
50
  function decodeSet(registry, valType, value) {
51
- if (!value) {
52
- return [new Set(), 0];
53
- }
54
-
55
51
  const ValClass = typeToConstructor(registry, valType);
56
52
 
57
- if (isU8a(value) || isHex(value)) {
53
+ if (!value) {
54
+ return [ValClass, new Set(), 0];
55
+ } else if (isU8a(value) || isHex(value)) {
58
56
  return decodeSetFromU8a(registry, ValClass, u8aToU8a(value));
59
57
  } else if (Array.isArray(value) || value instanceof Set) {
60
58
  return decodeSetFromSet(registry, ValClass, value);
@@ -67,11 +65,11 @@ export class BTreeSet extends Set {
67
65
  #ValClass;
68
66
 
69
67
  constructor(registry, valType, rawValue) {
70
- const [values, decodedLength] = decodeSet(registry, valType, rawValue);
68
+ const [ValClass, values, decodedLength] = decodeSet(registry, valType, rawValue);
71
69
  super(sortSet(values));
72
70
  this.registry = registry;
73
71
  this.initialU8aLength = decodedLength;
74
- this.#ValClass = typeToConstructor(registry, valType);
72
+ this.#ValClass = ValClass;
75
73
  }
76
74
 
77
75
  static with(valType) {
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, compactFromU8a, compactToU8a, isString, isU8a, u8aToU8a } from '@polkadot/util';
3
+ import { assert, compactAddLength, compactFromU8a, compactToU8a, isCodec, 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;
@@ -18,20 +18,6 @@ function decodeBytesU8a(value) {
18
18
  assert(total <= value.length, () => `Bytes: required length less than remainder, expected at least ${total}, found ${value.length}`);
19
19
  return [value.subarray(offset, total), total];
20
20
  }
21
- /** @internal */
22
-
23
-
24
- function decodeBytes(value) {
25
- if (Array.isArray(value) || isString(value)) {
26
- return [u8aToU8a(value), 0];
27
- } else if (!(value instanceof Raw) && isU8a(value)) {
28
- // We are ensuring we are not a Raw instance. In the case of a Raw we already have gotten
29
- // rid of the length, i.e. new Bytes(new Bytes(...)) will work as expected
30
- return decodeBytesU8a(value);
31
- }
32
-
33
- return [value, 0];
34
- }
35
21
  /**
36
22
  * @name Bytes
37
23
  * @description
@@ -43,7 +29,7 @@ function decodeBytes(value) {
43
29
 
44
30
  export class Bytes extends Raw {
45
31
  constructor(registry, value) {
46
- const [u8a, decodedLength] = decodeBytes(value);
32
+ const [u8a, decodedLength] = isU8a(value) && !(isCodec(value) && value instanceof Raw) ? decodeBytesU8a(value) : Array.isArray(value) || isString(value) ? [u8aToU8a(value), 0] : [value, 0];
47
33
  super(registry, u8a, decodedLength);
48
34
  }
49
35
  /**
package/extended/Map.js CHANGED
@@ -24,7 +24,7 @@ function decodeMapFromU8a(registry, KeyClass, ValClass, u8a) {
24
24
  output.set(values[i], values[i + 1]);
25
25
  }
26
26
 
27
- return [output, offset + decodedLength];
27
+ return [KeyClass, ValClass, output, offset + decodedLength];
28
28
  }
29
29
  /** @internal */
30
30
 
@@ -43,7 +43,7 @@ function decodeMapFromMap(registry, KeyClass, ValClass, value) {
43
43
  }
44
44
  }
45
45
 
46
- return [output, 0];
46
+ return [KeyClass, ValClass, output, 0];
47
47
  }
48
48
  /**
49
49
  * Decode input to pass into constructor.
@@ -67,7 +67,7 @@ function decodeMap(registry, keyType, valType, value) {
67
67
  const ValClass = typeToConstructor(registry, valType);
68
68
 
69
69
  if (!value) {
70
- return [new Map(), 0];
70
+ return [KeyClass, ValClass, new Map(), 0];
71
71
  } else if (isU8a(value) || isHex(value)) {
72
72
  return decodeMapFromU8a(registry, KeyClass, ValClass, u8aToU8a(value));
73
73
  } else if (value instanceof Map) {
@@ -85,12 +85,12 @@ export class CodecMap extends Map {
85
85
  #type;
86
86
 
87
87
  constructor(registry, keyType, valType, rawValue, type = 'HashMap') {
88
- const [decoded, decodedLength] = decodeMap(registry, keyType, valType, rawValue);
88
+ const [KeyClass, ValClass, decoded, decodedLength] = decodeMap(registry, keyType, valType, rawValue);
89
89
  super(type === 'BTreeMap' ? sortMap(decoded) : decoded);
90
90
  this.registry = registry;
91
91
  this.initialU8aLength = decodedLength;
92
- this.#KeyClass = typeToConstructor(registry, keyType);
93
- this.#ValClass = typeToConstructor(registry, valType);
92
+ this.#KeyClass = KeyClass;
93
+ this.#ValClass = ValClass;
94
94
  this.#type = type;
95
95
  }
96
96
  /**
package/native/Bool.js CHANGED
@@ -1,17 +1,6 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
  import { isU8a, u8aToHex } from '@polkadot/util';
4
- /** @internal */
5
-
6
- function decodeBool(value) {
7
- if (isU8a(value)) {
8
- return value[0] === 1;
9
- } else if (value instanceof Boolean) {
10
- return value.valueOf();
11
- }
12
-
13
- return !!value;
14
- }
15
4
  /**
16
5
  * @name bool
17
6
  * @description
@@ -19,11 +8,10 @@ function decodeBool(value) {
19
8
  * @noInheritDoc
20
9
  */
21
10
 
22
-
23
11
  export class bool extends Boolean {
24
12
  // eslint-disable-next-line @typescript-eslint/ban-types
25
13
  constructor(registry, value = false) {
26
- super(decodeBool(value));
14
+ super(isU8a(value) ? value[0] === 1 : value instanceof Boolean ? value.valueOf() : !!value);
27
15
  this.registry = registry;
28
16
  }
29
17
  /**
package/native/Date.js CHANGED
@@ -5,7 +5,7 @@ const BITLENGTH = 64;
5
5
 
6
6
  function decodeDate(value) {
7
7
  if (isU8a(value)) {
8
- value = u8aToBn(value.subarray(0, BITLENGTH / 8), true);
8
+ value = u8aToBn(value.subarray(0, BITLENGTH / 8));
9
9
  } else if (value instanceof Date) {
10
10
  return value;
11
11
  } else if (isString(value)) {
package/native/Text.js CHANGED
@@ -36,8 +36,6 @@ function decodeText(value) {
36
36
  * object, inheriting all methods exposed from `String`.
37
37
  * @noInheritDoc
38
38
  */
39
- // TODO
40
- // - Strings should probably be trimmed (docs do come through with extra padding)
41
39
 
42
40
 
43
41
  export class Text extends String {
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./cjs/detectPackage.js"
21
21
  ],
22
22
  "type": "module",
23
- "version": "8.3.3-13",
23
+ "version": "8.3.3-16",
24
24
  "main": "./cjs/index.js",
25
25
  "module": "./index.js",
26
26
  "types": "./index.d.ts",
@@ -410,6 +410,6 @@
410
410
  },
411
411
  "dependencies": {
412
412
  "@babel/runtime": "^7.17.9",
413
- "@polkadot/util": "^9.1.2-11"
413
+ "@polkadot/util": "^9.2.1"
414
414
  }
415
415
  }
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.3.3-13'
8
+ version: '8.3.3-16'
9
9
  };