@polkadot/types-create 8.8.3-1 → 8.8.3-12
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/cjs/create/class.js +32 -7
- package/cjs/create/type.js +4 -1
- package/cjs/packageInfo.js +1 -1
- package/cjs/util/encodeTypes.js +30 -6
- package/cjs/util/getTypeDef.js +45 -16
- package/cjs/util/typeSplit.js +32 -33
- package/create/class.js +30 -8
- package/create/type.js +5 -2
- package/package.json +3 -3
- package/packageInfo.js +1 -1
- package/util/encodeTypes.js +26 -7
- package/util/getTypeDef.d.ts +2 -1
- package/util/getTypeDef.js +46 -17
- package/util/typeSplit.js +32 -29
package/cjs/create/class.js
CHANGED
|
@@ -26,12 +26,18 @@ function getTypeDefType(_ref) {
|
|
|
26
26
|
}
|
|
27
27
|
|
|
28
28
|
function getSubDefArray(value) {
|
|
29
|
-
|
|
29
|
+
if (!Array.isArray(value.sub)) {
|
|
30
|
+
throw new Error(`Expected subtype as TypeDef[] in ${(0, _util.stringify)(value)}`);
|
|
31
|
+
}
|
|
32
|
+
|
|
30
33
|
return value.sub;
|
|
31
34
|
}
|
|
32
35
|
|
|
33
36
|
function getSubDef(value) {
|
|
34
|
-
|
|
37
|
+
if (!value.sub || Array.isArray(value.sub)) {
|
|
38
|
+
throw new Error(`Expected subtype as TypeDef in ${(0, _util.stringify)(value)}`);
|
|
39
|
+
}
|
|
40
|
+
|
|
35
41
|
return value.sub;
|
|
36
42
|
}
|
|
37
43
|
|
|
@@ -61,7 +67,11 @@ function createInt(Clazz, _ref2) {
|
|
|
61
67
|
displayName,
|
|
62
68
|
length
|
|
63
69
|
} = _ref2;
|
|
64
|
-
|
|
70
|
+
|
|
71
|
+
if (!(0, _util.isNumber)(length)) {
|
|
72
|
+
throw new Error(`Expected bitLength information for ${displayName || Clazz.constructor.name}<bitLength>`);
|
|
73
|
+
}
|
|
74
|
+
|
|
65
75
|
return Clazz.with(length, displayName);
|
|
66
76
|
}
|
|
67
77
|
|
|
@@ -117,11 +127,14 @@ const infoMapping = {
|
|
|
117
127
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
118
128
|
[_types.TypeDefInfo.Null]: (registry, _) => _typesCodec.Null,
|
|
119
129
|
[_types.TypeDefInfo.Option]: (registry, value) => {
|
|
120
|
-
|
|
130
|
+
if (!value.sub || Array.isArray(value.sub)) {
|
|
131
|
+
throw new Error('Expected type information for Option');
|
|
132
|
+
} // NOTE This is opt-in (unhandled), not by default
|
|
121
133
|
// if (value.sub.type === 'bool') {
|
|
122
134
|
// return OptionBool;
|
|
123
135
|
// }
|
|
124
136
|
|
|
137
|
+
|
|
125
138
|
return createWithSub(_typesCodec.Option, value);
|
|
126
139
|
},
|
|
127
140
|
[_types.TypeDefInfo.Plain]: (registry, value) => registry.getOrUnknown(value.type),
|
|
@@ -151,7 +164,11 @@ const infoMapping = {
|
|
|
151
164
|
let {
|
|
152
165
|
sub
|
|
153
166
|
} = _ref6;
|
|
154
|
-
|
|
167
|
+
|
|
168
|
+
if (!sub || Array.isArray(sub)) {
|
|
169
|
+
throw new Error('Expected type information for vector');
|
|
170
|
+
}
|
|
171
|
+
|
|
155
172
|
return sub.type === 'u8' ? _typesCodec.Bytes : _typesCodec.Vec.with(getTypeDefType(sub));
|
|
156
173
|
},
|
|
157
174
|
[_types.TypeDefInfo.VecFixed]: (registry, _ref7) => {
|
|
@@ -160,7 +177,11 @@ const infoMapping = {
|
|
|
160
177
|
length,
|
|
161
178
|
sub
|
|
162
179
|
} = _ref7;
|
|
163
|
-
|
|
180
|
+
|
|
181
|
+
if (!(0, _util.isNumber)(length) || !sub || Array.isArray(sub)) {
|
|
182
|
+
throw new Error('Expected length & type information for fixed vector');
|
|
183
|
+
}
|
|
184
|
+
|
|
164
185
|
return sub.type === 'u8' ? _typesCodec.U8aFixed.with(length * 8, displayName) : _typesCodec.VecFixed.with(getTypeDefType(sub), length);
|
|
165
186
|
},
|
|
166
187
|
[_types.TypeDefInfo.WrapperKeepOpaque]: (registry, value) => createWithSub(_typesCodec.WrapperKeepOpaque, value),
|
|
@@ -170,7 +191,11 @@ const infoMapping = {
|
|
|
170
191
|
function constructTypeClass(registry, typeDef) {
|
|
171
192
|
try {
|
|
172
193
|
const Type = infoMapping[typeDef.info](registry, typeDef);
|
|
173
|
-
|
|
194
|
+
|
|
195
|
+
if (!Type) {
|
|
196
|
+
throw new Error('No class created');
|
|
197
|
+
} // don't clobber any existing
|
|
198
|
+
|
|
174
199
|
|
|
175
200
|
if (!Type.__fallbackType && typeDef.fallbackType) {
|
|
176
201
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
package/cjs/create/type.js
CHANGED
|
@@ -22,7 +22,10 @@ function checkInstance(created, matcher) {
|
|
|
22
22
|
(0, _util.u8aEq)(u8a, matcher) || // on a length-prefixed type, just check the actual length
|
|
23
23
|
['Bytes', 'Text', 'Type'].includes(rawType) && matcher.length === created.length || // when the created is empty and matcher is also empty, let it slide...
|
|
24
24
|
created.isEmpty && matcher.every(v => !v);
|
|
25
|
-
|
|
25
|
+
|
|
26
|
+
if (!isOk) {
|
|
27
|
+
throw new Error(`${rawType}:: Decoded input doesn't match input, received ${(0, _util.u8aToHex)(matcher, 512)} (${matcher.length} bytes), created ${(0, _util.u8aToHex)(u8a, 512)} (${u8a.length} bytes)`);
|
|
28
|
+
}
|
|
26
29
|
}
|
|
27
30
|
|
|
28
31
|
function checkPedantic(created, _ref) {
|
package/cjs/packageInfo.js
CHANGED
package/cjs/util/encodeTypes.js
CHANGED
|
@@ -54,7 +54,11 @@ function encodeSubTypes(registry, sub, asEnum, extra) {
|
|
|
54
54
|
} = _ref;
|
|
55
55
|
return name;
|
|
56
56
|
});
|
|
57
|
-
|
|
57
|
+
|
|
58
|
+
if (!names.every(n => !!n)) {
|
|
59
|
+
throw new Error(`Subtypes does not have consistent names, ${names.join(', ')}`);
|
|
60
|
+
}
|
|
61
|
+
|
|
58
62
|
const inner = (0, _util.objectSpread)({}, extra);
|
|
59
63
|
|
|
60
64
|
for (let i = 0; i < sub.length; i++) {
|
|
@@ -85,9 +89,13 @@ const encoders = {
|
|
|
85
89
|
let {
|
|
86
90
|
sub
|
|
87
91
|
} = _ref3;
|
|
88
|
-
|
|
92
|
+
|
|
93
|
+
if (!Array.isArray(sub)) {
|
|
94
|
+
throw new Error('Unable to encode Enum type');
|
|
95
|
+
} // c-like enums have all Null entries
|
|
89
96
|
// TODO We need to take the disciminant into account and auto-add empty entries
|
|
90
97
|
|
|
98
|
+
|
|
91
99
|
return sub.every(_ref4 => {
|
|
92
100
|
let {
|
|
93
101
|
type
|
|
@@ -130,7 +138,11 @@ const encoders = {
|
|
|
130
138
|
length = 8,
|
|
131
139
|
sub
|
|
132
140
|
} = _ref8;
|
|
133
|
-
|
|
141
|
+
|
|
142
|
+
if (!Array.isArray(sub)) {
|
|
143
|
+
throw new Error('Unable to encode Set type');
|
|
144
|
+
}
|
|
145
|
+
|
|
134
146
|
return (0, _util.stringify)({
|
|
135
147
|
_set: sub.reduce((all, _ref9, count) => {
|
|
136
148
|
let {
|
|
@@ -158,7 +170,11 @@ const encoders = {
|
|
|
158
170
|
alias,
|
|
159
171
|
sub
|
|
160
172
|
} = _ref11;
|
|
161
|
-
|
|
173
|
+
|
|
174
|
+
if (!Array.isArray(sub)) {
|
|
175
|
+
throw new Error('Unable to encode Struct type');
|
|
176
|
+
}
|
|
177
|
+
|
|
162
178
|
return encodeSubTypes(registry, sub, false, alias ? {
|
|
163
179
|
_alias: [...alias.entries()].reduce((all, _ref12) => {
|
|
164
180
|
let [k, v] = _ref12;
|
|
@@ -172,7 +188,11 @@ const encoders = {
|
|
|
172
188
|
let {
|
|
173
189
|
sub
|
|
174
190
|
} = _ref13;
|
|
175
|
-
|
|
191
|
+
|
|
192
|
+
if (!Array.isArray(sub)) {
|
|
193
|
+
throw new Error('Unable to encode Tuple type');
|
|
194
|
+
}
|
|
195
|
+
|
|
176
196
|
return `(${sub.map(type => encodeTypeDef(registry, type)).join(',')})`;
|
|
177
197
|
},
|
|
178
198
|
[_types.TypeDefInfo.UInt]: (registry, _ref14) => {
|
|
@@ -187,7 +207,11 @@ const encoders = {
|
|
|
187
207
|
length,
|
|
188
208
|
sub
|
|
189
209
|
} = _ref15;
|
|
190
|
-
|
|
210
|
+
|
|
211
|
+
if (!(0, _util.isNumber)(length) || !sub || Array.isArray(sub)) {
|
|
212
|
+
throw new Error('Unable to encode VecFixed type');
|
|
213
|
+
}
|
|
214
|
+
|
|
191
215
|
return `[${sub.type};${length}]`;
|
|
192
216
|
},
|
|
193
217
|
[_types.TypeDefInfo.WrapperKeepOpaque]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'WrapperKeepOpaque'),
|
package/cjs/util/getTypeDef.js
CHANGED
|
@@ -15,7 +15,6 @@ var _typeSplit = require("./typeSplit");
|
|
|
15
15
|
|
|
16
16
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
17
17
|
// SPDX-License-Identifier: Apache-2.0
|
|
18
|
-
const MAX_NESTED = 64;
|
|
19
18
|
const KNOWN_INTERNALS = ['_alias', '_fallback'];
|
|
20
19
|
|
|
21
20
|
function getTypeString(typeOrObj) {
|
|
@@ -26,7 +25,10 @@ function isRustEnum(details) {
|
|
|
26
25
|
const values = Object.values(details);
|
|
27
26
|
|
|
28
27
|
if (values.some(v => (0, _util.isNumber)(v))) {
|
|
29
|
-
|
|
28
|
+
if (!values.every(v => (0, _util.isNumber)(v) && v >= 0 && v <= 255)) {
|
|
29
|
+
throw new Error('Invalid number-indexed enum definition');
|
|
30
|
+
}
|
|
31
|
+
|
|
30
32
|
return false;
|
|
31
33
|
}
|
|
32
34
|
|
|
@@ -121,21 +123,42 @@ function _decodeFixedVec(value, type, _, count) {
|
|
|
121
123
|
let inner = 0;
|
|
122
124
|
|
|
123
125
|
for (let i = 1; i < max && index === -1; i++) {
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
126
|
+
switch (type[i]) {
|
|
127
|
+
case ';':
|
|
128
|
+
{
|
|
129
|
+
if (inner === 0) {
|
|
130
|
+
index = i;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
case '[':
|
|
137
|
+
case '(':
|
|
138
|
+
case '<':
|
|
139
|
+
inner++;
|
|
140
|
+
break;
|
|
141
|
+
|
|
142
|
+
case ']':
|
|
143
|
+
case ')':
|
|
144
|
+
case '>':
|
|
145
|
+
inner--;
|
|
146
|
+
break;
|
|
130
147
|
}
|
|
131
148
|
}
|
|
132
149
|
|
|
133
|
-
|
|
150
|
+
if (index === -1) {
|
|
151
|
+
throw new Error(`${type}: Unable to extract location of ';'`);
|
|
152
|
+
}
|
|
153
|
+
|
|
134
154
|
const vecType = type.substring(1, index);
|
|
135
155
|
const [strLength, displayName] = type.substring(index + 1, max).split(';');
|
|
136
|
-
const length = parseInt(strLength.trim(), 10);
|
|
156
|
+
const length = parseInt(strLength.trim(), 10);
|
|
157
|
+
|
|
158
|
+
if (length > 2048) {
|
|
159
|
+
throw new Error(`${type}: Only support for [Type; <length>], where length <= 2048`);
|
|
160
|
+
}
|
|
137
161
|
|
|
138
|
-
(0, _util.assert)(length <= 256, () => `${type}: Only support for [Type; <length>], where length <= 256`);
|
|
139
162
|
value.displayName = displayName;
|
|
140
163
|
value.length = length;
|
|
141
164
|
value.sub = getTypeDef(vecType, {}, count);
|
|
@@ -152,9 +175,12 @@ function _decodeTuple(value, _, subType, count) {
|
|
|
152
175
|
|
|
153
176
|
function _decodeAnyInt(value, type, _, clazz) {
|
|
154
177
|
const [strLength, displayName] = type.substring(clazz.length + 1, type.length - 1).split(',');
|
|
155
|
-
const length = parseInt(strLength.trim(), 10);
|
|
178
|
+
const length = parseInt(strLength.trim(), 10);
|
|
179
|
+
|
|
180
|
+
if (length > 8192 || length % 8) {
|
|
181
|
+
throw new Error(`${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
|
|
182
|
+
}
|
|
156
183
|
|
|
157
|
-
(0, _util.assert)(length <= 8192 && length % 8 === 0, () => `${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
|
|
158
184
|
value.displayName = displayName;
|
|
159
185
|
value.length = length;
|
|
160
186
|
return value;
|
|
@@ -187,8 +213,7 @@ const wrappedExtraction = [['BTreeSet<', '>', _types.TypeDefInfo.BTreeSet], ['Co
|
|
|
187
213
|
function extractSubType(type, _ref6) {
|
|
188
214
|
let [start, end] = _ref6;
|
|
189
215
|
return type.substring(start.length, type.length - end.length);
|
|
190
|
-
}
|
|
191
|
-
|
|
216
|
+
}
|
|
192
217
|
|
|
193
218
|
function getTypeDef(_type) {
|
|
194
219
|
let {
|
|
@@ -204,7 +229,11 @@ function getTypeDef(_type) {
|
|
|
204
229
|
name,
|
|
205
230
|
type
|
|
206
231
|
};
|
|
207
|
-
|
|
232
|
+
|
|
233
|
+
if (++count > 64) {
|
|
234
|
+
throw new Error('getTypeDef: Maximum nested limit reached');
|
|
235
|
+
}
|
|
236
|
+
|
|
208
237
|
const nested = nestedExtraction.find(nested => hasWrapper(type, nested));
|
|
209
238
|
|
|
210
239
|
if (nested) {
|
package/cjs/util/typeSplit.js
CHANGED
|
@@ -5,76 +5,75 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
5
5
|
});
|
|
6
6
|
exports.typeSplit = typeSplit;
|
|
7
7
|
|
|
8
|
-
var _util = require("@polkadot/util");
|
|
9
|
-
|
|
10
8
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
11
9
|
// SPDX-License-Identifier: Apache-2.0
|
|
12
|
-
|
|
13
|
-
for (var _len = arguments.length, counters = new Array(_len), _key = 0; _key < _len; _key++) {
|
|
14
|
-
counters[_key] = arguments[_key];
|
|
15
|
-
}
|
|
16
|
-
|
|
17
|
-
return !counters.some(counter => counter !== 0);
|
|
18
|
-
} // safely split a string on ', ' while taking care of any nested occurences
|
|
19
|
-
|
|
20
|
-
|
|
10
|
+
// safely split a string on ', ' while taking care of any nested occurences
|
|
21
11
|
function typeSplit(type) {
|
|
22
|
-
|
|
23
|
-
const result = [];
|
|
12
|
+
const result = []; // these are the depths of the various tokens: <, [, {, (
|
|
24
13
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
}
|
|
30
|
-
};
|
|
14
|
+
let c = 0;
|
|
15
|
+
let f = 0;
|
|
16
|
+
let s = 0;
|
|
17
|
+
let t = 0; // current start position
|
|
31
18
|
|
|
32
|
-
|
|
33
|
-
|
|
19
|
+
let start = 0;
|
|
20
|
+
|
|
21
|
+
for (let i = 0; i < type.length; i++) {
|
|
22
|
+
switch (type[i]) {
|
|
34
23
|
// if we are not nested, add the type
|
|
35
24
|
case ',':
|
|
36
|
-
|
|
37
|
-
|
|
25
|
+
{
|
|
26
|
+
if (!(c || f || s || t)) {
|
|
27
|
+
result.push(type.substring(start, i).trim());
|
|
28
|
+
start = i + 1;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
break;
|
|
32
|
+
}
|
|
38
33
|
// adjust compact/vec (and friends) depth
|
|
39
34
|
|
|
40
35
|
case '<':
|
|
41
|
-
|
|
36
|
+
c++;
|
|
42
37
|
break;
|
|
43
38
|
|
|
44
39
|
case '>':
|
|
45
|
-
|
|
40
|
+
c--;
|
|
46
41
|
break;
|
|
47
42
|
// adjust fixed vec depths
|
|
48
43
|
|
|
49
44
|
case '[':
|
|
50
|
-
|
|
45
|
+
f++;
|
|
51
46
|
break;
|
|
52
47
|
|
|
53
48
|
case ']':
|
|
54
|
-
|
|
49
|
+
f--;
|
|
55
50
|
break;
|
|
56
51
|
// adjust struct depth
|
|
57
52
|
|
|
58
53
|
case '{':
|
|
59
|
-
|
|
54
|
+
s++;
|
|
60
55
|
break;
|
|
61
56
|
|
|
62
57
|
case '}':
|
|
63
|
-
|
|
58
|
+
s--;
|
|
64
59
|
break;
|
|
65
60
|
// adjust tuple depth
|
|
66
61
|
|
|
67
62
|
case '(':
|
|
68
|
-
|
|
63
|
+
t++;
|
|
69
64
|
break;
|
|
70
65
|
|
|
71
66
|
case ')':
|
|
72
|
-
|
|
67
|
+
t--;
|
|
73
68
|
break;
|
|
74
69
|
}
|
|
75
|
-
}
|
|
70
|
+
} // ensure we have all the terminators taken care of
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
if (c || f || s || t) {
|
|
74
|
+
throw new Error(`Invalid definition (missing terminators) found in ${type}`);
|
|
75
|
+
} // the final leg of the journey
|
|
76
76
|
|
|
77
|
-
(0, _util.assert)(isNotNested(cDepth, fDepth, sDepth, tDepth), () => `Invalid definition (missing terminators) found in ${type}`); // the final leg of the journey
|
|
78
77
|
|
|
79
78
|
result.push(type.substring(start, type.length).trim());
|
|
80
79
|
return result;
|
package/create/class.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
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
|
-
import {
|
|
4
|
+
import { isNumber, stringify } from '@polkadot/util';
|
|
5
5
|
import { TypeDefInfo } from "../types/index.js";
|
|
6
6
|
import { getTypeDef } from "../util/getTypeDef.js";
|
|
7
7
|
|
|
@@ -13,12 +13,18 @@ function getTypeDefType({
|
|
|
13
13
|
}
|
|
14
14
|
|
|
15
15
|
function getSubDefArray(value) {
|
|
16
|
-
|
|
16
|
+
if (!Array.isArray(value.sub)) {
|
|
17
|
+
throw new Error(`Expected subtype as TypeDef[] in ${stringify(value)}`);
|
|
18
|
+
}
|
|
19
|
+
|
|
17
20
|
return value.sub;
|
|
18
21
|
}
|
|
19
22
|
|
|
20
23
|
function getSubDef(value) {
|
|
21
|
-
|
|
24
|
+
if (!value.sub || Array.isArray(value.sub)) {
|
|
25
|
+
throw new Error(`Expected subtype as TypeDef in ${stringify(value)}`);
|
|
26
|
+
}
|
|
27
|
+
|
|
22
28
|
return value.sub;
|
|
23
29
|
}
|
|
24
30
|
|
|
@@ -47,7 +53,10 @@ function createInt(Clazz, {
|
|
|
47
53
|
displayName,
|
|
48
54
|
length
|
|
49
55
|
}) {
|
|
50
|
-
|
|
56
|
+
if (!isNumber(length)) {
|
|
57
|
+
throw new Error(`Expected bitLength information for ${displayName || Clazz.constructor.name}<bitLength>`);
|
|
58
|
+
}
|
|
59
|
+
|
|
51
60
|
return Clazz.with(length, displayName);
|
|
52
61
|
}
|
|
53
62
|
|
|
@@ -98,11 +107,14 @@ const infoMapping = {
|
|
|
98
107
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
99
108
|
[TypeDefInfo.Null]: (registry, _) => Null,
|
|
100
109
|
[TypeDefInfo.Option]: (registry, value) => {
|
|
101
|
-
|
|
110
|
+
if (!value.sub || Array.isArray(value.sub)) {
|
|
111
|
+
throw new Error('Expected type information for Option');
|
|
112
|
+
} // NOTE This is opt-in (unhandled), not by default
|
|
102
113
|
// if (value.sub.type === 'bool') {
|
|
103
114
|
// return OptionBool;
|
|
104
115
|
// }
|
|
105
116
|
|
|
117
|
+
|
|
106
118
|
return createWithSub(Option, value);
|
|
107
119
|
},
|
|
108
120
|
[TypeDefInfo.Plain]: (registry, value) => registry.getOrUnknown(value.type),
|
|
@@ -130,7 +142,10 @@ const infoMapping = {
|
|
|
130
142
|
[TypeDefInfo.Vec]: (registry, {
|
|
131
143
|
sub
|
|
132
144
|
}) => {
|
|
133
|
-
|
|
145
|
+
if (!sub || Array.isArray(sub)) {
|
|
146
|
+
throw new Error('Expected type information for vector');
|
|
147
|
+
}
|
|
148
|
+
|
|
134
149
|
return sub.type === 'u8' ? Bytes : Vec.with(getTypeDefType(sub));
|
|
135
150
|
},
|
|
136
151
|
[TypeDefInfo.VecFixed]: (registry, {
|
|
@@ -138,7 +153,10 @@ const infoMapping = {
|
|
|
138
153
|
length,
|
|
139
154
|
sub
|
|
140
155
|
}) => {
|
|
141
|
-
|
|
156
|
+
if (!isNumber(length) || !sub || Array.isArray(sub)) {
|
|
157
|
+
throw new Error('Expected length & type information for fixed vector');
|
|
158
|
+
}
|
|
159
|
+
|
|
142
160
|
return sub.type === 'u8' ? U8aFixed.with(length * 8, displayName) : VecFixed.with(getTypeDefType(sub), length);
|
|
143
161
|
},
|
|
144
162
|
[TypeDefInfo.WrapperKeepOpaque]: (registry, value) => createWithSub(WrapperKeepOpaque, value),
|
|
@@ -147,7 +165,11 @@ const infoMapping = {
|
|
|
147
165
|
export function constructTypeClass(registry, typeDef) {
|
|
148
166
|
try {
|
|
149
167
|
const Type = infoMapping[typeDef.info](registry, typeDef);
|
|
150
|
-
|
|
168
|
+
|
|
169
|
+
if (!Type) {
|
|
170
|
+
throw new Error('No class created');
|
|
171
|
+
} // don't clobber any existing
|
|
172
|
+
|
|
151
173
|
|
|
152
174
|
if (!Type.__fallbackType && typeDef.fallbackType) {
|
|
153
175
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
package/create/type.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { Option } from '@polkadot/types-codec';
|
|
4
|
-
import {
|
|
4
|
+
import { isHex, isU8a, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
5
5
|
import { createClassUnsafe } from "./class.js"; // With isPedantic, actually check that the encoding matches that supplied. This
|
|
6
6
|
// is much slower, but verifies that we have the correct types defined
|
|
7
7
|
|
|
@@ -12,7 +12,10 @@ function checkInstance(created, matcher) {
|
|
|
12
12
|
u8aEq(u8a, matcher) || // on a length-prefixed type, just check the actual length
|
|
13
13
|
['Bytes', 'Text', 'Type'].includes(rawType) && matcher.length === created.length || // when the created is empty and matcher is also empty, let it slide...
|
|
14
14
|
created.isEmpty && matcher.every(v => !v);
|
|
15
|
-
|
|
15
|
+
|
|
16
|
+
if (!isOk) {
|
|
17
|
+
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
|
+
}
|
|
16
19
|
}
|
|
17
20
|
|
|
18
21
|
function checkPedantic(created, [value]) {
|
package/package.json
CHANGED
|
@@ -20,7 +20,7 @@
|
|
|
20
20
|
"./cjs/detectPackage.js"
|
|
21
21
|
],
|
|
22
22
|
"type": "module",
|
|
23
|
-
"version": "8.8.3-
|
|
23
|
+
"version": "8.8.3-12",
|
|
24
24
|
"main": "./cjs/index.js",
|
|
25
25
|
"module": "./index.js",
|
|
26
26
|
"types": "./index.d.ts",
|
|
@@ -121,7 +121,7 @@
|
|
|
121
121
|
},
|
|
122
122
|
"dependencies": {
|
|
123
123
|
"@babel/runtime": "^7.18.3",
|
|
124
|
-
"@polkadot/types-codec": "8.8.3-
|
|
125
|
-
"@polkadot/util": "^9.4.
|
|
124
|
+
"@polkadot/types-codec": "8.8.3-12",
|
|
125
|
+
"@polkadot/util": "^9.4.2-22"
|
|
126
126
|
}
|
|
127
127
|
}
|
package/packageInfo.js
CHANGED
|
@@ -5,5 +5,5 @@ export const packageInfo = {
|
|
|
5
5
|
name: '@polkadot/types-create',
|
|
6
6
|
path: (import.meta && import.meta.url) ? new URL(import.meta.url).pathname.substring(0, new URL(import.meta.url).pathname.lastIndexOf('/') + 1) : 'auto',
|
|
7
7
|
type: 'esm',
|
|
8
|
-
version: '8.8.3-
|
|
8
|
+
version: '8.8.3-12'
|
|
9
9
|
};
|
package/util/encodeTypes.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
import {
|
|
3
|
+
import { isNumber, isUndefined, objectSpread, stringify } from '@polkadot/util';
|
|
4
4
|
import { TypeDefInfo } from "../types/index.js";
|
|
5
5
|
|
|
6
6
|
const stringIdentity = value => value.toString();
|
|
@@ -39,7 +39,11 @@ function encodeSubTypes(registry, sub, asEnum, extra) {
|
|
|
39
39
|
const names = sub.map(({
|
|
40
40
|
name
|
|
41
41
|
}) => name);
|
|
42
|
-
|
|
42
|
+
|
|
43
|
+
if (!names.every(n => !!n)) {
|
|
44
|
+
throw new Error(`Subtypes does not have consistent names, ${names.join(', ')}`);
|
|
45
|
+
}
|
|
46
|
+
|
|
43
47
|
const inner = objectSpread({}, extra);
|
|
44
48
|
|
|
45
49
|
for (let i = 0; i < sub.length; i++) {
|
|
@@ -66,9 +70,12 @@ const encoders = {
|
|
|
66
70
|
[TypeDefInfo.Enum]: (registry, {
|
|
67
71
|
sub
|
|
68
72
|
}) => {
|
|
69
|
-
|
|
73
|
+
if (!Array.isArray(sub)) {
|
|
74
|
+
throw new Error('Unable to encode Enum type');
|
|
75
|
+
} // c-like enums have all Null entries
|
|
70
76
|
// TODO We need to take the disciminant into account and auto-add empty entries
|
|
71
77
|
|
|
78
|
+
|
|
72
79
|
return sub.every(({
|
|
73
80
|
type
|
|
74
81
|
}) => type === 'Null') ? stringify({
|
|
@@ -98,7 +105,10 @@ const encoders = {
|
|
|
98
105
|
length = 8,
|
|
99
106
|
sub
|
|
100
107
|
}) => {
|
|
101
|
-
|
|
108
|
+
if (!Array.isArray(sub)) {
|
|
109
|
+
throw new Error('Unable to encode Set type');
|
|
110
|
+
}
|
|
111
|
+
|
|
102
112
|
return stringify({
|
|
103
113
|
_set: sub.reduce((all, {
|
|
104
114
|
index,
|
|
@@ -119,7 +129,10 @@ const encoders = {
|
|
|
119
129
|
alias,
|
|
120
130
|
sub
|
|
121
131
|
}) => {
|
|
122
|
-
|
|
132
|
+
if (!Array.isArray(sub)) {
|
|
133
|
+
throw new Error('Unable to encode Struct type');
|
|
134
|
+
}
|
|
135
|
+
|
|
123
136
|
return encodeSubTypes(registry, sub, false, alias ? {
|
|
124
137
|
_alias: [...alias.entries()].reduce((all, [k, v]) => objectSpread(all, {
|
|
125
138
|
[k]: v
|
|
@@ -129,7 +142,10 @@ const encoders = {
|
|
|
129
142
|
[TypeDefInfo.Tuple]: (registry, {
|
|
130
143
|
sub
|
|
131
144
|
}) => {
|
|
132
|
-
|
|
145
|
+
if (!Array.isArray(sub)) {
|
|
146
|
+
throw new Error('Unable to encode Tuple type');
|
|
147
|
+
}
|
|
148
|
+
|
|
133
149
|
return `(${sub.map(type => encodeTypeDef(registry, type)).join(',')})`;
|
|
134
150
|
},
|
|
135
151
|
[TypeDefInfo.UInt]: (registry, {
|
|
@@ -140,7 +156,10 @@ const encoders = {
|
|
|
140
156
|
length,
|
|
141
157
|
sub
|
|
142
158
|
}) => {
|
|
143
|
-
|
|
159
|
+
if (!isNumber(length) || !sub || Array.isArray(sub)) {
|
|
160
|
+
throw new Error('Unable to encode VecFixed type');
|
|
161
|
+
}
|
|
162
|
+
|
|
144
163
|
return `[${sub.type};${length}]`;
|
|
145
164
|
},
|
|
146
165
|
[TypeDefInfo.WrapperKeepOpaque]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'WrapperKeepOpaque'),
|
package/util/getTypeDef.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
|
+
import type { AnyString } from '@polkadot/types-codec/types';
|
|
1
2
|
import type { TypeDef } from '@polkadot/types-create/types';
|
|
2
3
|
interface TypeDefOptions {
|
|
3
4
|
name?: string;
|
|
4
5
|
displayName?: string;
|
|
5
6
|
}
|
|
6
|
-
export declare function getTypeDef(_type:
|
|
7
|
+
export declare function getTypeDef(_type: AnyString, { displayName, name }?: TypeDefOptions, count?: number): TypeDef;
|
|
7
8
|
export {};
|
package/util/getTypeDef.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
import { sanitize } from '@polkadot/types-codec';
|
|
4
|
-
import {
|
|
4
|
+
import { isNumber, isString, objectSpread } from '@polkadot/util';
|
|
5
5
|
import { TypeDefInfo } from "../types/index.js";
|
|
6
6
|
import { typeSplit } from "./typeSplit.js";
|
|
7
|
-
const MAX_NESTED = 64;
|
|
8
7
|
const KNOWN_INTERNALS = ['_alias', '_fallback'];
|
|
9
8
|
|
|
10
9
|
function getTypeString(typeOrObj) {
|
|
@@ -15,7 +14,10 @@ function isRustEnum(details) {
|
|
|
15
14
|
const values = Object.values(details);
|
|
16
15
|
|
|
17
16
|
if (values.some(v => isNumber(v))) {
|
|
18
|
-
|
|
17
|
+
if (!values.every(v => isNumber(v) && v >= 0 && v <= 255)) {
|
|
18
|
+
throw new Error('Invalid number-indexed enum definition');
|
|
19
|
+
}
|
|
20
|
+
|
|
19
21
|
return false;
|
|
20
22
|
}
|
|
21
23
|
|
|
@@ -98,21 +100,42 @@ function _decodeFixedVec(value, type, _, count) {
|
|
|
98
100
|
let inner = 0;
|
|
99
101
|
|
|
100
102
|
for (let i = 1; i < max && index === -1; i++) {
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
103
|
+
switch (type[i]) {
|
|
104
|
+
case ';':
|
|
105
|
+
{
|
|
106
|
+
if (inner === 0) {
|
|
107
|
+
index = i;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
case '[':
|
|
114
|
+
case '(':
|
|
115
|
+
case '<':
|
|
116
|
+
inner++;
|
|
117
|
+
break;
|
|
118
|
+
|
|
119
|
+
case ']':
|
|
120
|
+
case ')':
|
|
121
|
+
case '>':
|
|
122
|
+
inner--;
|
|
123
|
+
break;
|
|
107
124
|
}
|
|
108
125
|
}
|
|
109
126
|
|
|
110
|
-
|
|
127
|
+
if (index === -1) {
|
|
128
|
+
throw new Error(`${type}: Unable to extract location of ';'`);
|
|
129
|
+
}
|
|
130
|
+
|
|
111
131
|
const vecType = type.substring(1, index);
|
|
112
132
|
const [strLength, displayName] = type.substring(index + 1, max).split(';');
|
|
113
|
-
const length = parseInt(strLength.trim(), 10);
|
|
133
|
+
const length = parseInt(strLength.trim(), 10);
|
|
134
|
+
|
|
135
|
+
if (length > 2048) {
|
|
136
|
+
throw new Error(`${type}: Only support for [Type; <length>], where length <= 2048`);
|
|
137
|
+
}
|
|
114
138
|
|
|
115
|
-
assert(length <= 256, () => `${type}: Only support for [Type; <length>], where length <= 256`);
|
|
116
139
|
value.displayName = displayName;
|
|
117
140
|
value.length = length;
|
|
118
141
|
value.sub = getTypeDef(vecType, {}, count);
|
|
@@ -129,9 +152,12 @@ function _decodeTuple(value, _, subType, count) {
|
|
|
129
152
|
|
|
130
153
|
function _decodeAnyInt(value, type, _, clazz) {
|
|
131
154
|
const [strLength, displayName] = type.substring(clazz.length + 1, type.length - 1).split(',');
|
|
132
|
-
const length = parseInt(strLength.trim(), 10);
|
|
155
|
+
const length = parseInt(strLength.trim(), 10);
|
|
156
|
+
|
|
157
|
+
if (length > 8192 || length % 8) {
|
|
158
|
+
throw new Error(`${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
|
|
159
|
+
}
|
|
133
160
|
|
|
134
|
-
assert(length <= 8192 && length % 8 === 0, () => `${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
|
|
135
161
|
value.displayName = displayName;
|
|
136
162
|
value.length = length;
|
|
137
163
|
return value;
|
|
@@ -162,8 +188,7 @@ const wrappedExtraction = [['BTreeSet<', '>', TypeDefInfo.BTreeSet], ['Compact<'
|
|
|
162
188
|
|
|
163
189
|
function extractSubType(type, [start, end]) {
|
|
164
190
|
return type.substring(start.length, type.length - end.length);
|
|
165
|
-
}
|
|
166
|
-
|
|
191
|
+
}
|
|
167
192
|
|
|
168
193
|
export function getTypeDef(_type, {
|
|
169
194
|
displayName,
|
|
@@ -177,7 +202,11 @@ export function getTypeDef(_type, {
|
|
|
177
202
|
name,
|
|
178
203
|
type
|
|
179
204
|
};
|
|
180
|
-
|
|
205
|
+
|
|
206
|
+
if (++count > 64) {
|
|
207
|
+
throw new Error('getTypeDef: Maximum nested limit reached');
|
|
208
|
+
}
|
|
209
|
+
|
|
181
210
|
const nested = nestedExtraction.find(nested => hasWrapper(type, nested));
|
|
182
211
|
|
|
183
212
|
if (nested) {
|
package/util/typeSplit.js
CHANGED
|
@@ -1,69 +1,72 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
function isNotNested(...counters) {
|
|
6
|
-
return !counters.some(counter => counter !== 0);
|
|
7
|
-
} // safely split a string on ', ' while taking care of any nested occurences
|
|
8
|
-
|
|
9
|
-
|
|
3
|
+
// safely split a string on ', ' while taking care of any nested occurences
|
|
10
4
|
export function typeSplit(type) {
|
|
11
|
-
|
|
12
|
-
const result = [];
|
|
5
|
+
const result = []; // these are the depths of the various tokens: <, [, {, (
|
|
13
6
|
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
7
|
+
let c = 0;
|
|
8
|
+
let f = 0;
|
|
9
|
+
let s = 0;
|
|
10
|
+
let t = 0; // current start position
|
|
11
|
+
|
|
12
|
+
let start = 0;
|
|
20
13
|
|
|
21
|
-
for (let
|
|
22
|
-
switch (type[
|
|
14
|
+
for (let i = 0; i < type.length; i++) {
|
|
15
|
+
switch (type[i]) {
|
|
23
16
|
// if we are not nested, add the type
|
|
24
17
|
case ',':
|
|
25
|
-
|
|
26
|
-
|
|
18
|
+
{
|
|
19
|
+
if (!(c || f || s || t)) {
|
|
20
|
+
result.push(type.substring(start, i).trim());
|
|
21
|
+
start = i + 1;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
break;
|
|
25
|
+
}
|
|
27
26
|
// adjust compact/vec (and friends) depth
|
|
28
27
|
|
|
29
28
|
case '<':
|
|
30
|
-
|
|
29
|
+
c++;
|
|
31
30
|
break;
|
|
32
31
|
|
|
33
32
|
case '>':
|
|
34
|
-
|
|
33
|
+
c--;
|
|
35
34
|
break;
|
|
36
35
|
// adjust fixed vec depths
|
|
37
36
|
|
|
38
37
|
case '[':
|
|
39
|
-
|
|
38
|
+
f++;
|
|
40
39
|
break;
|
|
41
40
|
|
|
42
41
|
case ']':
|
|
43
|
-
|
|
42
|
+
f--;
|
|
44
43
|
break;
|
|
45
44
|
// adjust struct depth
|
|
46
45
|
|
|
47
46
|
case '{':
|
|
48
|
-
|
|
47
|
+
s++;
|
|
49
48
|
break;
|
|
50
49
|
|
|
51
50
|
case '}':
|
|
52
|
-
|
|
51
|
+
s--;
|
|
53
52
|
break;
|
|
54
53
|
// adjust tuple depth
|
|
55
54
|
|
|
56
55
|
case '(':
|
|
57
|
-
|
|
56
|
+
t++;
|
|
58
57
|
break;
|
|
59
58
|
|
|
60
59
|
case ')':
|
|
61
|
-
|
|
60
|
+
t--;
|
|
62
61
|
break;
|
|
63
62
|
}
|
|
64
|
-
}
|
|
63
|
+
} // ensure we have all the terminators taken care of
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
if (c || f || s || t) {
|
|
67
|
+
throw new Error(`Invalid definition (missing terminators) found in ${type}`);
|
|
68
|
+
} // the final leg of the journey
|
|
65
69
|
|
|
66
|
-
assert(isNotNested(cDepth, fDepth, sDepth, tDepth), () => `Invalid definition (missing terminators) found in ${type}`); // the final leg of the journey
|
|
67
70
|
|
|
68
71
|
result.push(type.substring(start, type.length).trim());
|
|
69
72
|
return result;
|