@polkadot/types-create 9.14.2 → 10.0.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 +4 -7
- package/cjs/bundle.js +9 -46
- package/cjs/create/class.js +129 -172
- package/cjs/create/index.js +4 -26
- package/cjs/create/type.js +60 -75
- package/cjs/detectOther.js +3 -10
- package/cjs/detectPackage.js +6 -11
- package/cjs/index.js +3 -15
- package/cjs/packageInfo.js +2 -16
- package/cjs/types/augmentRegistry.js +1 -1
- package/cjs/types/index.js +4 -26
- package/cjs/types/lookup.js +2 -1
- package/cjs/types/types.js +26 -32
- package/cjs/util/encodeTypes.js +116 -199
- package/cjs/util/getTypeDef.js +166 -190
- package/cjs/util/index.js +6 -48
- package/cjs/util/typeSplit.js +57 -72
- package/cjs/util/xcm.js +7 -16
- package/create/class.js +121 -153
- package/create/index.js +2 -5
- package/create/type.js +52 -61
- package/detectOther.js +0 -3
- package/detectPackage.js +2 -7
- package/index.js +2 -5
- package/package.json +8 -5
- package/packageInfo.js +1 -11
- package/types/augmentRegistry.js +0 -5
- package/types/index.js +3 -6
- package/types/types.js +24 -27
- package/util/encodeTypes.js +108 -152
- package/util/getTypeDef.js +159 -165
- package/util/index.js +4 -7
- package/util/typeSplit.js +53 -66
- package/util/xcm.js +1 -6
package/util/encodeTypes.js
CHANGED
|
@@ -1,168 +1,124 @@
|
|
|
1
|
-
// Copyright 2017-2023 @polkadot/types-create authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
1
|
import { isNumber, isUndefined, objectSpread, stringify } from '@polkadot/util';
|
|
5
|
-
import { TypeDefInfo } from
|
|
6
|
-
const stringIdentity = value => value.toString();
|
|
2
|
+
import { TypeDefInfo } from '../types/index.js';
|
|
3
|
+
const stringIdentity = (value) => value.toString();
|
|
7
4
|
const INFO_WRAP = ['BTreeMap', 'BTreeSet', 'Compact', 'HashMap', 'Option', 'Result', 'Vec'];
|
|
8
5
|
export function paramsNotation(outer, inner, transform = stringIdentity) {
|
|
9
|
-
|
|
6
|
+
return `${outer}${inner
|
|
7
|
+
? `<${(Array.isArray(inner) ? inner : [inner]).map(transform).join(', ')}>`
|
|
8
|
+
: ''}`;
|
|
10
9
|
}
|
|
11
10
|
function encodeWithParams(registry, typeDef, outer) {
|
|
12
|
-
|
|
13
|
-
info
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
return paramsNotation(outer, sub, p => encodeTypeDef(registry, p));
|
|
30
|
-
}
|
|
31
|
-
throw new Error(`Unable to encode ${stringify(typeDef)} with params`);
|
|
11
|
+
const { info, sub } = typeDef;
|
|
12
|
+
switch (info) {
|
|
13
|
+
case TypeDefInfo.BTreeMap:
|
|
14
|
+
case TypeDefInfo.BTreeSet:
|
|
15
|
+
case TypeDefInfo.Compact:
|
|
16
|
+
case TypeDefInfo.HashMap:
|
|
17
|
+
case TypeDefInfo.Linkage:
|
|
18
|
+
case TypeDefInfo.Option:
|
|
19
|
+
case TypeDefInfo.Range:
|
|
20
|
+
case TypeDefInfo.RangeInclusive:
|
|
21
|
+
case TypeDefInfo.Result:
|
|
22
|
+
case TypeDefInfo.Vec:
|
|
23
|
+
case TypeDefInfo.WrapperKeepOpaque:
|
|
24
|
+
case TypeDefInfo.WrapperOpaque:
|
|
25
|
+
return paramsNotation(outer, sub, (p) => encodeTypeDef(registry, p));
|
|
26
|
+
}
|
|
27
|
+
throw new Error(`Unable to encode ${stringify(typeDef)} with params`);
|
|
32
28
|
}
|
|
33
29
|
function encodeSubTypes(registry, sub, asEnum, extra) {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
if (!names.every(n => !!n)) {
|
|
38
|
-
throw new Error(`Subtypes does not have consistent names, ${names.join(', ')}`);
|
|
39
|
-
}
|
|
40
|
-
const inner = objectSpread({}, extra);
|
|
41
|
-
for (let i = 0; i < sub.length; i++) {
|
|
42
|
-
const def = sub[i];
|
|
43
|
-
inner[def.name] = encodeTypeDef(registry, def);
|
|
44
|
-
}
|
|
45
|
-
return stringify(asEnum ? {
|
|
46
|
-
_enum: inner
|
|
47
|
-
} : inner);
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
// We setup a record here to ensure we have comprehensive coverage (any item not covered will result
|
|
51
|
-
// in a compile-time error with the missing index)
|
|
52
|
-
const encoders = {
|
|
53
|
-
[TypeDefInfo.BTreeMap]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'BTreeMap'),
|
|
54
|
-
[TypeDefInfo.BTreeSet]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'BTreeSet'),
|
|
55
|
-
[TypeDefInfo.Compact]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Compact'),
|
|
56
|
-
[TypeDefInfo.DoNotConstruct]: (registry, {
|
|
57
|
-
displayName,
|
|
58
|
-
lookupIndex,
|
|
59
|
-
lookupName
|
|
60
|
-
}) => `DoNotConstruct<${lookupName || displayName || (isUndefined(lookupIndex) ? 'Unknown' : registry.createLookupType(lookupIndex))}>`,
|
|
61
|
-
[TypeDefInfo.Enum]: (registry, {
|
|
62
|
-
sub
|
|
63
|
-
}) => {
|
|
64
|
-
if (!Array.isArray(sub)) {
|
|
65
|
-
throw new Error('Unable to encode Enum type');
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
// c-like enums have all Null entries
|
|
69
|
-
// TODO We need to take the disciminant into account and auto-add empty entries
|
|
70
|
-
return sub.every(({
|
|
71
|
-
type
|
|
72
|
-
}) => type === 'Null') ? stringify({
|
|
73
|
-
_enum: sub.map(({
|
|
74
|
-
name
|
|
75
|
-
}, index) => `${name || `Empty${index}`}`)
|
|
76
|
-
}) : encodeSubTypes(registry, sub, true);
|
|
77
|
-
},
|
|
78
|
-
[TypeDefInfo.HashMap]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'HashMap'),
|
|
79
|
-
[TypeDefInfo.Int]: (registry, {
|
|
80
|
-
length = 32
|
|
81
|
-
}) => `Int<${length}>`,
|
|
82
|
-
[TypeDefInfo.Linkage]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Linkage'),
|
|
83
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
84
|
-
[TypeDefInfo.Null]: (registry, typeDef) => 'Null',
|
|
85
|
-
[TypeDefInfo.Option]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Option'),
|
|
86
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
87
|
-
[TypeDefInfo.Plain]: (registry, {
|
|
88
|
-
displayName,
|
|
89
|
-
type
|
|
90
|
-
}) => displayName || type,
|
|
91
|
-
[TypeDefInfo.Range]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Range'),
|
|
92
|
-
[TypeDefInfo.RangeInclusive]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'RangeInclusive'),
|
|
93
|
-
[TypeDefInfo.Result]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Result'),
|
|
94
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
95
|
-
[TypeDefInfo.Set]: (registry, {
|
|
96
|
-
length = 8,
|
|
97
|
-
sub
|
|
98
|
-
}) => {
|
|
99
|
-
if (!Array.isArray(sub)) {
|
|
100
|
-
throw new Error('Unable to encode Set type');
|
|
101
|
-
}
|
|
102
|
-
return stringify({
|
|
103
|
-
_set: sub.reduce((all, {
|
|
104
|
-
index,
|
|
105
|
-
name
|
|
106
|
-
}, count) => objectSpread(all, {
|
|
107
|
-
[`${name || `Unknown${index || count}`}`]: index || count
|
|
108
|
-
}), {
|
|
109
|
-
_bitLength: length || 8
|
|
110
|
-
})
|
|
111
|
-
});
|
|
112
|
-
},
|
|
113
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
114
|
-
[TypeDefInfo.Si]: (registry, {
|
|
115
|
-
lookupName,
|
|
116
|
-
type
|
|
117
|
-
}) => lookupName || type,
|
|
118
|
-
[TypeDefInfo.Struct]: (registry, {
|
|
119
|
-
alias,
|
|
120
|
-
sub
|
|
121
|
-
}) => {
|
|
122
|
-
if (!Array.isArray(sub)) {
|
|
123
|
-
throw new Error('Unable to encode Struct type');
|
|
124
|
-
}
|
|
125
|
-
return encodeSubTypes(registry, sub, false, alias ? {
|
|
126
|
-
_alias: [...alias.entries()].reduce((all, [k, v]) => objectSpread(all, {
|
|
127
|
-
[k]: v
|
|
128
|
-
}), {})
|
|
129
|
-
} : {});
|
|
130
|
-
},
|
|
131
|
-
[TypeDefInfo.Tuple]: (registry, {
|
|
132
|
-
sub
|
|
133
|
-
}) => {
|
|
134
|
-
if (!Array.isArray(sub)) {
|
|
135
|
-
throw new Error('Unable to encode Tuple type');
|
|
30
|
+
const names = sub.map(({ name }) => name);
|
|
31
|
+
if (!names.every((n) => !!n)) {
|
|
32
|
+
throw new Error(`Subtypes does not have consistent names, ${names.join(', ')}`);
|
|
136
33
|
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
}) => `UInt<${length}>`,
|
|
142
|
-
[TypeDefInfo.Vec]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Vec'),
|
|
143
|
-
[TypeDefInfo.VecFixed]: (registry, {
|
|
144
|
-
length,
|
|
145
|
-
sub
|
|
146
|
-
}) => {
|
|
147
|
-
if (!isNumber(length) || !sub || Array.isArray(sub)) {
|
|
148
|
-
throw new Error('Unable to encode VecFixed type');
|
|
34
|
+
const inner = objectSpread({}, extra);
|
|
35
|
+
for (let i = 0; i < sub.length; i++) {
|
|
36
|
+
const def = sub[i];
|
|
37
|
+
inner[def.name] = encodeTypeDef(registry, def);
|
|
149
38
|
}
|
|
150
|
-
return
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
39
|
+
return stringify(asEnum
|
|
40
|
+
? { _enum: inner }
|
|
41
|
+
: inner);
|
|
42
|
+
}
|
|
43
|
+
const encoders = {
|
|
44
|
+
[TypeDefInfo.BTreeMap]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'BTreeMap'),
|
|
45
|
+
[TypeDefInfo.BTreeSet]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'BTreeSet'),
|
|
46
|
+
[TypeDefInfo.Compact]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Compact'),
|
|
47
|
+
[TypeDefInfo.DoNotConstruct]: (registry, { displayName, lookupIndex, lookupName }) => `DoNotConstruct<${lookupName || displayName || (isUndefined(lookupIndex) ? 'Unknown' : registry.createLookupType(lookupIndex))}>`,
|
|
48
|
+
[TypeDefInfo.Enum]: (registry, { sub }) => {
|
|
49
|
+
if (!Array.isArray(sub)) {
|
|
50
|
+
throw new Error('Unable to encode Enum type');
|
|
51
|
+
}
|
|
52
|
+
// c-like enums have all Null entries
|
|
53
|
+
// TODO We need to take the disciminant into account and auto-add empty entries
|
|
54
|
+
return sub.every(({ type }) => type === 'Null')
|
|
55
|
+
? stringify({ _enum: sub.map(({ name }, index) => `${name || `Empty${index}`}`) })
|
|
56
|
+
: encodeSubTypes(registry, sub, true);
|
|
57
|
+
},
|
|
58
|
+
[TypeDefInfo.HashMap]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'HashMap'),
|
|
59
|
+
[TypeDefInfo.Int]: (registry, { length = 32 }) => `Int<${length}>`,
|
|
60
|
+
[TypeDefInfo.Linkage]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Linkage'),
|
|
61
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
62
|
+
[TypeDefInfo.Null]: (registry, typeDef) => 'Null',
|
|
63
|
+
[TypeDefInfo.Option]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Option'),
|
|
64
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
65
|
+
[TypeDefInfo.Plain]: (registry, { displayName, type }) => displayName || type,
|
|
66
|
+
[TypeDefInfo.Range]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Range'),
|
|
67
|
+
[TypeDefInfo.RangeInclusive]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'RangeInclusive'),
|
|
68
|
+
[TypeDefInfo.Result]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Result'),
|
|
69
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
70
|
+
[TypeDefInfo.Set]: (registry, { length = 8, sub }) => {
|
|
71
|
+
if (!Array.isArray(sub)) {
|
|
72
|
+
throw new Error('Unable to encode Set type');
|
|
73
|
+
}
|
|
74
|
+
return stringify({
|
|
75
|
+
_set: sub.reduce((all, { index, name }, count) => objectSpread(all, { [`${name || `Unknown${index || count}`}`]: index || count }), { _bitLength: length || 8 })
|
|
76
|
+
});
|
|
77
|
+
},
|
|
78
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
79
|
+
[TypeDefInfo.Si]: (registry, { lookupName, type }) => lookupName || type,
|
|
80
|
+
[TypeDefInfo.Struct]: (registry, { alias, sub }) => {
|
|
81
|
+
if (!Array.isArray(sub)) {
|
|
82
|
+
throw new Error('Unable to encode Struct type');
|
|
83
|
+
}
|
|
84
|
+
return encodeSubTypes(registry, sub, false, alias
|
|
85
|
+
? {
|
|
86
|
+
_alias: [...alias.entries()].reduce((all, [k, v]) => objectSpread(all, { [k]: v }), {})
|
|
87
|
+
}
|
|
88
|
+
: {});
|
|
89
|
+
},
|
|
90
|
+
[TypeDefInfo.Tuple]: (registry, { sub }) => {
|
|
91
|
+
if (!Array.isArray(sub)) {
|
|
92
|
+
throw new Error('Unable to encode Tuple type');
|
|
93
|
+
}
|
|
94
|
+
return `(${sub.map((type) => encodeTypeDef(registry, type)).join(',')})`;
|
|
95
|
+
},
|
|
96
|
+
[TypeDefInfo.UInt]: (registry, { length = 32 }) => `UInt<${length}>`,
|
|
97
|
+
[TypeDefInfo.Vec]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'Vec'),
|
|
98
|
+
[TypeDefInfo.VecFixed]: (registry, { length, sub }) => {
|
|
99
|
+
if (!isNumber(length) || !sub || Array.isArray(sub)) {
|
|
100
|
+
throw new Error('Unable to encode VecFixed type');
|
|
101
|
+
}
|
|
102
|
+
return `[${sub.type};${length}]`;
|
|
103
|
+
},
|
|
104
|
+
[TypeDefInfo.WrapperKeepOpaque]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'WrapperKeepOpaque'),
|
|
105
|
+
[TypeDefInfo.WrapperOpaque]: (registry, typeDef) => encodeWithParams(registry, typeDef, 'WrapperOpaque')
|
|
154
106
|
};
|
|
155
107
|
function encodeType(registry, typeDef, withLookup = true) {
|
|
156
|
-
|
|
108
|
+
return withLookup && typeDef.lookupName
|
|
109
|
+
? typeDef.lookupName
|
|
110
|
+
: encoders[typeDef.info](registry, typeDef);
|
|
157
111
|
}
|
|
158
112
|
export function encodeTypeDef(registry, typeDef) {
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
113
|
+
// In the case of contracts we do have the unfortunate situation where the displayName would
|
|
114
|
+
// refer to "Option" when it is an option. For these, string it out, only using when actually
|
|
115
|
+
// not a top-level element to be used
|
|
116
|
+
return (typeDef.displayName && !INFO_WRAP.some((i) => typeDef.displayName === i))
|
|
117
|
+
? typeDef.displayName
|
|
118
|
+
: encodeType(registry, typeDef);
|
|
163
119
|
}
|
|
164
120
|
export function withTypeString(registry, typeDef) {
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
121
|
+
return objectSpread({}, typeDef, {
|
|
122
|
+
type: encodeType(registry, typeDef, false)
|
|
123
|
+
});
|
|
168
124
|
}
|
package/util/getTypeDef.js
CHANGED
|
@@ -1,198 +1,192 @@
|
|
|
1
|
-
// Copyright 2017-2023 @polkadot/types-create authors & contributors
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
1
|
import { sanitize } from '@polkadot/types-codec';
|
|
5
2
|
import { isNumber, isString, objectSpread } from '@polkadot/util';
|
|
6
|
-
import { TypeDefInfo } from
|
|
7
|
-
import { typeSplit } from
|
|
3
|
+
import { TypeDefInfo } from '../types/index.js';
|
|
4
|
+
import { typeSplit } from './typeSplit.js';
|
|
8
5
|
const KNOWN_INTERNALS = ['_alias', '_fallback'];
|
|
9
6
|
function getTypeString(typeOrObj) {
|
|
10
|
-
|
|
7
|
+
return isString(typeOrObj)
|
|
8
|
+
? typeOrObj.toString()
|
|
9
|
+
: JSON.stringify(typeOrObj);
|
|
11
10
|
}
|
|
12
11
|
function isRustEnum(details) {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
12
|
+
const values = Object.values(details);
|
|
13
|
+
if (values.some((v) => isNumber(v))) {
|
|
14
|
+
if (!values.every((v) => isNumber(v) && v >= 0 && v <= 255)) {
|
|
15
|
+
throw new Error('Invalid number-indexed enum definition');
|
|
16
|
+
}
|
|
17
|
+
return false;
|
|
17
18
|
}
|
|
18
|
-
return
|
|
19
|
-
}
|
|
20
|
-
return true;
|
|
19
|
+
return true;
|
|
21
20
|
}
|
|
22
|
-
|
|
23
|
-
// decode an enum of either of the following forms
|
|
24
|
-
// { _enum: ['A', 'B', 'C'] }
|
|
25
|
-
// { _enum: { A: AccountId, B: Balance, C: u32 } }
|
|
26
|
-
// { _enum: { A: 1, B: 2 } }
|
|
27
21
|
function _decodeEnum(value, details, count, fallbackType) {
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
}));
|
|
52
|
-
}
|
|
53
|
-
return value;
|
|
22
|
+
value.info = TypeDefInfo.Enum;
|
|
23
|
+
value.fallbackType = fallbackType;
|
|
24
|
+
// not as pretty, but remain compatible with oo7 for both struct and Array types
|
|
25
|
+
if (Array.isArray(details)) {
|
|
26
|
+
value.sub = details.map((name, index) => ({
|
|
27
|
+
index,
|
|
28
|
+
info: TypeDefInfo.Plain,
|
|
29
|
+
name,
|
|
30
|
+
type: 'Null'
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
else if (isRustEnum(details)) {
|
|
34
|
+
value.sub = Object.entries(details).map(([name, typeOrObj], index) => objectSpread({}, getTypeDef(getTypeString(typeOrObj || 'Null'), { name }, count), { index }));
|
|
35
|
+
}
|
|
36
|
+
else {
|
|
37
|
+
value.sub = Object.entries(details).map(([name, index]) => ({
|
|
38
|
+
index,
|
|
39
|
+
info: TypeDefInfo.Plain,
|
|
40
|
+
name,
|
|
41
|
+
type: 'Null'
|
|
42
|
+
}));
|
|
43
|
+
}
|
|
44
|
+
return value;
|
|
54
45
|
}
|
|
55
|
-
|
|
56
|
-
// decode a set of the form
|
|
57
|
-
// { _set: { A: 0b0001, B: 0b0010, C: 0b0100 } }
|
|
58
46
|
function _decodeSet(value, details, fallbackType) {
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
47
|
+
value.info = TypeDefInfo.Set;
|
|
48
|
+
value.fallbackType = fallbackType;
|
|
49
|
+
value.length = details._bitLength;
|
|
50
|
+
value.sub = Object
|
|
51
|
+
.entries(details)
|
|
52
|
+
.filter(([name]) => !name.startsWith('_'))
|
|
53
|
+
.map(([name, index]) => ({
|
|
54
|
+
index,
|
|
55
|
+
info: TypeDefInfo.Plain,
|
|
56
|
+
name,
|
|
57
|
+
type: 'Null'
|
|
58
|
+
}));
|
|
59
|
+
return value;
|
|
69
60
|
}
|
|
70
|
-
|
|
71
|
-
// decode a struct, set or enum
|
|
72
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
73
61
|
function _decodeStruct(value, type, _, count) {
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
62
|
+
const parsed = JSON.parse(type);
|
|
63
|
+
const keys = Object.keys(parsed);
|
|
64
|
+
if (keys.includes('_enum')) {
|
|
65
|
+
return _decodeEnum(value, parsed._enum, count, parsed._fallback);
|
|
66
|
+
}
|
|
67
|
+
else if (keys.includes('_set')) {
|
|
68
|
+
return _decodeSet(value, parsed._set, parsed._fallback);
|
|
69
|
+
}
|
|
70
|
+
value.alias = parsed._alias
|
|
71
|
+
? new Map(Object.entries(parsed._alias))
|
|
72
|
+
: undefined;
|
|
73
|
+
value.fallbackType = parsed._fallback;
|
|
74
|
+
value.sub = keys
|
|
75
|
+
.filter((name) => !KNOWN_INTERNALS.includes(name))
|
|
76
|
+
.map((name) => getTypeDef(getTypeString(parsed[name]), { name }, count));
|
|
77
|
+
return value;
|
|
87
78
|
}
|
|
88
|
-
|
|
89
|
-
// decode a fixed vector, e.g. [u8;32]
|
|
90
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
91
79
|
function _decodeFixedVec(value, type, _, count) {
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
80
|
+
const max = type.length - 1;
|
|
81
|
+
let index = -1;
|
|
82
|
+
let inner = 0;
|
|
83
|
+
for (let i = 1; (i < max) && (index === -1); i++) {
|
|
84
|
+
switch (type[i]) {
|
|
85
|
+
case ';': {
|
|
86
|
+
if (inner === 0) {
|
|
87
|
+
index = i;
|
|
88
|
+
}
|
|
89
|
+
break;
|
|
90
|
+
}
|
|
91
|
+
case '[':
|
|
92
|
+
case '(':
|
|
93
|
+
case '<':
|
|
94
|
+
inner++;
|
|
95
|
+
break;
|
|
96
|
+
case ']':
|
|
97
|
+
case ')':
|
|
98
|
+
case '>':
|
|
99
|
+
inner--;
|
|
100
|
+
break;
|
|
103
101
|
}
|
|
104
|
-
case '[':
|
|
105
|
-
case '(':
|
|
106
|
-
case '<':
|
|
107
|
-
inner++;
|
|
108
|
-
break;
|
|
109
|
-
case ']':
|
|
110
|
-
case ')':
|
|
111
|
-
case '>':
|
|
112
|
-
inner--;
|
|
113
|
-
break;
|
|
114
102
|
}
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
return value;
|
|
103
|
+
if (index === -1) {
|
|
104
|
+
throw new Error(`${type}: Unable to extract location of ';'`);
|
|
105
|
+
}
|
|
106
|
+
const vecType = type.substring(1, index);
|
|
107
|
+
const [strLength, displayName] = type.substring(index + 1, max).split(';');
|
|
108
|
+
const length = parseInt(strLength.trim(), 10);
|
|
109
|
+
if (length > 2048) {
|
|
110
|
+
throw new Error(`${type}: Only support for [Type; <length>], where length <= 2048`);
|
|
111
|
+
}
|
|
112
|
+
value.displayName = displayName;
|
|
113
|
+
value.length = length;
|
|
114
|
+
value.sub = getTypeDef(vecType, {}, count);
|
|
115
|
+
return value;
|
|
129
116
|
}
|
|
130
|
-
|
|
131
|
-
// decode a tuple
|
|
132
117
|
function _decodeTuple(value, _, subType, count) {
|
|
133
|
-
|
|
134
|
-
|
|
118
|
+
value.sub = subType.length === 0
|
|
119
|
+
? []
|
|
120
|
+
: typeSplit(subType).map((inner) => getTypeDef(inner, {}, count));
|
|
121
|
+
return value;
|
|
135
122
|
}
|
|
136
|
-
|
|
137
|
-
// decode a Int/UInt<bitLength[, name]>
|
|
138
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
139
123
|
function _decodeAnyInt(value, type, _, clazz) {
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
124
|
+
const [strLength, displayName] = type.substring(clazz.length + 1, type.length - 1).split(',');
|
|
125
|
+
const length = parseInt(strLength.trim(), 10);
|
|
126
|
+
if ((length > 8192) || (length % 8)) {
|
|
127
|
+
throw new Error(`${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
|
|
128
|
+
}
|
|
129
|
+
value.displayName = displayName;
|
|
130
|
+
value.length = length;
|
|
131
|
+
return value;
|
|
148
132
|
}
|
|
149
133
|
function _decodeInt(value, type, subType) {
|
|
150
|
-
|
|
134
|
+
return _decodeAnyInt(value, type, subType, 'Int');
|
|
151
135
|
}
|
|
152
136
|
function _decodeUInt(value, type, subType) {
|
|
153
|
-
|
|
137
|
+
return _decodeAnyInt(value, type, subType, 'UInt');
|
|
154
138
|
}
|
|
155
|
-
|
|
156
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
157
139
|
function _decodeDoNotConstruct(value, type, _) {
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
140
|
+
const NAME_LENGTH = 'DoNotConstruct'.length;
|
|
141
|
+
value.displayName = type.substring(NAME_LENGTH + 1, type.length - 1);
|
|
142
|
+
return value;
|
|
161
143
|
}
|
|
162
144
|
function hasWrapper(type, [start, end]) {
|
|
163
|
-
|
|
145
|
+
return (type.substring(0, start.length) === start) && (type.slice(-1 * end.length) === end);
|
|
164
146
|
}
|
|
165
|
-
const nestedExtraction = [
|
|
166
|
-
|
|
167
|
-
['
|
|
168
|
-
|
|
147
|
+
const nestedExtraction = [
|
|
148
|
+
['[', ']', TypeDefInfo.VecFixed, _decodeFixedVec],
|
|
149
|
+
['{', '}', TypeDefInfo.Struct, _decodeStruct],
|
|
150
|
+
['(', ')', TypeDefInfo.Tuple, _decodeTuple],
|
|
151
|
+
// the inner for these are the same as tuple, multiple values
|
|
152
|
+
['BTreeMap<', '>', TypeDefInfo.BTreeMap, _decodeTuple],
|
|
153
|
+
['HashMap<', '>', TypeDefInfo.HashMap, _decodeTuple],
|
|
154
|
+
['Int<', '>', TypeDefInfo.Int, _decodeInt],
|
|
155
|
+
['Result<', '>', TypeDefInfo.Result, _decodeTuple],
|
|
156
|
+
['UInt<', '>', TypeDefInfo.UInt, _decodeUInt],
|
|
157
|
+
['DoNotConstruct<', '>', TypeDefInfo.DoNotConstruct, _decodeDoNotConstruct]
|
|
158
|
+
];
|
|
159
|
+
const wrappedExtraction = [
|
|
160
|
+
['BTreeSet<', '>', TypeDefInfo.BTreeSet],
|
|
161
|
+
['Compact<', '>', TypeDefInfo.Compact],
|
|
162
|
+
['Linkage<', '>', TypeDefInfo.Linkage],
|
|
163
|
+
['Opaque<', '>', TypeDefInfo.WrapperOpaque],
|
|
164
|
+
['Option<', '>', TypeDefInfo.Option],
|
|
165
|
+
['Range<', '>', TypeDefInfo.Range],
|
|
166
|
+
['RangeInclusive<', '>', TypeDefInfo.RangeInclusive],
|
|
167
|
+
['Vec<', '>', TypeDefInfo.Vec],
|
|
168
|
+
['WrapperKeepOpaque<', '>', TypeDefInfo.WrapperKeepOpaque],
|
|
169
|
+
['WrapperOpaque<', '>', TypeDefInfo.WrapperOpaque]
|
|
170
|
+
];
|
|
169
171
|
function extractSubType(type, [start, end]) {
|
|
170
|
-
|
|
172
|
+
return type.substring(start.length, type.length - end.length);
|
|
171
173
|
}
|
|
172
|
-
export function getTypeDef(_type, {
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
value
|
|
190
|
-
return nested[3](value, type, extractSubType(type, nested), count);
|
|
191
|
-
}
|
|
192
|
-
const wrapped = wrappedExtraction.find(wrapped => hasWrapper(type, wrapped));
|
|
193
|
-
if (wrapped) {
|
|
194
|
-
value.info = wrapped[2];
|
|
195
|
-
value.sub = getTypeDef(extractSubType(type, wrapped), {}, count);
|
|
196
|
-
}
|
|
197
|
-
return value;
|
|
174
|
+
export function getTypeDef(_type, { displayName, name } = {}, count = 0) {
|
|
175
|
+
// create the type via Type, allowing types to be sanitized
|
|
176
|
+
const type = sanitize(_type);
|
|
177
|
+
const value = { displayName, info: TypeDefInfo.Plain, name, type };
|
|
178
|
+
if (++count > 64) {
|
|
179
|
+
throw new Error('getTypeDef: Maximum nested limit reached');
|
|
180
|
+
}
|
|
181
|
+
const nested = nestedExtraction.find((nested) => hasWrapper(type, nested));
|
|
182
|
+
if (nested) {
|
|
183
|
+
value.info = nested[2];
|
|
184
|
+
return nested[3](value, type, extractSubType(type, nested), count);
|
|
185
|
+
}
|
|
186
|
+
const wrapped = wrappedExtraction.find((wrapped) => hasWrapper(type, wrapped));
|
|
187
|
+
if (wrapped) {
|
|
188
|
+
value.info = wrapped[2];
|
|
189
|
+
value.sub = getTypeDef(extractSubType(type, wrapped), {}, count);
|
|
190
|
+
}
|
|
191
|
+
return value;
|
|
198
192
|
}
|
package/util/index.js
CHANGED
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
export * from
|
|
5
|
-
export * from "./getTypeDef.js";
|
|
6
|
-
export * from "./typeSplit.js";
|
|
7
|
-
export * from "./xcm.js";
|
|
1
|
+
export * from './encodeTypes.js';
|
|
2
|
+
export * from './getTypeDef.js';
|
|
3
|
+
export * from './typeSplit.js';
|
|
4
|
+
export * from './xcm.js';
|