@polkadot/types-create 6.12.2-3
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.cjs +53 -0
- package/bundle.d.ts +4 -0
- package/bundle.js +6 -0
- package/create/class.cjs +185 -0
- package/create/class.d.ts +5 -0
- package/create/class.js +160 -0
- package/create/index.cjs +31 -0
- package/create/index.d.ts +2 -0
- package/create/index.js +4 -0
- package/create/type.cjs +85 -0
- package/create/type.d.ts +3 -0
- package/create/type.js +68 -0
- package/detectPackage.cjs +11 -0
- package/detectPackage.d.ts +1 -0
- package/detectPackage.js +6 -0
- package/index.cjs +20 -0
- package/index.d.ts +2 -0
- package/index.js +4 -0
- package/package.json +92 -0
- package/packageInfo.cjs +14 -0
- package/packageInfo.d.ts +4 -0
- package/packageInfo.js +7 -0
- package/types.cjs +34 -0
- package/types.d.ts +57 -0
- package/types.js +27 -0
- package/util/encodeTypes.cjs +208 -0
- package/util/encodeTypes.d.ts +5 -0
- package/util/encodeTypes.js +159 -0
- package/util/getTypeDef.cjs +228 -0
- package/util/getTypeDef.d.ts +7 -0
- package/util/getTypeDef.js +201 -0
- package/util/index.cjs +44 -0
- package/util/index.d.ts +3 -0
- package/util/index.js +5 -0
- package/util/typeSplit.cjs +81 -0
- package/util/typeSplit.d.ts +1 -0
- package/util/typeSplit.js +70 -0
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.getTypeDef = getTypeDef;
|
|
7
|
+
|
|
8
|
+
var _typesCodec = require("@polkadot/types-codec");
|
|
9
|
+
|
|
10
|
+
var _util = require("@polkadot/util");
|
|
11
|
+
|
|
12
|
+
var _types = require("../types.cjs");
|
|
13
|
+
|
|
14
|
+
var _typeSplit = require("./typeSplit.cjs");
|
|
15
|
+
|
|
16
|
+
// Copyright 2017-2021 @polkadot/types-create authors & contributors
|
|
17
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
18
|
+
const MAX_NESTED = 64;
|
|
19
|
+
const KNOWN_INTERNALS = ['_alias', '_fallback'];
|
|
20
|
+
|
|
21
|
+
function getTypeString(typeOrObj) {
|
|
22
|
+
return (0, _util.isString)(typeOrObj) ? typeOrObj.toString() : JSON.stringify(typeOrObj);
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function isRustEnum(details) {
|
|
26
|
+
const values = Object.values(details);
|
|
27
|
+
|
|
28
|
+
if (values.some(v => (0, _util.isNumber)(v))) {
|
|
29
|
+
(0, _util.assert)(values.every(v => (0, _util.isNumber)(v) && v >= 0 && v <= 255), 'Invalid number-indexed enum definition');
|
|
30
|
+
return false;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
return true;
|
|
34
|
+
} // decode an enum of either of the following forms
|
|
35
|
+
// { _enum: ['A', 'B', 'C'] }
|
|
36
|
+
// { _enum: { A: AccountId, B: Balance, C: u32 } }
|
|
37
|
+
// { _enum: { A: 1, B: 2 } }
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
function _decodeEnum(value, details, count) {
|
|
41
|
+
value.info = _types.TypeDefInfo.Enum; // not as pretty, but remain compatible with oo7 for both struct and Array types
|
|
42
|
+
|
|
43
|
+
if (Array.isArray(details)) {
|
|
44
|
+
value.sub = details.map((name, index) => ({
|
|
45
|
+
index,
|
|
46
|
+
info: _types.TypeDefInfo.Plain,
|
|
47
|
+
name,
|
|
48
|
+
type: 'Null'
|
|
49
|
+
}));
|
|
50
|
+
} else if (isRustEnum(details)) {
|
|
51
|
+
value.sub = Object.entries(details).map((_ref, index) => {
|
|
52
|
+
let [name, typeOrObj] = _ref;
|
|
53
|
+
return (0, _util.objectSpread)({}, getTypeDef(getTypeString(typeOrObj || 'Null'), {
|
|
54
|
+
name
|
|
55
|
+
}, count), {
|
|
56
|
+
index
|
|
57
|
+
});
|
|
58
|
+
});
|
|
59
|
+
} else {
|
|
60
|
+
value.sub = Object.entries(details).map(_ref2 => {
|
|
61
|
+
let [name, index] = _ref2;
|
|
62
|
+
return {
|
|
63
|
+
index,
|
|
64
|
+
info: _types.TypeDefInfo.Plain,
|
|
65
|
+
name,
|
|
66
|
+
type: 'Null'
|
|
67
|
+
};
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
return value;
|
|
72
|
+
} // decode a set of the form
|
|
73
|
+
// { _set: { A: 0b0001, B: 0b0010, C: 0b0100 } }
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
function _decodeSet(value, details) {
|
|
77
|
+
value.info = _types.TypeDefInfo.Set;
|
|
78
|
+
value.length = details._bitLength;
|
|
79
|
+
value.sub = Object.entries(details).filter(_ref3 => {
|
|
80
|
+
let [name] = _ref3;
|
|
81
|
+
return !name.startsWith('_');
|
|
82
|
+
}).map(_ref4 => {
|
|
83
|
+
let [name, index] = _ref4;
|
|
84
|
+
return {
|
|
85
|
+
index,
|
|
86
|
+
info: _types.TypeDefInfo.Plain,
|
|
87
|
+
name,
|
|
88
|
+
type: 'Null'
|
|
89
|
+
};
|
|
90
|
+
});
|
|
91
|
+
return value;
|
|
92
|
+
} // decode a struct, set or enum
|
|
93
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
function _decodeStruct(value, type, _, count) {
|
|
97
|
+
const parsed = JSON.parse(type);
|
|
98
|
+
const keys = Object.keys(parsed);
|
|
99
|
+
|
|
100
|
+
if (keys.length === 1 && keys[0] === '_enum') {
|
|
101
|
+
return _decodeEnum(value, parsed[keys[0]], count);
|
|
102
|
+
} else if (keys.length === 1 && keys[0] === '_set') {
|
|
103
|
+
return _decodeSet(value, parsed[keys[0]]);
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
value.alias = parsed._alias ? new Map(Object.entries(parsed._alias)) : undefined;
|
|
107
|
+
value.fallbackType = parsed._fallback;
|
|
108
|
+
value.sub = keys.filter(name => !KNOWN_INTERNALS.includes(name)).map(name => getTypeDef(getTypeString(parsed[name]), {
|
|
109
|
+
name
|
|
110
|
+
}, count));
|
|
111
|
+
return value;
|
|
112
|
+
} // decode a fixed vector, e.g. [u8;32]
|
|
113
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
function _decodeFixedVec(value, type, _, count) {
|
|
117
|
+
const max = type.length - 1;
|
|
118
|
+
let index = -1;
|
|
119
|
+
let inner = 0;
|
|
120
|
+
|
|
121
|
+
for (let i = 1; i < max && index === -1; i++) {
|
|
122
|
+
if (type[i] === ';' && inner === 0) {
|
|
123
|
+
index = i;
|
|
124
|
+
} else if (['[', '(', '<'].includes(type[i])) {
|
|
125
|
+
inner++;
|
|
126
|
+
} else if ([']', ')', '>'].includes(type[i])) {
|
|
127
|
+
inner--;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
(0, _util.assert)(index !== -1, () => `${type}: Unable to extract location of ';'`);
|
|
132
|
+
const vecType = type.substr(1, index - 1);
|
|
133
|
+
const [strLength, displayName] = type.substr(index + 1, max - index - 1).split(';');
|
|
134
|
+
const length = parseInt(strLength.trim(), 10); // as a first round, only u8 via u8aFixed, we can add more support
|
|
135
|
+
|
|
136
|
+
(0, _util.assert)(length <= 256, () => `${type}: Only support for [Type; <length>], where length <= 256`);
|
|
137
|
+
value.displayName = displayName;
|
|
138
|
+
value.length = length;
|
|
139
|
+
value.sub = getTypeDef(vecType, {}, count);
|
|
140
|
+
return value;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
function _decodeRange(value, _, subType) {
|
|
144
|
+
const Type = getTypeDef(subType);
|
|
145
|
+
value.sub = [Type, Type];
|
|
146
|
+
return value;
|
|
147
|
+
} // decode a tuple
|
|
148
|
+
|
|
149
|
+
|
|
150
|
+
function _decodeTuple(value, _, subType, count) {
|
|
151
|
+
value.sub = subType.length === 0 ? [] : (0, _typeSplit.typeSplit)(subType).map(inner => getTypeDef(inner, {}, count));
|
|
152
|
+
return value;
|
|
153
|
+
} // decode a Int/UInt<bitLength[, name]>
|
|
154
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
function _decodeAnyInt(value, type, _, clazz) {
|
|
158
|
+
const [strLength, displayName] = type.substr(clazz.length + 1, type.length - clazz.length - 1 - 1).split(',');
|
|
159
|
+
const length = parseInt(strLength.trim(), 10); // as a first round, only u8 via u8aFixed, we can add more support
|
|
160
|
+
|
|
161
|
+
(0, _util.assert)(length <= 8192 && length % 8 === 0, () => `${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
|
|
162
|
+
value.displayName = displayName;
|
|
163
|
+
value.length = length;
|
|
164
|
+
return value;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
function _decodeInt(value, type, subType) {
|
|
168
|
+
return _decodeAnyInt(value, type, subType, 'Int');
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
function _decodeUInt(value, type, subType) {
|
|
172
|
+
return _decodeAnyInt(value, type, subType, 'UInt');
|
|
173
|
+
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
function _decodeDoNotConstruct(value, type, _) {
|
|
177
|
+
const NAME_LENGTH = 'DoNotConstruct'.length;
|
|
178
|
+
value.displayName = type.substr(NAME_LENGTH + 1, type.length - NAME_LENGTH - 1 - 1);
|
|
179
|
+
return value;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function hasWrapper(type, _ref5) {
|
|
183
|
+
let [start, end] = _ref5;
|
|
184
|
+
return type.substr(0, start.length) === start && type.substr(-1 * end.length) === end;
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
const nestedExtraction = [['[', ']', _types.TypeDefInfo.VecFixed, _decodeFixedVec], ['{', '}', _types.TypeDefInfo.Struct, _decodeStruct], ['(', ')', _types.TypeDefInfo.Tuple, _decodeTuple], // the inner for these are the same as tuple, multiple values
|
|
188
|
+
['BTreeMap<', '>', _types.TypeDefInfo.BTreeMap, _decodeTuple], ['HashMap<', '>', _types.TypeDefInfo.HashMap, _decodeTuple], ['Int<', '>', _types.TypeDefInfo.Int, _decodeInt], // Not sure about these, have a specific implementation?
|
|
189
|
+
['Range<', '>', _types.TypeDefInfo.Tuple, _decodeRange], ['RangeInclusive<', '>', _types.TypeDefInfo.Tuple, _decodeRange], ['Result<', '>', _types.TypeDefInfo.Result, _decodeTuple], ['UInt<', '>', _types.TypeDefInfo.UInt, _decodeUInt], ['DoNotConstruct<', '>', _types.TypeDefInfo.DoNotConstruct, _decodeDoNotConstruct]];
|
|
190
|
+
const wrappedExtraction = [['BTreeSet<', '>', _types.TypeDefInfo.BTreeSet], ['Compact<', '>', _types.TypeDefInfo.Compact], ['Linkage<', '>', _types.TypeDefInfo.Linkage], ['Option<', '>', _types.TypeDefInfo.Option], ['Vec<', '>', _types.TypeDefInfo.Vec], ['WrapperOpaque<', '>', _types.TypeDefInfo.WrapperOpaque]];
|
|
191
|
+
|
|
192
|
+
function extractSubType(type, _ref6) {
|
|
193
|
+
let [start, end] = _ref6;
|
|
194
|
+
return type.substr(start.length, type.length - start.length - end.length);
|
|
195
|
+
} // eslint-disable-next-line @typescript-eslint/ban-types
|
|
196
|
+
|
|
197
|
+
|
|
198
|
+
function getTypeDef(_type) {
|
|
199
|
+
let {
|
|
200
|
+
displayName,
|
|
201
|
+
name
|
|
202
|
+
} = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
|
|
203
|
+
let count = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
|
|
204
|
+
// create the type via Type, allowing types to be sanitized
|
|
205
|
+
const type = (0, _typesCodec.sanitize)(_type);
|
|
206
|
+
const value = {
|
|
207
|
+
displayName,
|
|
208
|
+
info: _types.TypeDefInfo.Plain,
|
|
209
|
+
name,
|
|
210
|
+
type
|
|
211
|
+
};
|
|
212
|
+
(0, _util.assert)(++count !== MAX_NESTED, 'getTypeDef: Maximum nested limit reached');
|
|
213
|
+
const nested = nestedExtraction.find(nested => hasWrapper(type, nested));
|
|
214
|
+
|
|
215
|
+
if (nested) {
|
|
216
|
+
value.info = nested[2];
|
|
217
|
+
return nested[3](value, type, extractSubType(type, nested), count);
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
const wrapped = wrappedExtraction.find(wrapped => hasWrapper(type, wrapped));
|
|
221
|
+
|
|
222
|
+
if (wrapped) {
|
|
223
|
+
value.info = wrapped[2];
|
|
224
|
+
value.sub = getTypeDef(extractSubType(type, wrapped), {}, count);
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
return value;
|
|
228
|
+
}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import type { TypeDef } from '@polkadot/types-create/types';
|
|
2
|
+
interface TypeDefOptions {
|
|
3
|
+
name?: string;
|
|
4
|
+
displayName?: string;
|
|
5
|
+
}
|
|
6
|
+
export declare function getTypeDef(_type: String | string, { displayName, name }?: TypeDefOptions, count?: number): TypeDef;
|
|
7
|
+
export {};
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
// Copyright 2017-2021 @polkadot/types-create authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { sanitize } from '@polkadot/types-codec';
|
|
4
|
+
import { assert, isNumber, isString, objectSpread } from '@polkadot/util';
|
|
5
|
+
import { TypeDefInfo } from "../types.js";
|
|
6
|
+
import { typeSplit } from "./typeSplit.js";
|
|
7
|
+
const MAX_NESTED = 64;
|
|
8
|
+
const KNOWN_INTERNALS = ['_alias', '_fallback'];
|
|
9
|
+
|
|
10
|
+
function getTypeString(typeOrObj) {
|
|
11
|
+
return isString(typeOrObj) ? typeOrObj.toString() : JSON.stringify(typeOrObj);
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
function isRustEnum(details) {
|
|
15
|
+
const values = Object.values(details);
|
|
16
|
+
|
|
17
|
+
if (values.some(v => isNumber(v))) {
|
|
18
|
+
assert(values.every(v => isNumber(v) && v >= 0 && v <= 255), 'Invalid number-indexed enum definition');
|
|
19
|
+
return false;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return true;
|
|
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
|
+
|
|
28
|
+
|
|
29
|
+
function _decodeEnum(value, details, count) {
|
|
30
|
+
value.info = TypeDefInfo.Enum; // not as pretty, but remain compatible with oo7 for both struct and Array types
|
|
31
|
+
|
|
32
|
+
if (Array.isArray(details)) {
|
|
33
|
+
value.sub = details.map((name, index) => ({
|
|
34
|
+
index,
|
|
35
|
+
info: TypeDefInfo.Plain,
|
|
36
|
+
name,
|
|
37
|
+
type: 'Null'
|
|
38
|
+
}));
|
|
39
|
+
} else if (isRustEnum(details)) {
|
|
40
|
+
value.sub = Object.entries(details).map(([name, typeOrObj], index) => objectSpread({}, getTypeDef(getTypeString(typeOrObj || 'Null'), {
|
|
41
|
+
name
|
|
42
|
+
}, count), {
|
|
43
|
+
index
|
|
44
|
+
}));
|
|
45
|
+
} else {
|
|
46
|
+
value.sub = Object.entries(details).map(([name, index]) => ({
|
|
47
|
+
index,
|
|
48
|
+
info: TypeDefInfo.Plain,
|
|
49
|
+
name,
|
|
50
|
+
type: 'Null'
|
|
51
|
+
}));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
return value;
|
|
55
|
+
} // decode a set of the form
|
|
56
|
+
// { _set: { A: 0b0001, B: 0b0010, C: 0b0100 } }
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
function _decodeSet(value, details) {
|
|
60
|
+
value.info = TypeDefInfo.Set;
|
|
61
|
+
value.length = details._bitLength;
|
|
62
|
+
value.sub = Object.entries(details).filter(([name]) => !name.startsWith('_')).map(([name, index]) => ({
|
|
63
|
+
index,
|
|
64
|
+
info: TypeDefInfo.Plain,
|
|
65
|
+
name,
|
|
66
|
+
type: 'Null'
|
|
67
|
+
}));
|
|
68
|
+
return value;
|
|
69
|
+
} // decode a struct, set or enum
|
|
70
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
function _decodeStruct(value, type, _, count) {
|
|
74
|
+
const parsed = JSON.parse(type);
|
|
75
|
+
const keys = Object.keys(parsed);
|
|
76
|
+
|
|
77
|
+
if (keys.length === 1 && keys[0] === '_enum') {
|
|
78
|
+
return _decodeEnum(value, parsed[keys[0]], count);
|
|
79
|
+
} else if (keys.length === 1 && keys[0] === '_set') {
|
|
80
|
+
return _decodeSet(value, parsed[keys[0]]);
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
value.alias = parsed._alias ? new Map(Object.entries(parsed._alias)) : undefined;
|
|
84
|
+
value.fallbackType = parsed._fallback;
|
|
85
|
+
value.sub = keys.filter(name => !KNOWN_INTERNALS.includes(name)).map(name => getTypeDef(getTypeString(parsed[name]), {
|
|
86
|
+
name
|
|
87
|
+
}, count));
|
|
88
|
+
return value;
|
|
89
|
+
} // decode a fixed vector, e.g. [u8;32]
|
|
90
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
91
|
+
|
|
92
|
+
|
|
93
|
+
function _decodeFixedVec(value, type, _, count) {
|
|
94
|
+
const max = type.length - 1;
|
|
95
|
+
let index = -1;
|
|
96
|
+
let inner = 0;
|
|
97
|
+
|
|
98
|
+
for (let i = 1; i < max && index === -1; i++) {
|
|
99
|
+
if (type[i] === ';' && inner === 0) {
|
|
100
|
+
index = i;
|
|
101
|
+
} else if (['[', '(', '<'].includes(type[i])) {
|
|
102
|
+
inner++;
|
|
103
|
+
} else if ([']', ')', '>'].includes(type[i])) {
|
|
104
|
+
inner--;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
assert(index !== -1, () => `${type}: Unable to extract location of ';'`);
|
|
109
|
+
const vecType = type.substr(1, index - 1);
|
|
110
|
+
const [strLength, displayName] = type.substr(index + 1, max - index - 1).split(';');
|
|
111
|
+
const length = parseInt(strLength.trim(), 10); // as a first round, only u8 via u8aFixed, we can add more support
|
|
112
|
+
|
|
113
|
+
assert(length <= 256, () => `${type}: Only support for [Type; <length>], where length <= 256`);
|
|
114
|
+
value.displayName = displayName;
|
|
115
|
+
value.length = length;
|
|
116
|
+
value.sub = getTypeDef(vecType, {}, count);
|
|
117
|
+
return value;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
function _decodeRange(value, _, subType) {
|
|
121
|
+
const Type = getTypeDef(subType);
|
|
122
|
+
value.sub = [Type, Type];
|
|
123
|
+
return value;
|
|
124
|
+
} // decode a tuple
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
function _decodeTuple(value, _, subType, count) {
|
|
128
|
+
value.sub = subType.length === 0 ? [] : typeSplit(subType).map(inner => getTypeDef(inner, {}, count));
|
|
129
|
+
return value;
|
|
130
|
+
} // decode a Int/UInt<bitLength[, name]>
|
|
131
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
function _decodeAnyInt(value, type, _, clazz) {
|
|
135
|
+
const [strLength, displayName] = type.substr(clazz.length + 1, type.length - clazz.length - 1 - 1).split(',');
|
|
136
|
+
const length = parseInt(strLength.trim(), 10); // as a first round, only u8 via u8aFixed, we can add more support
|
|
137
|
+
|
|
138
|
+
assert(length <= 8192 && length % 8 === 0, () => `${type}: Only support for ${clazz}<bitLength>, where length <= 8192 and a power of 8, found ${length}`);
|
|
139
|
+
value.displayName = displayName;
|
|
140
|
+
value.length = length;
|
|
141
|
+
return value;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function _decodeInt(value, type, subType) {
|
|
145
|
+
return _decodeAnyInt(value, type, subType, 'Int');
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
function _decodeUInt(value, type, subType) {
|
|
149
|
+
return _decodeAnyInt(value, type, subType, 'UInt');
|
|
150
|
+
} // eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
function _decodeDoNotConstruct(value, type, _) {
|
|
154
|
+
const NAME_LENGTH = 'DoNotConstruct'.length;
|
|
155
|
+
value.displayName = type.substr(NAME_LENGTH + 1, type.length - NAME_LENGTH - 1 - 1);
|
|
156
|
+
return value;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function hasWrapper(type, [start, end]) {
|
|
160
|
+
return type.substr(0, start.length) === start && type.substr(-1 * end.length) === end;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
const nestedExtraction = [['[', ']', TypeDefInfo.VecFixed, _decodeFixedVec], ['{', '}', TypeDefInfo.Struct, _decodeStruct], ['(', ')', TypeDefInfo.Tuple, _decodeTuple], // the inner for these are the same as tuple, multiple values
|
|
164
|
+
['BTreeMap<', '>', TypeDefInfo.BTreeMap, _decodeTuple], ['HashMap<', '>', TypeDefInfo.HashMap, _decodeTuple], ['Int<', '>', TypeDefInfo.Int, _decodeInt], // Not sure about these, have a specific implementation?
|
|
165
|
+
['Range<', '>', TypeDefInfo.Tuple, _decodeRange], ['RangeInclusive<', '>', TypeDefInfo.Tuple, _decodeRange], ['Result<', '>', TypeDefInfo.Result, _decodeTuple], ['UInt<', '>', TypeDefInfo.UInt, _decodeUInt], ['DoNotConstruct<', '>', TypeDefInfo.DoNotConstruct, _decodeDoNotConstruct]];
|
|
166
|
+
const wrappedExtraction = [['BTreeSet<', '>', TypeDefInfo.BTreeSet], ['Compact<', '>', TypeDefInfo.Compact], ['Linkage<', '>', TypeDefInfo.Linkage], ['Option<', '>', TypeDefInfo.Option], ['Vec<', '>', TypeDefInfo.Vec], ['WrapperOpaque<', '>', TypeDefInfo.WrapperOpaque]];
|
|
167
|
+
|
|
168
|
+
function extractSubType(type, [start, end]) {
|
|
169
|
+
return type.substr(start.length, type.length - start.length - end.length);
|
|
170
|
+
} // eslint-disable-next-line @typescript-eslint/ban-types
|
|
171
|
+
|
|
172
|
+
|
|
173
|
+
export function getTypeDef(_type, {
|
|
174
|
+
displayName,
|
|
175
|
+
name
|
|
176
|
+
} = {}, count = 0) {
|
|
177
|
+
// create the type via Type, allowing types to be sanitized
|
|
178
|
+
const type = sanitize(_type);
|
|
179
|
+
const value = {
|
|
180
|
+
displayName,
|
|
181
|
+
info: TypeDefInfo.Plain,
|
|
182
|
+
name,
|
|
183
|
+
type
|
|
184
|
+
};
|
|
185
|
+
assert(++count !== MAX_NESTED, 'getTypeDef: Maximum nested limit reached');
|
|
186
|
+
const nested = nestedExtraction.find(nested => hasWrapper(type, nested));
|
|
187
|
+
|
|
188
|
+
if (nested) {
|
|
189
|
+
value.info = nested[2];
|
|
190
|
+
return nested[3](value, type, extractSubType(type, nested), count);
|
|
191
|
+
}
|
|
192
|
+
|
|
193
|
+
const wrapped = wrappedExtraction.find(wrapped => hasWrapper(type, wrapped));
|
|
194
|
+
|
|
195
|
+
if (wrapped) {
|
|
196
|
+
value.info = wrapped[2];
|
|
197
|
+
value.sub = getTypeDef(extractSubType(type, wrapped), {}, count);
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
return value;
|
|
201
|
+
}
|
package/util/index.cjs
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _encodeTypes = require("./encodeTypes.cjs");
|
|
8
|
+
|
|
9
|
+
Object.keys(_encodeTypes).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
if (key in exports && exports[key] === _encodeTypes[key]) return;
|
|
12
|
+
Object.defineProperty(exports, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _encodeTypes[key];
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
var _getTypeDef = require("./getTypeDef.cjs");
|
|
21
|
+
|
|
22
|
+
Object.keys(_getTypeDef).forEach(function (key) {
|
|
23
|
+
if (key === "default" || key === "__esModule") return;
|
|
24
|
+
if (key in exports && exports[key] === _getTypeDef[key]) return;
|
|
25
|
+
Object.defineProperty(exports, key, {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return _getTypeDef[key];
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
var _typeSplit = require("./typeSplit.cjs");
|
|
34
|
+
|
|
35
|
+
Object.keys(_typeSplit).forEach(function (key) {
|
|
36
|
+
if (key === "default" || key === "__esModule") return;
|
|
37
|
+
if (key in exports && exports[key] === _typeSplit[key]) return;
|
|
38
|
+
Object.defineProperty(exports, key, {
|
|
39
|
+
enumerable: true,
|
|
40
|
+
get: function () {
|
|
41
|
+
return _typeSplit[key];
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
});
|
package/util/index.d.ts
ADDED
package/util/index.js
ADDED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.typeSplit = typeSplit;
|
|
7
|
+
|
|
8
|
+
var _util = require("@polkadot/util");
|
|
9
|
+
|
|
10
|
+
// Copyright 2017-2021 @polkadot/types-create authors & contributors
|
|
11
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
12
|
+
function isNotNested() {
|
|
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
|
+
|
|
21
|
+
function typeSplit(type) {
|
|
22
|
+
let [cDepth, fDepth, sDepth, tDepth, start] = [0, 0, 0, 0, 0];
|
|
23
|
+
const result = [];
|
|
24
|
+
|
|
25
|
+
const extract = index => {
|
|
26
|
+
if (isNotNested(cDepth, fDepth, sDepth, tDepth)) {
|
|
27
|
+
result.push(type.substr(start, index - start).trim());
|
|
28
|
+
start = index + 1;
|
|
29
|
+
}
|
|
30
|
+
};
|
|
31
|
+
|
|
32
|
+
for (let index = 0; index < type.length; index++) {
|
|
33
|
+
switch (type[index]) {
|
|
34
|
+
// if we are not nested, add the type
|
|
35
|
+
case ',':
|
|
36
|
+
extract(index);
|
|
37
|
+
break;
|
|
38
|
+
// adjust compact/vec (and friends) depth
|
|
39
|
+
|
|
40
|
+
case '<':
|
|
41
|
+
cDepth++;
|
|
42
|
+
break;
|
|
43
|
+
|
|
44
|
+
case '>':
|
|
45
|
+
cDepth--;
|
|
46
|
+
break;
|
|
47
|
+
// adjust fixed vec depths
|
|
48
|
+
|
|
49
|
+
case '[':
|
|
50
|
+
fDepth++;
|
|
51
|
+
break;
|
|
52
|
+
|
|
53
|
+
case ']':
|
|
54
|
+
fDepth--;
|
|
55
|
+
break;
|
|
56
|
+
// adjust struct depth
|
|
57
|
+
|
|
58
|
+
case '{':
|
|
59
|
+
sDepth++;
|
|
60
|
+
break;
|
|
61
|
+
|
|
62
|
+
case '}':
|
|
63
|
+
sDepth--;
|
|
64
|
+
break;
|
|
65
|
+
// adjust tuple depth
|
|
66
|
+
|
|
67
|
+
case '(':
|
|
68
|
+
tDepth++;
|
|
69
|
+
break;
|
|
70
|
+
|
|
71
|
+
case ')':
|
|
72
|
+
tDepth--;
|
|
73
|
+
break;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
(0, _util.assert)(isNotNested(cDepth, fDepth, sDepth, tDepth), () => `Invalid definition (missing terminators) found in ${type}`); // the final leg of the journey
|
|
78
|
+
|
|
79
|
+
result.push(type.substr(start, type.length - start).trim());
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function typeSplit(type: string): string[];
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
// Copyright 2017-2021 @polkadot/types-create authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { assert } from '@polkadot/util';
|
|
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
|
+
|
|
10
|
+
export function typeSplit(type) {
|
|
11
|
+
let [cDepth, fDepth, sDepth, tDepth, start] = [0, 0, 0, 0, 0];
|
|
12
|
+
const result = [];
|
|
13
|
+
|
|
14
|
+
const extract = index => {
|
|
15
|
+
if (isNotNested(cDepth, fDepth, sDepth, tDepth)) {
|
|
16
|
+
result.push(type.substr(start, index - start).trim());
|
|
17
|
+
start = index + 1;
|
|
18
|
+
}
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
for (let index = 0; index < type.length; index++) {
|
|
22
|
+
switch (type[index]) {
|
|
23
|
+
// if we are not nested, add the type
|
|
24
|
+
case ',':
|
|
25
|
+
extract(index);
|
|
26
|
+
break;
|
|
27
|
+
// adjust compact/vec (and friends) depth
|
|
28
|
+
|
|
29
|
+
case '<':
|
|
30
|
+
cDepth++;
|
|
31
|
+
break;
|
|
32
|
+
|
|
33
|
+
case '>':
|
|
34
|
+
cDepth--;
|
|
35
|
+
break;
|
|
36
|
+
// adjust fixed vec depths
|
|
37
|
+
|
|
38
|
+
case '[':
|
|
39
|
+
fDepth++;
|
|
40
|
+
break;
|
|
41
|
+
|
|
42
|
+
case ']':
|
|
43
|
+
fDepth--;
|
|
44
|
+
break;
|
|
45
|
+
// adjust struct depth
|
|
46
|
+
|
|
47
|
+
case '{':
|
|
48
|
+
sDepth++;
|
|
49
|
+
break;
|
|
50
|
+
|
|
51
|
+
case '}':
|
|
52
|
+
sDepth--;
|
|
53
|
+
break;
|
|
54
|
+
// adjust tuple depth
|
|
55
|
+
|
|
56
|
+
case '(':
|
|
57
|
+
tDepth++;
|
|
58
|
+
break;
|
|
59
|
+
|
|
60
|
+
case ')':
|
|
61
|
+
tDepth--;
|
|
62
|
+
break;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
assert(isNotNested(cDepth, fDepth, sDepth, tDepth), () => `Invalid definition (missing terminators) found in ${type}`); // the final leg of the journey
|
|
67
|
+
|
|
68
|
+
result.push(type.substr(start, type.length - start).trim());
|
|
69
|
+
return result;
|
|
70
|
+
}
|