@lucid-evolution/utils 0.1.10 → 0.1.12
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 +68 -11
- package/dist/index.d.cts +17 -3
- package/dist/index.d.ts +17 -3
- package/dist/index.js +60 -7
- package/package.json +3 -2
package/dist/index.cjs
CHANGED
|
@@ -34,6 +34,7 @@ __export(src_exports, {
|
|
|
34
34
|
addAssets: () => addAssets,
|
|
35
35
|
addressFromHexOrBech32: () => addressFromHexOrBech32,
|
|
36
36
|
applyDoubleCborEncoding: () => applyDoubleCborEncoding,
|
|
37
|
+
applyParamsToScript: () => applyParamsToScript,
|
|
37
38
|
assetsToValue: () => assetsToValue,
|
|
38
39
|
coreToOutRef: () => coreToOutRef,
|
|
39
40
|
coreToTxOutput: () => coreToTxOutput,
|
|
@@ -51,6 +52,7 @@ __export(src_exports, {
|
|
|
51
52
|
generatePrivateKey: () => generatePrivateKey,
|
|
52
53
|
generateSeedPhrase: () => generateSeedPhrase,
|
|
53
54
|
getAddressDetails: () => getAddressDetails,
|
|
55
|
+
isEqualUTxO: () => isEqualUTxO,
|
|
54
56
|
keyHashToCredential: () => keyHashToCredential,
|
|
55
57
|
mintingPolicyToId: () => mintingPolicyToId,
|
|
56
58
|
nativeFromJson: () => nativeFromJson,
|
|
@@ -59,7 +61,9 @@ __export(src_exports, {
|
|
|
59
61
|
networkToId: () => networkToId,
|
|
60
62
|
paymentCredentialOf: () => paymentCredentialOf,
|
|
61
63
|
scriptHashToCredential: () => scriptHashToCredential,
|
|
64
|
+
selectUTxOs: () => selectUTxOs,
|
|
62
65
|
slotToUnixTime: () => slotToUnixTime,
|
|
66
|
+
sortUTxOs: () => sortUTxOs,
|
|
63
67
|
stakeCredentialOf: () => stakeCredentialOf,
|
|
64
68
|
toLabel: () => toLabel,
|
|
65
69
|
toNativeScript: () => toNativeScript,
|
|
@@ -148,6 +152,9 @@ var applyDoubleCborEncoding = (script) => {
|
|
|
148
152
|
};
|
|
149
153
|
|
|
150
154
|
// src/scripts.ts
|
|
155
|
+
var import_plutus = require("@lucid-evolution/plutus");
|
|
156
|
+
var UPLC = __toESM(require("@lucid-evolution/uplc"), 1);
|
|
157
|
+
var import_core_utils2 = require("@lucid-evolution/core-utils");
|
|
151
158
|
function validatorToAddress(network, validator, stakeCredential) {
|
|
152
159
|
const validatorHash = validatorToScriptHash(validator);
|
|
153
160
|
if (stakeCredential) {
|
|
@@ -245,6 +252,12 @@ function nativeScriptFromJson(nativeScript) {
|
|
|
245
252
|
).to_cbor_hex()
|
|
246
253
|
};
|
|
247
254
|
}
|
|
255
|
+
function applyParamsToScript(plutusScript, params, type) {
|
|
256
|
+
const p = type ? import_plutus.Data.castTo(params, type) : params;
|
|
257
|
+
return (0, import_core_utils2.toHex)(
|
|
258
|
+
UPLC.apply_params_to_script((0, import_core_utils2.fromHex)(import_plutus.Data.to(p)), (0, import_core_utils2.fromHex)(plutusScript))
|
|
259
|
+
);
|
|
260
|
+
}
|
|
248
261
|
|
|
249
262
|
// src/address.ts
|
|
250
263
|
function addressFromHexOrBech32(address) {
|
|
@@ -878,7 +891,7 @@ function toPublicKey(privateKey) {
|
|
|
878
891
|
}
|
|
879
892
|
|
|
880
893
|
// src/label.ts
|
|
881
|
-
var
|
|
894
|
+
var import_core_utils3 = require("@lucid-evolution/core-utils");
|
|
882
895
|
var import_crc8 = require("@lucid-evolution/crc8");
|
|
883
896
|
function toLabel(num) {
|
|
884
897
|
if (num < 0 || num > 65535) {
|
|
@@ -899,25 +912,25 @@ function fromLabel(label) {
|
|
|
899
912
|
return check === checksum(numHex) ? num : null;
|
|
900
913
|
}
|
|
901
914
|
function checksum(num) {
|
|
902
|
-
return (0, import_crc8.crc8)((0,
|
|
915
|
+
return (0, import_crc8.crc8)((0, import_core_utils3.fromHex)(num)).toString(16).padStart(2, "0");
|
|
903
916
|
}
|
|
904
917
|
|
|
905
918
|
// src/time.ts
|
|
906
|
-
var
|
|
919
|
+
var import_plutus2 = require("@lucid-evolution/plutus");
|
|
907
920
|
function unixTimeToSlot(network, unixTime) {
|
|
908
|
-
return (0,
|
|
921
|
+
return (0, import_plutus2.unixTimeToEnclosingSlot)(unixTime, import_plutus2.SLOT_CONFIG_NETWORK[network]);
|
|
909
922
|
}
|
|
910
923
|
function slotToUnixTime(network, slot) {
|
|
911
|
-
return (0,
|
|
924
|
+
return (0, import_plutus2.slotToBeginUnixTime)(slot, import_plutus2.SLOT_CONFIG_NETWORK[network]);
|
|
912
925
|
}
|
|
913
926
|
|
|
914
927
|
// src/value.ts
|
|
915
|
-
var
|
|
928
|
+
var import_core_utils4 = require("@lucid-evolution/core-utils");
|
|
916
929
|
function valueToAssets(value) {
|
|
917
930
|
const assets = {};
|
|
918
931
|
assets["lovelace"] = value.coin();
|
|
919
|
-
|
|
920
|
-
|
|
932
|
+
if (value.has_multiassets()) {
|
|
933
|
+
const ma = value.multi_asset();
|
|
921
934
|
const multiAssets = ma.keys();
|
|
922
935
|
for (let j = 0; j < multiAssets.len(); j++) {
|
|
923
936
|
const policy = multiAssets.get(j);
|
|
@@ -926,7 +939,7 @@ function valueToAssets(value) {
|
|
|
926
939
|
for (let k = 0; k < assetNames.len(); k++) {
|
|
927
940
|
const policyAsset = assetNames.get(k);
|
|
928
941
|
const quantity = policyAssets.get(policyAsset);
|
|
929
|
-
const unit = policy.to_hex() + policyAsset.
|
|
942
|
+
const unit = policy.to_hex() + (0, import_core_utils4.fromText)(policyAsset.to_str());
|
|
930
943
|
assets[unit] = quantity;
|
|
931
944
|
}
|
|
932
945
|
}
|
|
@@ -947,7 +960,7 @@ function assetsToValue(assets) {
|
|
|
947
960
|
const assetsValue = CML.MapAssetNameToCoin.new();
|
|
948
961
|
for (const unit of policyUnits) {
|
|
949
962
|
assetsValue.insert(
|
|
950
|
-
CML.AssetName.from_str((0,
|
|
963
|
+
CML.AssetName.from_str((0, import_core_utils4.toText)(unit.slice(56))),
|
|
951
964
|
BigInt(assets[unit])
|
|
952
965
|
);
|
|
953
966
|
}
|
|
@@ -1035,7 +1048,6 @@ function coreToUtxo(coreUtxo) {
|
|
|
1035
1048
|
...coreToOutRef(CML.TransactionInput.from_cbor_hex(coreUtxo.to_cbor_hex())),
|
|
1036
1049
|
...coreToTxOutput(out)
|
|
1037
1050
|
};
|
|
1038
|
-
out.free();
|
|
1039
1051
|
return utxo;
|
|
1040
1052
|
}
|
|
1041
1053
|
function coresToUtxos(utxos) {
|
|
@@ -1074,12 +1086,54 @@ function coresToTxOutputs(outputs) {
|
|
|
1074
1086
|
}
|
|
1075
1087
|
return result;
|
|
1076
1088
|
}
|
|
1089
|
+
var selectUTxOs = (utxos, totalAssets) => {
|
|
1090
|
+
const selectedUtxos = [];
|
|
1091
|
+
let isSelected = false;
|
|
1092
|
+
const assetsRequired = new Map(Object.entries(totalAssets));
|
|
1093
|
+
const sortedUtxos = sortUTxOs(utxos);
|
|
1094
|
+
for (const utxo of sortedUtxos) {
|
|
1095
|
+
isSelected = false;
|
|
1096
|
+
for (const [unit, amount] of assetsRequired) {
|
|
1097
|
+
if (Object.hasOwn(utxo.assets, unit)) {
|
|
1098
|
+
const utxoAmount = utxo.assets[unit];
|
|
1099
|
+
if (utxoAmount >= amount) {
|
|
1100
|
+
assetsRequired.delete(unit);
|
|
1101
|
+
} else {
|
|
1102
|
+
assetsRequired.set(unit, amount - utxoAmount);
|
|
1103
|
+
}
|
|
1104
|
+
isSelected = true;
|
|
1105
|
+
}
|
|
1106
|
+
}
|
|
1107
|
+
if (isSelected) {
|
|
1108
|
+
selectedUtxos.push(utxo);
|
|
1109
|
+
}
|
|
1110
|
+
if (assetsRequired.size == 0) {
|
|
1111
|
+
break;
|
|
1112
|
+
}
|
|
1113
|
+
}
|
|
1114
|
+
if (assetsRequired.size > 0)
|
|
1115
|
+
return [];
|
|
1116
|
+
return selectedUtxos;
|
|
1117
|
+
};
|
|
1118
|
+
var sortUTxOs = (utxos, order = "descending") => {
|
|
1119
|
+
return utxos.toSorted((a, b) => {
|
|
1120
|
+
if (a.assets["lovelace"] > b.assets["lovelace"]) {
|
|
1121
|
+
return order === "ascending" ? 1 : -1;
|
|
1122
|
+
}
|
|
1123
|
+
if (a.assets["lovelace"] < b.assets["lovelace"]) {
|
|
1124
|
+
return order === "ascending" ? -1 : 1;
|
|
1125
|
+
}
|
|
1126
|
+
return 0;
|
|
1127
|
+
});
|
|
1128
|
+
};
|
|
1129
|
+
var isEqualUTxO = (self, that) => self.txHash === that.txHash && self.outputIndex === that.outputIndex;
|
|
1077
1130
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1078
1131
|
0 && (module.exports = {
|
|
1079
1132
|
PROTOCOL_PARAMETERS_DEFAULT,
|
|
1080
1133
|
addAssets,
|
|
1081
1134
|
addressFromHexOrBech32,
|
|
1082
1135
|
applyDoubleCborEncoding,
|
|
1136
|
+
applyParamsToScript,
|
|
1083
1137
|
assetsToValue,
|
|
1084
1138
|
coreToOutRef,
|
|
1085
1139
|
coreToTxOutput,
|
|
@@ -1097,6 +1151,7 @@ function coresToTxOutputs(outputs) {
|
|
|
1097
1151
|
generatePrivateKey,
|
|
1098
1152
|
generateSeedPhrase,
|
|
1099
1153
|
getAddressDetails,
|
|
1154
|
+
isEqualUTxO,
|
|
1100
1155
|
keyHashToCredential,
|
|
1101
1156
|
mintingPolicyToId,
|
|
1102
1157
|
nativeFromJson,
|
|
@@ -1105,7 +1160,9 @@ function coresToTxOutputs(outputs) {
|
|
|
1105
1160
|
networkToId,
|
|
1106
1161
|
paymentCredentialOf,
|
|
1107
1162
|
scriptHashToCredential,
|
|
1163
|
+
selectUTxOs,
|
|
1108
1164
|
slotToUnixTime,
|
|
1165
|
+
sortUTxOs,
|
|
1109
1166
|
stakeCredentialOf,
|
|
1110
1167
|
toLabel,
|
|
1111
1168
|
toNativeScript,
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Native, Script, Network, Credential, RewardAddress, CertificateValidator, WithdrawalValidator, AddressDetails, CostModels, ProtocolParameters, Address, ScriptHash, KeyHash, Datum, DatumHash, PrivateKey, PublicKey, SpendingValidator, Validator, MintingPolicy, PolicyId, NativeScript, UnixTime, Slot, UTxO, OutRef, TxOutput, Assets, Unit } from '@lucid-evolution/core-types';
|
|
1
|
+
import { Native, Script, Network, Credential, RewardAddress, CertificateValidator, WithdrawalValidator, AddressDetails, CostModels, ProtocolParameters, Address, ScriptHash, KeyHash, Datum, DatumHash, PrivateKey, PublicKey, SpendingValidator, Validator, MintingPolicy, PolicyId, NativeScript, Exact, UnixTime, Slot, UTxO, OutRef, TxOutput, Assets, Unit } from '@lucid-evolution/core-types';
|
|
2
2
|
import * as CML from '@dcspark/cardano-multiplatform-lib-nodejs';
|
|
3
|
+
import { Data } from '@lucid-evolution/plutus';
|
|
3
4
|
|
|
4
5
|
declare const toNativeScript: (native: Native) => CML.NativeScript;
|
|
5
6
|
declare const nativeJSFromJson: (native: Native) => Script;
|
|
@@ -43,6 +44,7 @@ declare function nativeFromJson(nativeScript: Native): Script;
|
|
|
43
44
|
* It follows this Json format: https://github.com/input-output-hk/cardano-node/blob/master/doc/reference/simple-scripts.md
|
|
44
45
|
*/
|
|
45
46
|
declare function nativeScriptFromJson(nativeScript: NativeScript): Script;
|
|
47
|
+
declare function applyParamsToScript<T extends unknown[] = Data[]>(plutusScript: string, params: Exact<[...T]>, type?: T): string;
|
|
46
48
|
|
|
47
49
|
declare function unixTimeToSlot(network: Network, unixTime: UnixTime): Slot;
|
|
48
50
|
declare function slotToUnixTime(network: Network, slot: Slot): UnixTime;
|
|
@@ -54,9 +56,21 @@ declare function utxosToCores(utxos: UTxO[]): CML.TransactionUnspentOutput[];
|
|
|
54
56
|
declare function coreToUtxo(coreUtxo: CML.TransactionUnspentOutput): UTxO;
|
|
55
57
|
declare function coresToUtxos(utxos: CML.TransactionUnspentOutput[]): UTxO[];
|
|
56
58
|
declare function coreToOutRef(input: CML.TransactionInput): OutRef;
|
|
57
|
-
declare function coresToOutRefs(inputs:
|
|
59
|
+
declare function coresToOutRefs(inputs: CML.TransactionInput[]): OutRef[];
|
|
58
60
|
declare function coreToTxOutput(output: CML.TransactionOutput): TxOutput;
|
|
59
61
|
declare function coresToTxOutputs(outputs: CML.TransactionOutput[]): TxOutput[];
|
|
62
|
+
declare const selectUTxOs: (utxos: UTxO[], totalAssets: Assets) => UTxO[];
|
|
63
|
+
/**
|
|
64
|
+
* Sorts an array of UTXOs by the amount of "lovelace" in ascending or descending order.
|
|
65
|
+
*
|
|
66
|
+
* @param {UTxO[]} utxos - The array of UTXO objects to be sorted.
|
|
67
|
+
* @param {"ascending" | "descending"} [order="descending"] - The order in which to sort the UTXOs.
|
|
68
|
+
* Use "ascending" for ascending order and "descending" for descending order.
|
|
69
|
+
* @returns {UTxO[]} - The sorted array of UTXOs.
|
|
70
|
+
*
|
|
71
|
+
*/
|
|
72
|
+
declare const sortUTxOs: (utxos: UTxO[], order?: "ascending" | "descending") => UTxO[];
|
|
73
|
+
declare const isEqualUTxO: (self: UTxO, that: UTxO) => boolean;
|
|
60
74
|
|
|
61
75
|
declare function valueToAssets(value: CML.Value): Assets;
|
|
62
76
|
declare function assetsToValue(assets: Assets): CML.Value;
|
|
@@ -76,4 +90,4 @@ declare function fromUnit(unit: Unit): {
|
|
|
76
90
|
declare function toUnit(policyId: PolicyId, name?: string | null, label?: number | null): Unit;
|
|
77
91
|
declare function addAssets(...assets: Assets[]): Assets;
|
|
78
92
|
|
|
79
|
-
export { PROTOCOL_PARAMETERS_DEFAULT, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, assetsToValue, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumToHash, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, keyHashToCredential, mintingPolicyToId, nativeFromJson, nativeJSFromJson, nativeScriptFromJson, networkToId, paymentCredentialOf, scriptHashToCredential, slotToUnixTime, stakeCredentialOf, toLabel, toNativeScript, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
|
|
93
|
+
export { PROTOCOL_PARAMETERS_DEFAULT, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, assetsToValue, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumToHash, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, isEqualUTxO, keyHashToCredential, mintingPolicyToId, nativeFromJson, nativeJSFromJson, nativeScriptFromJson, networkToId, paymentCredentialOf, scriptHashToCredential, selectUTxOs, slotToUnixTime, sortUTxOs, stakeCredentialOf, toLabel, toNativeScript, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { Native, Script, Network, Credential, RewardAddress, CertificateValidator, WithdrawalValidator, AddressDetails, CostModels, ProtocolParameters, Address, ScriptHash, KeyHash, Datum, DatumHash, PrivateKey, PublicKey, SpendingValidator, Validator, MintingPolicy, PolicyId, NativeScript, UnixTime, Slot, UTxO, OutRef, TxOutput, Assets, Unit } from '@lucid-evolution/core-types';
|
|
1
|
+
import { Native, Script, Network, Credential, RewardAddress, CertificateValidator, WithdrawalValidator, AddressDetails, CostModels, ProtocolParameters, Address, ScriptHash, KeyHash, Datum, DatumHash, PrivateKey, PublicKey, SpendingValidator, Validator, MintingPolicy, PolicyId, NativeScript, Exact, UnixTime, Slot, UTxO, OutRef, TxOutput, Assets, Unit } from '@lucid-evolution/core-types';
|
|
2
2
|
import * as CML from '@dcspark/cardano-multiplatform-lib-nodejs';
|
|
3
|
+
import { Data } from '@lucid-evolution/plutus';
|
|
3
4
|
|
|
4
5
|
declare const toNativeScript: (native: Native) => CML.NativeScript;
|
|
5
6
|
declare const nativeJSFromJson: (native: Native) => Script;
|
|
@@ -43,6 +44,7 @@ declare function nativeFromJson(nativeScript: Native): Script;
|
|
|
43
44
|
* It follows this Json format: https://github.com/input-output-hk/cardano-node/blob/master/doc/reference/simple-scripts.md
|
|
44
45
|
*/
|
|
45
46
|
declare function nativeScriptFromJson(nativeScript: NativeScript): Script;
|
|
47
|
+
declare function applyParamsToScript<T extends unknown[] = Data[]>(plutusScript: string, params: Exact<[...T]>, type?: T): string;
|
|
46
48
|
|
|
47
49
|
declare function unixTimeToSlot(network: Network, unixTime: UnixTime): Slot;
|
|
48
50
|
declare function slotToUnixTime(network: Network, slot: Slot): UnixTime;
|
|
@@ -54,9 +56,21 @@ declare function utxosToCores(utxos: UTxO[]): CML.TransactionUnspentOutput[];
|
|
|
54
56
|
declare function coreToUtxo(coreUtxo: CML.TransactionUnspentOutput): UTxO;
|
|
55
57
|
declare function coresToUtxos(utxos: CML.TransactionUnspentOutput[]): UTxO[];
|
|
56
58
|
declare function coreToOutRef(input: CML.TransactionInput): OutRef;
|
|
57
|
-
declare function coresToOutRefs(inputs:
|
|
59
|
+
declare function coresToOutRefs(inputs: CML.TransactionInput[]): OutRef[];
|
|
58
60
|
declare function coreToTxOutput(output: CML.TransactionOutput): TxOutput;
|
|
59
61
|
declare function coresToTxOutputs(outputs: CML.TransactionOutput[]): TxOutput[];
|
|
62
|
+
declare const selectUTxOs: (utxos: UTxO[], totalAssets: Assets) => UTxO[];
|
|
63
|
+
/**
|
|
64
|
+
* Sorts an array of UTXOs by the amount of "lovelace" in ascending or descending order.
|
|
65
|
+
*
|
|
66
|
+
* @param {UTxO[]} utxos - The array of UTXO objects to be sorted.
|
|
67
|
+
* @param {"ascending" | "descending"} [order="descending"] - The order in which to sort the UTXOs.
|
|
68
|
+
* Use "ascending" for ascending order and "descending" for descending order.
|
|
69
|
+
* @returns {UTxO[]} - The sorted array of UTXOs.
|
|
70
|
+
*
|
|
71
|
+
*/
|
|
72
|
+
declare const sortUTxOs: (utxos: UTxO[], order?: "ascending" | "descending") => UTxO[];
|
|
73
|
+
declare const isEqualUTxO: (self: UTxO, that: UTxO) => boolean;
|
|
60
74
|
|
|
61
75
|
declare function valueToAssets(value: CML.Value): Assets;
|
|
62
76
|
declare function assetsToValue(assets: Assets): CML.Value;
|
|
@@ -76,4 +90,4 @@ declare function fromUnit(unit: Unit): {
|
|
|
76
90
|
declare function toUnit(policyId: PolicyId, name?: string | null, label?: number | null): Unit;
|
|
77
91
|
declare function addAssets(...assets: Assets[]): Assets;
|
|
78
92
|
|
|
79
|
-
export { PROTOCOL_PARAMETERS_DEFAULT, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, assetsToValue, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumToHash, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, keyHashToCredential, mintingPolicyToId, nativeFromJson, nativeJSFromJson, nativeScriptFromJson, networkToId, paymentCredentialOf, scriptHashToCredential, slotToUnixTime, stakeCredentialOf, toLabel, toNativeScript, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
|
|
93
|
+
export { PROTOCOL_PARAMETERS_DEFAULT, addAssets, addressFromHexOrBech32, applyDoubleCborEncoding, applyParamsToScript, assetsToValue, coreToOutRef, coreToTxOutput, coreToUtxo, coresToOutRefs, coresToTxOutputs, coresToUtxos, createCostModels, credentialToAddress, credentialToRewardAddress, datumToHash, fromLabel, fromScriptRef, fromUnit, generatePrivateKey, generateSeedPhrase, getAddressDetails, isEqualUTxO, keyHashToCredential, mintingPolicyToId, nativeFromJson, nativeJSFromJson, nativeScriptFromJson, networkToId, paymentCredentialOf, scriptHashToCredential, selectUTxOs, slotToUnixTime, sortUTxOs, stakeCredentialOf, toLabel, toNativeScript, toPublicKey, toScriptRef, toUnit, unixTimeToSlot, utxoToCore, utxoToTransactionInput, utxoToTransactionOutput, utxosToCores, validatorToAddress, validatorToRewardAddress, validatorToScriptHash, valueToAssets };
|
package/dist/index.js
CHANGED
|
@@ -68,6 +68,9 @@ var applyDoubleCborEncoding = (script) => {
|
|
|
68
68
|
};
|
|
69
69
|
|
|
70
70
|
// src/scripts.ts
|
|
71
|
+
import { Data } from "@lucid-evolution/plutus";
|
|
72
|
+
import * as UPLC from "@lucid-evolution/uplc";
|
|
73
|
+
import { fromHex as fromHex2, toHex as toHex2 } from "@lucid-evolution/core-utils";
|
|
71
74
|
function validatorToAddress(network, validator, stakeCredential) {
|
|
72
75
|
const validatorHash = validatorToScriptHash(validator);
|
|
73
76
|
if (stakeCredential) {
|
|
@@ -165,6 +168,12 @@ function nativeScriptFromJson(nativeScript) {
|
|
|
165
168
|
).to_cbor_hex()
|
|
166
169
|
};
|
|
167
170
|
}
|
|
171
|
+
function applyParamsToScript(plutusScript, params, type) {
|
|
172
|
+
const p = type ? Data.castTo(params, type) : params;
|
|
173
|
+
return toHex2(
|
|
174
|
+
UPLC.apply_params_to_script(fromHex2(Data.to(p)), fromHex2(plutusScript))
|
|
175
|
+
);
|
|
176
|
+
}
|
|
168
177
|
|
|
169
178
|
// src/address.ts
|
|
170
179
|
function addressFromHexOrBech32(address) {
|
|
@@ -798,7 +807,7 @@ function toPublicKey(privateKey) {
|
|
|
798
807
|
}
|
|
799
808
|
|
|
800
809
|
// src/label.ts
|
|
801
|
-
import { fromHex as
|
|
810
|
+
import { fromHex as fromHex3 } from "@lucid-evolution/core-utils";
|
|
802
811
|
import { crc8 } from "@lucid-evolution/crc8";
|
|
803
812
|
function toLabel(num) {
|
|
804
813
|
if (num < 0 || num > 65535) {
|
|
@@ -819,7 +828,7 @@ function fromLabel(label) {
|
|
|
819
828
|
return check === checksum(numHex) ? num : null;
|
|
820
829
|
}
|
|
821
830
|
function checksum(num) {
|
|
822
|
-
return crc8(
|
|
831
|
+
return crc8(fromHex3(num)).toString(16).padStart(2, "0");
|
|
823
832
|
}
|
|
824
833
|
|
|
825
834
|
// src/time.ts
|
|
@@ -836,12 +845,12 @@ function slotToUnixTime(network, slot) {
|
|
|
836
845
|
}
|
|
837
846
|
|
|
838
847
|
// src/value.ts
|
|
839
|
-
import { toText } from "@lucid-evolution/core-utils";
|
|
848
|
+
import { fromText, toText } from "@lucid-evolution/core-utils";
|
|
840
849
|
function valueToAssets(value) {
|
|
841
850
|
const assets = {};
|
|
842
851
|
assets["lovelace"] = value.coin();
|
|
843
|
-
|
|
844
|
-
|
|
852
|
+
if (value.has_multiassets()) {
|
|
853
|
+
const ma = value.multi_asset();
|
|
845
854
|
const multiAssets = ma.keys();
|
|
846
855
|
for (let j = 0; j < multiAssets.len(); j++) {
|
|
847
856
|
const policy = multiAssets.get(j);
|
|
@@ -850,7 +859,7 @@ function valueToAssets(value) {
|
|
|
850
859
|
for (let k = 0; k < assetNames.len(); k++) {
|
|
851
860
|
const policyAsset = assetNames.get(k);
|
|
852
861
|
const quantity = policyAssets.get(policyAsset);
|
|
853
|
-
const unit = policy.to_hex() + policyAsset.
|
|
862
|
+
const unit = policy.to_hex() + fromText(policyAsset.to_str());
|
|
854
863
|
assets[unit] = quantity;
|
|
855
864
|
}
|
|
856
865
|
}
|
|
@@ -959,7 +968,6 @@ function coreToUtxo(coreUtxo) {
|
|
|
959
968
|
...coreToOutRef(CML.TransactionInput.from_cbor_hex(coreUtxo.to_cbor_hex())),
|
|
960
969
|
...coreToTxOutput(out)
|
|
961
970
|
};
|
|
962
|
-
out.free();
|
|
963
971
|
return utxo;
|
|
964
972
|
}
|
|
965
973
|
function coresToUtxos(utxos) {
|
|
@@ -998,11 +1006,53 @@ function coresToTxOutputs(outputs) {
|
|
|
998
1006
|
}
|
|
999
1007
|
return result;
|
|
1000
1008
|
}
|
|
1009
|
+
var selectUTxOs = (utxos, totalAssets) => {
|
|
1010
|
+
const selectedUtxos = [];
|
|
1011
|
+
let isSelected = false;
|
|
1012
|
+
const assetsRequired = new Map(Object.entries(totalAssets));
|
|
1013
|
+
const sortedUtxos = sortUTxOs(utxos);
|
|
1014
|
+
for (const utxo of sortedUtxos) {
|
|
1015
|
+
isSelected = false;
|
|
1016
|
+
for (const [unit, amount] of assetsRequired) {
|
|
1017
|
+
if (Object.hasOwn(utxo.assets, unit)) {
|
|
1018
|
+
const utxoAmount = utxo.assets[unit];
|
|
1019
|
+
if (utxoAmount >= amount) {
|
|
1020
|
+
assetsRequired.delete(unit);
|
|
1021
|
+
} else {
|
|
1022
|
+
assetsRequired.set(unit, amount - utxoAmount);
|
|
1023
|
+
}
|
|
1024
|
+
isSelected = true;
|
|
1025
|
+
}
|
|
1026
|
+
}
|
|
1027
|
+
if (isSelected) {
|
|
1028
|
+
selectedUtxos.push(utxo);
|
|
1029
|
+
}
|
|
1030
|
+
if (assetsRequired.size == 0) {
|
|
1031
|
+
break;
|
|
1032
|
+
}
|
|
1033
|
+
}
|
|
1034
|
+
if (assetsRequired.size > 0)
|
|
1035
|
+
return [];
|
|
1036
|
+
return selectedUtxos;
|
|
1037
|
+
};
|
|
1038
|
+
var sortUTxOs = (utxos, order = "descending") => {
|
|
1039
|
+
return utxos.toSorted((a, b) => {
|
|
1040
|
+
if (a.assets["lovelace"] > b.assets["lovelace"]) {
|
|
1041
|
+
return order === "ascending" ? 1 : -1;
|
|
1042
|
+
}
|
|
1043
|
+
if (a.assets["lovelace"] < b.assets["lovelace"]) {
|
|
1044
|
+
return order === "ascending" ? -1 : 1;
|
|
1045
|
+
}
|
|
1046
|
+
return 0;
|
|
1047
|
+
});
|
|
1048
|
+
};
|
|
1049
|
+
var isEqualUTxO = (self, that) => self.txHash === that.txHash && self.outputIndex === that.outputIndex;
|
|
1001
1050
|
export {
|
|
1002
1051
|
PROTOCOL_PARAMETERS_DEFAULT,
|
|
1003
1052
|
addAssets,
|
|
1004
1053
|
addressFromHexOrBech32,
|
|
1005
1054
|
applyDoubleCborEncoding,
|
|
1055
|
+
applyParamsToScript,
|
|
1006
1056
|
assetsToValue,
|
|
1007
1057
|
coreToOutRef,
|
|
1008
1058
|
coreToTxOutput,
|
|
@@ -1020,6 +1070,7 @@ export {
|
|
|
1020
1070
|
generatePrivateKey,
|
|
1021
1071
|
generateSeedPhrase,
|
|
1022
1072
|
getAddressDetails,
|
|
1073
|
+
isEqualUTxO,
|
|
1023
1074
|
keyHashToCredential,
|
|
1024
1075
|
mintingPolicyToId,
|
|
1025
1076
|
nativeFromJson,
|
|
@@ -1028,7 +1079,9 @@ export {
|
|
|
1028
1079
|
networkToId,
|
|
1029
1080
|
paymentCredentialOf,
|
|
1030
1081
|
scriptHashToCredential,
|
|
1082
|
+
selectUTxOs,
|
|
1031
1083
|
slotToUnixTime,
|
|
1084
|
+
sortUTxOs,
|
|
1032
1085
|
stakeCredentialOf,
|
|
1033
1086
|
toLabel,
|
|
1034
1087
|
toNativeScript,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@lucid-evolution/utils",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.12",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.js",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
@@ -32,7 +32,8 @@
|
|
|
32
32
|
"@lucid-evolution/core-types": "0.1.6",
|
|
33
33
|
"@lucid-evolution/core-utils": "0.1.6",
|
|
34
34
|
"@lucid-evolution/crc8": "0.1.6",
|
|
35
|
-
"@lucid-evolution/plutus": "0.1.9"
|
|
35
|
+
"@lucid-evolution/plutus": "0.1.9",
|
|
36
|
+
"@lucid-evolution/uplc": "0.2.5"
|
|
36
37
|
},
|
|
37
38
|
"devDependencies": {
|
|
38
39
|
"@types/node": "^20.12.8",
|