@harmoniclabs/buildooor 0.1.3 → 0.1.5

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.
@@ -17,7 +17,7 @@ export declare class TxBuilder {
17
17
  constructor(protocolParamters?: Readonly<TxBuilderProtocolParams>, genesisInfos?: GenesisInfos);
18
18
  keepRelevant(requestedOutputSet: Value | ValueUnits, initialUTxOSet: ITxBuildInput[], minimumLovelaceRequired?: CanBeUInteger): ITxBuildInput[];
19
19
  private calcLinearFee;
20
- calcMinFee(tx: Tx): bigint;
20
+ calcMinFee(tx: Tx, minimum?: CanBeUInteger | undefined): bigint;
21
21
  getMinimumOutputLovelaces(tx_out: TxOut | CanBeCborString): bigint;
22
22
  addMinLovelacesIfMissing(txOut: TxOut): TxOut;
23
23
  minimizeLovelaces(out: ITxOut): TxOut;
@@ -56,6 +56,7 @@ export declare class TxBuilder {
56
56
  */
57
57
  overrideTxRedeemers(tx: Tx, newRedeemers: TxRedeemer[], opts?: CostModelsToLanguageViewCborOpts): Tx;
58
58
  buildSync(buildArgs: ITxBuildArgs, { onScriptInvalid, onScriptResult }?: ITxBuildSyncOptions): Tx;
59
+ validatePhaseTwo(tx: Tx): boolean;
59
60
  assertMinOutLovelaces(txOuts: TxOut[]): void;
60
61
  /**
61
62
  * extracts the important data from the input
@@ -116,6 +116,7 @@ var TxBuilderProtocolParams_1 = require("./TxBuilderProtocolParams.js");
116
116
  var utils_1 = require("./utils.js");
117
117
  var Rational_1 = require("../utils/Rational.js");
118
118
  var stringify_1 = require("../utils/stringify.js");
119
+ var getScript_1 = require("./utils/getScript.js");
119
120
  // const scriptCache: { [x: string]: UPLCTerm } = {};
120
121
  function getScriptLikeUplc(scriptLike) {
121
122
  return uplc_1.UPLCDecoder.parse(scriptLike.bytes, "flat").body;
@@ -173,7 +174,7 @@ var TxBuilder = /** @class */ (function () {
173
174
  ) +
174
175
  (0, ints_1.forceBigUInt)(this.protocolParamters.txFeeFixed));
175
176
  };
176
- TxBuilder.prototype.calcMinFee = function (tx) {
177
+ TxBuilder.prototype.calcMinFee = function (tx, minimum) {
177
178
  var _a;
178
179
  var totRefScriptBytes = ((_a = tx.body.refInputs) !== null && _a !== void 0 ? _a : [])
179
180
  .reduce(function (sum, refIn) {
@@ -198,7 +199,10 @@ var TxBuilder = /** @class */ (function () {
198
199
  BigInt(104) * nVkeyWits * minFeeMultiplier +
199
200
  // we add some more bytes for the array tag
200
201
  BigInt(nVkeyWits < 24 ? 1 : (nVkeyWits < 256 ? 2 : 4)) * minFeeMultiplier;
201
- return minFee;
202
+ if (!(0, ints_1.canBeUInteger)(minimum))
203
+ return minFee;
204
+ var min = (0, ints_1.forceBigUInt)(minimum);
205
+ return minFee < min ? min : minFee;
202
206
  };
203
207
  TxBuilder.prototype.getMinimumOutputLovelaces = function (tx_out) {
204
208
  var size = BigInt(0);
@@ -410,7 +414,7 @@ var TxBuilder = /** @class */ (function () {
410
414
  for (var i = 0; i < nRdmrs; i++) {
411
415
  _loop_2(i);
412
416
  }
413
- minFee = this_1.calcMinFee(tx);
417
+ minFee = this_1.calcMinFee(tx, buildArgs.fee);
414
418
  fee = minFee +
415
419
  ((totExBudget.mem * memRational.num) / memRational.den) +
416
420
  ((totExBudget.cpu * cpuRational.num) / cpuRational.den) +
@@ -448,6 +452,135 @@ var TxBuilder = /** @class */ (function () {
448
452
  this.assertMinOutLovelaces(tx.body.outputs);
449
453
  return tx;
450
454
  };
455
+ TxBuilder.prototype.validatePhaseTwo = function (tx) {
456
+ var txBody = tx.body;
457
+ var rdmrs = tx.witnesses.redeemers;
458
+ if (!Array.isArray(rdmrs) || rdmrs.length === 0)
459
+ return true;
460
+ var nRdmrs = rdmrs.length;
461
+ var cek = this.cek;
462
+ if (!(cek instanceof plutus_machine_1.Machine))
463
+ throw new Error("protocol params are missing the script evaluation costs");
464
+ var executionUnitPrices = this.protocolParamters.executionUnitPrices;
465
+ executionUnitPrices = Array.isArray(executionUnitPrices) ? executionUnitPrices : [
466
+ executionUnitPrices.priceMemory,
467
+ executionUnitPrices.priceSteps,
468
+ ];
469
+ var _a = __read(executionUnitPrices, 2), memRational = _a[0], cpuRational = _a[1];
470
+ memRational = typeof memRational === "number" ? cbor_1.CborPositiveRational.fromNumber(memRational) : memRational;
471
+ cpuRational = typeof cpuRational === "number" ? cbor_1.CborPositiveRational.fromNumber(cpuRational) : cpuRational;
472
+ 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
+ var _loop_3 = function (i) {
475
+ var rdmr = rdmrs[i];
476
+ var tag = rdmr.tag, rdmrData = rdmr.data, rdmr_idx = rdmr.index;
477
+ // "+ 1" because we keep track of lovelaces even if in mint values these are 0
478
+ var index = rdmr_idx + (tag === cardano_ledger_ts_1.TxRedeemerTag.Mint ? 1 : 0);
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));
482
+ var expectedVersion = (0, utils_1.scriptTypeToDataVersion)(script.type);
483
+ if (typeof expectedVersion !== "string")
484
+ throw new Error("unexpected redeemer for native script");
485
+ var ctxData = getCtx(script.type, (0, getSpendingPurposeData_1.getSpendingPurposeData)(rdmr, tx.body, expectedVersion), (0, getSpendingPurposeData_1.getScriptInfoData)(rdmr, tx.body, expectedVersion), rdmrData, txInfosV1, txInfosV2, txInfosV3);
486
+ var isV2OrLess = (script.type === cardano_ledger_ts_1.ScriptType.PlutusV1 ||
487
+ script.type === cardano_ledger_ts_1.ScriptType.PlutusV2 ||
488
+ script.type === cardano_ledger_ts_1.ScriptType.NativeScript);
489
+ var _a = cek.eval(isV2OrLess ?
490
+ 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 uplc_1.UPLCConst
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;
507
+ };
508
+ if (tag === cardano_ledger_ts_1.TxRedeemerTag.Spend) {
509
+ var entry = (0, getScript_1.getSpendingScript)(tx, index);
510
+ if (!entry)
511
+ return { value: false };
512
+ var script = entry.script, datum = entry.datum;
513
+ // TODO: check if this is correct
514
+ // I'm assuming native scripts are phase 1
515
+ if (script.type === cardano_ledger_ts_1.ScriptType.NativeScript)
516
+ return { value: true };
517
+ var isV2OrLess = script.type === cardano_ledger_ts_1.ScriptType.PlutusV1 || script.type === cardano_ledger_ts_1.ScriptType.PlutusV2;
518
+ if (datum === undefined && isV2OrLess)
519
+ throw new Error("missing datum for spend redeemer " + index);
520
+ var expectedVersion = (0, utils_1.scriptTypeToDataVersion)(script.type);
521
+ if (typeof expectedVersion !== "string")
522
+ throw new Error("unexpected redeemer for native script");
523
+ 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 ?
525
+ 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 uplc_1.UPLCConst
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);
541
+ return "continue";
542
+ }
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
+ }
573
+ else
574
+ throw new Error("unrecoignized redeemer tag " + tag);
575
+ return { value: false };
576
+ };
577
+ 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;
581
+ } // for loop over redeemers
582
+ return true;
583
+ };
451
584
  TxBuilder.prototype.assertMinOutLovelaces = function (txOuts) {
452
585
  for (var i = 0; i < txOuts.length; i++) {
453
586
  var out = txOuts[i];
@@ -945,9 +1078,7 @@ var TxBuilder = /** @class */ (function () {
945
1078
  auxiliaryData: auxData,
946
1079
  isScriptValid: isScriptValid
947
1080
  });
948
- var minFee = this.calcMinFee(dummyTx);
949
- if (typeof args.fee === "bigint" && args.fee > minFee)
950
- minFee = args.fee;
1081
+ var minFee = this.calcMinFee(dummyTx, args.fee);
951
1082
  var txOuts = new Array(outs.length + 1);
952
1083
  outs.forEach(function (txO, i) { return txOuts[i] = _this.addMinLovelacesIfMissing(txO); });
953
1084
  var changeOutput = new cardano_ledger_ts_1.TxOut({
@@ -0,0 +1,11 @@
1
+ import { Script, Tx, ScriptType } from "@harmoniclabs/cardano-ledger-ts";
2
+ import { Data } from "@harmoniclabs/plutus-data";
3
+ export declare function getSpendingScript(tx: Tx, index: number): {
4
+ script: Script;
5
+ datum: Data | undefined;
6
+ } | undefined;
7
+ export declare function getMintingScript(tx: Tx, index: number): Script<ScriptType> | undefined;
8
+ export declare function getCeritficateScript(tx: Tx, index: number): Script<ScriptType> | undefined;
9
+ export declare function getWithdrawalScript(tx: Tx, index: number): Script<ScriptType> | undefined;
10
+ export declare function getVotingScript(tx: Tx, index: number): Script<ScriptType> | undefined;
11
+ export declare function getProposingScript(tx: Tx, index: number): Script<ScriptType> | undefined;
@@ -0,0 +1,90 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.getProposingScript = exports.getVotingScript = exports.getWithdrawalScript = exports.getCeritficateScript = exports.getMintingScript = exports.getSpendingScript = void 0;
4
+ var cardano_ledger_ts_1 = require("@harmoniclabs/cardano-ledger-ts");
5
+ var plutus_data_1 = require("@harmoniclabs/plutus-data");
6
+ var uint8array_utils_1 = require("@harmoniclabs/uint8array-utils");
7
+ function getScriptByHash(tx, hash) {
8
+ var _a, _b, _c, _d, _e, _f, _g, _h;
9
+ if (!(hash instanceof Uint8Array
10
+ && hash.length === 28))
11
+ return undefined;
12
+ var witnesses = tx.witnesses;
13
+ return ((_f = (_d = (_b = (_a = witnesses.plutusV1Scripts) === null || _a === void 0 ? void 0 : _a.find(function (s) { return (0, uint8array_utils_1.uint8ArrayEq)(hash, s.hash.toBuffer()); })) !== null && _b !== void 0 ? _b : (_c = witnesses.plutusV2Scripts) === null || _c === void 0 ? void 0 : _c.find(function (s) { return (0, uint8array_utils_1.uint8ArrayEq)(hash, s.hash.toBuffer()); })) !== null && _d !== void 0 ? _d : (_e = witnesses.plutusV3Scripts) === null || _e === void 0 ? void 0 : _e.find(function (s) { return (0, uint8array_utils_1.uint8ArrayEq)(hash, s.hash.toBuffer()); })) !== null && _f !== void 0 ? _f : (_h = (_g = tx.body.refInputs) === null || _g === void 0 ? void 0 : _g.find(function (i) { return i.resolved.refScript && (0, uint8array_utils_1.uint8ArrayEq)(hash, i.resolved.refScript.hash.toBuffer()); })) === null || _h === void 0 ? void 0 : _h.resolved.refScript);
14
+ }
15
+ function getSpendingScript(tx, index) {
16
+ var allScriptInputs = tx.body.inputs.filter(function (i) { return i.resolved.address.paymentCreds.type === cardano_ledger_ts_1.CredentialType.Script; });
17
+ if (allScriptInputs.length === 0)
18
+ return undefined;
19
+ var scriptInput = allScriptInputs[index];
20
+ var scriptHash = scriptInput.resolved.address.paymentCreds.hash.toBuffer();
21
+ var script = getScriptByHash(tx, scriptHash);
22
+ if (!script)
23
+ return undefined;
24
+ if ((0, plutus_data_1.isData)(scriptInput.resolved.datum))
25
+ return { script: script, datum: scriptInput.resolved.datum };
26
+ return { script: script, datum: undefined };
27
+ }
28
+ exports.getSpendingScript = getSpendingScript;
29
+ function getMintingScript(tx, index) {
30
+ var mintedValue = tx.body.mint;
31
+ if (!mintedValue)
32
+ return undefined;
33
+ var allPolicies = mintedValue.map.map(function (entry) { return entry.policy; }).filter(function (p) { return p instanceof cardano_ledger_ts_1.Hash28; });
34
+ if (allPolicies.length === 0)
35
+ return undefined;
36
+ var policyHash = allPolicies[index];
37
+ return getScriptByHash(tx, policyHash.toBuffer());
38
+ }
39
+ exports.getMintingScript = getMintingScript;
40
+ function getCeritficateScript(tx, index) {
41
+ var _a;
42
+ var allCertificates = tx.body.certs;
43
+ if (!allCertificates)
44
+ return undefined;
45
+ var cert = allCertificates[index];
46
+ if (!cert)
47
+ return undefined;
48
+ return getScriptByHash(tx, (_a = getCertStakeCreds(cert)) === null || _a === void 0 ? void 0 : _a.hash.toBuffer());
49
+ }
50
+ exports.getCeritficateScript = getCeritficateScript;
51
+ function getWithdrawalScript(tx, index) {
52
+ var _a;
53
+ var allWithdrawals = tx.body.withdrawals;
54
+ if (!allWithdrawals)
55
+ return undefined;
56
+ var scriptHash = (_a = allWithdrawals.map[index]) === null || _a === void 0 ? void 0 : _a.rewardAccount.credentials.toBuffer();
57
+ if (!scriptHash)
58
+ return undefined;
59
+ return getScriptByHash(tx, scriptHash);
60
+ }
61
+ exports.getWithdrawalScript = getWithdrawalScript;
62
+ function getVotingScript(tx, index) {
63
+ // TODO
64
+ return undefined;
65
+ }
66
+ exports.getVotingScript = getVotingScript;
67
+ function getProposingScript(tx, index) {
68
+ // TODO
69
+ return undefined;
70
+ }
71
+ exports.getProposingScript = getProposingScript;
72
+ function getCertStakeCreds(cert) {
73
+ // CertAuthCommitteeHot | CertResignCommitteeCold | CertRegistrationDrep | CertUnRegistrationDrep | CertUpdateDrep;
74
+ if (cert instanceof cardano_ledger_ts_1.CertStakeRegistration
75
+ || cert instanceof cardano_ledger_ts_1.CertStakeDeRegistration
76
+ || cert instanceof cardano_ledger_ts_1.CertStakeDelegation
77
+ || cert instanceof cardano_ledger_ts_1.CertVoteDeleg
78
+ || cert instanceof cardano_ledger_ts_1.CertStakeVoteDeleg
79
+ || cert instanceof cardano_ledger_ts_1.CertRegistrationDeposit
80
+ || cert instanceof cardano_ledger_ts_1.CertUnRegistrationDeposit
81
+ || cert instanceof cardano_ledger_ts_1.CertStakeRegistrationDeleg
82
+ || cert instanceof cardano_ledger_ts_1.CertVoteRegistrationDeleg
83
+ || cert instanceof cardano_ledger_ts_1.CertStakeVoteRegistrationDeleg)
84
+ return cert.stakeCredential;
85
+ if (cert instanceof cardano_ledger_ts_1.CertRegistrationDrep
86
+ || cert instanceof cardano_ledger_ts_1.CertUnRegistrationDrep
87
+ || cert instanceof cardano_ledger_ts_1.CertUpdateDrep)
88
+ return cert.drepCredential;
89
+ return undefined;
90
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@harmoniclabs/buildooor",
3
- "version": "0.1.3",
3
+ "version": "0.1.5",
4
4
  "description": "Cardano transaction builder in typescript",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",
@@ -54,7 +54,7 @@
54
54
  "@harmoniclabs/cardano-ledger-ts": "^0.3.2",
55
55
  "@harmoniclabs/cbor": "^1.6.0",
56
56
  "@harmoniclabs/pair": "^1.0.0",
57
- "@harmoniclabs/plutus-data": "^1.2.4",
57
+ "@harmoniclabs/plutus-data": "^1.2.6",
58
58
  "@harmoniclabs/plutus-machine": "^2.1.0",
59
59
  "@harmoniclabs/uplc": "^1.4.0"
60
60
  },