seedelf_core/
data_structures.rs

1use anyhow::{Context, Result, anyhow};
2use hex;
3use hex::FromHex;
4use pallas_primitives::{
5    BoundedBytes, Fragment,
6    alonzo::{Constr, MaybeIndefArray, PlutusData},
7};
8
9/// Creates a mint redeemer for a Plutus script.
10///
11/// This function encodes a given string label as a hex string, converts it to bytes,
12/// and wraps it into `PlutusData` of type `BoundedBytes`. The result is serialized
13/// as a fragment.
14///
15/// # Arguments
16///
17/// * `label` - A string that represents the label to encode.
18///
19/// # Returns
20///
21/// * `Vec<u8>` - The encoded PlutusData fragment as a byte vector.
22///
23/// # Panics
24///
25/// * If the label cannot be converted into a valid hex string.
26pub fn create_mint_redeemer(label: String) -> Result<Vec<u8>> {
27    let mut label_hex: String = hex::encode(label);
28    label_hex.truncate(30);
29    let lb: Vec<u8> = Vec::from_hex(&label_hex).context("Invalid hex string")?;
30    let d: PlutusData = PlutusData::BoundedBytes(BoundedBytes::from(lb));
31    d.encode_fragment()
32        .map_err(|e| anyhow!("Failed to encode PlutusData fragment: {e}"))
33}
34
35/// Creates a spend redeemer for a Plutus script.
36///
37/// This function takes three hex-encoded strings (`z`, `g_r`, `pkh`), converts them to byte vectors,
38/// and constructs a `PlutusData` structure with a custom `Constr` type (tag `121`).
39/// The resulting data is serialized as a fragment.
40///
41/// # Arguments
42///
43/// * `z` - A hex-encoded string representing the first field.
44/// * `g_r` - A hex-encoded string representing the second field.
45/// * `pkh` - A hex-encoded string representing the third field.
46///
47/// # Returns
48///
49/// * `Vec<u8>` - The encoded PlutusData fragment as a byte vector.
50///
51/// # Panics
52///
53/// * If any input string cannot be converted into a valid hex string.
54pub fn create_spend_redeemer(z: String, g_r: String, pkh: String) -> Result<Vec<u8>> {
55    let zb: Vec<u8> = Vec::from_hex(z).context("Invalid hex string")?;
56    let grb: Vec<u8> = Vec::from_hex(g_r).context("Invalid hex string")?;
57    let pkhb: Vec<u8> = Vec::from_hex(pkh).context("Invalid hex string")?;
58    let d: PlutusData = PlutusData::Constr(Constr {
59        tag: 121,
60        any_constructor: None,
61        fields: MaybeIndefArray::Indef(vec![
62            PlutusData::BoundedBytes(BoundedBytes::from(zb)),
63            PlutusData::BoundedBytes(BoundedBytes::from(grb)),
64            PlutusData::BoundedBytes(BoundedBytes::from(pkhb)),
65        ]),
66    });
67    d.encode_fragment()
68        .map_err(|e| anyhow!("Failed to encode PlutusData fragment: {e}"))
69}