@talismn/scale 0.0.0-pr2277-20251211071316 → 0.0.0-pr2295-20260110044132

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.
@@ -1,495 +0,0 @@
1
- 'use strict';
2
-
3
- var metadataBuilders = require('@polkadot-api/metadata-builders');
4
- var substrateBindings = require('@polkadot-api/substrate-bindings');
5
- var utils = require('@polkadot-api/utils');
6
- var anylogger = require('anylogger');
7
- var scaleTs = require('scale-ts');
8
-
9
- function _interopDefault (e) { return e && e.__esModule ? e : { default: e }; }
10
-
11
- var anylogger__default = /*#__PURE__*/_interopDefault(anylogger);
12
-
13
- /** Constant: https://docs.substrate.io/build/application-development/#metadata-format */
14
- const magicNumber = 1635018093;
15
-
16
- var packageJson = {
17
- name: "@talismn/scale"};
18
-
19
- var log = anylogger__default.default(packageJson.name);
20
-
21
- /**
22
- * Converts a `Metadata` into a `MiniMetadata`.
23
- *
24
- * A `MiniMetadata` only contains the types inside of its `lookup` which are relevant for
25
- * the storage queries specified in `palletsAndItems`.
26
- *
27
- * E.g. if `palletsAndItems` is `{ pallet: "System", items: ["Account"] }`, then only the
28
- * types used in the `System.Account` storage query will remain inside of metadata.lookups.
29
- */
30
- const compactMetadata = (anyMetadata, palletsAndItems = [], runtimeApisAndMethods = [], extraKeepTypes = []) => {
31
- if (!isCompactableMetadata(anyMetadata)) throw new Error(`Metadata version ${anyMetadata.metadata.tag} not supported in compactMetadata`);
32
- const metadata = anyMetadata.metadata.value;
33
-
34
- // remove pallets we don't care about
35
- metadata.pallets = metadata.pallets.filter(pallet =>
36
- // keep this pallet if it's listed in `palletsAndItems`
37
- palletsAndItems.some(({
38
- pallet: palletName
39
- }) => pallet.name === palletName));
40
-
41
- // remove fields we don't care about from each pallet, and extract types for each storage item we care about
42
- const palletsKeepTypes = palletsAndItems.flatMap(({
43
- pallet: palletName,
44
- constants: constantNames,
45
- items: itemNames
46
- }) => {
47
- const pallet = metadata.pallets.find(pallet => pallet.name === palletName);
48
- if (!pallet) return [];
49
-
50
- // remove pallet fields we don't care about
51
- pallet.calls = undefined;
52
- pallet.constants = constantNames ? pallet.constants.filter(constant => constantNames.includes(constant.name)) : [];
53
- // v15 (NOT v14) has docs
54
- if ("docs" in pallet) pallet.docs = [];
55
- pallet.errors = undefined;
56
- pallet.events = undefined;
57
- if (!pallet.storage) return [];
58
-
59
- // filter and extract storage items we care about
60
- pallet.storage.items = pallet.storage.items.filter(item => itemNames.some(itemName => item.name === itemName));
61
- return [...pallet.storage.items.flatMap(item => [
62
- // each type can be either "Plain" or "Map"
63
- // if it's "Plain" we only need to get the value type
64
- // if it's a "Map" we want to keep both the key AND the value types
65
- item.type.tag === "plain" && item.type.value, item.type.tag === "map" && item.type.value.key, item.type.tag === "map" && item.type.value.value]).filter(type => typeof type === "number"), ...pallet.constants.flatMap(constant => constant.type)];
66
- });
67
-
68
- // remove runtime apis we don't care about
69
- let runtimeApisKeepTypes = [];
70
- if ("apis" in metadata) {
71
- // metadata is v15 (NOT v14)
72
-
73
- // keep this api if it's listed in `runtimeApisAndMethods`
74
- metadata.apis = metadata.apis.filter(runtimeApi => runtimeApisAndMethods.some(({
75
- runtimeApi: runtimeApiName
76
- }) => runtimeApi.name === runtimeApiName));
77
-
78
- // remove methods we don't care about from each runtime api, and extract types for each call's params and result
79
- runtimeApisKeepTypes = runtimeApisAndMethods.flatMap(({
80
- runtimeApi: runtimeApiName,
81
- methods: methodNames
82
- }) => {
83
- const runtimeApi = metadata.apis.find(runtimeApi => runtimeApi.name === runtimeApiName);
84
- if (!runtimeApi) return [];
85
-
86
- // remove runtime fields we don't care about
87
- runtimeApi.docs = [];
88
- if (!runtimeApi.methods) return [];
89
-
90
- // filter and extract methods we care about
91
- runtimeApi.methods = runtimeApi.methods.filter(method => methodNames.some(methodName => method.name === methodName));
92
- return runtimeApi.methods.flatMap(method => [
93
- // each method has an array of input types (for the params)
94
- ...method.inputs.map(input => input.type),
95
- // and one output type (for the result)
96
- method.output]);
97
- });
98
- }
99
-
100
- // this is a set of type ids which we plan to keep in our compacted metadata
101
- // anything not in this set will be deleted
102
- // we start off with just the types of the state calls we plan to make,
103
- // then we run those types through a function (addDependentTypes) which will also include
104
- // all of the types which those types depend on - recursively
105
- const keepTypes = new Set([...palletsKeepTypes, ...runtimeApisKeepTypes]);
106
- extraKeepTypes?.forEach(type => keepTypes.add(type));
107
-
108
- // recursively find all the types which our keepTypes depend on and add them to the keepTypes set
109
- const metadataTysMap = new Map(metadata.lookup.map(ty => [ty.id, ty]));
110
- addDependentTypes(metadataTysMap, keepTypes, [...keepTypes]);
111
-
112
- // ditch the types we aren't keeping
113
- metadata.lookup = metadata.lookup.filter(type => keepTypes.has(type.id));
114
-
115
- // update all type ids to be sequential (fill the gaps left by the deleted types)
116
- const newTypeIds = new Map();
117
- metadata.lookup.forEach((type, index) => newTypeIds.set(type.id, index));
118
- const getNewTypeId = oldTypeId => {
119
- const newTypeId = newTypeIds.get(oldTypeId);
120
- if (typeof newTypeId !== "number") log.error(`Failed to find newTypeId for type ${oldTypeId}`);
121
- return newTypeId ?? 0;
122
- };
123
- remapTypeIds(metadata, getNewTypeId);
124
- if ("address" in metadata.extrinsic) metadata.extrinsic.address = 0;
125
- if ("call" in metadata.extrinsic) metadata.extrinsic.call = 0;
126
- if ("signature" in metadata.extrinsic) metadata.extrinsic.signature = 0;
127
- if ("extra" in metadata.extrinsic) metadata.extrinsic.extra = 0;
128
- if ("signedExtensions" in metadata.extrinsic) metadata.extrinsic.signedExtensions = [];
129
- if ("outerEnums" in metadata) {
130
- // metadata is v15 (NOT v14)
131
- metadata.outerEnums.call = 0;
132
- metadata.outerEnums.error = 0;
133
- metadata.outerEnums.event = 0;
134
- }
135
- };
136
- const isCompactableMetadata = metadata => {
137
- switch (metadata.metadata.tag) {
138
- case "v14":
139
- case "v15":
140
- case "v16":
141
- return true;
142
- default:
143
- return false;
144
- }
145
- };
146
- const addDependentTypes = (metadataTysMap, keepTypes, types,
147
- // Prevent stack overflow when a type references itself
148
- addedTypes = new Set()) => {
149
- const addDependentSubTypes = subTypes => addDependentTypes(metadataTysMap, keepTypes, subTypes, addedTypes);
150
- for (const typeId of types) {
151
- const type = metadataTysMap.get(typeId);
152
- if (!type) {
153
- log.warn(`Unable to find type with id ${typeId}`);
154
- continue;
155
- }
156
- if (addedTypes.has(type.id)) continue;
157
- keepTypes.add(type.id);
158
- addedTypes.add(type.id);
159
- const paramTypes = type.params.map(param => param.type).filter(type => typeof type === "number");
160
- addDependentSubTypes(paramTypes);
161
- switch (type.def.tag) {
162
- case "array":
163
- addDependentSubTypes([type.def.value.type]);
164
- break;
165
- case "bitSequence":
166
- addDependentSubTypes([type.def.value.bitOrderType, type.def.value.bitStoreType]);
167
- break;
168
- case "compact":
169
- addDependentSubTypes([type.def.value]);
170
- break;
171
- case "composite":
172
- addDependentSubTypes(type.def.value.map(field => field.type).filter(type => typeof type === "number"));
173
- break;
174
- case "primitive":
175
- break;
176
- case "sequence":
177
- addDependentSubTypes([type.def.value]);
178
- break;
179
- case "tuple":
180
- addDependentSubTypes(type.def.value.filter(type => typeof type === "number"));
181
- break;
182
- case "variant":
183
- addDependentSubTypes(type.def.value.flatMap(member => member.fields.map(field => field.type)).filter(type => typeof type === "number"));
184
- break;
185
- default:
186
- {
187
- // force compilation error if any types don't have a case
188
- const exhaustiveCheck = type.def;
189
- log.error(`Unhandled V15Type type ${exhaustiveCheck}`);
190
- }
191
- }
192
- }
193
- };
194
- const remapTypeIds = (metadata, getNewTypeId) => {
195
- remapLookupTypeIds(metadata, getNewTypeId);
196
- remapStorageTypeIds(metadata, getNewTypeId);
197
- remapRuntimeApisTypeIds(metadata, getNewTypeId);
198
- };
199
- const remapLookupTypeIds = (metadata, getNewTypeId) => {
200
- for (const type of metadata.lookup) {
201
- type.id = getNewTypeId(type.id);
202
- for (const param of type.params) {
203
- if (typeof param.type !== "number") continue;
204
- param.type = getNewTypeId(param.type);
205
- }
206
- switch (type.def.tag) {
207
- case "array":
208
- type.def.value.type = getNewTypeId(type.def.value.type);
209
- break;
210
- case "bitSequence":
211
- type.def.value.bitOrderType = getNewTypeId(type.def.value.bitOrderType);
212
- type.def.value.bitStoreType = getNewTypeId(type.def.value.bitStoreType);
213
- break;
214
- case "compact":
215
- type.def.value = getNewTypeId(type.def.value);
216
- break;
217
- case "composite":
218
- for (const field of type.def.value) {
219
- if (typeof field.type !== "number") continue;
220
- field.type = getNewTypeId(field.type);
221
- }
222
- break;
223
- case "primitive":
224
- break;
225
- case "sequence":
226
- type.def.value = getNewTypeId(type.def.value);
227
- break;
228
- case "tuple":
229
- type.def.value = type.def.value.map(type => {
230
- if (typeof type !== "number") return type;
231
- return getNewTypeId(type);
232
- });
233
- break;
234
- case "variant":
235
- for (const member of type.def.value) {
236
- for (const field of member.fields) {
237
- if (typeof field.type !== "number") continue;
238
- field.type = getNewTypeId(field.type);
239
- }
240
- }
241
- break;
242
- default:
243
- {
244
- // force compilation error if any types don't have a case
245
- const exhaustiveCheck = type.def;
246
- log.error(`Unhandled V15Type type ${exhaustiveCheck}`);
247
- }
248
- }
249
- }
250
- };
251
- const remapStorageTypeIds = (metadata, getNewTypeId) => {
252
- for (const pallet of metadata.pallets) {
253
- for (const item of pallet.storage?.items ?? []) {
254
- if (item.type.tag === "plain") item.type.value = getNewTypeId(item.type.value);
255
- if (item.type.tag === "map") {
256
- item.type.value.key = getNewTypeId(item.type.value.key);
257
- item.type.value.value = getNewTypeId(item.type.value.value);
258
- }
259
- }
260
- for (const constant of pallet.constants ?? []) {
261
- constant.type = getNewTypeId(constant.type);
262
- }
263
- }
264
- };
265
- const remapRuntimeApisTypeIds = (metadata, getNewTypeId) => {
266
- for (const runtimeApi of metadata.apis) {
267
- for (const method of runtimeApi.methods ?? []) {
268
- for (const input of method.inputs) {
269
- input.type = getNewTypeId(input.type);
270
- }
271
- method.output = getNewTypeId(method.output);
272
- }
273
- }
274
- };
275
-
276
- const decodeScale = (scaleCoder, change, error) => {
277
- if (change === null) return null;
278
- try {
279
- return scaleCoder?.value?.dec(change) ?? null;
280
- } catch (cause) {
281
- log.warn(error ?? `Failed to decode ${change}`, cause);
282
- return null;
283
- }
284
- };
285
-
286
- const encodeMetadata = metadata => utils.toHex(substrateBindings.metadata.enc(metadata));
287
-
288
- const encodeStateKey = (scaleCoder, error, ...args) => {
289
- try {
290
- return scaleCoder?.keys?.enc(...args);
291
- } catch (cause) {
292
- log.warn(error ?? `Failed to encode stateKey ${JSON.stringify(args)}`, cause);
293
- return;
294
- }
295
- };
296
-
297
- /**
298
- * Extracts the `version` u8 from a SCALE-encoded metadata blob and returns it as a `number`.
299
- *
300
- * Only reads the first 40 bytes of the blob.
301
- */
302
- const getMetadataVersion = metadataRpc => {
303
- try {
304
- return scaleTs.Struct({
305
- magicNumber: scaleTs.u32,
306
- version: scaleTs.u8
307
- }).dec(metadataRpc).version;
308
- } catch {
309
- return 0;
310
- }
311
- };
312
-
313
- /**
314
- * For the substrate-tokens (and other) modules, we configure the `onChainId` field in chaindata to tell the module how to query each token.
315
- * These queries are made to the tokens pallet.
316
- * E.g. api.query.Tokens.Account(accountAddress, papiParse(onChainId))
317
- *
318
- * The `onChainId` field on chaindata must be a JSON-parseable string, but for some SCALE types (especially the Binary type) we must
319
- * use specific `polkadot-api` classes to handle SCALE-encoding the statekey.
320
- *
321
- * Some examples:
322
- * Input: `5`
323
- * Output: `5`
324
- *
325
- * Input: `{ type: "DexShare", value: [ { type: "Token", value: { type: "ACA" } }, { type: "Token", value: { type: "AUSD" } } ] }`
326
- * Output: `Enum("DexShare", [Enum("Token", Enum("ACA")), Enum("Token", Enum("AUSD"))])`
327
- *
328
- * Input: `{ type: "LiquidCrowdloan", value: 13 }`
329
- * Output: `Enum("LiquidCrowdloan", 13)`
330
- *
331
- * Input: `{ type: "NativeToken", value: "bigint:2" }`
332
- * Output: `Enum("NativeToken", 2n)`
333
- *
334
- * Input: `{ type: "Erc20", value: "hex:0x07df96d1341a7d16ba1ad431e2c847d978bc2bce" }`
335
- * Output: `Enum("Erc20", Binary.fromHex("0x07df96d1341a7d16ba1ad431e2c847d978bc2bce"))`
336
- *
337
- * Input: `{ type: "Stellar", value: { code: "bin:TZS", issuer: "hex:0x34c94b2a4ba9e8b57b22547dcbb30f443c4cb02da3829a89aa1bd4780e4466ba" } }`
338
- * Output: `Enum("Stellar", { code: Binary.fromText("TZS"), issuer: Binary.fromHex("0x34c94b2a4ba9e8b57b22547dcbb30f443c4cb02da3829a89aa1bd4780e4466ba") })`
339
- */
340
- const papiParse = text => {
341
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
342
- const reviver = (_key, value) => {
343
- if (typeof value !== "string") return value;
344
- if (value.startsWith("bigint:")) return BigInt(value.slice("bigint:".length));
345
- if (value.startsWith("hex:")) return substrateBindings.Binary.fromHex(value.slice("hex:".length));
346
- if (value.startsWith("bin:")) return substrateBindings.Binary.fromText(value.slice("bin:".length));
347
- return value;
348
- };
349
- if (typeof text !== "string") return text;
350
- return JSON.parse(text, reviver);
351
- };
352
- const papiStringify = (value, space) => {
353
- // eslint-disable-next-line @typescript-eslint/no-explicit-any
354
- const replacer = (_key, value) => {
355
- if (typeof value === "bigint") return `bigint:${String(value)}`;
356
- if (value instanceof substrateBindings.Binary) return `hex:${value.asHex()}`;
357
- return value;
358
- };
359
- return JSON.stringify(value, replacer, space);
360
- };
361
-
362
- const parseMetadataRpc = metadataRpc => {
363
- const metadata = substrateBindings.decAnyMetadata(metadataRpc);
364
- const unifiedMetadata = substrateBindings.unifyMetadata(metadata);
365
- const lookupFn = metadataBuilders.getLookupFn(unifiedMetadata);
366
- const builder = metadataBuilders.getDynamicBuilder(lookupFn);
367
- return {
368
- metadata,
369
- unifiedMetadata,
370
- lookupFn,
371
- builder
372
- };
373
- };
374
-
375
- const getStorageKeyPrefix = (palletName, storageName) => {
376
- const palletHash = substrateBindings.Twox128(new TextEncoder().encode(palletName));
377
- const storageHash = substrateBindings.Twox128(new TextEncoder().encode(storageName));
378
-
379
- // Concatenate and convert to hex
380
- const combined = new Uint8Array(palletHash.length + storageHash.length);
381
- combined.set(palletHash, 0);
382
- combined.set(storageHash, palletHash.length);
383
- return utils.toHex(combined);
384
- };
385
-
386
- const getConstantValueFromMetadata = (metadata, pallet, constant) => {
387
- const {
388
- builder,
389
- unifiedMetadata
390
- } = typeof metadata === "string" ? parseMetadataRpc(metadata) : metadata;
391
- return getConstantValueInner(builder, unifiedMetadata, pallet, constant);
392
- };
393
- const getConstantValueInner = (builder, unifiedMetadata, pallet, constant) => {
394
- try {
395
- const storageCodec = builder.buildConstant(pallet, constant);
396
- const encodedValue = unifiedMetadata.pallets.find(({
397
- name
398
- }) => name === pallet)?.constants.find(({
399
- name
400
- }) => name === constant)?.value;
401
- if (!encodedValue) throw new Error(`Constant ${pallet}.${constant} not found`);
402
- return storageCodec.dec(encodedValue);
403
- } catch (err) {
404
- log.error("Failed to get constant value from metadata", {
405
- err,
406
- pallet,
407
- constant
408
- });
409
- throw err;
410
- }
411
- };
412
-
413
- Object.defineProperty(exports, "getDynamicBuilder", {
414
- enumerable: true,
415
- get: function () { return metadataBuilders.getDynamicBuilder; }
416
- });
417
- Object.defineProperty(exports, "getLookupFn", {
418
- enumerable: true,
419
- get: function () { return metadataBuilders.getLookupFn; }
420
- });
421
- Object.defineProperty(exports, "Binary", {
422
- enumerable: true,
423
- get: function () { return substrateBindings.Binary; }
424
- });
425
- Object.defineProperty(exports, "Blake2128", {
426
- enumerable: true,
427
- get: function () { return substrateBindings.Blake2128; }
428
- });
429
- Object.defineProperty(exports, "Blake2128Concat", {
430
- enumerable: true,
431
- get: function () { return substrateBindings.Blake2128Concat; }
432
- });
433
- Object.defineProperty(exports, "Blake2256", {
434
- enumerable: true,
435
- get: function () { return substrateBindings.Blake2256; }
436
- });
437
- Object.defineProperty(exports, "Blake3256", {
438
- enumerable: true,
439
- get: function () { return substrateBindings.Blake3256; }
440
- });
441
- Object.defineProperty(exports, "Blake3256Concat", {
442
- enumerable: true,
443
- get: function () { return substrateBindings.Blake3256Concat; }
444
- });
445
- Object.defineProperty(exports, "FixedSizeBinary", {
446
- enumerable: true,
447
- get: function () { return substrateBindings.FixedSizeBinary; }
448
- });
449
- Object.defineProperty(exports, "Twox128", {
450
- enumerable: true,
451
- get: function () { return substrateBindings.Twox128; }
452
- });
453
- Object.defineProperty(exports, "Twox256", {
454
- enumerable: true,
455
- get: function () { return substrateBindings.Twox256; }
456
- });
457
- Object.defineProperty(exports, "Twox64Concat", {
458
- enumerable: true,
459
- get: function () { return substrateBindings.Twox64Concat; }
460
- });
461
- Object.defineProperty(exports, "decAnyMetadata", {
462
- enumerable: true,
463
- get: function () { return substrateBindings.decAnyMetadata; }
464
- });
465
- Object.defineProperty(exports, "metadata", {
466
- enumerable: true,
467
- get: function () { return substrateBindings.metadata; }
468
- });
469
- Object.defineProperty(exports, "unifyMetadata", {
470
- enumerable: true,
471
- get: function () { return substrateBindings.unifyMetadata; }
472
- });
473
- Object.defineProperty(exports, "fromHex", {
474
- enumerable: true,
475
- get: function () { return utils.fromHex; }
476
- });
477
- Object.defineProperty(exports, "mergeUint8", {
478
- enumerable: true,
479
- get: function () { return utils.mergeUint8; }
480
- });
481
- Object.defineProperty(exports, "toHex", {
482
- enumerable: true,
483
- get: function () { return utils.toHex; }
484
- });
485
- exports.compactMetadata = compactMetadata;
486
- exports.decodeScale = decodeScale;
487
- exports.encodeMetadata = encodeMetadata;
488
- exports.encodeStateKey = encodeStateKey;
489
- exports.getConstantValueFromMetadata = getConstantValueFromMetadata;
490
- exports.getMetadataVersion = getMetadataVersion;
491
- exports.getStorageKeyPrefix = getStorageKeyPrefix;
492
- exports.magicNumber = magicNumber;
493
- exports.papiParse = papiParse;
494
- exports.papiStringify = papiStringify;
495
- exports.parseMetadataRpc = parseMetadataRpc;