cml_multi_era/shelley/
utils.rs

1use cml_chain::{
2    certs::{DNSName, PoolParams, PoolRegistration, Relay},
3    transaction::{NativeScript, TransactionWitnessSet},
4};
5
6use super::{
7    MultisigScript, ShelleyPoolRegistration, ShelleyRelay, ShelleyTransactionBody,
8    ShelleyTransactionWitnessSet,
9};
10
11use cml_core::serialization::Serialize;
12use cml_crypto::{blake2b256, TransactionHash};
13
14impl ShelleyTransactionBody {
15    pub fn hash(&self) -> TransactionHash {
16        blake2b256(&self.to_cbor_bytes()).into()
17    }
18}
19
20impl From<ShelleyTransactionWitnessSet> for TransactionWitnessSet {
21    fn from(wits: ShelleyTransactionWitnessSet) -> Self {
22        let mut new_wits = TransactionWitnessSet::new();
23        new_wits.vkeywitnesses = wits.vkeywitnesses.map(Into::into);
24        new_wits.native_scripts = wits.native_scripts.map(|native_scripts| {
25            native_scripts
26                .into_iter()
27                .map(NativeScript::from)
28                .collect::<Vec<_>>()
29                .into()
30        });
31        new_wits.bootstrap_witnesses = wits.bootstrap_witnesses.map(Into::into);
32        new_wits
33    }
34}
35
36impl From<MultisigScript> for NativeScript {
37    fn from(script: MultisigScript) -> Self {
38        match script {
39            MultisigScript::MultisigPubkey(key) => {
40                NativeScript::new_script_pubkey(key.ed25519_key_hash)
41            }
42            MultisigScript::MultisigAll(all) => NativeScript::new_script_all(
43                all.multisig_scripts
44                    .into_iter()
45                    .map(NativeScript::from)
46                    .collect(),
47            ),
48            MultisigScript::MultisigAny(any) => NativeScript::new_script_any(
49                any.multisig_scripts
50                    .into_iter()
51                    .map(NativeScript::from)
52                    .collect(),
53            ),
54            MultisigScript::MultisigNOfK(nok) => NativeScript::new_script_n_of_k(
55                nok.n,
56                nok.multisig_scripts
57                    .into_iter()
58                    .map(NativeScript::from)
59                    .collect(),
60            ),
61        }
62    }
63}
64
65impl From<ShelleyPoolRegistration> for PoolRegistration {
66    fn from(pool_reg: ShelleyPoolRegistration) -> Self {
67        Self::new(PoolParams::new(
68            pool_reg.pool_params.operator,
69            pool_reg.pool_params.vrf_keyhash,
70            pool_reg.pool_params.pledge,
71            pool_reg.pool_params.cost,
72            pool_reg.pool_params.margin,
73            pool_reg.pool_params.reward_account,
74            pool_reg.pool_params.pool_owners.into(),
75            pool_reg
76                .pool_params
77                .relays
78                .into_iter()
79                .map(Into::into)
80                .collect(),
81            pool_reg.pool_params.pool_metadata,
82        ))
83    }
84}
85
86impl From<ShelleyRelay> for Relay {
87    fn from(relay: ShelleyRelay) -> Self {
88        match relay {
89            ShelleyRelay::SingleHostAddr(host) => {
90                Self::new_single_host_addr(host.port, host.ipv4, host.ipv6)
91            }
92            ShelleyRelay::ShelleySingleHostName(host) => Self::new_single_host_name(
93                host.port,
94                DNSName::new(host.shelley_dns_name.inner).unwrap(),
95            ),
96            ShelleyRelay::ShelleyMultiHostName(host) => {
97                Self::new_multi_host_name(DNSName::new(host.shelley_dns_name.inner).unwrap())
98            }
99        }
100    }
101}