@polkadot/types-create 9.5.1 → 9.6.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/bundle.js +1 -0
- package/cjs/bundle.js +0 -6
- package/cjs/create/class.js +22 -44
- package/cjs/create/index.js +0 -4
- package/cjs/create/type.js +15 -22
- package/cjs/detectOther.js +0 -2
- package/cjs/detectPackage.js +2 -4
- package/cjs/index.js +0 -3
- package/cjs/packageInfo.js +3 -1
- package/cjs/types/index.js +0 -5
- package/cjs/types/types.js +0 -1
- package/cjs/util/encodeTypes.js +7 -31
- package/cjs/util/getTypeDef.js +24 -56
- package/cjs/util/index.js +0 -8
- package/cjs/util/typeSplit.js +13 -17
- package/cjs/util/xcm.js +1 -3
- package/create/class.js +22 -33
- package/create/index.js +1 -0
- package/create/type.js +18 -19
- package/detectOther.js +1 -0
- package/detectPackage.js +2 -0
- package/index.js +1 -0
- package/package.json +4 -4
- package/packageInfo.js +3 -1
- package/types/augmentRegistry.js +1 -0
- package/types/index.js +1 -0
- package/types/types.js +1 -1
- package/util/encodeTypes.js +7 -22
- package/util/getTypeDef.js +24 -51
- package/util/index.js +1 -0
- package/util/typeSplit.js +13 -16
- package/util/xcm.js +1 -0
package/cjs/util/getTypeDef.js
CHANGED
|
@@ -4,45 +4,37 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.getTypeDef = getTypeDef;
|
|
7
|
-
|
|
8
7
|
var _typesCodec = require("@polkadot/types-codec");
|
|
9
|
-
|
|
10
8
|
var _util = require("@polkadot/util");
|
|
11
|
-
|
|
12
9
|
var _types = require("../types");
|
|
13
|
-
|
|
14
10
|
var _typeSplit = require("./typeSplit");
|
|
15
|
-
|
|
16
11
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
17
12
|
// SPDX-License-Identifier: Apache-2.0
|
|
18
|
-
const KNOWN_INTERNALS = ['_alias', '_fallback'];
|
|
19
13
|
|
|
14
|
+
const KNOWN_INTERNALS = ['_alias', '_fallback'];
|
|
20
15
|
function getTypeString(typeOrObj) {
|
|
21
16
|
return (0, _util.isString)(typeOrObj) ? typeOrObj.toString() : JSON.stringify(typeOrObj);
|
|
22
17
|
}
|
|
23
|
-
|
|
24
18
|
function isRustEnum(details) {
|
|
25
19
|
const values = Object.values(details);
|
|
26
|
-
|
|
27
20
|
if (values.some(v => (0, _util.isNumber)(v))) {
|
|
28
21
|
if (!values.every(v => (0, _util.isNumber)(v) && v >= 0 && v <= 255)) {
|
|
29
22
|
throw new Error('Invalid number-indexed enum definition');
|
|
30
23
|
}
|
|
31
|
-
|
|
32
24
|
return false;
|
|
33
25
|
}
|
|
34
|
-
|
|
35
26
|
return true;
|
|
36
|
-
}
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
// decode an enum of either of the following forms
|
|
37
30
|
// { _enum: ['A', 'B', 'C'] }
|
|
38
31
|
// { _enum: { A: AccountId, B: Balance, C: u32 } }
|
|
39
32
|
// { _enum: { A: 1, B: 2 } }
|
|
40
|
-
|
|
41
|
-
|
|
42
33
|
function _decodeEnum(value, details, count, fallbackType) {
|
|
43
34
|
value.info = _types.TypeDefInfo.Enum;
|
|
44
|
-
value.fallbackType = fallbackType;
|
|
35
|
+
value.fallbackType = fallbackType;
|
|
45
36
|
|
|
37
|
+
// not as pretty, but remain compatible with oo7 for both struct and Array types
|
|
46
38
|
if (Array.isArray(details)) {
|
|
47
39
|
value.sub = details.map((name, index) => ({
|
|
48
40
|
index,
|
|
@@ -70,12 +62,11 @@ function _decodeEnum(value, details, count, fallbackType) {
|
|
|
70
62
|
};
|
|
71
63
|
});
|
|
72
64
|
}
|
|
73
|
-
|
|
74
65
|
return value;
|
|
75
|
-
}
|
|
76
|
-
// { _set: { A: 0b0001, B: 0b0010, C: 0b0100 } }
|
|
77
|
-
|
|
66
|
+
}
|
|
78
67
|
|
|
68
|
+
// decode a set of the form
|
|
69
|
+
// { _set: { A: 0b0001, B: 0b0010, C: 0b0100 } }
|
|
79
70
|
function _decodeSet(value, details, fallbackType) {
|
|
80
71
|
value.info = _types.TypeDefInfo.Set;
|
|
81
72
|
value.fallbackType = fallbackType;
|
|
@@ -93,35 +84,32 @@ function _decodeSet(value, details, fallbackType) {
|
|
|
93
84
|
};
|
|
94
85
|
});
|
|
95
86
|
return value;
|
|
96
|
-
}
|
|
97
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
98
|
-
|
|
87
|
+
}
|
|
99
88
|
|
|
89
|
+
// decode a struct, set or enum
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
100
91
|
function _decodeStruct(value, type, _, count) {
|
|
101
92
|
const parsed = JSON.parse(type);
|
|
102
93
|
const keys = Object.keys(parsed);
|
|
103
|
-
|
|
104
94
|
if (keys.includes('_enum')) {
|
|
105
95
|
return _decodeEnum(value, parsed._enum, count, parsed._fallback);
|
|
106
96
|
} else if (keys.includes('_set')) {
|
|
107
97
|
return _decodeSet(value, parsed._set, parsed._fallback);
|
|
108
98
|
}
|
|
109
|
-
|
|
110
99
|
value.alias = parsed._alias ? new Map(Object.entries(parsed._alias)) : undefined;
|
|
111
100
|
value.fallbackType = parsed._fallback;
|
|
112
101
|
value.sub = keys.filter(name => !KNOWN_INTERNALS.includes(name)).map(name => getTypeDef(getTypeString(parsed[name]), {
|
|
113
102
|
name
|
|
114
103
|
}, count));
|
|
115
104
|
return value;
|
|
116
|
-
}
|
|
117
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
118
|
-
|
|
105
|
+
}
|
|
119
106
|
|
|
107
|
+
// decode a fixed vector, e.g. [u8;32]
|
|
108
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
120
109
|
function _decodeFixedVec(value, type, _, count) {
|
|
121
110
|
const max = type.length - 1;
|
|
122
111
|
let index = -1;
|
|
123
112
|
let inner = 0;
|
|
124
|
-
|
|
125
113
|
for (let i = 1; i < max && index === -1; i++) {
|
|
126
114
|
switch (type[i]) {
|
|
127
115
|
case ';':
|
|
@@ -129,16 +117,13 @@ function _decodeFixedVec(value, type, _, count) {
|
|
|
129
117
|
if (inner === 0) {
|
|
130
118
|
index = i;
|
|
131
119
|
}
|
|
132
|
-
|
|
133
120
|
break;
|
|
134
121
|
}
|
|
135
|
-
|
|
136
122
|
case '[':
|
|
137
123
|
case '(':
|
|
138
124
|
case '<':
|
|
139
125
|
inner++;
|
|
140
126
|
break;
|
|
141
|
-
|
|
142
127
|
case ']':
|
|
143
128
|
case ')':
|
|
144
129
|
case '>':
|
|
@@ -146,75 +131,64 @@ function _decodeFixedVec(value, type, _, count) {
|
|
|
146
131
|
break;
|
|
147
132
|
}
|
|
148
133
|
}
|
|
149
|
-
|
|
150
134
|
if (index === -1) {
|
|
151
135
|
throw new Error(`${type}: Unable to extract location of ';'`);
|
|
152
136
|
}
|
|
153
|
-
|
|
154
137
|
const vecType = type.substring(1, index);
|
|
155
138
|
const [strLength, displayName] = type.substring(index + 1, max).split(';');
|
|
156
139
|
const length = parseInt(strLength.trim(), 10);
|
|
157
|
-
|
|
158
140
|
if (length > 2048) {
|
|
159
141
|
throw new Error(`${type}: Only support for [Type; <length>], where length <= 2048`);
|
|
160
142
|
}
|
|
161
|
-
|
|
162
143
|
value.displayName = displayName;
|
|
163
144
|
value.length = length;
|
|
164
145
|
value.sub = getTypeDef(vecType, {}, count);
|
|
165
146
|
return value;
|
|
166
|
-
}
|
|
167
|
-
|
|
147
|
+
}
|
|
168
148
|
|
|
149
|
+
// decode a tuple
|
|
169
150
|
function _decodeTuple(value, _, subType, count) {
|
|
170
151
|
value.sub = subType.length === 0 ? [] : (0, _typeSplit.typeSplit)(subType).map(inner => getTypeDef(inner, {}, count));
|
|
171
152
|
return value;
|
|
172
|
-
}
|
|
173
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
174
|
-
|
|
153
|
+
}
|
|
175
154
|
|
|
155
|
+
// decode a Int/UInt<bitLength[, name]>
|
|
156
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
176
157
|
function _decodeAnyInt(value, type, _, clazz) {
|
|
177
158
|
const [strLength, displayName] = type.substring(clazz.length + 1, type.length - 1).split(',');
|
|
178
159
|
const length = parseInt(strLength.trim(), 10);
|
|
179
|
-
|
|
180
160
|
if (length > 8192 || length % 8) {
|
|
181
161
|
throw new Error(`${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
|
|
182
162
|
}
|
|
183
|
-
|
|
184
163
|
value.displayName = displayName;
|
|
185
164
|
value.length = length;
|
|
186
165
|
return value;
|
|
187
166
|
}
|
|
188
|
-
|
|
189
167
|
function _decodeInt(value, type, subType) {
|
|
190
168
|
return _decodeAnyInt(value, type, subType, 'Int');
|
|
191
169
|
}
|
|
192
|
-
|
|
193
170
|
function _decodeUInt(value, type, subType) {
|
|
194
171
|
return _decodeAnyInt(value, type, subType, 'UInt');
|
|
195
|
-
}
|
|
196
|
-
|
|
172
|
+
}
|
|
197
173
|
|
|
174
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
198
175
|
function _decodeDoNotConstruct(value, type, _) {
|
|
199
176
|
const NAME_LENGTH = 'DoNotConstruct'.length;
|
|
200
177
|
value.displayName = type.substring(NAME_LENGTH + 1, type.length - 1);
|
|
201
178
|
return value;
|
|
202
179
|
}
|
|
203
|
-
|
|
204
180
|
function hasWrapper(type, _ref5) {
|
|
205
181
|
let [start, end] = _ref5;
|
|
206
182
|
return type.substring(0, start.length) === start && type.slice(-1 * end.length) === end;
|
|
207
183
|
}
|
|
208
|
-
|
|
209
|
-
|
|
184
|
+
const nestedExtraction = [['[', ']', _types.TypeDefInfo.VecFixed, _decodeFixedVec], ['{', '}', _types.TypeDefInfo.Struct, _decodeStruct], ['(', ')', _types.TypeDefInfo.Tuple, _decodeTuple],
|
|
185
|
+
// the inner for these are the same as tuple, multiple values
|
|
210
186
|
['BTreeMap<', '>', _types.TypeDefInfo.BTreeMap, _decodeTuple], ['HashMap<', '>', _types.TypeDefInfo.HashMap, _decodeTuple], ['Int<', '>', _types.TypeDefInfo.Int, _decodeInt], ['Result<', '>', _types.TypeDefInfo.Result, _decodeTuple], ['UInt<', '>', _types.TypeDefInfo.UInt, _decodeUInt], ['DoNotConstruct<', '>', _types.TypeDefInfo.DoNotConstruct, _decodeDoNotConstruct]];
|
|
211
187
|
const wrappedExtraction = [['BTreeSet<', '>', _types.TypeDefInfo.BTreeSet], ['Compact<', '>', _types.TypeDefInfo.Compact], ['Linkage<', '>', _types.TypeDefInfo.Linkage], ['Opaque<', '>', _types.TypeDefInfo.WrapperOpaque], ['Option<', '>', _types.TypeDefInfo.Option], ['Range<', '>', _types.TypeDefInfo.Range], ['RangeInclusive<', '>', _types.TypeDefInfo.RangeInclusive], ['Vec<', '>', _types.TypeDefInfo.Vec], ['WrapperKeepOpaque<', '>', _types.TypeDefInfo.WrapperKeepOpaque], ['WrapperOpaque<', '>', _types.TypeDefInfo.WrapperOpaque]];
|
|
212
|
-
|
|
213
188
|
function extractSubType(type, _ref6) {
|
|
214
189
|
let [start, end] = _ref6;
|
|
215
190
|
return type.substring(start.length, type.length - end.length);
|
|
216
191
|
}
|
|
217
|
-
|
|
218
192
|
function getTypeDef(_type) {
|
|
219
193
|
let {
|
|
220
194
|
displayName,
|
|
@@ -229,24 +203,18 @@ function getTypeDef(_type) {
|
|
|
229
203
|
name,
|
|
230
204
|
type
|
|
231
205
|
};
|
|
232
|
-
|
|
233
206
|
if (++count > 64) {
|
|
234
207
|
throw new Error('getTypeDef: Maximum nested limit reached');
|
|
235
208
|
}
|
|
236
|
-
|
|
237
209
|
const nested = nestedExtraction.find(nested => hasWrapper(type, nested));
|
|
238
|
-
|
|
239
210
|
if (nested) {
|
|
240
211
|
value.info = nested[2];
|
|
241
212
|
return nested[3](value, type, extractSubType(type, nested), count);
|
|
242
213
|
}
|
|
243
|
-
|
|
244
214
|
const wrapped = wrappedExtraction.find(wrapped => hasWrapper(type, wrapped));
|
|
245
|
-
|
|
246
215
|
if (wrapped) {
|
|
247
216
|
value.info = wrapped[2];
|
|
248
217
|
value.sub = getTypeDef(extractSubType(type, wrapped), {}, count);
|
|
249
218
|
}
|
|
250
|
-
|
|
251
219
|
return value;
|
|
252
220
|
}
|
package/cjs/util/index.js
CHANGED
|
@@ -3,9 +3,7 @@
|
|
|
3
3
|
Object.defineProperty(exports, "__esModule", {
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
|
-
|
|
7
6
|
var _encodeTypes = require("./encodeTypes");
|
|
8
|
-
|
|
9
7
|
Object.keys(_encodeTypes).forEach(function (key) {
|
|
10
8
|
if (key === "default" || key === "__esModule") return;
|
|
11
9
|
if (key in exports && exports[key] === _encodeTypes[key]) return;
|
|
@@ -16,9 +14,7 @@ Object.keys(_encodeTypes).forEach(function (key) {
|
|
|
16
14
|
}
|
|
17
15
|
});
|
|
18
16
|
});
|
|
19
|
-
|
|
20
17
|
var _getTypeDef = require("./getTypeDef");
|
|
21
|
-
|
|
22
18
|
Object.keys(_getTypeDef).forEach(function (key) {
|
|
23
19
|
if (key === "default" || key === "__esModule") return;
|
|
24
20
|
if (key in exports && exports[key] === _getTypeDef[key]) return;
|
|
@@ -29,9 +25,7 @@ Object.keys(_getTypeDef).forEach(function (key) {
|
|
|
29
25
|
}
|
|
30
26
|
});
|
|
31
27
|
});
|
|
32
|
-
|
|
33
28
|
var _typeSplit = require("./typeSplit");
|
|
34
|
-
|
|
35
29
|
Object.keys(_typeSplit).forEach(function (key) {
|
|
36
30
|
if (key === "default" || key === "__esModule") return;
|
|
37
31
|
if (key in exports && exports[key] === _typeSplit[key]) return;
|
|
@@ -42,9 +36,7 @@ Object.keys(_typeSplit).forEach(function (key) {
|
|
|
42
36
|
}
|
|
43
37
|
});
|
|
44
38
|
});
|
|
45
|
-
|
|
46
39
|
var _xcm = require("./xcm");
|
|
47
|
-
|
|
48
40
|
Object.keys(_xcm).forEach(function (key) {
|
|
49
41
|
if (key === "default" || key === "__esModule") return;
|
|
50
42
|
if (key in exports && exports[key] === _xcm[key]) return;
|
package/cjs/util/typeSplit.js
CHANGED
|
@@ -4,20 +4,21 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.typeSplit = typeSplit;
|
|
7
|
-
|
|
8
7
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
9
8
|
// SPDX-License-Identifier: Apache-2.0
|
|
9
|
+
|
|
10
10
|
// safely split a string on ', ' while taking care of any nested occurences
|
|
11
11
|
function typeSplit(type) {
|
|
12
|
-
const result = [];
|
|
12
|
+
const result = [];
|
|
13
13
|
|
|
14
|
+
// these are the depths of the various tokens: <, [, {, (
|
|
14
15
|
let c = 0;
|
|
15
16
|
let f = 0;
|
|
16
17
|
let s = 0;
|
|
17
|
-
let t = 0;
|
|
18
|
+
let t = 0;
|
|
18
19
|
|
|
20
|
+
// current start position
|
|
19
21
|
let start = 0;
|
|
20
|
-
|
|
21
22
|
for (let i = 0; i < type.length; i++) {
|
|
22
23
|
switch (type[i]) {
|
|
23
24
|
// if we are not nested, add the type
|
|
@@ -27,54 +28,49 @@ function typeSplit(type) {
|
|
|
27
28
|
result.push(type.substring(start, i).trim());
|
|
28
29
|
start = i + 1;
|
|
29
30
|
}
|
|
30
|
-
|
|
31
31
|
break;
|
|
32
32
|
}
|
|
33
|
-
// adjust compact/vec (and friends) depth
|
|
34
33
|
|
|
34
|
+
// adjust compact/vec (and friends) depth
|
|
35
35
|
case '<':
|
|
36
36
|
c++;
|
|
37
37
|
break;
|
|
38
|
-
|
|
39
38
|
case '>':
|
|
40
39
|
c--;
|
|
41
40
|
break;
|
|
42
|
-
// adjust fixed vec depths
|
|
43
41
|
|
|
42
|
+
// adjust fixed vec depths
|
|
44
43
|
case '[':
|
|
45
44
|
f++;
|
|
46
45
|
break;
|
|
47
|
-
|
|
48
46
|
case ']':
|
|
49
47
|
f--;
|
|
50
48
|
break;
|
|
51
|
-
// adjust struct depth
|
|
52
49
|
|
|
50
|
+
// adjust struct depth
|
|
53
51
|
case '{':
|
|
54
52
|
s++;
|
|
55
53
|
break;
|
|
56
|
-
|
|
57
54
|
case '}':
|
|
58
55
|
s--;
|
|
59
56
|
break;
|
|
60
|
-
// adjust tuple depth
|
|
61
57
|
|
|
58
|
+
// adjust tuple depth
|
|
62
59
|
case '(':
|
|
63
60
|
t++;
|
|
64
61
|
break;
|
|
65
|
-
|
|
66
62
|
case ')':
|
|
67
63
|
t--;
|
|
68
64
|
break;
|
|
69
65
|
}
|
|
70
|
-
}
|
|
71
|
-
|
|
66
|
+
}
|
|
72
67
|
|
|
68
|
+
// ensure we have all the terminators taken care of
|
|
73
69
|
if (c || f || s || t) {
|
|
74
70
|
throw new Error(`Invalid definition (missing terminators) found in ${type}`);
|
|
75
|
-
}
|
|
76
|
-
|
|
71
|
+
}
|
|
77
72
|
|
|
73
|
+
// the final leg of the journey
|
|
78
74
|
result.push(type.substring(start, type.length).trim());
|
|
79
75
|
return result;
|
|
80
76
|
}
|
package/cjs/util/xcm.js
CHANGED
|
@@ -5,14 +5,12 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.XCM_MAPPINGS = void 0;
|
|
7
7
|
exports.mapXcmTypes = mapXcmTypes;
|
|
8
|
-
|
|
9
8
|
var _util = require("@polkadot/util");
|
|
10
|
-
|
|
11
9
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
12
10
|
// SPDX-License-Identifier: Apache-2.0
|
|
11
|
+
|
|
13
12
|
const XCM_MAPPINGS = ['AssetInstance', 'Fungibility', 'Junction', 'Junctions', 'MultiAsset', 'MultiAssetFilter', 'MultiLocation', 'Response', 'WildFungibility', 'WildMultiAsset', 'Xcm', 'XcmError', 'XcmOrder'];
|
|
14
13
|
exports.XCM_MAPPINGS = XCM_MAPPINGS;
|
|
15
|
-
|
|
16
14
|
function mapXcmTypes(version) {
|
|
17
15
|
return XCM_MAPPINGS.reduce((all, key) => (0, _util.objectSpread)(all, {
|
|
18
16
|
[key]: `${key}${version}`
|
package/create/class.js
CHANGED
|
@@ -1,54 +1,46 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
3
4
|
import { BTreeMap, BTreeSet, Bytes, CodecSet, Compact, DoNotConstruct, Enum, HashMap, Int, Null, Option, Range, RangeInclusive, Result, Struct, Tuple, U8aFixed, UInt, Vec, VecFixed, WrapperKeepOpaque, WrapperOpaque } from '@polkadot/types-codec';
|
|
4
5
|
import { isNumber, stringify } from '@polkadot/util';
|
|
5
6
|
import { TypeDefInfo } from "../types/index.js";
|
|
6
7
|
import { getTypeDef } from "../util/getTypeDef.js";
|
|
7
|
-
|
|
8
8
|
function getTypeDefType({
|
|
9
9
|
lookupName,
|
|
10
10
|
type
|
|
11
11
|
}) {
|
|
12
12
|
return lookupName || type;
|
|
13
13
|
}
|
|
14
|
-
|
|
15
14
|
function getSubDefArray(value) {
|
|
16
15
|
if (!Array.isArray(value.sub)) {
|
|
17
16
|
throw new Error(`Expected subtype as TypeDef[] in ${stringify(value)}`);
|
|
18
17
|
}
|
|
19
|
-
|
|
20
18
|
return value.sub;
|
|
21
19
|
}
|
|
22
|
-
|
|
23
20
|
function getSubDef(value) {
|
|
24
21
|
if (!value.sub || Array.isArray(value.sub)) {
|
|
25
22
|
throw new Error(`Expected subtype as TypeDef in ${stringify(value)}`);
|
|
26
23
|
}
|
|
27
|
-
|
|
28
24
|
return value.sub;
|
|
29
25
|
}
|
|
30
|
-
|
|
31
26
|
function getSubType(value) {
|
|
32
27
|
return getTypeDefType(getSubDef(value));
|
|
33
|
-
}
|
|
34
|
-
|
|
28
|
+
}
|
|
35
29
|
|
|
30
|
+
// create a maps of type string CodecClasss from the input
|
|
36
31
|
function getTypeClassMap(value) {
|
|
37
32
|
const subs = getSubDefArray(value);
|
|
38
33
|
const map = {};
|
|
39
|
-
|
|
40
34
|
for (let i = 0; i < subs.length; i++) {
|
|
41
35
|
map[subs[i].name] = getTypeDefType(subs[i]);
|
|
42
36
|
}
|
|
43
|
-
|
|
44
37
|
return map;
|
|
45
|
-
}
|
|
46
|
-
|
|
38
|
+
}
|
|
47
39
|
|
|
40
|
+
// create an array of type string CodecClasss from the input
|
|
48
41
|
function getTypeClassArray(value) {
|
|
49
42
|
return getSubDefArray(value).map(getTypeDefType);
|
|
50
43
|
}
|
|
51
|
-
|
|
52
44
|
function createInt(Clazz, {
|
|
53
45
|
displayName,
|
|
54
46
|
length
|
|
@@ -56,19 +48,15 @@ function createInt(Clazz, {
|
|
|
56
48
|
if (!isNumber(length)) {
|
|
57
49
|
throw new Error(`Expected bitLength information for ${displayName || Clazz.constructor.name}<bitLength>`);
|
|
58
50
|
}
|
|
59
|
-
|
|
60
51
|
return Clazz.with(length, displayName);
|
|
61
52
|
}
|
|
62
|
-
|
|
63
53
|
function createHashMap(Clazz, value) {
|
|
64
54
|
const [keyType, valueType] = getTypeClassArray(value);
|
|
65
55
|
return Clazz.with(keyType, valueType);
|
|
66
56
|
}
|
|
67
|
-
|
|
68
57
|
function createWithSub(Clazz, value) {
|
|
69
58
|
return Clazz.with(getSubType(value));
|
|
70
59
|
}
|
|
71
|
-
|
|
72
60
|
const infoMapping = {
|
|
73
61
|
[TypeDefInfo.BTreeMap]: (registry, value) => createHashMap(BTreeMap, value),
|
|
74
62
|
[TypeDefInfo.BTreeSet]: (registry, value) => createWithSub(BTreeSet, value),
|
|
@@ -90,18 +78,18 @@ const infoMapping = {
|
|
|
90
78
|
[TypeDefInfo.Int]: (registry, value) => createInt(Int, value),
|
|
91
79
|
// We have circular deps between Linkage & Struct
|
|
92
80
|
[TypeDefInfo.Linkage]: (registry, value) => {
|
|
93
|
-
const type = `Option<${getSubType(value)}>`;
|
|
94
|
-
|
|
81
|
+
const type = `Option<${getSubType(value)}>`;
|
|
82
|
+
// eslint-disable-next-line sort-keys
|
|
95
83
|
const Clazz = Struct.with({
|
|
96
84
|
previous: type,
|
|
97
85
|
next: type
|
|
98
|
-
});
|
|
86
|
+
});
|
|
99
87
|
|
|
88
|
+
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
100
89
|
Clazz.prototype.toRawType = function () {
|
|
101
90
|
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call
|
|
102
91
|
return `Linkage<${this.next.toRawType(true)}>`;
|
|
103
92
|
};
|
|
104
|
-
|
|
105
93
|
return Clazz;
|
|
106
94
|
},
|
|
107
95
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
@@ -109,20 +97,22 @@ const infoMapping = {
|
|
|
109
97
|
[TypeDefInfo.Option]: (registry, value) => {
|
|
110
98
|
if (!value.sub || Array.isArray(value.sub)) {
|
|
111
99
|
throw new Error('Expected type information for Option');
|
|
112
|
-
}
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
// NOTE This is opt-in (unhandled), not by default
|
|
113
103
|
// if (value.sub.type === 'bool') {
|
|
114
104
|
// return OptionBool;
|
|
115
105
|
// }
|
|
116
106
|
|
|
117
|
-
|
|
118
107
|
return createWithSub(Option, value);
|
|
119
108
|
},
|
|
120
109
|
[TypeDefInfo.Plain]: (registry, value) => registry.getOrUnknown(value.type),
|
|
121
110
|
[TypeDefInfo.Range]: (registry, value) => createWithSub(Range, value),
|
|
122
111
|
[TypeDefInfo.RangeInclusive]: (registry, value) => createWithSub(RangeInclusive, value),
|
|
123
112
|
[TypeDefInfo.Result]: (registry, value) => {
|
|
124
|
-
const [Ok, Err] = getTypeClassArray(value);
|
|
113
|
+
const [Ok, Err] = getTypeClassArray(value);
|
|
125
114
|
|
|
115
|
+
// eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
126
116
|
return Result.with({
|
|
127
117
|
Err,
|
|
128
118
|
Ok
|
|
@@ -145,7 +135,6 @@ const infoMapping = {
|
|
|
145
135
|
if (!sub || Array.isArray(sub)) {
|
|
146
136
|
throw new Error('Expected type information for vector');
|
|
147
137
|
}
|
|
148
|
-
|
|
149
138
|
return sub.type === 'u8' ? Bytes : Vec.with(getTypeDefType(sub));
|
|
150
139
|
},
|
|
151
140
|
[TypeDefInfo.VecFixed]: (registry, {
|
|
@@ -156,7 +145,6 @@ const infoMapping = {
|
|
|
156
145
|
if (!isNumber(length) || !sub || Array.isArray(sub)) {
|
|
157
146
|
throw new Error('Expected length & type information for fixed vector');
|
|
158
147
|
}
|
|
159
|
-
|
|
160
148
|
return sub.type === 'u8' ? U8aFixed.with(length * 8, displayName) : VecFixed.with(getTypeDefType(sub), length);
|
|
161
149
|
},
|
|
162
150
|
[TypeDefInfo.WrapperKeepOpaque]: (registry, value) => createWithSub(WrapperKeepOpaque, value),
|
|
@@ -165,30 +153,31 @@ const infoMapping = {
|
|
|
165
153
|
export function constructTypeClass(registry, typeDef) {
|
|
166
154
|
try {
|
|
167
155
|
const Type = infoMapping[typeDef.info](registry, typeDef);
|
|
168
|
-
|
|
169
156
|
if (!Type) {
|
|
170
157
|
throw new Error('No class created');
|
|
171
|
-
}
|
|
172
|
-
|
|
158
|
+
}
|
|
173
159
|
|
|
160
|
+
// don't clobber any existing
|
|
174
161
|
if (!Type.__fallbackType && typeDef.fallbackType) {
|
|
175
162
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
176
163
|
// @ts-ignore ...this is the only place we we actually assign this...
|
|
177
164
|
Type.__fallbackType = typeDef.fallbackType;
|
|
178
165
|
}
|
|
179
|
-
|
|
180
166
|
return Type;
|
|
181
167
|
} catch (error) {
|
|
182
168
|
throw new Error(`Unable to construct class from ${stringify(typeDef)}: ${error.message}`);
|
|
183
169
|
}
|
|
184
|
-
}
|
|
170
|
+
}
|
|
185
171
|
|
|
172
|
+
// Returns the type Class for construction
|
|
186
173
|
export function getTypeClass(registry, typeDef) {
|
|
187
174
|
return registry.getUnsafe(typeDef.type, false, typeDef);
|
|
188
175
|
}
|
|
189
176
|
export function createClassUnsafe(registry, type) {
|
|
190
|
-
return (
|
|
191
|
-
|
|
177
|
+
return (
|
|
178
|
+
// just retrieve via name, no creation via typeDef
|
|
179
|
+
registry.getUnsafe(type) ||
|
|
180
|
+
// we don't have an existing type, create the class via typeDef
|
|
192
181
|
getTypeClass(registry, registry.isLookupType(type) ? registry.lookup.getTypeDef(type) : getTypeDef(type))
|
|
193
182
|
);
|
|
194
183
|
}
|
package/create/index.js
CHANGED
package/create/type.js
CHANGED
|
@@ -1,33 +1,36 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
3
4
|
import { Option } from '@polkadot/types-codec';
|
|
4
5
|
import { isHex, isU8a, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
5
|
-
import { createClassUnsafe } from "./class.js";
|
|
6
|
-
// is much slower, but verifies that we have the correct types defined
|
|
6
|
+
import { createClassUnsafe } from "./class.js";
|
|
7
7
|
|
|
8
|
+
// With isPedantic, actually check that the encoding matches that supplied. This
|
|
9
|
+
// is much slower, but verifies that we have the correct types defined
|
|
8
10
|
function checkInstance(created, matcher) {
|
|
9
11
|
const u8a = created.toU8a();
|
|
10
12
|
const rawType = created.toRawType();
|
|
11
|
-
const isOk =
|
|
12
|
-
|
|
13
|
-
|
|
13
|
+
const isOk =
|
|
14
|
+
// full match, all ok
|
|
15
|
+
u8aEq(u8a, matcher) ||
|
|
16
|
+
// on a length-prefixed type, just check the actual length
|
|
17
|
+
['Bytes', 'Text', 'Type'].includes(rawType) && matcher.length === created.length ||
|
|
18
|
+
// when the created is empty and matcher is also empty, let it slide...
|
|
14
19
|
created.isEmpty && matcher.every(v => !v);
|
|
15
|
-
|
|
16
20
|
if (!isOk) {
|
|
17
21
|
throw new Error(`${rawType}:: Decoded input doesn't match input, received ${u8aToHex(matcher, 512)} (${matcher.length} bytes), created ${u8aToHex(u8a, 512)} (${u8a.length} bytes)`);
|
|
18
22
|
}
|
|
19
23
|
}
|
|
20
|
-
|
|
21
24
|
function checkPedantic(created, [value]) {
|
|
22
25
|
if (isU8a(value)) {
|
|
23
26
|
checkInstance(created, value);
|
|
24
27
|
} else if (isHex(value)) {
|
|
25
28
|
checkInstance(created, u8aToU8a(value));
|
|
26
29
|
}
|
|
27
|
-
}
|
|
28
|
-
// where isPedantic is specified (storage decoding), also check the format/structure
|
|
29
|
-
|
|
30
|
+
}
|
|
30
31
|
|
|
32
|
+
// Initializes a type with a value. This also checks for fallbacks and in the cases
|
|
33
|
+
// where isPedantic is specified (storage decoding), also check the format/structure
|
|
31
34
|
function initType(registry, Type, params = [], {
|
|
32
35
|
blockHash,
|
|
33
36
|
isOptional,
|
|
@@ -35,35 +38,31 @@ function initType(registry, Type, params = [], {
|
|
|
35
38
|
} = {}) {
|
|
36
39
|
const created = new (isOptional ? Option.with(Type) : Type)(registry, ...params);
|
|
37
40
|
isPedantic && checkPedantic(created, params);
|
|
38
|
-
|
|
39
41
|
if (blockHash) {
|
|
40
42
|
created.createdAtHash = createTypeUnsafe(registry, 'Hash', [blockHash]);
|
|
41
43
|
}
|
|
42
|
-
|
|
43
44
|
return created;
|
|
44
|
-
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
// An unsafe version of the `createType` below. It's unsafe because the `type`
|
|
45
48
|
// argument here can be any string, which, when it cannot parse, will yield a
|
|
46
49
|
// runtime error.
|
|
47
|
-
|
|
48
|
-
|
|
49
50
|
export function createTypeUnsafe(registry, type, params = [], options = {}) {
|
|
50
51
|
let Clazz = null;
|
|
51
52
|
let firstError = null;
|
|
52
|
-
|
|
53
53
|
try {
|
|
54
54
|
Clazz = createClassUnsafe(registry, type);
|
|
55
55
|
return initType(registry, Clazz, params, options);
|
|
56
56
|
} catch (error) {
|
|
57
57
|
firstError = new Error(`createType(${type}):: ${error.message}`);
|
|
58
58
|
}
|
|
59
|
-
|
|
60
59
|
if (Clazz && Clazz.__fallbackType) {
|
|
61
60
|
try {
|
|
62
61
|
Clazz = createClassUnsafe(registry, Clazz.__fallbackType);
|
|
63
62
|
return initType(registry, Clazz, params, options);
|
|
64
|
-
} catch {
|
|
63
|
+
} catch {
|
|
64
|
+
// swallow, we will throw the first error again
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
|
-
|
|
68
67
|
throw firstError;
|
|
69
68
|
}
|
package/detectOther.js
CHANGED
package/detectPackage.js
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
3
4
|
// Do not edit, auto-generated by @polkadot/dev
|
|
5
|
+
|
|
4
6
|
import { detectPackage } from '@polkadot/util';
|
|
5
7
|
import others from "./detectOther.js";
|
|
6
8
|
import { packageInfo } from "./packageInfo.js";
|