@talismn/scale 0.1.1 → 0.2.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.
@@ -1,8 +1,9 @@
1
+ import { getLookupFn, getDynamicBuilder } from '@polkadot-api/metadata-builders';
1
2
  export { getDynamicBuilder, getLookupFn } from '@polkadot-api/metadata-builders';
3
+ import { metadata, Binary, decAnyMetadata, unifyMetadata, Twox128 } from '@polkadot-api/substrate-bindings';
4
+ export { Blake2128, Blake2128Concat, Blake2256, Blake3256, Blake3256Concat, Twox128, Twox256, Twox64Concat, decAnyMetadata, metadata, unifyMetadata } from '@polkadot-api/substrate-bindings';
2
5
  import { toHex } from '@polkadot-api/utils';
3
6
  export { fromHex, toHex } from '@polkadot-api/utils';
4
- import { metadata, Binary } from '@polkadot-api/substrate-bindings';
5
- export { metadata, v14, v15 } from '@polkadot-api/substrate-bindings';
6
7
  import anylogger from 'anylogger';
7
8
  import { Struct, u8, u32 } from 'scale-ts';
8
9
 
@@ -23,7 +24,10 @@ var log = anylogger(packageJson.name);
23
24
  * E.g. if `palletsAndItems` is `{ pallet: "System", items: ["Account"] }`, then only the
24
25
  * types used in the `System.Account` storage query will remain inside of metadata.lookups.
25
26
  */
26
- const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods = [], extraKeepTypes = []) => {
27
+ const compactMetadata = (anyMetadata, palletsAndItems = [], runtimeApisAndMethods = [], extraKeepTypes = []) => {
28
+ if (!isCompactableMetadata(anyMetadata)) throw new Error(`Metadata version ${anyMetadata.metadata.tag} not supported in compactMetadata`);
29
+ const metadata = anyMetadata.metadata.value;
30
+
27
31
  // remove pallets we don't care about
28
32
  metadata.pallets = metadata.pallets.filter(pallet =>
29
33
  // keep this pallet if it's listed in `palletsAndItems`
@@ -38,10 +42,7 @@ const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods =
38
42
  items: itemNames
39
43
  }) => {
40
44
  const pallet = metadata.pallets.find(pallet => pallet.name === palletName);
41
- if (!pallet) {
42
- log.debug("Failed to find pallet", palletName);
43
- return [];
44
- }
45
+ if (!pallet) return [];
45
46
 
46
47
  // remove pallet fields we don't care about
47
48
  pallet.calls = undefined;
@@ -77,10 +78,7 @@ const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods =
77
78
  methods: methodNames
78
79
  }) => {
79
80
  const runtimeApi = metadata.apis.find(runtimeApi => runtimeApi.name === runtimeApiName);
80
- if (!runtimeApi) {
81
- log.debug("Failed to find runtimeApi", runtimeApiName);
82
- return [];
83
- }
81
+ if (!runtimeApi) return [];
84
82
 
85
83
  // remove runtime fields we don't care about
86
84
  runtimeApi.docs = [];
@@ -120,14 +118,11 @@ const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods =
120
118
  return newTypeId ?? 0;
121
119
  };
122
120
  remapTypeIds(metadata, getNewTypeId);
123
- if ("address" in metadata.extrinsic) {
124
- // metadata is v15 (NOT v14)
125
- metadata.extrinsic.address = 0;
126
- metadata.extrinsic.call = 0;
127
- metadata.extrinsic.extra = 0;
128
- metadata.extrinsic.signature = 0;
129
- }
130
- metadata.extrinsic.signedExtensions = [];
121
+ if ("address" in metadata.extrinsic) metadata.extrinsic.address = 0;
122
+ if ("call" in metadata.extrinsic) metadata.extrinsic.call = 0;
123
+ if ("signature" in metadata.extrinsic) metadata.extrinsic.signature = 0;
124
+ if ("extra" in metadata.extrinsic) metadata.extrinsic.extra = 0;
125
+ if ("signedExtensions" in metadata.extrinsic) metadata.extrinsic.signedExtensions = [];
131
126
  if ("outerEnums" in metadata) {
132
127
  // metadata is v15 (NOT v14)
133
128
  metadata.outerEnums.call = 0;
@@ -135,6 +130,16 @@ const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods =
135
130
  metadata.outerEnums.event = 0;
136
131
  }
137
132
  };
133
+ const isCompactableMetadata = metadata => {
134
+ switch (metadata.metadata.tag) {
135
+ case "v14":
136
+ case "v15":
137
+ case "v16":
138
+ return true;
139
+ default:
140
+ return false;
141
+ }
142
+ };
138
143
  const addDependentTypes = (metadataTysMap, keepTypes, types,
139
144
  // Prevent stack overflow when a type references itself
140
145
  addedTypes = new Set()) => {
@@ -265,43 +270,6 @@ const remapRuntimeApisTypeIds = (metadata, getNewTypeId) => {
265
270
  }
266
271
  };
267
272
 
268
- /**
269
- * Extracts the `version` u8 from a SCALE-encoded metadata blob and returns it as a `number`.
270
- *
271
- * Only reads the first 40 bytes of the blob.
272
- */
273
- const getMetadataVersion = metadataRpc => {
274
- try {
275
- return Struct({
276
- magicNumber: u32,
277
- version: u8
278
- }).dec(metadataRpc).version;
279
- } catch {
280
- return 0;
281
- }
282
- };
283
-
284
- const decodeMetadata = metadataRpc => {
285
- const metadataVersion = getMetadataVersion(metadataRpc);
286
- if (metadataVersion !== 15 && metadataVersion !== 14) return {
287
- metadataVersion
288
- };
289
- const decoded = metadata.dec(metadataRpc);
290
- if (decoded.metadata.tag === "v15") return {
291
- metadataVersion,
292
- metadata: decoded.metadata.value,
293
- tag: decoded.metadata.tag
294
- };
295
- if (decoded.metadata.tag === "v14") return {
296
- metadataVersion,
297
- metadata: decoded.metadata.value,
298
- tag: decoded.metadata.tag
299
- };
300
- return {
301
- metadataVersion
302
- };
303
- };
304
-
305
273
  const decodeScale = (scaleCoder, change, error) => {
306
274
  if (change === null) return null;
307
275
  try {
@@ -312,19 +280,7 @@ const decodeScale = (scaleCoder, change, error) => {
312
280
  }
313
281
  };
314
282
 
315
- const encodeMetadata = ({
316
- metadata: metadata$1,
317
- tag
318
- }) => toHex(metadata.enc({
319
- magicNumber,
320
- metadata: tag === "v15" ? {
321
- tag,
322
- value: metadata$1
323
- } : {
324
- tag,
325
- value: metadata$1
326
- }
327
- }));
283
+ const encodeMetadata = metadata$1 => toHex(metadata.enc(metadata$1));
328
284
 
329
285
  const encodeStateKey = (scaleCoder, error, ...args) => {
330
286
  try {
@@ -335,6 +291,22 @@ const encodeStateKey = (scaleCoder, error, ...args) => {
335
291
  }
336
292
  };
337
293
 
294
+ /**
295
+ * Extracts the `version` u8 from a SCALE-encoded metadata blob and returns it as a `number`.
296
+ *
297
+ * Only reads the first 40 bytes of the blob.
298
+ */
299
+ const getMetadataVersion = metadataRpc => {
300
+ try {
301
+ return Struct({
302
+ magicNumber: u32,
303
+ version: u8
304
+ }).dec(metadataRpc).version;
305
+ } catch {
306
+ return 0;
307
+ }
308
+ };
309
+
338
310
  /**
339
311
  * For the substrate-tokens (and other) modules, we configure the `onChainId` field in chaindata to tell the module how to query each token.
340
312
  * These queries are made to the tokens pallet.
@@ -384,4 +356,28 @@ const papiStringify = (value, space) => {
384
356
  return JSON.stringify(value, replacer, space);
385
357
  };
386
358
 
387
- export { compactMetadata, decodeMetadata, decodeScale, encodeMetadata, encodeStateKey, getMetadataVersion, magicNumber, papiParse, papiStringify };
359
+ const parseMetadataRpc = metadataRpc => {
360
+ const metadata = decAnyMetadata(metadataRpc);
361
+ const unifiedMetadata = unifyMetadata(metadata);
362
+ const lookupFn = getLookupFn(unifiedMetadata);
363
+ const builder = getDynamicBuilder(lookupFn);
364
+ return {
365
+ metadata,
366
+ unifiedMetadata,
367
+ lookupFn,
368
+ builder
369
+ };
370
+ };
371
+
372
+ const getStorageKeyPrefix = (palletName, storageName) => {
373
+ const palletHash = Twox128(new TextEncoder().encode(palletName));
374
+ const storageHash = Twox128(new TextEncoder().encode(storageName));
375
+
376
+ // Concatenate and convert to hex
377
+ const combined = new Uint8Array(palletHash.length + storageHash.length);
378
+ combined.set(palletHash, 0);
379
+ combined.set(storageHash, palletHash.length);
380
+ return toHex(combined);
381
+ };
382
+
383
+ export { compactMetadata, decodeScale, encodeMetadata, encodeStateKey, getMetadataVersion, getStorageKeyPrefix, magicNumber, papiParse, papiStringify, parseMetadataRpc };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/scale",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "author": "Talisman",
5
5
  "homepage": "https://talisman.xyz",
6
6
  "license": "GPL-3.0-or-later",
@@ -21,9 +21,9 @@
21
21
  "node": ">=18"
22
22
  },
23
23
  "dependencies": {
24
- "@polkadot-api/metadata-builders": "0.10.2",
25
- "@polkadot-api/substrate-bindings": "0.11.1",
26
- "@polkadot-api/utils": "0.1.2",
24
+ "@polkadot-api/metadata-builders": "0.12.2",
25
+ "@polkadot-api/substrate-bindings": "0.14.0",
26
+ "@polkadot-api/utils": "0.2.0",
27
27
  "anylogger": "^1.0.11",
28
28
  "scale-ts": "^1.6.1"
29
29
  },
@@ -33,8 +33,8 @@
33
33
  "jest": "^29.7",
34
34
  "ts-jest": "^29.2.5",
35
35
  "typescript": "^5.6.3",
36
- "@talismn/eslint-config": "0.0.3",
37
- "@talismn/tsconfig": "0.0.2"
36
+ "@talismn/tsconfig": "0.0.2",
37
+ "@talismn/eslint-config": "0.0.3"
38
38
  },
39
39
  "eslintConfig": {
40
40
  "root": true,
@@ -1,13 +0,0 @@
1
- import { V14, V15 } from "@polkadot-api/substrate-bindings";
2
- export declare const decodeMetadata: (metadataRpc: string) => {
3
- metadataVersion: number;
4
- } & ({
5
- metadata: V15;
6
- tag: "v15";
7
- } | {
8
- metadata: V14;
9
- tag: "v14";
10
- } | {
11
- metadata?: undefined;
12
- tag?: undefined;
13
- });