@polkadot/types-codec 10.4.1 → 10.5.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.
- package/abstract/Array.js +21 -14
- package/abstract/Base.js +13 -17
- package/abstract/Int.js +8 -12
- package/base/Compact.js +20 -28
- package/base/DoNotConstruct.js +14 -18
- package/base/Enum.js +42 -53
- package/base/Option.js +17 -25
- package/base/Tuple.js +12 -19
- package/base/Vec.js +10 -17
- package/base/VecFixed.js +7 -14
- package/cjs/abstract/Array.js +21 -14
- package/cjs/abstract/Base.js +13 -17
- package/cjs/abstract/Int.js +8 -12
- package/cjs/base/Compact.js +19 -27
- package/cjs/base/DoNotConstruct.js +14 -18
- package/cjs/base/Enum.js +41 -52
- package/cjs/base/Option.js +16 -24
- package/cjs/base/Tuple.js +10 -17
- package/cjs/base/Vec.js +9 -16
- package/cjs/base/VecFixed.js +6 -13
- package/cjs/extended/BTreeSet.js +2 -6
- package/cjs/extended/BitVec.js +10 -14
- package/cjs/extended/Map.js +5 -11
- package/cjs/extended/Range.js +2 -6
- package/cjs/extended/WrapperKeepOpaque.js +16 -22
- package/cjs/native/Float.js +3 -7
- package/cjs/native/Set.js +15 -18
- package/cjs/native/Struct.js +12 -16
- package/cjs/native/Text.js +3 -6
- package/cjs/native/index.js +2 -2
- package/cjs/packageInfo.js +1 -1
- package/cjs/primitive/index.js +17 -17
- package/cjs/utils/index.js +6 -6
- package/cjs/utils/sanitize.js +16 -16
- package/cjs/utils/sortValues.js +2 -6
- package/cjs/utils/toConstructors.js +41 -0
- package/cjs/utils/typesToMap.js +1 -1
- package/extended/BTreeSet.js +2 -6
- package/extended/BitVec.js +10 -14
- package/extended/Map.js +5 -11
- package/extended/Range.js +2 -6
- package/extended/WrapperKeepOpaque.js +16 -22
- package/native/Float.js +3 -7
- package/native/Set.js +15 -18
- package/native/Struct.js +12 -16
- package/native/Text.js +3 -6
- package/native/index.d.ts +1 -1
- package/native/index.js +1 -1
- package/package.json +8 -13
- package/packageInfo.js +1 -1
- package/primitive/index.d.ts +16 -16
- package/primitive/index.js +16 -16
- package/types/registry.d.ts +1 -0
- package/utils/index.d.ts +2 -3
- package/utils/index.js +2 -3
- package/utils/sanitize.js +16 -16
- package/utils/sortValues.js +1 -5
- package/utils/toConstructors.d.ts +16 -0
- package/utils/toConstructors.js +35 -0
- package/utils/typesToMap.js +1 -1
- package/cjs/utils/mapToTypeMap.js +0 -18
- package/cjs/utils/typeToConstructor.js +0 -10
- package/utils/mapToTypeMap.d.ts +0 -5
- package/utils/mapToTypeMap.js +0 -14
- package/utils/typeToConstructor.d.ts +0 -2
- package/utils/typeToConstructor.js +0 -6
package/base/Enum.js
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
|
-
import { isHex, isNumber, isObject, isString, isU8a, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
1
|
+
import { identity, isHex, isNumber, isObject, isString, isU8a, objectProperties, stringCamelCase, stringify, stringPascalCase, u8aConcatStrict, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
4
2
|
import { mapToTypeMap, typesToMap } from '../utils/index.js';
|
|
5
3
|
import { Null } from './Null.js';
|
|
6
|
-
function noopSetDefinition(d) {
|
|
7
|
-
return d;
|
|
8
|
-
}
|
|
9
4
|
function isRustEnum(def) {
|
|
10
5
|
const defValues = Object.values(def);
|
|
11
6
|
if (defValues.some((v) => isNumber(v))) {
|
|
@@ -21,7 +16,7 @@ function extractDef(registry, _def) {
|
|
|
21
16
|
let isBasic;
|
|
22
17
|
let isIndexed;
|
|
23
18
|
if (Array.isArray(_def)) {
|
|
24
|
-
for (let i = 0
|
|
19
|
+
for (let i = 0, count = _def.length; i < count; i++) {
|
|
25
20
|
def[_def[i]] = { Type: Null, index: i };
|
|
26
21
|
}
|
|
27
22
|
isBasic = true;
|
|
@@ -29,7 +24,7 @@ function extractDef(registry, _def) {
|
|
|
29
24
|
}
|
|
30
25
|
else if (isRustEnum(_def)) {
|
|
31
26
|
const [Types, keys] = mapToTypeMap(registry, _def);
|
|
32
|
-
for (let i = 0
|
|
27
|
+
for (let i = 0, count = keys.length; i < count; i++) {
|
|
33
28
|
def[keys[i]] = { Type: Types[i], index: i };
|
|
34
29
|
}
|
|
35
30
|
isBasic = !Object.values(def).some(({ Type }) => Type !== Null);
|
|
@@ -37,7 +32,7 @@ function extractDef(registry, _def) {
|
|
|
37
32
|
}
|
|
38
33
|
else {
|
|
39
34
|
const entries = Object.entries(_def);
|
|
40
|
-
for (let i = 0
|
|
35
|
+
for (let i = 0, count = entries.length; i < count; i++) {
|
|
41
36
|
const [key, index] = entries[i];
|
|
42
37
|
def[key] = { Type: Null, index };
|
|
43
38
|
}
|
|
@@ -128,27 +123,21 @@ function decodeEnum(registry, def, value, index) {
|
|
|
128
123
|
* an extension to enum where the value type is determined by the actual index.
|
|
129
124
|
*/
|
|
130
125
|
export class Enum {
|
|
131
|
-
constructor(registry, Types, value, index, { definition, setDefinition =
|
|
132
|
-
_Enum_def.set(this, void 0);
|
|
133
|
-
_Enum_entryIndex.set(this, void 0);
|
|
134
|
-
_Enum_indexes.set(this, void 0);
|
|
135
|
-
_Enum_isBasic.set(this, void 0);
|
|
136
|
-
_Enum_isIndexed.set(this, void 0);
|
|
137
|
-
_Enum_raw.set(this, void 0);
|
|
126
|
+
constructor(registry, Types, value, index, { definition, setDefinition = identity } = {}) {
|
|
138
127
|
const { def, isBasic, isIndexed } = definition || setDefinition(extractDef(registry, Types));
|
|
139
128
|
// shortcut isU8a as used in SCALE decoding
|
|
140
129
|
const decoded = isU8a(value) && value.length && !isNumber(index)
|
|
141
130
|
? createFromU8a(registry, def, value[0], value.subarray(1))
|
|
142
131
|
: decodeEnum(registry, def, value, index);
|
|
143
132
|
this.registry = registry;
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
if (
|
|
151
|
-
this.initialU8aLength = 1 +
|
|
133
|
+
this.__internal__def = def;
|
|
134
|
+
this.__internal__isBasic = isBasic;
|
|
135
|
+
this.__internal__isIndexed = isIndexed;
|
|
136
|
+
this.__internal__indexes = Object.values(def).map(({ index }) => index);
|
|
137
|
+
this.__internal__entryIndex = this.__internal__indexes.indexOf(decoded.index);
|
|
138
|
+
this.__internal__raw = decoded.value;
|
|
139
|
+
if (this.__internal__raw.initialU8aLength) {
|
|
140
|
+
this.initialU8aLength = 1 + this.__internal__raw.initialU8aLength;
|
|
152
141
|
}
|
|
153
142
|
}
|
|
154
143
|
static with(Types) {
|
|
@@ -165,9 +154,10 @@ export class Enum {
|
|
|
165
154
|
const keys = Array.isArray(Types)
|
|
166
155
|
? Types
|
|
167
156
|
: Object.keys(Types);
|
|
168
|
-
const
|
|
169
|
-
const
|
|
170
|
-
|
|
157
|
+
const count = keys.length;
|
|
158
|
+
const asKeys = new Array(count);
|
|
159
|
+
const isKeys = new Array(count);
|
|
160
|
+
for (let i = 0; i < count; i++) {
|
|
171
161
|
const name = stringPascalCase(keys[i]);
|
|
172
162
|
asKeys[i] = `as${name}`;
|
|
173
163
|
isKeys[i] = `is${name}`;
|
|
@@ -186,7 +176,7 @@ export class Enum {
|
|
|
186
176
|
* @description The length of the value when encoded as a Uint8Array
|
|
187
177
|
*/
|
|
188
178
|
get encodedLength() {
|
|
189
|
-
return 1 +
|
|
179
|
+
return 1 + this.__internal__raw.encodedLength;
|
|
190
180
|
}
|
|
191
181
|
/**
|
|
192
182
|
* @description returns a hash of the contents
|
|
@@ -198,55 +188,55 @@ export class Enum {
|
|
|
198
188
|
* @description The index of the enum value
|
|
199
189
|
*/
|
|
200
190
|
get index() {
|
|
201
|
-
return
|
|
191
|
+
return this.__internal__indexes[this.__internal__entryIndex];
|
|
202
192
|
}
|
|
203
193
|
/**
|
|
204
194
|
* @description The value of the enum
|
|
205
195
|
*/
|
|
206
196
|
get inner() {
|
|
207
|
-
return
|
|
197
|
+
return this.__internal__raw;
|
|
208
198
|
}
|
|
209
199
|
/**
|
|
210
200
|
* @description true if this is a basic enum (no values)
|
|
211
201
|
*/
|
|
212
202
|
get isBasic() {
|
|
213
|
-
return
|
|
203
|
+
return this.__internal__isBasic;
|
|
214
204
|
}
|
|
215
205
|
/**
|
|
216
206
|
* @description Checks if the value is an empty value
|
|
217
207
|
*/
|
|
218
208
|
get isEmpty() {
|
|
219
|
-
return
|
|
209
|
+
return this.__internal__raw.isEmpty;
|
|
220
210
|
}
|
|
221
211
|
/**
|
|
222
212
|
* @description Checks if the Enum points to a [[Null]] type
|
|
223
213
|
*/
|
|
224
214
|
get isNone() {
|
|
225
|
-
return
|
|
215
|
+
return this.__internal__raw instanceof Null;
|
|
226
216
|
}
|
|
227
217
|
/**
|
|
228
218
|
* @description The available keys for this enum
|
|
229
219
|
*/
|
|
230
220
|
get defIndexes() {
|
|
231
|
-
return
|
|
221
|
+
return this.__internal__indexes;
|
|
232
222
|
}
|
|
233
223
|
/**
|
|
234
224
|
* @description The available keys for this enum
|
|
235
225
|
*/
|
|
236
226
|
get defKeys() {
|
|
237
|
-
return Object.keys(
|
|
227
|
+
return Object.keys(this.__internal__def);
|
|
238
228
|
}
|
|
239
229
|
/**
|
|
240
230
|
* @description The name of the type this enum value represents
|
|
241
231
|
*/
|
|
242
232
|
get type() {
|
|
243
|
-
return this.defKeys[
|
|
233
|
+
return this.defKeys[this.__internal__entryIndex];
|
|
244
234
|
}
|
|
245
235
|
/**
|
|
246
236
|
* @description The value of the enum
|
|
247
237
|
*/
|
|
248
238
|
get value() {
|
|
249
|
-
return
|
|
239
|
+
return this.__internal__raw;
|
|
250
240
|
}
|
|
251
241
|
/**
|
|
252
242
|
* @description Compares the value of the input to see if there is a match
|
|
@@ -259,7 +249,7 @@ export class Enum {
|
|
|
259
249
|
else if (isNumber(other)) {
|
|
260
250
|
return this.toNumber() === other;
|
|
261
251
|
}
|
|
262
|
-
else if (
|
|
252
|
+
else if (this.__internal__isBasic && isString(other)) {
|
|
263
253
|
return this.type === other;
|
|
264
254
|
}
|
|
265
255
|
else if (isHex(other)) {
|
|
@@ -278,10 +268,10 @@ export class Enum {
|
|
|
278
268
|
* @description Returns a breakdown of the hex encoding for this Codec
|
|
279
269
|
*/
|
|
280
270
|
inspect() {
|
|
281
|
-
if (
|
|
271
|
+
if (this.__internal__isBasic) {
|
|
282
272
|
return { outer: [new Uint8Array([this.index])] };
|
|
283
273
|
}
|
|
284
|
-
const { inner, outer = [] } =
|
|
274
|
+
const { inner, outer = [] } = this.__internal__raw.inspect();
|
|
285
275
|
return {
|
|
286
276
|
inner,
|
|
287
277
|
outer: [new Uint8Array([this.index]), ...outer]
|
|
@@ -297,17 +287,17 @@ export class Enum {
|
|
|
297
287
|
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
298
288
|
*/
|
|
299
289
|
toHuman(isExtended) {
|
|
300
|
-
return
|
|
290
|
+
return this.__internal__isBasic || this.isNone
|
|
301
291
|
? this.type
|
|
302
|
-
: { [this.type]:
|
|
292
|
+
: { [this.type]: this.__internal__raw.toHuman(isExtended) };
|
|
303
293
|
}
|
|
304
294
|
/**
|
|
305
295
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
306
296
|
*/
|
|
307
297
|
toJSON() {
|
|
308
|
-
return
|
|
298
|
+
return this.__internal__isBasic
|
|
309
299
|
? this.type
|
|
310
|
-
: { [stringCamelCase(this.type)]:
|
|
300
|
+
: { [stringCamelCase(this.type)]: this.__internal__raw.toJSON() };
|
|
311
301
|
}
|
|
312
302
|
/**
|
|
313
303
|
* @description Returns the number representation for the value
|
|
@@ -319,23 +309,23 @@ export class Enum {
|
|
|
319
309
|
* @description Converts the value in a best-fit primitive form
|
|
320
310
|
*/
|
|
321
311
|
toPrimitive() {
|
|
322
|
-
return
|
|
312
|
+
return this.__internal__isBasic
|
|
323
313
|
? this.type
|
|
324
|
-
: { [stringCamelCase(this.type)]:
|
|
314
|
+
: { [stringCamelCase(this.type)]: this.__internal__raw.toPrimitive() };
|
|
325
315
|
}
|
|
326
316
|
/**
|
|
327
317
|
* @description Returns a raw struct representation of the enum types
|
|
328
318
|
*/
|
|
329
319
|
_toRawStruct() {
|
|
330
|
-
if (
|
|
331
|
-
return
|
|
320
|
+
if (this.__internal__isBasic) {
|
|
321
|
+
return this.__internal__isIndexed
|
|
332
322
|
? this.defKeys.reduce((out, key, index) => {
|
|
333
|
-
out[key] =
|
|
323
|
+
out[key] = this.__internal__indexes[index];
|
|
334
324
|
return out;
|
|
335
325
|
}, {})
|
|
336
326
|
: this.defKeys;
|
|
337
327
|
}
|
|
338
|
-
const entries = Object.entries(
|
|
328
|
+
const entries = Object.entries(this.__internal__def);
|
|
339
329
|
return typesToMap(this.registry, entries.reduce((out, [key, { Type }], i) => {
|
|
340
330
|
out[0][i] = Type;
|
|
341
331
|
out[1][i] = key;
|
|
@@ -362,11 +352,10 @@ export class Enum {
|
|
|
362
352
|
*/
|
|
363
353
|
toU8a(isBare) {
|
|
364
354
|
return isBare
|
|
365
|
-
?
|
|
355
|
+
? this.__internal__raw.toU8a(isBare)
|
|
366
356
|
: u8aConcatStrict([
|
|
367
357
|
new Uint8Array([this.index]),
|
|
368
|
-
|
|
358
|
+
this.__internal__raw.toU8a(isBare)
|
|
369
359
|
]);
|
|
370
360
|
}
|
|
371
361
|
}
|
|
372
|
-
_Enum_def = new WeakMap(), _Enum_entryIndex = new WeakMap(), _Enum_indexes = new WeakMap(), _Enum_isBasic = new WeakMap(), _Enum_isIndexed = new WeakMap(), _Enum_raw = new WeakMap();
|
package/base/Option.js
CHANGED
|
@@ -1,11 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
|
-
import { isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
|
|
1
|
+
import { identity, isCodec, isNull, isU8a, isUndefined, u8aToHex } from '@polkadot/util';
|
|
4
2
|
import { typeToConstructor } from '../utils/index.js';
|
|
5
3
|
import { Null } from './Null.js';
|
|
6
|
-
function noopSetDefinition(d) {
|
|
7
|
-
return d;
|
|
8
|
-
}
|
|
9
4
|
class None extends Null {
|
|
10
5
|
/**
|
|
11
6
|
* @description Returns the base runtime type name for this instance
|
|
@@ -54,9 +49,7 @@ function decodeOption(registry, Type, value) {
|
|
|
54
49
|
* with a value if/as required/found.
|
|
55
50
|
*/
|
|
56
51
|
export class Option {
|
|
57
|
-
constructor(registry, typeName, value, { definition, setDefinition =
|
|
58
|
-
_Option_Type.set(this, void 0);
|
|
59
|
-
_Option_raw.set(this, void 0);
|
|
52
|
+
constructor(registry, typeName, value, { definition, setDefinition = identity } = {}) {
|
|
60
53
|
const Type = definition || setDefinition(typeToConstructor(registry, typeName));
|
|
61
54
|
const decoded = isU8a(value) && value.length && !isCodec(value)
|
|
62
55
|
? value[0] === 0
|
|
@@ -64,8 +57,8 @@ export class Option {
|
|
|
64
57
|
: new Type(registry, value.subarray(1))
|
|
65
58
|
: decodeOption(registry, Type, value);
|
|
66
59
|
this.registry = registry;
|
|
67
|
-
|
|
68
|
-
|
|
60
|
+
this.__internal__Type = Type;
|
|
61
|
+
this.__internal__raw = decoded;
|
|
69
62
|
if (decoded?.initialU8aLength) {
|
|
70
63
|
this.initialU8aLength = 1 + decoded.initialU8aLength;
|
|
71
64
|
}
|
|
@@ -87,7 +80,7 @@ export class Option {
|
|
|
87
80
|
*/
|
|
88
81
|
get encodedLength() {
|
|
89
82
|
// boolean byte (has value, doesn't have) along with wrapped length
|
|
90
|
-
return 1 +
|
|
83
|
+
return 1 + this.__internal__raw.encodedLength;
|
|
91
84
|
}
|
|
92
85
|
/**
|
|
93
86
|
* @description returns a hash of the contents
|
|
@@ -105,7 +98,7 @@ export class Option {
|
|
|
105
98
|
* @description Checks if the Option has no value
|
|
106
99
|
*/
|
|
107
100
|
get isNone() {
|
|
108
|
-
return
|
|
101
|
+
return this.__internal__raw instanceof None;
|
|
109
102
|
}
|
|
110
103
|
/**
|
|
111
104
|
* @description Checks if the Option has a value
|
|
@@ -117,7 +110,7 @@ export class Option {
|
|
|
117
110
|
* @description The actual value for the Option
|
|
118
111
|
*/
|
|
119
112
|
get value() {
|
|
120
|
-
return
|
|
113
|
+
return this.__internal__raw;
|
|
121
114
|
}
|
|
122
115
|
/**
|
|
123
116
|
* @description Compares the value of the input to see if there is a match
|
|
@@ -135,7 +128,7 @@ export class Option {
|
|
|
135
128
|
if (this.isNone) {
|
|
136
129
|
return { outer: [new Uint8Array([0])] };
|
|
137
130
|
}
|
|
138
|
-
const { inner, outer = [] } =
|
|
131
|
+
const { inner, outer = [] } = this.__internal__raw.inspect();
|
|
139
132
|
return {
|
|
140
133
|
inner,
|
|
141
134
|
outer: [new Uint8Array([1]), ...outer]
|
|
@@ -155,7 +148,7 @@ export class Option {
|
|
|
155
148
|
* @description Converts the Object to to a human-friendly JSON, with additional fields, expansion and formatting of information
|
|
156
149
|
*/
|
|
157
150
|
toHuman(isExtended) {
|
|
158
|
-
return
|
|
151
|
+
return this.__internal__raw.toHuman(isExtended);
|
|
159
152
|
}
|
|
160
153
|
/**
|
|
161
154
|
* @description Converts the Object to JSON, typically used for RPC transfers
|
|
@@ -163,7 +156,7 @@ export class Option {
|
|
|
163
156
|
toJSON() {
|
|
164
157
|
return this.isNone
|
|
165
158
|
? null
|
|
166
|
-
:
|
|
159
|
+
: this.__internal__raw.toJSON();
|
|
167
160
|
}
|
|
168
161
|
/**
|
|
169
162
|
* @description Converts the value in a best-fit primitive form
|
|
@@ -171,13 +164,13 @@ export class Option {
|
|
|
171
164
|
toPrimitive() {
|
|
172
165
|
return this.isNone
|
|
173
166
|
? null
|
|
174
|
-
:
|
|
167
|
+
: this.__internal__raw.toPrimitive();
|
|
175
168
|
}
|
|
176
169
|
/**
|
|
177
170
|
* @description Returns the base runtime type name for this instance
|
|
178
171
|
*/
|
|
179
172
|
toRawType(isBare) {
|
|
180
|
-
const wrapped = this.registry.getClassName(
|
|
173
|
+
const wrapped = this.registry.getClassName(this.__internal__Type) || new this.__internal__Type(this.registry).toRawType();
|
|
181
174
|
return isBare
|
|
182
175
|
? wrapped
|
|
183
176
|
: `Option<${wrapped}>`;
|
|
@@ -186,7 +179,7 @@ export class Option {
|
|
|
186
179
|
* @description Returns the string representation of the value
|
|
187
180
|
*/
|
|
188
181
|
toString() {
|
|
189
|
-
return
|
|
182
|
+
return this.__internal__raw.toString();
|
|
190
183
|
}
|
|
191
184
|
/**
|
|
192
185
|
* @description Encodes the value as a Uint8Array as per the SCALE specifications
|
|
@@ -194,12 +187,12 @@ export class Option {
|
|
|
194
187
|
*/
|
|
195
188
|
toU8a(isBare) {
|
|
196
189
|
if (isBare) {
|
|
197
|
-
return
|
|
190
|
+
return this.__internal__raw.toU8a(true);
|
|
198
191
|
}
|
|
199
192
|
const u8a = new Uint8Array(this.encodedLength);
|
|
200
193
|
if (this.isSome) {
|
|
201
194
|
u8a.set([1]);
|
|
202
|
-
u8a.set(
|
|
195
|
+
u8a.set(this.__internal__raw.toU8a(), 1);
|
|
203
196
|
}
|
|
204
197
|
return u8a;
|
|
205
198
|
}
|
|
@@ -210,7 +203,7 @@ export class Option {
|
|
|
210
203
|
if (this.isNone) {
|
|
211
204
|
throw new Error('Option: unwrapping a None value');
|
|
212
205
|
}
|
|
213
|
-
return
|
|
206
|
+
return this.__internal__raw;
|
|
214
207
|
}
|
|
215
208
|
/**
|
|
216
209
|
* @description Returns the value that the Option represents (if available) or defaultValue if none
|
|
@@ -228,7 +221,6 @@ export class Option {
|
|
|
228
221
|
unwrapOrDefault() {
|
|
229
222
|
return this.isSome
|
|
230
223
|
? this.unwrap()
|
|
231
|
-
: new
|
|
224
|
+
: new this.__internal__Type(this.registry);
|
|
232
225
|
}
|
|
233
226
|
}
|
|
234
|
-
_Option_Type = new WeakMap(), _Option_raw = new WeakMap();
|
package/base/Tuple.js
CHANGED
|
@@ -1,16 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
|
-
import { isFunction, isHex, isString, isU8a, stringify, u8aConcatStrict, u8aToU8a } from '@polkadot/util';
|
|
1
|
+
import { identity, isFunction, isHex, isString, isU8a, stringify, u8aConcatStrict, u8aToU8a } from '@polkadot/util';
|
|
4
2
|
import { AbstractArray } from '../abstract/Array.js';
|
|
5
|
-
import { decodeU8a, mapToTypeMap, typeToConstructor } from '../utils/index.js';
|
|
6
|
-
function noopSetDefinition(d) {
|
|
7
|
-
return d;
|
|
8
|
-
}
|
|
3
|
+
import { decodeU8a, mapToTypeMap, typesToConstructors, typeToConstructor } from '../utils/index.js';
|
|
9
4
|
/** @internal */
|
|
10
5
|
function decodeTuple(registry, result, value, Classes) {
|
|
11
6
|
if (Array.isArray(value)) {
|
|
12
7
|
const Types = Classes[0];
|
|
13
|
-
for (let i = 0
|
|
8
|
+
for (let i = 0, count = Types.length; i < count; i++) {
|
|
14
9
|
try {
|
|
15
10
|
const entry = value?.[i];
|
|
16
11
|
result[i] = entry instanceof Types[i]
|
|
@@ -28,7 +23,7 @@ function decodeTuple(registry, result, value, Classes) {
|
|
|
28
23
|
}
|
|
29
24
|
else if (!value || !result.length) {
|
|
30
25
|
const Types = Classes[0];
|
|
31
|
-
for (let i = 0
|
|
26
|
+
for (let i = 0, count = Types.length; i < count; i++) {
|
|
32
27
|
result[i] = new Types[i](registry);
|
|
33
28
|
}
|
|
34
29
|
return [result, 0];
|
|
@@ -42,18 +37,17 @@ function decodeTuple(registry, result, value, Classes) {
|
|
|
42
37
|
* own type. It extends the base JS `Array` object.
|
|
43
38
|
*/
|
|
44
39
|
export class Tuple extends AbstractArray {
|
|
45
|
-
constructor(registry, Types, value, { definition, setDefinition =
|
|
40
|
+
constructor(registry, Types, value, { definition, setDefinition = identity } = {}) {
|
|
46
41
|
const Classes = definition || setDefinition(Array.isArray(Types)
|
|
47
|
-
? [
|
|
42
|
+
? [typesToConstructors(registry, Types), []]
|
|
48
43
|
: isFunction(Types) || isString(Types)
|
|
49
44
|
? [[typeToConstructor(registry, Types)], []]
|
|
50
45
|
: mapToTypeMap(registry, Types));
|
|
51
46
|
super(registry, Classes[0].length);
|
|
52
|
-
_Tuple_Types.set(this, void 0);
|
|
53
47
|
this.initialU8aLength = (isU8a(value)
|
|
54
48
|
? decodeU8a(registry, this, value, Classes)
|
|
55
49
|
: decodeTuple(registry, this, value, Classes))[1];
|
|
56
|
-
|
|
50
|
+
this.__internal__Types = Classes;
|
|
57
51
|
}
|
|
58
52
|
static with(Types) {
|
|
59
53
|
let definition;
|
|
@@ -70,7 +64,7 @@ export class Tuple extends AbstractArray {
|
|
|
70
64
|
*/
|
|
71
65
|
get encodedLength() {
|
|
72
66
|
let total = 0;
|
|
73
|
-
for (let i = 0
|
|
67
|
+
for (let i = 0, count = this.length; i < count; i++) {
|
|
74
68
|
total += this[i].encodedLength;
|
|
75
69
|
}
|
|
76
70
|
return total;
|
|
@@ -79,9 +73,9 @@ export class Tuple extends AbstractArray {
|
|
|
79
73
|
* @description The types definition of the tuple
|
|
80
74
|
*/
|
|
81
75
|
get Types() {
|
|
82
|
-
return
|
|
83
|
-
?
|
|
84
|
-
:
|
|
76
|
+
return this.__internal__Types[1].length
|
|
77
|
+
? this.__internal__Types[1]
|
|
78
|
+
: this.__internal__Types[0].map((T) => new T(this.registry).toRawType());
|
|
85
79
|
}
|
|
86
80
|
/**
|
|
87
81
|
* @description Returns a breakdown of the hex encoding for this Codec
|
|
@@ -95,7 +89,7 @@ export class Tuple extends AbstractArray {
|
|
|
95
89
|
* @description Returns the base runtime type name for this instance
|
|
96
90
|
*/
|
|
97
91
|
toRawType() {
|
|
98
|
-
const types =
|
|
92
|
+
const types = this.__internal__Types[0].map((T) => this.registry.getClassName(T) || new T(this.registry).toRawType());
|
|
99
93
|
return `(${types.join(',')})`;
|
|
100
94
|
}
|
|
101
95
|
/**
|
|
@@ -113,4 +107,3 @@ export class Tuple extends AbstractArray {
|
|
|
113
107
|
return u8aConcatStrict(this.toU8aInner(isBare));
|
|
114
108
|
}
|
|
115
109
|
}
|
|
116
|
-
_Tuple_Types = new WeakMap();
|
package/base/Vec.js
CHANGED
|
@@ -1,13 +1,8 @@
|
|
|
1
|
-
|
|
2
|
-
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
|
-
import { compactFromU8aLim, isHex, isU8a, logger, stringify, u8aToU8a } from '@polkadot/util';
|
|
1
|
+
import { compactFromU8aLim, identity, isHex, isU8a, logger, stringify, u8aToU8a } from '@polkadot/util';
|
|
4
2
|
import { AbstractArray } from '../abstract/Array.js';
|
|
5
3
|
import { decodeU8aVec, typeToConstructor } from '../utils/index.js';
|
|
6
4
|
const MAX_LENGTH = 64 * 1024;
|
|
7
5
|
const l = logger('Vec');
|
|
8
|
-
function noopSetDefinition(d) {
|
|
9
|
-
return d;
|
|
10
|
-
}
|
|
11
6
|
function decodeVecLength(value) {
|
|
12
7
|
if (Array.isArray(value)) {
|
|
13
8
|
return [value, value.length, 0];
|
|
@@ -58,14 +53,13 @@ export function decodeVec(registry, result, value, startAt, Type) {
|
|
|
58
53
|
* specific encoding/decoding on top of the base type.
|
|
59
54
|
*/
|
|
60
55
|
export class Vec extends AbstractArray {
|
|
61
|
-
constructor(registry, Type, value = [], { definition, setDefinition =
|
|
56
|
+
constructor(registry, Type, value = [], { definition, setDefinition = identity } = {}) {
|
|
62
57
|
const [decodeFrom, length, startAt] = decodeVecLength(value);
|
|
63
58
|
super(registry, length);
|
|
64
|
-
|
|
65
|
-
__classPrivateFieldSet(this, _Vec_Type, definition || setDefinition(typeToConstructor(registry, Type)), "f");
|
|
59
|
+
this.__internal__Type = definition || setDefinition(typeToConstructor(registry, Type));
|
|
66
60
|
this.initialU8aLength = (isU8a(decodeFrom)
|
|
67
|
-
? decodeU8aVec(registry, this, decodeFrom, startAt,
|
|
68
|
-
: decodeVec(registry, this, decodeFrom, startAt,
|
|
61
|
+
? decodeU8aVec(registry, this, decodeFrom, startAt, this.__internal__Type)
|
|
62
|
+
: decodeVec(registry, this, decodeFrom, startAt, this.__internal__Type))[0];
|
|
69
63
|
}
|
|
70
64
|
static with(Type) {
|
|
71
65
|
let definition;
|
|
@@ -81,17 +75,17 @@ export class Vec extends AbstractArray {
|
|
|
81
75
|
* @description The type for the items
|
|
82
76
|
*/
|
|
83
77
|
get Type() {
|
|
84
|
-
return
|
|
78
|
+
return this.__internal__Type.name;
|
|
85
79
|
}
|
|
86
80
|
/**
|
|
87
81
|
* @description Finds the index of the value in the array
|
|
88
82
|
*/
|
|
89
83
|
indexOf(other) {
|
|
90
84
|
// convert type first, this removes overhead from the eq
|
|
91
|
-
const check = other instanceof
|
|
85
|
+
const check = other instanceof this.__internal__Type
|
|
92
86
|
? other
|
|
93
|
-
: new
|
|
94
|
-
for (let i = 0
|
|
87
|
+
: new this.__internal__Type(this.registry, other);
|
|
88
|
+
for (let i = 0, count = this.length; i < count; i++) {
|
|
95
89
|
if (check.eq(this[i])) {
|
|
96
90
|
return i;
|
|
97
91
|
}
|
|
@@ -102,7 +96,6 @@ export class Vec extends AbstractArray {
|
|
|
102
96
|
* @description Returns the base runtime type name for this instance
|
|
103
97
|
*/
|
|
104
98
|
toRawType() {
|
|
105
|
-
return `Vec<${this.registry.getClassName(
|
|
99
|
+
return `Vec<${this.registry.getClassName(this.__internal__Type) || new this.__internal__Type(this.registry).toRawType()}>`;
|
|
106
100
|
}
|
|
107
101
|
}
|
|
108
|
-
_Vec_Type = new WeakMap();
|
package/base/VecFixed.js
CHANGED
|
@@ -1,25 +1,19 @@
|
|
|
1
|
-
|
|
2
|
-
import { __classPrivateFieldGet, __classPrivateFieldSet } from "tslib";
|
|
3
|
-
import { isU8a, u8aConcatStrict } from '@polkadot/util';
|
|
1
|
+
import { identity, isU8a, u8aConcatStrict } from '@polkadot/util';
|
|
4
2
|
import { AbstractArray } from '../abstract/Array.js';
|
|
5
3
|
import { decodeU8aVec, typeToConstructor } from '../utils/index.js';
|
|
6
4
|
import { decodeVec } from './Vec.js';
|
|
7
|
-
function noopSetDefinition(d) {
|
|
8
|
-
return d;
|
|
9
|
-
}
|
|
10
5
|
/**
|
|
11
6
|
* @name VecFixed
|
|
12
7
|
* @description
|
|
13
8
|
* This manages codec arrays of a fixed length
|
|
14
9
|
*/
|
|
15
10
|
export class VecFixed extends AbstractArray {
|
|
16
|
-
constructor(registry, Type, length, value = [], { definition, setDefinition =
|
|
11
|
+
constructor(registry, Type, length, value = [], { definition, setDefinition = identity } = {}) {
|
|
17
12
|
super(registry, length);
|
|
18
|
-
|
|
19
|
-
__classPrivateFieldSet(this, _VecFixed_Type, definition || setDefinition(typeToConstructor(registry, Type)), "f");
|
|
13
|
+
this.__internal__Type = definition || setDefinition(typeToConstructor(registry, Type));
|
|
20
14
|
this.initialU8aLength = (isU8a(value)
|
|
21
|
-
? decodeU8aVec(registry, this, value, 0,
|
|
22
|
-
: decodeVec(registry, this, value, 0,
|
|
15
|
+
? decodeU8aVec(registry, this, value, 0, this.__internal__Type)
|
|
16
|
+
: decodeVec(registry, this, value, 0, this.__internal__Type))[1];
|
|
23
17
|
}
|
|
24
18
|
static with(Type, length) {
|
|
25
19
|
let definition;
|
|
@@ -35,14 +29,14 @@ export class VecFixed extends AbstractArray {
|
|
|
35
29
|
* @description The type for the items
|
|
36
30
|
*/
|
|
37
31
|
get Type() {
|
|
38
|
-
return new
|
|
32
|
+
return new this.__internal__Type(this.registry).toRawType();
|
|
39
33
|
}
|
|
40
34
|
/**
|
|
41
35
|
* @description The length of the value when encoded as a Uint8Array
|
|
42
36
|
*/
|
|
43
37
|
get encodedLength() {
|
|
44
38
|
let total = 0;
|
|
45
|
-
for (let i = 0
|
|
39
|
+
for (let i = 0, count = this.length; i < count; i++) {
|
|
46
40
|
total += this[i].encodedLength;
|
|
47
41
|
}
|
|
48
42
|
return total;
|
|
@@ -70,4 +64,3 @@ export class VecFixed extends AbstractArray {
|
|
|
70
64
|
return `[${this.Type};${this.length}]`;
|
|
71
65
|
}
|
|
72
66
|
}
|
|
73
|
-
_VecFixed_Type = new WeakMap();
|