@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
package/bundle.cjs
ADDED
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
var _exportNames = {
|
|
7
|
+
packageInfo: true,
|
|
8
|
+
TypeDefInfo: true
|
|
9
|
+
};
|
|
10
|
+
Object.defineProperty(exports, "TypeDefInfo", {
|
|
11
|
+
enumerable: true,
|
|
12
|
+
get: function () {
|
|
13
|
+
return _types.TypeDefInfo;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
Object.defineProperty(exports, "packageInfo", {
|
|
17
|
+
enumerable: true,
|
|
18
|
+
get: function () {
|
|
19
|
+
return _packageInfo.packageInfo;
|
|
20
|
+
}
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
var _packageInfo = require("./packageInfo.cjs");
|
|
24
|
+
|
|
25
|
+
var _types = require("./types.cjs");
|
|
26
|
+
|
|
27
|
+
var _index = require("./create/index.cjs");
|
|
28
|
+
|
|
29
|
+
Object.keys(_index).forEach(function (key) {
|
|
30
|
+
if (key === "default" || key === "__esModule") return;
|
|
31
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
32
|
+
if (key in exports && exports[key] === _index[key]) return;
|
|
33
|
+
Object.defineProperty(exports, key, {
|
|
34
|
+
enumerable: true,
|
|
35
|
+
get: function () {
|
|
36
|
+
return _index[key];
|
|
37
|
+
}
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
var _index2 = require("./util/index.cjs");
|
|
42
|
+
|
|
43
|
+
Object.keys(_index2).forEach(function (key) {
|
|
44
|
+
if (key === "default" || key === "__esModule") return;
|
|
45
|
+
if (Object.prototype.hasOwnProperty.call(_exportNames, key)) return;
|
|
46
|
+
if (key in exports && exports[key] === _index2[key]) return;
|
|
47
|
+
Object.defineProperty(exports, key, {
|
|
48
|
+
enumerable: true,
|
|
49
|
+
get: function () {
|
|
50
|
+
return _index2[key];
|
|
51
|
+
}
|
|
52
|
+
});
|
|
53
|
+
});
|
package/bundle.d.ts
ADDED
package/bundle.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Copyright 2017-2021 @polkadot/types-codec authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
export { packageInfo } from "./packageInfo.js";
|
|
4
|
+
export { TypeDefInfo } from "./types.js";
|
|
5
|
+
export * from "./create/index.js";
|
|
6
|
+
export * from "./util/index.js";
|
package/create/class.cjs
ADDED
|
@@ -0,0 +1,185 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.constructTypeClass = constructTypeClass;
|
|
7
|
+
exports.createClassUnsafe = createClassUnsafe;
|
|
8
|
+
exports.getTypeClass = getTypeClass;
|
|
9
|
+
|
|
10
|
+
var _typesCodec = require("@polkadot/types-codec");
|
|
11
|
+
|
|
12
|
+
var _util = require("@polkadot/util");
|
|
13
|
+
|
|
14
|
+
var _types = require("../types.cjs");
|
|
15
|
+
|
|
16
|
+
var _getTypeDef = require("../util/getTypeDef.cjs");
|
|
17
|
+
|
|
18
|
+
// Copyright 2017-2021 @polkadot/types-create authors & contributors
|
|
19
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
20
|
+
function getTypeDefType(_ref) {
|
|
21
|
+
let {
|
|
22
|
+
lookupName,
|
|
23
|
+
type
|
|
24
|
+
} = _ref;
|
|
25
|
+
return lookupName || type;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function getSubDefArray(value) {
|
|
29
|
+
(0, _util.assert)(value.sub && Array.isArray(value.sub), () => `Expected subtype as TypeDef[] in ${(0, _util.stringify)(value)}`);
|
|
30
|
+
return value.sub;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function getSubDef(value) {
|
|
34
|
+
(0, _util.assert)(value.sub && !Array.isArray(value.sub), () => `Expected subtype as TypeDef in ${(0, _util.stringify)(value)}`);
|
|
35
|
+
return value.sub;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function getSubType(value) {
|
|
39
|
+
return getTypeDefType(getSubDef(value));
|
|
40
|
+
} // create a maps of type string CodecClasss from the input
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
function getTypeClassMap(value) {
|
|
44
|
+
const subs = getSubDefArray(value);
|
|
45
|
+
const map = {};
|
|
46
|
+
|
|
47
|
+
for (let i = 0; i < subs.length; i++) {
|
|
48
|
+
map[subs[i].name] = getTypeDefType(subs[i]);
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
return map;
|
|
52
|
+
} // create an array of type string CodecClasss from the input
|
|
53
|
+
|
|
54
|
+
|
|
55
|
+
function getTypeClassArray(value) {
|
|
56
|
+
return getSubDefArray(value).map(getTypeDefType);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function createInt(Clazz, _ref2) {
|
|
60
|
+
let {
|
|
61
|
+
displayName,
|
|
62
|
+
length
|
|
63
|
+
} = _ref2;
|
|
64
|
+
(0, _util.assert)((0, _util.isNumber)(length), () => `Expected bitLength information for ${displayName || Clazz.constructor.name}<bitLength>`);
|
|
65
|
+
return Clazz.with(length, displayName);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
function createHashMap(Clazz, value) {
|
|
69
|
+
const [keyType, valueType] = getTypeClassArray(value);
|
|
70
|
+
return Clazz.with(keyType, valueType);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function createWithSub(Clazz, value) {
|
|
74
|
+
return Clazz.with(getSubType(value));
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
const infoMapping = {
|
|
78
|
+
[_types.TypeDefInfo.BTreeMap]: (registry, value) => createHashMap(_typesCodec.BTreeMap, value),
|
|
79
|
+
[_types.TypeDefInfo.BTreeSet]: (registry, value) => createWithSub(_typesCodec.BTreeSet, value),
|
|
80
|
+
[_types.TypeDefInfo.Compact]: (registry, value) => createWithSub(_typesCodec.Compact, value),
|
|
81
|
+
[_types.TypeDefInfo.DoNotConstruct]: (registry, value) => _typesCodec.DoNotConstruct.with(value.displayName || value.type),
|
|
82
|
+
[_types.TypeDefInfo.Enum]: (registry, value) => {
|
|
83
|
+
const subs = getSubDefArray(value);
|
|
84
|
+
return _typesCodec.Enum.with(subs.every(_ref3 => {
|
|
85
|
+
let {
|
|
86
|
+
type
|
|
87
|
+
} = _ref3;
|
|
88
|
+
return type === 'Null';
|
|
89
|
+
}) ? subs.reduce((out, _ref4, count) => {
|
|
90
|
+
let {
|
|
91
|
+
index,
|
|
92
|
+
name
|
|
93
|
+
} = _ref4;
|
|
94
|
+
out[name] = index || count;
|
|
95
|
+
return out;
|
|
96
|
+
}, {}) : getTypeClassMap(value));
|
|
97
|
+
},
|
|
98
|
+
[_types.TypeDefInfo.HashMap]: (registry, value) => createHashMap(_typesCodec.HashMap, value),
|
|
99
|
+
[_types.TypeDefInfo.Int]: (registry, value) => createInt(_typesCodec.Int, value),
|
|
100
|
+
// We have circular deps between Linkage & Struct
|
|
101
|
+
[_types.TypeDefInfo.Linkage]: (registry, value) => {
|
|
102
|
+
const type = `Option<${getSubType(value)}>`; // eslint-disable-next-line sort-keys
|
|
103
|
+
|
|
104
|
+
const Clazz = _typesCodec.Struct.with({
|
|
105
|
+
previous: type,
|
|
106
|
+
next: type
|
|
107
|
+
}); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
Clazz.prototype.toRawType = function () {
|
|
111
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call
|
|
112
|
+
return `Linkage<${this.next.toRawType(true)}>`;
|
|
113
|
+
};
|
|
114
|
+
|
|
115
|
+
return Clazz;
|
|
116
|
+
},
|
|
117
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
118
|
+
[_types.TypeDefInfo.Null]: (registry, _) => _typesCodec.Null,
|
|
119
|
+
[_types.TypeDefInfo.Option]: (registry, value) => createWithSub(_typesCodec.Option, value),
|
|
120
|
+
[_types.TypeDefInfo.Plain]: (registry, value) => registry.getOrUnknown(value.type),
|
|
121
|
+
[_types.TypeDefInfo.Range]: (registry, value) => (value.type.includes('RangeInclusive') ? _typesCodec.RangeInclusive : _typesCodec.Range).with(getSubType(value)),
|
|
122
|
+
[_types.TypeDefInfo.Result]: (registry, value) => {
|
|
123
|
+
const [Ok, Err] = getTypeClassArray(value); // eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
124
|
+
|
|
125
|
+
return _typesCodec.Result.with({
|
|
126
|
+
Err,
|
|
127
|
+
Ok
|
|
128
|
+
});
|
|
129
|
+
},
|
|
130
|
+
[_types.TypeDefInfo.Set]: (registry, value) => _typesCodec.CodecSet.with(getSubDefArray(value).reduce((result, _ref5) => {
|
|
131
|
+
let {
|
|
132
|
+
index,
|
|
133
|
+
name
|
|
134
|
+
} = _ref5;
|
|
135
|
+
result[name] = index;
|
|
136
|
+
return result;
|
|
137
|
+
}, {}), value.length),
|
|
138
|
+
[_types.TypeDefInfo.Si]: (registry, value) => getTypeClass(registry, registry.lookup.getTypeDef(value.type)),
|
|
139
|
+
[_types.TypeDefInfo.Struct]: (registry, value) => _typesCodec.Struct.with(getTypeClassMap(value), value.alias),
|
|
140
|
+
[_types.TypeDefInfo.Tuple]: (registry, value) => _typesCodec.Tuple.with(getTypeClassArray(value)),
|
|
141
|
+
[_types.TypeDefInfo.UInt]: (registry, value) => createInt(_typesCodec.UInt, value),
|
|
142
|
+
[_types.TypeDefInfo.Vec]: (registry, _ref6) => {
|
|
143
|
+
let {
|
|
144
|
+
sub
|
|
145
|
+
} = _ref6;
|
|
146
|
+
(0, _util.assert)(sub && !Array.isArray(sub), 'Expected type information for vector');
|
|
147
|
+
return sub.type === 'u8' ? _typesCodec.Bytes : _typesCodec.Vec.with(getTypeDefType(sub));
|
|
148
|
+
},
|
|
149
|
+
[_types.TypeDefInfo.VecFixed]: (registry, _ref7) => {
|
|
150
|
+
let {
|
|
151
|
+
displayName,
|
|
152
|
+
length,
|
|
153
|
+
sub
|
|
154
|
+
} = _ref7;
|
|
155
|
+
(0, _util.assert)(sub && (0, _util.isNumber)(length) && !Array.isArray(sub), 'Expected length & type information for fixed vector');
|
|
156
|
+
return sub.type === 'u8' ? _typesCodec.U8aFixed.with(length * 8, displayName) : _typesCodec.VecFixed.with(getTypeDefType(sub), length);
|
|
157
|
+
},
|
|
158
|
+
[_types.TypeDefInfo.WrapperOpaque]: (registry, value) => createWithSub(_typesCodec.WrapperOpaque, value)
|
|
159
|
+
};
|
|
160
|
+
|
|
161
|
+
function constructTypeClass(registry, typeDef) {
|
|
162
|
+
try {
|
|
163
|
+
const Type = infoMapping[typeDef.info](registry, typeDef);
|
|
164
|
+
(0, _util.assert)(Type, 'No class created'); // don't clobber any existing
|
|
165
|
+
|
|
166
|
+
if (!Type.__fallbackType && typeDef.fallbackType) {
|
|
167
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
168
|
+
// @ts-ignore ...this is the only place we we actually assign this...
|
|
169
|
+
Type.__fallbackType = typeDef.fallbackType;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
return Type;
|
|
173
|
+
} catch (error) {
|
|
174
|
+
throw new Error(`Unable to construct class from ${(0, _util.stringify)(typeDef)}: ${error.message}`);
|
|
175
|
+
}
|
|
176
|
+
} // Returns the type Class for construction
|
|
177
|
+
|
|
178
|
+
|
|
179
|
+
function getTypeClass(registry, typeDef) {
|
|
180
|
+
return registry.getUnsafe(typeDef.type, false, typeDef);
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function createClassUnsafe(registry, type) {
|
|
184
|
+
return getTypeClass(registry, registry.isLookupType(type) ? registry.lookup.getTypeDef(type) : (0, _getTypeDef.getTypeDef)(type));
|
|
185
|
+
}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Codec, CodecClass, CodecRegistry } from '@polkadot/types-codec/types';
|
|
2
|
+
import type { TypeDef } from '../types';
|
|
3
|
+
export declare function constructTypeClass<T extends Codec = Codec>(registry: CodecRegistry, typeDef: TypeDef): CodecClass<T>;
|
|
4
|
+
export declare function getTypeClass<T extends Codec = Codec>(registry: CodecRegistry, typeDef: TypeDef): CodecClass<T>;
|
|
5
|
+
export declare function createClassUnsafe<T extends Codec = Codec, K extends string = string>(registry: CodecRegistry, type: K): CodecClass<T>;
|
package/create/class.js
ADDED
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
// Copyright 2017-2021 @polkadot/types-create authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { BTreeMap, BTreeSet, Bytes, CodecSet, Compact, DoNotConstruct, Enum, HashMap, Int, Null, Option, Range, RangeInclusive, Result, Struct, Tuple, U8aFixed, UInt, Vec, VecFixed, WrapperOpaque } from '@polkadot/types-codec';
|
|
4
|
+
import { assert, isNumber, stringify } from '@polkadot/util';
|
|
5
|
+
import { TypeDefInfo } from "../types.js";
|
|
6
|
+
import { getTypeDef } from "../util/getTypeDef.js";
|
|
7
|
+
|
|
8
|
+
function getTypeDefType({
|
|
9
|
+
lookupName,
|
|
10
|
+
type
|
|
11
|
+
}) {
|
|
12
|
+
return lookupName || type;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
function getSubDefArray(value) {
|
|
16
|
+
assert(value.sub && Array.isArray(value.sub), () => `Expected subtype as TypeDef[] in ${stringify(value)}`);
|
|
17
|
+
return value.sub;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
function getSubDef(value) {
|
|
21
|
+
assert(value.sub && !Array.isArray(value.sub), () => `Expected subtype as TypeDef in ${stringify(value)}`);
|
|
22
|
+
return value.sub;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function getSubType(value) {
|
|
26
|
+
return getTypeDefType(getSubDef(value));
|
|
27
|
+
} // create a maps of type string CodecClasss from the input
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
function getTypeClassMap(value) {
|
|
31
|
+
const subs = getSubDefArray(value);
|
|
32
|
+
const map = {};
|
|
33
|
+
|
|
34
|
+
for (let i = 0; i < subs.length; i++) {
|
|
35
|
+
map[subs[i].name] = getTypeDefType(subs[i]);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return map;
|
|
39
|
+
} // create an array of type string CodecClasss from the input
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
function getTypeClassArray(value) {
|
|
43
|
+
return getSubDefArray(value).map(getTypeDefType);
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function createInt(Clazz, {
|
|
47
|
+
displayName,
|
|
48
|
+
length
|
|
49
|
+
}) {
|
|
50
|
+
assert(isNumber(length), () => `Expected bitLength information for ${displayName || Clazz.constructor.name}<bitLength>`);
|
|
51
|
+
return Clazz.with(length, displayName);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
function createHashMap(Clazz, value) {
|
|
55
|
+
const [keyType, valueType] = getTypeClassArray(value);
|
|
56
|
+
return Clazz.with(keyType, valueType);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
function createWithSub(Clazz, value) {
|
|
60
|
+
return Clazz.with(getSubType(value));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const infoMapping = {
|
|
64
|
+
[TypeDefInfo.BTreeMap]: (registry, value) => createHashMap(BTreeMap, value),
|
|
65
|
+
[TypeDefInfo.BTreeSet]: (registry, value) => createWithSub(BTreeSet, value),
|
|
66
|
+
[TypeDefInfo.Compact]: (registry, value) => createWithSub(Compact, value),
|
|
67
|
+
[TypeDefInfo.DoNotConstruct]: (registry, value) => DoNotConstruct.with(value.displayName || value.type),
|
|
68
|
+
[TypeDefInfo.Enum]: (registry, value) => {
|
|
69
|
+
const subs = getSubDefArray(value);
|
|
70
|
+
return Enum.with(subs.every(({
|
|
71
|
+
type
|
|
72
|
+
}) => type === 'Null') ? subs.reduce((out, {
|
|
73
|
+
index,
|
|
74
|
+
name
|
|
75
|
+
}, count) => {
|
|
76
|
+
out[name] = index || count;
|
|
77
|
+
return out;
|
|
78
|
+
}, {}) : getTypeClassMap(value));
|
|
79
|
+
},
|
|
80
|
+
[TypeDefInfo.HashMap]: (registry, value) => createHashMap(HashMap, value),
|
|
81
|
+
[TypeDefInfo.Int]: (registry, value) => createInt(Int, value),
|
|
82
|
+
// We have circular deps between Linkage & Struct
|
|
83
|
+
[TypeDefInfo.Linkage]: (registry, value) => {
|
|
84
|
+
const type = `Option<${getSubType(value)}>`; // eslint-disable-next-line sort-keys
|
|
85
|
+
|
|
86
|
+
const Clazz = Struct.with({
|
|
87
|
+
previous: type,
|
|
88
|
+
next: type
|
|
89
|
+
}); // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
|
|
90
|
+
|
|
91
|
+
Clazz.prototype.toRawType = function () {
|
|
92
|
+
// eslint-disable-next-line @typescript-eslint/restrict-template-expressions,@typescript-eslint/no-unsafe-member-access,@typescript-eslint/no-unsafe-call
|
|
93
|
+
return `Linkage<${this.next.toRawType(true)}>`;
|
|
94
|
+
};
|
|
95
|
+
|
|
96
|
+
return Clazz;
|
|
97
|
+
},
|
|
98
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
99
|
+
[TypeDefInfo.Null]: (registry, _) => Null,
|
|
100
|
+
[TypeDefInfo.Option]: (registry, value) => createWithSub(Option, value),
|
|
101
|
+
[TypeDefInfo.Plain]: (registry, value) => registry.getOrUnknown(value.type),
|
|
102
|
+
[TypeDefInfo.Range]: (registry, value) => (value.type.includes('RangeInclusive') ? RangeInclusive : Range).with(getSubType(value)),
|
|
103
|
+
[TypeDefInfo.Result]: (registry, value) => {
|
|
104
|
+
const [Ok, Err] = getTypeClassArray(value); // eslint-disable-next-line @typescript-eslint/no-use-before-define
|
|
105
|
+
|
|
106
|
+
return Result.with({
|
|
107
|
+
Err,
|
|
108
|
+
Ok
|
|
109
|
+
});
|
|
110
|
+
},
|
|
111
|
+
[TypeDefInfo.Set]: (registry, value) => CodecSet.with(getSubDefArray(value).reduce((result, {
|
|
112
|
+
index,
|
|
113
|
+
name
|
|
114
|
+
}) => {
|
|
115
|
+
result[name] = index;
|
|
116
|
+
return result;
|
|
117
|
+
}, {}), value.length),
|
|
118
|
+
[TypeDefInfo.Si]: (registry, value) => getTypeClass(registry, registry.lookup.getTypeDef(value.type)),
|
|
119
|
+
[TypeDefInfo.Struct]: (registry, value) => Struct.with(getTypeClassMap(value), value.alias),
|
|
120
|
+
[TypeDefInfo.Tuple]: (registry, value) => Tuple.with(getTypeClassArray(value)),
|
|
121
|
+
[TypeDefInfo.UInt]: (registry, value) => createInt(UInt, value),
|
|
122
|
+
[TypeDefInfo.Vec]: (registry, {
|
|
123
|
+
sub
|
|
124
|
+
}) => {
|
|
125
|
+
assert(sub && !Array.isArray(sub), 'Expected type information for vector');
|
|
126
|
+
return sub.type === 'u8' ? Bytes : Vec.with(getTypeDefType(sub));
|
|
127
|
+
},
|
|
128
|
+
[TypeDefInfo.VecFixed]: (registry, {
|
|
129
|
+
displayName,
|
|
130
|
+
length,
|
|
131
|
+
sub
|
|
132
|
+
}) => {
|
|
133
|
+
assert(sub && isNumber(length) && !Array.isArray(sub), 'Expected length & type information for fixed vector');
|
|
134
|
+
return sub.type === 'u8' ? U8aFixed.with(length * 8, displayName) : VecFixed.with(getTypeDefType(sub), length);
|
|
135
|
+
},
|
|
136
|
+
[TypeDefInfo.WrapperOpaque]: (registry, value) => createWithSub(WrapperOpaque, value)
|
|
137
|
+
};
|
|
138
|
+
export function constructTypeClass(registry, typeDef) {
|
|
139
|
+
try {
|
|
140
|
+
const Type = infoMapping[typeDef.info](registry, typeDef);
|
|
141
|
+
assert(Type, 'No class created'); // don't clobber any existing
|
|
142
|
+
|
|
143
|
+
if (!Type.__fallbackType && typeDef.fallbackType) {
|
|
144
|
+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
145
|
+
// @ts-ignore ...this is the only place we we actually assign this...
|
|
146
|
+
Type.__fallbackType = typeDef.fallbackType;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
return Type;
|
|
150
|
+
} catch (error) {
|
|
151
|
+
throw new Error(`Unable to construct class from ${stringify(typeDef)}: ${error.message}`);
|
|
152
|
+
}
|
|
153
|
+
} // Returns the type Class for construction
|
|
154
|
+
|
|
155
|
+
export function getTypeClass(registry, typeDef) {
|
|
156
|
+
return registry.getUnsafe(typeDef.type, false, typeDef);
|
|
157
|
+
}
|
|
158
|
+
export function createClassUnsafe(registry, type) {
|
|
159
|
+
return getTypeClass(registry, registry.isLookupType(type) ? registry.lookup.getTypeDef(type) : getTypeDef(type));
|
|
160
|
+
}
|
package/create/index.cjs
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
var _class = require("./class.cjs");
|
|
8
|
+
|
|
9
|
+
Object.keys(_class).forEach(function (key) {
|
|
10
|
+
if (key === "default" || key === "__esModule") return;
|
|
11
|
+
if (key in exports && exports[key] === _class[key]) return;
|
|
12
|
+
Object.defineProperty(exports, key, {
|
|
13
|
+
enumerable: true,
|
|
14
|
+
get: function () {
|
|
15
|
+
return _class[key];
|
|
16
|
+
}
|
|
17
|
+
});
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
var _type = require("./type.cjs");
|
|
21
|
+
|
|
22
|
+
Object.keys(_type).forEach(function (key) {
|
|
23
|
+
if (key === "default" || key === "__esModule") return;
|
|
24
|
+
if (key in exports && exports[key] === _type[key]) return;
|
|
25
|
+
Object.defineProperty(exports, key, {
|
|
26
|
+
enumerable: true,
|
|
27
|
+
get: function () {
|
|
28
|
+
return _type[key];
|
|
29
|
+
}
|
|
30
|
+
});
|
|
31
|
+
});
|
package/create/index.js
ADDED
package/create/type.cjs
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
exports.createTypeUnsafe = createTypeUnsafe;
|
|
7
|
+
|
|
8
|
+
var _typesCodec = require("@polkadot/types-codec");
|
|
9
|
+
|
|
10
|
+
var _util = require("@polkadot/util");
|
|
11
|
+
|
|
12
|
+
var _class = require("./class.cjs");
|
|
13
|
+
|
|
14
|
+
// Copyright 2017-2021 @polkadot/types-create authors & contributors
|
|
15
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
16
|
+
// With isPedantic, actually check that the encoding matches that supplied. This
|
|
17
|
+
// is much slower, but verifies that we have the correct types defined
|
|
18
|
+
function checkInstance(created, matcher) {
|
|
19
|
+
const u8a = created.toU8a();
|
|
20
|
+
const rawType = created.toRawType();
|
|
21
|
+
const isOk = // full match, all ok
|
|
22
|
+
(0, _util.u8aEq)(u8a, matcher) || // on a length-prefixed type, just check the actual length
|
|
23
|
+
['Bytes', 'Text', 'Type'].includes(rawType) && matcher.length === created.length || // when the created is empty and matcher is also empty, let it slide...
|
|
24
|
+
created.isEmpty && matcher.every(v => !v);
|
|
25
|
+
(0, _util.assert)(isOk, () => `${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)`);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function checkPedantic(created, _ref) {
|
|
29
|
+
let [value] = _ref;
|
|
30
|
+
let isPedantic = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : false;
|
|
31
|
+
|
|
32
|
+
if (isPedantic) {
|
|
33
|
+
if ((0, _util.isU8a)(value)) {
|
|
34
|
+
checkInstance(created, value);
|
|
35
|
+
} else if ((0, _util.isHex)(value)) {
|
|
36
|
+
checkInstance(created, (0, _util.u8aToU8a)(value));
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
} // Initializes a type with a value. This also checks for fallbacks and in the cases
|
|
40
|
+
// where isPedantic is specified (storage decoding), also check the format/structure
|
|
41
|
+
|
|
42
|
+
|
|
43
|
+
function initType(registry, Type) {
|
|
44
|
+
let params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
45
|
+
let {
|
|
46
|
+
blockHash,
|
|
47
|
+
isOptional,
|
|
48
|
+
isPedantic
|
|
49
|
+
} = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
50
|
+
const created = new (isOptional ? _typesCodec.Option.with(Type) : Type)(registry, ...params);
|
|
51
|
+
checkPedantic(created, params, isPedantic);
|
|
52
|
+
|
|
53
|
+
if (blockHash) {
|
|
54
|
+
created.createdAtHash = createTypeUnsafe(registry, 'Hash', [blockHash]);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
return created;
|
|
58
|
+
} // An unsafe version of the `createType` below. It's unsafe because the `type`
|
|
59
|
+
// argument here can be any string, which, when it cannot parse, will yield a
|
|
60
|
+
// runtime error.
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
function createTypeUnsafe(registry, type) {
|
|
64
|
+
let params = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : [];
|
|
65
|
+
let options = arguments.length > 3 && arguments[3] !== undefined ? arguments[3] : {};
|
|
66
|
+
let Clazz = null;
|
|
67
|
+
let firstError = null;
|
|
68
|
+
|
|
69
|
+
try {
|
|
70
|
+
Clazz = (0, _class.createClassUnsafe)(registry, type);
|
|
71
|
+
return initType(registry, Clazz, params, options);
|
|
72
|
+
} catch (error) {
|
|
73
|
+
firstError = new Error(`createType(${type}):: ${error.message}`);
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
if (Clazz && Clazz.__fallbackType) {
|
|
77
|
+
try {
|
|
78
|
+
Clazz = (0, _class.createClassUnsafe)(registry, Clazz.__fallbackType);
|
|
79
|
+
return initType(registry, Clazz, params, options);
|
|
80
|
+
} catch {// swallow, we will throw the first error again
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
throw firstError;
|
|
85
|
+
}
|
package/create/type.d.ts
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
import type { Codec, CodecRegistry } from '@polkadot/types-codec/types';
|
|
2
|
+
import type { CreateOptions } from '../types';
|
|
3
|
+
export declare function createTypeUnsafe<T extends Codec = Codec, K extends string = string>(registry: CodecRegistry, type: K, params?: unknown[], options?: CreateOptions): T;
|
package/create/type.js
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
// Copyright 2017-2021 @polkadot/types-create authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { Option } from '@polkadot/types-codec';
|
|
4
|
+
import { assert, isHex, isU8a, u8aEq, u8aToHex, u8aToU8a } from '@polkadot/util';
|
|
5
|
+
import { createClassUnsafe } from "./class.js"; // With isPedantic, actually check that the encoding matches that supplied. This
|
|
6
|
+
// is much slower, but verifies that we have the correct types defined
|
|
7
|
+
|
|
8
|
+
function checkInstance(created, matcher) {
|
|
9
|
+
const u8a = created.toU8a();
|
|
10
|
+
const rawType = created.toRawType();
|
|
11
|
+
const isOk = // full match, all ok
|
|
12
|
+
u8aEq(u8a, matcher) || // on a length-prefixed type, just check the actual length
|
|
13
|
+
['Bytes', 'Text', 'Type'].includes(rawType) && matcher.length === created.length || // when the created is empty and matcher is also empty, let it slide...
|
|
14
|
+
created.isEmpty && matcher.every(v => !v);
|
|
15
|
+
assert(isOk, () => `${rawType}:: Decoded input doesn't match input, received ${u8aToHex(matcher, 512)} (${matcher.length} bytes), created ${u8aToHex(u8a, 512)} (${u8a.length} bytes)`);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function checkPedantic(created, [value], isPedantic = false) {
|
|
19
|
+
if (isPedantic) {
|
|
20
|
+
if (isU8a(value)) {
|
|
21
|
+
checkInstance(created, value);
|
|
22
|
+
} else if (isHex(value)) {
|
|
23
|
+
checkInstance(created, u8aToU8a(value));
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
} // Initializes a type with a value. This also checks for fallbacks and in the cases
|
|
27
|
+
// where isPedantic is specified (storage decoding), also check the format/structure
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
function initType(registry, Type, params = [], {
|
|
31
|
+
blockHash,
|
|
32
|
+
isOptional,
|
|
33
|
+
isPedantic
|
|
34
|
+
} = {}) {
|
|
35
|
+
const created = new (isOptional ? Option.with(Type) : Type)(registry, ...params);
|
|
36
|
+
checkPedantic(created, params, isPedantic);
|
|
37
|
+
|
|
38
|
+
if (blockHash) {
|
|
39
|
+
created.createdAtHash = createTypeUnsafe(registry, 'Hash', [blockHash]);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
return created;
|
|
43
|
+
} // An unsafe version of the `createType` below. It's unsafe because the `type`
|
|
44
|
+
// argument here can be any string, which, when it cannot parse, will yield a
|
|
45
|
+
// runtime error.
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
export function createTypeUnsafe(registry, type, params = [], options = {}) {
|
|
49
|
+
let Clazz = null;
|
|
50
|
+
let firstError = null;
|
|
51
|
+
|
|
52
|
+
try {
|
|
53
|
+
Clazz = createClassUnsafe(registry, type);
|
|
54
|
+
return initType(registry, Clazz, params, options);
|
|
55
|
+
} catch (error) {
|
|
56
|
+
firstError = new Error(`createType(${type}):: ${error.message}`);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
if (Clazz && Clazz.__fallbackType) {
|
|
60
|
+
try {
|
|
61
|
+
Clazz = createClassUnsafe(registry, Clazz.__fallbackType);
|
|
62
|
+
return initType(registry, Clazz, params, options);
|
|
63
|
+
} catch {// swallow, we will throw the first error again
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
throw firstError;
|
|
68
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
var _typesCodec = require("@polkadot/types-codec");
|
|
4
|
+
|
|
5
|
+
var _util = require("@polkadot/util");
|
|
6
|
+
|
|
7
|
+
var _packageInfo = require("./packageInfo.cjs");
|
|
8
|
+
|
|
9
|
+
// Copyright 2017-2021 @polkadot/types-create authors & contributors
|
|
10
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
11
|
+
(0, _util.detectPackage)(_packageInfo.packageInfo, typeof __dirname !== 'undefined' && __dirname, [_typesCodec.packageInfo]);
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
package/detectPackage.js
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
// Copyright 2017-2021 @polkadot/types-create authors & contributors
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { packageInfo as codecInfo } from '@polkadot/types-codec';
|
|
4
|
+
import { detectPackage } from '@polkadot/util';
|
|
5
|
+
import { packageInfo } from "./packageInfo.js";
|
|
6
|
+
detectPackage(packageInfo, typeof __dirname !== 'undefined' && __dirname, [codecInfo]);
|
package/index.cjs
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
Object.defineProperty(exports, "__esModule", {
|
|
4
|
+
value: true
|
|
5
|
+
});
|
|
6
|
+
|
|
7
|
+
require("./detectPackage.cjs");
|
|
8
|
+
|
|
9
|
+
var _bundle = require("./bundle.cjs");
|
|
10
|
+
|
|
11
|
+
Object.keys(_bundle).forEach(function (key) {
|
|
12
|
+
if (key === "default" || key === "__esModule") return;
|
|
13
|
+
if (key in exports && exports[key] === _bundle[key]) return;
|
|
14
|
+
Object.defineProperty(exports, key, {
|
|
15
|
+
enumerable: true,
|
|
16
|
+
get: function () {
|
|
17
|
+
return _bundle[key];
|
|
18
|
+
}
|
|
19
|
+
});
|
|
20
|
+
});
|
package/index.d.ts
ADDED
package/index.js
ADDED