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
use cml_crypto_wasm::{
    impl_hash_type_ext, Bip32PrivateKey, Bip32PublicKey, LegacyDaedalusPrivateKey, PublicKey,
    TransactionHash,
};
use wasm_bindgen::{prelude::wasm_bindgen, JsError};

use crate::{
    address::Address,
    byron::{AddrAttributes, ByronAddress, HDAddressPayload, SpendingData},
    crypto::BootstrapWitness,
};
// this is alredy wasm-exposed since enum
pub use cml_chain::byron::ByronAddrType;

use super::AddressContent;

impl_hash_type_ext!(cml_chain::byron::AddressId, AddressId);
// not sure if this is a hash but it likely is and has the same byte format
impl_hash_type_ext!(cml_chain::byron::ByronScript, ByronScript);
impl_hash_type_ext!(cml_chain::byron::StakeholderId, StakeholderId);

#[wasm_bindgen]
impl StakeholderId {
    pub fn new(pubk: &Bip32PublicKey) -> StakeholderId {
        Self(cml_chain::byron::StakeholderId::new(pubk.as_ref()))
    }
}

#[wasm_bindgen]
impl AddrAttributes {
    pub fn new_bootstrap_era(
        hdap: Option<HDAddressPayload>,
        protocol_magic: Option<ProtocolMagic>,
    ) -> Self {
        cml_chain::byron::AddrAttributes::new_bootstrap_era(
            hdap.map(Into::into),
            protocol_magic.map(Into::into),
        )
        .into()
    }

    pub fn new_single_key(
        pubk: &Bip32PublicKey,
        hdap: Option<HDAddressPayload>,
        protocol_magic: ProtocolMagic,
    ) -> Self {
        cml_chain::byron::AddrAttributes::new_single_key(
            pubk.as_ref(),
            hdap.map(Into::into),
            protocol_magic.into(),
        )
        .into()
    }
}

#[wasm_bindgen]
impl AddressId {
    pub fn new(
        addr_type: ByronAddrType,
        spending_data: &SpendingData,
        attrs: &AddrAttributes,
    ) -> Self {
        cml_chain::byron::AddressId::new(addr_type, spending_data.as_ref(), attrs.as_ref()).into()
    }
}

#[wasm_bindgen]
impl AddressContent {
    pub fn hash_and_create(
        addr_type: ByronAddrType,
        spending_data: &SpendingData,
        attributes: &AddrAttributes,
    ) -> AddressContent {
        cml_chain::byron::AddressContent::hash_and_create(
            addr_type,
            spending_data.as_ref(),
            attributes.clone().into(),
        )
        .into()
    }

    // bootstrap era + no hdpayload address
    pub fn new_redeem(pubkey: &PublicKey, protocol_magic: Option<ProtocolMagic>) -> Self {
        cml_chain::byron::AddressContent::new_redeem(
            pubkey.clone().into(),
            protocol_magic.map(Into::into),
        )
        .into()
    }

    // bootstrap era + no hdpayload address
    pub fn new_simple(xpub: &Bip32PublicKey, protocol_magic: Option<ProtocolMagic>) -> Self {
        cml_chain::byron::AddressContent::new_simple(
            xpub.clone().into(),
            protocol_magic.map(Into::into),
        )
        .into()
    }

    /// Do we want to remove this or keep it for people who were using old Byron code?
    pub fn to_address(&self) -> ByronAddress {
        self.0.to_address().into()
    }

    /// returns the byron protocol magic embedded in the address, or mainnet id if none is present
    /// note: for bech32 addresses, you need to use network_id instead
    pub fn byron_protocol_magic(&self) -> ProtocolMagic {
        self.0.byron_protocol_magic().into()
    }

    pub fn network_id(&self) -> Result<u8, JsError> {
        self.0.network_id().map_err(Into::into)
    }

    // icarus-style address (Ae2)
    pub fn icarus_from_key(key: &Bip32PublicKey, protocol_magic: &ProtocolMagic) -> AddressContent {
        cml_chain::byron::AddressContent::icarus_from_key(
            key.clone().into(),
            (*protocol_magic).into(),
        )
        .into()
    }

    /// Check if the Addr can be reconstructed with a specific xpub
    pub fn identical_with_pubkey(&self, xpub: &Bip32PublicKey) -> bool {
        self.0.identical_with_pubkey(xpub.clone().into())
    }
}

#[wasm_bindgen]
impl ByronAddress {
    pub fn to_base58(&self) -> String {
        self.0.to_base58()
    }

    pub fn from_base58(s: &str) -> Result<ByronAddress, JsError> {
        cml_chain::byron::ByronAddress::from_base58(s)
            .map(Self)
            .map_err(|e| JsError::new(&format!("ByronAddress::from_base58: {e:?}")))
    }

    pub fn is_valid(s: &str) -> bool {
        cml_chain::byron::ByronAddress::is_valid(s)
    }

    pub fn to_address(&self) -> Address {
        self.0.clone().to_address().into()
    }

    pub fn from_address(addr: &Address) -> Option<ByronAddress> {
        cml_chain::byron::ByronAddress::from_address(addr.as_ref()).map(Self)
    }

    pub fn from_address_content(address_content: &AddressContent) -> Self {
        cml_chain::byron::ByronAddress::from(address_content.as_ref().clone()).into()
    }
}

#[wasm_bindgen]
#[derive(Clone, Copy)]
pub struct ProtocolMagic(cml_chain::byron::ProtocolMagic);

#[wasm_bindgen]
impl ProtocolMagic {
    pub fn new(pm: u32) -> Self {
        Self(pm.into())
    }

    pub fn to_int(&self) -> u32 {
        self.0.into()
    }
}

impl From<cml_chain::byron::ProtocolMagic> for ProtocolMagic {
    fn from(native: cml_chain::byron::ProtocolMagic) -> Self {
        Self(native)
    }
}

impl From<ProtocolMagic> for cml_chain::byron::ProtocolMagic {
    fn from(wasm: ProtocolMagic) -> Self {
        wasm.0
    }
}

impl AsRef<cml_chain::byron::ProtocolMagic> for ProtocolMagic {
    fn as_ref(&self) -> &cml_chain::byron::ProtocolMagic {
        &self.0
    }
}

#[wasm_bindgen]
pub fn make_daedalus_bootstrap_witness(
    tx_body_hash: &TransactionHash,
    addr: &ByronAddress,
    key: &LegacyDaedalusPrivateKey,
) -> BootstrapWitness {
    cml_chain::byron::make_daedalus_bootstrap_witness(
        tx_body_hash.clone().into(),
        addr.clone().into(),
        key.clone().into(),
    )
    .into()
}

#[wasm_bindgen]
pub fn make_icarus_bootstrap_witness(
    tx_body_hash: TransactionHash,
    addr: ByronAddress,
    key: &Bip32PrivateKey,
) -> BootstrapWitness {
    cml_chain::byron::make_icarus_bootstrap_witness(tx_body_hash.into(), addr.into(), key.as_ref())
        .into()
}