@harmoniclabs/buildooor 0.1.8 → 0.1.10

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.
@@ -3,6 +3,7 @@ import { CostModelsToLanguageViewCborOpts } from "@harmoniclabs/cardano-costmode
3
3
  import { Tx, Value, ValueUnits, TxOut, TxRedeemerTag, ScriptType, UTxO, TxRedeemer, TxWitnessSet, ScriptDataHash, ITxOut } from "@harmoniclabs/cardano-ledger-ts";
4
4
  import { CanBeCborString } from "@harmoniclabs/cbor";
5
5
  import { Data } from "@harmoniclabs/plutus-data";
6
+ import { ExBudget, CEKValue } from "@harmoniclabs/plutus-machine";
6
7
  import { ITxBuildArgs, ITxBuildOptions, ITxBuildInput, ITxBuildSyncOptions } from "../txBuild/index.js";
7
8
  import { CanBeUInteger } from "../utils/ints.js";
8
9
  import { TxBuilderRunner } from "./TxBuilderRunner/TxBuilderRunner.js";
@@ -56,6 +57,13 @@ export declare class TxBuilder {
56
57
  */
57
58
  overrideTxRedeemers(tx: Tx, newRedeemers: TxRedeemer[], opts?: CostModelsToLanguageViewCborOpts): Tx;
58
59
  buildSync(buildArgs: ITxBuildArgs, { onScriptInvalid, onScriptResult }?: ITxBuildSyncOptions): Tx;
60
+ validatePhaseTwoVerbose(tx: Tx): {
61
+ result: CEKValue;
62
+ budgetSpent: ExBudget;
63
+ logs: string[];
64
+ rdmr: TxRedeemer;
65
+ isV2OrLess: boolean;
66
+ }[];
59
67
  validatePhaseTwo(tx: Tx): boolean;
60
68
  assertMinOutLovelaces(txOuts: TxOut[]): void;
61
69
  /**
@@ -452,12 +452,13 @@ var TxBuilder = /** @class */ (function () {
452
452
  this.assertMinOutLovelaces(tx.body.outputs);
453
453
  return tx;
454
454
  };
455
- TxBuilder.prototype.validatePhaseTwo = function (tx) {
455
+ TxBuilder.prototype.validatePhaseTwoVerbose = function (tx) {
456
456
  var txBody = tx.body;
457
457
  var rdmrs = tx.witnesses.redeemers;
458
458
  if (!Array.isArray(rdmrs) || rdmrs.length === 0)
459
- return true;
459
+ return [];
460
460
  var nRdmrs = rdmrs.length;
461
+ var evalResults = [];
461
462
  var cek = this.cek;
462
463
  if (!(cek instanceof plutus_machine_1.Machine))
463
464
  throw new Error("protocol params are missing the script evaluation costs");
@@ -470,15 +471,22 @@ var TxBuilder = /** @class */ (function () {
470
471
  memRational = typeof memRational === "number" ? cbor_1.CborPositiveRational.fromNumber(memRational) : memRational;
471
472
  cpuRational = typeof cpuRational === "number" ? cbor_1.CborPositiveRational.fromNumber(cpuRational) : cpuRational;
472
473
  var _b = (0, toOnChain_1.getTxInfos)(tx, this.genesisInfos), txInfosV1 = _b.v1, txInfosV2 = _b.v2, txInfosV3 = _b.v3;
473
- var totExBudget = new plutus_machine_1.ExBudget({ mem: 0, cpu: 0 });
474
474
  var _loop_3 = function (i) {
475
475
  var rdmr = rdmrs[i];
476
476
  var tag = rdmr.tag, rdmrData = rdmr.data, rdmr_idx = rdmr.index;
477
477
  // "+ 1" because we keep track of lovelaces even if in mint values these are 0
478
478
  var index = rdmr_idx + (tag === cardano_ledger_ts_1.TxRedeemerTag.Mint ? 1 : 0);
479
479
  var onlyRedeemerArg = function (script) {
480
- if (!(script instanceof cardano_ledger_ts_1.Script))
481
- throw new Error("missing script for " + (0, cardano_ledger_ts_1.txRedeemerTagToString)(tag) + " redeemer " + (index - 1));
480
+ if (!(script instanceof cardano_ledger_ts_1.Script)) {
481
+ evalResults.push({
482
+ result: new uplc_1.ErrorUPLC("missig script"),
483
+ budgetSpent: new plutus_machine_1.ExBudget({ mem: 0, cpu: 0 }),
484
+ logs: [],
485
+ rdmr: rdmr,
486
+ isV2OrLess: false
487
+ });
488
+ return;
489
+ }
482
490
  var expectedVersion = (0, utils_1.scriptTypeToDataVersion)(script.type);
483
491
  if (typeof expectedVersion !== "string")
484
492
  throw new Error("unexpected redeemer for native script");
@@ -486,34 +494,28 @@ var TxBuilder = /** @class */ (function () {
486
494
  var isV2OrLess = (script.type === cardano_ledger_ts_1.ScriptType.PlutusV1 ||
487
495
  script.type === cardano_ledger_ts_1.ScriptType.PlutusV2 ||
488
496
  script.type === cardano_ledger_ts_1.ScriptType.NativeScript);
489
- var _a = cek.eval(isV2OrLess ?
497
+ evalResults.push(__assign(__assign({}, cek.eval(isV2OrLess ?
490
498
  new uplc_1.Application(new uplc_1.Application((0, uplc_1.parseUPLC)(script.bytes).body, uplc_1.UPLCConst.data(rdmrData)), uplc_1.UPLCConst.data(ctxData)) :
491
- new uplc_1.Application((0, uplc_1.parseUPLC)(script.bytes).body, uplc_1.UPLCConst.data(ctxData))), result = _a.result, budgetSpent = _a.budgetSpent, logs = _a.logs;
492
- var successExec = isV2OrLess ?
493
- !(result instanceof uplc_1.ErrorUPLC) :
494
- ( // v3 requires to return unit
495
- result instanceof plutus_machine_1.CEKConst
496
- && Array.isArray(result.type)
497
- && result.type.length === 1
498
- && result.type[0] === uplc_1.ConstTyTag.unit
499
- && result.value === undefined);
500
- if (!successExec)
501
- return false;
502
- if (budgetSpent.cpu > rdmr.execUnits.cpu
503
- || budgetSpent.mem > rdmr.execUnits.mem)
504
- return false;
505
- totExBudget.add(rdmr.execUnits);
506
- return true;
499
+ new uplc_1.Application((0, uplc_1.parseUPLC)(script.bytes).body, uplc_1.UPLCConst.data(ctxData)))), { rdmr: rdmr, isV2OrLess: isV2OrLess }));
507
500
  };
508
501
  if (tag === cardano_ledger_ts_1.TxRedeemerTag.Spend) {
509
502
  var entry = (0, getScript_1.getSpendingScript)(tx, index);
510
- if (!entry)
511
- return { value: false };
503
+ if (!entry) {
504
+ evalResults.push({
505
+ result: new uplc_1.ErrorUPLC("missig script"),
506
+ budgetSpent: new plutus_machine_1.ExBudget({ mem: 0, cpu: 0 }),
507
+ logs: [],
508
+ rdmr: rdmr,
509
+ isV2OrLess: false
510
+ });
511
+ return "continue";
512
+ }
513
+ ;
512
514
  var script = entry.script, datum = entry.datum;
513
515
  // TODO: check if this is correct
514
516
  // I'm assuming native scripts are phase 1
515
517
  if (script.type === cardano_ledger_ts_1.ScriptType.NativeScript)
516
- return { value: true };
518
+ return "continue";
517
519
  var isV2OrLess = script.type === cardano_ledger_ts_1.ScriptType.PlutusV1 || script.type === cardano_ledger_ts_1.ScriptType.PlutusV2;
518
520
  if (datum === undefined && isV2OrLess)
519
521
  throw new Error("missing datum for spend redeemer " + index);
@@ -521,65 +523,52 @@ var TxBuilder = /** @class */ (function () {
521
523
  if (typeof expectedVersion !== "string")
522
524
  throw new Error("unexpected redeemer for native script");
523
525
  var ctxData = getCtx(script.type, (0, getSpendingPurposeData_1.getSpendingPurposeData)(rdmr, tx.body, expectedVersion), (0, getSpendingPurposeData_1.getScriptInfoData)(rdmr, tx.body, expectedVersion, datum), rdmrData, txInfosV1, txInfosV2, txInfosV3);
524
- var _c = cek.eval(isV2OrLess ?
526
+ evalResults.push(__assign(__assign({}, cek.eval(isV2OrLess ?
525
527
  new uplc_1.Application(new uplc_1.Application(new uplc_1.Application((0, uplc_1.parseUPLC)(script.bytes).body, uplc_1.UPLCConst.data(datum)), uplc_1.UPLCConst.data(rdmrData)), uplc_1.UPLCConst.data(ctxData)) :
526
- new uplc_1.Application((0, uplc_1.parseUPLC)(script.bytes).body, uplc_1.UPLCConst.data(ctxData))), result = _c.result, budgetSpent = _c.budgetSpent, logs = _c.logs;
527
- var successExec = isV2OrLess ?
528
- !(result instanceof uplc_1.ErrorUPLC) :
529
- ( // v3 requires to return unit
530
- result instanceof plutus_machine_1.CEKConst
531
- && Array.isArray(result.type)
532
- && result.type.length === 1
533
- && result.type[0] === uplc_1.ConstTyTag.unit
534
- && result.value === undefined);
535
- if (!successExec)
536
- return { value: false };
537
- if (budgetSpent.cpu > rdmr.execUnits.cpu
538
- || budgetSpent.mem > rdmr.execUnits.mem)
539
- return { value: false };
540
- totExBudget.add(rdmr.execUnits);
528
+ new uplc_1.Application((0, uplc_1.parseUPLC)(script.bytes).body, uplc_1.UPLCConst.data(ctxData)))), { rdmr: rdmr, isV2OrLess: isV2OrLess }));
541
529
  return "continue";
542
530
  }
543
- else if (tag === cardano_ledger_ts_1.TxRedeemerTag.Mint) {
544
- if (onlyRedeemerArg((0, getScript_1.getMintingScript)(tx, index)))
545
- return "continue";
546
- else
547
- return { value: false };
548
- }
549
- else if (tag === cardano_ledger_ts_1.TxRedeemerTag.Cert) {
550
- if (onlyRedeemerArg((0, getScript_1.getCeritficateScript)(tx, index)))
551
- return "continue";
552
- else
553
- return { value: false };
554
- }
555
- else if (tag === cardano_ledger_ts_1.TxRedeemerTag.Withdraw) {
556
- if (onlyRedeemerArg((0, getScript_1.getWithdrawalScript)(tx, index)))
557
- return "continue";
558
- else
559
- return { value: false };
560
- }
561
- else if (tag === cardano_ledger_ts_1.TxRedeemerTag.Voting) {
562
- if (onlyRedeemerArg((0, getScript_1.getVotingScript)(tx, index)))
563
- return "continue";
564
- else
565
- return { value: false };
566
- }
567
- else if (tag === cardano_ledger_ts_1.TxRedeemerTag.Proposing) {
568
- if (onlyRedeemerArg((0, getScript_1.getProposingScript)(tx, index)))
569
- return "continue";
570
- else
571
- return { value: false };
572
- }
531
+ else if (tag === cardano_ledger_ts_1.TxRedeemerTag.Mint)
532
+ onlyRedeemerArg((0, getScript_1.getMintingScript)(tx, index));
533
+ else if (tag === cardano_ledger_ts_1.TxRedeemerTag.Cert)
534
+ onlyRedeemerArg((0, getScript_1.getCeritficateScript)(tx, index));
535
+ else if (tag === cardano_ledger_ts_1.TxRedeemerTag.Withdraw)
536
+ onlyRedeemerArg((0, getScript_1.getWithdrawalScript)(tx, index));
537
+ else if (tag === cardano_ledger_ts_1.TxRedeemerTag.Voting)
538
+ onlyRedeemerArg((0, getScript_1.getVotingScript)(tx, index));
539
+ else if (tag === cardano_ledger_ts_1.TxRedeemerTag.Proposing)
540
+ onlyRedeemerArg((0, getScript_1.getProposingScript)(tx, index));
573
541
  else
574
- throw new Error("unrecoignized redeemer tag " + tag);
575
- return { value: false };
542
+ return "continue";
576
543
  };
577
544
  for (var i = 0; i < nRdmrs; i++) {
578
- var state_2 = _loop_3(i);
579
- if (typeof state_2 === "object")
580
- return state_2.value;
545
+ _loop_3(i);
581
546
  } // for loop over redeemers
582
- return true;
547
+ return evalResults;
548
+ };
549
+ TxBuilder.prototype.validatePhaseTwo = function (tx) {
550
+ var totExBudget = new plutus_machine_1.ExBudget({ mem: 0, cpu: 0 });
551
+ var results = this.validatePhaseTwoVerbose(tx);
552
+ return results.every(function (_a) {
553
+ var result = _a.result, budgetSpent = _a.budgetSpent, logs = _a.logs, isV2OrLess = _a.isV2OrLess, rdmr = _a.rdmr;
554
+ var successExec = isV2OrLess ?
555
+ !(result instanceof uplc_1.ErrorUPLC) :
556
+ ( // v3 requires to return unit
557
+ result instanceof plutus_machine_1.CEKConst
558
+ && Array.isArray(result.type)
559
+ && result.type.length === 1
560
+ && result.type[0] === uplc_1.ConstTyTag.unit
561
+ && result.value === undefined);
562
+ if (!successExec)
563
+ return false;
564
+ if (budgetSpent.cpu > rdmr.execUnits.cpu
565
+ || budgetSpent.mem > rdmr.execUnits.mem)
566
+ return false;
567
+ totExBudget.add(rdmr.execUnits);
568
+ return true;
569
+ })
570
+ && totExBudget.cpu <= this.protocolParamters.maxTxExecutionUnits.cpu
571
+ && totExBudget.mem <= this.protocolParamters.maxTxExecutionUnits.mem;
583
572
  };
584
573
  TxBuilder.prototype.assertMinOutLovelaces = function (txOuts) {
585
574
  for (var i = 0; i < txOuts.length; i++) {
@@ -42,7 +42,9 @@ function getMintingScript(tx, index) {
42
42
  var allPolicies = mintedValue.map.map(function (entry) { return entry.policy; }).filter(function (p) { return p instanceof cardano_ledger_ts_1.Hash28; });
43
43
  if (allPolicies.length === 0)
44
44
  return undefined;
45
- var policyHash = allPolicies[index];
45
+ var policyHash = allPolicies[index - 1];
46
+ if (!policyHash)
47
+ return undefined;
46
48
  return getScriptByHash(tx, policyHash.toBuffer());
47
49
  }
48
50
  exports.getMintingScript = getMintingScript;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@harmoniclabs/buildooor",
3
- "version": "0.1.8",
3
+ "version": "0.1.10",
4
4
  "description": "Cardano transaction builder in typescript",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",