1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
//#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
//use noop_proc_macro::wasm_bindgen;

use std::io::{BufRead, Write};

use cml_crypto::{chain_crypto::hash::Blake2b224, Bip32PublicKey, PublicKey};

use crate::Coin;

// This library was code-generated using an experimental CDDL to rust tool:
// https://github.com/Emurgo/cddl-codegen

use cbor_event::{self, de::Deserializer, se::Serializer};

pub use self::crc32::Crc32;
pub use cml_core::network::ProtocolMagic;
pub use utils::{
    make_daedalus_bootstrap_witness, make_icarus_bootstrap_witness, AddressId, ByronAddressError,
    ByronScript, ParseExtendedAddrError, StakeholderId,
};

mod base58;
mod crc32;
mod serialization;
mod utils;

//#![allow(clippy::too_many_arguments)]

// This file was code-generated using an experimental CDDL to rust tool:
// https://github.com/dcSpark/cddl-codegen

#[derive(
    Clone,
    Debug,
    Eq,
    PartialEq,
    Ord,
    PartialOrd,
    Hash,
    serde::Deserialize,
    serde::Serialize,
    schemars::JsonSchema,
)]
pub struct AddrAttributes {
    pub stake_distribution: Option<StakeDistribution>,
    pub derivation_path: Option<HDAddressPayload>,
    pub protocol_magic: Option<ProtocolMagic>,
}

impl AddrAttributes {
    pub fn new() -> Self {
        Self {
            stake_distribution: None,
            derivation_path: None,
            protocol_magic: None,
        }
    }
}

impl Default for AddrAttributes {
    fn default() -> Self {
        Self::new()
    }
}

#[derive(
    Copy,
    Eq,
    PartialEq,
    Ord,
    PartialOrd,
    Clone,
    Debug,
    Hash,
    serde::Deserialize,
    serde::Serialize,
    schemars::JsonSchema,
)]
#[wasm_bindgen::prelude::wasm_bindgen]
pub enum ByronAddrType {
    PublicKey = 0,
    Script = 1,
    Redeem = 2,
}

#[derive(
    Clone,
    Debug,
    Eq,
    PartialEq,
    Ord,
    PartialOrd,
    Hash,
    serde::Deserialize,
    serde::Serialize,
    schemars::JsonSchema,
)]
pub struct AddressContent {
    pub address_id: AddressId,
    pub addr_attributes: AddrAttributes,
    pub addr_type: ByronAddrType,
}

impl AddressContent {
    pub fn new(
        address_id: AddressId,
        addr_attributes: AddrAttributes,
        addr_type: ByronAddrType,
    ) -> Self {
        Self {
            address_id,
            addr_attributes,
            addr_type,
        }
    }
}

#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct ByronAddress {
    pub content: AddressContent,
    pub crc: Crc32,
}

impl ByronAddress {
    /// Create a ByronAddress from an already calculated CRC32.
    /// Does not validate the crc whatsoever.
    /// use From<AddressContent> to calculate the CRC from the content
    pub fn new(content: AddressContent, crc: Crc32) -> Self {
        Self { content, crc }
    }
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub struct ByronTxOut {
    pub address: ByronAddress,
    pub amount: Coin,
}

impl ByronTxOut {
    pub fn new(address: ByronAddress, amount: Coin) -> Self {
        Self { address, amount }
    }
}

#[derive(
    Clone,
    Debug,
    Eq,
    PartialEq,
    Ord,
    PartialOrd,
    Hash,
    serde::Deserialize,
    serde::Serialize,
    schemars::JsonSchema,
)]
pub struct HDAddressPayload(pub Vec<u8>);

impl HDAddressPayload {
    pub fn get(&self) -> &Vec<u8> {
        &self.0
    }

    pub fn new(inner: Vec<u8>) -> Self {
        Self(inner)
    }
}

impl From<Vec<u8>> for HDAddressPayload {
    fn from(inner: Vec<u8>) -> Self {
        HDAddressPayload::new(inner)
    }
}

impl From<HDAddressPayload> for Vec<u8> {
    fn from(wrapper: HDAddressPayload) -> Self {
        wrapper.0
    }
}

#[derive(Clone, Debug, serde::Deserialize, serde::Serialize, schemars::JsonSchema)]
pub enum SpendingData {
    SpendingDataPubKey(Bip32PublicKey),
    SpendingDataScript(ByronScript),
    SpendingDataRedeem(PublicKey),
}

impl SpendingData {
    pub fn new_spending_data_pub_key(pubkey: Bip32PublicKey) -> Self {
        Self::SpendingDataPubKey(pubkey)
    }

    pub fn new_spending_data_script(script: ByronScript) -> Self {
        Self::SpendingDataScript(script)
    }

    pub fn new_spending_data_redeem(redeem: PublicKey) -> Self {
        Self::SpendingDataRedeem(redeem)
    }
}

#[derive(
    Clone,
    Debug,
    Eq,
    PartialEq,
    Ord,
    PartialOrd,
    Hash,
    serde::Deserialize,
    serde::Serialize,
    schemars::JsonSchema,
)]
pub enum StakeDistribution {
    SingleKey(StakeholderId),
    BootstrapEra,
}

impl StakeDistribution {
    pub fn new_single_key(stakeholder_id: StakeholderId) -> Self {
        Self::SingleKey(stakeholder_id)
    }

    pub fn new_bootstrap_era() -> Self {
        Self::BootstrapEra
    }
}

// #[cfg(test)]
// mod tests {
//     use cml_core::serialization::ToBytes;
//     use super::*;

//     #[test]
//     fn tx_output_decoding() {
//         let tx_out = ByronTxOut::from_bytes(
//             hex::decode("8282d818582183581cc6eb29e2cbb7b616b28c83da505a08253c33ec371319261ad93e558ca0001a1102942c1b00000005f817ddfc").unwrap()
//         ).unwrap();
//         assert_eq!(tx_out.address().to_base58(), "Ae2tdPwUPEZGexC4LXgsr1BJ1PppXk71zpuRkboFopVpSDcykQvpyYJXCJf");
//         assert!(tx_out.to_json().unwrap().contains("Ae2tdPwUPEZGexC4LXgsr1BJ1PppXk71zpuRkboFopVpSDcykQvpyYJXCJf"));
//     }
// }