@meshsdk/transaction 1.8.1 → 1.8.3

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,6 +33,8 @@ __export(src_exports, {
33
33
  ForgeScript: () => ForgeScript,
34
34
  MeshTxBuilder: () => MeshTxBuilder,
35
35
  Transaction: () => Transaction,
36
+ mergeContents: () => mergeContents,
37
+ metadataObjToMap: () => metadataObjToMap,
36
38
  utxoToTxIn: () => utxoToTxIn
37
39
  });
38
40
  module.exports = __toCommonJS(src_exports);
@@ -41,8 +43,92 @@ module.exports = __toCommonJS(src_exports);
41
43
  var import_core_csl = require("@meshsdk/core-csl");
42
44
 
43
45
  // src/mesh-tx-builder/tx-builder-core.ts
44
- var import_json_bigint = __toESM(require("json-bigint"), 1);
46
+ var import_json_bigint2 = __toESM(require("json-bigint"), 1);
45
47
  var import_common = require("@meshsdk/common");
48
+
49
+ // src/utils/metadata.ts
50
+ var import_json_bigint = __toESM(require("json-bigint"), 1);
51
+ var metadataObjToMap = (metadata) => {
52
+ if (typeof metadata === "bigint") {
53
+ return metadata;
54
+ } else if (typeof metadata === "string") {
55
+ return metadata;
56
+ } else if (typeof metadata === "number") {
57
+ return metadata;
58
+ } else if (metadata instanceof Uint8Array) {
59
+ return metadata;
60
+ } else if (Array.isArray(metadata)) {
61
+ return metadata.map(metadataObjToMap);
62
+ } else if (metadata && typeof metadata === "object") {
63
+ const map = /* @__PURE__ */ new Map();
64
+ if (metadata instanceof Map) {
65
+ metadata.forEach((value, key) => {
66
+ map.set(metadataObjToMap(key), metadataObjToMap(value));
67
+ });
68
+ } else {
69
+ Object.entries(metadata).forEach(([key, value]) => {
70
+ map.set(metadataObjToMap(key), metadataObjToMap(value));
71
+ });
72
+ }
73
+ return map;
74
+ } else {
75
+ throw new Error("Metadata map conversion: Unsupported metadata type");
76
+ }
77
+ };
78
+ var mergeContents = (a, b, currentDepth) => {
79
+ if (currentDepth <= 0) {
80
+ return b;
81
+ }
82
+ if (a instanceof Map && b instanceof Map) {
83
+ b.forEach((value, key) => {
84
+ if (a.has(key)) {
85
+ a.set(
86
+ key,
87
+ mergeContents(a.get(key), value, currentDepth - 1)
88
+ );
89
+ } else {
90
+ a.set(key, value);
91
+ }
92
+ });
93
+ return a;
94
+ } else if (Array.isArray(a) && Array.isArray(b)) {
95
+ return [...a, ...b];
96
+ }
97
+ if ((typeof a === "number" || typeof a === "bigint" || typeof a === "string" || a instanceof Uint8Array) && (typeof b === "number" || typeof b === "bigint" || typeof b === "string" || b instanceof Uint8Array)) {
98
+ if (typeof a === typeof b) {
99
+ if (a === b) {
100
+ return b;
101
+ }
102
+ if (a instanceof Uint8Array && b instanceof Uint8Array && areUint8ArraysEqual(a, b)) {
103
+ return b;
104
+ }
105
+ }
106
+ throw new Error(
107
+ `Tx metadata merge error: cannot merge ${import_json_bigint.default.stringify(a)} with ${import_json_bigint.default.stringify(b)}`
108
+ );
109
+ }
110
+ throw new Error(
111
+ `Tx metadata merge error: cannot merge ${getMetadatumType(a)} type with ${getMetadatumType(b)} type`
112
+ );
113
+ };
114
+ var getMetadatumType = (a) => {
115
+ if (a instanceof Map) return "map";
116
+ if (Array.isArray(a)) return "array";
117
+ return "primitive";
118
+ };
119
+ var areUint8ArraysEqual = (a, b) => {
120
+ if (a.length !== b.length) {
121
+ return false;
122
+ }
123
+ for (let i = 0; i < a.length; i++) {
124
+ if (a[i] !== b[i]) {
125
+ return false;
126
+ }
127
+ }
128
+ return true;
129
+ };
130
+
131
+ // src/mesh-tx-builder/tx-builder-core.ts
46
132
  var MeshTxBuilderCore = class {
47
133
  txEvaluationMultiplier = 1.1;
48
134
  txOutput;
@@ -1227,13 +1313,17 @@ var MeshTxBuilderCore = class {
1227
1313
  };
1228
1314
  /**
1229
1315
  * Add metadata to the transaction
1230
- * @param tag The tag of the metadata
1316
+ * @param label The label of the metadata, preferably number
1231
1317
  * @param metadata The metadata in any format
1232
1318
  * @returns The MeshTxBuilder instance
1233
1319
  */
1234
- metadataValue = (tag, metadata) => {
1235
- const metadataString = import_json_bigint.default.stringify(metadata);
1236
- this.meshTxBuilderBody.metadata.push({ tag, metadata: metadataString });
1320
+ metadataValue = (label, metadata) => {
1321
+ label = BigInt(label);
1322
+ if (typeof metadata === "object" && !(metadata instanceof Map)) {
1323
+ this.meshTxBuilderBody.metadata.set(label, metadataObjToMap(metadata));
1324
+ } else {
1325
+ this.meshTxBuilderBody.metadata.set(label, metadata);
1326
+ }
1237
1327
  return this;
1238
1328
  };
1239
1329
  /**
@@ -1386,7 +1476,7 @@ var MeshTxBuilderCore = class {
1386
1476
  };
1387
1477
  castRawDataToJsonString = (rawData) => {
1388
1478
  if (typeof rawData === "object") {
1389
- return import_json_bigint.default.stringify(rawData);
1479
+ return import_json_bigint2.default.stringify(rawData);
1390
1480
  } else {
1391
1481
  return rawData;
1392
1482
  }
@@ -1700,6 +1790,20 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1700
1790
  }
1701
1791
  });
1702
1792
  this.addUtxosFromSelection();
1793
+ this.meshTxBuilderBody.inputs.sort((a, b) => {
1794
+ if (a.txIn.txHash < b.txIn.txHash) return -1;
1795
+ if (a.txIn.txHash > b.txIn.txHash) return 1;
1796
+ if (a.txIn.txIndex < b.txIn.txIndex) return -1;
1797
+ if (a.txIn.txIndex > b.txIn.txIndex) return 1;
1798
+ return 0;
1799
+ });
1800
+ this.meshTxBuilderBody.mints.sort((a, b) => {
1801
+ if (a.policyId < b.policyId) return -1;
1802
+ if (a.policyId > b.policyId) return 1;
1803
+ if (a.assetName < b.assetName) return -1;
1804
+ if (a.assetName > b.assetName) return 1;
1805
+ return 0;
1806
+ });
1703
1807
  let txHex = this.serializer.serializeTxBody(
1704
1808
  this.meshTxBuilderBody,
1705
1809
  this._protocolParams
@@ -2269,9 +2373,22 @@ var Transaction = class {
2269
2373
  }
2270
2374
  if (!mint.cip68ScriptAddress && mint.metadata && mint.label) {
2271
2375
  if (mint.label === "721" || mint.label === "20") {
2272
- this.setMetadata(Number(mint.label), {
2273
- [policyId]: { [mint.assetName]: mint.metadata }
2274
- });
2376
+ let currentMetadata = this.txBuilder.meshTxBuilderBody.metadata;
2377
+ if (currentMetadata.get(BigInt(mint.label)) === void 0) {
2378
+ this.setMetadata(Number(mint.label), {
2379
+ [policyId]: { [mint.assetName]: mint.metadata }
2380
+ });
2381
+ } else {
2382
+ let metadataMap = metadataObjToMap({
2383
+ [policyId]: { [mint.assetName]: mint.metadata }
2384
+ });
2385
+ let newMetadata = mergeContents(
2386
+ currentMetadata.get(BigInt(mint.label)),
2387
+ metadataMap,
2388
+ mint.label === "721" ? 2 : 0
2389
+ );
2390
+ this.setMetadata(Number(mint.label), newMetadata);
2391
+ }
2275
2392
  } else {
2276
2393
  this.setMetadata(Number(mint.label), mint.metadata);
2277
2394
  }
@@ -2367,13 +2484,13 @@ var Transaction = class {
2367
2484
  /**
2368
2485
  * Add a JSON metadata entry to the transaction.
2369
2486
  *
2370
- * @param {number} key The key to use for the metadata entry.
2371
- * @param {unknown} value The value to use for the metadata entry.
2487
+ * @param {number} label The label to use for the metadata entry.
2488
+ * @param {unknown} metadata The value to use for the metadata entry.
2372
2489
  * @returns {Transaction} The Transaction object.
2373
2490
  * @see {@link https://meshjs.dev/apis/transaction#setMetadata}
2374
2491
  */
2375
- setMetadata(key, value) {
2376
- this.txBuilder.metadataValue(key.toString(), value);
2492
+ setMetadata(label, metadata) {
2493
+ this.txBuilder.metadataValue(label, metadata);
2377
2494
  return this;
2378
2495
  }
2379
2496
  withdrawRewards(rewardAddress, lovelace) {
@@ -2515,5 +2632,7 @@ function mask(metadatum) {
2515
2632
  ForgeScript,
2516
2633
  MeshTxBuilder,
2517
2634
  Transaction,
2635
+ mergeContents,
2636
+ metadataObjToMap,
2518
2637
  utxoToTxIn
2519
2638
  });
package/dist/index.d.cts CHANGED
@@ -1,4 +1,4 @@
1
- import { Protocol, MintItem, TxIn, Withdrawal, Vote, PubKeyTxIn, RefTxIn, MeshTxBuilderBody, Asset, BuilderData, LanguageVersion, Voter, VotingProcedure, PoolParams, Anchor, DRep, UTxO, UtxoSelectionStrategy, Network, Redeemer, Action, IFetcher, ISubmitter, IEvaluator, IMeshTxSerializer, ScriptSource, SimpleScriptSourceInfo, NativeScript, IInitiator, Recipient, Token, PlutusScript, Budget, Data, Mint } from '@meshsdk/common';
1
+ import { Protocol, MintItem, TxIn, Withdrawal, Vote, PubKeyTxIn, RefTxIn, MeshTxBuilderBody, Asset, BuilderData, LanguageVersion, Voter, VotingProcedure, PoolParams, Anchor, DRep, Metadatum, UTxO, UtxoSelectionStrategy, Network, Redeemer, Action, IFetcher, ISubmitter, IEvaluator, IMeshTxSerializer, ScriptSource, SimpleScriptSourceInfo, NativeScript, IInitiator, Recipient, Token, PlutusScript, Budget, Data, Mint } from '@meshsdk/common';
2
2
 
3
3
  declare class MeshTxBuilderCore {
4
4
  txEvaluationMultiplier: number;
@@ -447,11 +447,11 @@ declare class MeshTxBuilderCore {
447
447
  invalidHereafter: (slot: number) => this;
448
448
  /**
449
449
  * Add metadata to the transaction
450
- * @param tag The tag of the metadata
450
+ * @param label The label of the metadata, preferably number
451
451
  * @param metadata The metadata in any format
452
452
  * @returns The MeshTxBuilder instance
453
453
  */
454
- metadataValue: (tag: string, metadata: any) => this;
454
+ metadataValue: (label: number | bigint | string, metadata: Metadatum | object) => this;
455
455
  /**
456
456
  * Sign the transaction with the private key
457
457
  * @param skeyHex The private key in cborHex (with or without 5820 prefix, i.e. the format when generated from cardano-cli)
@@ -723,12 +723,12 @@ declare class Transaction {
723
723
  /**
724
724
  * Add a JSON metadata entry to the transaction.
725
725
  *
726
- * @param {number} key The key to use for the metadata entry.
727
- * @param {unknown} value The value to use for the metadata entry.
726
+ * @param {number} label The label to use for the metadata entry.
727
+ * @param {unknown} metadata The value to use for the metadata entry.
728
728
  * @returns {Transaction} The Transaction object.
729
729
  * @see {@link https://meshjs.dev/apis/transaction#setMetadata}
730
730
  */
731
- setMetadata(key: number, value: unknown): Transaction;
731
+ setMetadata(label: number, metadata: Metadatum | object): Transaction;
732
732
  withdrawRewards(rewardAddress: string, lovelace: string): Transaction;
733
733
  delegateStake(rewardAddress: string, poolId: string): Transaction;
734
734
  deregisterStake(rewardAddress: string): Transaction;
@@ -743,4 +743,31 @@ declare class Transaction {
743
743
  private addChangeAddress;
744
744
  }
745
745
 
746
- export { ForgeScript, MeshTxBuilder, type MeshTxBuilderOptions, Transaction, type TransactionOptions, utxoToTxIn };
746
+ type MetadataMergeLevel = boolean | number;
747
+ declare const metadataObjToMap: (metadata: any) => Metadatum;
748
+ /**
749
+ * Recursively merge two metadata. Returns the 2nd item if the maximum allowed
750
+ * merge depth has passed.
751
+ *
752
+ * Merging maps ({ key: value }):
753
+ * Two maps are merged by recursively including the (key, value) pairs from both the maps.
754
+ * When further merge isn't allowed (by currentDepth), the 2nd item is preferred,
755
+ * replacing the 1st item.
756
+ *
757
+ * Merging arrays:
758
+ * Two arrays are merged by concatenating them.
759
+ * When merge isn't allowed (by currentDepth), the 2nd array is returned.
760
+ *
761
+ * Merging primitive types (number, string, etc.):
762
+ * Primitive types are not merged in the sense of concatenating. In case they are the same,
763
+ * either of them can be considered as the "merged value". 2nd item is returned here.
764
+ * When merge isn't allowed (by currentDepth), the 2nd item is returned.
765
+ *
766
+ * @param a first item
767
+ * @param b second item
768
+ * @param currentDepth the current merge depth; decreases in a recursive call
769
+ * @returns merged item or a preferred item, chosen according to currentDepth
770
+ */
771
+ declare const mergeContents: (a: Metadatum, b: Metadatum, currentDepth: number) => Metadatum;
772
+
773
+ export { ForgeScript, MeshTxBuilder, type MeshTxBuilderOptions, type MetadataMergeLevel, Transaction, type TransactionOptions, mergeContents, metadataObjToMap, utxoToTxIn };
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Protocol, MintItem, TxIn, Withdrawal, Vote, PubKeyTxIn, RefTxIn, MeshTxBuilderBody, Asset, BuilderData, LanguageVersion, Voter, VotingProcedure, PoolParams, Anchor, DRep, UTxO, UtxoSelectionStrategy, Network, Redeemer, Action, IFetcher, ISubmitter, IEvaluator, IMeshTxSerializer, ScriptSource, SimpleScriptSourceInfo, NativeScript, IInitiator, Recipient, Token, PlutusScript, Budget, Data, Mint } from '@meshsdk/common';
1
+ import { Protocol, MintItem, TxIn, Withdrawal, Vote, PubKeyTxIn, RefTxIn, MeshTxBuilderBody, Asset, BuilderData, LanguageVersion, Voter, VotingProcedure, PoolParams, Anchor, DRep, Metadatum, UTxO, UtxoSelectionStrategy, Network, Redeemer, Action, IFetcher, ISubmitter, IEvaluator, IMeshTxSerializer, ScriptSource, SimpleScriptSourceInfo, NativeScript, IInitiator, Recipient, Token, PlutusScript, Budget, Data, Mint } from '@meshsdk/common';
2
2
 
3
3
  declare class MeshTxBuilderCore {
4
4
  txEvaluationMultiplier: number;
@@ -447,11 +447,11 @@ declare class MeshTxBuilderCore {
447
447
  invalidHereafter: (slot: number) => this;
448
448
  /**
449
449
  * Add metadata to the transaction
450
- * @param tag The tag of the metadata
450
+ * @param label The label of the metadata, preferably number
451
451
  * @param metadata The metadata in any format
452
452
  * @returns The MeshTxBuilder instance
453
453
  */
454
- metadataValue: (tag: string, metadata: any) => this;
454
+ metadataValue: (label: number | bigint | string, metadata: Metadatum | object) => this;
455
455
  /**
456
456
  * Sign the transaction with the private key
457
457
  * @param skeyHex The private key in cborHex (with or without 5820 prefix, i.e. the format when generated from cardano-cli)
@@ -723,12 +723,12 @@ declare class Transaction {
723
723
  /**
724
724
  * Add a JSON metadata entry to the transaction.
725
725
  *
726
- * @param {number} key The key to use for the metadata entry.
727
- * @param {unknown} value The value to use for the metadata entry.
726
+ * @param {number} label The label to use for the metadata entry.
727
+ * @param {unknown} metadata The value to use for the metadata entry.
728
728
  * @returns {Transaction} The Transaction object.
729
729
  * @see {@link https://meshjs.dev/apis/transaction#setMetadata}
730
730
  */
731
- setMetadata(key: number, value: unknown): Transaction;
731
+ setMetadata(label: number, metadata: Metadatum | object): Transaction;
732
732
  withdrawRewards(rewardAddress: string, lovelace: string): Transaction;
733
733
  delegateStake(rewardAddress: string, poolId: string): Transaction;
734
734
  deregisterStake(rewardAddress: string): Transaction;
@@ -743,4 +743,31 @@ declare class Transaction {
743
743
  private addChangeAddress;
744
744
  }
745
745
 
746
- export { ForgeScript, MeshTxBuilder, type MeshTxBuilderOptions, Transaction, type TransactionOptions, utxoToTxIn };
746
+ type MetadataMergeLevel = boolean | number;
747
+ declare const metadataObjToMap: (metadata: any) => Metadatum;
748
+ /**
749
+ * Recursively merge two metadata. Returns the 2nd item if the maximum allowed
750
+ * merge depth has passed.
751
+ *
752
+ * Merging maps ({ key: value }):
753
+ * Two maps are merged by recursively including the (key, value) pairs from both the maps.
754
+ * When further merge isn't allowed (by currentDepth), the 2nd item is preferred,
755
+ * replacing the 1st item.
756
+ *
757
+ * Merging arrays:
758
+ * Two arrays are merged by concatenating them.
759
+ * When merge isn't allowed (by currentDepth), the 2nd array is returned.
760
+ *
761
+ * Merging primitive types (number, string, etc.):
762
+ * Primitive types are not merged in the sense of concatenating. In case they are the same,
763
+ * either of them can be considered as the "merged value". 2nd item is returned here.
764
+ * When merge isn't allowed (by currentDepth), the 2nd item is returned.
765
+ *
766
+ * @param a first item
767
+ * @param b second item
768
+ * @param currentDepth the current merge depth; decreases in a recursive call
769
+ * @returns merged item or a preferred item, chosen according to currentDepth
770
+ */
771
+ declare const mergeContents: (a: Metadatum, b: Metadatum, currentDepth: number) => Metadatum;
772
+
773
+ export { ForgeScript, MeshTxBuilder, type MeshTxBuilderOptions, type MetadataMergeLevel, Transaction, type TransactionOptions, mergeContents, metadataObjToMap, utxoToTxIn };
package/dist/index.js CHANGED
@@ -2,7 +2,7 @@
2
2
  import { CSLSerializer } from "@meshsdk/core-csl";
3
3
 
4
4
  // src/mesh-tx-builder/tx-builder-core.ts
5
- import JSONBig from "json-bigint";
5
+ import JSONBig2 from "json-bigint";
6
6
  import {
7
7
  DEFAULT_PROTOCOL_PARAMETERS,
8
8
  DEFAULT_REDEEMER_BUDGET,
@@ -10,6 +10,90 @@ import {
10
10
  emptyTxBuilderBody,
11
11
  UtxoSelection
12
12
  } from "@meshsdk/common";
13
+
14
+ // src/utils/metadata.ts
15
+ import JSONBig from "json-bigint";
16
+ var metadataObjToMap = (metadata) => {
17
+ if (typeof metadata === "bigint") {
18
+ return metadata;
19
+ } else if (typeof metadata === "string") {
20
+ return metadata;
21
+ } else if (typeof metadata === "number") {
22
+ return metadata;
23
+ } else if (metadata instanceof Uint8Array) {
24
+ return metadata;
25
+ } else if (Array.isArray(metadata)) {
26
+ return metadata.map(metadataObjToMap);
27
+ } else if (metadata && typeof metadata === "object") {
28
+ const map = /* @__PURE__ */ new Map();
29
+ if (metadata instanceof Map) {
30
+ metadata.forEach((value, key) => {
31
+ map.set(metadataObjToMap(key), metadataObjToMap(value));
32
+ });
33
+ } else {
34
+ Object.entries(metadata).forEach(([key, value]) => {
35
+ map.set(metadataObjToMap(key), metadataObjToMap(value));
36
+ });
37
+ }
38
+ return map;
39
+ } else {
40
+ throw new Error("Metadata map conversion: Unsupported metadata type");
41
+ }
42
+ };
43
+ var mergeContents = (a, b, currentDepth) => {
44
+ if (currentDepth <= 0) {
45
+ return b;
46
+ }
47
+ if (a instanceof Map && b instanceof Map) {
48
+ b.forEach((value, key) => {
49
+ if (a.has(key)) {
50
+ a.set(
51
+ key,
52
+ mergeContents(a.get(key), value, currentDepth - 1)
53
+ );
54
+ } else {
55
+ a.set(key, value);
56
+ }
57
+ });
58
+ return a;
59
+ } else if (Array.isArray(a) && Array.isArray(b)) {
60
+ return [...a, ...b];
61
+ }
62
+ if ((typeof a === "number" || typeof a === "bigint" || typeof a === "string" || a instanceof Uint8Array) && (typeof b === "number" || typeof b === "bigint" || typeof b === "string" || b instanceof Uint8Array)) {
63
+ if (typeof a === typeof b) {
64
+ if (a === b) {
65
+ return b;
66
+ }
67
+ if (a instanceof Uint8Array && b instanceof Uint8Array && areUint8ArraysEqual(a, b)) {
68
+ return b;
69
+ }
70
+ }
71
+ throw new Error(
72
+ `Tx metadata merge error: cannot merge ${JSONBig.stringify(a)} with ${JSONBig.stringify(b)}`
73
+ );
74
+ }
75
+ throw new Error(
76
+ `Tx metadata merge error: cannot merge ${getMetadatumType(a)} type with ${getMetadatumType(b)} type`
77
+ );
78
+ };
79
+ var getMetadatumType = (a) => {
80
+ if (a instanceof Map) return "map";
81
+ if (Array.isArray(a)) return "array";
82
+ return "primitive";
83
+ };
84
+ var areUint8ArraysEqual = (a, b) => {
85
+ if (a.length !== b.length) {
86
+ return false;
87
+ }
88
+ for (let i = 0; i < a.length; i++) {
89
+ if (a[i] !== b[i]) {
90
+ return false;
91
+ }
92
+ }
93
+ return true;
94
+ };
95
+
96
+ // src/mesh-tx-builder/tx-builder-core.ts
13
97
  var MeshTxBuilderCore = class {
14
98
  txEvaluationMultiplier = 1.1;
15
99
  txOutput;
@@ -1194,13 +1278,17 @@ var MeshTxBuilderCore = class {
1194
1278
  };
1195
1279
  /**
1196
1280
  * Add metadata to the transaction
1197
- * @param tag The tag of the metadata
1281
+ * @param label The label of the metadata, preferably number
1198
1282
  * @param metadata The metadata in any format
1199
1283
  * @returns The MeshTxBuilder instance
1200
1284
  */
1201
- metadataValue = (tag, metadata) => {
1202
- const metadataString = JSONBig.stringify(metadata);
1203
- this.meshTxBuilderBody.metadata.push({ tag, metadata: metadataString });
1285
+ metadataValue = (label, metadata) => {
1286
+ label = BigInt(label);
1287
+ if (typeof metadata === "object" && !(metadata instanceof Map)) {
1288
+ this.meshTxBuilderBody.metadata.set(label, metadataObjToMap(metadata));
1289
+ } else {
1290
+ this.meshTxBuilderBody.metadata.set(label, metadata);
1291
+ }
1204
1292
  return this;
1205
1293
  };
1206
1294
  /**
@@ -1353,7 +1441,7 @@ var MeshTxBuilderCore = class {
1353
1441
  };
1354
1442
  castRawDataToJsonString = (rawData) => {
1355
1443
  if (typeof rawData === "object") {
1356
- return JSONBig.stringify(rawData);
1444
+ return JSONBig2.stringify(rawData);
1357
1445
  } else {
1358
1446
  return rawData;
1359
1447
  }
@@ -1667,6 +1755,20 @@ var MeshTxBuilder = class extends MeshTxBuilderCore {
1667
1755
  }
1668
1756
  });
1669
1757
  this.addUtxosFromSelection();
1758
+ this.meshTxBuilderBody.inputs.sort((a, b) => {
1759
+ if (a.txIn.txHash < b.txIn.txHash) return -1;
1760
+ if (a.txIn.txHash > b.txIn.txHash) return 1;
1761
+ if (a.txIn.txIndex < b.txIn.txIndex) return -1;
1762
+ if (a.txIn.txIndex > b.txIn.txIndex) return 1;
1763
+ return 0;
1764
+ });
1765
+ this.meshTxBuilderBody.mints.sort((a, b) => {
1766
+ if (a.policyId < b.policyId) return -1;
1767
+ if (a.policyId > b.policyId) return 1;
1768
+ if (a.assetName < b.assetName) return -1;
1769
+ if (a.assetName > b.assetName) return 1;
1770
+ return 0;
1771
+ });
1670
1772
  let txHex = this.serializer.serializeTxBody(
1671
1773
  this.meshTxBuilderBody,
1672
1774
  this._protocolParams
@@ -2259,9 +2361,22 @@ var Transaction = class {
2259
2361
  }
2260
2362
  if (!mint.cip68ScriptAddress && mint.metadata && mint.label) {
2261
2363
  if (mint.label === "721" || mint.label === "20") {
2262
- this.setMetadata(Number(mint.label), {
2263
- [policyId]: { [mint.assetName]: mint.metadata }
2264
- });
2364
+ let currentMetadata = this.txBuilder.meshTxBuilderBody.metadata;
2365
+ if (currentMetadata.get(BigInt(mint.label)) === void 0) {
2366
+ this.setMetadata(Number(mint.label), {
2367
+ [policyId]: { [mint.assetName]: mint.metadata }
2368
+ });
2369
+ } else {
2370
+ let metadataMap = metadataObjToMap({
2371
+ [policyId]: { [mint.assetName]: mint.metadata }
2372
+ });
2373
+ let newMetadata = mergeContents(
2374
+ currentMetadata.get(BigInt(mint.label)),
2375
+ metadataMap,
2376
+ mint.label === "721" ? 2 : 0
2377
+ );
2378
+ this.setMetadata(Number(mint.label), newMetadata);
2379
+ }
2265
2380
  } else {
2266
2381
  this.setMetadata(Number(mint.label), mint.metadata);
2267
2382
  }
@@ -2357,13 +2472,13 @@ var Transaction = class {
2357
2472
  /**
2358
2473
  * Add a JSON metadata entry to the transaction.
2359
2474
  *
2360
- * @param {number} key The key to use for the metadata entry.
2361
- * @param {unknown} value The value to use for the metadata entry.
2475
+ * @param {number} label The label to use for the metadata entry.
2476
+ * @param {unknown} metadata The value to use for the metadata entry.
2362
2477
  * @returns {Transaction} The Transaction object.
2363
2478
  * @see {@link https://meshjs.dev/apis/transaction#setMetadata}
2364
2479
  */
2365
- setMetadata(key, value) {
2366
- this.txBuilder.metadataValue(key.toString(), value);
2480
+ setMetadata(label, metadata) {
2481
+ this.txBuilder.metadataValue(label, metadata);
2367
2482
  return this;
2368
2483
  }
2369
2484
  withdrawRewards(rewardAddress, lovelace) {
@@ -2504,5 +2619,7 @@ export {
2504
2619
  ForgeScript,
2505
2620
  MeshTxBuilder,
2506
2621
  Transaction,
2622
+ mergeContents,
2623
+ metadataObjToMap,
2507
2624
  utxoToTxIn
2508
2625
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@meshsdk/transaction",
3
- "version": "1.8.1",
3
+ "version": "1.8.3",
4
4
  "description": "Transactions - https://meshjs.dev/apis/transaction",
5
5
  "main": "./dist/index.cjs",
6
6
  "browser": "./dist/index.js",
@@ -35,9 +35,9 @@
35
35
  "typescript": "^5.3.3"
36
36
  },
37
37
  "dependencies": {
38
- "@meshsdk/common": "1.8.1",
39
- "@meshsdk/core-csl": "1.8.1",
40
- "@meshsdk/core-cst": "1.8.1",
38
+ "@meshsdk/common": "1.8.3",
39
+ "@meshsdk/core-csl": "1.8.3",
40
+ "@meshsdk/core-cst": "1.8.3",
41
41
  "json-bigint": "^1.0.0"
42
42
  },
43
43
  "prettier": "@meshsdk/configs/prettier",