UNPKG

@stricahq/cardano-codec

Version:

Cardano Codec library for parsing Cardano CBOR data types

99 lines (98 loc) 3.28 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k); __setModuleDefault(result, mod); return result; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.parseNativeScript = exports.parseNativeScripts = exports.parseMetadata = exports.parsePlutusData = void 0; const cbors = __importStar(require("@stricahq/cbors")); const utils = __importStar(require("../utils/utils")); const parsePlutusData = (datum, blockCbor) => { // datum does not have strings if (typeof datum === "number") { return cbors.Encoder.encode(datum); } const datumBuf = utils.getCborSpanBuffer(blockCbor, datum); return datumBuf; }; exports.parsePlutusData = parsePlutusData; const parseMetadata = (metadata) => { const data = []; for (const [label, datum] of metadata.entries()) { data.push({ label, data: datum, }); } return data; }; exports.parseMetadata = parseMetadata; const parseNativeScripts = (nativeScripts) => { const data = []; for (const nativeScript of nativeScripts) { // eslint-disable-next-line no-use-before-define data.push((0, exports.parseNativeScript)(nativeScript)); } return data; }; exports.parseNativeScripts = parseNativeScripts; const parseNativeScript = (nativeScript) => { if (nativeScript[0] === 0) { const pubKey = nativeScript[1].toString("hex"); return { pubKeyHash: pubKey, }; } if (nativeScript[0] === 1) { const all = (0, exports.parseNativeScripts)(nativeScript[1]); return { all, }; } if (nativeScript[0] === 2) { const any = (0, exports.parseNativeScripts)(nativeScript[1]); return { any, }; } if (nativeScript[0] === 3) { const k = (0, exports.parseNativeScripts)(nativeScript[2]); return { n: nativeScript[1], k: k, }; } if (nativeScript[0] === 4) { const invalidBefore = nativeScript[1]; return { invalidBefore, }; } if (nativeScript[0] === 5) { const invalidAfter = nativeScript[1]; return { invalidAfter, }; } throw new Error("Error parsing Native Script"); }; exports.parseNativeScript = parseNativeScript;