@talismn/scale 1.0.0 → 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 +847 -842
- package/dist/index.d.mts.map +1 -0
- package/dist/index.d.ts +847 -842
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +457 -469
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +329 -430
- package/dist/index.mjs.map +1 -1
- package/package.json +4 -4
package/dist/index.mjs
CHANGED
|
@@ -1,448 +1,347 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
getLookupFn
|
|
5
|
-
} from "@polkadot-api/metadata-builders";
|
|
6
|
-
import {
|
|
7
|
-
Binary,
|
|
8
|
-
decAnyMetadata,
|
|
9
|
-
metadata,
|
|
10
|
-
unifyMetadata
|
|
11
|
-
} from "@polkadot-api/substrate-bindings";
|
|
12
|
-
import { fromHex, mergeUint8, toHex } from "@polkadot-api/utils";
|
|
13
|
-
var magicNumber = 1635018093;
|
|
14
|
-
|
|
15
|
-
// 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";
|
|
16
4
|
import anylogger from "anylogger";
|
|
17
|
-
|
|
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
|
-
const pallet = metadata2.pallets.find((pallet2) => pallet2.name === palletName);
|
|
91
|
-
if (!pallet) return [];
|
|
92
|
-
pallet.calls = void 0;
|
|
93
|
-
pallet.constants = constantNames ? pallet.constants.filter((constant) => constantNames.includes(constant.name)) : [];
|
|
94
|
-
if ("docs" in pallet) pallet.docs = [];
|
|
95
|
-
pallet.errors = void 0;
|
|
96
|
-
pallet.events = void 0;
|
|
97
|
-
if (!pallet.storage) return [];
|
|
98
|
-
pallet.storage.items = pallet.storage.items.filter(
|
|
99
|
-
(item) => itemNames.some((itemName) => item.name === itemName)
|
|
100
|
-
);
|
|
101
|
-
return [
|
|
102
|
-
...pallet.storage.items.flatMap((item) => [
|
|
103
|
-
// each type can be either "Plain" or "Map"
|
|
104
|
-
// if it's "Plain" we only need to get the value type
|
|
105
|
-
// if it's a "Map" we want to keep both the key AND the value types
|
|
106
|
-
item.type.tag === "plain" && item.type.value,
|
|
107
|
-
item.type.tag === "map" && item.type.value.key,
|
|
108
|
-
item.type.tag === "map" && item.type.value.value
|
|
109
|
-
]).filter((type) => typeof type === "number"),
|
|
110
|
-
...pallet.constants.flatMap((constant) => constant.type)
|
|
111
|
-
];
|
|
112
|
-
}
|
|
113
|
-
);
|
|
114
|
-
let runtimeApisKeepTypes = [];
|
|
115
|
-
if ("apis" in metadata2) {
|
|
116
|
-
metadata2.apis = metadata2.apis.filter(
|
|
117
|
-
(runtimeApi) => runtimeApisAndMethods.some(
|
|
118
|
-
({ runtimeApi: runtimeApiName }) => runtimeApi.name === runtimeApiName
|
|
119
|
-
)
|
|
120
|
-
);
|
|
121
|
-
runtimeApisKeepTypes = runtimeApisAndMethods.flatMap(
|
|
122
|
-
({ runtimeApi: runtimeApiName, methods: methodNames }) => {
|
|
123
|
-
const runtimeApi = metadata2.apis.find((runtimeApi2) => runtimeApi2.name === runtimeApiName);
|
|
124
|
-
if (!runtimeApi) return [];
|
|
125
|
-
runtimeApi.docs = [];
|
|
126
|
-
if (!runtimeApi.methods) return [];
|
|
127
|
-
runtimeApi.methods = runtimeApi.methods.filter(
|
|
128
|
-
(method) => methodNames.some((methodName) => method.name === methodName)
|
|
129
|
-
);
|
|
130
|
-
return runtimeApi.methods.flatMap((method) => [
|
|
131
|
-
// each method has an array of input types (for the params)
|
|
132
|
-
...method.inputs.map((input) => input.type),
|
|
133
|
-
// and one output type (for the result)
|
|
134
|
-
method.output
|
|
135
|
-
]);
|
|
136
|
-
}
|
|
137
|
-
);
|
|
138
|
-
}
|
|
139
|
-
const keepTypes = /* @__PURE__ */ new Set([...palletsKeepTypes, ...runtimeApisKeepTypes]);
|
|
140
|
-
extraKeepTypes?.forEach((type) => keepTypes.add(type));
|
|
141
|
-
const metadataTysMap = new Map(metadata2.lookup.map((ty) => [ty.id, ty]));
|
|
142
|
-
addDependentTypes(metadataTysMap, keepTypes, [...keepTypes]);
|
|
143
|
-
metadata2.lookup = metadata2.lookup.filter((type) => keepTypes.has(type.id));
|
|
144
|
-
const newTypeIds = /* @__PURE__ */ new Map();
|
|
145
|
-
metadata2.lookup.forEach((type, index) => newTypeIds.set(type.id, index));
|
|
146
|
-
const getNewTypeId = (oldTypeId) => {
|
|
147
|
-
const newTypeId = newTypeIds.get(oldTypeId);
|
|
148
|
-
if (typeof newTypeId !== "number") log_default.error(`Failed to find newTypeId for type ${oldTypeId}`);
|
|
149
|
-
return newTypeId ?? 0;
|
|
150
|
-
};
|
|
151
|
-
remapTypeIds(metadata2, getNewTypeId);
|
|
152
|
-
if ("address" in metadata2.extrinsic) metadata2.extrinsic.address = 0;
|
|
153
|
-
if ("call" in metadata2.extrinsic) metadata2.extrinsic.call = 0;
|
|
154
|
-
if ("signature" in metadata2.extrinsic) metadata2.extrinsic.signature = 0;
|
|
155
|
-
if ("extra" in metadata2.extrinsic) metadata2.extrinsic.extra = 0;
|
|
156
|
-
if ("signedExtensions" in metadata2.extrinsic) metadata2.extrinsic.signedExtensions = [];
|
|
157
|
-
if ("outerEnums" in metadata2) {
|
|
158
|
-
metadata2.outerEnums.call = 0;
|
|
159
|
-
metadata2.outerEnums.error = 0;
|
|
160
|
-
metadata2.outerEnums.event = 0;
|
|
161
|
-
}
|
|
162
|
-
};
|
|
163
|
-
var isCompactableMetadata = (metadata2) => {
|
|
164
|
-
switch (metadata2.metadata.tag) {
|
|
165
|
-
case "v14":
|
|
166
|
-
case "v15":
|
|
167
|
-
case "v16":
|
|
168
|
-
return true;
|
|
169
|
-
default:
|
|
170
|
-
return false;
|
|
171
|
-
}
|
|
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
|
+
}
|
|
172
78
|
};
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
}
|
|
181
|
-
if (addedTypes.has(type.id)) continue;
|
|
182
|
-
keepTypes.add(type.id);
|
|
183
|
-
addedTypes.add(type.id);
|
|
184
|
-
const paramTypes = type.params.map((param) => param.type).filter((type2) => typeof type2 === "number");
|
|
185
|
-
addDependentSubTypes(paramTypes);
|
|
186
|
-
switch (type.def.tag) {
|
|
187
|
-
case "array":
|
|
188
|
-
addDependentSubTypes([type.def.value.type]);
|
|
189
|
-
break;
|
|
190
|
-
case "bitSequence":
|
|
191
|
-
addDependentSubTypes([type.def.value.bitOrderType, type.def.value.bitStoreType]);
|
|
192
|
-
break;
|
|
193
|
-
case "compact":
|
|
194
|
-
addDependentSubTypes([type.def.value]);
|
|
195
|
-
break;
|
|
196
|
-
case "composite":
|
|
197
|
-
addDependentSubTypes(
|
|
198
|
-
type.def.value.map((field) => field.type).filter((type2) => typeof type2 === "number")
|
|
199
|
-
);
|
|
200
|
-
break;
|
|
201
|
-
case "primitive":
|
|
202
|
-
break;
|
|
203
|
-
case "sequence":
|
|
204
|
-
addDependentSubTypes([type.def.value]);
|
|
205
|
-
break;
|
|
206
|
-
case "tuple":
|
|
207
|
-
addDependentSubTypes(
|
|
208
|
-
type.def.value.filter((type2) => typeof type2 === "number")
|
|
209
|
-
);
|
|
210
|
-
break;
|
|
211
|
-
case "variant":
|
|
212
|
-
addDependentSubTypes(
|
|
213
|
-
type.def.value.flatMap((member) => member.fields.map((field) => field.type)).filter((type2) => typeof type2 === "number")
|
|
214
|
-
);
|
|
215
|
-
break;
|
|
216
|
-
default: {
|
|
217
|
-
const exhaustiveCheck = type.def;
|
|
218
|
-
log_default.error(`Unhandled V15Type type ${exhaustiveCheck}`);
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
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
|
+
}
|
|
222
86
|
};
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
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
|
+
}
|
|
227
128
|
};
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
if (typeof param.type !== "number") continue;
|
|
233
|
-
param.type = getNewTypeId(param.type);
|
|
234
|
-
}
|
|
235
|
-
switch (type.def.tag) {
|
|
236
|
-
case "array":
|
|
237
|
-
type.def.value.type = getNewTypeId(type.def.value.type);
|
|
238
|
-
break;
|
|
239
|
-
case "bitSequence":
|
|
240
|
-
type.def.value.bitOrderType = getNewTypeId(type.def.value.bitOrderType);
|
|
241
|
-
type.def.value.bitStoreType = getNewTypeId(type.def.value.bitStoreType);
|
|
242
|
-
break;
|
|
243
|
-
case "compact":
|
|
244
|
-
type.def.value = getNewTypeId(type.def.value);
|
|
245
|
-
break;
|
|
246
|
-
case "composite":
|
|
247
|
-
for (const field of type.def.value) {
|
|
248
|
-
if (typeof field.type !== "number") continue;
|
|
249
|
-
field.type = getNewTypeId(field.type);
|
|
250
|
-
}
|
|
251
|
-
break;
|
|
252
|
-
case "primitive":
|
|
253
|
-
break;
|
|
254
|
-
case "sequence":
|
|
255
|
-
type.def.value = getNewTypeId(type.def.value);
|
|
256
|
-
break;
|
|
257
|
-
case "tuple":
|
|
258
|
-
type.def.value = type.def.value.map((type2) => {
|
|
259
|
-
if (typeof type2 !== "number") return type2;
|
|
260
|
-
return getNewTypeId(type2);
|
|
261
|
-
});
|
|
262
|
-
break;
|
|
263
|
-
case "variant":
|
|
264
|
-
for (const member of type.def.value) {
|
|
265
|
-
for (const field of member.fields) {
|
|
266
|
-
if (typeof field.type !== "number") continue;
|
|
267
|
-
field.type = getNewTypeId(field.type);
|
|
268
|
-
}
|
|
269
|
-
}
|
|
270
|
-
break;
|
|
271
|
-
default: {
|
|
272
|
-
const exhaustiveCheck = type.def;
|
|
273
|
-
log_default.error(`Unhandled V15Type type ${exhaustiveCheck}`);
|
|
274
|
-
}
|
|
275
|
-
}
|
|
276
|
-
}
|
|
129
|
+
const remapTypeIds = (metadata, getNewTypeId) => {
|
|
130
|
+
remapLookupTypeIds(metadata, getNewTypeId);
|
|
131
|
+
remapStorageTypeIds(metadata, getNewTypeId);
|
|
132
|
+
remapRuntimeApisTypeIds(metadata, getNewTypeId);
|
|
277
133
|
};
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
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
|
+
}
|
|
291
180
|
};
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
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
|
+
}
|
|
301
192
|
};
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
|
|
306
|
-
|
|
307
|
-
return scaleCoder?.value?.dec(change) ?? null;
|
|
308
|
-
} catch (cause) {
|
|
309
|
-
log_default.warn(error ?? `Failed to decode ${change}`, cause);
|
|
310
|
-
return null;
|
|
311
|
-
}
|
|
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
|
+
}
|
|
312
198
|
};
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
323
|
-
return;
|
|
324
|
-
}
|
|
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
|
+
}
|
|
325
209
|
};
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
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
|
+
}
|
|
336
222
|
};
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
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
|
+
};
|
|
342
235
|
};
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
return storageCodec.dec(encodedValue);
|
|
349
|
-
} catch (err) {
|
|
350
|
-
log_default.error("Failed to get constant value from metadata", {
|
|
351
|
-
err,
|
|
352
|
-
pallet,
|
|
353
|
-
constant
|
|
354
|
-
});
|
|
355
|
-
throw err;
|
|
356
|
-
}
|
|
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);
|
|
357
241
|
};
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
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
|
+
}
|
|
370
256
|
};
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
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
|
+
}
|
|
382
273
|
};
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
Twox128 as Twox1282,
|
|
393
|
-
Twox256
|
|
394
|
-
} from "@polkadot-api/substrate-bindings";
|
|
395
|
-
|
|
396
|
-
// src/util/serdePapi.ts
|
|
397
|
-
import { Binary as Binary2 } from "@polkadot-api/substrate-bindings";
|
|
398
|
-
var papiParse = (text) => {
|
|
399
|
-
const reviver = (_key, value) => {
|
|
400
|
-
if (typeof value !== "string") return value;
|
|
401
|
-
if (value.startsWith("bigint:")) return BigInt(value.slice("bigint:".length));
|
|
402
|
-
if (value.startsWith("u8a:")) return Binary2.fromHex(value.slice("u8a:".length));
|
|
403
|
-
if (value.startsWith("hex:")) return value.slice("hex:".length);
|
|
404
|
-
if (value.startsWith("bin:")) return Binary2.toHex(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
|
-
Twox1282 as Twox128,
|
|
426
|
-
Twox256,
|
|
427
|
-
Twox64Concat,
|
|
428
|
-
compactMetadata,
|
|
429
|
-
decAnyMetadata,
|
|
430
|
-
decodeScale,
|
|
431
|
-
encodeMetadata,
|
|
432
|
-
encodeStateKey,
|
|
433
|
-
fromHex,
|
|
434
|
-
getConstantValueFromMetadata,
|
|
435
|
-
getDynamicBuilder,
|
|
436
|
-
getLookupFn,
|
|
437
|
-
getMetadataVersion,
|
|
438
|
-
getStorageKeyPrefix,
|
|
439
|
-
magicNumber,
|
|
440
|
-
mergeUint8,
|
|
441
|
-
metadata,
|
|
442
|
-
papiParse,
|
|
443
|
-
papiStringify,
|
|
444
|
-
parseMetadataRpc,
|
|
445
|
-
toHex,
|
|
446
|
-
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);
|
|
447
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
|
+
|
|
448
347
|
//# sourceMappingURL=index.mjs.map
|