@lucid-evolution/utils 0.1.72 → 0.1.74

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.
Files changed (3) hide show
  1. package/dist/index.cjs +355 -288
  2. package/dist/index.js +356 -285
  3. package/package.json +4 -4
package/dist/index.cjs CHANGED
@@ -177,6 +177,9 @@ var scriptFromCMLNative = (cmlNative) => {
177
177
  };
178
178
  };
179
179
 
180
+ // src/address.ts
181
+ var import_core_utils3 = require("@lucid-evolution/core-utils");
182
+
180
183
  // src/network.ts
181
184
  function networkToId(network) {
182
185
  switch (network) {
@@ -285,103 +288,131 @@ var import_cbor_x2 = require("cbor-x");
285
288
  var import_plutus_data = require("@harmoniclabs/plutus-data");
286
289
  function validatorToAddress(network, validator, stakeCredential) {
287
290
  const validatorHash = validatorToScriptHash(validator);
288
- if (stakeCredential) {
289
- return CML.BaseAddress.new(
290
- networkToId(network),
291
- CML.Credential.new_script(CML.ScriptHash.from_hex(validatorHash)),
292
- stakeCredential.type === "Key" ? CML.Credential.new_pub_key(
293
- CML.Ed25519KeyHash.from_hex(stakeCredential.hash)
294
- ) : CML.Credential.new_script(
295
- CML.ScriptHash.from_hex(stakeCredential.hash)
296
- )
297
- ).to_address().to_bech32(void 0);
298
- } else {
299
- return CML.EnterpriseAddress.new(
300
- networkToId(network),
301
- CML.Credential.new_script(CML.ScriptHash.from_hex(validatorHash))
302
- ).to_address().to_bech32(void 0);
303
- }
304
- }
305
- function validatorToScriptHash(validator) {
306
- switch (validator.type) {
307
- case "Native":
308
- return CML.NativeScript.from_cbor_hex(validator.script).hash().to_hex();
309
- case "PlutusV1":
310
- return CML.PlutusScript.from_v1(
311
- CML.PlutusV1Script.from_cbor_hex(
312
- applyDoubleCborEncoding(validator.script)
313
- )
314
- ).hash().to_hex();
315
- case "PlutusV2":
316
- return CML.PlutusScript.from_v2(
317
- CML.PlutusV2Script.from_cbor_hex(
318
- applyDoubleCborEncoding(validator.script)
319
- )
320
- ).hash().to_hex();
321
- case "PlutusV3":
322
- return CML.PlutusScript.from_v3(
323
- CML.PlutusV3Script.from_cbor_hex(
324
- applyDoubleCborEncoding(validator.script)
291
+ return (0, import_core_utils2.withCMLScope)((own) => {
292
+ const paymentCredential = own(
293
+ CML.Credential.new_script(own(CML.ScriptHash.from_hex(validatorHash)))
294
+ );
295
+ if (stakeCredential) {
296
+ const stake = stakeCredential.type === "Key" ? own(
297
+ CML.Credential.new_pub_key(
298
+ own(CML.Ed25519KeyHash.from_hex(stakeCredential.hash))
325
299
  )
326
- ).hash().to_hex();
327
- default:
328
- throw new Error("No variant matched");
329
- }
330
- }
331
- function toScriptRef(script) {
332
- switch (script.type) {
333
- case "Native":
334
- return CML.Script.new_native(
335
- CML.NativeScript.from_cbor_hex(script.script)
336
- );
337
- case "PlutusV1":
338
- return CML.Script.new_plutus_v1(
339
- CML.PlutusV1Script.from_cbor_hex(
340
- applyDoubleCborEncoding(script.script)
300
+ ) : own(
301
+ CML.Credential.new_script(
302
+ own(CML.ScriptHash.from_hex(stakeCredential.hash))
341
303
  )
342
304
  );
343
- case "PlutusV2":
344
- return CML.Script.new_plutus_v2(
345
- CML.PlutusV2Script.from_cbor_hex(
346
- applyDoubleCborEncoding(script.script)
347
- )
305
+ const address2 = own(
306
+ CML.BaseAddress.new(networkToId(network), paymentCredential, stake)
348
307
  );
349
- case "PlutusV3":
350
- return CML.Script.new_plutus_v3(
351
- CML.PlutusV3Script.from_cbor_hex(
352
- applyDoubleCborEncoding(script.script)
353
- )
354
- );
355
- default:
356
- throw new Error("No variant matched.");
357
- }
308
+ return own(address2.to_address()).to_bech32(void 0);
309
+ }
310
+ const address = own(
311
+ CML.EnterpriseAddress.new(networkToId(network), paymentCredential)
312
+ );
313
+ return own(address.to_address()).to_bech32(void 0);
314
+ });
315
+ }
316
+ function validatorToScriptHash(validator) {
317
+ return (0, import_core_utils2.withCMLScope)((own) => {
318
+ switch (validator.type) {
319
+ case "Native":
320
+ return own(
321
+ own(CML.NativeScript.from_cbor_hex(validator.script)).hash()
322
+ ).to_hex();
323
+ case "PlutusV1": {
324
+ const script = own(
325
+ CML.PlutusV1Script.from_cbor_hex(
326
+ applyDoubleCborEncoding(validator.script)
327
+ )
328
+ );
329
+ return own(own(CML.PlutusScript.from_v1(script)).hash()).to_hex();
330
+ }
331
+ case "PlutusV2": {
332
+ const script = own(
333
+ CML.PlutusV2Script.from_cbor_hex(
334
+ applyDoubleCborEncoding(validator.script)
335
+ )
336
+ );
337
+ return own(own(CML.PlutusScript.from_v2(script)).hash()).to_hex();
338
+ }
339
+ case "PlutusV3": {
340
+ const script = own(
341
+ CML.PlutusV3Script.from_cbor_hex(
342
+ applyDoubleCborEncoding(validator.script)
343
+ )
344
+ );
345
+ return own(own(CML.PlutusScript.from_v3(script)).hash()).to_hex();
346
+ }
347
+ default:
348
+ throw new Error("No variant matched");
349
+ }
350
+ });
351
+ }
352
+ function toScriptRef(script) {
353
+ return (0, import_core_utils2.withCMLScope)((own) => {
354
+ switch (script.type) {
355
+ case "Native":
356
+ return CML.Script.new_native(
357
+ own(CML.NativeScript.from_cbor_hex(script.script))
358
+ );
359
+ case "PlutusV1":
360
+ return CML.Script.new_plutus_v1(
361
+ own(
362
+ CML.PlutusV1Script.from_cbor_hex(
363
+ applyDoubleCborEncoding(script.script)
364
+ )
365
+ )
366
+ );
367
+ case "PlutusV2":
368
+ return CML.Script.new_plutus_v2(
369
+ own(
370
+ CML.PlutusV2Script.from_cbor_hex(
371
+ applyDoubleCborEncoding(script.script)
372
+ )
373
+ )
374
+ );
375
+ case "PlutusV3":
376
+ return CML.Script.new_plutus_v3(
377
+ own(
378
+ CML.PlutusV3Script.from_cbor_hex(
379
+ applyDoubleCborEncoding(script.script)
380
+ )
381
+ )
382
+ );
383
+ default:
384
+ throw new Error("No variant matched.");
385
+ }
386
+ });
358
387
  }
359
388
  function fromScriptRef(scriptRef) {
360
- const kind = scriptRef.kind();
361
- switch (kind) {
362
- case 0:
363
- return {
364
- type: "Native",
365
- script: scriptRef.as_native().to_cbor_hex()
366
- };
367
- case 1:
368
- return {
369
- type: "PlutusV1",
370
- script: scriptRef.as_plutus_v1().to_cbor_hex()
371
- };
372
- case 2:
373
- return {
374
- type: "PlutusV2",
375
- script: scriptRef.as_plutus_v2().to_cbor_hex()
376
- };
377
- case 3:
378
- return {
379
- type: "PlutusV3",
380
- script: scriptRef.as_plutus_v3().to_cbor_hex()
381
- };
382
- default:
383
- throw new Error("No variant matched.");
384
- }
389
+ return (0, import_core_utils2.withCMLScope)((own) => {
390
+ const kind = scriptRef.kind();
391
+ switch (kind) {
392
+ case 0:
393
+ return {
394
+ type: "Native",
395
+ script: own(scriptRef.as_native()).to_cbor_hex()
396
+ };
397
+ case 1:
398
+ return {
399
+ type: "PlutusV1",
400
+ script: own(scriptRef.as_plutus_v1()).to_cbor_hex()
401
+ };
402
+ case 2:
403
+ return {
404
+ type: "PlutusV2",
405
+ script: own(scriptRef.as_plutus_v2()).to_cbor_hex()
406
+ };
407
+ case 3:
408
+ return {
409
+ type: "PlutusV3",
410
+ script: own(scriptRef.as_plutus_v3()).to_cbor_hex()
411
+ };
412
+ default:
413
+ throw new Error("No variant matched.");
414
+ }
415
+ });
385
416
  }
386
417
  function mintingPolicyToId(mintingPolicy) {
387
418
  return validatorToScriptHash(mintingPolicy);
@@ -417,139 +448,151 @@ function addressFromHexOrBech32(address) {
417
448
  }
418
449
  }
419
450
  function credentialToRewardAddress(network, stakeCredential) {
420
- return CML.RewardAddress.new(
421
- networkToId(network),
422
- stakeCredential.type === "Key" ? CML.Credential.new_pub_key(
423
- CML.Ed25519KeyHash.from_hex(stakeCredential.hash)
424
- ) : CML.Credential.new_script(
425
- CML.ScriptHash.from_hex(stakeCredential.hash)
426
- )
427
- ).to_address().to_bech32(void 0);
451
+ return (0, import_core_utils3.withCMLScope)((own) => {
452
+ const credential = stakeCredential.type === "Key" ? own(
453
+ CML.Credential.new_pub_key(
454
+ own(CML.Ed25519KeyHash.from_hex(stakeCredential.hash))
455
+ )
456
+ ) : own(
457
+ CML.Credential.new_script(
458
+ own(CML.ScriptHash.from_hex(stakeCredential.hash))
459
+ )
460
+ );
461
+ const rewardAddress = own(
462
+ CML.RewardAddress.new(networkToId(network), credential)
463
+ );
464
+ return own(rewardAddress.to_address()).to_bech32(void 0);
465
+ });
428
466
  }
429
467
  function validatorToRewardAddress(network, validator) {
430
468
  const validatorHash = validatorToScriptHash(validator);
431
- return CML.RewardAddress.new(
432
- networkToId(network),
433
- CML.Credential.new_script(CML.ScriptHash.from_hex(validatorHash))
434
- ).to_address().to_bech32(void 0);
469
+ return (0, import_core_utils3.withCMLScope)((own) => {
470
+ const credential = own(
471
+ CML.Credential.new_script(own(CML.ScriptHash.from_hex(validatorHash)))
472
+ );
473
+ const rewardAddress = own(
474
+ CML.RewardAddress.new(networkToId(network), credential)
475
+ );
476
+ return own(rewardAddress.to_address()).to_bech32(void 0);
477
+ });
435
478
  }
479
+ var credentialDetails = (credential, own) => credential.kind() === 0 ? { type: "Key", hash: own(credential.as_pub_key()).to_hex() } : { type: "Script", hash: own(credential.as_script()).to_hex() };
480
+ var addressDetails = (address) => ({
481
+ networkId: address.network_id(),
482
+ bech32: address.to_bech32(void 0),
483
+ hex: address.to_hex()
484
+ });
436
485
  function getAddressDetails(address) {
437
486
  try {
438
- const parsedAddress = CML.BaseAddress.from_address(
439
- addressFromHexOrBech32(address)
440
- );
441
- const paymentCredential = parsedAddress.payment().kind() === 0 ? {
442
- type: "Key",
443
- hash: parsedAddress.payment().as_pub_key().to_hex()
444
- } : {
445
- type: "Script",
446
- hash: parsedAddress.payment().as_script().to_hex()
447
- };
448
- const stakeCredential = parsedAddress.stake().kind() === 0 ? {
449
- type: "Key",
450
- hash: parsedAddress.stake().as_pub_key().to_hex()
451
- } : {
452
- type: "Script",
453
- hash: parsedAddress.stake().as_script().to_hex()
454
- };
455
- return {
456
- type: "Base",
457
- networkId: parsedAddress.to_address().network_id(),
458
- address: {
459
- bech32: parsedAddress.to_address().to_bech32(void 0),
460
- hex: parsedAddress.to_address().to_hex()
461
- },
462
- paymentCredential,
463
- stakeCredential
464
- };
487
+ return (0, import_core_utils3.withCMLScope)((own) => {
488
+ const parsedAddress = own(
489
+ CML.BaseAddress.from_address(own(addressFromHexOrBech32(address)))
490
+ );
491
+ const paymentCredential = credentialDetails(
492
+ own(parsedAddress.payment()),
493
+ own
494
+ );
495
+ const stakeCredential = credentialDetails(
496
+ own(parsedAddress.stake()),
497
+ own
498
+ );
499
+ const { networkId, ...details } = addressDetails(
500
+ own(parsedAddress.to_address())
501
+ );
502
+ return {
503
+ type: "Base",
504
+ networkId,
505
+ address: details,
506
+ paymentCredential,
507
+ stakeCredential
508
+ };
509
+ });
465
510
  } catch (_e) {
466
511
  }
467
512
  try {
468
- const parsedAddress = CML.EnterpriseAddress.from_address(
469
- addressFromHexOrBech32(address)
470
- );
471
- const paymentCredential = parsedAddress.payment().kind() === 0 ? {
472
- type: "Key",
473
- hash: parsedAddress.payment().as_pub_key().to_hex()
474
- } : {
475
- type: "Script",
476
- hash: parsedAddress.payment().as_script().to_hex()
477
- };
478
- return {
479
- type: "Enterprise",
480
- networkId: parsedAddress.to_address().network_id(),
481
- address: {
482
- bech32: parsedAddress.to_address().to_bech32(void 0),
483
- hex: parsedAddress.to_address().to_hex()
484
- },
485
- paymentCredential
486
- };
513
+ return (0, import_core_utils3.withCMLScope)((own) => {
514
+ const parsedAddress = own(
515
+ CML.EnterpriseAddress.from_address(
516
+ own(addressFromHexOrBech32(address))
517
+ )
518
+ );
519
+ const paymentCredential = credentialDetails(
520
+ own(parsedAddress.payment()),
521
+ own
522
+ );
523
+ const { networkId, ...details } = addressDetails(
524
+ own(parsedAddress.to_address())
525
+ );
526
+ return {
527
+ type: "Enterprise",
528
+ networkId,
529
+ address: details,
530
+ paymentCredential
531
+ };
532
+ });
487
533
  } catch (_e) {
488
534
  }
489
535
  try {
490
- const parsedAddress = CML.PointerAddress.from_address(
491
- addressFromHexOrBech32(address)
492
- );
493
- const paymentCredential = parsedAddress?.payment().kind() === 0 ? {
494
- type: "Key",
495
- hash: parsedAddress.payment().as_pub_key().to_hex()
496
- } : {
497
- type: "Script",
498
- hash: parsedAddress.payment().as_script().to_hex()
499
- };
500
- return {
501
- type: "Pointer",
502
- networkId: parsedAddress.to_address().network_id(),
503
- address: {
504
- bech32: parsedAddress.to_address().to_bech32(void 0),
505
- hex: parsedAddress.to_address().to_hex()
506
- },
507
- paymentCredential
508
- };
536
+ return (0, import_core_utils3.withCMLScope)((own) => {
537
+ const parsedAddress = own(
538
+ CML.PointerAddress.from_address(own(addressFromHexOrBech32(address)))
539
+ );
540
+ const paymentCredential = credentialDetails(
541
+ own(parsedAddress.payment()),
542
+ own
543
+ );
544
+ const { networkId, ...details } = addressDetails(
545
+ own(parsedAddress.to_address())
546
+ );
547
+ return {
548
+ type: "Pointer",
549
+ networkId,
550
+ address: details,
551
+ paymentCredential
552
+ };
553
+ });
509
554
  } catch (_e) {
510
555
  }
511
556
  try {
512
- const parsedAddress = CML.RewardAddress.from_address(
513
- addressFromHexOrBech32(address)
514
- );
515
- const stakeCredential = parsedAddress.payment().kind() === 0 ? {
516
- type: "Key",
517
- hash: parsedAddress.payment().as_pub_key().to_hex()
518
- } : {
519
- type: "Script",
520
- hash: parsedAddress.payment().as_script().to_hex()
521
- };
522
- return {
523
- type: "Reward",
524
- networkId: parsedAddress.to_address().network_id(),
525
- address: {
526
- bech32: parsedAddress.to_address().to_bech32(void 0),
527
- hex: parsedAddress.to_address().to_hex()
528
- },
529
- stakeCredential
530
- };
557
+ return (0, import_core_utils3.withCMLScope)((own) => {
558
+ const parsedAddress = own(
559
+ CML.RewardAddress.from_address(own(addressFromHexOrBech32(address)))
560
+ );
561
+ const stakeCredential = credentialDetails(
562
+ own(parsedAddress.payment()),
563
+ own
564
+ );
565
+ const { networkId, ...details } = addressDetails(
566
+ own(parsedAddress.to_address())
567
+ );
568
+ return { type: "Reward", networkId, address: details, stakeCredential };
569
+ });
531
570
  } catch (_e) {
532
571
  }
533
572
  try {
534
- const parsedAddress = ((address2) => {
535
- try {
536
- return CML.ByronAddress.from_cbor_hex(address2);
537
- } catch (_e) {
538
- try {
539
- return CML.ByronAddress.from_base58(address2);
540
- } catch (_e2) {
541
- throw new Error("Could not deserialize address.");
573
+ return (0, import_core_utils3.withCMLScope)((own) => {
574
+ const parsedAddress = own(
575
+ ((address2) => {
576
+ try {
577
+ return CML.ByronAddress.from_cbor_hex(address2);
578
+ } catch (_e) {
579
+ try {
580
+ return CML.ByronAddress.from_base58(address2);
581
+ } catch (_e2) {
582
+ throw new Error("Could not deserialize address.");
583
+ }
584
+ }
585
+ })(address)
586
+ );
587
+ return {
588
+ type: "Byron",
589
+ networkId: own(parsedAddress.content()).network_id(),
590
+ address: {
591
+ bech32: "",
592
+ hex: own(parsedAddress.to_address()).to_hex()
542
593
  }
543
- }
544
- })(address);
545
- return {
546
- type: "Byron",
547
- networkId: parsedAddress.content().network_id(),
548
- address: {
549
- bech32: "",
550
- hex: parsedAddress.to_address().to_hex()
551
- }
552
- };
594
+ };
595
+ });
553
596
  } catch (_e) {
554
597
  }
555
598
  throw new Error("No address type matched for: " + address);
@@ -1326,7 +1369,7 @@ function toPublicKey(privateKey) {
1326
1369
  }
1327
1370
 
1328
1371
  // src/label.ts
1329
- var import_core_utils3 = require("@lucid-evolution/core-utils");
1372
+ var import_core_utils4 = require("@lucid-evolution/core-utils");
1330
1373
  var import_crc8 = require("@lucid-evolution/crc8");
1331
1374
  function toLabel(num) {
1332
1375
  if (num < 0 || num > 65535) {
@@ -1347,7 +1390,7 @@ function fromLabel(label) {
1347
1390
  return check === checksum(numHex) ? num : null;
1348
1391
  }
1349
1392
  function checksum(num) {
1350
- return (0, import_crc8.crc8)((0, import_core_utils3.fromHex)(num)).toString(16).padStart(2, "0");
1393
+ return (0, import_crc8.crc8)((0, import_core_utils4.fromHex)(num)).toString(16).padStart(2, "0");
1351
1394
  }
1352
1395
 
1353
1396
  // src/time.ts
@@ -1359,50 +1402,60 @@ function slotToUnixTime(network, slot) {
1359
1402
  return (0, import_plutus2.slotToBeginUnixTime)(slot, import_plutus2.SLOT_CONFIG_NETWORK[network]);
1360
1403
  }
1361
1404
 
1405
+ // src/utxo.ts
1406
+ var import_core_utils6 = require("@lucid-evolution/core-utils");
1407
+
1362
1408
  // src/value.ts
1363
- var import_core_utils4 = require("@lucid-evolution/core-utils");
1409
+ var import_core_utils5 = require("@lucid-evolution/core-utils");
1364
1410
  var import_effect = require("effect");
1365
1411
  function valueToAssets(value) {
1366
1412
  const assets = {};
1367
1413
  assets["lovelace"] = value.coin();
1368
1414
  if (value.has_multiassets()) {
1369
- const ma = value.multi_asset();
1370
- const multiAssets = ma.keys();
1371
- for (let j = 0; j < multiAssets.len(); j++) {
1372
- const policy = multiAssets.get(j);
1373
- const policyAssets = ma.get_assets(policy);
1374
- const assetNames = policyAssets.keys();
1375
- for (let k = 0; k < assetNames.len(); k++) {
1376
- const policyAsset = assetNames.get(k);
1377
- const quantity = policyAssets.get(policyAsset);
1378
- const unit = policy.to_hex() + policyAsset.to_js_value();
1379
- assets[unit] = quantity;
1415
+ (0, import_core_utils5.withCMLScope)((own) => {
1416
+ const ma = own(value.multi_asset());
1417
+ const multiAssets = own(ma.keys());
1418
+ for (let j = 0; j < multiAssets.len(); j++) {
1419
+ const policy = own(multiAssets.get(j));
1420
+ const policyAssets = own(ma.get_assets(policy));
1421
+ const assetNames = own(policyAssets.keys());
1422
+ for (let k = 0; k < assetNames.len(); k++) {
1423
+ const policyAsset = own(assetNames.get(k));
1424
+ const quantity = policyAssets.get(policyAsset);
1425
+ const unit = policy.to_hex() + policyAsset.to_js_value();
1426
+ assets[unit] = quantity;
1427
+ }
1380
1428
  }
1381
- }
1429
+ });
1382
1430
  }
1383
1431
  return assets;
1384
1432
  }
1385
1433
  function assetsToValue(assets) {
1386
- const multiAsset = CML.MultiAsset.new();
1387
- const lovelace = assets["lovelace"] ? BigInt(assets["lovelace"]) : 0n;
1388
- const units = Object.keys(assets);
1389
- const policies = Array.from(
1390
- new Set(
1391
- units.filter((unit) => unit !== "lovelace").map((unit) => unit.slice(0, 56))
1392
- )
1393
- );
1394
- for (const policy of policies) {
1395
- const policyUnits = units.filter((unit) => unit.slice(0, 56) === policy);
1396
- const assetsValue = CML.MapAssetNameToCoin.new();
1397
- for (const unit of policyUnits) {
1398
- assetsValue.insert(
1399
- CML.AssetName.from_hex(unit.slice(56)),
1400
- BigInt(assets[unit])
1434
+ return (0, import_core_utils5.withCMLScope)((own) => {
1435
+ const multiAsset = own(CML.MultiAsset.new());
1436
+ const lovelace = assets["lovelace"] ? BigInt(assets["lovelace"]) : 0n;
1437
+ const units = Object.keys(assets);
1438
+ const policies = Array.from(
1439
+ new Set(
1440
+ units.filter((unit) => unit !== "lovelace").map((unit) => unit.slice(0, 56))
1441
+ )
1442
+ );
1443
+ for (const policy of policies) {
1444
+ const policyUnits = units.filter((unit) => unit.slice(0, 56) === policy);
1445
+ const assetsValue = own(CML.MapAssetNameToCoin.new());
1446
+ for (const unit of policyUnits) {
1447
+ assetsValue.insert(
1448
+ own(CML.AssetName.from_hex(unit.slice(56))),
1449
+ BigInt(assets[unit])
1450
+ );
1451
+ }
1452
+ multiAsset.insert_assets(
1453
+ own(CML.ScriptHash.from_hex(policy)),
1454
+ assetsValue
1401
1455
  );
1402
1456
  }
1403
- multiAsset.insert_assets(CML.ScriptHash.from_hex(policy), assetsValue);
1404
- }
1405
- return CML.Value.new(lovelace, multiAsset);
1457
+ return CML.Value.new(lovelace, multiAsset);
1458
+ });
1406
1459
  }
1407
1460
  function fromUnit(unit) {
1408
1461
  const policyId = unit.slice(0, 56);
@@ -1441,10 +1494,10 @@ function addAssets(...assets) {
1441
1494
  }, {});
1442
1495
  }
1443
1496
  async function getUniqueTokenName(utxo) {
1444
- const id = (0, import_core_utils4.fromHex)(utxo.txHash);
1497
+ const id = (0, import_core_utils5.fromHex)(utxo.txHash);
1445
1498
  const data = new Uint8Array([utxo.outputIndex, ...id]);
1446
1499
  const hash = new Uint8Array(await crypto.subtle.digest("SHA3-256", data));
1447
- return (0, import_core_utils4.toHex)(hash);
1500
+ return (0, import_core_utils5.toHex)(hash);
1448
1501
  }
1449
1502
  var sortCanonical = (assets) => (0, import_effect.pipe)(
1450
1503
  Object.entries(assets).sort(([aUnit], [bUnit]) => {
@@ -1465,23 +1518,23 @@ var sortCanonical = (assets) => (0, import_effect.pipe)(
1465
1518
  );
1466
1519
 
1467
1520
  // src/utxo.ts
1468
- var utxoToTransactionOutput = (utxo) => {
1469
- return buildOutput(utxo).with_value(assetsToValue(utxo.assets)).build().output();
1470
- };
1471
- var utxoToTransactionInput = (utxo) => {
1472
- return CML.TransactionInput.new(
1473
- CML.TransactionHash.from_hex(utxo.txHash),
1521
+ var utxoToTransactionOutput = (utxo) => (0, import_core_utils6.withCMLScope)((own) => {
1522
+ const value = own(assetsToValue(utxo.assets));
1523
+ const builder = own(buildOutput(utxo, own).with_value(value));
1524
+ return own(builder.build()).output();
1525
+ });
1526
+ var utxoToTransactionInput = (utxo) => (0, import_core_utils6.withCMLScope)(
1527
+ (own) => CML.TransactionInput.new(
1528
+ own(CML.TransactionHash.from_hex(utxo.txHash)),
1474
1529
  BigInt(utxo.outputIndex)
1475
- );
1476
- };
1477
- var utxoToCore = (utxo) => {
1478
- const out = utxoToTransactionOutput(utxo);
1479
- const utxoCore = CML.TransactionUnspentOutput.new(
1480
- utxoToTransactionInput(utxo),
1481
- out
1482
- );
1483
- return utxoCore;
1484
- };
1530
+ )
1531
+ );
1532
+ var utxoToCore = (utxo) => (0, import_core_utils6.withCMLScope)(
1533
+ (own) => CML.TransactionUnspentOutput.new(
1534
+ own(utxoToTransactionInput(utxo)),
1535
+ own(utxoToTransactionOutput(utxo))
1536
+ )
1537
+ );
1485
1538
  function utxosToCores(utxos) {
1486
1539
  const result = [];
1487
1540
  for (const utxo of utxos) {
@@ -1490,11 +1543,10 @@ function utxosToCores(utxos) {
1490
1543
  return result;
1491
1544
  }
1492
1545
  function coreToUtxo(coreUtxo) {
1493
- const utxo = {
1494
- ...coreToOutRef(coreUtxo.input()),
1495
- ...coreToTxOutput(coreUtxo.output())
1496
- };
1497
- return utxo;
1546
+ return (0, import_core_utils6.withCMLScope)((own) => ({
1547
+ ...coreToOutRef(own(coreUtxo.input())),
1548
+ ...coreToTxOutput(own(coreUtxo.output()))
1549
+ }));
1498
1550
  }
1499
1551
  function coresToUtxos(utxos) {
1500
1552
  const result = [];
@@ -1504,10 +1556,10 @@ function coresToUtxos(utxos) {
1504
1556
  return result;
1505
1557
  }
1506
1558
  function coreToOutRef(input) {
1507
- return {
1508
- txHash: input.transaction_id().to_hex(),
1559
+ return (0, import_core_utils6.withCMLScope)((own) => ({
1560
+ txHash: own(input.transaction_id()).to_hex(),
1509
1561
  outputIndex: parseInt(input.index().toString())
1510
- };
1562
+ }));
1511
1563
  }
1512
1564
  function coresToOutRefs(inputs) {
1513
1565
  const result = [];
@@ -1517,13 +1569,17 @@ function coresToOutRefs(inputs) {
1517
1569
  return result;
1518
1570
  }
1519
1571
  function coreToTxOutput(output) {
1520
- return {
1521
- assets: valueToAssets(output.amount()),
1522
- address: output.address().to_bech32(void 0),
1523
- datumHash: output.datum()?.as_hash()?.to_hex(),
1524
- datum: output.datum()?.as_datum()?.to_cbor_hex(),
1525
- scriptRef: output.script_ref() && fromScriptRef(output.script_ref())
1526
- };
1572
+ return (0, import_core_utils6.withCMLScope)((own) => {
1573
+ const datum = own(output.datum());
1574
+ const scriptRef = own(output.script_ref());
1575
+ return {
1576
+ assets: valueToAssets(own(output.amount())),
1577
+ address: own(output.address()).to_bech32(void 0),
1578
+ datumHash: own(datum?.as_hash())?.to_hex(),
1579
+ datum: own(datum?.as_datum())?.to_cbor_hex(),
1580
+ scriptRef: scriptRef && fromScriptRef(scriptRef)
1581
+ };
1582
+ });
1527
1583
  }
1528
1584
  function coresToTxOutputs(outputs) {
1529
1585
  let result = [];
@@ -1608,25 +1664,36 @@ function getInputIndices(indexInputs, allInputs, sorted = false) {
1608
1664
  else return [];
1609
1665
  });
1610
1666
  }
1611
- var calculateMinLovelaceFromUTxO = (coinsPerUtxoByte, utxo) => buildOutput(utxo).with_asset_and_min_required_coin(
1612
- assetsToValue(utxo.assets).multi_asset(),
1613
- coinsPerUtxoByte
1614
- ).build().output().amount().coin();
1615
- var buildOutput = (utxo) => {
1616
- const builder = CML.TransactionOutputBuilder.new().with_address(
1617
- CML.Address.from_bech32(utxo.address)
1667
+ var calculateMinLovelaceFromUTxO = (coinsPerUtxoByte, utxo) => (0, import_core_utils6.withCMLScope)((own) => {
1668
+ const value = own(assetsToValue(utxo.assets));
1669
+ const builder = own(
1670
+ buildOutput(utxo, own).with_asset_and_min_required_coin(
1671
+ own(value.multi_asset()),
1672
+ coinsPerUtxoByte
1673
+ )
1674
+ );
1675
+ const output = own(own(builder.build()).output());
1676
+ return own(output.amount()).coin();
1677
+ });
1678
+ var buildOutput = (utxo, own) => {
1679
+ const address = own(CML.Address.from_bech32(utxo.address));
1680
+ const addressed = own(
1681
+ own(CML.TransactionOutputBuilder.new()).with_address(address)
1618
1682
  );
1619
- return utxo.scriptRef ? buildDatum(utxo, builder).with_reference_script(toScriptRef(utxo.scriptRef)).next() : buildDatum(utxo, builder).next();
1683
+ const builder = buildDatum(utxo, addressed, own);
1684
+ if (!utxo.scriptRef) return own(builder.next());
1685
+ const scriptRef = own(toScriptRef(utxo.scriptRef));
1686
+ return own(own(builder.with_reference_script(scriptRef)).next());
1620
1687
  };
1621
- var buildDatum = (utxo, builder) => {
1622
- if (utxo.datumHash && utxo.datum)
1623
- return builder.with_communication_data(
1624
- CML.PlutusData.from_cbor_hex(utxo.datum)
1625
- );
1626
- if (utxo.datum)
1627
- return builder.with_data(
1628
- CML.DatumOption.new_datum(CML.PlutusData.from_cbor_hex(utxo.datum))
1629
- );
1688
+ var buildDatum = (utxo, builder, own) => {
1689
+ if (utxo.datumHash && utxo.datum) {
1690
+ const datum = own(CML.PlutusData.from_cbor_hex(utxo.datum));
1691
+ return own(builder.with_communication_data(datum));
1692
+ }
1693
+ if (utxo.datum) {
1694
+ const datum = own(CML.PlutusData.from_cbor_hex(utxo.datum));
1695
+ return own(builder.with_data(own(CML.DatumOption.new_datum(datum))));
1696
+ }
1630
1697
  return builder;
1631
1698
  };
1632
1699