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