@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,289 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.Struct = 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
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
19
+ // SPDX-License-Identifier: Apache-2.0
20
+
21
+ /** @internal */
22
+ function decodeStructFromObject(registry, Types, value, jsonMap) {
23
+ let jsonObj;
24
+ const inputKeys = Object.keys(Types);
25
+ const typeofArray = Array.isArray(value);
26
+ const typeofMap = value instanceof Map;
27
+ (0, _util.assert)(typeofArray || typeofMap || (0, _util.isObject)(value), () => `Struct: Cannot decode value ${(0, _util.stringify)(value)} (typeof ${typeof value}), expected an input object, map or array`);
28
+ (0, _util.assert)(!typeofArray || value.length === inputKeys.length, () => `Struct: Unable to map ${(0, _util.stringify)(value)} array to object with known keys ${inputKeys.join(', ')}`);
29
+ const raw = new Array(inputKeys.length);
30
+
31
+ for (let i = 0; i < inputKeys.length; i++) {
32
+ const key = inputKeys[i];
33
+ const jsonKey = jsonMap.get(key) || key;
34
+ const Type = Types[key];
35
+ let assign;
36
+
37
+ try {
38
+ if (typeofArray) {
39
+ assign = value[i];
40
+ } else if (typeofMap) {
41
+ assign = jsonKey && value.get(jsonKey);
42
+ } else {
43
+ assign = jsonKey && value[jsonKey];
44
+
45
+ if ((0, _util.isUndefined)(assign)) {
46
+ if ((0, _util.isUndefined)(jsonObj)) {
47
+ const entries = Object.entries(value);
48
+ jsonObj = {};
49
+
50
+ for (let e = 0; e < entries.length; e++) {
51
+ jsonObj[(0, _util.stringCamelCase)(entries[e][0])] = entries[e][1];
52
+ }
53
+ }
54
+
55
+ assign = jsonKey && jsonObj[jsonKey];
56
+ }
57
+ }
58
+
59
+ raw[i] = [key, assign instanceof Type ? assign : new Type(registry, assign)];
60
+ } catch (error) {
61
+ let type = Type.name;
62
+
63
+ try {
64
+ type = new Type(registry).toRawType();
65
+ } catch (error) {// ignore
66
+ }
67
+
68
+ throw new Error(`Struct: failed on ${jsonKey}: ${type}:: ${error.message}`);
69
+ }
70
+ }
71
+
72
+ return [raw, 0];
73
+ }
74
+ /**
75
+ * @name Struct
76
+ * @description
77
+ * A Struct defines an Object with key-value pairs - where the values are Codec values. It removes
78
+ * a lot of repetition from the actual coding, define a structure type, pass it the key/Codec
79
+ * values in the constructor and it manages the decoding. It is important that the constructor
80
+ * values matches 100% to the order in th Rust code, i.e. don't go crazy and make it alphabetical,
81
+ * it needs to decoded in the specific defined order.
82
+ * @noInheritDoc
83
+ */
84
+
85
+
86
+ var _jsonMap = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("jsonMap");
87
+
88
+ var _Types = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("Types");
89
+
90
+ class Struct extends Map {
91
+ constructor(registry, Types, value) {
92
+ let jsonMap = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : new Map();
93
+ const typeMap = (0, _index.mapToTypeMap)(registry, Types);
94
+ const [decoded, decodedLength] = (0, _util.isU8a)(value) ? (0, _index.decodeU8a)(registry, value, typeMap, true) : (0, _util.isHex)(value) ? (0, _index.decodeU8a)(registry, (0, _util.u8aToU8a)(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
95
+ super(decoded);
96
+ this.registry = void 0;
97
+ this.createdAtHash = void 0;
98
+ this.initialU8aLength = void 0;
99
+ Object.defineProperty(this, _jsonMap, {
100
+ writable: true,
101
+ value: void 0
102
+ });
103
+ Object.defineProperty(this, _Types, {
104
+ writable: true,
105
+ value: void 0
106
+ });
107
+ this.registry = registry;
108
+ this.initialU8aLength = decodedLength;
109
+ (0, _classPrivateFieldLooseBase2.default)(this, _jsonMap)[_jsonMap] = jsonMap;
110
+ (0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types] = typeMap;
111
+ }
112
+
113
+ static with(Types, jsonMap) {
114
+ const keys = Object.keys(Types);
115
+ return class extends Struct {
116
+ constructor(registry, value) {
117
+ super(registry, Types, value, jsonMap);
118
+ (0, _util.objectProperties)(this, keys, k => this.get(k));
119
+ }
120
+
121
+ };
122
+ }
123
+ /**
124
+ * @description The available keys for this struct
125
+ */
126
+
127
+
128
+ get defKeys() {
129
+ return Object.keys((0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types]);
130
+ }
131
+ /**
132
+ * @description Checks if the value is an empty value
133
+ */
134
+
135
+
136
+ get isEmpty() {
137
+ for (const v of this.values()) {
138
+ if (!v.isEmpty) {
139
+ return false;
140
+ }
141
+ }
142
+
143
+ return true;
144
+ }
145
+ /**
146
+ * @description Returns the Type description of the structure
147
+ */
148
+
149
+
150
+ get Type() {
151
+ const result = {};
152
+ const defs = Object.entries((0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types]);
153
+
154
+ for (let i = 0; i < defs.length; i++) {
155
+ const [key, Type] = defs[i];
156
+ result[key] = new Type(this.registry).toRawType();
157
+ }
158
+
159
+ return result;
160
+ }
161
+ /**
162
+ * @description The length of the value when encoded as a Uint8Array
163
+ */
164
+
165
+
166
+ get encodedLength() {
167
+ let total = 0;
168
+
169
+ for (const v of this.values()) {
170
+ total += v.encodedLength;
171
+ }
172
+
173
+ return total;
174
+ }
175
+ /**
176
+ * @description returns a hash of the contents
177
+ */
178
+
179
+
180
+ get hash() {
181
+ return this.registry.hash(this.toU8a());
182
+ }
183
+ /**
184
+ * @description Compares the value of the input to see if there is a match
185
+ */
186
+
187
+
188
+ eq(other) {
189
+ return (0, _index.compareMap)(this, other);
190
+ }
191
+ /**
192
+ * @description Returns a specific names entry in the structure
193
+ * @param name The name of the entry to retrieve
194
+ */
195
+
196
+
197
+ get(name) {
198
+ return super.get(name);
199
+ }
200
+ /**
201
+ * @description Returns the values of a member at a specific index (Rather use get(name) for performance)
202
+ */
203
+
204
+
205
+ getAtIndex(index) {
206
+ return this.toArray()[index];
207
+ }
208
+ /**
209
+ * @description Converts the Object to an standard JavaScript Array
210
+ */
211
+
212
+
213
+ toArray() {
214
+ return [...this.values()];
215
+ }
216
+ /**
217
+ * @description Returns a hex string representation of the value
218
+ */
219
+
220
+
221
+ toHex() {
222
+ return (0, _util.u8aToHex)(this.toU8a());
223
+ }
224
+ /**
225
+ * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
226
+ */
227
+
228
+
229
+ toHuman(isExtended) {
230
+ const json = {};
231
+
232
+ for (const [k, v] of this.entries()) {
233
+ json[k] = v && v.toHuman(isExtended);
234
+ }
235
+
236
+ return json;
237
+ }
238
+ /**
239
+ * @description Converts the Object to JSON, typically used for RPC transfers
240
+ */
241
+
242
+
243
+ toJSON() {
244
+ const json = {};
245
+
246
+ for (const [k, v] of this.entries()) {
247
+ const jsonKey = (0, _classPrivateFieldLooseBase2.default)(this, _jsonMap)[_jsonMap].get(k) || k;
248
+ json[jsonKey] = v && v.toJSON();
249
+ }
250
+
251
+ return json;
252
+ }
253
+ /**
254
+ * @description Returns the base runtime type name for this instance
255
+ */
256
+
257
+
258
+ toRawType() {
259
+ return (0, _util.stringify)((0, _index.typesToMap)(this.registry, (0, _classPrivateFieldLooseBase2.default)(this, _Types)[_Types]));
260
+ }
261
+ /**
262
+ * @description Returns the string representation of the value
263
+ */
264
+
265
+
266
+ toString() {
267
+ return (0, _util.stringify)(this.toJSON());
268
+ }
269
+ /**
270
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
271
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
272
+ */
273
+
274
+
275
+ toU8a(isBare) {
276
+ const encoded = [];
277
+
278
+ for (const [k, v] of this.entries()) {
279
+ if (v && (0, _util.isFunction)(v.toU8a)) {
280
+ encoded.push(v.toU8a(!isBare || (0, _util.isBoolean)(isBare) ? isBare : isBare[k]));
281
+ }
282
+ }
283
+
284
+ return (0, _util.u8aConcat)(...encoded);
285
+ }
286
+
287
+ }
288
+
289
+ exports.Struct = Struct;
@@ -0,0 +1,92 @@
1
+ import type { HexString } from '@polkadot/util/types';
2
+ import type { AnyJson, BareOpts, Codec, CodecClass, CodecRegistry, IStruct, IU8a } from '../types';
3
+ declare type TypesDef<T = Codec> = Record<string, string | CodecClass<T>>;
4
+ /**
5
+ * @name Struct
6
+ * @description
7
+ * A Struct defines an Object with key-value pairs - where the values are Codec values. It removes
8
+ * a lot of repetition from the actual coding, define a structure type, pass it the key/Codec
9
+ * values in the constructor and it manages the decoding. It is important that the constructor
10
+ * values matches 100% to the order in th Rust code, i.e. don't go crazy and make it alphabetical,
11
+ * it needs to decoded in the specific defined order.
12
+ * @noInheritDoc
13
+ */
14
+ export declare class Struct<S extends TypesDef = TypesDef, V extends {
15
+ [K in keyof S]: any;
16
+ } = {
17
+ [K in keyof S]: any;
18
+ }, E extends {
19
+ [K in keyof S]: string;
20
+ } = {
21
+ [K in keyof S]: string;
22
+ }> extends Map<keyof S, Codec> implements IStruct<keyof S> {
23
+ #private;
24
+ readonly registry: CodecRegistry;
25
+ createdAtHash?: IU8a;
26
+ readonly initialU8aLength?: number;
27
+ constructor(registry: CodecRegistry, Types: S, value?: V | Map<unknown, unknown> | unknown[] | HexString | null, jsonMap?: Map<string, string>);
28
+ static with<S extends TypesDef>(Types: S, jsonMap?: Map<string, string>): CodecClass<Struct<S>>;
29
+ /**
30
+ * @description The available keys for this struct
31
+ */
32
+ get defKeys(): string[];
33
+ /**
34
+ * @description Checks if the value is an empty value
35
+ */
36
+ get isEmpty(): boolean;
37
+ /**
38
+ * @description Returns the Type description of the structure
39
+ */
40
+ get Type(): E;
41
+ /**
42
+ * @description The length of the value when encoded as a Uint8Array
43
+ */
44
+ get encodedLength(): number;
45
+ /**
46
+ * @description returns a hash of the contents
47
+ */
48
+ get hash(): IU8a;
49
+ /**
50
+ * @description Compares the value of the input to see if there is a match
51
+ */
52
+ eq(other?: unknown): boolean;
53
+ /**
54
+ * @description Returns a specific names entry in the structure
55
+ * @param name The name of the entry to retrieve
56
+ */
57
+ get(name: keyof S): Codec | undefined;
58
+ /**
59
+ * @description Returns the values of a member at a specific index (Rather use get(name) for performance)
60
+ */
61
+ getAtIndex(index: number): Codec;
62
+ /**
63
+ * @description Converts the Object to an standard JavaScript Array
64
+ */
65
+ toArray(): Codec[];
66
+ /**
67
+ * @description Returns a hex string representation of the value
68
+ */
69
+ toHex(): HexString;
70
+ /**
71
+ * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
72
+ */
73
+ toHuman(isExtended?: boolean): Record<string, AnyJson>;
74
+ /**
75
+ * @description Converts the Object to JSON, typically used for RPC transfers
76
+ */
77
+ toJSON(): Record<string, AnyJson>;
78
+ /**
79
+ * @description Returns the base runtime type name for this instance
80
+ */
81
+ toRawType(): string;
82
+ /**
83
+ * @description Returns the string representation of the value
84
+ */
85
+ toString(): string;
86
+ /**
87
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
88
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
89
+ */
90
+ toU8a(isBare?: BareOpts): Uint8Array;
91
+ }
92
+ export {};
@@ -0,0 +1,273 @@
1
+ import _classPrivateFieldLooseBase from "@babel/runtime/helpers/esm/classPrivateFieldLooseBase";
2
+ import _classPrivateFieldLooseKey from "@babel/runtime/helpers/esm/classPrivateFieldLooseKey";
3
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
4
+ // SPDX-License-Identifier: Apache-2.0
5
+ import { assert, isBoolean, isFunction, isHex, isObject, isU8a, isUndefined, objectProperties, stringCamelCase, stringify, u8aConcat, u8aToHex, u8aToU8a } from '@polkadot/util';
6
+ import { compareMap, decodeU8a, mapToTypeMap, typesToMap } from "../utils/index.js";
7
+
8
+ /** @internal */
9
+ function decodeStructFromObject(registry, Types, value, jsonMap) {
10
+ let jsonObj;
11
+ const inputKeys = Object.keys(Types);
12
+ const typeofArray = Array.isArray(value);
13
+ const typeofMap = value instanceof Map;
14
+ assert(typeofArray || typeofMap || isObject(value), () => `Struct: Cannot decode value ${stringify(value)} (typeof ${typeof value}), expected an input object, map or array`);
15
+ assert(!typeofArray || value.length === inputKeys.length, () => `Struct: Unable to map ${stringify(value)} array to object with known keys ${inputKeys.join(', ')}`);
16
+ const raw = new Array(inputKeys.length);
17
+
18
+ for (let i = 0; i < inputKeys.length; i++) {
19
+ const key = inputKeys[i];
20
+ const jsonKey = jsonMap.get(key) || key;
21
+ const Type = Types[key];
22
+ let assign;
23
+
24
+ try {
25
+ if (typeofArray) {
26
+ assign = value[i];
27
+ } else if (typeofMap) {
28
+ assign = jsonKey && value.get(jsonKey);
29
+ } else {
30
+ assign = jsonKey && value[jsonKey];
31
+
32
+ if (isUndefined(assign)) {
33
+ if (isUndefined(jsonObj)) {
34
+ const entries = Object.entries(value);
35
+ jsonObj = {};
36
+
37
+ for (let e = 0; e < entries.length; e++) {
38
+ jsonObj[stringCamelCase(entries[e][0])] = entries[e][1];
39
+ }
40
+ }
41
+
42
+ assign = jsonKey && jsonObj[jsonKey];
43
+ }
44
+ }
45
+
46
+ raw[i] = [key, assign instanceof Type ? assign : new Type(registry, assign)];
47
+ } catch (error) {
48
+ let type = Type.name;
49
+
50
+ try {
51
+ type = new Type(registry).toRawType();
52
+ } catch (error) {// ignore
53
+ }
54
+
55
+ throw new Error(`Struct: failed on ${jsonKey}: ${type}:: ${error.message}`);
56
+ }
57
+ }
58
+
59
+ return [raw, 0];
60
+ }
61
+ /**
62
+ * @name Struct
63
+ * @description
64
+ * A Struct defines an Object with key-value pairs - where the values are Codec values. It removes
65
+ * a lot of repetition from the actual coding, define a structure type, pass it the key/Codec
66
+ * values in the constructor and it manages the decoding. It is important that the constructor
67
+ * values matches 100% to the order in th Rust code, i.e. don't go crazy and make it alphabetical,
68
+ * it needs to decoded in the specific defined order.
69
+ * @noInheritDoc
70
+ */
71
+
72
+
73
+ var _jsonMap = /*#__PURE__*/_classPrivateFieldLooseKey("jsonMap");
74
+
75
+ var _Types = /*#__PURE__*/_classPrivateFieldLooseKey("Types");
76
+
77
+ export class Struct extends Map {
78
+ constructor(registry, Types, value, jsonMap = new Map()) {
79
+ const typeMap = mapToTypeMap(registry, Types);
80
+ const [decoded, decodedLength] = isU8a(value) ? decodeU8a(registry, value, typeMap, true) : isHex(value) ? decodeU8a(registry, u8aToU8a(value), typeMap, true) : value instanceof Struct ? [value, 0] : decodeStructFromObject(registry, typeMap, value || {}, jsonMap);
81
+ super(decoded);
82
+ this.registry = void 0;
83
+ this.createdAtHash = void 0;
84
+ this.initialU8aLength = void 0;
85
+ Object.defineProperty(this, _jsonMap, {
86
+ writable: true,
87
+ value: void 0
88
+ });
89
+ Object.defineProperty(this, _Types, {
90
+ writable: true,
91
+ value: void 0
92
+ });
93
+ this.registry = registry;
94
+ this.initialU8aLength = decodedLength;
95
+ _classPrivateFieldLooseBase(this, _jsonMap)[_jsonMap] = jsonMap;
96
+ _classPrivateFieldLooseBase(this, _Types)[_Types] = typeMap;
97
+ }
98
+
99
+ static with(Types, jsonMap) {
100
+ const keys = Object.keys(Types);
101
+ return class extends Struct {
102
+ constructor(registry, value) {
103
+ super(registry, Types, value, jsonMap);
104
+ objectProperties(this, keys, k => this.get(k));
105
+ }
106
+
107
+ };
108
+ }
109
+ /**
110
+ * @description The available keys for this struct
111
+ */
112
+
113
+
114
+ get defKeys() {
115
+ return Object.keys(_classPrivateFieldLooseBase(this, _Types)[_Types]);
116
+ }
117
+ /**
118
+ * @description Checks if the value is an empty value
119
+ */
120
+
121
+
122
+ get isEmpty() {
123
+ for (const v of this.values()) {
124
+ if (!v.isEmpty) {
125
+ return false;
126
+ }
127
+ }
128
+
129
+ return true;
130
+ }
131
+ /**
132
+ * @description Returns the Type description of the structure
133
+ */
134
+
135
+
136
+ get Type() {
137
+ const result = {};
138
+ const defs = Object.entries(_classPrivateFieldLooseBase(this, _Types)[_Types]);
139
+
140
+ for (let i = 0; i < defs.length; i++) {
141
+ const [key, Type] = defs[i];
142
+ result[key] = new Type(this.registry).toRawType();
143
+ }
144
+
145
+ return result;
146
+ }
147
+ /**
148
+ * @description The length of the value when encoded as a Uint8Array
149
+ */
150
+
151
+
152
+ get encodedLength() {
153
+ let total = 0;
154
+
155
+ for (const v of this.values()) {
156
+ total += v.encodedLength;
157
+ }
158
+
159
+ return total;
160
+ }
161
+ /**
162
+ * @description returns a hash of the contents
163
+ */
164
+
165
+
166
+ get hash() {
167
+ return this.registry.hash(this.toU8a());
168
+ }
169
+ /**
170
+ * @description Compares the value of the input to see if there is a match
171
+ */
172
+
173
+
174
+ eq(other) {
175
+ return compareMap(this, other);
176
+ }
177
+ /**
178
+ * @description Returns a specific names entry in the structure
179
+ * @param name The name of the entry to retrieve
180
+ */
181
+
182
+
183
+ get(name) {
184
+ return super.get(name);
185
+ }
186
+ /**
187
+ * @description Returns the values of a member at a specific index (Rather use get(name) for performance)
188
+ */
189
+
190
+
191
+ getAtIndex(index) {
192
+ return this.toArray()[index];
193
+ }
194
+ /**
195
+ * @description Converts the Object to an standard JavaScript Array
196
+ */
197
+
198
+
199
+ toArray() {
200
+ return [...this.values()];
201
+ }
202
+ /**
203
+ * @description Returns a hex string representation of the value
204
+ */
205
+
206
+
207
+ toHex() {
208
+ return u8aToHex(this.toU8a());
209
+ }
210
+ /**
211
+ * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
212
+ */
213
+
214
+
215
+ toHuman(isExtended) {
216
+ const json = {};
217
+
218
+ for (const [k, v] of this.entries()) {
219
+ json[k] = v && v.toHuman(isExtended);
220
+ }
221
+
222
+ return json;
223
+ }
224
+ /**
225
+ * @description Converts the Object to JSON, typically used for RPC transfers
226
+ */
227
+
228
+
229
+ toJSON() {
230
+ const json = {};
231
+
232
+ for (const [k, v] of this.entries()) {
233
+ const jsonKey = _classPrivateFieldLooseBase(this, _jsonMap)[_jsonMap].get(k) || k;
234
+ json[jsonKey] = v && v.toJSON();
235
+ }
236
+
237
+ return json;
238
+ }
239
+ /**
240
+ * @description Returns the base runtime type name for this instance
241
+ */
242
+
243
+
244
+ toRawType() {
245
+ return stringify(typesToMap(this.registry, _classPrivateFieldLooseBase(this, _Types)[_Types]));
246
+ }
247
+ /**
248
+ * @description Returns the string representation of the value
249
+ */
250
+
251
+
252
+ toString() {
253
+ return stringify(this.toJSON());
254
+ }
255
+ /**
256
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
257
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
258
+ */
259
+
260
+
261
+ toU8a(isBare) {
262
+ const encoded = [];
263
+
264
+ for (const [k, v] of this.entries()) {
265
+ if (v && isFunction(v.toU8a)) {
266
+ encoded.push(v.toU8a(!isBare || isBoolean(isBare) ? isBare : isBare[k]));
267
+ }
268
+ }
269
+
270
+ return u8aConcat(...encoded);
271
+ }
272
+
273
+ }