@polkadot/types-codec 9.12.1 → 9.13.2

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.
@@ -19,6 +19,9 @@ export declare abstract class AbstractBase<T extends Codec> implements Codec {
19
19
  * @description returns a hash of the contents
20
20
  */
21
21
  get hash(): IU8a;
22
+ /**
23
+ * @description returns the inner (wrapped value)
24
+ */
22
25
  get inner(): T;
23
26
  /**
24
27
  * @description Checks if the value is an empty value
@@ -60,6 +63,13 @@ export declare abstract class AbstractBase<T extends Codec> implements Codec {
60
63
  /**
61
64
  * @description Returns the base runtime type name for this instance
62
65
  */
63
- toRawType(): string;
66
+ abstract toRawType(): string;
67
+ /**
68
+ * @description Returns the inner wrapped value (equivalent to valueOf)
69
+ */
64
70
  unwrap(): T;
71
+ /**
72
+ * @description Returns the inner wrapped value
73
+ */
74
+ valueOf(): T;
65
75
  }
package/abstract/Base.js CHANGED
@@ -26,6 +26,10 @@ export class AbstractBase {
26
26
  get hash() {
27
27
  return this.registry.hash(this.toU8a());
28
28
  }
29
+
30
+ /**
31
+ * @description returns the inner (wrapped value)
32
+ */
29
33
  get inner() {
30
34
  return this.#raw;
31
35
  }
@@ -97,10 +101,18 @@ export class AbstractBase {
97
101
  /**
98
102
  * @description Returns the base runtime type name for this instance
99
103
  */
100
- toRawType() {
101
- return 'Base';
102
- }
104
+
105
+ /**
106
+ * @description Returns the inner wrapped value (equivalent to valueOf)
107
+ */
103
108
  unwrap() {
104
109
  return this.#raw;
105
110
  }
111
+
112
+ /**
113
+ * @description Returns the inner wrapped value
114
+ */
115
+ valueOf() {
116
+ return this.#raw;
117
+ }
106
118
  }
package/abstract/Int.d.ts CHANGED
@@ -16,7 +16,7 @@ export declare abstract class AbstractInt extends BN implements INumber {
16
16
  createdAtHash?: IU8a;
17
17
  initialU8aLength?: number;
18
18
  isStorageFallback?: boolean;
19
- constructor(registry: Registry, value?: AnyNumber, bitLength?: UIntBitLength, isSigned?: boolean);
19
+ constructor(registry: Registry, value?: AnyNumber | null, bitLength?: UIntBitLength, isSigned?: boolean);
20
20
  /**
21
21
  * @description returns a hash of the contents
22
22
  */
package/abstract/Int.js CHANGED
@@ -1,7 +1,7 @@
1
1
  // Copyright 2017-2023 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBn, isFunction, isHex, isNumber, isObject, isString, isU8a, u8aToBn, u8aToNumber } from '@polkadot/util';
4
+ import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBigInt, isBn, isFunction, isHex, isNumber, isObject, isString, isU8a, u8aToBn, u8aToNumber } from '@polkadot/util';
5
5
  export const DEFAULT_UINT_BITS = 64;
6
6
 
7
7
  // Maximum allowed integer for JS is 2^53 - 1, set limit at 52
@@ -9,6 +9,9 @@ export const DEFAULT_UINT_BITS = 64;
9
9
  const MAX_NUMBER_BITS = 52;
10
10
  const MUL_P = new BN(10000);
11
11
  const FORMATTERS = [['Perquintill', BN_QUINTILL], ['Perbill', BN_BILLION], ['Permill', BN_MILLION], ['Percent', BN_HUNDRED]];
12
+ function isToBn(value) {
13
+ return isFunction(value.toBn);
14
+ }
12
15
  function toPercentage(value, divisor) {
13
16
  return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`;
14
17
  }
@@ -31,9 +34,13 @@ function decodeAbstractInt(value, isNegative) {
31
34
  throw new Error('String should not contain decimal points or scientific notation');
32
35
  }
33
36
  return value;
34
- } else if (isBn(value)) {
37
+ } else if (isBn(value) || isBigInt(value)) {
35
38
  return value.toString();
36
- } else if (isObject(value) && !isFunction(value.toBn)) {
39
+ } else if (isObject(value)) {
40
+ if (isToBn(value)) {
41
+ return value.toBn().toString();
42
+ }
43
+
37
44
  // Allow the construction from an object with a single top-level key. This means that
38
45
  // single key objects can be treated equivalently to numbers, assuming they meet the
39
46
  // specific requirements. (This is useful in Weights 1.5 where Objects are compact)
@@ -41,13 +48,11 @@ function decodeAbstractInt(value, isNegative) {
41
48
  if (keys.length !== 1) {
42
49
  throw new Error('Unable to construct number from multi-key object');
43
50
  }
44
- const inner = value[keys[0]];
45
- if (!isString(inner) && !isNumber(inner)) {
46
- throw new Error('Unable to construct from object with non-string/non-number value');
47
- }
48
- return decodeAbstractInt(inner, isNegative);
51
+ return decodeAbstractInt(value[keys[0]], isNegative);
52
+ } else if (!value) {
53
+ return 0;
49
54
  }
50
- return bnToBn(value).toString();
55
+ throw new Error(`Unable to create BN from unknown type ${typeof value}`);
51
56
  }
52
57
 
53
58
  /**
@@ -0,0 +1,67 @@
1
+ import type { HexString } from '@polkadot/util/types';
2
+ import type { AnyJson, BareOpts, CodecObject, Inspect, IU8a, Registry, ToString } from '../types';
3
+ /**
4
+ * @name Object
5
+ * @description A type extends the Base class, when it holds a value
6
+ */
7
+ export declare abstract class AbstractObject<T extends ToString> implements CodecObject<T> {
8
+ readonly registry: Registry;
9
+ createdAtHash?: IU8a;
10
+ initialU8aLength?: number;
11
+ isStorageFallback?: boolean;
12
+ readonly $: T;
13
+ protected constructor(registry: Registry, value: T, initialU8aLength?: number);
14
+ /**
15
+ * @description The length of the value when encoded as a Uint8Array
16
+ */
17
+ get encodedLength(): number;
18
+ /**
19
+ * @description returns a hash of the contents
20
+ */
21
+ get hash(): IU8a;
22
+ /**
23
+ * @description Checks if the value is an empty value
24
+ */
25
+ abstract get isEmpty(): boolean;
26
+ /**
27
+ * @description Compares the value of the input to see if there is a match
28
+ */
29
+ abstract eq(other?: unknown): boolean;
30
+ /**
31
+ * @description Returns a breakdown of the hex encoding for this Codec
32
+ */
33
+ abstract inspect(): Inspect;
34
+ /**
35
+ * @description Returns a hex string representation of the value. isLe returns a LE (number-only) representation
36
+ */
37
+ abstract toHex(isLe?: boolean): HexString;
38
+ /**
39
+ * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
40
+ */
41
+ abstract toHuman(isExtended?: boolean): AnyJson;
42
+ /**
43
+ * @description Converts the Object to JSON, typically used for RPC transfers
44
+ */
45
+ abstract toJSON(): AnyJson;
46
+ /**
47
+ * @description Converts the value in a best-fit primitive form
48
+ */
49
+ abstract toPrimitive(): AnyJson;
50
+ /**
51
+ * @description Returns the string representation of the value
52
+ */
53
+ toString(): string;
54
+ /**
55
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
56
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
57
+ */
58
+ abstract toU8a(isBare?: BareOpts): Uint8Array;
59
+ /**
60
+ * @description Returns the base runtime type name for this instance
61
+ */
62
+ abstract toRawType(): string;
63
+ /**
64
+ * @description Return the internal value (JS-aligned, same result as $)
65
+ */
66
+ valueOf(): T;
67
+ }
@@ -0,0 +1,51 @@
1
+ // Copyright 2017-2023 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ /**
5
+ * @name Object
6
+ * @description A type extends the Base class, when it holds a value
7
+ */
8
+ export class AbstractObject {
9
+ constructor(registry, value, initialU8aLength) {
10
+ this.$ = value;
11
+ this.initialU8aLength = initialU8aLength;
12
+ this.registry = registry;
13
+ }
14
+
15
+ /**
16
+ * @description The length of the value when encoded as a Uint8Array
17
+ */
18
+ get encodedLength() {
19
+ return this.toU8a().length;
20
+ }
21
+
22
+ /**
23
+ * @description returns a hash of the contents
24
+ */
25
+ get hash() {
26
+ return this.registry.hash(this.toU8a());
27
+ }
28
+
29
+ /**
30
+ * @description Checks if the value is an empty value
31
+ */
32
+
33
+ /**
34
+ * @description Returns the string representation of the value
35
+ */
36
+ toString() {
37
+ return this.$.toString();
38
+ }
39
+
40
+ /**
41
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
42
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
43
+ */
44
+
45
+ /**
46
+ * @description Return the internal value (JS-aligned, same result as $)
47
+ */
48
+ valueOf() {
49
+ return this.$;
50
+ }
51
+ }
package/base/Int.d.ts CHANGED
@@ -11,6 +11,6 @@ import { AbstractInt } from '../abstract/Int';
11
11
  * @noInheritDoc
12
12
  */
13
13
  export declare class Int extends AbstractInt {
14
- constructor(registry: Registry, value?: AnyNumber, bitLength?: UIntBitLength);
14
+ constructor(registry: Registry, value?: AnyNumber | null, bitLength?: UIntBitLength);
15
15
  static with(bitLength: UIntBitLength, typeName?: string): CodecClass<Int>;
16
16
  }
@@ -32,6 +32,10 @@ class AbstractBase {
32
32
  get hash() {
33
33
  return this.registry.hash(this.toU8a());
34
34
  }
35
+
36
+ /**
37
+ * @description returns the inner (wrapped value)
38
+ */
35
39
  get inner() {
36
40
  return this.#raw;
37
41
  }
@@ -103,11 +107,19 @@ class AbstractBase {
103
107
  /**
104
108
  * @description Returns the base runtime type name for this instance
105
109
  */
106
- toRawType() {
107
- return 'Base';
108
- }
110
+
111
+ /**
112
+ * @description Returns the inner wrapped value (equivalent to valueOf)
113
+ */
109
114
  unwrap() {
110
115
  return this.#raw;
111
116
  }
117
+
118
+ /**
119
+ * @description Returns the inner wrapped value
120
+ */
121
+ valueOf() {
122
+ return this.#raw;
123
+ }
112
124
  }
113
125
  exports.AbstractBase = AbstractBase;
@@ -16,6 +16,9 @@ exports.DEFAULT_UINT_BITS = DEFAULT_UINT_BITS;
16
16
  const MAX_NUMBER_BITS = 52;
17
17
  const MUL_P = new _util.BN(10000);
18
18
  const FORMATTERS = [['Perquintill', _util.BN_QUINTILL], ['Perbill', _util.BN_BILLION], ['Permill', _util.BN_MILLION], ['Percent', _util.BN_HUNDRED]];
19
+ function isToBn(value) {
20
+ return (0, _util.isFunction)(value.toBn);
21
+ }
19
22
  function toPercentage(value, divisor) {
20
23
  return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`;
21
24
  }
@@ -38,9 +41,13 @@ function decodeAbstractInt(value, isNegative) {
38
41
  throw new Error('String should not contain decimal points or scientific notation');
39
42
  }
40
43
  return value;
41
- } else if ((0, _util.isBn)(value)) {
44
+ } else if ((0, _util.isBn)(value) || (0, _util.isBigInt)(value)) {
42
45
  return value.toString();
43
- } else if ((0, _util.isObject)(value) && !(0, _util.isFunction)(value.toBn)) {
46
+ } else if ((0, _util.isObject)(value)) {
47
+ if (isToBn(value)) {
48
+ return value.toBn().toString();
49
+ }
50
+
44
51
  // Allow the construction from an object with a single top-level key. This means that
45
52
  // single key objects can be treated equivalently to numbers, assuming they meet the
46
53
  // specific requirements. (This is useful in Weights 1.5 where Objects are compact)
@@ -48,13 +55,11 @@ function decodeAbstractInt(value, isNegative) {
48
55
  if (keys.length !== 1) {
49
56
  throw new Error('Unable to construct number from multi-key object');
50
57
  }
51
- const inner = value[keys[0]];
52
- if (!(0, _util.isString)(inner) && !(0, _util.isNumber)(inner)) {
53
- throw new Error('Unable to construct from object with non-string/non-number value');
54
- }
55
- return decodeAbstractInt(inner, isNegative);
58
+ return decodeAbstractInt(value[keys[0]], isNegative);
59
+ } else if (!value) {
60
+ return 0;
56
61
  }
57
- return (0, _util.bnToBn)(value).toString();
62
+ throw new Error(`Unable to create BN from unknown type ${typeof value}`);
58
63
  }
59
64
 
60
65
  /**
@@ -0,0 +1,58 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.AbstractObject = void 0;
7
+ // Copyright 2017-2023 @polkadot/types-codec authors & contributors
8
+ // SPDX-License-Identifier: Apache-2.0
9
+
10
+ /**
11
+ * @name Object
12
+ * @description A type extends the Base class, when it holds a value
13
+ */
14
+ class AbstractObject {
15
+ constructor(registry, value, initialU8aLength) {
16
+ this.$ = value;
17
+ this.initialU8aLength = initialU8aLength;
18
+ this.registry = registry;
19
+ }
20
+
21
+ /**
22
+ * @description The length of the value when encoded as a Uint8Array
23
+ */
24
+ get encodedLength() {
25
+ return this.toU8a().length;
26
+ }
27
+
28
+ /**
29
+ * @description returns a hash of the contents
30
+ */
31
+ get hash() {
32
+ return this.registry.hash(this.toU8a());
33
+ }
34
+
35
+ /**
36
+ * @description Checks if the value is an empty value
37
+ */
38
+
39
+ /**
40
+ * @description Returns the string representation of the value
41
+ */
42
+ toString() {
43
+ return this.$.toString();
44
+ }
45
+
46
+ /**
47
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
48
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
49
+ */
50
+
51
+ /**
52
+ * @description Return the internal value (JS-aligned, same result as $)
53
+ */
54
+ valueOf() {
55
+ return this.$;
56
+ }
57
+ }
58
+ exports.AbstractObject = AbstractObject;
@@ -13,6 +13,6 @@ const packageInfo = {
13
13
  name: '@polkadot/types-codec',
14
14
  path: typeof __dirname === 'string' ? __dirname : 'auto',
15
15
  type: 'cjs',
16
- version: '9.12.1'
16
+ version: '9.13.2'
17
17
  };
18
18
  exports.packageInfo = packageInfo;
package/package.json CHANGED
@@ -20,7 +20,7 @@
20
20
  "./cjs/detectPackage.js"
21
21
  ],
22
22
  "type": "module",
23
- "version": "9.12.1",
23
+ "version": "9.13.2",
24
24
  "main": "./cjs/index.js",
25
25
  "module": "./index.js",
26
26
  "types": "./index.d.ts",
@@ -47,16 +47,16 @@
47
47
  "require": "./cjs/abstract/Base.js",
48
48
  "default": "./abstract/Base.js"
49
49
  },
50
- "./abstract/BigInt": {
51
- "types": "./abstract/BigInt.d.ts",
52
- "require": "./cjs/abstract/BigInt.js",
53
- "default": "./abstract/BigInt.js"
54
- },
55
50
  "./abstract/Int": {
56
51
  "types": "./abstract/Int.d.ts",
57
52
  "require": "./cjs/abstract/Int.js",
58
53
  "default": "./abstract/Int.js"
59
54
  },
55
+ "./abstract/Object": {
56
+ "types": "./abstract/Object.d.ts",
57
+ "require": "./cjs/abstract/Object.js",
58
+ "default": "./abstract/Object.js"
59
+ },
60
60
  "./base": {
61
61
  "types": "./base/index.d.ts",
62
62
  "require": "./cjs/base/index.js",
@@ -435,7 +435,7 @@
435
435
  },
436
436
  "dependencies": {
437
437
  "@babel/runtime": "^7.20.13",
438
- "@polkadot/util": "^10.2.6",
439
- "@polkadot/x-bigint": "^10.2.6"
438
+ "@polkadot/util": "^10.3.1",
439
+ "@polkadot/x-bigint": "^10.3.1"
440
440
  }
441
441
  }
package/packageInfo.js CHANGED
@@ -7,5 +7,5 @@ export const packageInfo = {
7
7
  name: '@polkadot/types-codec',
8
8
  path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
9
9
  type: 'esm',
10
- version: '9.12.1'
10
+ version: '9.13.2'
11
11
  };
package/types/codec.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import type { HexString } from '@polkadot/util/types';
2
- import type { AnyJson } from './helpers';
2
+ import type { AnyJson, ToString } from './helpers';
3
3
  import type { IU8a } from './interfaces';
4
4
  import type { Registry } from './registry';
5
5
  export type BareOpts = boolean | Record<string, boolean>;
@@ -101,5 +101,9 @@ export interface CodecClass<T = Codec> {
101
101
  readonly __fallbackType?: string;
102
102
  new (registry: Registry, ...args: any[]): T;
103
103
  }
104
+ export interface CodecObject<T extends ToString> extends Codec {
105
+ readonly $: T;
106
+ valueOf(): T;
107
+ }
104
108
  export type CodecTo = 'toHex' | 'toJSON' | 'toPrimitive' | 'toString' | 'toU8a';
105
109
  export type ArgsDef = Record<string, CodecClass | string>;
@@ -15,3 +15,10 @@ export type AnyU8a = Uint8Array | number[] | string;
15
15
  export type UIntBitLength = 8 | 16 | 32 | 64 | 128 | 256;
16
16
  export type U8aBitLength = 8 | 16 | 32 | 64 | 128 | 160 | 256 | 264 | 512 | 520 | 1024 | 2048;
17
17
  export type AnyTupleValue = Exclude<AnyU8a, string> | HexString | (Codec | AnyU8a | AnyNumber | AnyString | undefined | null)[];
18
+ export interface ToString {
19
+ toString: () => string;
20
+ }
21
+ export interface ToBn {
22
+ toBn: () => BN;
23
+ }
24
+ export type LookupString = `Lookup${number}`;
@@ -1,7 +1,7 @@
1
1
  /// <reference types="bn.js" />
2
2
  import type { BN } from '@polkadot/util';
3
3
  import type { Codec, CodecClass } from './codec';
4
- import type { AnyTuple } from './helpers';
4
+ import type { AnyTuple, LookupString } from './helpers';
5
5
  import type { ICompact, IEnum, IMap, IMethod, INumber, IOption, IResult, ISet, IStruct, ITuple, IU8a, IVec } from './interfaces';
6
6
  export type OnlyCodec<K, T, Types> = K extends keyof Types ? Types[K] : T extends ICompact | IEnum | IMap | IMethod | INumber | IOption | IResult | ISet | IStruct | ITuple | IU8a | IVec ? T : T extends Codec ? T : never;
7
7
  export interface RegistryError {
@@ -45,8 +45,8 @@ export interface Registry {
45
45
  index: BN;
46
46
  }): RegistryError;
47
47
  findMetaEvent(eventIndex: Uint8Array): CodecClass<any>;
48
- isLookupType(value: string): boolean;
49
- createLookupType(lookupId: ICompact<INumber> | number): string;
48
+ isLookupType(value: string): value is LookupString;
49
+ createLookupType(lookupId: ICompact<INumber> | number): LookupString;
50
50
  createClassUnsafe<T extends Codec = Codec, K extends string = string>(type: K): CodecClass<T>;
51
51
  createTypeUnsafe<T extends Codec = Codec, K extends string = string>(type: K, params: unknown[], options?: CodecCreateOptions): T;
52
52
  getClassName(clazz: CodecClass): string | undefined;
@@ -1,4 +0,0 @@
1
- import { BigInt } from '@polkadot/x-bigint';
2
- export declare abstract class AbstractBigInt extends BigInt {
3
- constructor(value: string | number | bigint | boolean);
4
- }
@@ -1,14 +0,0 @@
1
- // Copyright 2017-2023 @polkadot/types-codec authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- import { BigInt } from '@polkadot/x-bigint';
5
-
6
- // @ts-expect-error not extensible, we make it extensible here
7
- export class AbstractBigInt extends BigInt {
8
- // @ts-expect-error super() not required, the self magic does that
9
- constructor(value) {
10
- const self = Object(BigInt(value));
11
- Object.setPrototypeOf(self, AbstractBigInt.prototype);
12
- return self;
13
- }
14
- }
@@ -1,20 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.AbstractBigInt = void 0;
7
- var _xBigint = require("@polkadot/x-bigint");
8
- // Copyright 2017-2023 @polkadot/types-codec authors & contributors
9
- // SPDX-License-Identifier: Apache-2.0
10
-
11
- // @ts-expect-error not extensible, we make it extensible here
12
- class AbstractBigInt extends _xBigint.BigInt {
13
- // @ts-expect-error super() not required, the self magic does that
14
- constructor(value) {
15
- const self = Object((0, _xBigint.BigInt)(value));
16
- Object.setPrototypeOf(self, AbstractBigInt.prototype);
17
- return self;
18
- }
19
- }
20
- exports.AbstractBigInt = AbstractBigInt;