UNPKG

@harmoniclabs/buildooor

Version:

Cardano transaction builder in typescript

102 lines (101 loc) 5.28 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getProposingScript = exports.getVotingScript = exports.getWithdrawalScript = exports.getCeritficateScript = exports.getMintingScript = exports.getSpendingScript = void 0; var cardano_ledger_ts_1 = require("@harmoniclabs/cardano-ledger-ts"); var plutus_data_1 = require("@harmoniclabs/plutus-data"); var uint8array_utils_1 = require("@harmoniclabs/uint8array-utils"); function getScriptByHash(tx, hash) { var _a, _b, _c, _d, _e, _f, _g, _h; if (!(hash instanceof Uint8Array && hash.length === 28)) return undefined; var witnesses = tx.witnesses; 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); } function getSpendingScript(tx, index) { if (tx.body.inputs.length <= index) return undefined; var sortedIns = tx.body.inputs.slice().sort(function (a, b) { var ord = (0, uint8array_utils_1.lexCompare)(a.utxoRef.id.toBuffer(), b.utxoRef.id.toBuffer()); // if equal tx id order based on tx output index if (ord === 0) return a.utxoRef.index - b.utxoRef.index; // else order by tx id return ord; }); var scriptInput = sortedIns[index]; if (!scriptInput) return undefined; var scriptHash = scriptInput.resolved.address.paymentCreds.hash.toBuffer(); var script = getScriptByHash(tx, scriptHash); if (!script) return undefined; if ((0, plutus_data_1.isData)(scriptInput.resolved.datum)) return { script: script, datum: scriptInput.resolved.datum }; return { script: script, datum: undefined }; } exports.getSpendingScript = getSpendingScript; function getMintingScript(tx, index) { var mintedValue = tx.body.mint; if (!mintedValue) return undefined; var allPolicies = mintedValue.map.map(function (entry) { return entry.policy; }).filter(function (p) { return p instanceof cardano_ledger_ts_1.Hash28; }); if (allPolicies.length === 0) return undefined; var policyHash = allPolicies[index - 1]; if (!policyHash) return undefined; return getScriptByHash(tx, policyHash.toBuffer()); } exports.getMintingScript = getMintingScript; function getCeritficateScript(tx, index) { var _a; var allCertificates = tx.body.certs; if (!allCertificates) return undefined; var cert = allCertificates[index]; if (!cert) return undefined; return getScriptByHash(tx, (_a = getCertStakeCreds(cert)) === null || _a === void 0 ? void 0 : _a.hash.toBuffer()); } exports.getCeritficateScript = getCeritficateScript; function getWithdrawalScript(tx, index) { var _a; var allWithdrawals = tx.body.withdrawals; if (!allWithdrawals) return undefined; var scriptHash = (_a = allWithdrawals.map[index]) === null || _a === void 0 ? void 0 : _a.rewardAccount.credentials.toBuffer(); if (!scriptHash) return undefined; return getScriptByHash(tx, scriptHash); } exports.getWithdrawalScript = getWithdrawalScript; function getVotingScript(tx, index) { // TODO return undefined; } exports.getVotingScript = getVotingScript; function getProposingScript(tx, index) { // TODO return undefined; } exports.getProposingScript = getProposingScript; function getCertStakeCreds(cert) { // CertAuthCommitteeHot | CertResignCommitteeCold | CertRegistrationDrep | CertUnRegistrationDrep | CertUpdateDrep; if (cert instanceof cardano_ledger_ts_1.CertStakeRegistration || cert instanceof cardano_ledger_ts_1.CertStakeDeRegistration || cert instanceof cardano_ledger_ts_1.CertStakeDelegation || cert instanceof cardano_ledger_ts_1.CertVoteDeleg || cert instanceof cardano_ledger_ts_1.CertStakeVoteDeleg || cert instanceof cardano_ledger_ts_1.CertRegistrationDeposit || cert instanceof cardano_ledger_ts_1.CertUnRegistrationDeposit || cert instanceof cardano_ledger_ts_1.CertStakeRegistrationDeleg || cert instanceof cardano_ledger_ts_1.CertVoteRegistrationDeleg || cert instanceof cardano_ledger_ts_1.CertStakeVoteRegistrationDeleg) return cert.stakeCredential; if (cert instanceof cardano_ledger_ts_1.CertRegistrationDrep || cert instanceof cardano_ledger_ts_1.CertUnRegistrationDrep || cert instanceof cardano_ledger_ts_1.CertUpdateDrep) return cert.drepCredential; return undefined; }