@talismn/scale 0.0.0-pr997-20231115151657 → 0.0.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.
@@ -30,16 +30,63 @@ function _interopNamespace(e) {
30
30
  var $__namespace = /*#__PURE__*/_interopNamespace($);
31
31
  var anylogger__default = /*#__PURE__*/_interopDefault(anylogger);
32
32
 
33
+ const ignoreModuleMessages = {
34
+ "PORTABLEREGISTRY:": ["Unable to determine runtime Event type, cannot inspect frame_system::EventRecord", "Unable to determine runtime Call type, cannot inspect sp_runtime::generic::unchecked_extrinsic::UncheckedExtrinsic"]
35
+ };
36
+ function suppressPortableRegistryConsoleWarnings() {
37
+ /* eslint-disable-next-line no-console */
38
+ const originalWarn = console.warn;
39
+
40
+ /* eslint-disable-next-line no-console */
41
+ console.warn = (...data) => {
42
+ const [, dataModule, dataMessage] = data;
43
+ const ignoreMessages = typeof dataModule === "string" && ignoreModuleMessages[dataModule];
44
+ if (Array.isArray(ignoreMessages) && ignoreMessages.includes(dataMessage)) return;
45
+ if (data[0] === "Unable to map Bytes to a lookup index") return;
46
+ if (data[0] === "Unable to map [u8; 32] to a lookup index") return;
47
+ if (data[0] === "Unable to map u16 to a lookup index") return;
48
+ if (data[0] === "Unable to map u32 to a lookup index") return;
49
+ if (data[0] === "Unable to map u64 to a lookup index") return;
50
+ if (data[0] === "Unable to map u128 to a lookup index") return;
51
+ originalWarn(...data);
52
+ };
53
+ }
54
+
33
55
  //
34
56
  // The simd variants of these hash fns are faster, but some devices don't support them.
35
57
  //
36
58
  // This file re-exports the faster fns from `./simd` for devices which support them,
37
59
  // and falls back to `./nosimd` re-exports for devices which do not.
38
60
  //
61
+ /**
62
+ * Show a useful error message when someone attempts to use one of the classes
63
+ * from this lib without waiting for them to be initialized.
64
+ */
65
+ class UninitializedHasher {
66
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars, @typescript-eslint/no-explicit-any
67
+ constructor(...args) {
68
+ throw new Error(`This class cannot be used before @talismn/scale has initialized it. Please await 'watCryptoWaitReady' before attempting to construct this class.`);
69
+ }
70
+
71
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
72
+ update(input) {
73
+ throw new Error(`This class cannot be used before @talismn/scale has initialized it. Please await 'watCryptoWaitReady' before attempting to construct this class.`);
74
+ }
75
+ digest() {
76
+ throw new Error(`This class cannot be used before @talismn/scale has initialized it. Please await 'watCryptoWaitReady' before attempting to construct this class.`);
77
+ }
78
+ digestInto(digest) {
79
+ digest.set(this.digest());
80
+ }
81
+ dispose() {
82
+ throw new Error(`This class cannot be used before @talismn/scale has initialized it. Please await 'watCryptoWaitReady' before attempting to construct this class.`);
83
+ }
84
+ }
85
+
39
86
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
40
- let Blake2b;
87
+ let Blake2b = UninitializedHasher;
41
88
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
42
- let Xxhash;
89
+ let Xxhash = UninitializedHasher;
43
90
  let readyPromise = null;
44
91
  const watCryptoWaitReady = async () => {
45
92
  // if this is the second/third/etc time we've called watCryptoWaitReady,
@@ -63,7 +110,7 @@ const watCryptoWaitReady = async () => {
63
110
  Xxhash = imported.Xxhash;
64
111
  } else {
65
112
  // system does not support wasm simd
66
- const imported = await Promise.resolve().then(function () { return require('./nosimd-3e5ed5a9.cjs.prod.js'); });
113
+ const imported = await Promise.resolve().then(function () { return require('./nosimd-b32b4901.cjs.prod.js'); });
67
114
  Blake2b = imported.Blake2b;
68
115
  Xxhash = imported.Xxhash;
69
116
  }
@@ -448,6 +495,7 @@ const blake2_128Concat = new Blake2Hasher(128, true);
448
495
  const blake2_256 = new Blake2Hasher(256, false);
449
496
  const blake2_512 = new Blake2Hasher(512, false);
450
497
  const identity = new IdentityHasher();
498
+ const twox64 = new TwoxHasher(64, false);
451
499
  const twox128 = new TwoxHasher(128, false);
452
500
  const twox256 = new TwoxHasher(256, false);
453
501
  const twox64Concat = new TwoxHasher(64, true);
@@ -1161,9 +1209,12 @@ function transformMetadata(metadata) {
1161
1209
  }
1162
1210
  };
1163
1211
  function getExtrinsicParameter(key) {
1212
+ if (!metadata.extrinsic.ty) return $__namespace.never;
1164
1213
  const extrinsicTy = metadata.tys[metadata.extrinsic.ty];
1165
1214
  if (!extrinsicTy) return $__namespace.never;
1166
- return ids[extrinsicTy.params.find(x => x.name.toLowerCase() === key).ty];
1215
+ const id = extrinsicTy.params.find(x => x.name.toLowerCase() === key)?.ty;
1216
+ if (id === undefined) return $__namespace.never;
1217
+ return ids[id];
1167
1218
  }
1168
1219
  function getExtensionsCodec(key) {
1169
1220
  return $__namespace.object(...metadata.extrinsic.signedExtensions.flatMap(ext => {
@@ -1309,7 +1360,7 @@ class SignerError extends Error {
1309
1360
 
1310
1361
  var packageJson = {
1311
1362
  name: "@talismn/scale",
1312
- version: "0.0.0-pr997-20231115151657",
1363
+ version: "0.0.2",
1313
1364
  author: "Talisman",
1314
1365
  homepage: "https://talisman.xyz",
1315
1366
  license: "GPL-3.0-or-later",
@@ -1335,14 +1386,14 @@ var packageJson = {
1335
1386
  clean: "rm -rf dist && rm -rf .turbo rm -rf node_modules"
1336
1387
  },
1337
1388
  dependencies: {
1338
- "@talismn/subshape-fork": "^0.0.1",
1389
+ "@talismn/subshape-fork": "^0.0.2",
1339
1390
  "@talismn/util": "workspace:*",
1340
1391
  anylogger: "^1.0.11",
1341
- "wasm-feature-detect": "^1.6.0",
1392
+ "wasm-feature-detect": "^1.6.1",
1342
1393
  "wat-the-crypto": "^0.0.3"
1343
1394
  },
1344
1395
  devDependencies: {
1345
- "@polkadot/util-crypto": "^11.1.1",
1396
+ "@polkadot/util-crypto": "^12.3.2",
1346
1397
  "@talismn/eslint-config": "workspace:*",
1347
1398
  "@talismn/tsconfig": "workspace:*",
1348
1399
  "@types/jest": "^27.5.1",
@@ -1352,7 +1403,7 @@ var packageJson = {
1352
1403
  typescript: "^5.2.2"
1353
1404
  },
1354
1405
  peerDependencies: {
1355
- "@polkadot/util-crypto": "^11.x"
1406
+ "@polkadot/util-crypto": "12.x"
1356
1407
  },
1357
1408
  eslintConfig: {
1358
1409
  root: true,
@@ -1364,6 +1415,14 @@ var packageJson = {
1364
1415
 
1365
1416
  var log = anylogger__default["default"](packageJson.name);
1366
1417
 
1418
+ const getMetadataVersion = metadataRpc => {
1419
+ // https://docs.substrate.io/build/application-development/#metadata-system
1420
+ const magicNumber = 1635018093;
1421
+ const {
1422
+ version
1423
+ } = $__namespace.object($__namespace.field("magicNumber", $__namespace.constant(magicNumber, $__namespace.u32)), $__namespace.field("version", $__namespace.u8)).decode($__namespace.decodeHex(metadataRpc));
1424
+ return version;
1425
+ };
1367
1426
  const filterMetadataPalletsAndItems = (metadata, palletsAndItems, extraKeepTypes) => {
1368
1427
  // remove pallets we don't care about
1369
1428
  metadata.pallets = metadata.pallets.filter(pallet =>
@@ -1379,7 +1438,7 @@ const filterMetadataPalletsAndItems = (metadata, palletsAndItems, extraKeepTypes
1379
1438
  }) => {
1380
1439
  const pallet = metadata.pallets.find(palletFilter);
1381
1440
  if (!pallet || !pallet.storage) {
1382
- log.warn("Failed to find pallet", palletFilter);
1441
+ log.debug("Failed to find pallet", palletFilter);
1383
1442
  return [];
1384
1443
  }
1385
1444
 
@@ -1461,6 +1520,209 @@ addedTypes = new Set()) => {
1461
1520
  }
1462
1521
  };
1463
1522
 
1523
+ /**
1524
+ * This module is largely copied from https://github.com/0xKheops/substrate-metadata-explorer/blob/4b5a991e5ced45cad3b8675ff9104b8366d20429/packages/sme-codegen/src/types/getConstantVariableName.ts
1525
+ *
1526
+ * The primary difference between this module and `sme-codegen` is in the output.
1527
+ *
1528
+ * The `sme-codegen` module exports typescript code as a string, which can then be interpretted in order to construct subshape objects.
1529
+ *
1530
+ * Whereas this module directly exports the subshape objects described by that code.
1531
+ */
1532
+
1533
+ /** Returns a unique name (relative to all of the types in `metadata`) to identify this type. */
1534
+ const getTypeName = (metadata, type) => {
1535
+ const uniqueName = getUniqueTypeName(metadata, type);
1536
+ return `${uniqueName.charAt(0).toUpperCase()}${uniqueName.substring(1)}`;
1537
+ };
1538
+
1539
+ /**
1540
+ * Tries each of `getSimpleTypeName`, `getSmartTypeName`, `getFullTypeName` in order and
1541
+ * returns the first name which is unique in the collection of types in `metadata`.
1542
+ */
1543
+ const getUniqueTypeName = (metadata, type) => {
1544
+ const rawTypeName = getRawTypeName(type);
1545
+ if (type.path.length < 1) return rawTypeName;
1546
+
1547
+ // use simpleName if it is unique
1548
+ const simpleName = getSimpleTypeName(type);
1549
+ if (!metadata.tys.some(t => t.id !== type.id && getSimpleTypeName(t) === simpleName)) return simpleName;
1550
+
1551
+ // use smartName if it is unique
1552
+ const smartName = getSmartTypeName(type);
1553
+ if (!metadata.tys.some(t => t.id !== type.id && getSmartTypeName(t) === smartName)) return smartName;
1554
+
1555
+ // use fullName if it is unique
1556
+ const fullName = getFullTypeName(type);
1557
+ if (!metadata.tys.some(t => t.id !== type.id && getFullTypeName(t) === fullName))
1558
+ // return if fullName is unique
1559
+ return fullName;
1560
+
1561
+ // use fullName + type number
1562
+ return `${fullName}${type.id}`;
1563
+ };
1564
+
1565
+ /** Gets "Type" + type number */
1566
+ const getRawTypeName = type => `Type${type.id}`;
1567
+
1568
+ /** Gets the last element of `type.path` */
1569
+ const getSimpleTypeName = type => type.path.slice(-1)[0];
1570
+
1571
+ /** Gets the first two elements, and the last element, of `type.path` and joins them together with `::` */
1572
+ const getSmartTypeName = type => type.path.length > 3 ? normalizeTypeName([...type.path.slice(0, 2), ...type.path.slice(-1)].join("::")) : getFullTypeName(type);
1573
+
1574
+ /** Gets all elements of `type.path` and joins them together with `::` */
1575
+ const getFullTypeName = type => normalizeTypeName(type.path.join("::"));
1576
+
1577
+ /**
1578
+ * This module is largely copied from https://github.com/0xKheops/substrate-metadata-explorer/blob/4b5a991e5ced45cad3b8675ff9104b8366d20429/packages/sme-codegen/src/types/getShape.ts
1579
+ *
1580
+ * The primary difference between this module and `sme-codegen` is in the output.
1581
+ *
1582
+ * The `sme-codegen` module exports typescript code as a string, which can then be interpretted in order to construct subshape objects.
1583
+ *
1584
+ * Whereas this module directly exports the subshape objects described by that code.
1585
+ */
1586
+ const getShape = (metadata, typeId, gotShapes = new Map()) => {
1587
+ const type = metadata.tys.find(type => type.id === typeId);
1588
+ if (!type) throw new Error(`Type not found (${typeId})`);
1589
+
1590
+ // Short circuit if we've already encountered this shape
1591
+ const typeName = getTypeName(metadata, type);
1592
+ if (gotShapes.has(typeName)) return gotShapes.get(typeName);
1593
+
1594
+ // TODO: Use `$.deferred(() => Type)` for self-referential types
1595
+
1596
+ // Get shape, add to gotShapes list, return shape
1597
+ const shape = getTypeShape(metadata, type, gotShapes);
1598
+ gotShapes.set(typeName, shape);
1599
+ return shape;
1600
+ };
1601
+ const getTypeShape = (metadata, type, gotShapes) => {
1602
+ const tyType = type.type;
1603
+ switch (tyType) {
1604
+ case "Primitive":
1605
+ return getPrimitiveShape(type);
1606
+ case "SizedArray":
1607
+ return getSizedArrayShape(metadata, type, gotShapes);
1608
+ case "Compact":
1609
+ return getCompactShape(metadata, type, gotShapes);
1610
+ case "Sequence":
1611
+ return getSequenceShape(metadata, type, gotShapes);
1612
+ case "Struct":
1613
+ return getStructShape(metadata, type, gotShapes);
1614
+ case "Tuple":
1615
+ return getTupleShape(metadata, type, gotShapes);
1616
+ case "Union":
1617
+ return getUnionShape(metadata, type, gotShapes);
1618
+ case "BitSequence":
1619
+ return $__namespace.bitSequence;
1620
+ default:
1621
+ {
1622
+ // force compilation error if any types don't have a case
1623
+ const exhaustiveCheck = tyType;
1624
+ throw new Error(`Unsupported type shape ${exhaustiveCheck}`);
1625
+ }
1626
+ }
1627
+ };
1628
+ const tyIsU8 = type => type?.type === "Primitive" && type.kind === "u8";
1629
+ const getPrimitiveShape = primitive => {
1630
+ // TODO: Test that `char` and `$.u8` are equivalent (`$.char` does not exist)
1631
+ if (primitive.kind === "char") return $__namespace.u8;
1632
+ return $__namespace[primitive.kind];
1633
+ };
1634
+ const getSizedArrayShape = (metadata, sizedArray, gotShapes) => {
1635
+ // Get the type definition for the items of this array from the metadata
1636
+ const typeParam = metadata.tys.find(({
1637
+ id
1638
+ }) => id === sizedArray.typeParam);
1639
+ if (!typeParam) {
1640
+ const typeName = getTypeName(metadata, sizedArray);
1641
+ throw new Error(`Could not find typeParam ${sizedArray.typeParam} for sizedArray ${typeName}`);
1642
+ }
1643
+
1644
+ // Shortcut for uint8 arrays
1645
+ if (tyIsU8(typeParam)) return $__namespace.sizedUint8Array(sizedArray.len);
1646
+
1647
+ // Get the subshape object for the items of this array
1648
+ const typeParamShape = getShape(metadata, typeParam.id, gotShapes);
1649
+
1650
+ // Return a subshape sizedArray
1651
+ return $__namespace.sizedArray(typeParamShape, sizedArray.len);
1652
+ };
1653
+ const getCompactShape = (metadata, compact, gotShapes) => {
1654
+ // Get the type definition for the item of this compact from the metadata
1655
+ const typeParam = metadata.tys.find(({
1656
+ id
1657
+ }) => id === compact.typeParam);
1658
+ if (!typeParam) {
1659
+ const typeName = getTypeName(metadata, compact);
1660
+ throw new Error(`Could not find typeParam ${compact.typeParam} for compact ${typeName}`);
1661
+ }
1662
+
1663
+ // Get the subshape object for the item of this compact
1664
+ const typeParamShape = getShape(metadata, typeParam.id, gotShapes);
1665
+
1666
+ // Return a subshape compact
1667
+ return $__namespace.compact(typeParamShape);
1668
+ };
1669
+ const getSequenceShape = (metadata, sequence, gotShapes) => {
1670
+ // Get the type definition for the items of this sequence from the metadata
1671
+ const typeParam = metadata.tys.find(({
1672
+ id
1673
+ }) => id === sequence.typeParam);
1674
+ if (!typeParam) {
1675
+ const typeName = getTypeName(metadata, sequence);
1676
+ throw new Error(`Could not find typeParam ${sequence.typeParam} for sequence ${typeName}`);
1677
+ }
1678
+
1679
+ // Shortcut for uint8 sequences
1680
+ if (tyIsU8(typeParam)) return $__namespace.uint8Array;
1681
+
1682
+ // Get the subshape object for the items of this sequence
1683
+ const typeParamShape = getShape(metadata, typeParam.id, gotShapes);
1684
+
1685
+ // Return a subshape sequence
1686
+ return $__namespace.array(typeParamShape);
1687
+ };
1688
+ const getStructShape = (metadata, struct, gotShapes) => {
1689
+ // If there's only one field and it has no name, don't wrap it in $.object
1690
+ if (struct.fields.length === 1 && !struct.fields[0].name) return getShape(metadata, struct.fields[0].ty, gotShapes);
1691
+
1692
+ // Check that all fields have a name
1693
+ if (!struct.fields.every(field => field.name)) {
1694
+ const typeName = getTypeName(metadata, struct);
1695
+ throw new Error(`Could not build subshape object for struct ${struct.id} (${typeName})): Not all fields have a name`);
1696
+ }
1697
+
1698
+ // Get the type definition for the fields of this struct from the metadata
1699
+ const fieldsShape = struct.fields.map(field => $__namespace.field(normalizeIdent(field.name), getShape(metadata, field.ty, gotShapes)));
1700
+ return $__namespace.object(...fieldsShape);
1701
+ };
1702
+ const getTupleShape = (metadata, tuple, gotShapes) =>
1703
+ // Get the type definition for the fields of this tuple from the metadata and wrap them in `$.tuple`
1704
+ $__namespace.tuple(...tuple.fields.map(type => getShape(metadata, type, gotShapes)));
1705
+ const getUnionShape = (metadata, union, gotShapes) => {
1706
+ if (union.members.every(member => !member.fields.length)) return $__namespace.literalUnion(Object.fromEntries(union.members.map(member => [member.index, normalizeIdent(member.name)])));
1707
+
1708
+ // TODO: Check if invalid
1709
+ if (union.members.length === 2 && union.path[union.path.length - 1] === "Option" && union.members[0]?.name === "None" && union.members[1]?.name === "Some") return $__namespace.option(getShape(metadata, union.members[1].fields[0].ty, gotShapes));
1710
+ return $__namespace.taggedUnion("type", Object.fromEntries(union.members.map(member => {
1711
+ const args = [];
1712
+
1713
+ // invalid if only some fields (but not all of them) have a name
1714
+ if (member.fields.some(field => field.name) && !member.fields.every(field => field.name)) {
1715
+ const typeName = getTypeName(metadata, union);
1716
+ throw new Error(`Could not build subshape object for union ${union.id} (${typeName}): Not all fields have a name`);
1717
+ }
1718
+ if (member.fields.every(field => field.name)) for (const field of member.fields) args.push($__namespace.field(normalizeIdent(field.name), getShape(metadata, field.ty, gotShapes)));else if (member.fields.length > 1) args.push($__namespace.field("value", $__namespace.tuple(...member.fields.map(field => getShape(metadata, field.ty, gotShapes)))));else args.push($__namespace.field("value", getShape(metadata, member.fields[0].ty, gotShapes)));
1719
+ return [member.index, $__namespace.variant(normalizeIdent(member.name), ...args)];
1720
+ })));
1721
+ };
1722
+
1723
+ // TODO: Get the DX of this lib as close as possible to the DX of `const jsonResult = '{"someKey":"someValue"}'; JSON.decode(jsonResult)`
1724
+ suppressPortableRegistryConsoleWarnings();
1725
+
1464
1726
  exports.$emptyKey = $emptyKey;
1465
1727
  exports.$era = $era;
1466
1728
  exports.$extrinsic = $extrinsic;
@@ -1490,7 +1752,16 @@ exports.blake2_512 = blake2_512;
1490
1752
  exports.blake2_64 = blake2_64;
1491
1753
  exports.decodeMetadata = decodeMetadata;
1492
1754
  exports.filterMetadataPalletsAndItems = filterMetadataPalletsAndItems;
1755
+ exports.getFullTypeName = getFullTypeName;
1756
+ exports.getMetadataVersion = getMetadataVersion;
1493
1757
  exports.getOrInit = getOrInit;
1758
+ exports.getRawTypeName = getRawTypeName;
1759
+ exports.getShape = getShape;
1760
+ exports.getSimpleTypeName = getSimpleTypeName;
1761
+ exports.getSmartTypeName = getSmartTypeName;
1762
+ exports.getTypeName = getTypeName;
1763
+ exports.getTypeShape = getTypeShape;
1764
+ exports.getUniqueTypeName = getUniqueTypeName;
1494
1765
  exports.identity = identity;
1495
1766
  exports.normalizeDocs = normalizeDocs;
1496
1767
  exports.normalizeIdent = normalizeIdent;
@@ -1504,5 +1775,6 @@ exports.transformMetadataV14 = transformMetadata;
1504
1775
  exports.transformTys = transformTys;
1505
1776
  exports.twox128 = twox128;
1506
1777
  exports.twox256 = twox256;
1778
+ exports.twox64 = twox64;
1507
1779
  exports.twox64Concat = twox64Concat;
1508
1780
  exports.watCryptoWaitReady = watCryptoWaitReady;