@lucid-evolution/utils 0.2.0 → 0.2.1

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
@@ -1583,7 +1583,8 @@ var PROTOCOL_PARAMETERS_DEFAULT = {
1583
1583
  };
1584
1584
 
1585
1585
  // src/credential.ts
1586
- var import_core7 = require("@cardano-sdk/core");
1586
+ var import_core_utils4 = require("@lucid-evolution/core-utils");
1587
+ var import_base = require("@scure/base");
1587
1588
  function credentialToAddress(network, paymentCredential, stakeCredential) {
1588
1589
  if (stakeCredential) {
1589
1590
  return CML.BaseAddress.new(
@@ -1641,14 +1642,27 @@ function stakeCredentialOf(rewardAddress) {
1641
1642
  return stakeCredential;
1642
1643
  }
1643
1644
  var drepIDToCredential = (drepID) => {
1644
- if (!import_core7.Cardano.DRepID.isValid(drepID)) {
1645
- throw new Error(`Invalid DRep ID: ${drepID}`);
1645
+ const decoded = import_base.bech32.decodeUnsafe(drepID, 1023);
1646
+ if (!decoded) throw new Error(`Invalid DRep ID: ${drepID}`);
1647
+ const { prefix, words } = decoded;
1648
+ const isValid = prefix === "drep" && (words.length === 47 || words.length === 45) || prefix === "drep_script" && words.length === 45;
1649
+ if (!isValid) throw new Error(`Invalid DRep ID: ${drepID}`);
1650
+ const payload = import_base.bech32.fromWords(words);
1651
+ if (payload.length === 28) {
1652
+ return {
1653
+ type: drepID.includes("drep_script") ? "Script" : "Key",
1654
+ hash: (0, import_core_utils4.toHex)(payload)
1655
+ };
1656
+ }
1657
+ const header = payload[0];
1658
+ if (header !== 34 && header !== 35) {
1659
+ throw new Error(
1660
+ `Invalid DRep ID: ${drepID}, header 0x${header.toString(16).padStart(2, "0")} is neither a DRep key hash (0x22) nor a script hash (0x23)`
1661
+ );
1646
1662
  }
1647
- const drepId = import_core7.Cardano.DRepID(drepID);
1648
- const drepCred = import_core7.Cardano.DRepID.toCredential(drepId);
1649
1663
  return {
1650
- type: drepCred.type == import_core7.Cardano.CredentialType.KeyHash ? "Key" : "Script",
1651
- hash: drepCred.hash
1664
+ type: header === 35 ? "Script" : "Key",
1665
+ hash: (0, import_core_utils4.toHex)(payload.subarray(1))
1652
1666
  };
1653
1667
  };
1654
1668
 
@@ -1670,7 +1684,7 @@ function toPublicKey(privateKey) {
1670
1684
  }
1671
1685
 
1672
1686
  // src/label.ts
1673
- var import_core_utils4 = require("@lucid-evolution/core-utils");
1687
+ var import_core_utils5 = require("@lucid-evolution/core-utils");
1674
1688
  var import_crc8 = require("@lucid-evolution/crc8");
1675
1689
  function toLabel(num) {
1676
1690
  if (num < 0 || num > 65535) {
@@ -1691,7 +1705,7 @@ function fromLabel(label) {
1691
1705
  return check === checksum(numHex) ? num : null;
1692
1706
  }
1693
1707
  function checksum(num) {
1694
- return (0, import_crc8.crc8)((0, import_core_utils4.fromHex)(num)).toString(16).padStart(2, "0");
1708
+ return (0, import_crc8.crc8)((0, import_core_utils5.fromHex)(num)).toString(16).padStart(2, "0");
1695
1709
  }
1696
1710
 
1697
1711
  // src/time.ts
@@ -1704,16 +1718,16 @@ function slotToUnixTime(network, slot) {
1704
1718
  }
1705
1719
 
1706
1720
  // src/utxo.ts
1707
- var import_core_utils6 = require("@lucid-evolution/core-utils");
1721
+ var import_core_utils7 = require("@lucid-evolution/core-utils");
1708
1722
 
1709
1723
  // src/value.ts
1710
- var import_core_utils5 = require("@lucid-evolution/core-utils");
1711
- var import_effect = require("effect");
1724
+ var import_core_utils6 = require("@lucid-evolution/core-utils");
1725
+ var import_Function = require("effect/Function");
1712
1726
  function valueToAssets(value) {
1713
1727
  const assets = {};
1714
1728
  assets["lovelace"] = value.coin();
1715
1729
  if (value.has_multiassets()) {
1716
- (0, import_core_utils5.withCMLScope)((own) => {
1730
+ (0, import_core_utils6.withCMLScope)((own) => {
1717
1731
  const ma = own(value.multi_asset());
1718
1732
  const multiAssets = own(ma.keys());
1719
1733
  for (let j = 0; j < multiAssets.len(); j++) {
@@ -1732,7 +1746,7 @@ function valueToAssets(value) {
1732
1746
  return assets;
1733
1747
  }
1734
1748
  function assetsToValue(assets) {
1735
- return (0, import_core_utils5.withCMLScope)((own) => {
1749
+ return (0, import_core_utils6.withCMLScope)((own) => {
1736
1750
  const multiAsset = own(CML.MultiAsset.new());
1737
1751
  const lovelace = assets["lovelace"] ? BigInt(assets["lovelace"]) : 0n;
1738
1752
  const units = Object.keys(assets);
@@ -1795,12 +1809,12 @@ function addAssets(...assets) {
1795
1809
  }, {});
1796
1810
  }
1797
1811
  async function getUniqueTokenName(utxo) {
1798
- const id = (0, import_core_utils5.fromHex)(utxo.txHash);
1812
+ const id = (0, import_core_utils6.fromHex)(utxo.txHash);
1799
1813
  const data = new Uint8Array([utxo.outputIndex, ...id]);
1800
1814
  const hash = new Uint8Array(await crypto.subtle.digest("SHA3-256", data));
1801
- return (0, import_core_utils5.toHex)(hash);
1815
+ return (0, import_core_utils6.toHex)(hash);
1802
1816
  }
1803
- var sortCanonical = (assets) => (0, import_effect.pipe)(
1817
+ var sortCanonical = (assets) => (0, import_Function.pipe)(
1804
1818
  Object.entries(assets).sort(([aUnit], [bUnit]) => {
1805
1819
  const a = fromUnit(aUnit);
1806
1820
  const b = fromUnit(bUnit);
@@ -1819,7 +1833,7 @@ var sortCanonical = (assets) => (0, import_effect.pipe)(
1819
1833
  );
1820
1834
 
1821
1835
  // src/utxo.ts
1822
- var utxoToTransactionOutput = (utxo) => (0, import_core_utils6.withCMLScope)((own) => {
1836
+ var utxoToTransactionOutput = (utxo) => (0, import_core_utils7.withCMLScope)((own) => {
1823
1837
  const address = own(CML.Address.from_bech32(utxo.address));
1824
1838
  const value = own(assetsToValue(utxo.assets));
1825
1839
  const datum = utxo.datum ? own(CML.PlutusData.from_cbor_hex(utxo.datum)) : void 0;
@@ -1828,13 +1842,13 @@ var utxoToTransactionOutput = (utxo) => (0, import_core_utils6.withCMLScope)((ow
1828
1842
  const datumOption = datumHash ? CML.DatumOption.new_hash(datumHash) : datum ? CML.DatumOption.new_datum(datum) : void 0;
1829
1843
  return CML.TransactionOutput.new(address, value, datumOption, scriptRef);
1830
1844
  });
1831
- var utxoToTransactionInput = (utxo) => (0, import_core_utils6.withCMLScope)(
1845
+ var utxoToTransactionInput = (utxo) => (0, import_core_utils7.withCMLScope)(
1832
1846
  (own) => CML.TransactionInput.new(
1833
1847
  own(CML.TransactionHash.from_hex(utxo.txHash)),
1834
1848
  BigInt(utxo.outputIndex)
1835
1849
  )
1836
1850
  );
1837
- var utxoToCore = (utxo) => (0, import_core_utils6.withCMLScope)(
1851
+ var utxoToCore = (utxo) => (0, import_core_utils7.withCMLScope)(
1838
1852
  (own) => CML.TransactionUnspentOutput.new(
1839
1853
  own(utxoToTransactionInput(utxo)),
1840
1854
  own(utxoToTransactionOutput(utxo))
@@ -1848,7 +1862,7 @@ function utxosToCores(utxos) {
1848
1862
  return result;
1849
1863
  }
1850
1864
  function coreToUtxo(coreUtxo) {
1851
- return (0, import_core_utils6.withCMLScope)((own) => ({
1865
+ return (0, import_core_utils7.withCMLScope)((own) => ({
1852
1866
  ...coreToOutRef(own(coreUtxo.input())),
1853
1867
  ...coreToTxOutput(own(coreUtxo.output()))
1854
1868
  }));
@@ -1861,7 +1875,7 @@ function coresToUtxos(utxos) {
1861
1875
  return result;
1862
1876
  }
1863
1877
  function coreToOutRef(input) {
1864
- return (0, import_core_utils6.withCMLScope)((own) => ({
1878
+ return (0, import_core_utils7.withCMLScope)((own) => ({
1865
1879
  txHash: own(input.transaction_id()).to_hex(),
1866
1880
  outputIndex: parseInt(input.index().toString())
1867
1881
  }));
@@ -1874,7 +1888,7 @@ function coresToOutRefs(inputs) {
1874
1888
  return result;
1875
1889
  }
1876
1890
  function coreToTxOutput(output) {
1877
- return (0, import_core_utils6.withCMLScope)((own) => {
1891
+ return (0, import_core_utils7.withCMLScope)((own) => {
1878
1892
  const datum = own(output.datum());
1879
1893
  const scriptRef = own(output.script_ref());
1880
1894
  return {
@@ -1969,7 +1983,7 @@ function getInputIndices(indexInputs, allInputs, sorted = false) {
1969
1983
  else return [];
1970
1984
  });
1971
1985
  }
1972
- var calculateMinLovelaceFromUTxO = (coinsPerUtxoByte, utxo) => (0, import_core_utils6.withCMLScope)((own) => {
1986
+ var calculateMinLovelaceFromUTxO = (coinsPerUtxoByte, utxo) => (0, import_core_utils7.withCMLScope)((own) => {
1973
1987
  const value = own(assetsToValue(utxo.assets));
1974
1988
  const builder = own(
1975
1989
  buildOutput(utxo, own).with_asset_and_min_required_coin(
package/dist/index.d.cts CHANGED
@@ -78,6 +78,22 @@ declare function scriptHashToCredential(scriptHash: ScriptHash): Credential;
78
78
  declare function keyHashToCredential(keyHash: KeyHash): Credential;
79
79
  declare function paymentCredentialOf(address: Address): Credential;
80
80
  declare function stakeCredentialOf(rewardAddress: RewardAddress): Credential;
81
+ /**
82
+ * Accepts CIP-105 IDs (`drep` or `drep_script` and the bare hash) and CIP-129
83
+ * IDs (`drep`, a header byte and the hash), with the checks `@cardano-sdk/core`
84
+ * applies, without loading that package. A CIP-129 header must name a DRep
85
+ * (`0x2_`) and a key hash (`0x22`) or script hash (`0x23`), which
86
+ * `@cardano-sdk/core` does not check.
87
+ *
88
+ * @example
89
+ * import { drepIDToCredential } from "@lucid-evolution/utils";
90
+ *
91
+ * drepIDToCredential("drep1y2sm9s75uhmqwxpf8f94cmt737g2rvkr6njlvpcc9yaykhq23nmjy");
92
+ * // { type: "Key", hash: "a1b2c3d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c" }
93
+ *
94
+ * @since 0.1.65
95
+ * @category Credentials
96
+ */
81
97
  declare const drepIDToCredential: (drepID: string) => Credential;
82
98
 
83
99
  declare function datumToHash(datum: Datum): DatumHash;
package/dist/index.d.ts CHANGED
@@ -78,6 +78,22 @@ declare function scriptHashToCredential(scriptHash: ScriptHash): Credential;
78
78
  declare function keyHashToCredential(keyHash: KeyHash): Credential;
79
79
  declare function paymentCredentialOf(address: Address): Credential;
80
80
  declare function stakeCredentialOf(rewardAddress: RewardAddress): Credential;
81
+ /**
82
+ * Accepts CIP-105 IDs (`drep` or `drep_script` and the bare hash) and CIP-129
83
+ * IDs (`drep`, a header byte and the hash), with the checks `@cardano-sdk/core`
84
+ * applies, without loading that package. A CIP-129 header must name a DRep
85
+ * (`0x2_`) and a key hash (`0x22`) or script hash (`0x23`), which
86
+ * `@cardano-sdk/core` does not check.
87
+ *
88
+ * @example
89
+ * import { drepIDToCredential } from "@lucid-evolution/utils";
90
+ *
91
+ * drepIDToCredential("drep1y2sm9s75uhmqwxpf8f94cmt737g2rvkr6njlvpcc9yaykhq23nmjy");
92
+ * // { type: "Key", hash: "a1b2c3d4e5f60718293a4b5c6d7e8f90a1b2c3d4e5f60718293a4b5c" }
93
+ *
94
+ * @since 0.1.65
95
+ * @category Credentials
96
+ */
81
97
  declare const drepIDToCredential: (drepID: string) => Credential;
82
98
 
83
99
  declare function datumToHash(datum: Datum): DatumHash;
package/dist/index.js CHANGED
@@ -1492,7 +1492,8 @@ var PROTOCOL_PARAMETERS_DEFAULT = {
1492
1492
  };
1493
1493
 
1494
1494
  // src/credential.ts
1495
- import { Cardano } from "@cardano-sdk/core";
1495
+ import { toHex as toHex3 } from "@lucid-evolution/core-utils";
1496
+ import { bech32 } from "@scure/base";
1496
1497
  function credentialToAddress(network, paymentCredential, stakeCredential) {
1497
1498
  if (stakeCredential) {
1498
1499
  return CML.BaseAddress.new(
@@ -1550,14 +1551,27 @@ function stakeCredentialOf(rewardAddress) {
1550
1551
  return stakeCredential;
1551
1552
  }
1552
1553
  var drepIDToCredential = (drepID) => {
1553
- if (!Cardano.DRepID.isValid(drepID)) {
1554
- throw new Error(`Invalid DRep ID: ${drepID}`);
1554
+ const decoded = bech32.decodeUnsafe(drepID, 1023);
1555
+ if (!decoded) throw new Error(`Invalid DRep ID: ${drepID}`);
1556
+ const { prefix, words } = decoded;
1557
+ const isValid = prefix === "drep" && (words.length === 47 || words.length === 45) || prefix === "drep_script" && words.length === 45;
1558
+ if (!isValid) throw new Error(`Invalid DRep ID: ${drepID}`);
1559
+ const payload = bech32.fromWords(words);
1560
+ if (payload.length === 28) {
1561
+ return {
1562
+ type: drepID.includes("drep_script") ? "Script" : "Key",
1563
+ hash: toHex3(payload)
1564
+ };
1565
+ }
1566
+ const header = payload[0];
1567
+ if (header !== 34 && header !== 35) {
1568
+ throw new Error(
1569
+ `Invalid DRep ID: ${drepID}, header 0x${header.toString(16).padStart(2, "0")} is neither a DRep key hash (0x22) nor a script hash (0x23)`
1570
+ );
1555
1571
  }
1556
- const drepId = Cardano.DRepID(drepID);
1557
- const drepCred = Cardano.DRepID.toCredential(drepId);
1558
1572
  return {
1559
- type: drepCred.type == Cardano.CredentialType.KeyHash ? "Key" : "Script",
1560
- hash: drepCred.hash
1573
+ type: header === 35 ? "Script" : "Key",
1574
+ hash: toHex3(payload.subarray(1))
1561
1575
  };
1562
1576
  };
1563
1577
 
@@ -1622,10 +1636,10 @@ import { withCMLScope as withCMLScope4 } from "@lucid-evolution/core-utils";
1622
1636
  // src/value.ts
1623
1637
  import {
1624
1638
  fromHex as fromHex4,
1625
- toHex as toHex3,
1639
+ toHex as toHex4,
1626
1640
  withCMLScope as withCMLScope3
1627
1641
  } from "@lucid-evolution/core-utils";
1628
- import { pipe } from "effect";
1642
+ import { pipe } from "effect/Function";
1629
1643
  function valueToAssets(value) {
1630
1644
  const assets = {};
1631
1645
  assets["lovelace"] = value.coin();
@@ -1715,7 +1729,7 @@ async function getUniqueTokenName(utxo) {
1715
1729
  const id = fromHex4(utxo.txHash);
1716
1730
  const data = new Uint8Array([utxo.outputIndex, ...id]);
1717
1731
  const hash = new Uint8Array(await crypto.subtle.digest("SHA3-256", data));
1718
- return toHex3(hash);
1732
+ return toHex4(hash);
1719
1733
  }
1720
1734
  var sortCanonical = (assets) => pipe(
1721
1735
  Object.entries(assets).sort(([aUnit], [bUnit]) => {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@lucid-evolution/utils",
3
- "version": "0.2.0",
3
+ "version": "0.2.1",
4
4
  "description": "",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -25,21 +25,22 @@
25
25
  "@emurgo/cardano-serialization-lib-nodejs": "@emurgo/cardano-serialization-lib-browser"
26
26
  },
27
27
  "dependencies": {
28
- "@cardano-sdk/core": "^0.46.10",
29
28
  "@effect/schema": "^0.68.16",
30
29
  "@harmoniclabs/plutus-data": "^1.2.4",
31
30
  "@harmoniclabs/uplc": "^1.2.4",
31
+ "@scure/base": "^1.2.6",
32
32
  "bip39": "^3.1.0",
33
33
  "cbor-x": "^1.6.0",
34
34
  "effect": "^3.10.4",
35
+ "@lucid-evolution/cml": "0.1.1",
36
+ "@lucid-evolution/core-utils": "0.2.1",
37
+ "@lucid-evolution/core-types": "0.5.1",
35
38
  "@lucid-evolution/crc8": "0.1.8",
36
- "@lucid-evolution/core-utils": "0.2.0",
37
- "@lucid-evolution/core-types": "0.5.0",
38
- "@lucid-evolution/cml": "0.1.0",
39
- "@lucid-evolution/plutus": "0.2.0",
39
+ "@lucid-evolution/plutus": "0.2.1",
40
40
  "@lucid-evolution/uplc": "0.3.0"
41
41
  },
42
42
  "devDependencies": {
43
+ "@cardano-sdk/core": "^0.46.10",
43
44
  "@types/node": "^20.12.8",
44
45
  "tsup": "^8.0.2",
45
46
  "typescript": "^5.4.5",