All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.bloxbean.cardano.client.backend.api.ScriptService Maven / Gradle / Ivy

There is a newer version: 0.6.3
Show newest version
package com.bloxbean.cardano.client.backend.api;

import com.bloxbean.cardano.client.api.exception.ApiException;
import com.bloxbean.cardano.client.api.model.Result;
import com.bloxbean.cardano.client.backend.model.ScriptDatum;
import com.bloxbean.cardano.client.backend.model.ScriptDatumCbor;
import com.bloxbean.cardano.client.exception.CborDeserializationException;
import com.bloxbean.cardano.client.plutus.spec.PlutusScript;
import com.bloxbean.cardano.client.plutus.util.PlutusUtil;
import com.bloxbean.cardano.client.transaction.spec.script.NativeScript;
import com.bloxbean.cardano.client.util.JsonUtil;
import com.fasterxml.jackson.databind.JsonNode;

public interface ScriptService {

    /**
     * Datum value
     * Query JSON value of a datum by its hash.
     *
     * @param datumHash Hash of the datum. (required)
     * @return ScriptDatum
     */
    Result getScriptDatum(String datumHash) throws ApiException;

    /**
     * Datum value CBOR
     * Query CBOR value of a datum by its hash.
     *
     * @param datumHash Hash of the datum. (required)
     * @return ScriptDatumCbor
     */
    Result getScriptDatumCbor(String datumHash) throws ApiException;

    /**
     * Get native script json by script hash
     *
     * @param scriptHash Script hash
     * @return JsonNode
     */
    Result getNativeScriptJson(String scriptHash) throws ApiException;

    /**
     * Get plutus script CBOR by script hash
     *
     * @param scriptHash Script hash
     * @return CBOR in hex string
     */
    Result getPlutusScriptCbor(String scriptHash) throws ApiException;

    /**
     * Get NativeScript by script hash
     *
     * @param scriptHash script hash
     * @return NativeScript
     * @throws ApiException
     */
    default Result getNativeScript(String scriptHash) throws ApiException {
        Result contentResult = getNativeScriptJson(scriptHash);
        if (!contentResult.isSuccessful())
            return Result.error(contentResult.getResponse()).code(contentResult.code());

        try {
            NativeScript script = NativeScript.deserialize(contentResult.getValue());
            return Result.success(JsonUtil.getPrettyJson(script)).withValue(script);
        } catch (CborDeserializationException e) {
            throw new ApiException("Error deserializing native script content " + contentResult.getValue(), e);
        }
    }

    /**
     * Get PlutusScript by script hash
     *
     * @param scriptHash script hash
     * @return PlutusScript
     * @throws ApiException
     */
    default Result getPlutusScript(String scriptHash) throws ApiException {
        Result cborResult = getPlutusScriptCbor(scriptHash);
        if (!cborResult.isSuccessful())
            return Result.error(cborResult.getResponse()).code(cborResult.code());

        try {
            return PlutusUtil.getPlutusScript(scriptHash, cborResult.getValue())
                    .map(script -> Result.success(JsonUtil.getPrettyJson(script))
                            .withValue(script))
                    .orElse(Result.error("Error getting plutus script for hash: " + scriptHash));
        } catch (Exception e) {
            throw new ApiException("Error getting plutus script" + cborResult.getValue(), e);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy