@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
package/base/Tuple.js ADDED
@@ -0,0 +1,116 @@
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 { isFunction, isHex, isString, isU8a, stringify, u8aConcat, u8aToU8a } from '@polkadot/util';
6
+ import { AbstractArray } from "../abstract/AbstractArray.js";
7
+ import { decodeU8a, mapToTypeMap, typeToConstructor } from "../utils/index.js";
8
+
9
+ /** @internal */
10
+ function decodeTuple(registry, Classes, value) {
11
+ if (isU8a(value) || isHex(value)) {
12
+ return decodeU8a(registry, u8aToU8a(value), Classes);
13
+ }
14
+
15
+ const Types = Array.isArray(Classes) ? Classes : Object.values(Classes);
16
+ return [Types.map((Type, index) => {
17
+ try {
18
+ const entry = value === null || value === void 0 ? void 0 : value[index];
19
+
20
+ if (entry instanceof Type) {
21
+ return entry;
22
+ }
23
+
24
+ return new Type(registry, entry);
25
+ } catch (error) {
26
+ throw new Error(`Tuple: failed on ${index}:: ${error.message}`);
27
+ }
28
+ }), 0];
29
+ }
30
+ /**
31
+ * @name Tuple
32
+ * @description
33
+ * A Tuple defines an anonymous fixed-length array, where each element has its
34
+ * own type. It extends the base JS `Array` object.
35
+ */
36
+
37
+
38
+ var _Types = /*#__PURE__*/_classPrivateFieldLooseKey("Types");
39
+
40
+ export class Tuple extends AbstractArray {
41
+ constructor(registry, Types, value) {
42
+ const Classes = Array.isArray(Types) ? Types.map(t => typeToConstructor(registry, t)) : isFunction(Types) || isString(Types) ? [typeToConstructor(registry, Types)] : mapToTypeMap(registry, Types);
43
+ const [values, decodedLength] = isU8a(value) ? decodeU8a(registry, value, Classes) : decodeTuple(registry, Classes, value);
44
+ super(registry, values, decodedLength);
45
+ Object.defineProperty(this, _Types, {
46
+ writable: true,
47
+ value: void 0
48
+ });
49
+ _classPrivateFieldLooseBase(this, _Types)[_Types] = Classes;
50
+ }
51
+
52
+ static with(Types) {
53
+ return class extends Tuple {
54
+ constructor(registry, value) {
55
+ super(registry, Types, value);
56
+ }
57
+
58
+ };
59
+ }
60
+ /**
61
+ * @description The length of the value when encoded as a Uint8Array
62
+ */
63
+
64
+
65
+ get encodedLength() {
66
+ let total = 0;
67
+
68
+ for (let i = 0; i < this.length; i++) {
69
+ total += this[i].encodedLength;
70
+ }
71
+
72
+ return total;
73
+ }
74
+ /**
75
+ * @description The types definition of the tuple
76
+ */
77
+
78
+
79
+ get Types() {
80
+ return Array.isArray(_classPrivateFieldLooseBase(this, _Types)[_Types]) ? _classPrivateFieldLooseBase(this, _Types)[_Types].map(T => new T(this.registry).toRawType()) : Object.keys(_classPrivateFieldLooseBase(this, _Types)[_Types]);
81
+ }
82
+ /**
83
+ * @description Returns the base runtime type name for this instance
84
+ */
85
+
86
+
87
+ toRawType() {
88
+ const types = (Array.isArray(_classPrivateFieldLooseBase(this, _Types)[_Types]) ? _classPrivateFieldLooseBase(this, _Types)[_Types] : Object.values(_classPrivateFieldLooseBase(this, _Types)[_Types])).map(T => this.registry.getClassName(T) || new T(this.registry).toRawType());
89
+ return `(${types.join(',')})`;
90
+ }
91
+ /**
92
+ * @description Returns the string representation of the value
93
+ */
94
+
95
+
96
+ toString() {
97
+ // Overwrite the default toString representation of Array.
98
+ return stringify(this.toJSON());
99
+ }
100
+ /**
101
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
102
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
103
+ */
104
+
105
+
106
+ toU8a(isBare) {
107
+ const encoded = new Array(this.length);
108
+
109
+ for (let i = 0; i < this.length; i++) {
110
+ encoded[i] = this[i].toU8a(isBare);
111
+ }
112
+
113
+ return u8aConcat(...encoded);
114
+ }
115
+
116
+ }
package/base/UInt.cjs ADDED
@@ -0,0 +1,39 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.UInt = void 0;
7
+
8
+ var _AbstractInt = require("../abstract/AbstractInt.cjs");
9
+
10
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
11
+ // SPDX-License-Identifier: Apache-2.0
12
+
13
+ /**
14
+ * @name UInt
15
+ * @description
16
+ * A generic unsigned integer codec. For Substrate all numbers are Little Endian encoded,
17
+ * this handles the encoding and decoding of those numbers. Upon construction
18
+ * the bitLength is provided and any additional use keeps the number to this
19
+ * length. This extends `BN`, so all methods available on a normal `BN` object
20
+ * is available here.
21
+ * @noInheritDoc
22
+ */
23
+ class UInt extends _AbstractInt.AbstractInt {
24
+ static with(bitLength, typeName) {
25
+ return class extends UInt {
26
+ constructor(registry, value) {
27
+ super(registry, value, bitLength);
28
+ }
29
+
30
+ toRawType() {
31
+ return typeName || super.toRawType();
32
+ }
33
+
34
+ };
35
+ }
36
+
37
+ }
38
+
39
+ exports.UInt = UInt;
package/base/UInt.d.ts ADDED
@@ -0,0 +1,15 @@
1
+ import type { CodecClass, UIntBitLength } from '../types';
2
+ import { AbstractInt } from '../abstract/AbstractInt';
3
+ /**
4
+ * @name UInt
5
+ * @description
6
+ * A generic unsigned integer codec. For Substrate all numbers are Little Endian encoded,
7
+ * this handles the encoding and decoding of those numbers. Upon construction
8
+ * the bitLength is provided and any additional use keeps the number to this
9
+ * length. This extends `BN`, so all methods available on a normal `BN` object
10
+ * is available here.
11
+ * @noInheritDoc
12
+ */
13
+ export declare class UInt extends AbstractInt {
14
+ static with(bitLength: UIntBitLength, typeName?: string): CodecClass<UInt>;
15
+ }
package/base/UInt.js ADDED
@@ -0,0 +1,29 @@
1
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { AbstractInt } from "../abstract/AbstractInt.js";
4
+ /**
5
+ * @name UInt
6
+ * @description
7
+ * A generic unsigned integer codec. For Substrate all numbers are Little Endian encoded,
8
+ * this handles the encoding and decoding of those numbers. Upon construction
9
+ * the bitLength is provided and any additional use keeps the number to this
10
+ * length. This extends `BN`, so all methods available on a normal `BN` object
11
+ * is available here.
12
+ * @noInheritDoc
13
+ */
14
+
15
+ export class UInt extends AbstractInt {
16
+ static with(bitLength, typeName) {
17
+ return class extends UInt {
18
+ constructor(registry, value) {
19
+ super(registry, value, bitLength);
20
+ }
21
+
22
+ toRawType() {
23
+ return typeName || super.toRawType();
24
+ }
25
+
26
+ };
27
+ }
28
+
29
+ }
package/base/Vec.cjs ADDED
@@ -0,0 +1,126 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.Vec = void 0;
9
+ exports.decodeVec = decodeVec;
10
+
11
+ var _classPrivateFieldLooseBase2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldLooseBase"));
12
+
13
+ var _classPrivateFieldLooseKey2 = _interopRequireDefault(require("@babel/runtime/helpers/classPrivateFieldLooseKey"));
14
+
15
+ var _util = require("@polkadot/util");
16
+
17
+ var _AbstractArray = require("../abstract/AbstractArray.cjs");
18
+
19
+ var _index = require("../utils/index.cjs");
20
+
21
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
22
+ // SPDX-License-Identifier: Apache-2.0
23
+ const MAX_LENGTH = 64 * 1024;
24
+ const l = (0, _util.logger)('Vec');
25
+
26
+ function decodeVec(registry, Type, value) {
27
+ let length = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : -1;
28
+
29
+ if (Array.isArray(value)) {
30
+ const result = new Array(value.length);
31
+
32
+ for (let i = 0; i < value.length; i++) {
33
+ const entry = value[i];
34
+
35
+ try {
36
+ result[i] = entry instanceof Type ? entry : new Type(registry, entry);
37
+ } catch (error) {
38
+ l.error(`Unable to decode on index ${i}`, error.message);
39
+ throw error;
40
+ }
41
+ }
42
+
43
+ return [result, 0, 0];
44
+ }
45
+
46
+ const u8a = (0, _util.u8aToU8a)(value);
47
+ let offset = 0;
48
+
49
+ if (length === -1) {
50
+ const [_offset, _length] = (0, _util.compactFromU8a)(u8a);
51
+ (0, _util.assert)(_length.lten(MAX_LENGTH), () => `Vec length ${_length.toString()} exceeds ${MAX_LENGTH}`);
52
+ length = _length.toNumber();
53
+ offset = _offset;
54
+ }
55
+
56
+ return (0, _index.decodeU8aVec)(registry, u8a, offset, Type, length);
57
+ }
58
+ /**
59
+ * @name Vec
60
+ * @description
61
+ * This manages codec arrays. Internally it keeps track of the length (as decoded) and allows
62
+ * construction with the passed `Type` in the constructor. It is an extension to Array, providing
63
+ * specific encoding/decoding on top of the base type.
64
+ */
65
+
66
+
67
+ var _Type = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("Type");
68
+
69
+ class Vec extends _AbstractArray.AbstractArray {
70
+ constructor(registry, Type) {
71
+ let value = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
72
+ const Clazz = (0, _index.typeToConstructor)(registry, Type);
73
+ const [values, decodedLength] = decodeVec(registry, Clazz, value);
74
+ super(registry, values, decodedLength);
75
+ Object.defineProperty(this, _Type, {
76
+ writable: true,
77
+ value: void 0
78
+ });
79
+ (0, _classPrivateFieldLooseBase2.default)(this, _Type)[_Type] = Clazz;
80
+ }
81
+
82
+ static with(Type) {
83
+ return class extends Vec {
84
+ constructor(registry, value) {
85
+ super(registry, Type, value);
86
+ }
87
+
88
+ };
89
+ }
90
+ /**
91
+ * @description The type for the items
92
+ */
93
+
94
+
95
+ get Type() {
96
+ return (0, _classPrivateFieldLooseBase2.default)(this, _Type)[_Type].name;
97
+ }
98
+ /**
99
+ * @description Finds the index of the value in the array
100
+ */
101
+
102
+
103
+ indexOf(_other) {
104
+ // convert type first, this removes overhead from the eq
105
+ const other = _other instanceof (0, _classPrivateFieldLooseBase2.default)(this, _Type)[_Type] ? _other : new ((0, _classPrivateFieldLooseBase2.default)(this, _Type)[_Type])(this.registry, _other);
106
+
107
+ for (let i = 0; i < this.length; i++) {
108
+ if (other.eq(this[i])) {
109
+ return i;
110
+ }
111
+ }
112
+
113
+ return -1;
114
+ }
115
+ /**
116
+ * @description Returns the base runtime type name for this instance
117
+ */
118
+
119
+
120
+ toRawType() {
121
+ return `Vec<${this.registry.getClassName((0, _classPrivateFieldLooseBase2.default)(this, _Type)[_Type]) || new ((0, _classPrivateFieldLooseBase2.default)(this, _Type)[_Type])(this.registry).toRawType()}>`;
122
+ }
123
+
124
+ }
125
+
126
+ exports.Vec = Vec;
package/base/Vec.d.ts ADDED
@@ -0,0 +1,28 @@
1
+ import type { HexString } from '@polkadot/util/types';
2
+ import type { Codec, CodecClass, CodecRegistry } from '../types';
3
+ import { AbstractArray } from '../abstract/AbstractArray';
4
+ export declare function decodeVec<T extends Codec>(registry: CodecRegistry, Type: CodecClass<T>, value: Uint8Array | HexString | unknown[], length?: number): [T[], number, number];
5
+ /**
6
+ * @name Vec
7
+ * @description
8
+ * This manages codec arrays. Internally it keeps track of the length (as decoded) and allows
9
+ * construction with the passed `Type` in the constructor. It is an extension to Array, providing
10
+ * specific encoding/decoding on top of the base type.
11
+ */
12
+ export declare class Vec<T extends Codec> extends AbstractArray<T> {
13
+ #private;
14
+ constructor(registry: CodecRegistry, Type: CodecClass<T> | string, value?: Uint8Array | HexString | unknown[]);
15
+ static with<O extends Codec>(Type: CodecClass<O> | string): CodecClass<Vec<O>>;
16
+ /**
17
+ * @description The type for the items
18
+ */
19
+ get Type(): string;
20
+ /**
21
+ * @description Finds the index of the value in the array
22
+ */
23
+ indexOf(_other?: unknown): number;
24
+ /**
25
+ * @description Returns the base runtime type name for this instance
26
+ */
27
+ toRawType(): string;
28
+ }
package/base/Vec.js ADDED
@@ -0,0 +1,104 @@
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, compactFromU8a, logger, u8aToU8a } from '@polkadot/util';
6
+ import { AbstractArray } from "../abstract/AbstractArray.js";
7
+ import { decodeU8aVec, typeToConstructor } from "../utils/index.js";
8
+ const MAX_LENGTH = 64 * 1024;
9
+ const l = logger('Vec');
10
+ export function decodeVec(registry, Type, value, length = -1) {
11
+ if (Array.isArray(value)) {
12
+ const result = new Array(value.length);
13
+
14
+ for (let i = 0; i < value.length; i++) {
15
+ const entry = value[i];
16
+
17
+ try {
18
+ result[i] = entry instanceof Type ? entry : new Type(registry, entry);
19
+ } catch (error) {
20
+ l.error(`Unable to decode on index ${i}`, error.message);
21
+ throw error;
22
+ }
23
+ }
24
+
25
+ return [result, 0, 0];
26
+ }
27
+
28
+ const u8a = u8aToU8a(value);
29
+ let offset = 0;
30
+
31
+ if (length === -1) {
32
+ const [_offset, _length] = compactFromU8a(u8a);
33
+ assert(_length.lten(MAX_LENGTH), () => `Vec length ${_length.toString()} exceeds ${MAX_LENGTH}`);
34
+ length = _length.toNumber();
35
+ offset = _offset;
36
+ }
37
+
38
+ return decodeU8aVec(registry, u8a, offset, Type, length);
39
+ }
40
+ /**
41
+ * @name Vec
42
+ * @description
43
+ * This manages codec arrays. Internally it keeps track of the length (as decoded) and allows
44
+ * construction with the passed `Type` in the constructor. It is an extension to Array, providing
45
+ * specific encoding/decoding on top of the base type.
46
+ */
47
+
48
+ var _Type = /*#__PURE__*/_classPrivateFieldLooseKey("Type");
49
+
50
+ export class Vec extends AbstractArray {
51
+ constructor(registry, Type, value = []) {
52
+ const Clazz = typeToConstructor(registry, Type);
53
+ const [values, decodedLength] = decodeVec(registry, Clazz, value);
54
+ super(registry, values, decodedLength);
55
+ Object.defineProperty(this, _Type, {
56
+ writable: true,
57
+ value: void 0
58
+ });
59
+ _classPrivateFieldLooseBase(this, _Type)[_Type] = Clazz;
60
+ }
61
+
62
+ static with(Type) {
63
+ return class extends Vec {
64
+ constructor(registry, value) {
65
+ super(registry, Type, value);
66
+ }
67
+
68
+ };
69
+ }
70
+ /**
71
+ * @description The type for the items
72
+ */
73
+
74
+
75
+ get Type() {
76
+ return _classPrivateFieldLooseBase(this, _Type)[_Type].name;
77
+ }
78
+ /**
79
+ * @description Finds the index of the value in the array
80
+ */
81
+
82
+
83
+ indexOf(_other) {
84
+ // convert type first, this removes overhead from the eq
85
+ const other = _other instanceof _classPrivateFieldLooseBase(this, _Type)[_Type] ? _other : new (_classPrivateFieldLooseBase(this, _Type)[_Type])(this.registry, _other);
86
+
87
+ for (let i = 0; i < this.length; i++) {
88
+ if (other.eq(this[i])) {
89
+ return i;
90
+ }
91
+ }
92
+
93
+ return -1;
94
+ }
95
+ /**
96
+ * @description Returns the base runtime type name for this instance
97
+ */
98
+
99
+
100
+ toRawType() {
101
+ return `Vec<${this.registry.getClassName(_classPrivateFieldLooseBase(this, _Type)[_Type]) || new (_classPrivateFieldLooseBase(this, _Type)[_Type])(this.registry).toRawType()}>`;
102
+ }
103
+
104
+ }
@@ -0,0 +1,31 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.VecAny = void 0;
7
+
8
+ var _AbstractArray = require("../abstract/AbstractArray.cjs");
9
+
10
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
11
+ // SPDX-License-Identifier: Apache-2.0
12
+
13
+ /**
14
+ * @name VecAny
15
+ * @description
16
+ * This manages codec arrays, assuming that the inputs are already of type Codec. Unlike
17
+ * a vector, this can be used to manage array-like structures with variable arguments of
18
+ * any types
19
+ */
20
+ class VecAny extends _AbstractArray.AbstractArray {
21
+ /**
22
+ * @description Returns the base runtime type name for this instance
23
+ */
24
+ toRawType() {
25
+ // FIXME This is basically an any type, cannot instantiate via createType
26
+ return 'Vec<Codec>';
27
+ }
28
+
29
+ }
30
+
31
+ exports.VecAny = VecAny;
@@ -0,0 +1,15 @@
1
+ import type { Codec } from '../types';
2
+ import { AbstractArray } from '../abstract/AbstractArray';
3
+ /**
4
+ * @name VecAny
5
+ * @description
6
+ * This manages codec arrays, assuming that the inputs are already of type Codec. Unlike
7
+ * a vector, this can be used to manage array-like structures with variable arguments of
8
+ * any types
9
+ */
10
+ export declare class VecAny<T extends Codec> extends AbstractArray<T> {
11
+ /**
12
+ * @description Returns the base runtime type name for this instance
13
+ */
14
+ toRawType(): string;
15
+ }
package/base/VecAny.js ADDED
@@ -0,0 +1,21 @@
1
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
2
+ // SPDX-License-Identifier: Apache-2.0
3
+ import { AbstractArray } from "../abstract/AbstractArray.js";
4
+ /**
5
+ * @name VecAny
6
+ * @description
7
+ * This manages codec arrays, assuming that the inputs are already of type Codec. Unlike
8
+ * a vector, this can be used to manage array-like structures with variable arguments of
9
+ * any types
10
+ */
11
+
12
+ export class VecAny extends AbstractArray {
13
+ /**
14
+ * @description Returns the base runtime type name for this instance
15
+ */
16
+ toRawType() {
17
+ // FIXME This is basically an any type, cannot instantiate via createType
18
+ return 'Vec<Codec>';
19
+ }
20
+
21
+ }
@@ -0,0 +1,111 @@
1
+ "use strict";
2
+
3
+ var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
+
5
+ Object.defineProperty(exports, "__esModule", {
6
+ value: true
7
+ });
8
+ exports.VecFixed = 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 _AbstractArray = require("../abstract/AbstractArray.cjs");
17
+
18
+ var _index = require("../utils/index.cjs");
19
+
20
+ var _Vec = require("./Vec.cjs");
21
+
22
+ // Copyright 2017-2021 @polkadot/types-codec authors & contributors
23
+ // SPDX-License-Identifier: Apache-2.0
24
+
25
+ /** @internal */
26
+ function decodeVecFixed(registry, value, Type, length) {
27
+ const [values, decodedLength, decodedLengthNoOffset] = (0, _Vec.decodeVec)(registry, Type, value, length);
28
+
29
+ while (values.length < length) {
30
+ values.push(new Type(registry));
31
+ }
32
+
33
+ (0, _util.assert)(values.length === length, () => `Expected a length of exactly ${length} entries`);
34
+ return [values, decodedLength, decodedLengthNoOffset];
35
+ }
36
+ /**
37
+ * @name VecFixed
38
+ * @description
39
+ * This manages codec arrays of a fixed length
40
+ */
41
+
42
+
43
+ var _Type = /*#__PURE__*/(0, _classPrivateFieldLooseKey2.default)("Type");
44
+
45
+ class VecFixed extends _AbstractArray.AbstractArray {
46
+ constructor(registry, Type, length) {
47
+ let value = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : [];
48
+ const Clazz = (0, _index.typeToConstructor)(registry, Type);
49
+ const [values,, decodedLengthNoOffset] = (0, _util.isU8a)(value) ? (0, _index.decodeU8aVec)(registry, value, 0, Clazz, length) : decodeVecFixed(registry, value, Clazz, length);
50
+ super(registry, values, decodedLengthNoOffset);
51
+ Object.defineProperty(this, _Type, {
52
+ writable: true,
53
+ value: void 0
54
+ });
55
+ (0, _classPrivateFieldLooseBase2.default)(this, _Type)[_Type] = Clazz;
56
+ }
57
+
58
+ static with(Type, length) {
59
+ return class extends VecFixed {
60
+ constructor(registry, value) {
61
+ super(registry, Type, length, value);
62
+ }
63
+
64
+ };
65
+ }
66
+ /**
67
+ * @description The type for the items
68
+ */
69
+
70
+
71
+ get Type() {
72
+ return new ((0, _classPrivateFieldLooseBase2.default)(this, _Type)[_Type])(this.registry).toRawType();
73
+ }
74
+ /**
75
+ * @description The length of the value when encoded as a Uint8Array
76
+ */
77
+
78
+
79
+ get encodedLength() {
80
+ let total = 0;
81
+
82
+ for (let i = 0; i < this.length; i++) {
83
+ total += this[i].encodedLength;
84
+ }
85
+
86
+ return total;
87
+ }
88
+
89
+ toU8a() {
90
+ // we override, we don't add the length prefix for ourselves, and at the same time we
91
+ // ignore isBare on entries, since they should be properly encoded at all times
92
+ const encoded = new Array(this.length);
93
+
94
+ for (let i = 0; i < this.length; i++) {
95
+ encoded[i] = this[i].toU8a();
96
+ }
97
+
98
+ return encoded.length ? (0, _util.u8aConcat)(...encoded) : new Uint8Array([]);
99
+ }
100
+ /**
101
+ * @description Returns the base runtime type name for this instance
102
+ */
103
+
104
+
105
+ toRawType() {
106
+ return `[${this.Type};${this.length}]`;
107
+ }
108
+
109
+ }
110
+
111
+ exports.VecFixed = VecFixed;
@@ -0,0 +1,26 @@
1
+ import type { HexString } from '@polkadot/util/types';
2
+ import type { Codec, CodecClass, CodecRegistry } from '../types';
3
+ import { AbstractArray } from '../abstract/AbstractArray';
4
+ /**
5
+ * @name VecFixed
6
+ * @description
7
+ * This manages codec arrays of a fixed length
8
+ */
9
+ export declare class VecFixed<T extends Codec> extends AbstractArray<T> {
10
+ #private;
11
+ constructor(registry: CodecRegistry, Type: CodecClass<T> | string, length: number, value?: Uint8Array | HexString | unknown[]);
12
+ static with<O extends Codec>(Type: CodecClass<O> | string, length: number): CodecClass<VecFixed<O>>;
13
+ /**
14
+ * @description The type for the items
15
+ */
16
+ get Type(): string;
17
+ /**
18
+ * @description The length of the value when encoded as a Uint8Array
19
+ */
20
+ get encodedLength(): number;
21
+ toU8a(): Uint8Array;
22
+ /**
23
+ * @description Returns the base runtime type name for this instance
24
+ */
25
+ toRawType(): string;
26
+ }