@polkadot/types-create 8.8.3-10 → 8.8.3-13
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 +23 -8
- package/cjs/util/typeSplit.js +4 -3
- 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.js +24 -9
- package/util/typeSplit.js +5 -3
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,9 +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
|
-
const MAX_FIX_LEN = 2048;
|
|
20
|
-
const MAX_BIT_LEN = 8192;
|
|
21
18
|
const KNOWN_INTERNALS = ['_alias', '_fallback'];
|
|
22
19
|
|
|
23
20
|
function getTypeString(typeOrObj) {
|
|
@@ -28,7 +25,10 @@ function isRustEnum(details) {
|
|
|
28
25
|
const values = Object.values(details);
|
|
29
26
|
|
|
30
27
|
if (values.some(v => (0, _util.isNumber)(v))) {
|
|
31
|
-
|
|
28
|
+
if (!values.every(v => (0, _util.isNumber)(v) && v >= 0 && v <= 255)) {
|
|
29
|
+
throw new Error('Invalid number-indexed enum definition');
|
|
30
|
+
}
|
|
31
|
+
|
|
32
32
|
return false;
|
|
33
33
|
}
|
|
34
34
|
|
|
@@ -147,11 +147,18 @@ function _decodeFixedVec(value, type, _, count) {
|
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
149
|
|
|
150
|
-
|
|
150
|
+
if (index === -1) {
|
|
151
|
+
throw new Error(`${type}: Unable to extract location of ';'`);
|
|
152
|
+
}
|
|
153
|
+
|
|
151
154
|
const vecType = type.substring(1, index);
|
|
152
155
|
const [strLength, displayName] = type.substring(index + 1, max).split(';');
|
|
153
156
|
const length = parseInt(strLength.trim(), 10);
|
|
154
|
-
|
|
157
|
+
|
|
158
|
+
if (length > 2048) {
|
|
159
|
+
throw new Error(`${type}: Only support for [Type; <length>], where length <= 2048`);
|
|
160
|
+
}
|
|
161
|
+
|
|
155
162
|
value.displayName = displayName;
|
|
156
163
|
value.length = length;
|
|
157
164
|
value.sub = getTypeDef(vecType, {}, count);
|
|
@@ -169,7 +176,11 @@ function _decodeTuple(value, _, subType, count) {
|
|
|
169
176
|
function _decodeAnyInt(value, type, _, clazz) {
|
|
170
177
|
const [strLength, displayName] = type.substring(clazz.length + 1, type.length - 1).split(',');
|
|
171
178
|
const length = parseInt(strLength.trim(), 10);
|
|
172
|
-
|
|
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
|
+
}
|
|
183
|
+
|
|
173
184
|
value.displayName = displayName;
|
|
174
185
|
value.length = length;
|
|
175
186
|
return value;
|
|
@@ -218,7 +229,11 @@ function getTypeDef(_type) {
|
|
|
218
229
|
name,
|
|
219
230
|
type
|
|
220
231
|
};
|
|
221
|
-
|
|
232
|
+
|
|
233
|
+
if (++count > 64) {
|
|
234
|
+
throw new Error('getTypeDef: Maximum nested limit reached');
|
|
235
|
+
}
|
|
236
|
+
|
|
222
237
|
const nested = nestedExtraction.find(nested => hasWrapper(type, nested));
|
|
223
238
|
|
|
224
239
|
if (nested) {
|
package/cjs/util/typeSplit.js
CHANGED
|
@@ -5,8 +5,6 @@ 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
10
|
// safely split a string on ', ' while taking care of any nested occurences
|
|
@@ -72,7 +70,10 @@ function typeSplit(type) {
|
|
|
72
70
|
} // ensure we have all the terminators taken care of
|
|
73
71
|
|
|
74
72
|
|
|
75
|
-
|
|
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
|
|
|
77
78
|
result.push(type.substring(start, type.length).trim());
|
|
78
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-13",
|
|
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.
|
|
124
|
+
"@polkadot/types-codec": "8.8.3-13",
|
|
125
|
+
"@polkadot/util": "^9.5.1"
|
|
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-13'
|
|
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.js
CHANGED
|
@@ -1,12 +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
|
-
const MAX_FIX_LEN = 2048;
|
|
9
|
-
const MAX_BIT_LEN = 8192;
|
|
10
7
|
const KNOWN_INTERNALS = ['_alias', '_fallback'];
|
|
11
8
|
|
|
12
9
|
function getTypeString(typeOrObj) {
|
|
@@ -17,7 +14,10 @@ function isRustEnum(details) {
|
|
|
17
14
|
const values = Object.values(details);
|
|
18
15
|
|
|
19
16
|
if (values.some(v => isNumber(v))) {
|
|
20
|
-
|
|
17
|
+
if (!values.every(v => isNumber(v) && v >= 0 && v <= 255)) {
|
|
18
|
+
throw new Error('Invalid number-indexed enum definition');
|
|
19
|
+
}
|
|
20
|
+
|
|
21
21
|
return false;
|
|
22
22
|
}
|
|
23
23
|
|
|
@@ -124,11 +124,18 @@ function _decodeFixedVec(value, type, _, count) {
|
|
|
124
124
|
}
|
|
125
125
|
}
|
|
126
126
|
|
|
127
|
-
|
|
127
|
+
if (index === -1) {
|
|
128
|
+
throw new Error(`${type}: Unable to extract location of ';'`);
|
|
129
|
+
}
|
|
130
|
+
|
|
128
131
|
const vecType = type.substring(1, index);
|
|
129
132
|
const [strLength, displayName] = type.substring(index + 1, max).split(';');
|
|
130
133
|
const length = parseInt(strLength.trim(), 10);
|
|
131
|
-
|
|
134
|
+
|
|
135
|
+
if (length > 2048) {
|
|
136
|
+
throw new Error(`${type}: Only support for [Type; <length>], where length <= 2048`);
|
|
137
|
+
}
|
|
138
|
+
|
|
132
139
|
value.displayName = displayName;
|
|
133
140
|
value.length = length;
|
|
134
141
|
value.sub = getTypeDef(vecType, {}, count);
|
|
@@ -146,7 +153,11 @@ function _decodeTuple(value, _, subType, count) {
|
|
|
146
153
|
function _decodeAnyInt(value, type, _, clazz) {
|
|
147
154
|
const [strLength, displayName] = type.substring(clazz.length + 1, type.length - 1).split(',');
|
|
148
155
|
const length = parseInt(strLength.trim(), 10);
|
|
149
|
-
|
|
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
|
+
}
|
|
160
|
+
|
|
150
161
|
value.displayName = displayName;
|
|
151
162
|
value.length = length;
|
|
152
163
|
return value;
|
|
@@ -191,7 +202,11 @@ export function getTypeDef(_type, {
|
|
|
191
202
|
name,
|
|
192
203
|
type
|
|
193
204
|
};
|
|
194
|
-
|
|
205
|
+
|
|
206
|
+
if (++count > 64) {
|
|
207
|
+
throw new Error('getTypeDef: Maximum nested limit reached');
|
|
208
|
+
}
|
|
209
|
+
|
|
195
210
|
const nested = nestedExtraction.find(nested => hasWrapper(type, nested));
|
|
196
211
|
|
|
197
212
|
if (nested) {
|
package/util/typeSplit.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
// Copyright 2017-2022 @polkadot/types-create authors & contributors
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
|
|
3
|
+
// safely split a string on ', ' while taking care of any nested occurences
|
|
5
4
|
export function typeSplit(type) {
|
|
6
5
|
const result = []; // these are the depths of the various tokens: <, [, {, (
|
|
7
6
|
|
|
@@ -64,7 +63,10 @@ export function typeSplit(type) {
|
|
|
64
63
|
} // ensure we have all the terminators taken care of
|
|
65
64
|
|
|
66
65
|
|
|
67
|
-
|
|
66
|
+
if (c || f || s || t) {
|
|
67
|
+
throw new Error(`Invalid definition (missing terminators) found in ${type}`);
|
|
68
|
+
} // the final leg of the journey
|
|
69
|
+
|
|
68
70
|
|
|
69
71
|
result.push(type.substring(start, type.length).trim());
|
|
70
72
|
return result;
|