@polkadot/types-codec 9.14.2 → 10.1.1

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 (229) hide show
  1. package/abstract/Array.d.ts +1 -1
  2. package/abstract/Array.js +136 -155
  3. package/abstract/Base.d.ts +1 -1
  4. package/abstract/Base.js +95 -113
  5. package/abstract/Int.d.ts +1 -1
  6. package/abstract/Int.js +209 -220
  7. package/abstract/Object.d.ts +1 -1
  8. package/abstract/Object.js +29 -45
  9. package/abstract/index.d.ts +3 -3
  10. package/abstract/index.js +3 -6
  11. package/base/Compact.d.ts +1 -1
  12. package/base/Compact.js +149 -166
  13. package/base/DoNotConstruct.d.ts +1 -1
  14. package/base/DoNotConstruct.js +62 -65
  15. package/base/Enum.d.ts +1 -1
  16. package/base/Enum.js +347 -383
  17. package/base/Int.d.ts +2 -2
  18. package/base/Int.js +14 -18
  19. package/base/Null.d.ts +1 -1
  20. package/base/Null.js +68 -82
  21. package/base/Option.d.ts +1 -1
  22. package/base/Option.js +217 -227
  23. package/base/Result.d.ts +2 -2
  24. package/base/Result.js +54 -67
  25. package/base/Tuple.d.ts +2 -2
  26. package/base/Tuple.js +100 -102
  27. package/base/UInt.d.ts +2 -2
  28. package/base/UInt.js +11 -15
  29. package/base/Vec.d.ts +2 -2
  30. package/base/Vec.js +88 -90
  31. package/base/VecAny.d.ts +2 -2
  32. package/base/VecAny.js +8 -12
  33. package/base/VecFixed.d.ts +2 -2
  34. package/base/VecFixed.js +63 -71
  35. package/base/index.d.ts +12 -12
  36. package/base/index.js +12 -15
  37. package/bundle.d.ts +7 -7
  38. package/bundle.js +7 -10
  39. package/cjs/abstract/Array.js +138 -159
  40. package/cjs/abstract/Base.js +96 -117
  41. package/cjs/abstract/Int.js +215 -238
  42. package/cjs/abstract/Object.js +30 -49
  43. package/cjs/abstract/index.js +8 -25
  44. package/cjs/base/Compact.js +151 -173
  45. package/cjs/base/DoNotConstruct.js +63 -70
  46. package/cjs/base/Enum.js +350 -396
  47. package/cjs/base/Int.js +16 -24
  48. package/cjs/base/Null.js +70 -86
  49. package/cjs/base/Option.js +220 -234
  50. package/cjs/base/Result.js +56 -71
  51. package/cjs/base/Tuple.js +103 -109
  52. package/cjs/base/UInt.js +13 -19
  53. package/cjs/base/Vec.js +94 -101
  54. package/cjs/base/VecAny.js +10 -16
  55. package/cjs/base/VecFixed.js +66 -79
  56. package/cjs/base/index.js +26 -88
  57. package/cjs/bundle.js +11 -86
  58. package/cjs/detectOther.js +2 -10
  59. package/cjs/detectPackage.js +6 -11
  60. package/cjs/extended/BTreeMap.js +10 -16
  61. package/cjs/extended/BTreeSet.js +162 -178
  62. package/cjs/extended/BitVec.js +70 -77
  63. package/cjs/extended/Bytes.js +59 -62
  64. package/cjs/extended/HashMap.js +10 -16
  65. package/cjs/extended/Linkage.js +50 -60
  66. package/cjs/extended/Map.js +176 -181
  67. package/cjs/extended/OptionBool.js +65 -68
  68. package/cjs/extended/Range.js +37 -46
  69. package/cjs/extended/RangeInclusive.js +13 -21
  70. package/cjs/extended/Type.js +14 -22
  71. package/cjs/extended/U8aFixed.js +33 -43
  72. package/cjs/extended/WrapperKeepOpaque.js +99 -99
  73. package/cjs/extended/WrapperOpaque.js +19 -28
  74. package/cjs/extended/index.js +31 -108
  75. package/cjs/index.js +4 -16
  76. package/cjs/native/Bool.js +101 -116
  77. package/cjs/native/Date.js +125 -151
  78. package/cjs/native/Float.js +95 -114
  79. package/cjs/native/Json.js +102 -124
  80. package/cjs/native/Raw.js +128 -150
  81. package/cjs/native/Set.js +200 -220
  82. package/cjs/native/Struct.data.js +2 -9
  83. package/cjs/native/Struct.js +238 -251
  84. package/cjs/native/Text.js +137 -149
  85. package/cjs/native/index.js +21 -78
  86. package/cjs/packageInfo.js +2 -16
  87. package/cjs/primitive/F32.js +8 -11
  88. package/cjs/primitive/F64.js +8 -11
  89. package/cjs/primitive/I128.js +8 -11
  90. package/cjs/primitive/I16.js +8 -11
  91. package/cjs/primitive/I256.js +8 -11
  92. package/cjs/primitive/I32.js +8 -11
  93. package/cjs/primitive/I64.js +8 -11
  94. package/cjs/primitive/I8.js +8 -11
  95. package/cjs/primitive/U128.js +8 -11
  96. package/cjs/primitive/U16.js +8 -11
  97. package/cjs/primitive/U256.js +8 -11
  98. package/cjs/primitive/U32.js +8 -11
  99. package/cjs/primitive/U64.js +8 -11
  100. package/cjs/primitive/U8.js +8 -11
  101. package/cjs/primitive/USize.js +7 -13
  102. package/cjs/primitive/index.js +47 -199
  103. package/cjs/types/codec.js +2 -1
  104. package/cjs/types/helpers.js +2 -1
  105. package/cjs/types/index.js +6 -48
  106. package/cjs/types/interfaces.js +2 -1
  107. package/cjs/types/registry.js +2 -1
  108. package/cjs/utils/compareArray.js +12 -17
  109. package/cjs/utils/compareMap.js +23 -26
  110. package/cjs/utils/compareSet.js +17 -23
  111. package/cjs/utils/decodeU8a.js +63 -74
  112. package/cjs/utils/index.js +24 -91
  113. package/cjs/utils/mapToTypeMap.js +13 -18
  114. package/cjs/utils/sanitize.js +198 -204
  115. package/cjs/utils/sortValues.js +39 -49
  116. package/cjs/utils/typeToConstructor.js +8 -11
  117. package/cjs/utils/typesToMap.js +10 -16
  118. package/cjs/utils/util.js +6 -11
  119. package/detectOther.js +0 -5
  120. package/detectPackage.js +2 -7
  121. package/extended/BTreeMap.d.ts +2 -2
  122. package/extended/BTreeMap.js +8 -11
  123. package/extended/BTreeSet.d.ts +1 -1
  124. package/extended/BTreeSet.js +159 -172
  125. package/extended/BitVec.d.ts +2 -2
  126. package/extended/BitVec.js +67 -71
  127. package/extended/Bytes.d.ts +2 -2
  128. package/extended/Bytes.js +56 -57
  129. package/extended/HashMap.d.ts +2 -2
  130. package/extended/HashMap.js +8 -11
  131. package/extended/Linkage.d.ts +4 -4
  132. package/extended/Linkage.js +46 -51
  133. package/extended/Map.d.ts +1 -1
  134. package/extended/Map.js +173 -174
  135. package/extended/OptionBool.d.ts +3 -3
  136. package/extended/OptionBool.js +62 -62
  137. package/extended/Range.d.ts +2 -2
  138. package/extended/Range.js +35 -40
  139. package/extended/RangeInclusive.d.ts +2 -2
  140. package/extended/RangeInclusive.js +11 -16
  141. package/extended/Type.d.ts +2 -2
  142. package/extended/Type.js +12 -17
  143. package/extended/U8aFixed.d.ts +2 -2
  144. package/extended/U8aFixed.js +30 -36
  145. package/extended/WrapperKeepOpaque.d.ts +2 -2
  146. package/extended/WrapperKeepOpaque.js +96 -92
  147. package/extended/WrapperOpaque.d.ts +2 -2
  148. package/extended/WrapperOpaque.js +17 -23
  149. package/extended/index.d.ts +14 -14
  150. package/extended/index.js +14 -17
  151. package/index.d.ts +2 -2
  152. package/index.js +2 -5
  153. package/native/Bool.d.ts +1 -1
  154. package/native/Bool.js +99 -111
  155. package/native/Date.d.ts +1 -1
  156. package/native/Date.js +123 -144
  157. package/native/Float.d.ts +1 -1
  158. package/native/Float.js +93 -108
  159. package/native/Json.d.ts +1 -1
  160. package/native/Json.js +100 -114
  161. package/native/Raw.d.ts +1 -1
  162. package/native/Raw.js +126 -146
  163. package/native/Set.d.ts +1 -1
  164. package/native/Set.js +197 -211
  165. package/native/Struct.d.ts +1 -1
  166. package/native/Struct.data.js +0 -3
  167. package/native/Struct.js +235 -242
  168. package/native/Text.d.ts +1 -1
  169. package/native/Text.js +135 -144
  170. package/native/index.d.ts +8 -8
  171. package/native/index.js +8 -11
  172. package/package.json +8 -5
  173. package/packageInfo.js +1 -11
  174. package/primitive/F32.d.ts +2 -2
  175. package/primitive/F32.js +6 -7
  176. package/primitive/F64.d.ts +2 -2
  177. package/primitive/F64.js +6 -7
  178. package/primitive/I128.d.ts +2 -2
  179. package/primitive/I128.js +6 -7
  180. package/primitive/I16.d.ts +2 -2
  181. package/primitive/I16.js +6 -7
  182. package/primitive/I256.d.ts +2 -2
  183. package/primitive/I256.js +6 -7
  184. package/primitive/I32.d.ts +2 -2
  185. package/primitive/I32.js +6 -7
  186. package/primitive/I64.d.ts +2 -2
  187. package/primitive/I64.js +6 -7
  188. package/primitive/I8.d.ts +2 -2
  189. package/primitive/I8.js +6 -7
  190. package/primitive/U128.d.ts +2 -2
  191. package/primitive/U128.js +6 -7
  192. package/primitive/U16.d.ts +2 -2
  193. package/primitive/U16.js +6 -7
  194. package/primitive/U256.d.ts +2 -2
  195. package/primitive/U256.js +6 -7
  196. package/primitive/U32.d.ts +2 -2
  197. package/primitive/U32.js +6 -7
  198. package/primitive/U64.d.ts +2 -2
  199. package/primitive/U64.js +6 -7
  200. package/primitive/U8.d.ts +2 -2
  201. package/primitive/U8.js +6 -7
  202. package/primitive/USize.d.ts +2 -2
  203. package/primitive/USize.js +5 -9
  204. package/primitive/index.d.ts +15 -15
  205. package/primitive/index.js +15 -18
  206. package/types/codec.d.ts +3 -3
  207. package/types/helpers.d.ts +1 -1
  208. package/types/index.d.ts +4 -4
  209. package/types/index.js +4 -7
  210. package/types/interfaces.d.ts +2 -2
  211. package/types/registry.d.ts +3 -3
  212. package/utils/compareArray.js +7 -11
  213. package/utils/compareMap.js +18 -19
  214. package/utils/compareSet.js +12 -16
  215. package/utils/decodeU8a.d.ts +1 -1
  216. package/utils/decodeU8a.js +54 -61
  217. package/utils/index.d.ts +9 -9
  218. package/utils/index.js +9 -12
  219. package/utils/mapToTypeMap.d.ts +1 -1
  220. package/utils/mapToTypeMap.js +9 -13
  221. package/utils/sanitize.d.ts +1 -1
  222. package/utils/sanitize.js +182 -185
  223. package/utils/sortValues.d.ts +1 -1
  224. package/utils/sortValues.js +32 -36
  225. package/utils/typeToConstructor.d.ts +1 -1
  226. package/utils/typeToConstructor.js +3 -4
  227. package/utils/typesToMap.d.ts +1 -1
  228. package/utils/typesToMap.js +5 -8
  229. package/utils/util.js +1 -4
package/abstract/Int.js CHANGED
@@ -1,237 +1,226 @@
1
- // Copyright 2017-2023 @polkadot/types-codec authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
1
+ var _AbstractInt_bitLength;
2
+ import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
4
3
  import { BN, BN_BILLION, BN_HUNDRED, BN_MILLION, BN_QUINTILL, bnToBn, bnToHex, bnToU8a, formatBalance, formatNumber, hexToBn, isBigInt, isBn, isFunction, isHex, isNumber, isObject, isString, isU8a, u8aToBn, u8aToNumber } from '@polkadot/util';
5
4
  export const DEFAULT_UINT_BITS = 64;
6
-
7
- // Maximum allowed integer for JS is 2^53 - 1, set limit at 52
8
- // In this case however, we always print any >32 as hex
9
5
  const MAX_NUMBER_BITS = 52;
10
6
  const MUL_P = new BN(10000);
11
- const FORMATTERS = [['Perquintill', BN_QUINTILL], ['Perbill', BN_BILLION], ['Permill', BN_MILLION], ['Percent', BN_HUNDRED]];
7
+ const FORMATTERS = [
8
+ ['Perquintill', BN_QUINTILL],
9
+ ['Perbill', BN_BILLION],
10
+ ['Permill', BN_MILLION],
11
+ ['Percent', BN_HUNDRED]
12
+ ];
12
13
  function isToBn(value) {
13
- return isFunction(value.toBn);
14
+ return isFunction(value.toBn);
14
15
  }
15
16
  function toPercentage(value, divisor) {
16
- return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`;
17
+ return `${(value.mul(MUL_P).div(divisor).toNumber() / 100).toFixed(2)}%`;
17
18
  }
18
-
19
19
  /** @internal */
20
20
  function decodeAbstractInt(value, isNegative) {
21
- if (isNumber(value)) {
22
- if (!Number.isInteger(value) || value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER) {
23
- throw new Error('Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1');
24
- }
25
- return value;
26
- } else if (isString(value)) {
27
- if (isHex(value, -1, true)) {
28
- return hexToBn(value, {
29
- isLe: false,
30
- isNegative
31
- }).toString();
32
- }
33
- if (value.includes('.') || value.includes(',') || value.includes('e')) {
34
- throw new Error('String should not contain decimal points or scientific notation');
35
- }
36
- return value;
37
- } else if (isBn(value) || isBigInt(value)) {
38
- return value.toString();
39
- } else if (isObject(value)) {
40
- if (isToBn(value)) {
41
- return value.toBn().toString();
42
- }
43
-
44
- // Allow the construction from an object with a single top-level key. This means that
45
- // single key objects can be treated equivalently to numbers, assuming they meet the
46
- // specific requirements. (This is useful in Weights 1.5 where Objects are compact)
47
- const keys = Object.keys(value);
48
- if (keys.length !== 1) {
49
- throw new Error('Unable to construct number from multi-key object');
50
- }
51
- return decodeAbstractInt(value[keys[0]], isNegative);
52
- } else if (!value) {
53
- return 0;
54
- }
55
- throw new Error(`Unable to create BN from unknown type ${typeof value}`);
21
+ if (isNumber(value)) {
22
+ if (!Number.isInteger(value) || value > Number.MAX_SAFE_INTEGER || value < Number.MIN_SAFE_INTEGER) {
23
+ throw new Error('Number needs to be an integer <= Number.MAX_SAFE_INTEGER, i.e. 2 ^ 53 - 1');
24
+ }
25
+ return value;
26
+ }
27
+ else if (isString(value)) {
28
+ if (isHex(value, -1, true)) {
29
+ return hexToBn(value, { isLe: false, isNegative }).toString();
30
+ }
31
+ if (value.includes('.') || value.includes(',') || value.includes('e')) {
32
+ throw new Error('String should not contain decimal points or scientific notation');
33
+ }
34
+ return value;
35
+ }
36
+ else if (isBn(value) || isBigInt(value)) {
37
+ return value.toString();
38
+ }
39
+ else if (isObject(value)) {
40
+ if (isToBn(value)) {
41
+ return value.toBn().toString();
42
+ }
43
+ // Allow the construction from an object with a single top-level key. This means that
44
+ // single key objects can be treated equivalently to numbers, assuming they meet the
45
+ // specific requirements. (This is useful in Weights 1.5 where Objects are compact)
46
+ const keys = Object.keys(value);
47
+ if (keys.length !== 1) {
48
+ throw new Error('Unable to construct number from multi-key object');
49
+ }
50
+ return decodeAbstractInt(value[keys[0]], isNegative);
51
+ }
52
+ else if (!value) {
53
+ return 0;
54
+ }
55
+ throw new Error(`Unable to create BN from unknown type ${typeof value}`);
56
56
  }
57
-
58
57
  /**
59
58
  * @name AbstractInt
60
59
  * @ignore
61
60
  * @noInheritDoc
62
61
  */
63
62
  export class AbstractInt extends BN {
64
- #bitLength;
65
- constructor(registry, value = 0, bitLength = DEFAULT_UINT_BITS, isSigned = false) {
66
- // Construct via a string/number, which will be passed in the BN constructor.
67
- // It would be ideal to actually return a BN, but there is an issue:
68
- // https://github.com/indutny/bn.js/issues/206
69
- super(
70
- // shortcut isU8a as used in SCALE decoding
71
- isU8a(value) ? bitLength <= 48 ? u8aToNumber(value.subarray(0, bitLength / 8), {
72
- isNegative: isSigned
73
- }) : u8aToBn(value.subarray(0, bitLength / 8), {
74
- isLe: true,
75
- isNegative: isSigned
76
- }).toString() : decodeAbstractInt(value, isSigned));
77
- this.registry = registry;
78
- this.#bitLength = bitLength;
79
- this.encodedLength = this.#bitLength / 8;
80
- this.initialU8aLength = this.#bitLength / 8;
81
- this.isUnsigned = !isSigned;
82
- const isNegative = this.isNeg();
83
- const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
84
- if (isNegative && !isSigned) {
85
- throw new Error(`${this.toRawType()}: Negative number passed to unsigned type`);
86
- } else if (super.bitLength() > maxBits) {
87
- throw new Error(`${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${maxBits}`);
88
- }
89
- }
90
-
91
- /**
92
- * @description returns a hash of the contents
93
- */
94
- get hash() {
95
- return this.registry.hash(this.toU8a());
96
- }
97
-
98
- /**
99
- * @description Checks if the value is a zero value (align elsewhere)
100
- */
101
- get isEmpty() {
102
- return this.isZero();
103
- }
104
-
105
- /**
106
- * @description Returns the number of bits in the value
107
- */
108
- bitLength() {
109
- return this.#bitLength;
110
- }
111
-
112
- /**
113
- * @description Compares the value of the input to see if there is a match
114
- */
115
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
116
- eq(other) {
117
- // Here we are actually overriding the built-in .eq to take care of both
118
- // number and BN inputs (no `.eqn` needed) - numbers will be converted
119
- return super.eq(isHex(other) ? hexToBn(other.toString(), {
120
- isLe: false,
121
- isNegative: !this.isUnsigned
122
- }) : bnToBn(other));
123
- }
124
-
125
- /**
126
- * @description Returns a breakdown of the hex encoding for this Codec
127
- */
128
- inspect() {
129
- return {
130
- outer: [this.toU8a()]
131
- };
132
- }
133
-
134
- /**
135
- * @description True if this value is the max of the type
136
- */
137
- isMax() {
138
- const u8a = this.toU8a().filter(b => b === 0xff);
139
- return u8a.length === this.#bitLength / 8;
140
- }
141
-
142
- /**
143
- * @description Returns a BigInt representation of the number
144
- */
145
- toBigInt() {
146
- return BigInt(this.toString());
147
- }
148
-
149
- /**
150
- * @description Returns the BN representation of the number. (Compatibility)
151
- */
152
- toBn() {
153
- return this;
154
- }
155
-
156
- /**
157
- * @description Returns a hex string representation of the value
158
- */
159
- toHex(isLe = false) {
160
- // For display/JSON, this is BE, for compare, use isLe
161
- return bnToHex(this, {
162
- bitLength: this.bitLength(),
163
- isLe,
164
- isNegative: !this.isUnsigned
165
- });
166
- }
167
-
168
- /**
169
- * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
170
- */
171
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
172
- toHuman(isExpanded) {
173
- const rawType = this.toRawType();
174
- if (rawType === 'Balance') {
175
- return this.isMax() ? 'everything'
176
- // FIXME In the case of multiples we need some way of detecting which instance this belongs
177
- // to. as it stands we will always format (incorrectly) against the first token defined
178
- : formatBalance(this, {
179
- decimals: this.registry.chainDecimals[0],
180
- withSi: true,
181
- withUnit: this.registry.chainTokens[0]
182
- });
183
- }
184
- const [, divisor] = FORMATTERS.find(([type]) => type === rawType) || [];
185
- return divisor ? toPercentage(this, divisor) : formatNumber(this);
186
- }
187
-
188
- /**
189
- * @description Converts the Object to JSON, typically used for RPC transfers
190
- */
191
- toJSON(onlyHex = false) {
192
- // FIXME this return type should by string | number, however BN returns string
193
- // Options here are
194
- // - super.bitLength() - the actual used bits
195
- // - this.#bitLength - the type bits (this should be used, however contracts RPC is problematic)
196
- return onlyHex || super.bitLength() > MAX_NUMBER_BITS ? this.toHex() : this.toNumber();
197
- }
198
-
199
- /**
200
- * @description Returns the value in a primitive form, either number when <= 52 bits, or string otherwise
201
- */
202
- toPrimitive() {
203
- return super.bitLength() > MAX_NUMBER_BITS ? this.toString() : this.toNumber();
204
- }
205
-
206
- /**
207
- * @description Returns the base runtime type name for this instance
208
- */
209
- toRawType() {
210
- // NOTE In the case of balances, which have a special meaning on the UI
211
- // and can be interpreted differently, return a specific value for it so
212
- // underlying it always matches (no matter which length it actually is)
213
- return this instanceof this.registry.createClassUnsafe('Balance') ? 'Balance' : `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
214
- }
215
-
216
- /**
217
- * @description Returns the string representation of the value
218
- * @param base The base to use for the conversion
219
- */
220
- toString(base) {
221
- // only included here since we do not inherit docs
222
- return super.toString(base);
223
- }
224
-
225
- /**
226
- * @description Encodes the value as a Uint8Array as per the SCALE specifications
227
- * @param isBare true when the value has none of the type-specific prefixes (internal)
228
- */
229
- // eslint-disable-next-line @typescript-eslint/no-unused-vars
230
- toU8a(isBare) {
231
- return bnToU8a(this, {
232
- bitLength: this.bitLength(),
233
- isLe: true,
234
- isNegative: !this.isUnsigned
235
- });
236
- }
237
- }
63
+ constructor(registry, value = 0, bitLength = DEFAULT_UINT_BITS, isSigned = false) {
64
+ // Construct via a string/number, which will be passed in the BN constructor.
65
+ // It would be ideal to actually return a BN, but there is an issue:
66
+ // https://github.com/indutny/bn.js/issues/206
67
+ super(
68
+ // shortcut isU8a as used in SCALE decoding
69
+ isU8a(value)
70
+ ? bitLength <= 48
71
+ ? u8aToNumber(value.subarray(0, bitLength / 8), { isNegative: isSigned })
72
+ : u8aToBn(value.subarray(0, bitLength / 8), { isLe: true, isNegative: isSigned }).toString()
73
+ : decodeAbstractInt(value, isSigned));
74
+ _AbstractInt_bitLength.set(this, void 0);
75
+ this.registry = registry;
76
+ __classPrivateFieldSet(this, _AbstractInt_bitLength, bitLength, "f");
77
+ this.encodedLength = __classPrivateFieldGet(this, _AbstractInt_bitLength, "f") / 8;
78
+ this.initialU8aLength = __classPrivateFieldGet(this, _AbstractInt_bitLength, "f") / 8;
79
+ this.isUnsigned = !isSigned;
80
+ const isNegative = this.isNeg();
81
+ const maxBits = bitLength - (isSigned && !isNegative ? 1 : 0);
82
+ if (isNegative && !isSigned) {
83
+ throw new Error(`${this.toRawType()}: Negative number passed to unsigned type`);
84
+ }
85
+ else if (super.bitLength() > maxBits) {
86
+ throw new Error(`${this.toRawType()}: Input too large. Found input with ${super.bitLength()} bits, expected ${maxBits}`);
87
+ }
88
+ }
89
+ /**
90
+ * @description returns a hash of the contents
91
+ */
92
+ get hash() {
93
+ return this.registry.hash(this.toU8a());
94
+ }
95
+ /**
96
+ * @description Checks if the value is a zero value (align elsewhere)
97
+ */
98
+ get isEmpty() {
99
+ return this.isZero();
100
+ }
101
+ /**
102
+ * @description Returns the number of bits in the value
103
+ */
104
+ bitLength() {
105
+ return __classPrivateFieldGet(this, _AbstractInt_bitLength, "f");
106
+ }
107
+ /**
108
+ * @description Compares the value of the input to see if there is a match
109
+ */
110
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
111
+ eq(other) {
112
+ // Here we are actually overriding the built-in .eq to take care of both
113
+ // number and BN inputs (no `.eqn` needed) - numbers will be converted
114
+ return super.eq(isHex(other)
115
+ ? hexToBn(other.toString(), { isLe: false, isNegative: !this.isUnsigned })
116
+ : bnToBn(other));
117
+ }
118
+ /**
119
+ * @description Returns a breakdown of the hex encoding for this Codec
120
+ */
121
+ inspect() {
122
+ return {
123
+ outer: [this.toU8a()]
124
+ };
125
+ }
126
+ /**
127
+ * @description True if this value is the max of the type
128
+ */
129
+ isMax() {
130
+ const u8a = this.toU8a().filter((b) => b === 0xff);
131
+ return u8a.length === (__classPrivateFieldGet(this, _AbstractInt_bitLength, "f") / 8);
132
+ }
133
+ /**
134
+ * @description Returns a BigInt representation of the number
135
+ */
136
+ toBigInt() {
137
+ return BigInt(this.toString());
138
+ }
139
+ /**
140
+ * @description Returns the BN representation of the number. (Compatibility)
141
+ */
142
+ toBn() {
143
+ return this;
144
+ }
145
+ /**
146
+ * @description Returns a hex string representation of the value
147
+ */
148
+ toHex(isLe = false) {
149
+ // For display/JSON, this is BE, for compare, use isLe
150
+ return bnToHex(this, {
151
+ bitLength: this.bitLength(),
152
+ isLe,
153
+ isNegative: !this.isUnsigned
154
+ });
155
+ }
156
+ /**
157
+ * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
158
+ */
159
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
160
+ toHuman(isExpanded) {
161
+ const rawType = this.toRawType();
162
+ if (rawType === 'Balance') {
163
+ return this.isMax()
164
+ ? 'everything'
165
+ // FIXME In the case of multiples we need some way of detecting which instance this belongs
166
+ // to. as it stands we will always format (incorrectly) against the first token defined
167
+ : formatBalance(this, { decimals: this.registry.chainDecimals[0], withSi: true, withUnit: this.registry.chainTokens[0] });
168
+ }
169
+ const [, divisor] = FORMATTERS.find(([type]) => type === rawType) || [];
170
+ return divisor
171
+ ? toPercentage(this, divisor)
172
+ : formatNumber(this);
173
+ }
174
+ /**
175
+ * @description Converts the Object to JSON, typically used for RPC transfers
176
+ */
177
+ toJSON(onlyHex = false) {
178
+ // FIXME this return type should by string | number, however BN returns string
179
+ // Options here are
180
+ // - super.bitLength() - the actual used bits
181
+ // - this.#bitLength - the type bits (this should be used, however contracts RPC is problematic)
182
+ return onlyHex || (super.bitLength() > MAX_NUMBER_BITS)
183
+ ? this.toHex()
184
+ : this.toNumber();
185
+ }
186
+ /**
187
+ * @description Returns the value in a primitive form, either number when <= 52 bits, or string otherwise
188
+ */
189
+ toPrimitive() {
190
+ return super.bitLength() > MAX_NUMBER_BITS
191
+ ? this.toString()
192
+ : this.toNumber();
193
+ }
194
+ /**
195
+ * @description Returns the base runtime type name for this instance
196
+ */
197
+ toRawType() {
198
+ // NOTE In the case of balances, which have a special meaning on the UI
199
+ // and can be interpreted differently, return a specific value for it so
200
+ // underlying it always matches (no matter which length it actually is)
201
+ return this instanceof this.registry.createClassUnsafe('Balance')
202
+ ? 'Balance'
203
+ : `${this.isUnsigned ? 'u' : 'i'}${this.bitLength()}`;
204
+ }
205
+ /**
206
+ * @description Returns the string representation of the value
207
+ * @param base The base to use for the conversion
208
+ */
209
+ toString(base) {
210
+ // only included here since we do not inherit docs
211
+ return super.toString(base);
212
+ }
213
+ /**
214
+ * @description Encodes the value as a Uint8Array as per the SCALE specifications
215
+ * @param isBare true when the value has none of the type-specific prefixes (internal)
216
+ */
217
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
218
+ toU8a(isBare) {
219
+ return bnToU8a(this, {
220
+ bitLength: this.bitLength(),
221
+ isLe: true,
222
+ isNegative: !this.isUnsigned
223
+ });
224
+ }
225
+ }
226
+ _AbstractInt_bitLength = new WeakMap();
@@ -1,5 +1,5 @@
1
1
  import type { HexString } from '@polkadot/util/types';
2
- import type { AnyJson, BareOpts, CodecObject, Inspect, IU8a, Registry, ToString } from '../types';
2
+ import type { AnyJson, BareOpts, CodecObject, Inspect, IU8a, Registry, ToString } from '../types/index.js';
3
3
  /**
4
4
  * @name Object
5
5
  * @description A type extends the Base class, when it holds a value
@@ -1,51 +1,35 @@
1
- // Copyright 2017-2023 @polkadot/types-codec authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
1
  /**
5
2
  * @name Object
6
3
  * @description A type extends the Base class, when it holds a value
7
4
  */
8
5
  export class AbstractObject {
9
- constructor(registry, value, initialU8aLength) {
10
- this.$ = value;
11
- this.initialU8aLength = initialU8aLength;
12
- this.registry = registry;
13
- }
14
-
15
- /**
16
- * @description The length of the value when encoded as a Uint8Array
17
- */
18
- get encodedLength() {
19
- return this.toU8a().length;
20
- }
21
-
22
- /**
23
- * @description returns a hash of the contents
24
- */
25
- get hash() {
26
- return this.registry.hash(this.toU8a());
27
- }
28
-
29
- /**
30
- * @description Checks if the value is an empty value
31
- */
32
-
33
- /**
34
- * @description Returns the string representation of the value
35
- */
36
- toString() {
37
- return this.$.toString();
38
- }
39
-
40
- /**
41
- * @description Encodes the value as a Uint8Array as per the SCALE specifications
42
- * @param isBare true when the value has none of the type-specific prefixes (internal)
43
- */
44
-
45
- /**
46
- * @description Return the internal value (JS-aligned, same result as $)
47
- */
48
- valueOf() {
49
- return this.$;
50
- }
6
+ constructor(registry, value, initialU8aLength) {
7
+ this.$ = value;
8
+ this.initialU8aLength = initialU8aLength;
9
+ this.registry = registry;
10
+ }
11
+ /**
12
+ * @description The length of the value when encoded as a Uint8Array
13
+ */
14
+ get encodedLength() {
15
+ return this.toU8a().length;
16
+ }
17
+ /**
18
+ * @description returns a hash of the contents
19
+ */
20
+ get hash() {
21
+ return this.registry.hash(this.toU8a());
22
+ }
23
+ /**
24
+ * @description Returns the string representation of the value
25
+ */
26
+ toString() {
27
+ return this.$.toString();
28
+ }
29
+ /**
30
+ * @description Return the internal value (JS-aligned, same result as $)
31
+ */
32
+ valueOf() {
33
+ return this.$;
34
+ }
51
35
  }
@@ -1,3 +1,3 @@
1
- export { AbstractArray } from './Array';
2
- export { AbstractBase } from './Base';
3
- export { AbstractInt } from './Int';
1
+ export { AbstractArray } from './Array.js';
2
+ export { AbstractBase } from './Base.js';
3
+ export { AbstractInt } from './Int.js';
package/abstract/index.js CHANGED
@@ -1,6 +1,3 @@
1
- // Copyright 2017-2023 @polkadot/types-codec authors & contributors
2
- // SPDX-License-Identifier: Apache-2.0
3
-
4
- export { AbstractArray } from "./Array.js";
5
- export { AbstractBase } from "./Base.js";
6
- export { AbstractInt } from "./Int.js";
1
+ export { AbstractArray } from './Array.js';
2
+ export { AbstractBase } from './Base.js';
3
+ export { AbstractInt } from './Int.js';
package/base/Compact.d.ts CHANGED
@@ -1,7 +1,7 @@
1
1
  /// <reference types="bn.js" />
2
2
  import type { BN } from '@polkadot/util';
3
3
  import type { HexString } from '@polkadot/util/types';
4
- import type { AnyJson, AnyNumber, CodecClass, ICompact, Inspect, INumber, IU8a, Registry } from '../types';
4
+ import type { AnyJson, AnyNumber, CodecClass, ICompact, Inspect, INumber, IU8a, Registry } from '../types/index.js';
5
5
  interface Options<T> {
6
6
  definition?: CodecClass<T>;
7
7
  setDefinition?: (d: CodecClass<T>) => CodecClass<T>;