@talismn/scale 0.3.5 → 2.0.0
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/LICENSE +253 -674
- package/dist/index.d.mts +875 -859
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +875 -859
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +457 -470
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +329 -431
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -7
package/dist/index.mjs
CHANGED
|
@@ -1,449 +1,347 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
getLookupFn
|
|
5
|
-
} from "@polkadot-api/metadata-builders";
|
|
6
|
-
import {
|
|
7
|
-
Binary,
|
|
8
|
-
decAnyMetadata,
|
|
9
|
-
FixedSizeBinary,
|
|
10
|
-
metadata,
|
|
11
|
-
unifyMetadata
|
|
12
|
-
} from "@polkadot-api/substrate-bindings";
|
|
13
|
-
import { fromHex, mergeUint8, toHex } from "@polkadot-api/utils";
|
|
14
|
-
var magicNumber = 1635018093;
|
|
15
|
-
|
|
16
|
-
// src/log.ts
|
|
1
|
+
import { getDynamicBuilder, getDynamicBuilder as getDynamicBuilder$1, getLookupFn, getLookupFn as getLookupFn$1 } from "@polkadot-api/metadata-builders";
|
|
2
|
+
import { Binary, Binary as Binary$1, Blake2128, Blake2128Concat, Blake2256, Blake3256, Blake3256Concat, Twox128, Twox128 as Twox128$1, Twox256, Twox64Concat, decAnyMetadata, decAnyMetadata as decAnyMetadata$1, metadata, unifyMetadata, unifyMetadata as unifyMetadata$1 } from "@polkadot-api/substrate-bindings";
|
|
3
|
+
import { fromHex, mergeUint8, toHex, toHex as toHex$1 } from "@polkadot-api/utils";
|
|
17
4
|
import anylogger from "anylogger";
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
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
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
const pallet = metadata2.pallets.find((pallet2) => pallet2.name === palletName);
|
|
92
|
-
if (!pallet) return [];
|
|
93
|
-
pallet.calls = void 0;
|
|
94
|
-
pallet.constants = constantNames ? pallet.constants.filter((constant) => constantNames.includes(constant.name)) : [];
|
|
95
|
-
if ("docs" in pallet) pallet.docs = [];
|
|
96
|
-
pallet.errors = void 0;
|
|
97
|
-
pallet.events = void 0;
|
|
98
|
-
if (!pallet.storage) return [];
|
|
99
|
-
pallet.storage.items = pallet.storage.items.filter(
|
|
100
|
-
(item) => itemNames.some((itemName) => item.name === itemName)
|
|
101
|
-
);
|
|
102
|
-
return [
|
|
103
|
-
...pallet.storage.items.flatMap((item) => [
|
|
104
|
-
// each type can be either "Plain" or "Map"
|
|
105
|
-
// if it's "Plain" we only need to get the value type
|
|
106
|
-
// if it's a "Map" we want to keep both the key AND the value types
|
|
107
|
-
item.type.tag === "plain" && item.type.value,
|
|
108
|
-
item.type.tag === "map" && item.type.value.key,
|
|
109
|
-
item.type.tag === "map" && item.type.value.value
|
|
110
|
-
]).filter((type) => typeof type === "number"),
|
|
111
|
-
...pallet.constants.flatMap((constant) => constant.type)
|
|
112
|
-
];
|
|
113
|
-
}
|
|
114
|
-
);
|
|
115
|
-
let runtimeApisKeepTypes = [];
|
|
116
|
-
if ("apis" in metadata2) {
|
|
117
|
-
metadata2.apis = metadata2.apis.filter(
|
|
118
|
-
(runtimeApi) => runtimeApisAndMethods.some(
|
|
119
|
-
({ runtimeApi: runtimeApiName }) => runtimeApi.name === runtimeApiName
|
|
120
|
-
)
|
|
121
|
-
);
|
|
122
|
-
runtimeApisKeepTypes = runtimeApisAndMethods.flatMap(
|
|
123
|
-
({ runtimeApi: runtimeApiName, methods: methodNames }) => {
|
|
124
|
-
const runtimeApi = metadata2.apis.find((runtimeApi2) => runtimeApi2.name === runtimeApiName);
|
|
125
|
-
if (!runtimeApi) return [];
|
|
126
|
-
runtimeApi.docs = [];
|
|
127
|
-
if (!runtimeApi.methods) return [];
|
|
128
|
-
runtimeApi.methods = runtimeApi.methods.filter(
|
|
129
|
-
(method) => methodNames.some((methodName) => method.name === methodName)
|
|
130
|
-
);
|
|
131
|
-
return runtimeApi.methods.flatMap((method) => [
|
|
132
|
-
// each method has an array of input types (for the params)
|
|
133
|
-
...method.inputs.map((input) => input.type),
|
|
134
|
-
// and one output type (for the result)
|
|
135
|
-
method.output
|
|
136
|
-
]);
|
|
137
|
-
}
|
|
138
|
-
);
|
|
139
|
-
}
|
|
140
|
-
const keepTypes = /* @__PURE__ */ new Set([...palletsKeepTypes, ...runtimeApisKeepTypes]);
|
|
141
|
-
extraKeepTypes?.forEach((type) => keepTypes.add(type));
|
|
142
|
-
const metadataTysMap = new Map(metadata2.lookup.map((ty) => [ty.id, ty]));
|
|
143
|
-
addDependentTypes(metadataTysMap, keepTypes, [...keepTypes]);
|
|
144
|
-
metadata2.lookup = metadata2.lookup.filter((type) => keepTypes.has(type.id));
|
|
145
|
-
const newTypeIds = /* @__PURE__ */ new Map();
|
|
146
|
-
metadata2.lookup.forEach((type, index) => newTypeIds.set(type.id, index));
|
|
147
|
-
const getNewTypeId = (oldTypeId) => {
|
|
148
|
-
const newTypeId = newTypeIds.get(oldTypeId);
|
|
149
|
-
if (typeof newTypeId !== "number") log_default.error(`Failed to find newTypeId for type ${oldTypeId}`);
|
|
150
|
-
return newTypeId ?? 0;
|
|
151
|
-
};
|
|
152
|
-
remapTypeIds(metadata2, getNewTypeId);
|
|
153
|
-
if ("address" in metadata2.extrinsic) metadata2.extrinsic.address = 0;
|
|
154
|
-
if ("call" in metadata2.extrinsic) metadata2.extrinsic.call = 0;
|
|
155
|
-
if ("signature" in metadata2.extrinsic) metadata2.extrinsic.signature = 0;
|
|
156
|
-
if ("extra" in metadata2.extrinsic) metadata2.extrinsic.extra = 0;
|
|
157
|
-
if ("signedExtensions" in metadata2.extrinsic) metadata2.extrinsic.signedExtensions = [];
|
|
158
|
-
if ("outerEnums" in metadata2) {
|
|
159
|
-
metadata2.outerEnums.call = 0;
|
|
160
|
-
metadata2.outerEnums.error = 0;
|
|
161
|
-
metadata2.outerEnums.event = 0;
|
|
162
|
-
}
|
|
163
|
-
};
|
|
164
|
-
var isCompactableMetadata = (metadata2) => {
|
|
165
|
-
switch (metadata2.metadata.tag) {
|
|
166
|
-
case "v14":
|
|
167
|
-
case "v15":
|
|
168
|
-
case "v16":
|
|
169
|
-
return true;
|
|
170
|
-
default:
|
|
171
|
-
return false;
|
|
172
|
-
}
|
|
5
|
+
import { Struct, u32, u8 } from "scale-ts";
|
|
6
|
+
//#region src/papito.ts
|
|
7
|
+
/** Constant: https://docs.substrate.io/build/application-development/#metadata-format */
|
|
8
|
+
const magicNumber = 1635018093;
|
|
9
|
+
//#endregion
|
|
10
|
+
//#region src/log.ts
|
|
11
|
+
var log_default = anylogger("@talismn/scale");
|
|
12
|
+
//#endregion
|
|
13
|
+
//#region src/util/compactMetadata.ts
|
|
14
|
+
/**
|
|
15
|
+
* Converts a `Metadata` into a `MiniMetadata`.
|
|
16
|
+
*
|
|
17
|
+
* A `MiniMetadata` only contains the types inside of its `lookup` which are relevant for
|
|
18
|
+
* the storage queries specified in `palletsAndItems`.
|
|
19
|
+
*
|
|
20
|
+
* E.g. if `palletsAndItems` is `{ pallet: "System", items: ["Account"] }`, then only the
|
|
21
|
+
* types used in the `System.Account` storage query will remain inside of metadata.lookups.
|
|
22
|
+
*/
|
|
23
|
+
const compactMetadata = (anyMetadata, palletsAndItems = [], runtimeApisAndMethods = [], extraKeepTypes = []) => {
|
|
24
|
+
if (!isCompactableMetadata(anyMetadata)) throw new Error(`Metadata version ${anyMetadata.metadata.tag} not supported in compactMetadata`);
|
|
25
|
+
const metadata = anyMetadata.metadata.value;
|
|
26
|
+
metadata.pallets = metadata.pallets.filter((pallet) => palletsAndItems.some(({ pallet: palletName }) => pallet.name === palletName));
|
|
27
|
+
const palletsKeepTypes = palletsAndItems.flatMap(({ pallet: palletName, constants: constantNames, items: itemNames }) => {
|
|
28
|
+
const pallet = metadata.pallets.find((pallet) => pallet.name === palletName);
|
|
29
|
+
if (!pallet) return [];
|
|
30
|
+
pallet.calls = void 0;
|
|
31
|
+
pallet.constants = constantNames ? pallet.constants.filter((constant) => constantNames.includes(constant.name)) : [];
|
|
32
|
+
if ("docs" in pallet) pallet.docs = [];
|
|
33
|
+
pallet.errors = void 0;
|
|
34
|
+
pallet.events = void 0;
|
|
35
|
+
if (!pallet.storage) return [];
|
|
36
|
+
pallet.storage.items = pallet.storage.items.filter((item) => itemNames.some((itemName) => item.name === itemName));
|
|
37
|
+
return [...pallet.storage.items.flatMap((item) => [
|
|
38
|
+
item.type.tag === "plain" && item.type.value,
|
|
39
|
+
item.type.tag === "map" && item.type.value.key,
|
|
40
|
+
item.type.tag === "map" && item.type.value.value
|
|
41
|
+
]).filter((type) => typeof type === "number"), ...pallet.constants.flatMap((constant) => constant.type)];
|
|
42
|
+
});
|
|
43
|
+
let runtimeApisKeepTypes = [];
|
|
44
|
+
if ("apis" in metadata) {
|
|
45
|
+
metadata.apis = metadata.apis.filter((runtimeApi) => runtimeApisAndMethods.some(({ runtimeApi: runtimeApiName }) => runtimeApi.name === runtimeApiName));
|
|
46
|
+
runtimeApisKeepTypes = runtimeApisAndMethods.flatMap(({ runtimeApi: runtimeApiName, methods: methodNames }) => {
|
|
47
|
+
const runtimeApi = metadata.apis.find((runtimeApi) => runtimeApi.name === runtimeApiName);
|
|
48
|
+
if (!runtimeApi) return [];
|
|
49
|
+
runtimeApi.docs = [];
|
|
50
|
+
if (!runtimeApi.methods) return [];
|
|
51
|
+
runtimeApi.methods = runtimeApi.methods.filter((method) => methodNames.some((methodName) => method.name === methodName));
|
|
52
|
+
return runtimeApi.methods.flatMap((method) => [...method.inputs.map((input) => input.type), method.output]);
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
const keepTypes = /* @__PURE__ */ new Set([...palletsKeepTypes, ...runtimeApisKeepTypes]);
|
|
56
|
+
extraKeepTypes?.forEach((type) => keepTypes.add(type));
|
|
57
|
+
const metadataTysMap = new Map(metadata.lookup.map((ty) => [ty.id, ty]));
|
|
58
|
+
addDependentTypes(metadataTysMap, keepTypes, [...keepTypes]);
|
|
59
|
+
metadata.lookup = metadata.lookup.filter((type) => keepTypes.has(type.id));
|
|
60
|
+
const newTypeIds = /* @__PURE__ */ new Map();
|
|
61
|
+
metadata.lookup.forEach((type, index) => newTypeIds.set(type.id, index));
|
|
62
|
+
const getNewTypeId = (oldTypeId) => {
|
|
63
|
+
const newTypeId = newTypeIds.get(oldTypeId);
|
|
64
|
+
if (typeof newTypeId !== "number") log_default.error(`Failed to find newTypeId for type ${oldTypeId}`);
|
|
65
|
+
return newTypeId ?? 0;
|
|
66
|
+
};
|
|
67
|
+
remapTypeIds(metadata, getNewTypeId);
|
|
68
|
+
if ("address" in metadata.extrinsic) metadata.extrinsic.address = 0;
|
|
69
|
+
if ("call" in metadata.extrinsic) metadata.extrinsic.call = 0;
|
|
70
|
+
if ("signature" in metadata.extrinsic) metadata.extrinsic.signature = 0;
|
|
71
|
+
if ("extra" in metadata.extrinsic) metadata.extrinsic.extra = 0;
|
|
72
|
+
if ("signedExtensions" in metadata.extrinsic) metadata.extrinsic.signedExtensions = [];
|
|
73
|
+
if ("outerEnums" in metadata) {
|
|
74
|
+
metadata.outerEnums.call = 0;
|
|
75
|
+
metadata.outerEnums.error = 0;
|
|
76
|
+
metadata.outerEnums.event = 0;
|
|
77
|
+
}
|
|
173
78
|
};
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
}
|
|
182
|
-
if (addedTypes.has(type.id)) continue;
|
|
183
|
-
keepTypes.add(type.id);
|
|
184
|
-
addedTypes.add(type.id);
|
|
185
|
-
const paramTypes = type.params.map((param) => param.type).filter((type2) => typeof type2 === "number");
|
|
186
|
-
addDependentSubTypes(paramTypes);
|
|
187
|
-
switch (type.def.tag) {
|
|
188
|
-
case "array":
|
|
189
|
-
addDependentSubTypes([type.def.value.type]);
|
|
190
|
-
break;
|
|
191
|
-
case "bitSequence":
|
|
192
|
-
addDependentSubTypes([type.def.value.bitOrderType, type.def.value.bitStoreType]);
|
|
193
|
-
break;
|
|
194
|
-
case "compact":
|
|
195
|
-
addDependentSubTypes([type.def.value]);
|
|
196
|
-
break;
|
|
197
|
-
case "composite":
|
|
198
|
-
addDependentSubTypes(
|
|
199
|
-
type.def.value.map((field) => field.type).filter((type2) => typeof type2 === "number")
|
|
200
|
-
);
|
|
201
|
-
break;
|
|
202
|
-
case "primitive":
|
|
203
|
-
break;
|
|
204
|
-
case "sequence":
|
|
205
|
-
addDependentSubTypes([type.def.value]);
|
|
206
|
-
break;
|
|
207
|
-
case "tuple":
|
|
208
|
-
addDependentSubTypes(
|
|
209
|
-
type.def.value.filter((type2) => typeof type2 === "number")
|
|
210
|
-
);
|
|
211
|
-
break;
|
|
212
|
-
case "variant":
|
|
213
|
-
addDependentSubTypes(
|
|
214
|
-
type.def.value.flatMap((member) => member.fields.map((field) => field.type)).filter((type2) => typeof type2 === "number")
|
|
215
|
-
);
|
|
216
|
-
break;
|
|
217
|
-
default: {
|
|
218
|
-
const exhaustiveCheck = type.def;
|
|
219
|
-
log_default.error(`Unhandled V15Type type ${exhaustiveCheck}`);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
}
|
|
79
|
+
const isCompactableMetadata = (metadata) => {
|
|
80
|
+
switch (metadata.metadata.tag) {
|
|
81
|
+
case "v14":
|
|
82
|
+
case "v15":
|
|
83
|
+
case "v16": return true;
|
|
84
|
+
default: return false;
|
|
85
|
+
}
|
|
223
86
|
};
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
87
|
+
const addDependentTypes = (metadataTysMap, keepTypes, types, addedTypes = /* @__PURE__ */ new Set()) => {
|
|
88
|
+
const addDependentSubTypes = (subTypes) => addDependentTypes(metadataTysMap, keepTypes, subTypes, addedTypes);
|
|
89
|
+
for (const typeId of types) {
|
|
90
|
+
const type = metadataTysMap.get(typeId);
|
|
91
|
+
if (!type) {
|
|
92
|
+
log_default.warn(`Unable to find type with id ${typeId}`);
|
|
93
|
+
continue;
|
|
94
|
+
}
|
|
95
|
+
if (addedTypes.has(type.id)) continue;
|
|
96
|
+
keepTypes.add(type.id);
|
|
97
|
+
addedTypes.add(type.id);
|
|
98
|
+
addDependentSubTypes(type.params.map((param) => param.type).filter((type) => typeof type === "number"));
|
|
99
|
+
switch (type.def.tag) {
|
|
100
|
+
case "array":
|
|
101
|
+
addDependentSubTypes([type.def.value.type]);
|
|
102
|
+
break;
|
|
103
|
+
case "bitSequence":
|
|
104
|
+
addDependentSubTypes([type.def.value.bitOrderType, type.def.value.bitStoreType]);
|
|
105
|
+
break;
|
|
106
|
+
case "compact":
|
|
107
|
+
addDependentSubTypes([type.def.value]);
|
|
108
|
+
break;
|
|
109
|
+
case "composite":
|
|
110
|
+
addDependentSubTypes(type.def.value.map((field) => field.type).filter((type) => typeof type === "number"));
|
|
111
|
+
break;
|
|
112
|
+
case "primitive": break;
|
|
113
|
+
case "sequence":
|
|
114
|
+
addDependentSubTypes([type.def.value]);
|
|
115
|
+
break;
|
|
116
|
+
case "tuple":
|
|
117
|
+
addDependentSubTypes(type.def.value.filter((type) => typeof type === "number"));
|
|
118
|
+
break;
|
|
119
|
+
case "variant":
|
|
120
|
+
addDependentSubTypes(type.def.value.flatMap((member) => member.fields.map((field) => field.type)).filter((type) => typeof type === "number"));
|
|
121
|
+
break;
|
|
122
|
+
default: {
|
|
123
|
+
const exhaustiveCheck = type.def;
|
|
124
|
+
log_default.error(`Unhandled V15Type type ${exhaustiveCheck}`);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
228
128
|
};
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
if (typeof param.type !== "number") continue;
|
|
234
|
-
param.type = getNewTypeId(param.type);
|
|
235
|
-
}
|
|
236
|
-
switch (type.def.tag) {
|
|
237
|
-
case "array":
|
|
238
|
-
type.def.value.type = getNewTypeId(type.def.value.type);
|
|
239
|
-
break;
|
|
240
|
-
case "bitSequence":
|
|
241
|
-
type.def.value.bitOrderType = getNewTypeId(type.def.value.bitOrderType);
|
|
242
|
-
type.def.value.bitStoreType = getNewTypeId(type.def.value.bitStoreType);
|
|
243
|
-
break;
|
|
244
|
-
case "compact":
|
|
245
|
-
type.def.value = getNewTypeId(type.def.value);
|
|
246
|
-
break;
|
|
247
|
-
case "composite":
|
|
248
|
-
for (const field of type.def.value) {
|
|
249
|
-
if (typeof field.type !== "number") continue;
|
|
250
|
-
field.type = getNewTypeId(field.type);
|
|
251
|
-
}
|
|
252
|
-
break;
|
|
253
|
-
case "primitive":
|
|
254
|
-
break;
|
|
255
|
-
case "sequence":
|
|
256
|
-
type.def.value = getNewTypeId(type.def.value);
|
|
257
|
-
break;
|
|
258
|
-
case "tuple":
|
|
259
|
-
type.def.value = type.def.value.map((type2) => {
|
|
260
|
-
if (typeof type2 !== "number") return type2;
|
|
261
|
-
return getNewTypeId(type2);
|
|
262
|
-
});
|
|
263
|
-
break;
|
|
264
|
-
case "variant":
|
|
265
|
-
for (const member of type.def.value) {
|
|
266
|
-
for (const field of member.fields) {
|
|
267
|
-
if (typeof field.type !== "number") continue;
|
|
268
|
-
field.type = getNewTypeId(field.type);
|
|
269
|
-
}
|
|
270
|
-
}
|
|
271
|
-
break;
|
|
272
|
-
default: {
|
|
273
|
-
const exhaustiveCheck = type.def;
|
|
274
|
-
log_default.error(`Unhandled V15Type type ${exhaustiveCheck}`);
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
}
|
|
129
|
+
const remapTypeIds = (metadata, getNewTypeId) => {
|
|
130
|
+
remapLookupTypeIds(metadata, getNewTypeId);
|
|
131
|
+
remapStorageTypeIds(metadata, getNewTypeId);
|
|
132
|
+
remapRuntimeApisTypeIds(metadata, getNewTypeId);
|
|
278
133
|
};
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
134
|
+
const remapLookupTypeIds = (metadata, getNewTypeId) => {
|
|
135
|
+
for (const type of metadata.lookup) {
|
|
136
|
+
type.id = getNewTypeId(type.id);
|
|
137
|
+
for (const param of type.params) {
|
|
138
|
+
if (typeof param.type !== "number") continue;
|
|
139
|
+
param.type = getNewTypeId(param.type);
|
|
140
|
+
}
|
|
141
|
+
switch (type.def.tag) {
|
|
142
|
+
case "array":
|
|
143
|
+
type.def.value.type = getNewTypeId(type.def.value.type);
|
|
144
|
+
break;
|
|
145
|
+
case "bitSequence":
|
|
146
|
+
type.def.value.bitOrderType = getNewTypeId(type.def.value.bitOrderType);
|
|
147
|
+
type.def.value.bitStoreType = getNewTypeId(type.def.value.bitStoreType);
|
|
148
|
+
break;
|
|
149
|
+
case "compact":
|
|
150
|
+
type.def.value = getNewTypeId(type.def.value);
|
|
151
|
+
break;
|
|
152
|
+
case "composite":
|
|
153
|
+
for (const field of type.def.value) {
|
|
154
|
+
if (typeof field.type !== "number") continue;
|
|
155
|
+
field.type = getNewTypeId(field.type);
|
|
156
|
+
}
|
|
157
|
+
break;
|
|
158
|
+
case "primitive": break;
|
|
159
|
+
case "sequence":
|
|
160
|
+
type.def.value = getNewTypeId(type.def.value);
|
|
161
|
+
break;
|
|
162
|
+
case "tuple":
|
|
163
|
+
type.def.value = type.def.value.map((type) => {
|
|
164
|
+
if (typeof type !== "number") return type;
|
|
165
|
+
return getNewTypeId(type);
|
|
166
|
+
});
|
|
167
|
+
break;
|
|
168
|
+
case "variant":
|
|
169
|
+
for (const member of type.def.value) for (const field of member.fields) {
|
|
170
|
+
if (typeof field.type !== "number") continue;
|
|
171
|
+
field.type = getNewTypeId(field.type);
|
|
172
|
+
}
|
|
173
|
+
break;
|
|
174
|
+
default: {
|
|
175
|
+
const exhaustiveCheck = type.def;
|
|
176
|
+
log_default.error(`Unhandled V15Type type ${exhaustiveCheck}`);
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
}
|
|
292
180
|
};
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
181
|
+
const remapStorageTypeIds = (metadata, getNewTypeId) => {
|
|
182
|
+
for (const pallet of metadata.pallets) {
|
|
183
|
+
for (const item of pallet.storage?.items ?? []) {
|
|
184
|
+
if (item.type.tag === "plain") item.type.value = getNewTypeId(item.type.value);
|
|
185
|
+
if (item.type.tag === "map") {
|
|
186
|
+
item.type.value.key = getNewTypeId(item.type.value.key);
|
|
187
|
+
item.type.value.value = getNewTypeId(item.type.value.value);
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
for (const constant of pallet.constants ?? []) constant.type = getNewTypeId(constant.type);
|
|
191
|
+
}
|
|
302
192
|
};
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
return scaleCoder?.value?.dec(change) ?? null;
|
|
309
|
-
} catch (cause) {
|
|
310
|
-
log_default.warn(error ?? `Failed to decode ${change}`, cause);
|
|
311
|
-
return null;
|
|
312
|
-
}
|
|
193
|
+
const remapRuntimeApisTypeIds = (metadata, getNewTypeId) => {
|
|
194
|
+
for (const runtimeApi of metadata.apis) for (const method of runtimeApi.methods ?? []) {
|
|
195
|
+
for (const input of method.inputs) input.type = getNewTypeId(input.type);
|
|
196
|
+
method.output = getNewTypeId(method.output);
|
|
197
|
+
}
|
|
313
198
|
};
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
|
|
324
|
-
return;
|
|
325
|
-
}
|
|
199
|
+
//#endregion
|
|
200
|
+
//#region src/util/decodeScale.ts
|
|
201
|
+
const decodeScale = (scaleCoder, change, error) => {
|
|
202
|
+
if (change === null) return null;
|
|
203
|
+
try {
|
|
204
|
+
return scaleCoder?.value?.dec(change) ?? null;
|
|
205
|
+
} catch (cause) {
|
|
206
|
+
log_default.warn(error ?? `Failed to decode ${change}`, cause);
|
|
207
|
+
return null;
|
|
208
|
+
}
|
|
326
209
|
};
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
210
|
+
//#endregion
|
|
211
|
+
//#region src/util/encodeMetadata.ts
|
|
212
|
+
const encodeMetadata = (metadata$1) => toHex(metadata.enc(metadata$1));
|
|
213
|
+
//#endregion
|
|
214
|
+
//#region src/util/encodeStateKey.ts
|
|
215
|
+
const encodeStateKey = (scaleCoder, error, ...args) => {
|
|
216
|
+
try {
|
|
217
|
+
return scaleCoder?.keys?.enc(...args);
|
|
218
|
+
} catch (cause) {
|
|
219
|
+
log_default.warn(error ?? `Failed to encode stateKey ${JSON.stringify(args)}`, cause);
|
|
220
|
+
return;
|
|
221
|
+
}
|
|
337
222
|
};
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
223
|
+
//#endregion
|
|
224
|
+
//#region src/util/parseMetadataRpc.ts
|
|
225
|
+
const parseMetadataRpc = (metadataRpc) => {
|
|
226
|
+
const metadata = decAnyMetadata$1(metadataRpc);
|
|
227
|
+
const unifiedMetadata = unifyMetadata$1(metadata);
|
|
228
|
+
const lookupFn = getLookupFn$1(unifiedMetadata);
|
|
229
|
+
return {
|
|
230
|
+
metadata,
|
|
231
|
+
unifiedMetadata,
|
|
232
|
+
lookupFn,
|
|
233
|
+
builder: getDynamicBuilder$1(lookupFn)
|
|
234
|
+
};
|
|
343
235
|
};
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
return storageCodec.dec(encodedValue);
|
|
350
|
-
} catch (err) {
|
|
351
|
-
log_default.error("Failed to get constant value from metadata", {
|
|
352
|
-
err,
|
|
353
|
-
pallet,
|
|
354
|
-
constant
|
|
355
|
-
});
|
|
356
|
-
throw err;
|
|
357
|
-
}
|
|
236
|
+
//#endregion
|
|
237
|
+
//#region src/util/getConstantValueFromMetadata.ts
|
|
238
|
+
const getConstantValueFromMetadata = (metadata, pallet, constant) => {
|
|
239
|
+
const { builder, unifiedMetadata } = typeof metadata === "string" ? parseMetadataRpc(metadata) : metadata;
|
|
240
|
+
return getConstantValueInner(builder, unifiedMetadata, pallet, constant);
|
|
358
241
|
};
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
242
|
+
const getConstantValueInner = (builder, unifiedMetadata, pallet, constant) => {
|
|
243
|
+
try {
|
|
244
|
+
const storageCodec = builder.buildConstant(pallet, constant);
|
|
245
|
+
const encodedValue = unifiedMetadata.pallets.find(({ name }) => name === pallet)?.constants.find(({ name }) => name === constant)?.value;
|
|
246
|
+
if (!encodedValue) throw new Error(`Constant ${pallet}.${constant} not found`);
|
|
247
|
+
return storageCodec.dec(encodedValue);
|
|
248
|
+
} catch (err) {
|
|
249
|
+
log_default.error("Failed to get constant value from metadata", {
|
|
250
|
+
err,
|
|
251
|
+
pallet,
|
|
252
|
+
constant
|
|
253
|
+
});
|
|
254
|
+
throw err;
|
|
255
|
+
}
|
|
371
256
|
};
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
257
|
+
//#endregion
|
|
258
|
+
//#region src/util/getMetadataVersion.ts
|
|
259
|
+
/**
|
|
260
|
+
* Extracts the `version` u8 from a SCALE-encoded metadata blob and returns it as a `number`.
|
|
261
|
+
*
|
|
262
|
+
* Only reads the first 40 bytes of the blob.
|
|
263
|
+
*/
|
|
264
|
+
const getMetadataVersion = (metadataRpc) => {
|
|
265
|
+
try {
|
|
266
|
+
return Struct({
|
|
267
|
+
magicNumber: u32,
|
|
268
|
+
version: u8
|
|
269
|
+
}).dec(metadataRpc).version;
|
|
270
|
+
} catch {
|
|
271
|
+
return 0;
|
|
272
|
+
}
|
|
383
273
|
};
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
Twox128 as Twox1282,
|
|
394
|
-
Twox256
|
|
395
|
-
} from "@polkadot-api/substrate-bindings";
|
|
396
|
-
|
|
397
|
-
// src/util/serdePapi.ts
|
|
398
|
-
import { Binary as Binary2 } from "@polkadot-api/substrate-bindings";
|
|
399
|
-
var papiParse = (text) => {
|
|
400
|
-
const reviver = (_key, value) => {
|
|
401
|
-
if (typeof value !== "string") return value;
|
|
402
|
-
if (value.startsWith("bigint:")) return BigInt(value.slice("bigint:".length));
|
|
403
|
-
if (value.startsWith("hex:")) return Binary2.fromHex(value.slice("hex:".length));
|
|
404
|
-
if (value.startsWith("bin:")) return Binary2.fromText(value.slice("bin:".length));
|
|
405
|
-
return value;
|
|
406
|
-
};
|
|
407
|
-
if (typeof text !== "string") return text;
|
|
408
|
-
return JSON.parse(text, reviver);
|
|
274
|
+
//#endregion
|
|
275
|
+
//#region src/util/getStorageKeyPrefix.ts
|
|
276
|
+
const getStorageKeyPrefix = (palletName, storageName) => {
|
|
277
|
+
const palletHash = Twox128$1(new TextEncoder().encode(palletName));
|
|
278
|
+
const storageHash = Twox128$1(new TextEncoder().encode(storageName));
|
|
279
|
+
const combined = new Uint8Array(palletHash.length + storageHash.length);
|
|
280
|
+
combined.set(palletHash, 0);
|
|
281
|
+
combined.set(storageHash, palletHash.length);
|
|
282
|
+
return toHex$1(combined);
|
|
409
283
|
};
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
284
|
+
//#endregion
|
|
285
|
+
//#region src/util/serdePapi.ts
|
|
286
|
+
/**
|
|
287
|
+
* For the substrate-tokens (and other) modules, we configure the `onChainId` field in chaindata to tell the module how to query each token.
|
|
288
|
+
* These queries are made to the tokens pallet.
|
|
289
|
+
* E.g. api.query.Tokens.Account(accountAddress, papiParse(onChainId))
|
|
290
|
+
*
|
|
291
|
+
* The `onChainId` field on chaindata must be a JSON-parseable string, but a decoded papi value can
|
|
292
|
+
* contain types that plain JSON cannot represent. `papiStringify`/`papiParse` are inverse functions
|
|
293
|
+
* that serialise *any* decoded papi value to a deterministic string and back.
|
|
294
|
+
*
|
|
295
|
+
* Only two decoded types are not valid JSON, so only these get a tag — everything else (numbers,
|
|
296
|
+
* objects, and strings, including the `0x…` hex strings papi v2 decodes fixed-size `[u8; N]` fields
|
|
297
|
+
* into) is already JSON and is left untouched:
|
|
298
|
+
* - `bigint` → `bigint:<n>`
|
|
299
|
+
* - `Uint8Array` → `u8a:0x<hex>` (papi v2 decodes variable-length `Vec<u8>` to a `Uint8Array`)
|
|
300
|
+
*
|
|
301
|
+
* Note on polkadot-api v2 byte fields. The `Binary` class was removed and the SCALE codecs split
|
|
302
|
+
* byte decoding into two distinct JS types, which `papiParse` must reproduce exactly or the codec
|
|
303
|
+
* silently builds a garbage statekey (→ no balance):
|
|
304
|
+
* - fixed-size `[u8; N]` (`AccountId32`, `AccountKey20`, `GeneralKey.data`, Erc20 addresses,
|
|
305
|
+
* Stellar code/issuer, …) → a plain `0x` hex **string** (`SizedHex`). The codec's `.enc`
|
|
306
|
+
* expects that string. So a fixed-size field is just a JSON string: no tag, round-trips as-is.
|
|
307
|
+
* - variable-length `Vec<u8>` → a **`Uint8Array`**. The codec's `.enc` expects a `Uint8Array`,
|
|
308
|
+
* NOT a hex string. Hence the dedicated `u8a:` tag, which `papiParse` restores to a `Uint8Array`.
|
|
309
|
+
*
|
|
310
|
+
* `hex:` and `bin:` are legacy/authoring **input** forms still accepted by `papiParse` (some
|
|
311
|
+
* chaindata token templates are hand-written with them), but `papiStringify` never emits them — it
|
|
312
|
+
* normalises to the bare canonical form. So the canonical output is deterministic and idempotent:
|
|
313
|
+
* `papiStringify(papiParse(canonical)) === canonical`.
|
|
314
|
+
*
|
|
315
|
+
* Some examples (input string → papiParse value):
|
|
316
|
+
* Input: `5` → `5`
|
|
317
|
+
* Input: `{ type: "LiquidCrowdloan", value: 13 }` → `Enum("LiquidCrowdloan", 13)`
|
|
318
|
+
* Input: `{ type: "NativeToken", value: "bigint:2" }` → `Enum("NativeToken", 2n)`
|
|
319
|
+
* Input: `{ type: "Erc20", value: "0x07df…bce" }` → `Enum("Erc20", "0x07df…bce")` (fixed → string)
|
|
320
|
+
* Input: `{ type: "Erc20", value: "hex:0x07df…bce" }` → `Enum("Erc20", "0x07df…bce")` (legacy input)
|
|
321
|
+
* Input: `{ code: "bin:TZS" }` → `{ code: "0x545a53" }` (legacy input)
|
|
322
|
+
* Input: `"u8a:0xdeadbeef"` → `Uint8Array([0xde,0xad,0xbe,0xef])` (variable bytes)
|
|
323
|
+
*/
|
|
324
|
+
const papiParse = (text) => {
|
|
325
|
+
const reviver = (_key, value) => {
|
|
326
|
+
if (typeof value !== "string") return value;
|
|
327
|
+
if (value.startsWith("bigint:")) return BigInt(value.slice(7));
|
|
328
|
+
if (value.startsWith("u8a:")) return Binary$1.fromHex(value.slice(4));
|
|
329
|
+
if (value.startsWith("hex:")) return value.slice(4);
|
|
330
|
+
if (value.startsWith("bin:")) return Binary$1.toHex(Binary$1.fromText(value.slice(4)));
|
|
331
|
+
return value;
|
|
332
|
+
};
|
|
333
|
+
if (typeof text !== "string") return text;
|
|
334
|
+
return JSON.parse(text, reviver);
|
|
417
335
|
};
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
FixedSizeBinary,
|
|
426
|
-
Twox1282 as Twox128,
|
|
427
|
-
Twox256,
|
|
428
|
-
Twox64Concat,
|
|
429
|
-
compactMetadata,
|
|
430
|
-
decAnyMetadata,
|
|
431
|
-
decodeScale,
|
|
432
|
-
encodeMetadata,
|
|
433
|
-
encodeStateKey,
|
|
434
|
-
fromHex,
|
|
435
|
-
getConstantValueFromMetadata,
|
|
436
|
-
getDynamicBuilder,
|
|
437
|
-
getLookupFn,
|
|
438
|
-
getMetadataVersion,
|
|
439
|
-
getStorageKeyPrefix,
|
|
440
|
-
magicNumber,
|
|
441
|
-
mergeUint8,
|
|
442
|
-
metadata,
|
|
443
|
-
papiParse,
|
|
444
|
-
papiStringify,
|
|
445
|
-
parseMetadataRpc,
|
|
446
|
-
toHex,
|
|
447
|
-
unifyMetadata
|
|
336
|
+
const papiStringify = (value, space) => {
|
|
337
|
+
const replacer = (_key, value) => {
|
|
338
|
+
if (typeof value === "bigint") return `bigint:${String(value)}`;
|
|
339
|
+
if (value instanceof Uint8Array) return `u8a:${Binary$1.toHex(value)}`;
|
|
340
|
+
return value;
|
|
341
|
+
};
|
|
342
|
+
return JSON.stringify(value, replacer, space);
|
|
448
343
|
};
|
|
344
|
+
//#endregion
|
|
345
|
+
export { Binary, Blake2128, Blake2128Concat, Blake2256, Blake3256, Blake3256Concat, Twox128, Twox256, Twox64Concat, compactMetadata, decAnyMetadata, decodeScale, encodeMetadata, encodeStateKey, fromHex, getConstantValueFromMetadata, getDynamicBuilder, getLookupFn, getMetadataVersion, getStorageKeyPrefix, magicNumber, mergeUint8, metadata, papiParse, papiStringify, parseMetadataRpc, toHex, unifyMetadata };
|
|
346
|
+
|
|
449
347
|
//# sourceMappingURL=index.mjs.map
|