plutus_ledger_api/v1/
script.rs

1//! Types related to Plutus scripts
2
3use cardano_serialization_lib as csl;
4
5#[cfg(feature = "lbf")]
6use lbr_prelude::json::Json;
7#[cfg(feature = "serde")]
8use serde::{Deserialize, Serialize};
9
10use crate as plutus_ledger_api;
11use crate::aux::guard_bytes;
12use crate::csl::csl_to_pla::FromCSL;
13use crate::csl::pla_to_csl::{TryFromPLA, TryFromPLAError, TryToCSL};
14use crate::error::ConversionError;
15use crate::plutus_data::IsPlutusData;
16use crate::v1::crypto::LedgerBytes;
17
18///////////////////
19// ValidatorHash //
20///////////////////
21
22/// Identifier of a validator script
23#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, IsPlutusData)]
24#[is_plutus_data_derive_strategy = "Newtype"]
25#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
26#[cfg_attr(feature = "lbf", derive(Json))]
27pub struct ValidatorHash(pub ScriptHash);
28
29impl FromCSL<csl::ScriptHash> for ValidatorHash {
30    fn from_csl(value: &csl::ScriptHash) -> Self {
31        ValidatorHash(ScriptHash::from_csl(value))
32    }
33}
34
35impl ValidatorHash {
36    pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, ConversionError> {
37        Ok(ValidatorHash(ScriptHash::from_bytes(bytes)?))
38    }
39}
40
41///////////////////////
42// MintingPolicyHash //
43///////////////////////
44
45/// Hash of a minting policy script
46#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, IsPlutusData)]
47#[is_plutus_data_derive_strategy = "Newtype"]
48#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
49#[cfg_attr(feature = "lbf", derive(Json))]
50pub struct MintingPolicyHash(pub ScriptHash);
51
52impl MintingPolicyHash {
53    pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, ConversionError> {
54        Ok(MintingPolicyHash(ScriptHash::from_bytes(bytes)?))
55    }
56}
57
58impl FromCSL<csl::PolicyID> for MintingPolicyHash {
59    fn from_csl(value: &csl::PolicyID) -> Self {
60        MintingPolicyHash(ScriptHash(LedgerBytes(value.to_bytes())))
61    }
62}
63
64impl TryFromPLA<MintingPolicyHash> for csl::PolicyID {
65    fn try_from_pla(val: &MintingPolicyHash) -> Result<Self, TryFromPLAError> {
66        val.0.try_to_csl()
67    }
68}
69
70////////////////
71// ScriptHash //
72////////////////
73
74/// Hash of a Plutus script
75#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, IsPlutusData)]
76#[is_plutus_data_derive_strategy = "Newtype"]
77#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
78#[cfg_attr(feature = "lbf", derive(Json))]
79pub struct ScriptHash(pub LedgerBytes);
80
81impl ScriptHash {
82    pub fn from_bytes(bytes: Vec<u8>) -> Result<Self, ConversionError> {
83        Ok(ScriptHash(LedgerBytes(guard_bytes(
84            "ScriptHash",
85            bytes,
86            28,
87        )?)))
88    }
89}
90
91impl FromCSL<csl::ScriptHash> for ScriptHash {
92    fn from_csl(value: &csl::ScriptHash) -> Self {
93        ScriptHash(LedgerBytes(value.to_bytes()))
94    }
95}
96
97impl TryFromPLA<ScriptHash> for csl::ScriptHash {
98    fn try_from_pla(val: &ScriptHash) -> Result<Self, TryFromPLAError> {
99        csl::ScriptHash::from_bytes(val.0 .0.to_owned())
100            .map_err(TryFromPLAError::CSLDeserializeError)
101    }
102}