@polkadot/types-codec 6.12.2-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.
Files changed (214) hide show
  1. package/abstract/AbstractArray.cjs +208 -0
  2. package/abstract/AbstractArray.d.ts +84 -0
  3. package/abstract/AbstractArray.js +197 -0
  4. package/abstract/AbstractInt.cjs +254 -0
  5. package/abstract/AbstractInt.d.ts +75 -0
  6. package/abstract/AbstractInt.js +231 -0
  7. package/abstract/index.cjs +21 -0
  8. package/abstract/index.d.ts +2 -0
  9. package/abstract/index.js +4 -0
  10. package/base/Base.cjs +109 -0
  11. package/base/Base.d.ts +54 -0
  12. package/base/Base.js +100 -0
  13. package/base/Compact.cjs +201 -0
  14. package/base/Compact.d.ts +83 -0
  15. package/base/Compact.js +186 -0
  16. package/base/DoNotConstruct.cjs +101 -0
  17. package/base/DoNotConstruct.d.ts +33 -0
  18. package/base/DoNotConstruct.js +88 -0
  19. package/base/Enum.cjs +437 -0
  20. package/base/Enum.d.ts +101 -0
  21. package/base/Enum.js +413 -0
  22. package/base/Int.cjs +45 -0
  23. package/base/Int.d.ts +16 -0
  24. package/base/Int.js +33 -0
  25. package/base/Null.cjs +111 -0
  26. package/base/Null.d.ts +54 -0
  27. package/base/Null.js +101 -0
  28. package/base/Option.cjs +256 -0
  29. package/base/Option.d.ts +88 -0
  30. package/base/Option.js +240 -0
  31. package/base/Result.cjs +109 -0
  32. package/base/Result.d.ts +46 -0
  33. package/base/Result.js +98 -0
  34. package/base/Tuple.cjs +132 -0
  35. package/base/Tuple.d.ts +39 -0
  36. package/base/Tuple.js +116 -0
  37. package/base/UInt.cjs +39 -0
  38. package/base/UInt.d.ts +15 -0
  39. package/base/UInt.js +29 -0
  40. package/base/Vec.cjs +126 -0
  41. package/base/Vec.d.ts +28 -0
  42. package/base/Vec.js +104 -0
  43. package/base/VecAny.cjs +31 -0
  44. package/base/VecAny.d.ts +15 -0
  45. package/base/VecAny.js +21 -0
  46. package/base/VecFixed.cjs +111 -0
  47. package/base/VecFixed.d.ts +26 -0
  48. package/base/VecFixed.js +93 -0
  49. package/base/index.cjs +109 -0
  50. package/base/index.d.ts +13 -0
  51. package/base/index.js +15 -0
  52. package/bundle.cjs +100 -0
  53. package/bundle.d.ts +7 -0
  54. package/bundle.js +9 -0
  55. package/detectPackage.cjs +9 -0
  56. package/detectPackage.d.ts +1 -0
  57. package/detectPackage.js +5 -0
  58. package/extended/BTreeMap.cjs +24 -0
  59. package/extended/BTreeMap.d.ts +5 -0
  60. package/extended/BTreeMap.js +14 -0
  61. package/extended/BTreeSet.cjs +227 -0
  62. package/extended/BTreeSet.d.ts +55 -0
  63. package/extended/BTreeSet.js +212 -0
  64. package/extended/BitVec.cjs +97 -0
  65. package/extended/BitVec.d.ts +26 -0
  66. package/extended/BitVec.js +82 -0
  67. package/extended/Bytes.cjs +86 -0
  68. package/extended/Bytes.d.ts +25 -0
  69. package/extended/Bytes.js +75 -0
  70. package/extended/HashMap.cjs +24 -0
  71. package/extended/HashMap.d.ts +5 -0
  72. package/extended/HashMap.js +14 -0
  73. package/extended/Linkage.cjs +89 -0
  74. package/extended/Linkage.d.ts +31 -0
  75. package/extended/Linkage.js +71 -0
  76. package/extended/Map.cjs +244 -0
  77. package/extended/Map.d.ts +50 -0
  78. package/extended/Map.js +225 -0
  79. package/extended/Range.cjs +92 -0
  80. package/extended/Range.d.ts +30 -0
  81. package/extended/Range.js +75 -0
  82. package/extended/Type.cjs +39 -0
  83. package/extended/Type.d.ts +16 -0
  84. package/extended/Type.js +27 -0
  85. package/extended/U8aFixed.cjs +66 -0
  86. package/extended/U8aFixed.d.ts +16 -0
  87. package/extended/U8aFixed.js +53 -0
  88. package/extended/WrapperOpaque.cjs +68 -0
  89. package/extended/WrapperOpaque.d.ts +18 -0
  90. package/extended/WrapperOpaque.js +57 -0
  91. package/extended/index.cjs +105 -0
  92. package/extended/index.d.ts +11 -0
  93. package/extended/index.js +13 -0
  94. package/index.cjs +20 -0
  95. package/index.d.ts +2 -0
  96. package/index.js +4 -0
  97. package/native/Bool.cjs +141 -0
  98. package/native/Bool.d.ts +62 -0
  99. package/native/Bool.js +130 -0
  100. package/native/Date.cjs +170 -0
  101. package/native/Date.d.ts +76 -0
  102. package/native/Date.js +158 -0
  103. package/native/Json.cjs +133 -0
  104. package/native/Json.d.ts +55 -0
  105. package/native/Json.js +117 -0
  106. package/native/Raw.cjs +192 -0
  107. package/native/Raw.d.ts +90 -0
  108. package/native/Raw.js +182 -0
  109. package/native/Set.cjs +268 -0
  110. package/native/Set.d.ts +76 -0
  111. package/native/Set.js +251 -0
  112. package/native/Struct.cjs +289 -0
  113. package/native/Struct.d.ts +92 -0
  114. package/native/Struct.js +273 -0
  115. package/native/Text.cjs +193 -0
  116. package/native/Text.d.ts +71 -0
  117. package/native/Text.js +178 -0
  118. package/native/index.cjs +79 -0
  119. package/native/index.d.ts +7 -0
  120. package/native/index.js +9 -0
  121. package/package.json +387 -0
  122. package/packageInfo.cjs +14 -0
  123. package/packageInfo.d.ts +4 -0
  124. package/packageInfo.js +7 -0
  125. package/primitive/I128.cjs +26 -0
  126. package/primitive/I128.d.ts +11 -0
  127. package/primitive/I128.js +16 -0
  128. package/primitive/I16.cjs +26 -0
  129. package/primitive/I16.d.ts +11 -0
  130. package/primitive/I16.js +16 -0
  131. package/primitive/I256.cjs +26 -0
  132. package/primitive/I256.d.ts +11 -0
  133. package/primitive/I256.js +16 -0
  134. package/primitive/I32.cjs +26 -0
  135. package/primitive/I32.d.ts +11 -0
  136. package/primitive/I32.js +16 -0
  137. package/primitive/I64.cjs +26 -0
  138. package/primitive/I64.d.ts +11 -0
  139. package/primitive/I64.js +16 -0
  140. package/primitive/I8.cjs +26 -0
  141. package/primitive/I8.d.ts +11 -0
  142. package/primitive/I8.js +16 -0
  143. package/primitive/U128.cjs +26 -0
  144. package/primitive/U128.d.ts +11 -0
  145. package/primitive/U128.js +16 -0
  146. package/primitive/U16.cjs +26 -0
  147. package/primitive/U16.d.ts +11 -0
  148. package/primitive/U16.js +16 -0
  149. package/primitive/U256.cjs +26 -0
  150. package/primitive/U256.d.ts +11 -0
  151. package/primitive/U256.js +16 -0
  152. package/primitive/U32.cjs +26 -0
  153. package/primitive/U32.d.ts +11 -0
  154. package/primitive/U32.js +16 -0
  155. package/primitive/U64.cjs +26 -0
  156. package/primitive/U64.d.ts +11 -0
  157. package/primitive/U64.js +16 -0
  158. package/primitive/U8.cjs +26 -0
  159. package/primitive/U8.d.ts +11 -0
  160. package/primitive/U8.js +16 -0
  161. package/primitive/USize.cjs +28 -0
  162. package/primitive/USize.d.ts +12 -0
  163. package/primitive/USize.js +18 -0
  164. package/primitive/index.cjs +187 -0
  165. package/primitive/index.d.ts +13 -0
  166. package/primitive/index.js +15 -0
  167. package/types/codec.cjs +1 -0
  168. package/types/codec.d.ts +77 -0
  169. package/types/codec.js +1 -0
  170. package/types/helpers.cjs +1 -0
  171. package/types/helpers.d.ts +14 -0
  172. package/types/helpers.js +1 -0
  173. package/types/index.cjs +57 -0
  174. package/types/index.d.ts +4 -0
  175. package/types/index.js +6 -0
  176. package/types/interfaces.cjs +1 -0
  177. package/types/interfaces.d.ts +68 -0
  178. package/types/interfaces.js +1 -0
  179. package/types/registry.cjs +1 -0
  180. package/types/registry.d.ts +44 -0
  181. package/types/registry.js +1 -0
  182. package/utils/compareArray.cjs +22 -0
  183. package/utils/compareArray.d.ts +1 -0
  184. package/utils/compareArray.js +13 -0
  185. package/utils/compareMap.cjs +39 -0
  186. package/utils/compareMap.d.ts +1 -0
  187. package/utils/compareMap.js +31 -0
  188. package/utils/compareSet.cjs +29 -0
  189. package/utils/compareSet.d.ts +1 -0
  190. package/utils/compareSet.js +22 -0
  191. package/utils/decodeU8a.cjs +69 -0
  192. package/utils/decodeU8a.d.ts +13 -0
  193. package/utils/decodeU8a.js +60 -0
  194. package/utils/index.cjs +95 -0
  195. package/utils/index.d.ts +9 -0
  196. package/utils/index.js +11 -0
  197. package/utils/mapToTypeMap.cjs +25 -0
  198. package/utils/mapToTypeMap.d.ts +5 -0
  199. package/utils/mapToTypeMap.js +17 -0
  200. package/utils/sanitize.cjs +242 -0
  201. package/utils/sanitize.d.ts +16 -0
  202. package/utils/sanitize.js +215 -0
  203. package/utils/sortValues.cjs +89 -0
  204. package/utils/sortValues.d.ts +13 -0
  205. package/utils/sortValues.js +73 -0
  206. package/utils/typeToConstructor.cjs +14 -0
  207. package/utils/typeToConstructor.d.ts +2 -0
  208. package/utils/typeToConstructor.js +6 -0
  209. package/utils/typesToMap.cjs +20 -0
  210. package/utils/typesToMap.d.ts +2 -0
  211. package/utils/typesToMap.js +13 -0
  212. package/utils/util.cjs +15 -0
  213. package/utils/util.d.ts +3 -0
  214. package/utils/util.js +7 -0
@@ -0,0 +1,101 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.DoNotConstruct = void 0;
9
+
10
+ var _classPrivateFieldLooseBase2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldLooseBase"));
11
+
12
+ var _classPrivateFieldLooseKey2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldLooseKey"));
13
+
14
+ var _neverError = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("neverError");
15
+
16
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
17
+ // SPDX-License-Identifier: Apache-2.0
18
+
19
+ /**
20
+ * @name DoNotConstruct
21
+ * @description
22
+ * An unknown type that fails on construction with the type info
23
+ */
24
+ class DoNotConstruct {
25
+ constructor(registry) {
26
+ let typeName = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 'DoNotConstruct';
27
+ this.registry = void 0;
28
+ this.createdAtHash = void 0;
29
+ Object.defineProperty(this, _neverError, {
30
+ writable: true,
31
+ value: void 0
32
+ });
33
+ this.registry = registry;
34
+ (0, _classPrivateFieldLooseBase2.default)(this, _neverError)[_neverError] = new Error(`DoNotConstruct: Cannot construct unknown type ${typeName}`);
35
+ throw (0, _classPrivateFieldLooseBase2.default)(this, _neverError)[_neverError];
36
+ }
37
+
38
+ static with(typeName) {
39
+ return class extends DoNotConstruct {
40
+ constructor(registry) {
41
+ super(registry, typeName);
42
+ }
43
+
44
+ };
45
+ }
46
+ /**
47
+ * @description The length of the value when encoded as a Uint8Array
48
+ */
49
+
50
+
51
+ get encodedLength() {
52
+ throw (0, _classPrivateFieldLooseBase2.default)(this, _neverError)[_neverError];
53
+ }
54
+ /**
55
+ * @description returns a hash of the contents
56
+ */
57
+
58
+
59
+ get hash() {
60
+ throw (0, _classPrivateFieldLooseBase2.default)(this, _neverError)[_neverError];
61
+ }
62
+ /**
63
+ * @description Checks if the value is an empty value (always true)
64
+ */
65
+
66
+
67
+ get isEmpty() {
68
+ throw (0, _classPrivateFieldLooseBase2.default)(this, _neverError)[_neverError];
69
+ }
70
+
71
+ eq() {
72
+ throw (0, _classPrivateFieldLooseBase2.default)(this, _neverError)[_neverError];
73
+ }
74
+
75
+ toHex() {
76
+ throw (0, _classPrivateFieldLooseBase2.default)(this, _neverError)[_neverError];
77
+ }
78
+
79
+ toHuman() {
80
+ throw (0, _classPrivateFieldLooseBase2.default)(this, _neverError)[_neverError];
81
+ }
82
+
83
+ toJSON() {
84
+ throw (0, _classPrivateFieldLooseBase2.default)(this, _neverError)[_neverError];
85
+ }
86
+
87
+ toRawType() {
88
+ throw (0, _classPrivateFieldLooseBase2.default)(this, _neverError)[_neverError];
89
+ }
90
+
91
+ toString() {
92
+ throw (0, _classPrivateFieldLooseBase2.default)(this, _neverError)[_neverError];
93
+ }
94
+
95
+ toU8a() {
96
+ throw (0, _classPrivateFieldLooseBase2.default)(this, _neverError)[_neverError];
97
+ }
98
+
99
+ }
100
+
101
+ exports.DoNotConstruct = DoNotConstruct;
@@ -0,0 +1,33 @@
1
+ import type { HexString } from '@polkadot/util/types';
2
+ import type { AnyJson, Codec, CodecClass, CodecRegistry, IU8a } from '../types';
3
+ /**
4
+ * @name DoNotConstruct
5
+ * @description
6
+ * An unknown type that fails on construction with the type info
7
+ */
8
+ export declare class DoNotConstruct implements Codec {
9
+ #private;
10
+ readonly registry: CodecRegistry;
11
+ createdAtHash?: IU8a;
12
+ constructor(registry: CodecRegistry, typeName?: string);
13
+ static with(typeName?: string): CodecClass;
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 (always true)
24
+ */
25
+ get isEmpty(): boolean;
26
+ eq(): boolean;
27
+ toHex(): HexString;
28
+ toHuman(): AnyJson;
29
+ toJSON(): AnyJson;
30
+ toRawType(): string;
31
+ toString(): string;
32
+ toU8a(): Uint8Array;
33
+ }
@@ -0,0 +1,88 @@
1
+ import _classPrivateFieldLooseBase from "@babel/runtime/helpers/esm/classPrivateFieldLooseBase";
2
+ import _classPrivateFieldLooseKey from "@babel/runtime/helpers/esm/classPrivateFieldLooseKey";
3
+
4
+ var _neverError = /*#__PURE__*/_classPrivateFieldLooseKey("neverError");
5
+
6
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
7
+ // SPDX-License-Identifier: Apache-2.0
8
+
9
+ /**
10
+ * @name DoNotConstruct
11
+ * @description
12
+ * An unknown type that fails on construction with the type info
13
+ */
14
+ export class DoNotConstruct {
15
+ constructor(registry, typeName = 'DoNotConstruct') {
16
+ this.registry = void 0;
17
+ this.createdAtHash = void 0;
18
+ Object.defineProperty(this, _neverError, {
19
+ writable: true,
20
+ value: void 0
21
+ });
22
+ this.registry = registry;
23
+ _classPrivateFieldLooseBase(this, _neverError)[_neverError] = new Error(`DoNotConstruct: Cannot construct unknown type ${typeName}`);
24
+ throw _classPrivateFieldLooseBase(this, _neverError)[_neverError];
25
+ }
26
+
27
+ static with(typeName) {
28
+ return class extends DoNotConstruct {
29
+ constructor(registry) {
30
+ super(registry, typeName);
31
+ }
32
+
33
+ };
34
+ }
35
+ /**
36
+ * @description The length of the value when encoded as a Uint8Array
37
+ */
38
+
39
+
40
+ get encodedLength() {
41
+ throw _classPrivateFieldLooseBase(this, _neverError)[_neverError];
42
+ }
43
+ /**
44
+ * @description returns a hash of the contents
45
+ */
46
+
47
+
48
+ get hash() {
49
+ throw _classPrivateFieldLooseBase(this, _neverError)[_neverError];
50
+ }
51
+ /**
52
+ * @description Checks if the value is an empty value (always true)
53
+ */
54
+
55
+
56
+ get isEmpty() {
57
+ throw _classPrivateFieldLooseBase(this, _neverError)[_neverError];
58
+ }
59
+
60
+ eq() {
61
+ throw _classPrivateFieldLooseBase(this, _neverError)[_neverError];
62
+ }
63
+
64
+ toHex() {
65
+ throw _classPrivateFieldLooseBase(this, _neverError)[_neverError];
66
+ }
67
+
68
+ toHuman() {
69
+ throw _classPrivateFieldLooseBase(this, _neverError)[_neverError];
70
+ }
71
+
72
+ toJSON() {
73
+ throw _classPrivateFieldLooseBase(this, _neverError)[_neverError];
74
+ }
75
+
76
+ toRawType() {
77
+ throw _classPrivateFieldLooseBase(this, _neverError)[_neverError];
78
+ }
79
+
80
+ toString() {
81
+ throw _classPrivateFieldLooseBase(this, _neverError)[_neverError];
82
+ }
83
+
84
+ toU8a() {
85
+ throw _classPrivateFieldLooseBase(this, _neverError)[_neverError];
86
+ }
87
+
88
+ }
package/base/Enum.cjs ADDED
@@ -0,0 +1,437 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.Enum = void 0;
9
+
10
+ var _classPrivateFieldLooseBase2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldLooseBase"));
11
+
12
+ var _classPrivateFieldLooseKey2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldLooseKey"));
13
+
14
+ var _util = require("@polkadot/util");
15
+
16
+ var _index = require("../utils/index.cjs");
17
+
18
+ var _Null = require("./Null.cjs");
19
+
20
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
21
+ // SPDX-License-Identifier: Apache-2.0
22
+ function isRustEnum(def) {
23
+ const defValues = Object.values(def);
24
+
25
+ if (defValues.some(v => (0, _util.isNumber)(v))) {
26
+ (0, _util.assert)(defValues.every(v => (0, _util.isNumber)(v) && v >= 0 && v <= 255), 'Invalid number-indexed enum definition');
27
+ return false;
28
+ }
29
+
30
+ return true;
31
+ }
32
+
33
+ function extractDef(registry, _def) {
34
+ const def = {};
35
+ let isBasic;
36
+ let isIndexed;
37
+
38
+ if (Array.isArray(_def)) {
39
+ for (let i = 0; i < _def.length; i++) {
40
+ def[_def[i]] = {
41
+ Type: _Null.Null,
42
+ index: i
43
+ };
44
+ }
45
+
46
+ isBasic = true;
47
+ isIndexed = false;
48
+ } else if (isRustEnum(_def)) {
49
+ const entries = Object.entries((0, _index.mapToTypeMap)(registry, _def));
50
+
51
+ for (let i = 0; i < entries.length; i++) {
52
+ const [key, Type] = entries[i];
53
+ def[key] = {
54
+ Type,
55
+ index: i
56
+ };
57
+ }
58
+
59
+ isBasic = !Object.values(def).some(_ref => {
60
+ let {
61
+ Type
62
+ } = _ref;
63
+ return Type !== _Null.Null;
64
+ });
65
+ isIndexed = false;
66
+ } else {
67
+ const entries = Object.entries(_def);
68
+
69
+ for (let i = 0; i < entries.length; i++) {
70
+ const [key, index] = entries[i];
71
+ def[key] = {
72
+ Type: _Null.Null,
73
+ index
74
+ };
75
+ }
76
+
77
+ isBasic = true;
78
+ isIndexed = true;
79
+ }
80
+
81
+ return {
82
+ def,
83
+ isBasic,
84
+ isIndexed
85
+ };
86
+ }
87
+
88
+ function createFromValue(registry, def) {
89
+ let index = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
90
+ let value = arguments.length > 3 ? arguments[3] : undefined;
91
+ const entry = Object.values(def).find(e => e.index === index);
92
+ (0, _util.assert)(!(0, _util.isUndefined)(entry), () => `Unable to create Enum via index ${index}, in ${Object.keys(def).join(', ')}`);
93
+ return {
94
+ index,
95
+ value: value instanceof entry.Type ? value : new entry.Type(registry, value)
96
+ };
97
+ }
98
+
99
+ function decodeFromJSON(registry, def, key, value) {
100
+ // JSON comes in the form of { "<type (camelCase)>": "<value for type>" }, here we
101
+ // additionally force to lower to ensure forward compat
102
+ const keys = Object.keys(def).map(k => k.toLowerCase());
103
+ const keyLower = key.toLowerCase();
104
+ const index = keys.indexOf(keyLower);
105
+ (0, _util.assert)(index !== -1, () => `Cannot map Enum JSON, unable to find '${key}' in ${keys.join(', ')}`);
106
+
107
+ try {
108
+ return createFromValue(registry, def, Object.values(def)[index].index, value);
109
+ } catch (error) {
110
+ throw new Error(`Enum(${key}):: ${error.message}`);
111
+ }
112
+ }
113
+
114
+ function decodeEnum(registry, def, value, index) {
115
+ // NOTE We check the index path first, before looking at values - this allows treating
116
+ // the optional indexes before anything else, more-specific > less-specific
117
+ if ((0, _util.isNumber)(index)) {
118
+ return createFromValue(registry, def, index, value);
119
+ } else if ((0, _util.isU8a)(value) || (0, _util.isHex)(value)) {
120
+ const u8a = (0, _util.u8aToU8a)(value); // nested, we don't want to match isObject below
121
+
122
+ if (u8a.length) {
123
+ return createFromValue(registry, def, u8a[0], u8a.subarray(1));
124
+ }
125
+ } else if (value instanceof Enum) {
126
+ return createFromValue(registry, def, value.index, value.value);
127
+ } else if ((0, _util.isNumber)(value)) {
128
+ return createFromValue(registry, def, value);
129
+ } else if ((0, _util.isString)(value)) {
130
+ return decodeFromJSON(registry, def, value.toString());
131
+ } else if ((0, _util.isObject)(value)) {
132
+ const key = Object.keys(value)[0];
133
+ return decodeFromJSON(registry, def, key, value[key]);
134
+ } // Worst-case scenario, return the first with default
135
+
136
+
137
+ return createFromValue(registry, def, Object.values(def)[0].index);
138
+ }
139
+ /**
140
+ * @name Enum
141
+ * @description
142
+ * This implements an enum, that based on the value wraps a different type. It is effectively
143
+ * an extension to enum where the value type is determined by the actual index.
144
+ */
145
+
146
+
147
+ var _def2 = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("def");
148
+
149
+ var _entryIndex = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("entryIndex");
150
+
151
+ var _indexes = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("indexes");
152
+
153
+ var _isBasic = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("isBasic");
154
+
155
+ var _isIndexed = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("isIndexed");
156
+
157
+ var _raw = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("raw");
158
+
159
+ class Enum {
160
+ constructor(registry, Types, value, index) {
161
+ this.registry = void 0;
162
+ this.createdAtHash = void 0;
163
+ Object.defineProperty(this, _def2, {
164
+ writable: true,
165
+ value: void 0
166
+ });
167
+ Object.defineProperty(this, _entryIndex, {
168
+ writable: true,
169
+ value: void 0
170
+ });
171
+ this.initialU8aLength = void 0;
172
+ Object.defineProperty(this, _indexes, {
173
+ writable: true,
174
+ value: void 0
175
+ });
176
+ Object.defineProperty(this, _isBasic, {
177
+ writable: true,
178
+ value: void 0
179
+ });
180
+ Object.defineProperty(this, _isIndexed, {
181
+ writable: true,
182
+ value: void 0
183
+ });
184
+ Object.defineProperty(this, _raw, {
185
+ writable: true,
186
+ value: void 0
187
+ });
188
+ const {
189
+ def,
190
+ isBasic,
191
+ isIndexed
192
+ } = extractDef(registry, Types); // shortcut isU8a as used in SCALE decoding
193
+
194
+ 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);
195
+ this.registry = registry;
196
+ (0, _classPrivateFieldLooseBase2.default)(this, _def2)[_def2] = def;
197
+ (0, _classPrivateFieldLooseBase2.default)(this, _isBasic)[_isBasic] = isBasic;
198
+ (0, _classPrivateFieldLooseBase2.default)(this, _isIndexed)[_isIndexed] = isIndexed;
199
+ (0, _classPrivateFieldLooseBase2.default)(this, _indexes)[_indexes] = Object.values(def).map(_ref2 => {
200
+ let {
201
+ index
202
+ } = _ref2;
203
+ return index;
204
+ });
205
+ (0, _classPrivateFieldLooseBase2.default)(this, _entryIndex)[_entryIndex] = (0, _classPrivateFieldLooseBase2.default)(this, _indexes)[_indexes].indexOf(decoded.index) || 0;
206
+ (0, _classPrivateFieldLooseBase2.default)(this, _raw)[_raw] = decoded.value;
207
+
208
+ if ((0, _classPrivateFieldLooseBase2.default)(this, _raw)[_raw].initialU8aLength) {
209
+ this.initialU8aLength = 1 + (0, _classPrivateFieldLooseBase2.default)(this, _raw)[_raw].initialU8aLength;
210
+ }
211
+ }
212
+
213
+ static with(Types) {
214
+ const keys = Array.isArray(Types) ? Types : Object.keys(Types);
215
+ const asKeys = new Array(keys.length);
216
+ const isKeys = new Array(keys.length);
217
+
218
+ for (let i = 0; i < keys.length; i++) {
219
+ const name = (0, _util.stringPascalCase)(keys[i].replace(' ', '_'));
220
+ asKeys[i] = `as${name}`;
221
+ isKeys[i] = `is${name}`;
222
+ }
223
+
224
+ return class extends Enum {
225
+ constructor(registry, value, index) {
226
+ super(registry, Types, value, index);
227
+ (0, _util.objectProperties)(this, isKeys, (_, i) => this.type === keys[i]);
228
+ (0, _util.objectProperties)(this, asKeys, (k, i) => {
229
+ (0, _util.assert)(this[isKeys[i]], () => `Cannot convert '${this.type}' via ${k}`);
230
+ return this.value;
231
+ });
232
+ }
233
+
234
+ };
235
+ }
236
+ /**
237
+ * @description The length of the value when encoded as a Uint8Array
238
+ */
239
+
240
+
241
+ get encodedLength() {
242
+ return 1 + (0, _classPrivateFieldLooseBase2.default)(this, _raw)[_raw].encodedLength;
243
+ }
244
+ /**
245
+ * @description returns a hash of the contents
246
+ */
247
+
248
+
249
+ get hash() {
250
+ return this.registry.hash(this.toU8a());
251
+ }
252
+ /**
253
+ * @description The index of the enum value
254
+ */
255
+
256
+
257
+ get index() {
258
+ return (0, _classPrivateFieldLooseBase2.default)(this, _indexes)[_indexes][(0, _classPrivateFieldLooseBase2.default)(this, _entryIndex)[_entryIndex]];
259
+ }
260
+ /**
261
+ * @description true if this is a basic enum (no values)
262
+ */
263
+
264
+
265
+ get isBasic() {
266
+ return (0, _classPrivateFieldLooseBase2.default)(this, _isBasic)[_isBasic];
267
+ }
268
+ /**
269
+ * @description Checks if the value is an empty value
270
+ */
271
+
272
+
273
+ get isEmpty() {
274
+ return (0, _classPrivateFieldLooseBase2.default)(this, _raw)[_raw].isEmpty;
275
+ }
276
+ /**
277
+ * @description Checks if the Enum points to a [[Null]] type
278
+ */
279
+
280
+
281
+ get isNone() {
282
+ return (0, _classPrivateFieldLooseBase2.default)(this, _raw)[_raw] instanceof _Null.Null;
283
+ }
284
+ /**
285
+ * @description Checks if the Enum points to a [[Null]] type
286
+ * @deprecated use isNone
287
+ */
288
+
289
+
290
+ get isNull() {
291
+ return (0, _classPrivateFieldLooseBase2.default)(this, _raw)[_raw] instanceof _Null.Null;
292
+ }
293
+ /**
294
+ * @description The available keys for this enum
295
+ */
296
+
297
+
298
+ get defIndexes() {
299
+ return (0, _classPrivateFieldLooseBase2.default)(this, _indexes)[_indexes];
300
+ }
301
+ /**
302
+ * @description The available keys for this enum
303
+ */
304
+
305
+
306
+ get defKeys() {
307
+ return Object.keys((0, _classPrivateFieldLooseBase2.default)(this, _def2)[_def2]);
308
+ }
309
+ /**
310
+ * @description The name of the type this enum value represents
311
+ */
312
+
313
+
314
+ get type() {
315
+ return this.defKeys[(0, _classPrivateFieldLooseBase2.default)(this, _entryIndex)[_entryIndex]];
316
+ }
317
+ /**
318
+ * @description The value of the enum
319
+ */
320
+
321
+
322
+ get value() {
323
+ return (0, _classPrivateFieldLooseBase2.default)(this, _raw)[_raw];
324
+ }
325
+ /**
326
+ * @description Compares the value of the input to see if there is a match
327
+ */
328
+
329
+
330
+ eq(other) {
331
+ // cater for the case where we only pass the enum index
332
+ if ((0, _util.isU8a)(other)) {
333
+ return !this.toU8a().some((entry, index) => entry !== other[index]);
334
+ } else if ((0, _util.isNumber)(other)) {
335
+ return this.toNumber() === other;
336
+ } else if ((0, _classPrivateFieldLooseBase2.default)(this, _isBasic)[_isBasic] && (0, _util.isString)(other)) {
337
+ return this.type === other;
338
+ } else if ((0, _util.isHex)(other)) {
339
+ return this.toHex() === other;
340
+ } else if (other instanceof Enum) {
341
+ return this.index === other.index && this.value.eq(other.value);
342
+ } else if ((0, _util.isObject)(other)) {
343
+ return this.value.eq(other[this.type]);
344
+ } // compare the actual wrapper value
345
+
346
+
347
+ return this.value.eq(other);
348
+ }
349
+ /**
350
+ * @description Returns a hex string representation of the value
351
+ */
352
+
353
+
354
+ toHex() {
355
+ return (0, _util.u8aToHex)(this.toU8a());
356
+ }
357
+ /**
358
+ * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
359
+ */
360
+
361
+
362
+ toHuman(isExtended) {
363
+ return (0, _classPrivateFieldLooseBase2.default)(this, _isBasic)[_isBasic] || this.isNone ? this.type : {
364
+ [this.type]: (0, _classPrivateFieldLooseBase2.default)(this, _raw)[_raw].toHuman(isExtended)
365
+ };
366
+ }
367
+ /**
368
+ * @description Converts the Object to JSON, typically used for RPC transfers
369
+ */
370
+
371
+
372
+ toJSON() {
373
+ return (0, _classPrivateFieldLooseBase2.default)(this, _isBasic)[_isBasic] ? this.type : {
374
+ [(0, _util.stringCamelCase)(this.type)]: (0, _classPrivateFieldLooseBase2.default)(this, _raw)[_raw].toJSON()
375
+ };
376
+ }
377
+ /**
378
+ * @description Returns the number representation for the value
379
+ */
380
+
381
+
382
+ toNumber() {
383
+ return this.index;
384
+ }
385
+ /**
386
+ * @description Returns a raw struct representation of the enum types
387
+ */
388
+
389
+
390
+ _toRawStruct() {
391
+ if ((0, _classPrivateFieldLooseBase2.default)(this, _isBasic)[_isBasic]) {
392
+ return (0, _classPrivateFieldLooseBase2.default)(this, _isIndexed)[_isIndexed] ? this.defKeys.reduce((out, key, index) => {
393
+ out[key] = (0, _classPrivateFieldLooseBase2.default)(this, _indexes)[_indexes][index];
394
+ return out;
395
+ }, {}) : this.defKeys;
396
+ }
397
+
398
+ const typeMap = Object.entries((0, _classPrivateFieldLooseBase2.default)(this, _def2)[_def2]).reduce((out, _ref3) => {
399
+ let [key, {
400
+ Type
401
+ }] = _ref3;
402
+ out[key] = Type;
403
+ return out;
404
+ }, {});
405
+ return (0, _index.typesToMap)(this.registry, typeMap);
406
+ }
407
+ /**
408
+ * @description Returns the base runtime type name for this instance
409
+ */
410
+
411
+
412
+ toRawType() {
413
+ return (0, _util.stringify)({
414
+ _enum: this._toRawStruct()
415
+ });
416
+ }
417
+ /**
418
+ * @description Returns the string representation of the value
419
+ */
420
+
421
+
422
+ toString() {
423
+ return this.isNull ? this.type : (0, _util.stringify)(this.toJSON());
424
+ }
425
+ /**
426
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
427
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
428
+ */
429
+
430
+
431
+ toU8a(isBare) {
432
+ return (0, _util.u8aConcat)(new Uint8Array(isBare ? [] : [this.index]), (0, _classPrivateFieldLooseBase2.default)(this, _raw)[_raw].toU8a(isBare));
433
+ }
434
+
435
+ }
436
+
437
+ exports.Enum = Enum;