@meshsdk/core-csl 1.7.15 → 1.7.16
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 +138 -0
- package/dist/index.d.cts +87 -2
- package/dist/index.d.ts +87 -2
- package/dist/index.js +135 -0
- package/package.json +4 -3
package/dist/index.cjs
CHANGED
|
@@ -32,6 +32,7 @@ var src_exports = {};
|
|
|
32
32
|
__export(src_exports, {
|
|
33
33
|
CSLSerializer: () => CSLSerializer,
|
|
34
34
|
LANGUAGE_VERSIONS: () => LANGUAGE_VERSIONS,
|
|
35
|
+
OfflineEvaluator: () => OfflineEvaluator,
|
|
35
36
|
POLICY_ID_LENGTH: () => POLICY_ID_LENGTH,
|
|
36
37
|
REDEEMER_TAGS: () => REDEEMER_TAGS,
|
|
37
38
|
addrBech32ToHex: () => addrBech32ToHex,
|
|
@@ -65,9 +66,11 @@ __export(src_exports, {
|
|
|
65
66
|
deserializeTxUnspentOutput: () => deserializeTxUnspentOutput,
|
|
66
67
|
deserializeTxWitnessSet: () => deserializeTxWitnessSet,
|
|
67
68
|
deserializeValue: () => deserializeValue,
|
|
69
|
+
evaluateTransaction: () => evaluateTransaction,
|
|
68
70
|
fromBytes: () => fromBytes,
|
|
69
71
|
fromLovelace: () => fromLovelace,
|
|
70
72
|
fromUTF8: () => fromUTF8,
|
|
73
|
+
getTransactionInputs: () => getTransactionInputs,
|
|
71
74
|
getV2ScriptHash: () => getV2ScriptHash,
|
|
72
75
|
keyHashToRewardAddress: () => keyHashToRewardAddress,
|
|
73
76
|
meshTxBuilderBodyToObj: () => meshTxBuilderBodyToObj,
|
|
@@ -524,6 +527,86 @@ var signTransaction = (txHex, signingKeys) => {
|
|
|
524
527
|
const result = csl.js_sign_transaction(txHex, cslSigningKeys);
|
|
525
528
|
return parseWasmResult(result);
|
|
526
529
|
};
|
|
530
|
+
var evaluateTransaction = (txHex, resolvedUtxos, network) => {
|
|
531
|
+
const additionalTxs = csl.JsVecString.new();
|
|
532
|
+
const mappedUtxos = csl.JsVecString.new();
|
|
533
|
+
for (const utxo of resolvedUtxos) {
|
|
534
|
+
mappedUtxos.add(JSON.stringify(utxo));
|
|
535
|
+
}
|
|
536
|
+
const result = csl.evaluate_tx_scripts_js(txHex, mappedUtxos, additionalTxs, network);
|
|
537
|
+
const unwrappedResult = parseWasmResult(result);
|
|
538
|
+
try {
|
|
539
|
+
const actions = JSON.parse(unwrappedResult);
|
|
540
|
+
return actions.map(mapAction);
|
|
541
|
+
} catch (e) {
|
|
542
|
+
throw new Error("Cannot parse result from evaluate_tx_scripts_js. Expected Action[] type");
|
|
543
|
+
}
|
|
544
|
+
};
|
|
545
|
+
var mapAction = (action) => {
|
|
546
|
+
return {
|
|
547
|
+
index: action.index,
|
|
548
|
+
budget: mapBudget(action.budget),
|
|
549
|
+
tag: mapRedeemerTag(action.tag)
|
|
550
|
+
};
|
|
551
|
+
};
|
|
552
|
+
var mapBudget = (budget) => {
|
|
553
|
+
return {
|
|
554
|
+
mem: budget.mem,
|
|
555
|
+
steps: budget.steps
|
|
556
|
+
};
|
|
557
|
+
};
|
|
558
|
+
var mapRedeemerTag = (tag) => {
|
|
559
|
+
switch (tag) {
|
|
560
|
+
case "cert":
|
|
561
|
+
return "CERT";
|
|
562
|
+
case "mint":
|
|
563
|
+
return "MINT";
|
|
564
|
+
case "reward":
|
|
565
|
+
return "REWARD";
|
|
566
|
+
case "spend":
|
|
567
|
+
return "SPEND";
|
|
568
|
+
case "vote":
|
|
569
|
+
return "VOTE";
|
|
570
|
+
case "propose":
|
|
571
|
+
return "PROPOSE";
|
|
572
|
+
default:
|
|
573
|
+
throw new Error(`Unknown RedeemerTag: ${tag}`);
|
|
574
|
+
}
|
|
575
|
+
};
|
|
576
|
+
var getTransactionInputs = (txHex) => {
|
|
577
|
+
const inputs = [];
|
|
578
|
+
const deserializedTx = deserializeTx(txHex);
|
|
579
|
+
const body = deserializedTx.body();
|
|
580
|
+
const cslInputs = body.inputs();
|
|
581
|
+
for (let i = 0; i < cslInputs.len(); i++) {
|
|
582
|
+
const input = cslInputs.get(i);
|
|
583
|
+
inputs.push({
|
|
584
|
+
txHash: input.transaction_id().to_hex(),
|
|
585
|
+
index: input.index()
|
|
586
|
+
});
|
|
587
|
+
}
|
|
588
|
+
const cslCollaterals = body.collateral();
|
|
589
|
+
if (cslCollaterals) {
|
|
590
|
+
for (let i = 0; i < cslCollaterals.len(); i++) {
|
|
591
|
+
const collateral = cslCollaterals.get(i);
|
|
592
|
+
inputs.push({
|
|
593
|
+
txHash: collateral.transaction_id().to_hex(),
|
|
594
|
+
index: collateral.index()
|
|
595
|
+
});
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
const cslRefInputs = body.reference_inputs();
|
|
599
|
+
if (cslRefInputs) {
|
|
600
|
+
for (let i = 0; i < cslRefInputs.len(); i++) {
|
|
601
|
+
const refInput = cslRefInputs.get(i);
|
|
602
|
+
inputs.push({
|
|
603
|
+
txHash: refInput.transaction_id().to_hex(),
|
|
604
|
+
index: refInput.index()
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
}
|
|
608
|
+
return inputs;
|
|
609
|
+
};
|
|
527
610
|
|
|
528
611
|
// src/utils/aiken.ts
|
|
529
612
|
var applyParamsToScript = (rawScript, params, type = "Mesh") => {
|
|
@@ -1298,10 +1381,63 @@ var CSLSerializer = class {
|
|
|
1298
1381
|
}
|
|
1299
1382
|
};
|
|
1300
1383
|
};
|
|
1384
|
+
|
|
1385
|
+
// src/offline-providers/offline-evaluator.ts
|
|
1386
|
+
var OfflineEvaluator = class {
|
|
1387
|
+
fetcher;
|
|
1388
|
+
network;
|
|
1389
|
+
/**
|
|
1390
|
+
* Creates a new instance of OfflineEvaluator.
|
|
1391
|
+
* @param fetcher - An implementation of IFetcher to resolve transaction UTXOs
|
|
1392
|
+
* @param network - The network to evaluate scripts for
|
|
1393
|
+
*/
|
|
1394
|
+
constructor(fetcher, network) {
|
|
1395
|
+
this.fetcher = fetcher;
|
|
1396
|
+
this.network = network;
|
|
1397
|
+
}
|
|
1398
|
+
/**
|
|
1399
|
+
* Evaluates Plutus scripts in a transaction by resolving its input UTXOs and calculating execution costs.
|
|
1400
|
+
*
|
|
1401
|
+
* The method performs these steps:
|
|
1402
|
+
* 1. Extracts input references from the transaction
|
|
1403
|
+
* 2. Resolves the corresponding UTXOs using the fetcher
|
|
1404
|
+
* 3. Verifies all required UTXOs are available
|
|
1405
|
+
* 4. Evaluates each Plutus script to determine its memory and CPU costs
|
|
1406
|
+
*
|
|
1407
|
+
* @param tx - Transaction in CBOR hex format
|
|
1408
|
+
* @returns Promise resolving to array of script evaluation results, each containing:
|
|
1409
|
+
* - tag: Type of script (CERT | MINT | REWARD | SPEND | VOTE | PROPOSE)
|
|
1410
|
+
* - index: Script execution index
|
|
1411
|
+
* - budget: Memory units and CPU steps required
|
|
1412
|
+
* @throws Error if any required UTXOs cannot be resolved or if script evaluation fails
|
|
1413
|
+
*/
|
|
1414
|
+
async evaluateTx(tx) {
|
|
1415
|
+
const inputsToResolve = getTransactionInputs(tx);
|
|
1416
|
+
const txHashesSet = new Set(inputsToResolve.map((input) => input.txHash));
|
|
1417
|
+
const resolvedUTXOs = [];
|
|
1418
|
+
for (const txHash of txHashesSet) {
|
|
1419
|
+
const utxos = await this.fetcher.fetchUTxOs(txHash);
|
|
1420
|
+
for (const utxo of utxos) {
|
|
1421
|
+
if (utxo) {
|
|
1422
|
+
if (inputsToResolve.find((input) => input.txHash === txHash && input.index === utxo.input.outputIndex)) {
|
|
1423
|
+
resolvedUTXOs.push(utxo);
|
|
1424
|
+
}
|
|
1425
|
+
}
|
|
1426
|
+
}
|
|
1427
|
+
}
|
|
1428
|
+
if (resolvedUTXOs.length !== inputsToResolve.length) {
|
|
1429
|
+
const missing = inputsToResolve.filter((input) => !resolvedUTXOs.find((utxo) => utxo.input.txHash === input.txHash && utxo.input.outputIndex === input.index));
|
|
1430
|
+
const missingList = missing.map((m) => `${m.txHash}:${m.index}`).join(", ");
|
|
1431
|
+
throw new Error(`Can't resolve these UTXOs to execute plutus scripts: ${missingList}`);
|
|
1432
|
+
}
|
|
1433
|
+
return evaluateTransaction(tx, resolvedUTXOs, this.network);
|
|
1434
|
+
}
|
|
1435
|
+
};
|
|
1301
1436
|
// Annotate the CommonJS export names for ESM import in node:
|
|
1302
1437
|
0 && (module.exports = {
|
|
1303
1438
|
CSLSerializer,
|
|
1304
1439
|
LANGUAGE_VERSIONS,
|
|
1440
|
+
OfflineEvaluator,
|
|
1305
1441
|
POLICY_ID_LENGTH,
|
|
1306
1442
|
REDEEMER_TAGS,
|
|
1307
1443
|
addrBech32ToHex,
|
|
@@ -1335,9 +1471,11 @@ var CSLSerializer = class {
|
|
|
1335
1471
|
deserializeTxUnspentOutput,
|
|
1336
1472
|
deserializeTxWitnessSet,
|
|
1337
1473
|
deserializeValue,
|
|
1474
|
+
evaluateTransaction,
|
|
1338
1475
|
fromBytes,
|
|
1339
1476
|
fromLovelace,
|
|
1340
1477
|
fromUTF8,
|
|
1478
|
+
getTransactionInputs,
|
|
1341
1479
|
getV2ScriptHash,
|
|
1342
1480
|
keyHashToRewardAddress,
|
|
1343
1481
|
meshTxBuilderBodyToObj,
|
package/dist/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _meshsdk_common from '@meshsdk/common';
|
|
2
|
-
import { DeserializedAddress, PubKeyAddress, ScriptAddress, Data, IMeshTxSerializer, Protocol, MeshTxBuilderBody, BuilderData, IDeserializer, IResolver, Certificate, CertificateType, PoolParams, PoolMetadata, Relay, Redeemer, MintItem, Output, ScriptSource, SimpleScriptSourceInfo, TxIn, TxInParameter, ScriptTxInParameter, SimpleScriptTxInParameter,
|
|
2
|
+
import { DeserializedAddress, PubKeyAddress, ScriptAddress, UTxO, Network, Action, Data, IMeshTxSerializer, Protocol, MeshTxBuilderBody, BuilderData, IDeserializer, IResolver, Certificate, CertificateType, PoolParams, PoolMetadata, Relay, Redeemer, MintItem, Output, ScriptSource, SimpleScriptSourceInfo, TxIn, TxInParameter, ScriptTxInParameter, SimpleScriptTxInParameter, Withdrawal, LanguageVersion, PlutusScript, NativeScript, IEvaluator, IFetcher } from '@meshsdk/common';
|
|
3
3
|
import * as csl from '@sidan-lab/sidan-csl-rs-nodejs';
|
|
4
4
|
export { csl };
|
|
5
5
|
|
|
@@ -30,6 +30,11 @@ declare const keyHashToRewardAddress: (keyHashHex: string, network?: number) =>
|
|
|
30
30
|
|
|
31
31
|
declare const calculateTxHash: (txHex: string) => string;
|
|
32
32
|
declare const signTransaction: (txHex: string, signingKeys: string[]) => string;
|
|
33
|
+
declare const evaluateTransaction: (txHex: string, resolvedUtxos: UTxO[], network: Network) => Omit<Action, "data">[];
|
|
34
|
+
declare const getTransactionInputs: (txHex: string) => {
|
|
35
|
+
txHash: string;
|
|
36
|
+
index: number;
|
|
37
|
+
}[];
|
|
33
38
|
|
|
34
39
|
/**
|
|
35
40
|
* Apply parameters to a given script blueprint.
|
|
@@ -174,4 +179,84 @@ declare const serializePoolId: (hash: string) => string;
|
|
|
174
179
|
declare const resolveScriptRef: (script: PlutusScript | NativeScript) => string;
|
|
175
180
|
declare const resolveEd25519KeyHash: (bech32: string) => string;
|
|
176
181
|
|
|
177
|
-
|
|
182
|
+
/**
|
|
183
|
+
* OfflineEvaluator implements the IEvaluator interface to provide offline evaluation of Plutus scripts.
|
|
184
|
+
* This class evaluates Plutus scripts contained in Cardano transactions without requiring network connectivity,
|
|
185
|
+
* determining their execution costs in terms of memory and CPU steps.
|
|
186
|
+
*
|
|
187
|
+
* Each script evaluation returns an Action object (excluding the redeemer data) that contains:
|
|
188
|
+
* - tag: The type of script being executed (CERT | MINT | REWARD | SPEND | VOTE | PROPOSE)
|
|
189
|
+
* - index: Execution index of the script within the transaction
|
|
190
|
+
* - budget: Execution costs including:
|
|
191
|
+
* - mem: Memory units required
|
|
192
|
+
* - steps: CPU steps required
|
|
193
|
+
*
|
|
194
|
+
* Example usage:
|
|
195
|
+
* ```typescript
|
|
196
|
+
* import { OfflineEvaluator, OfflineFetcher } from '@meshsdk/core';
|
|
197
|
+
*
|
|
198
|
+
* // Create fetcher and evaluator instances
|
|
199
|
+
* const fetcher = new OfflineFetcher();
|
|
200
|
+
* const evaluator = new OfflineEvaluator(fetcher, 'preprod');
|
|
201
|
+
*
|
|
202
|
+
* // Add required UTXOs that the transaction references
|
|
203
|
+
* fetcher.addUTxOs([
|
|
204
|
+
* {
|
|
205
|
+
* input: {
|
|
206
|
+
* txHash: "1234...",
|
|
207
|
+
* outputIndex: 0
|
|
208
|
+
* },
|
|
209
|
+
* output: {
|
|
210
|
+
* address: "addr1...",
|
|
211
|
+
* amount: [{ unit: "lovelace", quantity: "1000000" }],
|
|
212
|
+
* scriptHash: "abcd..." // If this is a script UTXO
|
|
213
|
+
* }
|
|
214
|
+
* }
|
|
215
|
+
* ]);
|
|
216
|
+
*
|
|
217
|
+
* // Evaluate Plutus scripts in a transaction
|
|
218
|
+
* try {
|
|
219
|
+
* const actions = await evaluator.evaluateTx(transactionCbor);
|
|
220
|
+
* // Example result for a minting script:
|
|
221
|
+
* // [{
|
|
222
|
+
* // index: 0,
|
|
223
|
+
* // tag: "MINT",
|
|
224
|
+
* // budget: {
|
|
225
|
+
* // mem: 508703, // Memory units used
|
|
226
|
+
* // steps: 164980381 // CPU steps used
|
|
227
|
+
* // }
|
|
228
|
+
* // }]
|
|
229
|
+
* } catch (error) {
|
|
230
|
+
* console.error('Plutus script evaluation failed:', error);
|
|
231
|
+
* }
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
declare class OfflineEvaluator implements IEvaluator {
|
|
235
|
+
private readonly fetcher;
|
|
236
|
+
private readonly network;
|
|
237
|
+
/**
|
|
238
|
+
* Creates a new instance of OfflineEvaluator.
|
|
239
|
+
* @param fetcher - An implementation of IFetcher to resolve transaction UTXOs
|
|
240
|
+
* @param network - The network to evaluate scripts for
|
|
241
|
+
*/
|
|
242
|
+
constructor(fetcher: IFetcher, network: Network);
|
|
243
|
+
/**
|
|
244
|
+
* Evaluates Plutus scripts in a transaction by resolving its input UTXOs and calculating execution costs.
|
|
245
|
+
*
|
|
246
|
+
* The method performs these steps:
|
|
247
|
+
* 1. Extracts input references from the transaction
|
|
248
|
+
* 2. Resolves the corresponding UTXOs using the fetcher
|
|
249
|
+
* 3. Verifies all required UTXOs are available
|
|
250
|
+
* 4. Evaluates each Plutus script to determine its memory and CPU costs
|
|
251
|
+
*
|
|
252
|
+
* @param tx - Transaction in CBOR hex format
|
|
253
|
+
* @returns Promise resolving to array of script evaluation results, each containing:
|
|
254
|
+
* - tag: Type of script (CERT | MINT | REWARD | SPEND | VOTE | PROPOSE)
|
|
255
|
+
* - index: Script execution index
|
|
256
|
+
* - budget: Memory units and CPU steps required
|
|
257
|
+
* @throws Error if any required UTXOs cannot be resolved or if script evaluation fails
|
|
258
|
+
*/
|
|
259
|
+
evaluateTx(tx: string): Promise<Omit<Action, "data">[]>;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export { CSLSerializer, LANGUAGE_VERSIONS, OfflineEvaluator, POLICY_ID_LENGTH, REDEEMER_TAGS, addrBech32ToHex, addrBech32ToObj, applyCborEncoding, applyParamsToScript, baseAddressToStakeAddress, baseCertToObj, builderDataToCbor, calculateTxHash, castDataToPlutusData, castRawDataToJsonString, certificateToObj, collateralTxInToObj, deserializeAddress, deserializeBech32Address, deserializeBip32PrivateKey, deserializeDataHash, deserializeEd25519KeyHash, deserializeEd25519Signature, deserializeNativeScript, deserializePlutusData, deserializePlutusScript, deserializePublicKey, deserializeScriptHash, deserializeScriptRef, deserializeTx, deserializeTxBody, deserializeTxHash, deserializeTxUnspentOutput, deserializeTxWitnessSet, deserializeValue, evaluateTransaction, fromBytes, fromLovelace, fromUTF8, getTransactionInputs, getV2ScriptHash, keyHashToRewardAddress, meshTxBuilderBodyToObj, mintItemToObj, mintParametersObj, nativeMintItemToObj, outputToObj, parseDatumCbor, parseInlineDatum, plutusMintItemToObj, poolIdBech32ToHex, poolIdHexToBech32, poolMetadataToObj, poolParamsToObj, redeemerToObj, relayToObj, resolveDataHash, resolveEd25519KeyHash, resolveNativeScriptAddress, resolveNativeScriptHash, resolveNativeScriptHex, resolvePlutusScriptAddress, resolvePrivateKey, resolveRewardAddress, resolveScriptHashDRepId, resolveScriptRef, resolveStakeKeyHash, rewardAddressToKeyHash, scriptHashToBech32, scriptHashToRewardAddress, scriptSourceToObj, scriptTxInParameterToObj, serializeAddressObj, serializePlutusAddressToBech32, serializePoolId, serialzeAddress, signTransaction, simpleScriptSourceToObj, simpleScriptTxInParameterToObj, skeyToPubKeyHash, toAddress, toBaseAddress, toBytes, toEnterpriseAddress, toLovelace, toNativeScript, toPlutusData, toRewardAddress, toScriptRef, toUTF8, txInParameterToObj, txInToObj, utxoToObj, v2ScriptToBech32, withdrawalToObj };
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import * as _meshsdk_common from '@meshsdk/common';
|
|
2
|
-
import { DeserializedAddress, PubKeyAddress, ScriptAddress, Data, IMeshTxSerializer, Protocol, MeshTxBuilderBody, BuilderData, IDeserializer, IResolver, Certificate, CertificateType, PoolParams, PoolMetadata, Relay, Redeemer, MintItem, Output, ScriptSource, SimpleScriptSourceInfo, TxIn, TxInParameter, ScriptTxInParameter, SimpleScriptTxInParameter,
|
|
2
|
+
import { DeserializedAddress, PubKeyAddress, ScriptAddress, UTxO, Network, Action, Data, IMeshTxSerializer, Protocol, MeshTxBuilderBody, BuilderData, IDeserializer, IResolver, Certificate, CertificateType, PoolParams, PoolMetadata, Relay, Redeemer, MintItem, Output, ScriptSource, SimpleScriptSourceInfo, TxIn, TxInParameter, ScriptTxInParameter, SimpleScriptTxInParameter, Withdrawal, LanguageVersion, PlutusScript, NativeScript, IEvaluator, IFetcher } from '@meshsdk/common';
|
|
3
3
|
import * as csl from '@sidan-lab/sidan-csl-rs-nodejs';
|
|
4
4
|
export { csl };
|
|
5
5
|
|
|
@@ -30,6 +30,11 @@ declare const keyHashToRewardAddress: (keyHashHex: string, network?: number) =>
|
|
|
30
30
|
|
|
31
31
|
declare const calculateTxHash: (txHex: string) => string;
|
|
32
32
|
declare const signTransaction: (txHex: string, signingKeys: string[]) => string;
|
|
33
|
+
declare const evaluateTransaction: (txHex: string, resolvedUtxos: UTxO[], network: Network) => Omit<Action, "data">[];
|
|
34
|
+
declare const getTransactionInputs: (txHex: string) => {
|
|
35
|
+
txHash: string;
|
|
36
|
+
index: number;
|
|
37
|
+
}[];
|
|
33
38
|
|
|
34
39
|
/**
|
|
35
40
|
* Apply parameters to a given script blueprint.
|
|
@@ -174,4 +179,84 @@ declare const serializePoolId: (hash: string) => string;
|
|
|
174
179
|
declare const resolveScriptRef: (script: PlutusScript | NativeScript) => string;
|
|
175
180
|
declare const resolveEd25519KeyHash: (bech32: string) => string;
|
|
176
181
|
|
|
177
|
-
|
|
182
|
+
/**
|
|
183
|
+
* OfflineEvaluator implements the IEvaluator interface to provide offline evaluation of Plutus scripts.
|
|
184
|
+
* This class evaluates Plutus scripts contained in Cardano transactions without requiring network connectivity,
|
|
185
|
+
* determining their execution costs in terms of memory and CPU steps.
|
|
186
|
+
*
|
|
187
|
+
* Each script evaluation returns an Action object (excluding the redeemer data) that contains:
|
|
188
|
+
* - tag: The type of script being executed (CERT | MINT | REWARD | SPEND | VOTE | PROPOSE)
|
|
189
|
+
* - index: Execution index of the script within the transaction
|
|
190
|
+
* - budget: Execution costs including:
|
|
191
|
+
* - mem: Memory units required
|
|
192
|
+
* - steps: CPU steps required
|
|
193
|
+
*
|
|
194
|
+
* Example usage:
|
|
195
|
+
* ```typescript
|
|
196
|
+
* import { OfflineEvaluator, OfflineFetcher } from '@meshsdk/core';
|
|
197
|
+
*
|
|
198
|
+
* // Create fetcher and evaluator instances
|
|
199
|
+
* const fetcher = new OfflineFetcher();
|
|
200
|
+
* const evaluator = new OfflineEvaluator(fetcher, 'preprod');
|
|
201
|
+
*
|
|
202
|
+
* // Add required UTXOs that the transaction references
|
|
203
|
+
* fetcher.addUTxOs([
|
|
204
|
+
* {
|
|
205
|
+
* input: {
|
|
206
|
+
* txHash: "1234...",
|
|
207
|
+
* outputIndex: 0
|
|
208
|
+
* },
|
|
209
|
+
* output: {
|
|
210
|
+
* address: "addr1...",
|
|
211
|
+
* amount: [{ unit: "lovelace", quantity: "1000000" }],
|
|
212
|
+
* scriptHash: "abcd..." // If this is a script UTXO
|
|
213
|
+
* }
|
|
214
|
+
* }
|
|
215
|
+
* ]);
|
|
216
|
+
*
|
|
217
|
+
* // Evaluate Plutus scripts in a transaction
|
|
218
|
+
* try {
|
|
219
|
+
* const actions = await evaluator.evaluateTx(transactionCbor);
|
|
220
|
+
* // Example result for a minting script:
|
|
221
|
+
* // [{
|
|
222
|
+
* // index: 0,
|
|
223
|
+
* // tag: "MINT",
|
|
224
|
+
* // budget: {
|
|
225
|
+
* // mem: 508703, // Memory units used
|
|
226
|
+
* // steps: 164980381 // CPU steps used
|
|
227
|
+
* // }
|
|
228
|
+
* // }]
|
|
229
|
+
* } catch (error) {
|
|
230
|
+
* console.error('Plutus script evaluation failed:', error);
|
|
231
|
+
* }
|
|
232
|
+
* ```
|
|
233
|
+
*/
|
|
234
|
+
declare class OfflineEvaluator implements IEvaluator {
|
|
235
|
+
private readonly fetcher;
|
|
236
|
+
private readonly network;
|
|
237
|
+
/**
|
|
238
|
+
* Creates a new instance of OfflineEvaluator.
|
|
239
|
+
* @param fetcher - An implementation of IFetcher to resolve transaction UTXOs
|
|
240
|
+
* @param network - The network to evaluate scripts for
|
|
241
|
+
*/
|
|
242
|
+
constructor(fetcher: IFetcher, network: Network);
|
|
243
|
+
/**
|
|
244
|
+
* Evaluates Plutus scripts in a transaction by resolving its input UTXOs and calculating execution costs.
|
|
245
|
+
*
|
|
246
|
+
* The method performs these steps:
|
|
247
|
+
* 1. Extracts input references from the transaction
|
|
248
|
+
* 2. Resolves the corresponding UTXOs using the fetcher
|
|
249
|
+
* 3. Verifies all required UTXOs are available
|
|
250
|
+
* 4. Evaluates each Plutus script to determine its memory and CPU costs
|
|
251
|
+
*
|
|
252
|
+
* @param tx - Transaction in CBOR hex format
|
|
253
|
+
* @returns Promise resolving to array of script evaluation results, each containing:
|
|
254
|
+
* - tag: Type of script (CERT | MINT | REWARD | SPEND | VOTE | PROPOSE)
|
|
255
|
+
* - index: Script execution index
|
|
256
|
+
* - budget: Memory units and CPU steps required
|
|
257
|
+
* @throws Error if any required UTXOs cannot be resolved or if script evaluation fails
|
|
258
|
+
*/
|
|
259
|
+
evaluateTx(tx: string): Promise<Omit<Action, "data">[]>;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
export { CSLSerializer, LANGUAGE_VERSIONS, OfflineEvaluator, POLICY_ID_LENGTH, REDEEMER_TAGS, addrBech32ToHex, addrBech32ToObj, applyCborEncoding, applyParamsToScript, baseAddressToStakeAddress, baseCertToObj, builderDataToCbor, calculateTxHash, castDataToPlutusData, castRawDataToJsonString, certificateToObj, collateralTxInToObj, deserializeAddress, deserializeBech32Address, deserializeBip32PrivateKey, deserializeDataHash, deserializeEd25519KeyHash, deserializeEd25519Signature, deserializeNativeScript, deserializePlutusData, deserializePlutusScript, deserializePublicKey, deserializeScriptHash, deserializeScriptRef, deserializeTx, deserializeTxBody, deserializeTxHash, deserializeTxUnspentOutput, deserializeTxWitnessSet, deserializeValue, evaluateTransaction, fromBytes, fromLovelace, fromUTF8, getTransactionInputs, getV2ScriptHash, keyHashToRewardAddress, meshTxBuilderBodyToObj, mintItemToObj, mintParametersObj, nativeMintItemToObj, outputToObj, parseDatumCbor, parseInlineDatum, plutusMintItemToObj, poolIdBech32ToHex, poolIdHexToBech32, poolMetadataToObj, poolParamsToObj, redeemerToObj, relayToObj, resolveDataHash, resolveEd25519KeyHash, resolveNativeScriptAddress, resolveNativeScriptHash, resolveNativeScriptHex, resolvePlutusScriptAddress, resolvePrivateKey, resolveRewardAddress, resolveScriptHashDRepId, resolveScriptRef, resolveStakeKeyHash, rewardAddressToKeyHash, scriptHashToBech32, scriptHashToRewardAddress, scriptSourceToObj, scriptTxInParameterToObj, serializeAddressObj, serializePlutusAddressToBech32, serializePoolId, serialzeAddress, signTransaction, simpleScriptSourceToObj, simpleScriptTxInParameterToObj, skeyToPubKeyHash, toAddress, toBaseAddress, toBytes, toEnterpriseAddress, toLovelace, toNativeScript, toPlutusData, toRewardAddress, toScriptRef, toUTF8, txInParameterToObj, txInToObj, utxoToObj, v2ScriptToBech32, withdrawalToObj };
|
package/dist/index.js
CHANGED
|
@@ -401,6 +401,86 @@ var signTransaction = (txHex, signingKeys) => {
|
|
|
401
401
|
const result = csl.js_sign_transaction(txHex, cslSigningKeys);
|
|
402
402
|
return parseWasmResult(result);
|
|
403
403
|
};
|
|
404
|
+
var evaluateTransaction = (txHex, resolvedUtxos, network) => {
|
|
405
|
+
const additionalTxs = csl.JsVecString.new();
|
|
406
|
+
const mappedUtxos = csl.JsVecString.new();
|
|
407
|
+
for (const utxo of resolvedUtxos) {
|
|
408
|
+
mappedUtxos.add(JSON.stringify(utxo));
|
|
409
|
+
}
|
|
410
|
+
const result = csl.evaluate_tx_scripts_js(txHex, mappedUtxos, additionalTxs, network);
|
|
411
|
+
const unwrappedResult = parseWasmResult(result);
|
|
412
|
+
try {
|
|
413
|
+
const actions = JSON.parse(unwrappedResult);
|
|
414
|
+
return actions.map(mapAction);
|
|
415
|
+
} catch (e) {
|
|
416
|
+
throw new Error("Cannot parse result from evaluate_tx_scripts_js. Expected Action[] type");
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
var mapAction = (action) => {
|
|
420
|
+
return {
|
|
421
|
+
index: action.index,
|
|
422
|
+
budget: mapBudget(action.budget),
|
|
423
|
+
tag: mapRedeemerTag(action.tag)
|
|
424
|
+
};
|
|
425
|
+
};
|
|
426
|
+
var mapBudget = (budget) => {
|
|
427
|
+
return {
|
|
428
|
+
mem: budget.mem,
|
|
429
|
+
steps: budget.steps
|
|
430
|
+
};
|
|
431
|
+
};
|
|
432
|
+
var mapRedeemerTag = (tag) => {
|
|
433
|
+
switch (tag) {
|
|
434
|
+
case "cert":
|
|
435
|
+
return "CERT";
|
|
436
|
+
case "mint":
|
|
437
|
+
return "MINT";
|
|
438
|
+
case "reward":
|
|
439
|
+
return "REWARD";
|
|
440
|
+
case "spend":
|
|
441
|
+
return "SPEND";
|
|
442
|
+
case "vote":
|
|
443
|
+
return "VOTE";
|
|
444
|
+
case "propose":
|
|
445
|
+
return "PROPOSE";
|
|
446
|
+
default:
|
|
447
|
+
throw new Error(`Unknown RedeemerTag: ${tag}`);
|
|
448
|
+
}
|
|
449
|
+
};
|
|
450
|
+
var getTransactionInputs = (txHex) => {
|
|
451
|
+
const inputs = [];
|
|
452
|
+
const deserializedTx = deserializeTx(txHex);
|
|
453
|
+
const body = deserializedTx.body();
|
|
454
|
+
const cslInputs = body.inputs();
|
|
455
|
+
for (let i = 0; i < cslInputs.len(); i++) {
|
|
456
|
+
const input = cslInputs.get(i);
|
|
457
|
+
inputs.push({
|
|
458
|
+
txHash: input.transaction_id().to_hex(),
|
|
459
|
+
index: input.index()
|
|
460
|
+
});
|
|
461
|
+
}
|
|
462
|
+
const cslCollaterals = body.collateral();
|
|
463
|
+
if (cslCollaterals) {
|
|
464
|
+
for (let i = 0; i < cslCollaterals.len(); i++) {
|
|
465
|
+
const collateral = cslCollaterals.get(i);
|
|
466
|
+
inputs.push({
|
|
467
|
+
txHash: collateral.transaction_id().to_hex(),
|
|
468
|
+
index: collateral.index()
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
const cslRefInputs = body.reference_inputs();
|
|
473
|
+
if (cslRefInputs) {
|
|
474
|
+
for (let i = 0; i < cslRefInputs.len(); i++) {
|
|
475
|
+
const refInput = cslRefInputs.get(i);
|
|
476
|
+
inputs.push({
|
|
477
|
+
txHash: refInput.transaction_id().to_hex(),
|
|
478
|
+
index: refInput.index()
|
|
479
|
+
});
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
return inputs;
|
|
483
|
+
};
|
|
404
484
|
|
|
405
485
|
// src/utils/aiken.ts
|
|
406
486
|
var applyParamsToScript = (rawScript, params, type = "Mesh") => {
|
|
@@ -1178,9 +1258,62 @@ var CSLSerializer = class {
|
|
|
1178
1258
|
}
|
|
1179
1259
|
};
|
|
1180
1260
|
};
|
|
1261
|
+
|
|
1262
|
+
// src/offline-providers/offline-evaluator.ts
|
|
1263
|
+
var OfflineEvaluator = class {
|
|
1264
|
+
fetcher;
|
|
1265
|
+
network;
|
|
1266
|
+
/**
|
|
1267
|
+
* Creates a new instance of OfflineEvaluator.
|
|
1268
|
+
* @param fetcher - An implementation of IFetcher to resolve transaction UTXOs
|
|
1269
|
+
* @param network - The network to evaluate scripts for
|
|
1270
|
+
*/
|
|
1271
|
+
constructor(fetcher, network) {
|
|
1272
|
+
this.fetcher = fetcher;
|
|
1273
|
+
this.network = network;
|
|
1274
|
+
}
|
|
1275
|
+
/**
|
|
1276
|
+
* Evaluates Plutus scripts in a transaction by resolving its input UTXOs and calculating execution costs.
|
|
1277
|
+
*
|
|
1278
|
+
* The method performs these steps:
|
|
1279
|
+
* 1. Extracts input references from the transaction
|
|
1280
|
+
* 2. Resolves the corresponding UTXOs using the fetcher
|
|
1281
|
+
* 3. Verifies all required UTXOs are available
|
|
1282
|
+
* 4. Evaluates each Plutus script to determine its memory and CPU costs
|
|
1283
|
+
*
|
|
1284
|
+
* @param tx - Transaction in CBOR hex format
|
|
1285
|
+
* @returns Promise resolving to array of script evaluation results, each containing:
|
|
1286
|
+
* - tag: Type of script (CERT | MINT | REWARD | SPEND | VOTE | PROPOSE)
|
|
1287
|
+
* - index: Script execution index
|
|
1288
|
+
* - budget: Memory units and CPU steps required
|
|
1289
|
+
* @throws Error if any required UTXOs cannot be resolved or if script evaluation fails
|
|
1290
|
+
*/
|
|
1291
|
+
async evaluateTx(tx) {
|
|
1292
|
+
const inputsToResolve = getTransactionInputs(tx);
|
|
1293
|
+
const txHashesSet = new Set(inputsToResolve.map((input) => input.txHash));
|
|
1294
|
+
const resolvedUTXOs = [];
|
|
1295
|
+
for (const txHash of txHashesSet) {
|
|
1296
|
+
const utxos = await this.fetcher.fetchUTxOs(txHash);
|
|
1297
|
+
for (const utxo of utxos) {
|
|
1298
|
+
if (utxo) {
|
|
1299
|
+
if (inputsToResolve.find((input) => input.txHash === txHash && input.index === utxo.input.outputIndex)) {
|
|
1300
|
+
resolvedUTXOs.push(utxo);
|
|
1301
|
+
}
|
|
1302
|
+
}
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
if (resolvedUTXOs.length !== inputsToResolve.length) {
|
|
1306
|
+
const missing = inputsToResolve.filter((input) => !resolvedUTXOs.find((utxo) => utxo.input.txHash === input.txHash && utxo.input.outputIndex === input.index));
|
|
1307
|
+
const missingList = missing.map((m) => `${m.txHash}:${m.index}`).join(", ");
|
|
1308
|
+
throw new Error(`Can't resolve these UTXOs to execute plutus scripts: ${missingList}`);
|
|
1309
|
+
}
|
|
1310
|
+
return evaluateTransaction(tx, resolvedUTXOs, this.network);
|
|
1311
|
+
}
|
|
1312
|
+
};
|
|
1181
1313
|
export {
|
|
1182
1314
|
CSLSerializer,
|
|
1183
1315
|
LANGUAGE_VERSIONS,
|
|
1316
|
+
OfflineEvaluator,
|
|
1184
1317
|
POLICY_ID_LENGTH,
|
|
1185
1318
|
REDEEMER_TAGS,
|
|
1186
1319
|
addrBech32ToHex,
|
|
@@ -1214,9 +1347,11 @@ export {
|
|
|
1214
1347
|
deserializeTxUnspentOutput,
|
|
1215
1348
|
deserializeTxWitnessSet,
|
|
1216
1349
|
deserializeValue,
|
|
1350
|
+
evaluateTransaction,
|
|
1217
1351
|
fromBytes,
|
|
1218
1352
|
fromLovelace,
|
|
1219
1353
|
fromUTF8,
|
|
1354
|
+
getTransactionInputs,
|
|
1220
1355
|
getV2ScriptHash,
|
|
1221
1356
|
keyHashToRewardAddress,
|
|
1222
1357
|
meshTxBuilderBodyToObj,
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@meshsdk/core-csl",
|
|
3
|
-
"version": "1.7.
|
|
3
|
+
"version": "1.7.16",
|
|
4
4
|
"description": "",
|
|
5
5
|
"main": "./dist/index.cjs",
|
|
6
6
|
"module": "./dist/index.js",
|
|
@@ -31,6 +31,7 @@
|
|
|
31
31
|
},
|
|
32
32
|
"devDependencies": {
|
|
33
33
|
"@meshsdk/configs": "*",
|
|
34
|
+
"@meshsdk/provider": "1.7.16",
|
|
34
35
|
"@types/json-bigint": "^1.0.4",
|
|
35
36
|
"eslint": "^8.57.0",
|
|
36
37
|
"ts-jest": "^29.1.4",
|
|
@@ -38,7 +39,7 @@
|
|
|
38
39
|
"typescript": "^5.3.3"
|
|
39
40
|
},
|
|
40
41
|
"dependencies": {
|
|
41
|
-
"@meshsdk/common": "1.7.
|
|
42
|
+
"@meshsdk/common": "1.7.16",
|
|
42
43
|
"@sidan-lab/sidan-csl-rs-browser": "0.9.5",
|
|
43
44
|
"@sidan-lab/sidan-csl-rs-nodejs": "0.9.5",
|
|
44
45
|
"json-bigint": "^1.0.0"
|
|
@@ -55,4 +56,4 @@
|
|
|
55
56
|
"blockchain",
|
|
56
57
|
"sdk"
|
|
57
58
|
]
|
|
58
|
-
}
|
|
59
|
+
}
|