@harmoniclabs/plu-ts-onchain
Version:
An embedded DSL for Cardano smart contracts creation coupled with a library for Cardano transactions, all in Typescript
78 lines (77 loc) • 3 kB
JavaScript
;
var __read = (this && this.__read) || function (o, n) {
var m = typeof Symbol === "function" && o[Symbol.iterator];
if (!m) return o;
var i = m.call(o), r, ar = [], e;
try {
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
}
catch (error) { e = { error: error }; }
finally {
try {
if (r && !r.done && (m = i["return"])) m.call(i);
}
finally { if (e) throw e.error; }
}
return ar;
};
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
if (ar || !(i in from)) {
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
ar[i] = from[i];
}
}
return to.concat(ar || Array.prototype.slice.call(from));
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.toBlueprintValidator = void 0;
var uint8array_utils_1 = require("@harmoniclabs/uint8array-utils");
var getFnTypes_1 = require("../Parametrized/getFnTypes.js");
var compile_1 = require("../compile.js");
var toDataSchema_1 = require("./toDataSchema.js");
var crypto_1 = require("@harmoniclabs/crypto");
var cbor_1 = require("@harmoniclabs/cbor");
function toBlueprintValidator(title, purpose, validator, plutusVersion) {
if (plutusVersion === void 0) { plutusVersion = "PlutusScriptV2"; }
var t = validator.type;
var tys = (0, getFnTypes_1.getFnTypes)(t);
var len = tys.length;
var outT = tys[len - 1][0];
var isSpend = purpose === "spend";
if ((isSpend && len < 4) || len < 3)
throw new Error("invalid term to be a validator");
var paramsTys = isSpend ? tys.slice(0, len - 4) : tys.slice(0, len - 3);
var argsTys = isSpend ? tys.slice(len - 4, len - 1) : tys.slice(len - 3, len - 1);
var purposes = [purpose];
var compiled = (0, compile_1.compile)(validator);
var result = {
title: title,
redeemer: {
purposes: purposes,
schema: (0, toDataSchema_1.toDataSchemaAsData)(isSpend ? argsTys[1] : argsTys[0])
},
compiledCode: (0, uint8array_utils_1.toHex)(compiled),
hash: (0, uint8array_utils_1.toHex)((0, crypto_1.blake2b_224)(new Uint8Array(__spreadArray([
plutusVersion === "PlutusScriptV2" ? 0x02 : 0x01
], __read(cbor_1.Cbor.encode(new cbor_1.CborBytes(compiled)).toBuffer()), false))))
};
if (isSpend) {
result.datum = {
purposes: purposes,
schema: (0, toDataSchema_1.toDataSchemaAsData)(argsTys[0])
};
}
if (paramsTys.length > 0) {
result.parameters = paramsTys
.map(function (t) {
return ({
purposes: purposes,
schema: (0, toDataSchema_1.toDataSchema)(t)
});
});
}
// else no params
return result;
}
exports.toBlueprintValidator = toBlueprintValidator;