@polkadot/types-codec 9.5.2 → 9.6.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 (152) hide show
  1. package/abstract/Array.js +15 -44
  2. package/abstract/Base.js +12 -28
  3. package/abstract/BigInt.js +3 -2
  4. package/abstract/Int.js +25 -52
  5. package/abstract/index.js +1 -0
  6. package/base/Compact.js +21 -45
  7. package/base/DoNotConstruct.js +3 -19
  8. package/base/Enum.js +37 -88
  9. package/base/Int.js +2 -5
  10. package/base/Null.js +12 -23
  11. package/base/Option.js +23 -59
  12. package/base/Result.js +8 -19
  13. package/base/Tuple.js +12 -29
  14. package/base/UInt.js +2 -4
  15. package/base/Vec.js +7 -25
  16. package/base/VecAny.js +2 -2
  17. package/base/VecFixed.js +8 -20
  18. package/base/index.js +1 -0
  19. package/bundle.js +1 -0
  20. package/cjs/abstract/Array.js +13 -47
  21. package/cjs/abstract/Base.js +12 -30
  22. package/cjs/abstract/BigInt.js +1 -4
  23. package/cjs/abstract/Int.js +25 -55
  24. package/cjs/abstract/index.js +0 -3
  25. package/cjs/base/Compact.js +21 -48
  26. package/cjs/base/DoNotConstruct.js +3 -21
  27. package/cjs/base/Enum.js +34 -92
  28. package/cjs/base/Int.js +0 -7
  29. package/cjs/base/Null.js +10 -25
  30. package/cjs/base/Option.js +23 -63
  31. package/cjs/base/Result.js +6 -21
  32. package/cjs/base/Tuple.js +12 -33
  33. package/cjs/base/UInt.js +0 -6
  34. package/cjs/base/Vec.js +7 -31
  35. package/cjs/base/VecAny.js +0 -4
  36. package/cjs/base/VecFixed.js +8 -25
  37. package/cjs/base/index.js +0 -12
  38. package/cjs/bundle.js +0 -13
  39. package/cjs/detectPackage.js +2 -4
  40. package/cjs/extended/BTreeMap.js +1 -5
  41. package/cjs/extended/BTreeSet.js +17 -57
  42. package/cjs/extended/BitVec.js +11 -25
  43. package/cjs/extended/Bytes.js +9 -20
  44. package/cjs/extended/HashMap.js +1 -5
  45. package/cjs/extended/Linkage.js +5 -20
  46. package/cjs/extended/Map.js +16 -62
  47. package/cjs/extended/OptionBool.js +8 -21
  48. package/cjs/extended/Range.js +3 -13
  49. package/cjs/extended/RangeInclusive.js +1 -6
  50. package/cjs/extended/Type.js +1 -7
  51. package/cjs/extended/U8aFixed.js +2 -15
  52. package/cjs/extended/WrapperKeepOpaque.js +8 -28
  53. package/cjs/extended/WrapperOpaque.js +2 -8
  54. package/cjs/extended/index.js +0 -14
  55. package/cjs/index.js +0 -3
  56. package/cjs/native/Bool.js +14 -32
  57. package/cjs/native/Date.js +18 -40
  58. package/cjs/native/Float.js +11 -29
  59. package/cjs/native/Json.js +14 -33
  60. package/cjs/native/Raw.js +18 -41
  61. package/cjs/native/Set.js +21 -67
  62. package/cjs/native/Struct.data.js +1 -0
  63. package/cjs/native/Struct.js +33 -83
  64. package/cjs/native/Text.js +20 -44
  65. package/cjs/native/index.js +0 -8
  66. package/cjs/packageInfo.js +3 -1
  67. package/cjs/primitive/F32.js +0 -3
  68. package/cjs/primitive/F64.js +0 -3
  69. package/cjs/primitive/I128.js +0 -3
  70. package/cjs/primitive/I16.js +0 -3
  71. package/cjs/primitive/I256.js +0 -3
  72. package/cjs/primitive/I32.js +0 -3
  73. package/cjs/primitive/I64.js +0 -3
  74. package/cjs/primitive/I8.js +0 -3
  75. package/cjs/primitive/U128.js +0 -3
  76. package/cjs/primitive/U16.js +0 -3
  77. package/cjs/primitive/U256.js +0 -3
  78. package/cjs/primitive/U32.js +0 -3
  79. package/cjs/primitive/U64.js +0 -3
  80. package/cjs/primitive/U8.js +0 -3
  81. package/cjs/primitive/USize.js +0 -4
  82. package/cjs/primitive/index.js +0 -15
  83. package/cjs/types/index.js +0 -8
  84. package/cjs/utils/compareArray.js +1 -4
  85. package/cjs/utils/compareMap.js +4 -9
  86. package/cjs/utils/compareSet.js +4 -6
  87. package/cjs/utils/decodeU8a.js +10 -18
  88. package/cjs/utils/index.js +0 -9
  89. package/cjs/utils/mapToTypeMap.js +0 -4
  90. package/cjs/utils/sanitize.js +66 -71
  91. package/cjs/utils/sortValues.js +5 -18
  92. package/cjs/utils/typeToConstructor.js +1 -2
  93. package/cjs/utils/typesToMap.js +1 -3
  94. package/cjs/utils/util.js +1 -2
  95. package/detectOther.js +2 -0
  96. package/detectPackage.js +2 -0
  97. package/extended/BTreeMap.js +1 -2
  98. package/extended/BTreeSet.js +17 -53
  99. package/extended/BitVec.js +13 -22
  100. package/extended/Bytes.js +11 -17
  101. package/extended/HashMap.js +1 -2
  102. package/extended/Linkage.js +5 -12
  103. package/extended/Map.js +16 -54
  104. package/extended/OptionBool.js +8 -17
  105. package/extended/Range.js +4 -11
  106. package/extended/RangeInclusive.js +1 -3
  107. package/extended/Type.js +3 -4
  108. package/extended/U8aFixed.js +4 -12
  109. package/extended/WrapperKeepOpaque.js +8 -23
  110. package/extended/WrapperOpaque.js +2 -5
  111. package/extended/index.js +1 -0
  112. package/index.js +1 -0
  113. package/native/Bool.js +16 -30
  114. package/native/Date.js +18 -37
  115. package/native/Float.js +12 -27
  116. package/native/Json.js +16 -30
  117. package/native/Raw.js +20 -39
  118. package/native/Set.js +21 -63
  119. package/native/Struct.data.js +1 -0
  120. package/native/Struct.js +33 -80
  121. package/native/Text.js +20 -40
  122. package/native/index.js +1 -0
  123. package/package.json +2 -2
  124. package/packageInfo.js +3 -1
  125. package/primitive/F32.js +2 -1
  126. package/primitive/F64.js +2 -1
  127. package/primitive/I128.js +2 -1
  128. package/primitive/I16.js +2 -1
  129. package/primitive/I256.js +2 -1
  130. package/primitive/I32.js +2 -1
  131. package/primitive/I64.js +2 -1
  132. package/primitive/I8.js +2 -1
  133. package/primitive/U128.js +2 -1
  134. package/primitive/U16.js +2 -1
  135. package/primitive/U256.js +2 -1
  136. package/primitive/U32.js +2 -1
  137. package/primitive/U64.js +2 -1
  138. package/primitive/U8.js +2 -1
  139. package/primitive/USize.js +2 -2
  140. package/primitive/index.js +1 -0
  141. package/types/index.js +1 -0
  142. package/utils/compareArray.js +4 -3
  143. package/utils/compareMap.js +4 -7
  144. package/utils/compareSet.js +4 -5
  145. package/utils/decodeU8a.js +12 -15
  146. package/utils/index.js +1 -0
  147. package/utils/mapToTypeMap.js +2 -3
  148. package/utils/sanitize.js +66 -60
  149. package/utils/sortValues.js +6 -15
  150. package/utils/typeToConstructor.js +1 -0
  151. package/utils/typesToMap.js +1 -2
  152. package/utils/util.js +1 -0
package/native/Raw.js CHANGED
@@ -1,6 +1,8 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@polkadot/util';
5
+
4
6
  /**
5
7
  * @name Raw
6
8
  * @description
@@ -10,7 +12,6 @@ import { isAscii, isUndefined, isUtf8, u8aToHex, u8aToString, u8aToU8a } from '@
10
12
  * actual lengths instead of used directly.
11
13
  * @noInheritDoc
12
14
  */
13
-
14
15
  export class Raw extends Uint8Array {
15
16
  /**
16
17
  * @description This ensures that operators such as clice, filter, map, etc. return
@@ -19,159 +20,139 @@ export class Raw extends Uint8Array {
19
20
  static get [Symbol.species]() {
20
21
  return Uint8Array;
21
22
  }
22
-
23
23
  constructor(registry, value, initialU8aLength) {
24
24
  super(u8aToU8a(value));
25
25
  this.registry = registry;
26
26
  this.initialU8aLength = initialU8aLength;
27
27
  }
28
+
28
29
  /**
29
30
  * @description The length of the value when encoded as a Uint8Array
30
31
  */
31
-
32
-
33
32
  get encodedLength() {
34
33
  return this.length;
35
34
  }
35
+
36
36
  /**
37
37
  * @description returns a hash of the contents
38
38
  */
39
-
40
-
41
39
  get hash() {
42
40
  return this.registry.hash(this.toU8a());
43
41
  }
42
+
44
43
  /**
45
44
  * @description Returns true if the wrapped value contains only ASCII printable characters
46
45
  */
47
-
48
-
49
46
  get isAscii() {
50
47
  return isAscii(this);
51
48
  }
49
+
52
50
  /**
53
51
  * @description Returns true if the type wraps an empty/default all-0 value
54
52
  */
55
-
56
-
57
53
  get isEmpty() {
58
54
  return !this.length || isUndefined(this.find(b => !!b));
59
55
  }
56
+
60
57
  /**
61
58
  * @description Returns true if the wrapped value contains only utf8 characters
62
59
  */
63
-
64
-
65
60
  get isUtf8() {
66
61
  return isUtf8(this);
67
62
  }
63
+
68
64
  /**
69
65
  * @description Returns the number of bits in the value
70
66
  */
71
-
72
-
73
67
  bitLength() {
74
68
  return this.length * 8;
75
69
  }
70
+
76
71
  /**
77
72
  * @description Compares the value of the input to see if there is a match
78
73
  */
79
-
80
-
81
74
  eq(other) {
82
75
  if (other instanceof Uint8Array) {
83
76
  return this.length === other.length && !this.some((b, index) => b !== other[index]);
84
77
  }
85
-
86
78
  return this.eq(u8aToU8a(other));
87
79
  }
80
+
88
81
  /**
89
82
  * @description Returns a breakdown of the hex encoding for this Codec
90
83
  */
91
-
92
-
93
84
  inspect() {
94
85
  return {
95
86
  outer: [this.toU8a()]
96
87
  };
97
88
  }
89
+
98
90
  /**
99
91
  * @description Returns a hex string representation of the value
100
92
  */
101
-
102
-
103
93
  toHex() {
104
94
  return u8aToHex(this);
105
95
  }
96
+
106
97
  /**
107
98
  * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
108
99
  */
109
-
110
-
111
100
  toHuman() {
112
101
  return this.toPrimitive();
113
102
  }
103
+
114
104
  /**
115
105
  * @description Converts the Object to JSON, typically used for RPC transfers
116
106
  */
117
-
118
-
119
107
  toJSON() {
120
108
  return this.toHex();
121
109
  }
110
+
122
111
  /**
123
112
  * @description Converts the value in a best-fit primitive form
124
113
  */
125
-
126
-
127
114
  toPrimitive() {
128
115
  if (this.isAscii) {
129
- const text = this.toUtf8(); // ensure we didn't end up with multibyte codepoints
116
+ const text = this.toUtf8();
130
117
 
118
+ // ensure we didn't end up with multibyte codepoints
131
119
  if (isAscii(text)) {
132
120
  return text;
133
121
  }
134
122
  }
135
-
136
123
  return this.toJSON();
137
124
  }
125
+
138
126
  /**
139
127
  * @description Returns the base runtime type name for this instance
140
128
  */
141
-
142
-
143
129
  toRawType() {
144
130
  return 'Raw';
145
131
  }
132
+
146
133
  /**
147
134
  * @description Returns the string representation of the value
148
135
  */
149
-
150
-
151
136
  toString() {
152
137
  return this.toHex();
153
138
  }
139
+
154
140
  /**
155
141
  * @description Encodes the value as a Uint8Array as per the SCALE specifications
156
142
  * @param isBare true when the value has none of the type-specific prefixes (internal)
157
143
  */
158
144
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
159
-
160
-
161
145
  toU8a(isBare) {
162
146
  return Uint8Array.from(this);
163
147
  }
148
+
164
149
  /**
165
150
  * @description Returns the wrapped data as a UTF-8 string
166
151
  */
167
-
168
-
169
152
  toUtf8() {
170
153
  if (!this.isUtf8) {
171
154
  throw new Error('The character sequence is not a valid Utf8 string');
172
155
  }
173
-
174
156
  return u8aToString(this);
175
157
  }
176
-
177
158
  }
package/native/Set.js CHANGED
@@ -1,69 +1,53 @@
1
1
  // Copyright 2017-2022 @polkadot/types-codec authors & contributors
2
2
  // SPDX-License-Identifier: Apache-2.0
3
+
3
4
  import { BN, bnToBn, bnToU8a, isBn, isNumber, isString, isU8a, isUndefined, objectProperties, stringify, stringPascalCase, u8aToBn, u8aToHex, u8aToU8a } from '@polkadot/util';
4
5
  import { compareArray } from "../utils/index.js";
5
-
6
6
  function encodeSet(setValues, values) {
7
7
  const encoded = new BN(0);
8
-
9
8
  for (let i = 0; i < values.length; i++) {
10
9
  encoded.ior(bnToBn(setValues[values[i]] || 0));
11
10
  }
12
-
13
11
  return encoded;
14
12
  }
15
- /** @internal */
16
-
17
13
 
14
+ /** @internal */
18
15
  function decodeSetArray(setValues, values) {
19
16
  const result = new Array(values.length);
20
-
21
17
  for (let i = 0; i < values.length; i++) {
22
18
  const key = values[i];
23
-
24
19
  if (isUndefined(setValues[key])) {
25
20
  throw new Error(`Set: Invalid key '${key}' passed to Set, allowed ${Object.keys(setValues).join(', ')}`);
26
21
  }
27
-
28
22
  result[i] = key;
29
23
  }
30
-
31
24
  return result;
32
25
  }
33
- /** @internal */
34
-
35
26
 
27
+ /** @internal */
36
28
  function decodeSetNumber(setValues, _value) {
37
29
  const bn = bnToBn(_value);
38
30
  const keys = Object.keys(setValues);
39
31
  const result = [];
40
-
41
32
  for (let i = 0; i < keys.length; i++) {
42
33
  const key = keys[i];
43
-
44
34
  if (bn.and(bnToBn(setValues[key])).eq(bnToBn(setValues[key]))) {
45
35
  result.push(key);
46
36
  }
47
37
  }
48
-
49
38
  const computed = encodeSet(setValues, result);
50
-
51
39
  if (!bn.eq(computed)) {
52
40
  throw new Error(`Set: Mismatch decoding '${bn.toString()}', computed as '${computed.toString()}' with ${result.join(', ')}`);
53
41
  }
54
-
55
42
  return result;
56
43
  }
57
- /** @internal */
58
-
59
44
 
45
+ /** @internal */
60
46
  function decodeSet(setValues, value = 0, bitLength) {
61
47
  if (bitLength % 8 !== 0) {
62
48
  throw new Error(`Expected valid bitLength, power of 8, found ${bitLength}`);
63
49
  }
64
-
65
50
  const byteLength = bitLength / 8;
66
-
67
51
  if (isU8a(value)) {
68
52
  return value.length === 0 ? [] : decodeSetNumber(setValues, u8aToBn(value.subarray(0, byteLength), {
69
53
  isLe: true
@@ -74,92 +58,78 @@ function decodeSet(setValues, value = 0, bitLength) {
74
58
  const input = Array.isArray(value) ? value : [...value.values()];
75
59
  return decodeSetArray(setValues, input);
76
60
  }
77
-
78
61
  return decodeSetNumber(setValues, value);
79
62
  }
63
+
80
64
  /**
81
65
  * @name Set
82
66
  * @description
83
67
  * An Set is an array of string values, represented an an encoded type by
84
68
  * a bitwise representation of the values.
85
69
  */
86
-
87
-
88
70
  export class CodecSet extends Set {
89
71
  #allowed;
90
72
  #byteLength;
91
-
92
73
  constructor(registry, setValues, value, bitLength = 8) {
93
74
  super(decodeSet(setValues, value, bitLength));
94
75
  this.registry = registry;
95
76
  this.#allowed = setValues;
96
77
  this.#byteLength = bitLength / 8;
97
78
  }
98
-
99
79
  static with(values, bitLength) {
100
80
  var _class;
101
-
102
81
  return _class = class extends CodecSet {
103
82
  constructor(registry, value) {
104
83
  super(registry, values, value, bitLength);
105
84
  }
106
-
107
85
  }, (() => {
108
86
  const keys = Object.keys(values);
109
87
  const isKeys = new Array(keys.length);
110
-
111
88
  for (let i = 0; i < keys.length; i++) {
112
89
  isKeys[i] = `is${stringPascalCase(keys[i])}`;
113
90
  }
114
-
115
91
  objectProperties(_class.prototype, isKeys, (_, i, self) => self.strings.includes(keys[i]));
116
92
  })(), _class;
117
93
  }
94
+
118
95
  /**
119
96
  * @description The length of the value when encoded as a Uint8Array
120
97
  */
121
-
122
-
123
98
  get encodedLength() {
124
99
  return this.#byteLength;
125
100
  }
101
+
126
102
  /**
127
103
  * @description returns a hash of the contents
128
104
  */
129
-
130
-
131
105
  get hash() {
132
106
  return this.registry.hash(this.toU8a());
133
107
  }
108
+
134
109
  /**
135
110
  * @description true is the Set contains no values
136
111
  */
137
-
138
-
139
112
  get isEmpty() {
140
113
  return this.size === 0;
141
114
  }
115
+
142
116
  /**
143
117
  * @description The actual set values as a string[]
144
118
  */
145
-
146
-
147
119
  get strings() {
148
120
  return [...super.values()];
149
121
  }
122
+
150
123
  /**
151
124
  * @description The encoded value for the set members
152
125
  */
153
-
154
-
155
126
  get valueEncoded() {
156
127
  return encodeSet(this.#allowed, this.strings);
157
128
  }
129
+
158
130
  /**
159
131
  * @description adds a value to the Set (extended to allow for validity checking)
160
132
  */
161
-
162
-
163
133
  add = key => {
164
134
  // ^^^ add = () property done to assign this instance's this, otherwise Set.add creates "some" chaos
165
135
  // we have the isUndefined(this._setValues) in here as well, add is used internally
@@ -167,14 +137,13 @@ export class CodecSet extends Set {
167
137
  if (this.#allowed && isUndefined(this.#allowed[key])) {
168
138
  throw new Error(`Set: Invalid key '${key}' on add`);
169
139
  }
170
-
171
140
  super.add(key);
172
141
  return this;
173
142
  };
143
+
174
144
  /**
175
145
  * @description Compares the value of the input to see if there is a match
176
146
  */
177
-
178
147
  eq(other) {
179
148
  if (Array.isArray(other)) {
180
149
  // we don't actually care about the order, sort the values
@@ -184,89 +153,78 @@ export class CodecSet extends Set {
184
153
  } else if (isNumber(other) || isBn(other)) {
185
154
  return this.valueEncoded.eq(bnToBn(other));
186
155
  }
187
-
188
156
  return false;
189
157
  }
158
+
190
159
  /**
191
160
  * @description Returns a breakdown of the hex encoding for this Codec
192
161
  */
193
-
194
-
195
162
  inspect() {
196
163
  return {
197
164
  outer: [this.toU8a()]
198
165
  };
199
166
  }
167
+
200
168
  /**
201
169
  * @description Returns a hex string representation of the value
202
170
  */
203
-
204
-
205
171
  toHex() {
206
172
  return u8aToHex(this.toU8a());
207
173
  }
174
+
208
175
  /**
209
176
  * @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
210
177
  */
211
-
212
-
213
178
  toHuman() {
214
179
  return this.toJSON();
215
180
  }
181
+
216
182
  /**
217
183
  * @description Converts the Object to JSON, typically used for RPC transfers
218
184
  */
219
-
220
-
221
185
  toJSON() {
222
186
  return this.strings;
223
187
  }
188
+
224
189
  /**
225
190
  * @description The encoded value for the set members
226
191
  */
227
-
228
-
229
192
  toNumber() {
230
193
  return this.valueEncoded.toNumber();
231
194
  }
195
+
232
196
  /**
233
197
  * @description Converts the value in a best-fit primitive form
234
198
  */
235
-
236
-
237
199
  toPrimitive() {
238
200
  return this.toJSON();
239
201
  }
202
+
240
203
  /**
241
204
  * @description Returns the base runtime type name for this instance
242
205
  */
243
-
244
-
245
206
  toRawType() {
246
207
  return stringify({
247
208
  _set: this.#allowed
248
209
  });
249
210
  }
211
+
250
212
  /**
251
213
  * @description Returns the string representation of the value
252
214
  */
253
-
254
-
255
215
  toString() {
256
216
  return `[${this.strings.join(', ')}]`;
257
217
  }
218
+
258
219
  /**
259
220
  * @description Encodes the value as a Uint8Array as per the SCALE specifications
260
221
  * @param isBare true when the value has none of the type-specific prefixes (internal)
261
222
  */
262
223
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
263
-
264
-
265
224
  toU8a(isBare) {
266
225
  return bnToU8a(this.valueEncoded, {
267
226
  bitLength: this.#byteLength * 8,
268
227
  isLe: true
269
228
  });
270
229
  }
271
-
272
230
  }