whisky_pallas/wrapper/transaction_body/
constitution.rs

1use std::str::FromStr;
2
3use pallas::{
4    crypto::hash::Hash,
5    ledger::primitives::{conway::Constitution as PallasConstitution, Fragment},
6};
7
8use crate::wrapper::transaction_body::Anchor;
9
10#[derive(Debug, PartialEq, Eq, Clone)]
11pub struct Constitution {
12    pub inner: PallasConstitution,
13}
14
15impl Constitution {
16    pub fn new(anchor: Anchor, guardrail_script_hash: Option<String>) -> Result<Self, String> {
17        let guardrail_script = match guardrail_script_hash {
18            Some(hash_str) => {
19                Some(Hash::<28>::from_str(&hash_str).expect("Invalid guardrail script hash length"))
20            }
21            None => None,
22        };
23
24        Ok(Self {
25            inner: PallasConstitution {
26                anchor: anchor.inner,
27                guardrail_script,
28            },
29        })
30    }
31
32    pub fn encode(&self) -> String {
33        hex::encode(
34            self.inner
35                .encode_fragment()
36                .expect("encoding failed at Constitution"),
37        )
38    }
39
40    pub fn decode_bytes(bytes: &[u8]) -> Result<Self, String> {
41        let inner = PallasConstitution::decode_fragment(&bytes)
42            .map_err(|e| format!("Fragment decode error: {}", e.to_string()))?;
43        Ok(Self { inner })
44    }
45}