@talismn/scale 0.1.1 → 0.1.2

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,6 +1,6 @@
1
1
  export { getDynamicBuilder, getLookupFn } from "@polkadot-api/metadata-builders";
2
- export type { V14, V15, V15Extrinsic, Codec, Binary } from "@polkadot-api/substrate-bindings";
2
+ export type { Codec, UnifiedMetadata } from "@polkadot-api/substrate-bindings";
3
+ export { decAnyMetadata, unifyMetadata, metadata } from "@polkadot-api/substrate-bindings";
3
4
  export { toHex, fromHex } from "@polkadot-api/utils";
4
5
  /** Constant: https://docs.substrate.io/build/application-development/#metadata-format */
5
6
  export declare const magicNumber = 1635018093;
6
- export { v14, v15, metadata } from "@polkadot-api/substrate-bindings";
@@ -0,0 +1,3 @@
1
+ export type Prettify<T> = {
2
+ [K in keyof T]: T[K];
3
+ } & {};
@@ -1,10 +1,11 @@
1
- import { V14, V15 } from "../papito";
2
- export type V14Type = V14["lookup"][0];
3
- export type V14Pallet = V14["pallets"][0];
4
- export type V14StorageItem = NonNullable<V14Pallet["storage"]>["items"][0];
5
- export type V15Type = V15["lookup"][0];
6
- export type V15Pallet = V15["pallets"][0];
7
- export type V15StorageItem = NonNullable<V15Pallet["storage"]>["items"][0];
1
+ import { Metadata } from "@polkadot-api/substrate-bindings";
2
+ export type MetadataType = SupportedMetadata["lookup"][number];
3
+ export type MetadataPallet = SupportedMetadata["pallets"][number];
4
+ export type MetadataStorageItem = NonNullable<MetadataPallet["storage"]>["items"][number];
5
+ type SupportedMetadata = Extract<Metadata["metadata"], {
6
+ tag: SupportedMetadataVersion;
7
+ }>["value"];
8
+ type SupportedMetadataVersion = "v14" | "v15" | "v16";
8
9
  /**
9
10
  * Converts a `Metadata` into a `MiniMetadata`.
10
11
  *
@@ -14,7 +15,7 @@ export type V15StorageItem = NonNullable<V15Pallet["storage"]>["items"][0];
14
15
  * E.g. if `palletsAndItems` is `{ pallet: "System", items: ["Account"] }`, then only the
15
16
  * types used in the `System.Account` storage query will remain inside of metadata.lookups.
16
17
  */
17
- export declare const compactMetadata: (metadata: V15 | V14, palletsAndItems?: Array<{
18
+ export declare const compactMetadata: (anyMetadata: Metadata, palletsAndItems?: Array<{
18
19
  pallet: string;
19
20
  constants?: string[];
20
21
  items: string[];
@@ -22,3 +23,4 @@ export declare const compactMetadata: (metadata: V15 | V14, palletsAndItems?: Ar
22
23
  runtimeApi: string;
23
24
  methods: string[];
24
25
  }>, extraKeepTypes?: number[]) => void;
26
+ export {};
@@ -1,8 +1,2 @@
1
- import { V14, V15 } from "@polkadot-api/substrate-bindings";
2
- export declare const encodeMetadata: ({ metadata, tag, }: {
3
- metadata: V15;
4
- tag: "v15";
5
- } | {
6
- metadata: V14;
7
- tag: "v14";
8
- }) => string;
1
+ import { Metadata } from "@polkadot-api/substrate-bindings";
2
+ export declare const encodeMetadata: (metadata: Metadata) => string;
@@ -1,5 +1,4 @@
1
1
  export * from "./compactMetadata";
2
- export * from "./decodeMetadata";
3
2
  export * from "./decodeScale";
4
3
  export * from "./encodeMetadata";
5
4
  export * from "./encodeStateKey";
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var metadataBuilders = require('@polkadot-api/metadata-builders');
4
- var utils = require('@polkadot-api/utils');
5
4
  var substrateBindings = require('@polkadot-api/substrate-bindings');
5
+ var utils = require('@polkadot-api/utils');
6
6
  var anylogger = require('anylogger');
7
7
  var scaleTs = require('scale-ts');
8
8
 
@@ -27,7 +27,10 @@ var log = anylogger__default.default(packageJson.name);
27
27
  * E.g. if `palletsAndItems` is `{ pallet: "System", items: ["Account"] }`, then only the
28
28
  * types used in the `System.Account` storage query will remain inside of metadata.lookups.
29
29
  */
30
- const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods = [], extraKeepTypes = []) => {
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
+
31
34
  // remove pallets we don't care about
32
35
  metadata.pallets = metadata.pallets.filter(pallet =>
33
36
  // keep this pallet if it's listed in `palletsAndItems`
@@ -124,14 +127,11 @@ const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods =
124
127
  return newTypeId ?? 0;
125
128
  };
126
129
  remapTypeIds(metadata, getNewTypeId);
127
- if ("address" in metadata.extrinsic) {
128
- // metadata is v15 (NOT v14)
129
- metadata.extrinsic.address = 0;
130
- metadata.extrinsic.call = 0;
131
- metadata.extrinsic.extra = 0;
132
- metadata.extrinsic.signature = 0;
133
- }
134
- metadata.extrinsic.signedExtensions = [];
130
+ if ("address" in metadata.extrinsic) metadata.extrinsic.address = 0;
131
+ if ("call" in metadata.extrinsic) metadata.extrinsic.call = 0;
132
+ if ("signature" in metadata.extrinsic) metadata.extrinsic.signature = 0;
133
+ if ("extra" in metadata.extrinsic) metadata.extrinsic.extra = 0;
134
+ if ("signedExtensions" in metadata.extrinsic) metadata.extrinsic.signedExtensions = [];
135
135
  if ("outerEnums" in metadata) {
136
136
  // metadata is v15 (NOT v14)
137
137
  metadata.outerEnums.call = 0;
@@ -139,6 +139,16 @@ const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods =
139
139
  metadata.outerEnums.event = 0;
140
140
  }
141
141
  };
142
+ const isCompactableMetadata = metadata => {
143
+ switch (metadata.metadata.tag) {
144
+ case "v14":
145
+ case "v15":
146
+ case "v16":
147
+ return true;
148
+ default:
149
+ return false;
150
+ }
151
+ };
142
152
  const addDependentTypes = (metadataTysMap, keepTypes, types,
143
153
  // Prevent stack overflow when a type references itself
144
154
  addedTypes = new Set()) => {
@@ -269,43 +279,6 @@ const remapRuntimeApisTypeIds = (metadata, getNewTypeId) => {
269
279
  }
270
280
  };
271
281
 
272
- /**
273
- * Extracts the `version` u8 from a SCALE-encoded metadata blob and returns it as a `number`.
274
- *
275
- * Only reads the first 40 bytes of the blob.
276
- */
277
- const getMetadataVersion = metadataRpc => {
278
- try {
279
- return scaleTs.Struct({
280
- magicNumber: scaleTs.u32,
281
- version: scaleTs.u8
282
- }).dec(metadataRpc).version;
283
- } catch {
284
- return 0;
285
- }
286
- };
287
-
288
- const decodeMetadata = metadataRpc => {
289
- const metadataVersion = getMetadataVersion(metadataRpc);
290
- if (metadataVersion !== 15 && metadataVersion !== 14) return {
291
- metadataVersion
292
- };
293
- const decoded = substrateBindings.metadata.dec(metadataRpc);
294
- if (decoded.metadata.tag === "v15") return {
295
- metadataVersion,
296
- metadata: decoded.metadata.value,
297
- tag: decoded.metadata.tag
298
- };
299
- if (decoded.metadata.tag === "v14") return {
300
- metadataVersion,
301
- metadata: decoded.metadata.value,
302
- tag: decoded.metadata.tag
303
- };
304
- return {
305
- metadataVersion
306
- };
307
- };
308
-
309
282
  const decodeScale = (scaleCoder, change, error) => {
310
283
  if (change === null) return null;
311
284
  try {
@@ -316,19 +289,7 @@ const decodeScale = (scaleCoder, change, error) => {
316
289
  }
317
290
  };
318
291
 
319
- const encodeMetadata = ({
320
- metadata,
321
- tag
322
- }) => utils.toHex(substrateBindings.metadata.enc({
323
- magicNumber,
324
- metadata: tag === "v15" ? {
325
- tag,
326
- value: metadata
327
- } : {
328
- tag,
329
- value: metadata
330
- }
331
- }));
292
+ const encodeMetadata = metadata => utils.toHex(substrateBindings.metadata.enc(metadata));
332
293
 
333
294
  const encodeStateKey = (scaleCoder, error, ...args) => {
334
295
  try {
@@ -339,6 +300,22 @@ const encodeStateKey = (scaleCoder, error, ...args) => {
339
300
  }
340
301
  };
341
302
 
303
+ /**
304
+ * Extracts the `version` u8 from a SCALE-encoded metadata blob and returns it as a `number`.
305
+ *
306
+ * Only reads the first 40 bytes of the blob.
307
+ */
308
+ const getMetadataVersion = metadataRpc => {
309
+ try {
310
+ return scaleTs.Struct({
311
+ magicNumber: scaleTs.u32,
312
+ version: scaleTs.u8
313
+ }).dec(metadataRpc).version;
314
+ } catch {
315
+ return 0;
316
+ }
317
+ };
318
+
342
319
  /**
343
320
  * For the substrate-tokens (and other) modules, we configure the `onChainId` field in chaindata to tell the module how to query each token.
344
321
  * These queries are made to the tokens pallet.
@@ -396,28 +373,27 @@ Object.defineProperty(exports, "getLookupFn", {
396
373
  enumerable: true,
397
374
  get: function () { return metadataBuilders.getLookupFn; }
398
375
  });
399
- Object.defineProperty(exports, "fromHex", {
400
- enumerable: true,
401
- get: function () { return utils.fromHex; }
402
- });
403
- Object.defineProperty(exports, "toHex", {
376
+ Object.defineProperty(exports, "decAnyMetadata", {
404
377
  enumerable: true,
405
- get: function () { return utils.toHex; }
378
+ get: function () { return substrateBindings.decAnyMetadata; }
406
379
  });
407
380
  Object.defineProperty(exports, "metadata", {
408
381
  enumerable: true,
409
382
  get: function () { return substrateBindings.metadata; }
410
383
  });
411
- Object.defineProperty(exports, "v14", {
384
+ Object.defineProperty(exports, "unifyMetadata", {
412
385
  enumerable: true,
413
- get: function () { return substrateBindings.v14; }
386
+ get: function () { return substrateBindings.unifyMetadata; }
414
387
  });
415
- Object.defineProperty(exports, "v15", {
388
+ Object.defineProperty(exports, "fromHex", {
416
389
  enumerable: true,
417
- get: function () { return substrateBindings.v15; }
390
+ get: function () { return utils.fromHex; }
391
+ });
392
+ Object.defineProperty(exports, "toHex", {
393
+ enumerable: true,
394
+ get: function () { return utils.toHex; }
418
395
  });
419
396
  exports.compactMetadata = compactMetadata;
420
- exports.decodeMetadata = decodeMetadata;
421
397
  exports.decodeScale = decodeScale;
422
398
  exports.encodeMetadata = encodeMetadata;
423
399
  exports.encodeStateKey = encodeStateKey;
@@ -1,8 +1,8 @@
1
1
  'use strict';
2
2
 
3
3
  var metadataBuilders = require('@polkadot-api/metadata-builders');
4
- var utils = require('@polkadot-api/utils');
5
4
  var substrateBindings = require('@polkadot-api/substrate-bindings');
5
+ var utils = require('@polkadot-api/utils');
6
6
  var anylogger = require('anylogger');
7
7
  var scaleTs = require('scale-ts');
8
8
 
@@ -27,7 +27,10 @@ var log = anylogger__default.default(packageJson.name);
27
27
  * E.g. if `palletsAndItems` is `{ pallet: "System", items: ["Account"] }`, then only the
28
28
  * types used in the `System.Account` storage query will remain inside of metadata.lookups.
29
29
  */
30
- const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods = [], extraKeepTypes = []) => {
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
+
31
34
  // remove pallets we don't care about
32
35
  metadata.pallets = metadata.pallets.filter(pallet =>
33
36
  // keep this pallet if it's listed in `palletsAndItems`
@@ -124,14 +127,11 @@ const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods =
124
127
  return newTypeId ?? 0;
125
128
  };
126
129
  remapTypeIds(metadata, getNewTypeId);
127
- if ("address" in metadata.extrinsic) {
128
- // metadata is v15 (NOT v14)
129
- metadata.extrinsic.address = 0;
130
- metadata.extrinsic.call = 0;
131
- metadata.extrinsic.extra = 0;
132
- metadata.extrinsic.signature = 0;
133
- }
134
- metadata.extrinsic.signedExtensions = [];
130
+ if ("address" in metadata.extrinsic) metadata.extrinsic.address = 0;
131
+ if ("call" in metadata.extrinsic) metadata.extrinsic.call = 0;
132
+ if ("signature" in metadata.extrinsic) metadata.extrinsic.signature = 0;
133
+ if ("extra" in metadata.extrinsic) metadata.extrinsic.extra = 0;
134
+ if ("signedExtensions" in metadata.extrinsic) metadata.extrinsic.signedExtensions = [];
135
135
  if ("outerEnums" in metadata) {
136
136
  // metadata is v15 (NOT v14)
137
137
  metadata.outerEnums.call = 0;
@@ -139,6 +139,16 @@ const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods =
139
139
  metadata.outerEnums.event = 0;
140
140
  }
141
141
  };
142
+ const isCompactableMetadata = metadata => {
143
+ switch (metadata.metadata.tag) {
144
+ case "v14":
145
+ case "v15":
146
+ case "v16":
147
+ return true;
148
+ default:
149
+ return false;
150
+ }
151
+ };
142
152
  const addDependentTypes = (metadataTysMap, keepTypes, types,
143
153
  // Prevent stack overflow when a type references itself
144
154
  addedTypes = new Set()) => {
@@ -269,43 +279,6 @@ const remapRuntimeApisTypeIds = (metadata, getNewTypeId) => {
269
279
  }
270
280
  };
271
281
 
272
- /**
273
- * Extracts the `version` u8 from a SCALE-encoded metadata blob and returns it as a `number`.
274
- *
275
- * Only reads the first 40 bytes of the blob.
276
- */
277
- const getMetadataVersion = metadataRpc => {
278
- try {
279
- return scaleTs.Struct({
280
- magicNumber: scaleTs.u32,
281
- version: scaleTs.u8
282
- }).dec(metadataRpc).version;
283
- } catch {
284
- return 0;
285
- }
286
- };
287
-
288
- const decodeMetadata = metadataRpc => {
289
- const metadataVersion = getMetadataVersion(metadataRpc);
290
- if (metadataVersion !== 15 && metadataVersion !== 14) return {
291
- metadataVersion
292
- };
293
- const decoded = substrateBindings.metadata.dec(metadataRpc);
294
- if (decoded.metadata.tag === "v15") return {
295
- metadataVersion,
296
- metadata: decoded.metadata.value,
297
- tag: decoded.metadata.tag
298
- };
299
- if (decoded.metadata.tag === "v14") return {
300
- metadataVersion,
301
- metadata: decoded.metadata.value,
302
- tag: decoded.metadata.tag
303
- };
304
- return {
305
- metadataVersion
306
- };
307
- };
308
-
309
282
  const decodeScale = (scaleCoder, change, error) => {
310
283
  if (change === null) return null;
311
284
  try {
@@ -316,19 +289,7 @@ const decodeScale = (scaleCoder, change, error) => {
316
289
  }
317
290
  };
318
291
 
319
- const encodeMetadata = ({
320
- metadata,
321
- tag
322
- }) => utils.toHex(substrateBindings.metadata.enc({
323
- magicNumber,
324
- metadata: tag === "v15" ? {
325
- tag,
326
- value: metadata
327
- } : {
328
- tag,
329
- value: metadata
330
- }
331
- }));
292
+ const encodeMetadata = metadata => utils.toHex(substrateBindings.metadata.enc(metadata));
332
293
 
333
294
  const encodeStateKey = (scaleCoder, error, ...args) => {
334
295
  try {
@@ -339,6 +300,22 @@ const encodeStateKey = (scaleCoder, error, ...args) => {
339
300
  }
340
301
  };
341
302
 
303
+ /**
304
+ * Extracts the `version` u8 from a SCALE-encoded metadata blob and returns it as a `number`.
305
+ *
306
+ * Only reads the first 40 bytes of the blob.
307
+ */
308
+ const getMetadataVersion = metadataRpc => {
309
+ try {
310
+ return scaleTs.Struct({
311
+ magicNumber: scaleTs.u32,
312
+ version: scaleTs.u8
313
+ }).dec(metadataRpc).version;
314
+ } catch {
315
+ return 0;
316
+ }
317
+ };
318
+
342
319
  /**
343
320
  * For the substrate-tokens (and other) modules, we configure the `onChainId` field in chaindata to tell the module how to query each token.
344
321
  * These queries are made to the tokens pallet.
@@ -396,28 +373,27 @@ Object.defineProperty(exports, "getLookupFn", {
396
373
  enumerable: true,
397
374
  get: function () { return metadataBuilders.getLookupFn; }
398
375
  });
399
- Object.defineProperty(exports, "fromHex", {
400
- enumerable: true,
401
- get: function () { return utils.fromHex; }
402
- });
403
- Object.defineProperty(exports, "toHex", {
376
+ Object.defineProperty(exports, "decAnyMetadata", {
404
377
  enumerable: true,
405
- get: function () { return utils.toHex; }
378
+ get: function () { return substrateBindings.decAnyMetadata; }
406
379
  });
407
380
  Object.defineProperty(exports, "metadata", {
408
381
  enumerable: true,
409
382
  get: function () { return substrateBindings.metadata; }
410
383
  });
411
- Object.defineProperty(exports, "v14", {
384
+ Object.defineProperty(exports, "unifyMetadata", {
412
385
  enumerable: true,
413
- get: function () { return substrateBindings.v14; }
386
+ get: function () { return substrateBindings.unifyMetadata; }
414
387
  });
415
- Object.defineProperty(exports, "v15", {
388
+ Object.defineProperty(exports, "fromHex", {
416
389
  enumerable: true,
417
- get: function () { return substrateBindings.v15; }
390
+ get: function () { return utils.fromHex; }
391
+ });
392
+ Object.defineProperty(exports, "toHex", {
393
+ enumerable: true,
394
+ get: function () { return utils.toHex; }
418
395
  });
419
396
  exports.compactMetadata = compactMetadata;
420
- exports.decodeMetadata = decodeMetadata;
421
397
  exports.decodeScale = decodeScale;
422
398
  exports.encodeMetadata = encodeMetadata;
423
399
  exports.encodeStateKey = encodeStateKey;
@@ -1,8 +1,8 @@
1
1
  export { getDynamicBuilder, getLookupFn } from '@polkadot-api/metadata-builders';
2
+ import { metadata, Binary } from '@polkadot-api/substrate-bindings';
3
+ export { decAnyMetadata, metadata, unifyMetadata } from '@polkadot-api/substrate-bindings';
2
4
  import { toHex } from '@polkadot-api/utils';
3
5
  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
6
  import anylogger from 'anylogger';
7
7
  import { Struct, u8, u32 } from 'scale-ts';
8
8
 
@@ -23,7 +23,10 @@ var log = anylogger(packageJson.name);
23
23
  * E.g. if `palletsAndItems` is `{ pallet: "System", items: ["Account"] }`, then only the
24
24
  * types used in the `System.Account` storage query will remain inside of metadata.lookups.
25
25
  */
26
- const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods = [], extraKeepTypes = []) => {
26
+ const compactMetadata = (anyMetadata, palletsAndItems = [], runtimeApisAndMethods = [], extraKeepTypes = []) => {
27
+ if (!isCompactableMetadata(anyMetadata)) throw new Error(`Metadata version ${anyMetadata.metadata.tag} not supported in compactMetadata`);
28
+ const metadata = anyMetadata.metadata.value;
29
+
27
30
  // remove pallets we don't care about
28
31
  metadata.pallets = metadata.pallets.filter(pallet =>
29
32
  // keep this pallet if it's listed in `palletsAndItems`
@@ -120,14 +123,11 @@ const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods =
120
123
  return newTypeId ?? 0;
121
124
  };
122
125
  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 = [];
126
+ if ("address" in metadata.extrinsic) metadata.extrinsic.address = 0;
127
+ if ("call" in metadata.extrinsic) metadata.extrinsic.call = 0;
128
+ if ("signature" in metadata.extrinsic) metadata.extrinsic.signature = 0;
129
+ if ("extra" in metadata.extrinsic) metadata.extrinsic.extra = 0;
130
+ if ("signedExtensions" in metadata.extrinsic) metadata.extrinsic.signedExtensions = [];
131
131
  if ("outerEnums" in metadata) {
132
132
  // metadata is v15 (NOT v14)
133
133
  metadata.outerEnums.call = 0;
@@ -135,6 +135,16 @@ const compactMetadata = (metadata, palletsAndItems = [], runtimeApisAndMethods =
135
135
  metadata.outerEnums.event = 0;
136
136
  }
137
137
  };
138
+ const isCompactableMetadata = metadata => {
139
+ switch (metadata.metadata.tag) {
140
+ case "v14":
141
+ case "v15":
142
+ case "v16":
143
+ return true;
144
+ default:
145
+ return false;
146
+ }
147
+ };
138
148
  const addDependentTypes = (metadataTysMap, keepTypes, types,
139
149
  // Prevent stack overflow when a type references itself
140
150
  addedTypes = new Set()) => {
@@ -265,43 +275,6 @@ const remapRuntimeApisTypeIds = (metadata, getNewTypeId) => {
265
275
  }
266
276
  };
267
277
 
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
278
  const decodeScale = (scaleCoder, change, error) => {
306
279
  if (change === null) return null;
307
280
  try {
@@ -312,19 +285,7 @@ const decodeScale = (scaleCoder, change, error) => {
312
285
  }
313
286
  };
314
287
 
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
- }));
288
+ const encodeMetadata = metadata$1 => toHex(metadata.enc(metadata$1));
328
289
 
329
290
  const encodeStateKey = (scaleCoder, error, ...args) => {
330
291
  try {
@@ -335,6 +296,22 @@ const encodeStateKey = (scaleCoder, error, ...args) => {
335
296
  }
336
297
  };
337
298
 
299
+ /**
300
+ * Extracts the `version` u8 from a SCALE-encoded metadata blob and returns it as a `number`.
301
+ *
302
+ * Only reads the first 40 bytes of the blob.
303
+ */
304
+ const getMetadataVersion = metadataRpc => {
305
+ try {
306
+ return Struct({
307
+ magicNumber: u32,
308
+ version: u8
309
+ }).dec(metadataRpc).version;
310
+ } catch {
311
+ return 0;
312
+ }
313
+ };
314
+
338
315
  /**
339
316
  * For the substrate-tokens (and other) modules, we configure the `onChainId` field in chaindata to tell the module how to query each token.
340
317
  * These queries are made to the tokens pallet.
@@ -384,4 +361,4 @@ const papiStringify = (value, space) => {
384
361
  return JSON.stringify(value, replacer, space);
385
362
  };
386
363
 
387
- export { compactMetadata, decodeMetadata, decodeScale, encodeMetadata, encodeStateKey, getMetadataVersion, magicNumber, papiParse, papiStringify };
364
+ export { compactMetadata, decodeScale, encodeMetadata, encodeStateKey, getMetadataVersion, magicNumber, papiParse, papiStringify };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@talismn/scale",
3
- "version": "0.1.1",
3
+ "version": "0.1.2",
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
  },
@@ -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
- });