@meshsdk/transaction 1.9.0-beta.54 → 1.9.0-beta.56

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
@@ -33,8 +33,14 @@ __export(index_exports, {
33
33
  ForgeScript: () => ForgeScript,
34
34
  MeshTxBuilder: () => MeshTxBuilder,
35
35
  Transaction: () => Transaction,
36
+ TxParser: () => TxParser,
37
+ cloneOutput: () => cloneOutput,
38
+ getLovelace: () => getLovelace,
39
+ getOutputMinLovelace: () => getOutputMinLovelace,
40
+ getUtxoMinLovelace: () => getUtxoMinLovelace,
36
41
  mergeContents: () => mergeContents,
37
42
  metadataObjToMap: () => metadataObjToMap,
43
+ setLoveLace: () => setLoveLace,
38
44
  utxoToTxIn: () => utxoToTxIn
39
45
  });
40
46
  module.exports = __toCommonJS(index_exports);
@@ -1385,6 +1391,7 @@ var bignumber_default = BigNumber;
1385
1391
 
1386
1392
  // src/mesh-tx-builder/index.ts
1387
1393
  var import_json_bigint3 = __toESM(require("json-bigint"), 1);
1394
+ var import_common3 = require("@meshsdk/common");
1388
1395
  var import_core_cst3 = require("@meshsdk/core-cst");
1389
1396
 
1390
1397
  // src/mesh-tx-builder/coin-selection/cardano-sdk-adapter.ts
@@ -3584,6 +3591,7 @@ var MeshTxBuilderCore = class {
3584
3591
  };
3585
3592
 
3586
3593
  // src/mesh-tx-builder/utils.ts
3594
+ var import_common2 = require("@meshsdk/common");
3587
3595
  var utxoToTxIn = (utxo) => {
3588
3596
  return [
3589
3597
  utxo.input.txHash,
@@ -3592,6 +3600,36 @@ var utxoToTxIn = (utxo) => {
3592
3600
  utxo.output.address
3593
3601
  ];
3594
3602
  };
3603
+ var getUtxoMinLovelace = (utxo, coinsPerUtxoSize = import_common2.DEFAULT_PROTOCOL_PARAMETERS.coinsPerUtxoSize) => {
3604
+ const referenceScript = utxo.scriptRef ? { code: utxo.scriptRef, version: "V3" } : void 0;
3605
+ let datum;
3606
+ if (utxo.plutusData) {
3607
+ datum = {
3608
+ type: "Inline",
3609
+ data: {
3610
+ content: utxo.plutusData,
3611
+ type: "CBOR"
3612
+ }
3613
+ };
3614
+ } else if (utxo.dataHash) {
3615
+ datum = {
3616
+ type: "Hash",
3617
+ data: {
3618
+ content: utxo.dataHash,
3619
+ // usually this should be entire datum cbor, but irrelevant in min utxo calculation
3620
+ type: "CBOR"
3621
+ }
3622
+ };
3623
+ }
3624
+ const output = {
3625
+ address: utxo.address,
3626
+ amount: utxo.amount,
3627
+ referenceScript,
3628
+ datum
3629
+ };
3630
+ const minLovelace = getOutputMinLovelace(output, coinsPerUtxoSize);
3631
+ return minLovelace;
3632
+ };
3595
3633
 
3596
3634
  // src/mesh-tx-builder/index.ts
3597
3635
  var MeshTxBuilder = class _MeshTxBuilder extends MeshTxBuilderCore {
@@ -4353,6 +4391,7 @@ var MeshTxBuilder = class _MeshTxBuilder extends MeshTxBuilderCore {
4353
4391
  };
4354
4392
  collectAllRequiredSignatures = () => {
4355
4393
  const { paymentCreds, byronAddresses } = this.getInputsRequiredSignatures();
4394
+ const { collateralPaymentCreds, collateralByronAddresses } = this.getCollateralRequiredSignatures();
4356
4395
  const withdrawalCreds = this.getWithdrawalRequiredSignatures();
4357
4396
  const certCreds = this.getCertificatesRequiredSignatures();
4358
4397
  const voteCreds = this.getVoteRequiredSignatures();
@@ -4361,12 +4400,17 @@ var MeshTxBuilder = class _MeshTxBuilder extends MeshTxBuilderCore {
4361
4400
  const allCreds = /* @__PURE__ */ new Set([
4362
4401
  ...paymentCreds,
4363
4402
  ...withdrawalCreds,
4403
+ ...collateralPaymentCreds,
4364
4404
  ...certCreds,
4365
4405
  ...voteCreds,
4366
4406
  ...requiredSignatures,
4367
4407
  ...mintCreds
4368
4408
  ]);
4369
- return { keyHashes: allCreds, byronAddresses };
4409
+ const allByronAddresses = /* @__PURE__ */ new Set([
4410
+ ...byronAddresses,
4411
+ ...collateralByronAddresses
4412
+ ]);
4413
+ return { keyHashes: allCreds, byronAddresses: allByronAddresses };
4370
4414
  };
4371
4415
  getInputsRequiredSignatures() {
4372
4416
  const byronAddresses = /* @__PURE__ */ new Set();
@@ -4399,6 +4443,29 @@ var MeshTxBuilder = class _MeshTxBuilder extends MeshTxBuilderCore {
4399
4443
  }
4400
4444
  return { paymentCreds, byronAddresses };
4401
4445
  }
4446
+ getCollateralRequiredSignatures() {
4447
+ const collateralByronAddresses = /* @__PURE__ */ new Set();
4448
+ const collateralPaymentCreds = /* @__PURE__ */ new Set();
4449
+ for (let collateral of this.meshTxBuilderBody.collaterals) {
4450
+ if (collateral.type === "PubKey") {
4451
+ if (collateral.txIn.address) {
4452
+ const address = import_core_cst3.Address.fromString(collateral.txIn.address);
4453
+ if (!address) {
4454
+ continue;
4455
+ }
4456
+ const addressDetails = address.getProps();
4457
+ const paymentCred = addressDetails.paymentPart;
4458
+ if (paymentCred?.type === import_core_cst3.CredentialType.KeyHash) {
4459
+ collateralPaymentCreds.add(paymentCred.hash);
4460
+ }
4461
+ if (addressDetails.type === import_core_cst3.AddressType.Byron) {
4462
+ collateralByronAddresses.add(collateral.txIn.address);
4463
+ }
4464
+ }
4465
+ }
4466
+ }
4467
+ return { collateralPaymentCreds, collateralByronAddresses };
4468
+ }
4402
4469
  getWithdrawalRequiredSignatures() {
4403
4470
  const withdrawalCreds = /* @__PURE__ */ new Set();
4404
4471
  for (let withdrawal of this.meshTxBuilderBody.withdrawals) {
@@ -4980,24 +5047,7 @@ var MeshTxBuilder = class _MeshTxBuilder extends MeshTxBuilderCore {
4980
5047
  );
4981
5048
  };
4982
5049
  calculateMinLovelaceForOutput = (output) => {
4983
- let currentOutput = cloneOutput(output);
4984
- let lovelace = getLovelace(currentOutput);
4985
- let minAda = 0n;
4986
- for (let i = 0; i < 3; i++) {
4987
- const txOutSize = BigInt(
4988
- this.serializer.serializeOutput(currentOutput).length / 2
4989
- );
4990
- const txOutByteCost = BigInt(this._protocolParams.coinsPerUtxoSize);
4991
- const totalOutCost = (160n + BigInt(txOutSize)) * txOutByteCost;
4992
- minAda = totalOutCost;
4993
- if (lovelace < totalOutCost) {
4994
- lovelace = totalOutCost;
4995
- } else {
4996
- break;
4997
- }
4998
- currentOutput = setLoveLace(currentOutput, lovelace);
4999
- }
5000
- return minAda;
5050
+ return getOutputMinLovelace(output, this._protocolParams.coinsPerUtxoSize);
5001
5051
  };
5002
5052
  clone() {
5003
5053
  const newBuilder = super._cloneCore(() => {
@@ -5076,6 +5126,27 @@ var getLovelace = (output) => {
5076
5126
  }
5077
5127
  return 0n;
5078
5128
  };
5129
+ var getOutputMinLovelace = (output, coinsPerUtxoSize = import_common3.DEFAULT_PROTOCOL_PARAMETERS.coinsPerUtxoSize) => {
5130
+ const serializer = new import_core_cst3.CardanoSDKSerializer();
5131
+ let currentOutput = cloneOutput(output);
5132
+ let lovelace = getLovelace(currentOutput);
5133
+ let minLovelace = 0n;
5134
+ for (let i = 0; i < 3; i++) {
5135
+ const txOutSize = BigInt(
5136
+ serializer.serializeOutput(currentOutput).length / 2
5137
+ );
5138
+ const txOutByteCost = BigInt(coinsPerUtxoSize);
5139
+ const totalOutCost = (160n + BigInt(txOutSize)) * txOutByteCost;
5140
+ minLovelace = totalOutCost;
5141
+ if (lovelace < totalOutCost) {
5142
+ lovelace = totalOutCost;
5143
+ } else {
5144
+ break;
5145
+ }
5146
+ currentOutput = setLoveLace(currentOutput, lovelace);
5147
+ }
5148
+ return minLovelace;
5149
+ };
5079
5150
 
5080
5151
  // src/scripts/forge.script.ts
5081
5152
  var import_core_cst4 = require("@meshsdk/core-cst");
@@ -5125,7 +5196,7 @@ var ForgeScript = class {
5125
5196
  };
5126
5197
 
5127
5198
  // src/transaction/index.ts
5128
- var import_common2 = require("@meshsdk/common");
5199
+ var import_common4 = require("@meshsdk/common");
5129
5200
  var import_core_cst5 = require("@meshsdk/core-cst");
5130
5201
  var Transaction = class {
5131
5202
  txBuilder;
@@ -5254,7 +5325,7 @@ var Transaction = class {
5254
5325
  * @see {@link https://meshjs.dev/apis/transaction#sendToken}
5255
5326
  */
5256
5327
  sendToken(recipient, ticker, amount) {
5257
- const assets = [{ unit: import_common2.SUPPORTED_TOKENS[ticker], quantity: amount }];
5328
+ const assets = [{ unit: import_common4.SUPPORTED_TOKENS[ticker], quantity: amount }];
5258
5329
  return this.sendAssets(recipient, assets);
5259
5330
  }
5260
5331
  /**
@@ -5340,7 +5411,7 @@ var Transaction = class {
5340
5411
  const { value, script, datum, redeemer } = options;
5341
5412
  const red = redeemer || {
5342
5413
  data: { alternative: 0, fields: ["mesh"] },
5343
- budget: import_common2.DEFAULT_REDEEMER_BUDGET
5414
+ budget: import_common4.DEFAULT_REDEEMER_BUDGET
5344
5415
  };
5345
5416
  if ("code" in script) {
5346
5417
  this.isCollateralNeeded = true;
@@ -5386,10 +5457,10 @@ var Transaction = class {
5386
5457
  */
5387
5458
  mintAsset(forgeScript, mint, redeemer) {
5388
5459
  const assetQuantity = mint.assetQuantity;
5389
- let assetNameHex = (0, import_common2.stringToHex)(mint.assetName);
5390
- const referenceAssetNameHex = (0, import_common2.CIP68_100)(assetNameHex);
5460
+ let assetNameHex = (0, import_common4.stringToHex)(mint.assetName);
5461
+ const referenceAssetNameHex = (0, import_common4.CIP68_100)(assetNameHex);
5391
5462
  if (mint.cip68ScriptAddress) {
5392
- assetNameHex = (0, import_common2.CIP68_222)(assetNameHex);
5463
+ assetNameHex = (0, import_common4.CIP68_222)(assetNameHex);
5393
5464
  }
5394
5465
  let policyId = "";
5395
5466
  switch (typeof forgeScript) {
@@ -5461,7 +5532,7 @@ var Transaction = class {
5461
5532
  this.sendAssets(
5462
5533
  {
5463
5534
  address: mint.cip68ScriptAddress,
5464
- datum: { inline: true, value: (0, import_common2.metadataToCip68)(mint.metadata) }
5535
+ datum: { inline: true, value: (0, import_common4.metadataToCip68)(mint.metadata) }
5465
5536
  },
5466
5537
  [
5467
5538
  {
@@ -5502,7 +5573,7 @@ var Transaction = class {
5502
5573
  burnAsset(forgeScript, asset, redeemer) {
5503
5574
  const assetQuantity = "-" + asset.quantity;
5504
5575
  const mint = {
5505
- assetName: (0, import_common2.hexToString)(asset.unit.slice(import_common2.POLICY_ID_LENGTH)),
5576
+ assetName: (0, import_common4.hexToString)(asset.unit.slice(import_common4.POLICY_ID_LENGTH)),
5506
5577
  assetQuantity
5507
5578
  };
5508
5579
  try {
@@ -5776,12 +5847,67 @@ function mask(metadatum) {
5776
5847
  throw new Error(`Unsupported metadatum kind: ${metadatum.getKind()}`);
5777
5848
  }
5778
5849
  }
5850
+
5851
+ // src/tx-parser/index.ts
5852
+ var TxParser = class {
5853
+ constructor(serializer, fetcher) {
5854
+ this.serializer = serializer;
5855
+ this.fetcher = fetcher;
5856
+ }
5857
+ parse = async (txHex, providedUtxos = []) => {
5858
+ const resolvedUtxos = [...providedUtxos];
5859
+ const resolvedUtxosSet = new Set(
5860
+ providedUtxos.map(
5861
+ (utxo) => `${utxo.input.txHash}#${utxo.input.outputIndex}`
5862
+ )
5863
+ );
5864
+ const toResolveUtxos = {};
5865
+ const fetchResult = {};
5866
+ this.serializer.parser.getRequiredInputs(txHex).forEach((input) => {
5867
+ if (!resolvedUtxosSet.has(`${input.txHash}#${input.outputIndex}`)) {
5868
+ if (!toResolveUtxos[input.txHash]) {
5869
+ toResolveUtxos[input.txHash] = [];
5870
+ }
5871
+ toResolveUtxos[input.txHash].push(input.outputIndex);
5872
+ }
5873
+ });
5874
+ for (const txHash in toResolveUtxos) {
5875
+ const outputIndices = toResolveUtxos[txHash];
5876
+ if (!this.fetcher) {
5877
+ throw new Error(
5878
+ "Fetcher is not provided. Cannot resolve UTxOs without fetcher."
5879
+ );
5880
+ }
5881
+ const utxos = await this.fetcher.fetchUTxOs(txHash);
5882
+ outputIndices?.forEach((outputIndex) => {
5883
+ const utxoData = utxos.find(
5884
+ (utxo) => utxo.input.outputIndex === outputIndex
5885
+ );
5886
+ if (!utxoData) {
5887
+ throw new Error(`UTxO not found: ${txHash}:${outputIndex}`);
5888
+ }
5889
+ resolvedUtxos.push(utxoData);
5890
+ });
5891
+ }
5892
+ this.serializer.parser.parse(txHex, resolvedUtxos);
5893
+ return this.serializer.parser.getBuilderBody();
5894
+ };
5895
+ getBuilderBody = () => this.serializer.parser.getBuilderBody();
5896
+ getBuilderBodyWithoutChange = () => this.serializer.parser.getBuilderBodyWithoutChange();
5897
+ toTester = () => this.serializer.parser.toTester();
5898
+ };
5779
5899
  // Annotate the CommonJS export names for ESM import in node:
5780
5900
  0 && (module.exports = {
5781
5901
  ForgeScript,
5782
5902
  MeshTxBuilder,
5783
5903
  Transaction,
5904
+ TxParser,
5905
+ cloneOutput,
5906
+ getLovelace,
5907
+ getOutputMinLovelace,
5908
+ getUtxoMinLovelace,
5784
5909
  mergeContents,
5785
5910
  metadataObjToMap,
5911
+ setLoveLace,
5786
5912
  utxoToTxIn
5787
5913
  });
package/dist/index.d.cts CHANGED
@@ -1,3 +1,4 @@
1
+ import * as _meshsdk_common from '@meshsdk/common';
1
2
  import { UTxO, TxOutput, Action, Protocol, MintItem, TxIn, Withdrawal, Vote, PubKeyTxIn, RefTxIn, MeshTxBuilderBody, Asset, BuilderData, LanguageVersion, Voter, VotingProcedure, PoolParams, Anchor, DRep, Metadatum, Network, Redeemer, IFetcher, ISubmitter, IEvaluator, IMeshTxSerializer, ScriptSource, SimpleScriptSourceInfo, Certificate, MintParam, Output, NativeScript as NativeScript$1, IInitiator, Recipient, Token, PlutusScript, Budget, Data, Mint } from '@meshsdk/common';
2
3
  import { CredentialCore, NativeScript } from '@meshsdk/core-cst';
3
4
 
@@ -541,6 +542,13 @@ declare class MeshTxBuilderCore {
541
542
  * @returns [txHash, outputIndex, amount, address]
542
543
  */
543
544
  declare const utxoToTxIn: (utxo: UTxO) => [string, number, Asset[], string];
545
+ /**
546
+ * Calculate minimum lovelace required for a UTxO output
547
+ * @param utxo Output of utxo
548
+ * @param coinsPerUtxoSize From protocol parameters
549
+ * @returns Minimum lovelace required for the UTxO
550
+ */
551
+ declare const getUtxoMinLovelace: (utxo: TxOutput, coinsPerUtxoSize?: number) => bigint;
544
552
 
545
553
  interface MeshTxBuilderOptions {
546
554
  fetcher?: IFetcher;
@@ -632,6 +640,10 @@ declare class MeshTxBuilder extends MeshTxBuilderCore {
632
640
  paymentCreds: Set<string>;
633
641
  byronAddresses: Set<string>;
634
642
  };
643
+ protected getCollateralRequiredSignatures(): {
644
+ collateralPaymentCreds: Set<string>;
645
+ collateralByronAddresses: Set<string>;
646
+ };
635
647
  protected getWithdrawalRequiredSignatures(): Set<string>;
636
648
  protected getCertificatesRequiredSignatures(): Set<string>;
637
649
  protected getVoteRequiredSignatures(): Set<string>;
@@ -669,6 +681,10 @@ declare class MeshTxBuilder extends MeshTxBuilderCore {
669
681
  calculateMinLovelaceForOutput: (output: Output) => bigint;
670
682
  protected clone(): MeshTxBuilder;
671
683
  }
684
+ declare const cloneOutput: (output: Output) => Output;
685
+ declare const setLoveLace: (output: Output, lovelace: bigint) => Output;
686
+ declare const getLovelace: (output: Output) => bigint;
687
+ declare const getOutputMinLovelace: (output: Output, coinsPerUtxoSize?: number) => bigint;
672
688
 
673
689
  declare class ForgeScript {
674
690
  static withOneSignature(address: string): string;
@@ -942,4 +958,19 @@ declare const metadataObjToMap: (metadata: any) => Metadatum;
942
958
  */
943
959
  declare const mergeContents: (a: Metadatum, b: Metadatum, currentDepth: number) => Metadatum;
944
960
 
945
- export { ForgeScript, MeshTxBuilder, type MeshTxBuilderOptions, type MetadataMergeLevel, Transaction, type TransactionOptions, mergeContents, metadataObjToMap, utxoToTxIn };
961
+ /**
962
+ * TxParser class to parse transaction hex strings and resolve UTxOs.
963
+ *
964
+ * It is used for either manipulating transactions or for unit testing transaction buildings.
965
+ */
966
+ declare class TxParser {
967
+ serializer: IMeshTxSerializer;
968
+ fetcher?: IFetcher | undefined;
969
+ constructor(serializer: IMeshTxSerializer, fetcher?: IFetcher | undefined);
970
+ parse: (txHex: string, providedUtxos?: UTxO[]) => Promise<_meshsdk_common.MeshTxBuilderBody>;
971
+ getBuilderBody: () => _meshsdk_common.MeshTxBuilderBody;
972
+ getBuilderBodyWithoutChange: () => _meshsdk_common.MeshTxBuilderBody;
973
+ toTester: () => _meshsdk_common.TxTester;
974
+ }
975
+
976
+ export { ForgeScript, MeshTxBuilder, type MeshTxBuilderOptions, type MetadataMergeLevel, Transaction, type TransactionOptions, TxParser, cloneOutput, getLovelace, getOutputMinLovelace, getUtxoMinLovelace, mergeContents, metadataObjToMap, setLoveLace, utxoToTxIn };
package/dist/index.d.ts CHANGED
@@ -1,3 +1,4 @@
1
+ import * as _meshsdk_common from '@meshsdk/common';
1
2
  import { UTxO, TxOutput, Action, Protocol, MintItem, TxIn, Withdrawal, Vote, PubKeyTxIn, RefTxIn, MeshTxBuilderBody, Asset, BuilderData, LanguageVersion, Voter, VotingProcedure, PoolParams, Anchor, DRep, Metadatum, Network, Redeemer, IFetcher, ISubmitter, IEvaluator, IMeshTxSerializer, ScriptSource, SimpleScriptSourceInfo, Certificate, MintParam, Output, NativeScript as NativeScript$1, IInitiator, Recipient, Token, PlutusScript, Budget, Data, Mint } from '@meshsdk/common';
2
3
  import { CredentialCore, NativeScript } from '@meshsdk/core-cst';
3
4
 
@@ -541,6 +542,13 @@ declare class MeshTxBuilderCore {
541
542
  * @returns [txHash, outputIndex, amount, address]
542
543
  */
543
544
  declare const utxoToTxIn: (utxo: UTxO) => [string, number, Asset[], string];
545
+ /**
546
+ * Calculate minimum lovelace required for a UTxO output
547
+ * @param utxo Output of utxo
548
+ * @param coinsPerUtxoSize From protocol parameters
549
+ * @returns Minimum lovelace required for the UTxO
550
+ */
551
+ declare const getUtxoMinLovelace: (utxo: TxOutput, coinsPerUtxoSize?: number) => bigint;
544
552
 
545
553
  interface MeshTxBuilderOptions {
546
554
  fetcher?: IFetcher;
@@ -632,6 +640,10 @@ declare class MeshTxBuilder extends MeshTxBuilderCore {
632
640
  paymentCreds: Set<string>;
633
641
  byronAddresses: Set<string>;
634
642
  };
643
+ protected getCollateralRequiredSignatures(): {
644
+ collateralPaymentCreds: Set<string>;
645
+ collateralByronAddresses: Set<string>;
646
+ };
635
647
  protected getWithdrawalRequiredSignatures(): Set<string>;
636
648
  protected getCertificatesRequiredSignatures(): Set<string>;
637
649
  protected getVoteRequiredSignatures(): Set<string>;
@@ -669,6 +681,10 @@ declare class MeshTxBuilder extends MeshTxBuilderCore {
669
681
  calculateMinLovelaceForOutput: (output: Output) => bigint;
670
682
  protected clone(): MeshTxBuilder;
671
683
  }
684
+ declare const cloneOutput: (output: Output) => Output;
685
+ declare const setLoveLace: (output: Output, lovelace: bigint) => Output;
686
+ declare const getLovelace: (output: Output) => bigint;
687
+ declare const getOutputMinLovelace: (output: Output, coinsPerUtxoSize?: number) => bigint;
672
688
 
673
689
  declare class ForgeScript {
674
690
  static withOneSignature(address: string): string;
@@ -942,4 +958,19 @@ declare const metadataObjToMap: (metadata: any) => Metadatum;
942
958
  */
943
959
  declare const mergeContents: (a: Metadatum, b: Metadatum, currentDepth: number) => Metadatum;
944
960
 
945
- export { ForgeScript, MeshTxBuilder, type MeshTxBuilderOptions, type MetadataMergeLevel, Transaction, type TransactionOptions, mergeContents, metadataObjToMap, utxoToTxIn };
961
+ /**
962
+ * TxParser class to parse transaction hex strings and resolve UTxOs.
963
+ *
964
+ * It is used for either manipulating transactions or for unit testing transaction buildings.
965
+ */
966
+ declare class TxParser {
967
+ serializer: IMeshTxSerializer;
968
+ fetcher?: IFetcher | undefined;
969
+ constructor(serializer: IMeshTxSerializer, fetcher?: IFetcher | undefined);
970
+ parse: (txHex: string, providedUtxos?: UTxO[]) => Promise<_meshsdk_common.MeshTxBuilderBody>;
971
+ getBuilderBody: () => _meshsdk_common.MeshTxBuilderBody;
972
+ getBuilderBodyWithoutChange: () => _meshsdk_common.MeshTxBuilderBody;
973
+ toTester: () => _meshsdk_common.TxTester;
974
+ }
975
+
976
+ export { ForgeScript, MeshTxBuilder, type MeshTxBuilderOptions, type MetadataMergeLevel, Transaction, type TransactionOptions, TxParser, cloneOutput, getLovelace, getOutputMinLovelace, getUtxoMinLovelace, mergeContents, metadataObjToMap, setLoveLace, utxoToTxIn };
package/dist/index.js CHANGED
@@ -1344,6 +1344,9 @@ var bignumber_default = BigNumber;
1344
1344
 
1345
1345
  // src/mesh-tx-builder/index.ts
1346
1346
  import JSONBig3 from "json-bigint";
1347
+ import {
1348
+ DEFAULT_PROTOCOL_PARAMETERS as DEFAULT_PROTOCOL_PARAMETERS3
1349
+ } from "@meshsdk/common";
1347
1350
  import {
1348
1351
  CardanoSDKSerializer,
1349
1352
  toDRep as coreToCstDRep,
@@ -3564,6 +3567,9 @@ var MeshTxBuilderCore = class {
3564
3567
  };
3565
3568
 
3566
3569
  // src/mesh-tx-builder/utils.ts
3570
+ import {
3571
+ DEFAULT_PROTOCOL_PARAMETERS as DEFAULT_PROTOCOL_PARAMETERS2
3572
+ } from "@meshsdk/common";
3567
3573
  var utxoToTxIn = (utxo) => {
3568
3574
  return [
3569
3575
  utxo.input.txHash,
@@ -3572,6 +3578,36 @@ var utxoToTxIn = (utxo) => {
3572
3578
  utxo.output.address
3573
3579
  ];
3574
3580
  };
3581
+ var getUtxoMinLovelace = (utxo, coinsPerUtxoSize = DEFAULT_PROTOCOL_PARAMETERS2.coinsPerUtxoSize) => {
3582
+ const referenceScript = utxo.scriptRef ? { code: utxo.scriptRef, version: "V3" } : void 0;
3583
+ let datum;
3584
+ if (utxo.plutusData) {
3585
+ datum = {
3586
+ type: "Inline",
3587
+ data: {
3588
+ content: utxo.plutusData,
3589
+ type: "CBOR"
3590
+ }
3591
+ };
3592
+ } else if (utxo.dataHash) {
3593
+ datum = {
3594
+ type: "Hash",
3595
+ data: {
3596
+ content: utxo.dataHash,
3597
+ // usually this should be entire datum cbor, but irrelevant in min utxo calculation
3598
+ type: "CBOR"
3599
+ }
3600
+ };
3601
+ }
3602
+ const output = {
3603
+ address: utxo.address,
3604
+ amount: utxo.amount,
3605
+ referenceScript,
3606
+ datum
3607
+ };
3608
+ const minLovelace = getOutputMinLovelace(output, coinsPerUtxoSize);
3609
+ return minLovelace;
3610
+ };
3575
3611
 
3576
3612
  // src/mesh-tx-builder/index.ts
3577
3613
  var MeshTxBuilder = class _MeshTxBuilder extends MeshTxBuilderCore {
@@ -4333,6 +4369,7 @@ var MeshTxBuilder = class _MeshTxBuilder extends MeshTxBuilderCore {
4333
4369
  };
4334
4370
  collectAllRequiredSignatures = () => {
4335
4371
  const { paymentCreds, byronAddresses } = this.getInputsRequiredSignatures();
4372
+ const { collateralPaymentCreds, collateralByronAddresses } = this.getCollateralRequiredSignatures();
4336
4373
  const withdrawalCreds = this.getWithdrawalRequiredSignatures();
4337
4374
  const certCreds = this.getCertificatesRequiredSignatures();
4338
4375
  const voteCreds = this.getVoteRequiredSignatures();
@@ -4341,12 +4378,17 @@ var MeshTxBuilder = class _MeshTxBuilder extends MeshTxBuilderCore {
4341
4378
  const allCreds = /* @__PURE__ */ new Set([
4342
4379
  ...paymentCreds,
4343
4380
  ...withdrawalCreds,
4381
+ ...collateralPaymentCreds,
4344
4382
  ...certCreds,
4345
4383
  ...voteCreds,
4346
4384
  ...requiredSignatures,
4347
4385
  ...mintCreds
4348
4386
  ]);
4349
- return { keyHashes: allCreds, byronAddresses };
4387
+ const allByronAddresses = /* @__PURE__ */ new Set([
4388
+ ...byronAddresses,
4389
+ ...collateralByronAddresses
4390
+ ]);
4391
+ return { keyHashes: allCreds, byronAddresses: allByronAddresses };
4350
4392
  };
4351
4393
  getInputsRequiredSignatures() {
4352
4394
  const byronAddresses = /* @__PURE__ */ new Set();
@@ -4379,6 +4421,29 @@ var MeshTxBuilder = class _MeshTxBuilder extends MeshTxBuilderCore {
4379
4421
  }
4380
4422
  return { paymentCreds, byronAddresses };
4381
4423
  }
4424
+ getCollateralRequiredSignatures() {
4425
+ const collateralByronAddresses = /* @__PURE__ */ new Set();
4426
+ const collateralPaymentCreds = /* @__PURE__ */ new Set();
4427
+ for (let collateral of this.meshTxBuilderBody.collaterals) {
4428
+ if (collateral.type === "PubKey") {
4429
+ if (collateral.txIn.address) {
4430
+ const address = CstAddress.fromString(collateral.txIn.address);
4431
+ if (!address) {
4432
+ continue;
4433
+ }
4434
+ const addressDetails = address.getProps();
4435
+ const paymentCred = addressDetails.paymentPart;
4436
+ if (paymentCred?.type === CstCredentialType.KeyHash) {
4437
+ collateralPaymentCreds.add(paymentCred.hash);
4438
+ }
4439
+ if (addressDetails.type === CstAddressType.Byron) {
4440
+ collateralByronAddresses.add(collateral.txIn.address);
4441
+ }
4442
+ }
4443
+ }
4444
+ }
4445
+ return { collateralPaymentCreds, collateralByronAddresses };
4446
+ }
4382
4447
  getWithdrawalRequiredSignatures() {
4383
4448
  const withdrawalCreds = /* @__PURE__ */ new Set();
4384
4449
  for (let withdrawal of this.meshTxBuilderBody.withdrawals) {
@@ -4960,24 +5025,7 @@ var MeshTxBuilder = class _MeshTxBuilder extends MeshTxBuilderCore {
4960
5025
  );
4961
5026
  };
4962
5027
  calculateMinLovelaceForOutput = (output) => {
4963
- let currentOutput = cloneOutput(output);
4964
- let lovelace = getLovelace(currentOutput);
4965
- let minAda = 0n;
4966
- for (let i = 0; i < 3; i++) {
4967
- const txOutSize = BigInt(
4968
- this.serializer.serializeOutput(currentOutput).length / 2
4969
- );
4970
- const txOutByteCost = BigInt(this._protocolParams.coinsPerUtxoSize);
4971
- const totalOutCost = (160n + BigInt(txOutSize)) * txOutByteCost;
4972
- minAda = totalOutCost;
4973
- if (lovelace < totalOutCost) {
4974
- lovelace = totalOutCost;
4975
- } else {
4976
- break;
4977
- }
4978
- currentOutput = setLoveLace(currentOutput, lovelace);
4979
- }
4980
- return minAda;
5028
+ return getOutputMinLovelace(output, this._protocolParams.coinsPerUtxoSize);
4981
5029
  };
4982
5030
  clone() {
4983
5031
  const newBuilder = super._cloneCore(() => {
@@ -5056,6 +5104,27 @@ var getLovelace = (output) => {
5056
5104
  }
5057
5105
  return 0n;
5058
5106
  };
5107
+ var getOutputMinLovelace = (output, coinsPerUtxoSize = DEFAULT_PROTOCOL_PARAMETERS3.coinsPerUtxoSize) => {
5108
+ const serializer = new CardanoSDKSerializer();
5109
+ let currentOutput = cloneOutput(output);
5110
+ let lovelace = getLovelace(currentOutput);
5111
+ let minLovelace = 0n;
5112
+ for (let i = 0; i < 3; i++) {
5113
+ const txOutSize = BigInt(
5114
+ serializer.serializeOutput(currentOutput).length / 2
5115
+ );
5116
+ const txOutByteCost = BigInt(coinsPerUtxoSize);
5117
+ const totalOutCost = (160n + BigInt(txOutSize)) * txOutByteCost;
5118
+ minLovelace = totalOutCost;
5119
+ if (lovelace < totalOutCost) {
5120
+ lovelace = totalOutCost;
5121
+ } else {
5122
+ break;
5123
+ }
5124
+ currentOutput = setLoveLace(currentOutput, lovelace);
5125
+ }
5126
+ return minLovelace;
5127
+ };
5059
5128
 
5060
5129
  // src/scripts/forge.script.ts
5061
5130
  import {
@@ -5779,11 +5848,66 @@ function mask(metadatum) {
5779
5848
  throw new Error(`Unsupported metadatum kind: ${metadatum.getKind()}`);
5780
5849
  }
5781
5850
  }
5851
+
5852
+ // src/tx-parser/index.ts
5853
+ var TxParser = class {
5854
+ constructor(serializer, fetcher) {
5855
+ this.serializer = serializer;
5856
+ this.fetcher = fetcher;
5857
+ }
5858
+ parse = async (txHex, providedUtxos = []) => {
5859
+ const resolvedUtxos = [...providedUtxos];
5860
+ const resolvedUtxosSet = new Set(
5861
+ providedUtxos.map(
5862
+ (utxo) => `${utxo.input.txHash}#${utxo.input.outputIndex}`
5863
+ )
5864
+ );
5865
+ const toResolveUtxos = {};
5866
+ const fetchResult = {};
5867
+ this.serializer.parser.getRequiredInputs(txHex).forEach((input) => {
5868
+ if (!resolvedUtxosSet.has(`${input.txHash}#${input.outputIndex}`)) {
5869
+ if (!toResolveUtxos[input.txHash]) {
5870
+ toResolveUtxos[input.txHash] = [];
5871
+ }
5872
+ toResolveUtxos[input.txHash].push(input.outputIndex);
5873
+ }
5874
+ });
5875
+ for (const txHash in toResolveUtxos) {
5876
+ const outputIndices = toResolveUtxos[txHash];
5877
+ if (!this.fetcher) {
5878
+ throw new Error(
5879
+ "Fetcher is not provided. Cannot resolve UTxOs without fetcher."
5880
+ );
5881
+ }
5882
+ const utxos = await this.fetcher.fetchUTxOs(txHash);
5883
+ outputIndices?.forEach((outputIndex) => {
5884
+ const utxoData = utxos.find(
5885
+ (utxo) => utxo.input.outputIndex === outputIndex
5886
+ );
5887
+ if (!utxoData) {
5888
+ throw new Error(`UTxO not found: ${txHash}:${outputIndex}`);
5889
+ }
5890
+ resolvedUtxos.push(utxoData);
5891
+ });
5892
+ }
5893
+ this.serializer.parser.parse(txHex, resolvedUtxos);
5894
+ return this.serializer.parser.getBuilderBody();
5895
+ };
5896
+ getBuilderBody = () => this.serializer.parser.getBuilderBody();
5897
+ getBuilderBodyWithoutChange = () => this.serializer.parser.getBuilderBodyWithoutChange();
5898
+ toTester = () => this.serializer.parser.toTester();
5899
+ };
5782
5900
  export {
5783
5901
  ForgeScript,
5784
5902
  MeshTxBuilder,
5785
5903
  Transaction,
5904
+ TxParser,
5905
+ cloneOutput,
5906
+ getLovelace,
5907
+ getOutputMinLovelace,
5908
+ getUtxoMinLovelace,
5786
5909
  mergeContents,
5787
5910
  metadataObjToMap,
5911
+ setLoveLace,
5788
5912
  utxoToTxIn
5789
5913
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshsdk/transaction",
3
- "version": "1.9.0-beta.54",
3
+ "version": "1.9.0-beta.56",
4
4
  "description": "Transactions - https://meshjs.dev/apis/transaction",
5
5
  "main": "./dist/index.cjs",
6
6
  "browser": "./dist/index.js",
@@ -35,8 +35,8 @@
35
35
  "typescript": "^5.3.3"
36
36
  },
37
37
  "dependencies": {
38
- "@meshsdk/common": "1.9.0-beta.54",
39
- "@meshsdk/core-cst": "1.9.0-beta.54",
38
+ "@meshsdk/common": "1.9.0-beta.56",
39
+ "@meshsdk/core-cst": "1.9.0-beta.56",
40
40
  "@cardano-sdk/core": "^0.45.5",
41
41
  "@cardano-sdk/util": "^0.15.5",
42
42
  "@cardano-sdk/input-selection": "^0.13.33",