@meshsdk/common 1.9.0-beta.93 → 1.9.0-beta.95

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/dist/index.cjs CHANGED
@@ -1563,6 +1563,7 @@ function getFile(url) {
1563
1563
  }
1564
1564
 
1565
1565
  // src/data/value.ts
1566
+ var compareByteOrder = (a, b) => a < b ? -1 : a > b ? 1 : 0;
1566
1567
  var value = (assets) => {
1567
1568
  return MeshValue.fromAssets(assets).toJSON();
1568
1569
  };
@@ -1574,6 +1575,26 @@ var MeshValue = class _MeshValue {
1574
1575
  constructor(value2 = {}) {
1575
1576
  this.value = value2;
1576
1577
  }
1578
+ /**
1579
+ * Sort a Value (JSON representation) by policy ID then token name
1580
+ * @param plutusValue The Value to sort
1581
+ * @returns Sorted Value
1582
+ */
1583
+ static sortValue = (plutusValue) => {
1584
+ const sortedPolicies = [...plutusValue.map].sort(
1585
+ (a, b) => compareByteOrder(a.k.bytes, b.k.bytes)
1586
+ );
1587
+ const sortedMap = sortedPolicies.map((policyEntry) => {
1588
+ const sortedTokens = [...policyEntry.v.map].sort(
1589
+ (a, b) => compareByteOrder(a.k.bytes, b.k.bytes)
1590
+ );
1591
+ return {
1592
+ k: policyEntry.k,
1593
+ v: { map: sortedTokens }
1594
+ };
1595
+ });
1596
+ return { map: sortedMap };
1597
+ };
1577
1598
  /**
1578
1599
  * Converting assets into MeshValue
1579
1600
  * @param assets The assets to convert
@@ -1782,17 +1803,18 @@ var MeshValue = class _MeshValue {
1782
1803
  };
1783
1804
  /**
1784
1805
  * Convert the MeshValue object into Cardano data Value in Mesh Data type
1806
+ * Entries are sorted by byte ordering of policy ID, then token name
1785
1807
  */
1786
1808
  toData = () => {
1787
- const valueMap = /* @__PURE__ */ new Map();
1809
+ const unsortedMap = /* @__PURE__ */ new Map();
1788
1810
  this.toAssets().forEach((asset) => {
1789
1811
  const sanitizedName = asset.unit.replace("lovelace", "");
1790
1812
  const policy = sanitizedName.slice(0, 56) || "";
1791
1813
  const token = sanitizedName.slice(56) || "";
1792
- if (!valueMap.has(policy)) {
1793
- valueMap.set(policy, /* @__PURE__ */ new Map());
1814
+ if (!unsortedMap.has(policy)) {
1815
+ unsortedMap.set(policy, /* @__PURE__ */ new Map());
1794
1816
  }
1795
- const tokenMap = valueMap.get(policy);
1817
+ const tokenMap = unsortedMap.get(policy);
1796
1818
  const quantity = tokenMap?.get(token);
1797
1819
  if (!quantity) {
1798
1820
  tokenMap.set(token, BigInt(asset.quantity));
@@ -1800,10 +1822,24 @@ var MeshValue = class _MeshValue {
1800
1822
  tokenMap.set(token, quantity + BigInt(asset.quantity));
1801
1823
  }
1802
1824
  });
1825
+ const sortedPolicies = Array.from(unsortedMap.keys()).sort(compareByteOrder);
1826
+ const valueMap = /* @__PURE__ */ new Map();
1827
+ sortedPolicies.forEach((policy) => {
1828
+ const unsortedTokenMap = unsortedMap.get(policy);
1829
+ const sortedTokens = Array.from(unsortedTokenMap.keys()).sort(
1830
+ compareByteOrder
1831
+ );
1832
+ const sortedTokenMap = /* @__PURE__ */ new Map();
1833
+ sortedTokens.forEach((token) => {
1834
+ sortedTokenMap.set(token, unsortedTokenMap.get(token));
1835
+ });
1836
+ valueMap.set(policy, sortedTokenMap);
1837
+ });
1803
1838
  return valueMap;
1804
1839
  };
1805
1840
  /**
1806
1841
  * Convert the MeshValue object into a JSON representation of Cardano data Value
1842
+ * Entries are sorted by byte ordering of policy ID, then token name
1807
1843
  * @returns Cardano data Value in JSON
1808
1844
  */
1809
1845
  toJSON = () => {
@@ -1822,11 +1858,16 @@ var MeshValue = class _MeshValue {
1822
1858
  valueMap[policy][token] += Number(asset.quantity);
1823
1859
  }
1824
1860
  });
1825
- Object.keys(valueMap).forEach((policy) => {
1861
+ const sortedPolicies = Object.keys(valueMap).sort(compareByteOrder);
1862
+ sortedPolicies.forEach((policy) => {
1826
1863
  const policyByte = currencySymbol(policy);
1827
- const tokens = Object.keys(valueMap[policy]).map(
1828
- (name) => [tokenName(name), integer(valueMap[policy][name])]
1864
+ const sortedTokenNames = Object.keys(valueMap[policy]).sort(
1865
+ compareByteOrder
1829
1866
  );
1867
+ const tokens = sortedTokenNames.map((name) => [
1868
+ tokenName(name),
1869
+ integer(valueMap[policy][name])
1870
+ ]);
1830
1871
  const policyMap = assocMap(tokens);
1831
1872
  valueMapToParse.push([policyByte, policyMap]);
1832
1873
  });
package/dist/index.d.cts CHANGED
@@ -1231,6 +1231,12 @@ declare const mValue: (assets: Asset[]) => MValue;
1231
1231
  declare class MeshValue {
1232
1232
  value: Record<string, bigint>;
1233
1233
  constructor(value?: Record<string, bigint>);
1234
+ /**
1235
+ * Sort a Value (JSON representation) by policy ID then token name
1236
+ * @param plutusValue The Value to sort
1237
+ * @returns Sorted Value
1238
+ */
1239
+ static sortValue: (plutusValue: Value) => Value;
1234
1240
  /**
1235
1241
  * Converting assets into MeshValue
1236
1242
  * @param assets The assets to convert
@@ -1341,10 +1347,12 @@ declare class MeshValue {
1341
1347
  toAssets: () => Asset[];
1342
1348
  /**
1343
1349
  * Convert the MeshValue object into Cardano data Value in Mesh Data type
1350
+ * Entries are sorted by byte ordering of policy ID, then token name
1344
1351
  */
1345
1352
  toData: () => MValue;
1346
1353
  /**
1347
1354
  * Convert the MeshValue object into a JSON representation of Cardano data Value
1355
+ * Entries are sorted by byte ordering of policy ID, then token name
1348
1356
  * @returns Cardano data Value in JSON
1349
1357
  */
1350
1358
  toJSON: () => Value;
package/dist/index.d.ts CHANGED
@@ -1231,6 +1231,12 @@ declare const mValue: (assets: Asset[]) => MValue;
1231
1231
  declare class MeshValue {
1232
1232
  value: Record<string, bigint>;
1233
1233
  constructor(value?: Record<string, bigint>);
1234
+ /**
1235
+ * Sort a Value (JSON representation) by policy ID then token name
1236
+ * @param plutusValue The Value to sort
1237
+ * @returns Sorted Value
1238
+ */
1239
+ static sortValue: (plutusValue: Value) => Value;
1234
1240
  /**
1235
1241
  * Converting assets into MeshValue
1236
1242
  * @param assets The assets to convert
@@ -1341,10 +1347,12 @@ declare class MeshValue {
1341
1347
  toAssets: () => Asset[];
1342
1348
  /**
1343
1349
  * Convert the MeshValue object into Cardano data Value in Mesh Data type
1350
+ * Entries are sorted by byte ordering of policy ID, then token name
1344
1351
  */
1345
1352
  toData: () => MValue;
1346
1353
  /**
1347
1354
  * Convert the MeshValue object into a JSON representation of Cardano data Value
1355
+ * Entries are sorted by byte ordering of policy ID, then token name
1348
1356
  * @returns Cardano data Value in JSON
1349
1357
  */
1350
1358
  toJSON: () => Value;
package/dist/index.js CHANGED
@@ -1404,6 +1404,7 @@ function getFile(url) {
1404
1404
  }
1405
1405
 
1406
1406
  // src/data/value.ts
1407
+ var compareByteOrder = (a, b) => a < b ? -1 : a > b ? 1 : 0;
1407
1408
  var value = (assets) => {
1408
1409
  return MeshValue.fromAssets(assets).toJSON();
1409
1410
  };
@@ -1415,6 +1416,26 @@ var MeshValue = class _MeshValue {
1415
1416
  constructor(value2 = {}) {
1416
1417
  this.value = value2;
1417
1418
  }
1419
+ /**
1420
+ * Sort a Value (JSON representation) by policy ID then token name
1421
+ * @param plutusValue The Value to sort
1422
+ * @returns Sorted Value
1423
+ */
1424
+ static sortValue = (plutusValue) => {
1425
+ const sortedPolicies = [...plutusValue.map].sort(
1426
+ (a, b) => compareByteOrder(a.k.bytes, b.k.bytes)
1427
+ );
1428
+ const sortedMap = sortedPolicies.map((policyEntry) => {
1429
+ const sortedTokens = [...policyEntry.v.map].sort(
1430
+ (a, b) => compareByteOrder(a.k.bytes, b.k.bytes)
1431
+ );
1432
+ return {
1433
+ k: policyEntry.k,
1434
+ v: { map: sortedTokens }
1435
+ };
1436
+ });
1437
+ return { map: sortedMap };
1438
+ };
1418
1439
  /**
1419
1440
  * Converting assets into MeshValue
1420
1441
  * @param assets The assets to convert
@@ -1623,17 +1644,18 @@ var MeshValue = class _MeshValue {
1623
1644
  };
1624
1645
  /**
1625
1646
  * Convert the MeshValue object into Cardano data Value in Mesh Data type
1647
+ * Entries are sorted by byte ordering of policy ID, then token name
1626
1648
  */
1627
1649
  toData = () => {
1628
- const valueMap = /* @__PURE__ */ new Map();
1650
+ const unsortedMap = /* @__PURE__ */ new Map();
1629
1651
  this.toAssets().forEach((asset) => {
1630
1652
  const sanitizedName = asset.unit.replace("lovelace", "");
1631
1653
  const policy = sanitizedName.slice(0, 56) || "";
1632
1654
  const token = sanitizedName.slice(56) || "";
1633
- if (!valueMap.has(policy)) {
1634
- valueMap.set(policy, /* @__PURE__ */ new Map());
1655
+ if (!unsortedMap.has(policy)) {
1656
+ unsortedMap.set(policy, /* @__PURE__ */ new Map());
1635
1657
  }
1636
- const tokenMap = valueMap.get(policy);
1658
+ const tokenMap = unsortedMap.get(policy);
1637
1659
  const quantity = tokenMap?.get(token);
1638
1660
  if (!quantity) {
1639
1661
  tokenMap.set(token, BigInt(asset.quantity));
@@ -1641,10 +1663,24 @@ var MeshValue = class _MeshValue {
1641
1663
  tokenMap.set(token, quantity + BigInt(asset.quantity));
1642
1664
  }
1643
1665
  });
1666
+ const sortedPolicies = Array.from(unsortedMap.keys()).sort(compareByteOrder);
1667
+ const valueMap = /* @__PURE__ */ new Map();
1668
+ sortedPolicies.forEach((policy) => {
1669
+ const unsortedTokenMap = unsortedMap.get(policy);
1670
+ const sortedTokens = Array.from(unsortedTokenMap.keys()).sort(
1671
+ compareByteOrder
1672
+ );
1673
+ const sortedTokenMap = /* @__PURE__ */ new Map();
1674
+ sortedTokens.forEach((token) => {
1675
+ sortedTokenMap.set(token, unsortedTokenMap.get(token));
1676
+ });
1677
+ valueMap.set(policy, sortedTokenMap);
1678
+ });
1644
1679
  return valueMap;
1645
1680
  };
1646
1681
  /**
1647
1682
  * Convert the MeshValue object into a JSON representation of Cardano data Value
1683
+ * Entries are sorted by byte ordering of policy ID, then token name
1648
1684
  * @returns Cardano data Value in JSON
1649
1685
  */
1650
1686
  toJSON = () => {
@@ -1663,11 +1699,16 @@ var MeshValue = class _MeshValue {
1663
1699
  valueMap[policy][token] += Number(asset.quantity);
1664
1700
  }
1665
1701
  });
1666
- Object.keys(valueMap).forEach((policy) => {
1702
+ const sortedPolicies = Object.keys(valueMap).sort(compareByteOrder);
1703
+ sortedPolicies.forEach((policy) => {
1667
1704
  const policyByte = currencySymbol(policy);
1668
- const tokens = Object.keys(valueMap[policy]).map(
1669
- (name) => [tokenName(name), integer(valueMap[policy][name])]
1705
+ const sortedTokenNames = Object.keys(valueMap[policy]).sort(
1706
+ compareByteOrder
1670
1707
  );
1708
+ const tokens = sortedTokenNames.map((name) => [
1709
+ tokenName(name),
1710
+ integer(valueMap[policy][name])
1711
+ ]);
1671
1712
  const policyMap = assocMap(tokens);
1672
1713
  valueMapToParse.push([policyByte, policyMap]);
1673
1714
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshsdk/common",
3
- "version": "1.9.0-beta.93",
3
+ "version": "1.9.0-beta.95",
4
4
  "description": "Contains constants, types and interfaces used across the SDK and different serialization libraries",
5
5
  "main": "./dist/index.cjs",
6
6
  "browser": "./dist/index.js",