@polkadot/types-codec 6.12.2-10

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 +137 -0
  11. package/base/Base.d.ts +56 -0
  12. package/base/Base.js +125 -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 +445 -0
  20. package/base/Enum.d.ts +105 -0
  21. package/base/Enum.js +421 -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 +60 -0
  89. package/extended/WrapperOpaque.d.ts +14 -0
  90. package/extended/WrapperOpaque.js +49 -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 +137 -0
  104. package/native/Json.d.ts +56 -0
  105. package/native/Json.js +121 -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 +302 -0
  113. package/native/Struct.d.ts +93 -0
  114. package/native/Struct.js +286 -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 +69 -0
  178. package/types/interfaces.js +1 -0
  179. package/types/registry.cjs +1 -0
  180. package/types/registry.d.ts +61 -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,39 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.compareMap = compareMap;
7
+
8
+ var _util = require("@polkadot/util");
9
+
10
+ var _util2 = require("./util.cjs");
11
+
12
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
13
+ // SPDX-License-Identifier: Apache-2.0
14
+ function hasMismatch(a, b) {
15
+ return (0, _util.isUndefined)(a) || ((0, _util2.hasEq)(a) ? !a.eq(b) : a !== b);
16
+ }
17
+
18
+ function notEntry(value) {
19
+ return !Array.isArray(value) || value.length !== 2;
20
+ }
21
+
22
+ function compareMapArray(a, b) {
23
+ // equal number of entries and each entry in the array should match
24
+ return a.size === b.length && !b.some(e => notEntry(e) || hasMismatch(a.get(e[0]), e[1]));
25
+ } // NOTE These are used internally and when comparing objects, expects that
26
+ // when the second is an Map<string, Codec> that the first has to be as well
27
+
28
+
29
+ function compareMap(a, b) {
30
+ if (Array.isArray(b)) {
31
+ return compareMapArray(a, b);
32
+ } else if (b instanceof Map) {
33
+ return compareMapArray(a, [...b.entries()]);
34
+ } else if ((0, _util.isObject)(b)) {
35
+ return compareMapArray(a, Object.entries(b));
36
+ }
37
+
38
+ return false;
39
+ }
@@ -0,0 +1 @@
1
+ export declare function compareMap(a: Map<unknown, unknown>, b?: unknown): boolean;
@@ -0,0 +1,31 @@
1
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { isObject, isUndefined } from '@polkadot/util';
4
+ import { hasEq } from "./util.js";
5
+
6
+ function hasMismatch(a, b) {
7
+ return isUndefined(a) || (hasEq(a) ? !a.eq(b) : a !== b);
8
+ }
9
+
10
+ function notEntry(value) {
11
+ return !Array.isArray(value) || value.length !== 2;
12
+ }
13
+
14
+ function compareMapArray(a, b) {
15
+ // equal number of entries and each entry in the array should match
16
+ return a.size === b.length && !b.some(e => notEntry(e) || hasMismatch(a.get(e[0]), e[1]));
17
+ } // NOTE These are used internally and when comparing objects, expects that
18
+ // when the second is an Map<string, Codec> that the first has to be as well
19
+
20
+
21
+ export function compareMap(a, b) {
22
+ if (Array.isArray(b)) {
23
+ return compareMapArray(a, b);
24
+ } else if (b instanceof Map) {
25
+ return compareMapArray(a, [...b.entries()]);
26
+ } else if (isObject(b)) {
27
+ return compareMapArray(a, Object.entries(b));
28
+ }
29
+
30
+ return false;
31
+ }
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.compareSet = compareSet;
7
+
8
+ var _util = require("@polkadot/util");
9
+
10
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
11
+ // SPDX-License-Identifier: Apache-2.0
12
+ function compareSetArray(a, b) {
13
+ // equal number of entries and each entry in the array should match
14
+ return a.size === b.length && !b.some(e => !a.has(e));
15
+ } // NOTE These are used internally and when comparing objects, expects that
16
+ // when the second is an Set<string, Codec> that the first has to be as well
17
+
18
+
19
+ function compareSet(a, b) {
20
+ if (Array.isArray(b)) {
21
+ return compareSetArray(a, b);
22
+ } else if (b instanceof Set) {
23
+ return compareSetArray(a, [...b.values()]);
24
+ } else if ((0, _util.isObject)(b)) {
25
+ return compareSetArray(a, Object.values(b));
26
+ }
27
+
28
+ return false;
29
+ }
@@ -0,0 +1 @@
1
+ export declare function compareSet(a: Set<unknown>, b?: unknown): boolean;
@@ -0,0 +1,22 @@
1
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { isObject } from '@polkadot/util';
4
+
5
+ function compareSetArray(a, b) {
6
+ // equal number of entries and each entry in the array should match
7
+ return a.size === b.length && !b.some(e => !a.has(e));
8
+ } // NOTE These are used internally and when comparing objects, expects that
9
+ // when the second is an Set<string, Codec> that the first has to be as well
10
+
11
+
12
+ export function compareSet(a, b) {
13
+ if (Array.isArray(b)) {
14
+ return compareSetArray(a, b);
15
+ } else if (b instanceof Set) {
16
+ return compareSetArray(a, [...b.values()]);
17
+ } else if (isObject(b)) {
18
+ return compareSetArray(a, Object.values(b));
19
+ }
20
+
21
+ return false;
22
+ }
@@ -0,0 +1,69 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.decodeU8a = decodeU8a;
7
+ exports.decodeU8aVec = decodeU8aVec;
8
+
9
+ var _util = require("@polkadot/util");
10
+
11
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
12
+ // SPDX-License-Identifier: Apache-2.0
13
+ function formatFailure(error, type, u8a, key) {
14
+ return `decodeU8a: failed at ${(0, _util.u8aToHex)(u8a)}…${key ? ` on ${key}` : ''}${type ? `: ${type}` : ''}:: ${error.message}`;
15
+ }
16
+
17
+ function getRawType(registry, Type) {
18
+ try {
19
+ return new Type(registry).toRawType();
20
+ } catch {
21
+ return null;
22
+ }
23
+ }
24
+ /**
25
+ * Given an u8a, and an array of Type constructors, decode the u8a against the
26
+ * types, and return an array of decoded values.
27
+ *
28
+ * @param u8a - The u8a to decode.
29
+ * @param result - The result array (will be returned with values pushed)
30
+ * @param types - The array of CodecClass to decode the U8a against.
31
+ */
32
+
33
+
34
+ function decodeU8a(registry, u8a, types, withZip) {
35
+ const [Types, keys] = Array.isArray(types) ? [types, []] : [Object.values(types), Object.keys(types)];
36
+ const result = new Array(Types.length);
37
+ let offset = 0;
38
+
39
+ for (let i = 0; i < Types.length; i++) {
40
+ try {
41
+ const value = new Types[i](registry, u8a.subarray(offset));
42
+ offset += value.initialU8aLength || value.encodedLength;
43
+ result[i] = withZip ? [keys[i], value] : value;
44
+ } catch (error) {
45
+ throw new Error(formatFailure(error, getRawType(registry, Types[i]), u8a.subarray(offset, offset + 16), keys[i]));
46
+ }
47
+ }
48
+
49
+ return [result, offset];
50
+ } // Split from decodeU8a since this is specialized to 1 instance ... yes duplication, but
51
+ // since we have to do less checks (and these are intensive anyway), much faster
52
+
53
+
54
+ function decodeU8aVec(registry, u8a, start, Type, count) {
55
+ const result = new Array(count);
56
+ let offset = start;
57
+
58
+ for (let i = 0; i < count; i++) {
59
+ try {
60
+ const value = new Type(registry, u8a.subarray(offset));
61
+ offset += value.initialU8aLength || value.encodedLength;
62
+ result[i] = value;
63
+ } catch (error) {
64
+ throw new Error(formatFailure(error, getRawType(registry, Type), u8a.subarray(offset, offset + 16)));
65
+ }
66
+ }
67
+
68
+ return [result, offset, offset - start];
69
+ }
@@ -0,0 +1,13 @@
1
+ import type { Codec, CodecClass, CodecRegistry } from '../types';
2
+ /**
3
+ * Given an u8a, and an array of Type constructors, decode the u8a against the
4
+ * types, and return an array of decoded values.
5
+ *
6
+ * @param u8a - The u8a to decode.
7
+ * @param result - The result array (will be returned with values pushed)
8
+ * @param types - The array of CodecClass to decode the U8a against.
9
+ */
10
+ export declare function decodeU8a<T extends Codec = Codec, E = T>(registry: CodecRegistry, u8a: Uint8Array, types: CodecClass[] | {
11
+ [index: string]: CodecClass;
12
+ }, withZip?: boolean): [E[], number];
13
+ export declare function decodeU8aVec<T extends Codec = Codec>(registry: CodecRegistry, u8a: Uint8Array, start: number, Type: CodecClass<T>, count: number): [T[], number, number];
@@ -0,0 +1,60 @@
1
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { u8aToHex } from '@polkadot/util';
4
+
5
+ function formatFailure(error, type, u8a, key) {
6
+ return `decodeU8a: failed at ${u8aToHex(u8a)}…${key ? ` on ${key}` : ''}${type ? `: ${type}` : ''}:: ${error.message}`;
7
+ }
8
+
9
+ function getRawType(registry, Type) {
10
+ try {
11
+ return new Type(registry).toRawType();
12
+ } catch {
13
+ return null;
14
+ }
15
+ }
16
+ /**
17
+ * Given an u8a, and an array of Type constructors, decode the u8a against the
18
+ * types, and return an array of decoded values.
19
+ *
20
+ * @param u8a - The u8a to decode.
21
+ * @param result - The result array (will be returned with values pushed)
22
+ * @param types - The array of CodecClass to decode the U8a against.
23
+ */
24
+
25
+
26
+ export function decodeU8a(registry, u8a, types, withZip) {
27
+ const [Types, keys] = Array.isArray(types) ? [types, []] : [Object.values(types), Object.keys(types)];
28
+ const result = new Array(Types.length);
29
+ let offset = 0;
30
+
31
+ for (let i = 0; i < Types.length; i++) {
32
+ try {
33
+ const value = new Types[i](registry, u8a.subarray(offset));
34
+ offset += value.initialU8aLength || value.encodedLength;
35
+ result[i] = withZip ? [keys[i], value] : value;
36
+ } catch (error) {
37
+ throw new Error(formatFailure(error, getRawType(registry, Types[i]), u8a.subarray(offset, offset + 16), keys[i]));
38
+ }
39
+ }
40
+
41
+ return [result, offset];
42
+ } // Split from decodeU8a since this is specialized to 1 instance ... yes duplication, but
43
+ // since we have to do less checks (and these are intensive anyway), much faster
44
+
45
+ export function decodeU8aVec(registry, u8a, start, Type, count) {
46
+ const result = new Array(count);
47
+ let offset = start;
48
+
49
+ for (let i = 0; i < count; i++) {
50
+ try {
51
+ const value = new Type(registry, u8a.subarray(offset));
52
+ offset += value.initialU8aLength || value.encodedLength;
53
+ result[i] = value;
54
+ } catch (error) {
55
+ throw new Error(formatFailure(error, getRawType(registry, Type), u8a.subarray(offset, offset + 16)));
56
+ }
57
+ }
58
+
59
+ return [result, offset, offset - start];
60
+ }
@@ -0,0 +1,95 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ Object.defineProperty(exports, "compareArray", {
7
+ enumerable: true,
8
+ get: function () {
9
+ return _compareArray.compareArray;
10
+ }
11
+ });
12
+ Object.defineProperty(exports, "compareMap", {
13
+ enumerable: true,
14
+ get: function () {
15
+ return _compareMap.compareMap;
16
+ }
17
+ });
18
+ Object.defineProperty(exports, "compareSet", {
19
+ enumerable: true,
20
+ get: function () {
21
+ return _compareSet.compareSet;
22
+ }
23
+ });
24
+ Object.defineProperty(exports, "decodeU8a", {
25
+ enumerable: true,
26
+ get: function () {
27
+ return _decodeU8a.decodeU8a;
28
+ }
29
+ });
30
+ Object.defineProperty(exports, "decodeU8aVec", {
31
+ enumerable: true,
32
+ get: function () {
33
+ return _decodeU8a.decodeU8aVec;
34
+ }
35
+ });
36
+ Object.defineProperty(exports, "mapToTypeMap", {
37
+ enumerable: true,
38
+ get: function () {
39
+ return _mapToTypeMap.mapToTypeMap;
40
+ }
41
+ });
42
+ Object.defineProperty(exports, "sanitize", {
43
+ enumerable: true,
44
+ get: function () {
45
+ return _sanitize.sanitize;
46
+ }
47
+ });
48
+ Object.defineProperty(exports, "sortAsc", {
49
+ enumerable: true,
50
+ get: function () {
51
+ return _sortValues.sortAsc;
52
+ }
53
+ });
54
+ Object.defineProperty(exports, "sortMap", {
55
+ enumerable: true,
56
+ get: function () {
57
+ return _sortValues.sortMap;
58
+ }
59
+ });
60
+ Object.defineProperty(exports, "sortSet", {
61
+ enumerable: true,
62
+ get: function () {
63
+ return _sortValues.sortSet;
64
+ }
65
+ });
66
+ Object.defineProperty(exports, "typeToConstructor", {
67
+ enumerable: true,
68
+ get: function () {
69
+ return _typeToConstructor.typeToConstructor;
70
+ }
71
+ });
72
+ Object.defineProperty(exports, "typesToMap", {
73
+ enumerable: true,
74
+ get: function () {
75
+ return _typesToMap.typesToMap;
76
+ }
77
+ });
78
+
79
+ var _compareArray = require("./compareArray.cjs");
80
+
81
+ var _compareMap = require("./compareMap.cjs");
82
+
83
+ var _compareSet = require("./compareSet.cjs");
84
+
85
+ var _decodeU8a = require("./decodeU8a.cjs");
86
+
87
+ var _mapToTypeMap = require("./mapToTypeMap.cjs");
88
+
89
+ var _typeToConstructor = require("./typeToConstructor.cjs");
90
+
91
+ var _sanitize = require("./sanitize.cjs");
92
+
93
+ var _sortValues = require("./sortValues.cjs");
94
+
95
+ var _typesToMap = require("./typesToMap.cjs");
@@ -0,0 +1,9 @@
1
+ export { compareArray } from './compareArray';
2
+ export { compareMap } from './compareMap';
3
+ export { compareSet } from './compareSet';
4
+ export { decodeU8a, decodeU8aVec } from './decodeU8a';
5
+ export { mapToTypeMap } from './mapToTypeMap';
6
+ export { typeToConstructor } from './typeToConstructor';
7
+ export { sanitize } from './sanitize';
8
+ export { sortAsc, sortSet, sortMap } from './sortValues';
9
+ export { typesToMap } from './typesToMap';
package/utils/index.js ADDED
@@ -0,0 +1,11 @@
1
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ export { compareArray } from "./compareArray.js";
4
+ export { compareMap } from "./compareMap.js";
5
+ export { compareSet } from "./compareSet.js";
6
+ export { decodeU8a, decodeU8aVec } from "./decodeU8a.js";
7
+ export { mapToTypeMap } from "./mapToTypeMap.js";
8
+ export { typeToConstructor } from "./typeToConstructor.js";
9
+ export { sanitize } from "./sanitize.js";
10
+ export { sortAsc, sortSet, sortMap } from "./sortValues.js";
11
+ export { typesToMap } from "./typesToMap.js";
@@ -0,0 +1,25 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.mapToTypeMap = mapToTypeMap;
7
+
8
+ var _typeToConstructor = require("./typeToConstructor.cjs");
9
+
10
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
11
+ // SPDX-License-Identifier: Apache-2.0
12
+
13
+ /**
14
+ * @description takes an input map of the form `{ [string]: string | CodecClass }` and returns a map of `{ [string]: CodecClass }`
15
+ */
16
+ function mapToTypeMap(registry, input) {
17
+ const entries = Object.entries(input);
18
+ const output = {};
19
+
20
+ for (let i = 0; i < entries.length; i++) {
21
+ output[entries[i][0]] = (0, _typeToConstructor.typeToConstructor)(registry, entries[i][1]);
22
+ }
23
+
24
+ return output;
25
+ }
@@ -0,0 +1,5 @@
1
+ import type { CodecClass, CodecRegistry } from '../types';
2
+ /**
3
+ * @description takes an input map of the form `{ [string]: string | CodecClass }` and returns a map of `{ [string]: CodecClass }`
4
+ */
5
+ export declare function mapToTypeMap(registry: CodecRegistry, input: Record<string, string | CodecClass>): Record<string, CodecClass>;
@@ -0,0 +1,17 @@
1
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { typeToConstructor } from "./typeToConstructor.js";
4
+ /**
5
+ * @description takes an input map of the form `{ [string]: string | CodecClass }` and returns a map of `{ [string]: CodecClass }`
6
+ */
7
+
8
+ export function mapToTypeMap(registry, input) {
9
+ const entries = Object.entries(input);
10
+ const output = {};
11
+
12
+ for (let i = 0; i < entries.length; i++) {
13
+ output[entries[i][0]] = typeToConstructor(registry, entries[i][1]);
14
+ }
15
+
16
+ return output;
17
+ }
@@ -0,0 +1,242 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.alias = alias;
7
+ exports.cleanupCompact = cleanupCompact;
8
+ exports.findClosing = findClosing;
9
+ exports.flattenSingleTuple = flattenSingleTuple;
10
+ exports.removeColons = removeColons;
11
+ exports.removeExtensions = removeExtensions;
12
+ exports.removeGenerics = removeGenerics;
13
+ exports.removePairOf = removePairOf;
14
+ exports.removeTraits = removeTraits;
15
+ exports.removeWrap = removeWrap;
16
+ exports.sanitize = sanitize;
17
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
18
+ // SPDX-License-Identifier: Apache-2.0
19
+ const BOUNDED = ['BTreeMap', 'BTreeSet', 'HashMap', 'Vec'];
20
+ const ALLOWED_BOXES = BOUNDED.concat(['Compact', 'DoNotConstruct', 'Int', 'Linkage', 'Range', 'RangeInclusive', 'Result', 'Option', 'UInt', 'WrapperOpaque']);
21
+ const BOX_PRECEDING = ['<', '(', '[', '"', ',', ' ']; // start of vec, tuple, fixed array, part of struct def or in tuple
22
+
23
+ const mappings = [// alias <T::InherentOfflineReport as InherentOfflineReport>::Inherent -> InherentOfflineReport
24
+ alias('<T::InherentOfflineReport as InherentOfflineReport>::Inherent', 'InherentOfflineReport', false), alias('VecDeque<', 'Vec<', false), // <T::Balance as HasCompact>
25
+ cleanupCompact(), // Change BoundedVec<Type, Size> to Vec<Type>
26
+ removeExtensions('Bounded', true), // Change WeakVec<Type> to Vec<Type>
27
+ removeExtensions('Weak', false), // Remove all the trait prefixes
28
+ removeTraits(), // remove PairOf<T> -> (T, T)
29
+ removePairOf(), // remove boxing, `Box<Proposal>` -> `Proposal`
30
+ removeWrap('Box<'), // remove generics, `MisbehaviorReport<Hash, BlockNumber>` -> `MisbehaviorReport`
31
+ removeGenerics(), // alias String -> Text (compat with jsonrpc methods)
32
+ alias('String', 'Text'), // alias Vec<u8> -> Bytes
33
+ alias('Vec<u8>', 'Bytes'), alias('&\\[u8\\]', 'Bytes'), alias("&'static\\[u8\\]", 'Bytes'), // alias RawAddress -> Address
34
+ alias('RawAddress', 'Address'), // lookups, mapped to Address/AccountId as appropriate in runtime
35
+ alias('Lookup::Source', 'LookupSource'), alias('Lookup::Target', 'LookupTarget'), // HACK duplication between contracts & primitives, however contracts prefixed with exec
36
+ alias('exec::StorageKey', 'ContractStorageKey'), // flattens tuples with one value, `(AccountId)` -> `AccountId`
37
+ flattenSingleTuple(), // converts ::Type to Type, <T as Trait<I>>::Proposal -> Proposal
38
+ removeColons()]; // given a starting index, find the closing >
39
+
40
+ function findClosing(value, start) {
41
+ let depth = 0;
42
+
43
+ for (let index = start; index < value.length; index++) {
44
+ if (value[index] === '>') {
45
+ if (!depth) {
46
+ return index;
47
+ }
48
+
49
+ depth--;
50
+ } else if (value[index] === '<') {
51
+ depth++;
52
+ }
53
+ }
54
+
55
+ throw new Error(`Unable to find closing matching <> on '${value}' (start ${start})`);
56
+ }
57
+
58
+ function alias(src, dest) {
59
+ let withChecks = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : true;
60
+ const from = new RegExp(`(^${src}|${BOX_PRECEDING.map(box => `\\${box}${src}`).join('|')})`, 'g');
61
+
62
+ const to = src => {
63
+ from.lastIndex = 0;
64
+ return withChecks && BOX_PRECEDING.includes(src[0]) ? `${src[0]}${dest}` : dest;
65
+ };
66
+
67
+ return value => value.replace(from, to);
68
+ }
69
+
70
+ function cleanupCompact() {
71
+ return value => {
72
+ for (let index = 0; index < value.length; index++) {
73
+ if (value[index] === '<') {
74
+ const end = findClosing(value, index + 1) - 14;
75
+
76
+ if (value.substr(end, 14) === ' as HasCompact') {
77
+ value = `Compact<${value.substr(index + 1, end - index - 1)}>`;
78
+ }
79
+ }
80
+ }
81
+
82
+ return value;
83
+ };
84
+ }
85
+
86
+ function flattenSingleTuple() {
87
+ const from1 = /,\)/g;
88
+ const from2 = /\(([^,]+)\)/;
89
+ return value => {
90
+ from1.lastIndex = 0;
91
+ return value // tuples may have trailing commas, e.g. (u32, BlockNumber, )
92
+ .replace(from1, ')') // change (u32) -> u32
93
+ .replace(from2, '$1');
94
+ };
95
+ }
96
+
97
+ function replaceTagWith(value, matcher, replacer) {
98
+ let index = -1;
99
+
100
+ while (true) {
101
+ index = value.indexOf(matcher, index + 1);
102
+
103
+ if (index === -1) {
104
+ return value;
105
+ }
106
+
107
+ const start = index + matcher.length;
108
+ const end = findClosing(value, start);
109
+ value = `${value.substr(0, index)}${replacer(value.substr(start, end - start))}${value.substr(end + 1)}`;
110
+ }
111
+ } // remove the Bounded* or Weak* wrappers
112
+
113
+
114
+ function removeExtensions(type, isSized) {
115
+ return value => BOUNDED.reduce((value, tag) => replaceTagWith(value, `${type}${tag}<`, inner => {
116
+ const parts = inner.split(',').map(s => s.trim()).filter(s => s);
117
+
118
+ if (isSized) {
119
+ parts.pop();
120
+ }
121
+
122
+ return `${tag}<${parts.join(',')}>`;
123
+ }), value);
124
+ }
125
+
126
+ function removeColons() {
127
+ return function (value) {
128
+ let {
129
+ allowNamespaces
130
+ } = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
131
+ let index = 0;
132
+
133
+ while (index !== -1) {
134
+ index = value.indexOf('::');
135
+
136
+ if (index === 0) {
137
+ value = value.substr(2);
138
+ } else if (index !== -1) {
139
+ if (allowNamespaces) {
140
+ return value;
141
+ }
142
+
143
+ let start = index;
144
+
145
+ while (start !== -1 && !BOX_PRECEDING.includes(value[start])) {
146
+ start--;
147
+ }
148
+
149
+ value = `${value.substr(0, start + 1)}${value.substr(index + 2)}`;
150
+ }
151
+ }
152
+
153
+ return value;
154
+ };
155
+ }
156
+
157
+ function removeGenerics() {
158
+ return value => {
159
+ for (let index = 0; index < value.length; index++) {
160
+ if (value[index] === '<') {
161
+ // check against the allowed wrappers, be it Vec<..>, Option<...> ...
162
+ const box = ALLOWED_BOXES.find(box => {
163
+ const start = index - box.length;
164
+ return start >= 0 && value.substr(start, box.length) === box && ( // make sure it is stand-alone, i.e. don't catch ElectionResult<...> as Result<...>
165
+ start === 0 || BOX_PRECEDING.includes(value[start - 1]));
166
+ }); // we have not found anything, unwrap generic innards
167
+
168
+ if (!box) {
169
+ const end = findClosing(value, index + 1);
170
+ value = `${value.substr(0, index)}${value.substr(end + 1)}`;
171
+ }
172
+ }
173
+ }
174
+
175
+ return value;
176
+ };
177
+ } // remove the PairOf wrappers
178
+
179
+
180
+ function removePairOf() {
181
+ const replacer = inner => `(${inner},${inner})`;
182
+
183
+ return value => replaceTagWith(value, 'PairOf<', replacer);
184
+ } // remove the type traits
185
+
186
+
187
+ function removeTraits() {
188
+ const from1 = /\s/g;
189
+ const from2 = /(T|Self)::/g;
190
+ const from3 = /<(T|Self)asTrait>::/g;
191
+ const from4 = /<Tas[a-z]+::Trait>::/g;
192
+ const from5 = /<LookupasStaticLookup>/g;
193
+ const from6 = /::Type/g;
194
+ return value => {
195
+ from1.lastIndex = 0;
196
+ from2.lastIndex = 0;
197
+ from3.lastIndex = 0;
198
+ from4.lastIndex = 0;
199
+ from5.lastIndex = 0;
200
+ from6.lastIndex = 0;
201
+ return value // remove all whitespaces
202
+ .replace(from1, '') // anything `T::<type>` to end up as `<type>`
203
+ .replace(from2, '') // replace `<T as Trait>::` (whitespaces were removed above)
204
+ .replace(from3, '') // replace `<T as something::Trait>::` (whitespaces were removed above)
205
+ .replace(from4, '') // replace <Lookup as StaticLookup>
206
+ .replace(from5, 'Lookup') // replace `<...>::Type`
207
+ .replace(from6, '');
208
+ };
209
+ } // remove wrapping values, i.e. Box<Proposal> -> Proposal
210
+
211
+
212
+ function removeWrap(check) {
213
+ const replacer = inner => inner;
214
+
215
+ return value => replaceTagWith(value, check, replacer);
216
+ }
217
+
218
+ const sanitizeMap = new Map(); // eslint-disable-next-line @typescript-eslint/ban-types
219
+
220
+ function sanitize(value, options) {
221
+ let result = value.toString();
222
+
223
+ if (!options) {
224
+ const memoized = sanitizeMap.get(result);
225
+
226
+ if (memoized) {
227
+ return memoized;
228
+ }
229
+ }
230
+
231
+ for (let i = 0; i < mappings.length; i++) {
232
+ result = mappings[i](result, options);
233
+ }
234
+
235
+ result = result.trim();
236
+
237
+ if (!options) {
238
+ sanitizeMap.set(value.toString(), result);
239
+ }
240
+
241
+ return result;
242
+ }