cml_chain_wasm/byron/
utils.rs1use cml_crypto_wasm::{
2 impl_hash_type_ext, Bip32PrivateKey, Bip32PublicKey, LegacyDaedalusPrivateKey, PublicKey,
3 TransactionHash,
4};
5use wasm_bindgen::{prelude::wasm_bindgen, JsError};
6
7use crate::{
8 address::Address,
9 byron::{AddrAttributes, ByronAddress, HDAddressPayload, SpendingData},
10 crypto::BootstrapWitness,
11};
12pub use cml_chain::byron::ByronAddrType;
14
15use super::AddressContent;
16
17impl_hash_type_ext!(cml_chain::byron::AddressId, AddressId);
18impl_hash_type_ext!(cml_chain::byron::ByronScript, ByronScript);
20impl_hash_type_ext!(cml_chain::byron::StakeholderId, StakeholderId);
21
22#[wasm_bindgen]
23impl StakeholderId {
24 pub fn new(pubk: &Bip32PublicKey) -> StakeholderId {
25 Self(cml_chain::byron::StakeholderId::new(pubk.as_ref()))
26 }
27}
28
29#[wasm_bindgen]
30impl AddrAttributes {
31 pub fn new_bootstrap_era(
32 hdap: Option<HDAddressPayload>,
33 protocol_magic: Option<ProtocolMagic>,
34 ) -> Self {
35 cml_chain::byron::AddrAttributes::new_bootstrap_era(
36 hdap.map(Into::into),
37 protocol_magic.map(Into::into),
38 )
39 .into()
40 }
41
42 pub fn new_single_key(
43 pubk: &Bip32PublicKey,
44 hdap: Option<HDAddressPayload>,
45 protocol_magic: ProtocolMagic,
46 ) -> Self {
47 cml_chain::byron::AddrAttributes::new_single_key(
48 pubk.as_ref(),
49 hdap.map(Into::into),
50 protocol_magic.into(),
51 )
52 .into()
53 }
54}
55
56#[wasm_bindgen]
57impl AddressId {
58 pub fn new(
59 addr_type: ByronAddrType,
60 spending_data: &SpendingData,
61 attrs: &AddrAttributes,
62 ) -> Self {
63 cml_chain::byron::AddressId::new(addr_type, spending_data.as_ref(), attrs.as_ref()).into()
64 }
65}
66
67#[wasm_bindgen]
68impl AddressContent {
69 pub fn hash_and_create(
70 addr_type: ByronAddrType,
71 spending_data: &SpendingData,
72 attributes: &AddrAttributes,
73 ) -> AddressContent {
74 cml_chain::byron::AddressContent::hash_and_create(
75 addr_type,
76 spending_data.as_ref(),
77 attributes.clone().into(),
78 )
79 .into()
80 }
81
82 pub fn new_redeem(pubkey: &PublicKey, protocol_magic: Option<ProtocolMagic>) -> Self {
84 cml_chain::byron::AddressContent::new_redeem(
85 pubkey.clone().into(),
86 protocol_magic.map(Into::into),
87 )
88 .into()
89 }
90
91 pub fn new_simple(xpub: &Bip32PublicKey, protocol_magic: Option<ProtocolMagic>) -> Self {
93 cml_chain::byron::AddressContent::new_simple(
94 xpub.clone().into(),
95 protocol_magic.map(Into::into),
96 )
97 .into()
98 }
99
100 pub fn to_address(&self) -> ByronAddress {
102 self.0.to_address().into()
103 }
104
105 pub fn byron_protocol_magic(&self) -> ProtocolMagic {
108 self.0.byron_protocol_magic().into()
109 }
110
111 pub fn network_id(&self) -> Result<u8, JsError> {
112 self.0.network_id().map_err(Into::into)
113 }
114
115 pub fn icarus_from_key(key: &Bip32PublicKey, protocol_magic: &ProtocolMagic) -> AddressContent {
117 cml_chain::byron::AddressContent::icarus_from_key(
118 key.clone().into(),
119 (*protocol_magic).into(),
120 )
121 .into()
122 }
123
124 pub fn identical_with_pubkey(&self, xpub: &Bip32PublicKey) -> bool {
126 self.0.identical_with_pubkey(xpub.clone().into())
127 }
128}
129
130#[wasm_bindgen]
131impl ByronAddress {
132 pub fn to_base58(&self) -> String {
133 self.0.to_base58()
134 }
135
136 pub fn from_base58(s: &str) -> Result<ByronAddress, JsError> {
137 cml_chain::byron::ByronAddress::from_base58(s)
138 .map(Self)
139 .map_err(|e| JsError::new(&format!("ByronAddress::from_base58: {e:?}")))
140 }
141
142 pub fn is_valid(s: &str) -> bool {
143 cml_chain::byron::ByronAddress::is_valid(s)
144 }
145
146 pub fn to_address(&self) -> Address {
147 self.0.clone().to_address().into()
148 }
149
150 pub fn from_address(addr: &Address) -> Option<ByronAddress> {
151 cml_chain::byron::ByronAddress::from_address(addr.as_ref()).map(Self)
152 }
153
154 pub fn from_address_content(address_content: &AddressContent) -> Self {
155 cml_chain::byron::ByronAddress::from(address_content.as_ref().clone()).into()
156 }
157}
158
159#[wasm_bindgen]
160#[derive(Clone, Copy)]
161pub struct ProtocolMagic(cml_chain::byron::ProtocolMagic);
162
163#[wasm_bindgen]
164impl ProtocolMagic {
165 pub fn new(pm: u32) -> Self {
166 Self(pm.into())
167 }
168
169 pub fn to_int(&self) -> u32 {
170 self.0.into()
171 }
172}
173
174impl From<cml_chain::byron::ProtocolMagic> for ProtocolMagic {
175 fn from(native: cml_chain::byron::ProtocolMagic) -> Self {
176 Self(native)
177 }
178}
179
180impl From<ProtocolMagic> for cml_chain::byron::ProtocolMagic {
181 fn from(wasm: ProtocolMagic) -> Self {
182 wasm.0
183 }
184}
185
186impl AsRef<cml_chain::byron::ProtocolMagic> for ProtocolMagic {
187 fn as_ref(&self) -> &cml_chain::byron::ProtocolMagic {
188 &self.0
189 }
190}
191
192#[wasm_bindgen]
193pub fn make_daedalus_bootstrap_witness(
194 tx_body_hash: &TransactionHash,
195 addr: &ByronAddress,
196 key: &LegacyDaedalusPrivateKey,
197) -> BootstrapWitness {
198 cml_chain::byron::make_daedalus_bootstrap_witness(
199 tx_body_hash.clone().into(),
200 addr.clone().into(),
201 key.clone().into(),
202 )
203 .into()
204}
205
206#[wasm_bindgen]
207pub fn make_icarus_bootstrap_witness(
208 tx_body_hash: TransactionHash,
209 addr: ByronAddress,
210 key: &Bip32PrivateKey,
211) -> BootstrapWitness {
212 cml_chain::byron::make_icarus_bootstrap_witness(tx_body_hash.into(), addr.into(), key.as_ref())
213 .into()
214}