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

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.
@@ -15,17 +15,21 @@ function toPercentage(value, divisor) {
15
15
 
16
16
 
17
17
  function decodeAbstractInt(value, isNegative) {
18
- if (isBn(value)) {
19
- return value.toString();
20
- } else if (isHex(value, -1, true)) {
21
- return hexToBn(value, {
22
- isLe: false,
23
- isNegative
24
- }).toString();
25
- } else if (isNumber(value)) {
18
+ if (isNumber(value)) {
26
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');
20
+ return value;
27
21
  } else if (isString(value)) {
22
+ if (isHex(value, -1, true)) {
23
+ return hexToBn(value, {
24
+ isLe: false,
25
+ isNegative
26
+ }).toString();
27
+ }
28
+
28
29
  assert(!(value.includes('.') || value.includes(',') || value.includes('e')), 'String should not contain decimal points or scientific notation');
30
+ return value;
31
+ } else if (isBn(value)) {
32
+ return value.toString();
29
33
  }
30
34
 
31
35
  return bnToBn(value).toString();
package/base/Compact.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 { compactFromU8a, compactToU8a, isU8a } from '@polkadot/util';
3
+ import { compactFromU8a, compactFromU8aLim, compactToU8a, isU8a } from '@polkadot/util';
4
4
  import { typeToConstructor } from "../utils/index.js";
5
5
 
6
6
  function noopSetDefinition(d) {
@@ -51,10 +51,10 @@ export class Compact {
51
51
 
52
52
  static decodeCompact(registry, Type, value) {
53
53
  if (isU8a(value)) {
54
- const [decodedLength, bn] = compactFromU8a(value);
54
+ const [decodedLength, bn] = (value[0] & 0b11) < 0b11 ? compactFromU8aLim(value) : compactFromU8a(value);
55
55
  return [new Type(registry, bn), decodedLength];
56
56
  } else if (value instanceof Compact) {
57
- return [new Type(registry, value.#raw), 0];
57
+ return value.#raw instanceof Type ? [value.#raw, 0] : [new Type(registry, value.#raw), 0];
58
58
  } else if (value instanceof Type) {
59
59
  return [value, 0];
60
60
  }
@@ -24,17 +24,21 @@ function toPercentage(value, divisor) {
24
24
 
25
25
 
26
26
  function decodeAbstractInt(value, isNegative) {
27
- if ((0, _util.isBn)(value)) {
28
- return value.toString();
29
- } else if ((0, _util.isHex)(value, -1, true)) {
30
- return (0, _util.hexToBn)(value, {
31
- isLe: false,
32
- isNegative
33
- }).toString();
34
- } else if ((0, _util.isNumber)(value)) {
27
+ if ((0, _util.isNumber)(value)) {
35
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');
29
+ return value;
36
30
  } else if ((0, _util.isString)(value)) {
31
+ if ((0, _util.isHex)(value, -1, true)) {
32
+ return (0, _util.hexToBn)(value, {
33
+ isLe: false,
34
+ isNegative
35
+ }).toString();
36
+ }
37
+
37
38
  (0, _util.assert)(!(value.includes('.') || value.includes(',') || value.includes('e')), 'String should not contain decimal points or scientific notation');
39
+ return value;
40
+ } else if ((0, _util.isBn)(value)) {
41
+ return value.toString();
38
42
  }
39
43
 
40
44
  return (0, _util.bnToBn)(value).toString();
@@ -61,10 +61,10 @@ class Compact {
61
61
 
62
62
  static decodeCompact(registry, Type, value) {
63
63
  if ((0, _util.isU8a)(value)) {
64
- const [decodedLength, bn] = (0, _util.compactFromU8a)(value);
64
+ const [decodedLength, bn] = (value[0] & 0b11) < 0b11 ? (0, _util.compactFromU8aLim)(value) : (0, _util.compactFromU8a)(value);
65
65
  return [new Type(registry, bn), decodedLength];
66
66
  } else if (value instanceof Compact) {
67
- return [new Type(registry, value.#raw), 0];
67
+ return value.#raw instanceof Type ? [value.#raw, 0] : [new Type(registry, value.#raw), 0];
68
68
  } else if (value instanceof Type) {
69
69
  return [value, 0];
70
70
  }
@@ -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-0'
14
+ version: '8.8.3-1'
15
15
  };
16
16
  exports.packageInfo = packageInfo;
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./cjs/detectPackage.js"
21
21
  ],
22
22
  "type": "module",
23
- "version": "8.8.3-0",
23
+ "version": "8.8.3-1",
24
24
  "main": "./cjs/index.js",
25
25
  "module": "./index.js",
26
26
  "types": "./index.d.ts",
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-0'
8
+ version: '8.8.3-1'
9
9
  };