@meshsdk/transaction 1.8.0 → 1.8.2
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 +122 -17
- package/dist/index.d.cts +38 -11
- package/dist/index.d.ts +38 -11
- package/dist/index.js +120 -17
- package/package.json +4 -4
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
|
|
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
|
|
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 = (
|
|
1235
|
-
|
|
1236
|
-
|
|
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
|
/**
|
|
@@ -1276,10 +1366,10 @@ var MeshTxBuilderCore = class {
|
|
|
1276
1366
|
return this;
|
|
1277
1367
|
};
|
|
1278
1368
|
/**
|
|
1279
|
-
|
|
1280
|
-
|
|
1281
|
-
|
|
1282
|
-
|
|
1369
|
+
* Sets a specific fee for the transaction to use
|
|
1370
|
+
* @param fee The specified fee
|
|
1371
|
+
* @returns The MeshTxBuilder instance
|
|
1372
|
+
*/
|
|
1283
1373
|
setFee = (fee) => {
|
|
1284
1374
|
this.meshTxBuilderBody.fee = fee;
|
|
1285
1375
|
return this;
|
|
@@ -1386,7 +1476,7 @@ var MeshTxBuilderCore = class {
|
|
|
1386
1476
|
};
|
|
1387
1477
|
castRawDataToJsonString = (rawData) => {
|
|
1388
1478
|
if (typeof rawData === "object") {
|
|
1389
|
-
return
|
|
1479
|
+
return import_json_bigint2.default.stringify(rawData);
|
|
1390
1480
|
} else {
|
|
1391
1481
|
return rawData;
|
|
1392
1482
|
}
|
|
@@ -2269,9 +2359,22 @@ var Transaction = class {
|
|
|
2269
2359
|
}
|
|
2270
2360
|
if (!mint.cip68ScriptAddress && mint.metadata && mint.label) {
|
|
2271
2361
|
if (mint.label === "721" || mint.label === "20") {
|
|
2272
|
-
this.
|
|
2273
|
-
|
|
2274
|
-
|
|
2362
|
+
let currentMetadata = this.txBuilder.meshTxBuilderBody.metadata;
|
|
2363
|
+
if (currentMetadata.size === 0) {
|
|
2364
|
+
this.setMetadata(Number(mint.label), {
|
|
2365
|
+
[policyId]: { [mint.assetName]: mint.metadata }
|
|
2366
|
+
});
|
|
2367
|
+
} else {
|
|
2368
|
+
let metadataMap = metadataObjToMap({
|
|
2369
|
+
[policyId]: { [mint.assetName]: mint.metadata }
|
|
2370
|
+
});
|
|
2371
|
+
let newMetadata = mergeContents(
|
|
2372
|
+
currentMetadata.get(BigInt(mint.label)),
|
|
2373
|
+
metadataMap,
|
|
2374
|
+
mint.label === "721" ? 2 : 0
|
|
2375
|
+
);
|
|
2376
|
+
this.setMetadata(Number(mint.label), newMetadata);
|
|
2377
|
+
}
|
|
2275
2378
|
} else {
|
|
2276
2379
|
this.setMetadata(Number(mint.label), mint.metadata);
|
|
2277
2380
|
}
|
|
@@ -2367,13 +2470,13 @@ var Transaction = class {
|
|
|
2367
2470
|
/**
|
|
2368
2471
|
* Add a JSON metadata entry to the transaction.
|
|
2369
2472
|
*
|
|
2370
|
-
* @param {number}
|
|
2371
|
-
* @param {unknown}
|
|
2473
|
+
* @param {number} label The label to use for the metadata entry.
|
|
2474
|
+
* @param {unknown} metadata The value to use for the metadata entry.
|
|
2372
2475
|
* @returns {Transaction} The Transaction object.
|
|
2373
2476
|
* @see {@link https://meshjs.dev/apis/transaction#setMetadata}
|
|
2374
2477
|
*/
|
|
2375
|
-
setMetadata(
|
|
2376
|
-
this.txBuilder.metadataValue(
|
|
2478
|
+
setMetadata(label, metadata) {
|
|
2479
|
+
this.txBuilder.metadataValue(label, metadata);
|
|
2377
2480
|
return this;
|
|
2378
2481
|
}
|
|
2379
2482
|
withdrawRewards(rewardAddress, lovelace) {
|
|
@@ -2515,5 +2618,7 @@ function mask(metadatum) {
|
|
|
2515
2618
|
ForgeScript,
|
|
2516
2619
|
MeshTxBuilder,
|
|
2517
2620
|
Transaction,
|
|
2621
|
+
mergeContents,
|
|
2622
|
+
metadataObjToMap,
|
|
2518
2623
|
utxoToTxIn
|
|
2519
2624
|
});
|
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
|
|
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: (
|
|
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)
|
|
@@ -473,10 +473,10 @@ declare class MeshTxBuilderCore {
|
|
|
473
473
|
*/
|
|
474
474
|
protocolParams: (params: Partial<Protocol>) => this;
|
|
475
475
|
/**
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
476
|
+
* Sets a specific fee for the transaction to use
|
|
477
|
+
* @param fee The specified fee
|
|
478
|
+
* @returns The MeshTxBuilder instance
|
|
479
|
+
*/
|
|
480
480
|
setFee: (fee: string) => this;
|
|
481
481
|
/**
|
|
482
482
|
* Sets the network to use, this is mainly to know the cost models to be used to calculate script integrity hash
|
|
@@ -723,12 +723,12 @@ declare class Transaction {
|
|
|
723
723
|
/**
|
|
724
724
|
* Add a JSON metadata entry to the transaction.
|
|
725
725
|
*
|
|
726
|
-
* @param {number}
|
|
727
|
-
* @param {unknown}
|
|
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(
|
|
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
|
-
|
|
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
|
|
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: (
|
|
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)
|
|
@@ -473,10 +473,10 @@ declare class MeshTxBuilderCore {
|
|
|
473
473
|
*/
|
|
474
474
|
protocolParams: (params: Partial<Protocol>) => this;
|
|
475
475
|
/**
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
476
|
+
* Sets a specific fee for the transaction to use
|
|
477
|
+
* @param fee The specified fee
|
|
478
|
+
* @returns The MeshTxBuilder instance
|
|
479
|
+
*/
|
|
480
480
|
setFee: (fee: string) => this;
|
|
481
481
|
/**
|
|
482
482
|
* Sets the network to use, this is mainly to know the cost models to be used to calculate script integrity hash
|
|
@@ -723,12 +723,12 @@ declare class Transaction {
|
|
|
723
723
|
/**
|
|
724
724
|
* Add a JSON metadata entry to the transaction.
|
|
725
725
|
*
|
|
726
|
-
* @param {number}
|
|
727
|
-
* @param {unknown}
|
|
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(
|
|
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
|
-
|
|
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
|
|
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
|
|
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 = (
|
|
1202
|
-
|
|
1203
|
-
|
|
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
|
/**
|
|
@@ -1243,10 +1331,10 @@ var MeshTxBuilderCore = class {
|
|
|
1243
1331
|
return this;
|
|
1244
1332
|
};
|
|
1245
1333
|
/**
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
|
|
1249
|
-
|
|
1334
|
+
* Sets a specific fee for the transaction to use
|
|
1335
|
+
* @param fee The specified fee
|
|
1336
|
+
* @returns The MeshTxBuilder instance
|
|
1337
|
+
*/
|
|
1250
1338
|
setFee = (fee) => {
|
|
1251
1339
|
this.meshTxBuilderBody.fee = fee;
|
|
1252
1340
|
return this;
|
|
@@ -1353,7 +1441,7 @@ var MeshTxBuilderCore = class {
|
|
|
1353
1441
|
};
|
|
1354
1442
|
castRawDataToJsonString = (rawData) => {
|
|
1355
1443
|
if (typeof rawData === "object") {
|
|
1356
|
-
return
|
|
1444
|
+
return JSONBig2.stringify(rawData);
|
|
1357
1445
|
} else {
|
|
1358
1446
|
return rawData;
|
|
1359
1447
|
}
|
|
@@ -2259,9 +2347,22 @@ var Transaction = class {
|
|
|
2259
2347
|
}
|
|
2260
2348
|
if (!mint.cip68ScriptAddress && mint.metadata && mint.label) {
|
|
2261
2349
|
if (mint.label === "721" || mint.label === "20") {
|
|
2262
|
-
this.
|
|
2263
|
-
|
|
2264
|
-
|
|
2350
|
+
let currentMetadata = this.txBuilder.meshTxBuilderBody.metadata;
|
|
2351
|
+
if (currentMetadata.size === 0) {
|
|
2352
|
+
this.setMetadata(Number(mint.label), {
|
|
2353
|
+
[policyId]: { [mint.assetName]: mint.metadata }
|
|
2354
|
+
});
|
|
2355
|
+
} else {
|
|
2356
|
+
let metadataMap = metadataObjToMap({
|
|
2357
|
+
[policyId]: { [mint.assetName]: mint.metadata }
|
|
2358
|
+
});
|
|
2359
|
+
let newMetadata = mergeContents(
|
|
2360
|
+
currentMetadata.get(BigInt(mint.label)),
|
|
2361
|
+
metadataMap,
|
|
2362
|
+
mint.label === "721" ? 2 : 0
|
|
2363
|
+
);
|
|
2364
|
+
this.setMetadata(Number(mint.label), newMetadata);
|
|
2365
|
+
}
|
|
2265
2366
|
} else {
|
|
2266
2367
|
this.setMetadata(Number(mint.label), mint.metadata);
|
|
2267
2368
|
}
|
|
@@ -2357,13 +2458,13 @@ var Transaction = class {
|
|
|
2357
2458
|
/**
|
|
2358
2459
|
* Add a JSON metadata entry to the transaction.
|
|
2359
2460
|
*
|
|
2360
|
-
* @param {number}
|
|
2361
|
-
* @param {unknown}
|
|
2461
|
+
* @param {number} label The label to use for the metadata entry.
|
|
2462
|
+
* @param {unknown} metadata The value to use for the metadata entry.
|
|
2362
2463
|
* @returns {Transaction} The Transaction object.
|
|
2363
2464
|
* @see {@link https://meshjs.dev/apis/transaction#setMetadata}
|
|
2364
2465
|
*/
|
|
2365
|
-
setMetadata(
|
|
2366
|
-
this.txBuilder.metadataValue(
|
|
2466
|
+
setMetadata(label, metadata) {
|
|
2467
|
+
this.txBuilder.metadataValue(label, metadata);
|
|
2367
2468
|
return this;
|
|
2368
2469
|
}
|
|
2369
2470
|
withdrawRewards(rewardAddress, lovelace) {
|
|
@@ -2504,5 +2605,7 @@ export {
|
|
|
2504
2605
|
ForgeScript,
|
|
2505
2606
|
MeshTxBuilder,
|
|
2506
2607
|
Transaction,
|
|
2608
|
+
mergeContents,
|
|
2609
|
+
metadataObjToMap,
|
|
2507
2610
|
utxoToTxIn
|
|
2508
2611
|
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meshsdk/transaction",
|
|
3
|
-
"version": "1.8.
|
|
3
|
+
"version": "1.8.2",
|
|
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.
|
|
39
|
-
"@meshsdk/core-csl": "1.8.
|
|
40
|
-
"@meshsdk/core-cst": "1.8.
|
|
38
|
+
"@meshsdk/common": "1.8.2",
|
|
39
|
+
"@meshsdk/core-csl": "1.8.2",
|
|
40
|
+
"@meshsdk/core-cst": "1.8.2",
|
|
41
41
|
"json-bigint": "^1.0.0"
|
|
42
42
|
},
|
|
43
43
|
"prettier": "@meshsdk/configs/prettier",
|