cml_chain/byron/
mod.rs

1#[cfg(not(feature = "used_from_wasm"))]
2use noop_proc_macro::wasm_bindgen;
3#[cfg(feature = "used_from_wasm")]
4use wasm_bindgen::prelude::wasm_bindgen;
5
6use std::io::{BufRead, Write};
7
8use cml_crypto::{chain_crypto::hash::Blake2b224, Bip32PublicKey, PublicKey};
9
10use crate::Coin;
11
12// This library was code-generated using an experimental CDDL to rust tool:
13// https://github.com/Emurgo/cddl-codegen
14
15use cbor_event::{self, de::Deserializer, se::Serializer};
16
17pub use self::crc32::Crc32;
18pub use cml_core::network::ProtocolMagic;
19pub use utils::{
20    make_daedalus_bootstrap_witness, make_icarus_bootstrap_witness, AddressId, ByronAddressError,
21    ByronScript, ParseExtendedAddrError, StakeholderId,
22};
23
24mod base58;
25mod crc32;
26mod serialization;
27mod utils;
28
29//#![allow(clippy::too_many_arguments)]
30
31// This file was code-generated using an experimental CDDL to rust tool:
32// https://github.com/dcSpark/cddl-codegen
33
34#[derive(
35    Clone,
36    Debug,
37    Eq,
38    PartialEq,
39    Ord,
40    PartialOrd,
41    Hash,
42    serde::Deserialize,
43    serde::Serialize,
44    schemars::JsonSchema,
45)]
46pub struct AddrAttributes {
47    pub stake_distribution: Option<StakeDistribution>,
48    pub derivation_path: Option<HDAddressPayload>,
49    pub protocol_magic: Option<ProtocolMagic>,
50}
51
52impl AddrAttributes {
53    pub fn new() -> Self {
54        Self {
55            stake_distribution: None,
56            derivation_path: None,
57            protocol_magic: None,
58        }
59    }
60}
61
62impl Default for AddrAttributes {
63    fn default() -> Self {
64        Self::new()
65    }
66}
67
68#[derive(
69    Copy,
70    Eq,
71    PartialEq,
72    Ord,
73    PartialOrd,
74    Clone,
75    Debug,
76    Hash,
77    serde::Deserialize,
78    serde::Serialize,
79    schemars::JsonSchema,
80)]
81#[wasm_bindgen]
82pub enum ByronAddrType {
83    PublicKey = 0,
84    Script = 1,
85    Redeem = 2,
86}
87
88#[derive(
89    Clone,
90    Debug,
91    Eq,
92    PartialEq,
93    Ord,
94    PartialOrd,
95    Hash,
96    serde::Deserialize,
97    serde::Serialize,
98    schemars::JsonSchema,
99)]
100pub struct AddressContent {
101    pub address_id: AddressId,
102    pub addr_attributes: AddrAttributes,
103    pub addr_type: ByronAddrType,
104}
105
106impl AddressContent {
107    pub fn new(
108        address_id: AddressId,
109        addr_attributes: AddrAttributes,
110        addr_type: ByronAddrType,
111    ) -> Self {
112        Self {
113            address_id,
114            addr_attributes,
115            addr_type,
116        }
117    }
118}
119
120#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
121pub struct ByronAddress {
122    pub content: AddressContent,
123    pub crc: Crc32,
124}
125
126impl ByronAddress {
127    /// Create a ByronAddress from an already calculated CRC32.
128    /// Does not validate the crc whatsoever.
129    /// use From<AddressContent> to calculate the CRC from the content
130    pub fn new(content: AddressContent, crc: Crc32) -> Self {
131        Self { content, crc }
132    }
133}
134
135#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
136pub struct ByronTxOut {
137    pub address: ByronAddress,
138    pub amount: Coin,
139}
140
141impl ByronTxOut {
142    pub fn new(address: ByronAddress, amount: Coin) -> Self {
143        Self { address, amount }
144    }
145}
146
147#[derive(
148    Clone,
149    Debug,
150    Eq,
151    PartialEq,
152    Ord,
153    PartialOrd,
154    Hash,
155    serde::Deserialize,
156    serde::Serialize,
157    schemars::JsonSchema,
158)]
159pub struct HDAddressPayload(pub Vec<u8>);
160
161impl HDAddressPayload {
162    pub fn get(&self) -> &Vec<u8> {
163        &self.0
164    }
165
166    pub fn new(inner: Vec<u8>) -> Self {
167        Self(inner)
168    }
169}
170
171impl From<Vec<u8>> for HDAddressPayload {
172    fn from(inner: Vec<u8>) -> Self {
173        HDAddressPayload::new(inner)
174    }
175}
176
177impl From<HDAddressPayload> for Vec<u8> {
178    fn from(wrapper: HDAddressPayload) -> Self {
179        wrapper.0
180    }
181}
182
183#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
184pub enum SpendingData {
185    SpendingDataPubKey(Bip32PublicKey),
186    SpendingDataScript(ByronScript),
187    SpendingDataRedeem(PublicKey),
188}
189
190impl SpendingData {
191    pub fn new_spending_data_pub_key(pubkey: Bip32PublicKey) -> Self {
192        Self::SpendingDataPubKey(pubkey)
193    }
194
195    pub fn new_spending_data_script(script: ByronScript) -> Self {
196        Self::SpendingDataScript(script)
197    }
198
199    pub fn new_spending_data_redeem(redeem: PublicKey) -> Self {
200        Self::SpendingDataRedeem(redeem)
201    }
202}
203
204#[derive(
205    Clone,
206    Debug,
207    Eq,
208    PartialEq,
209    Ord,
210    PartialOrd,
211    Hash,
212    serde::Deserialize,
213    serde::Serialize,
214    schemars::JsonSchema,
215)]
216pub enum StakeDistribution {
217    SingleKey(StakeholderId),
218    BootstrapEra,
219}
220
221impl StakeDistribution {
222    pub fn new_single_key(stakeholder_id: StakeholderId) -> Self {
223        Self::SingleKey(stakeholder_id)
224    }
225
226    pub fn new_bootstrap_era() -> Self {
227        Self::BootstrapEra
228    }
229}
230
231// #[cfg(test)]
232// mod tests {
233//     use cml_core::serialization::ToBytes;
234//     use super::*;
235
236//     #[test]
237//     fn tx_output_decoding() {
238//         let tx_out = ByronTxOut::from_bytes(
239//             hex::decode("8282d818582183581cc6eb29e2cbb7b616b28c83da505a08253c33ec371319261ad93e558ca0001a1102942c1b00000005f817ddfc").unwrap()
240//         ).unwrap();
241//         assert_eq!(tx_out.address().to_base58(), "Ae2tdPwUPEZGexC4LXgsr1BJ1PppXk71zpuRkboFopVpSDcykQvpyYJXCJf");
242//         assert!(tx_out.to_json().unwrap().contains("Ae2tdPwUPEZGexC4LXgsr1BJ1PppXk71zpuRkboFopVpSDcykQvpyYJXCJf"));
243//     }
244// }