1#![allow(
2 clippy::len_without_is_empty,
3 clippy::too_many_arguments,
4 clippy::new_without_default
5)]
6use wasm_bindgen::prelude::wasm_bindgen;
10
11use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions, impl_wasm_json_api};
12
13use cml_chain_wasm::address::Address;
14pub mod utils;
15
16#[derive(Clone, Debug)]
17#[wasm_bindgen]
18pub struct CIP36Delegation(cml_cip36::CIP36Delegation);
19
20impl_wasm_cbor_json_api!(CIP36Delegation);
21
22impl_wasm_conversions!(cml_cip36::CIP36Delegation, CIP36Delegation);
23
24#[wasm_bindgen]
25impl CIP36Delegation {
26 pub fn voting_pub_key(&self) -> CIP36VotingPubKey {
27 self.0.voting_pub_key.clone().into()
28 }
29
30 pub fn weight(&self) -> CIP36Weight {
31 self.0.weight
32 }
33
34 pub fn new(voting_pub_key: &CIP36VotingPubKey, weight: CIP36Weight) -> Self {
35 Self(cml_cip36::CIP36Delegation::new(
36 voting_pub_key.clone().into(),
37 weight,
38 ))
39 }
40}
41
42#[derive(Clone, Debug)]
43#[wasm_bindgen]
44pub struct CIP36DelegationDistribution(cml_cip36::CIP36DelegationDistribution);
45
46impl_wasm_cbor_json_api!(CIP36DelegationDistribution);
47
48impl_wasm_conversions!(
49 cml_cip36::CIP36DelegationDistribution,
50 CIP36DelegationDistribution
51);
52
53#[wasm_bindgen]
54impl CIP36DelegationDistribution {
55 pub fn new_weighted(delegations: &CIP36DelegationList) -> Self {
56 Self(cml_cip36::CIP36DelegationDistribution::new_weighted(
57 delegations.clone().into(),
58 ))
59 }
60
61 pub fn new_legacy(legacy: &LegacyKeyRegistration) -> Self {
62 Self(cml_cip36::CIP36DelegationDistribution::new_legacy(
63 legacy.clone().into(),
64 ))
65 }
66
67 pub fn kind(&self) -> DelegationDistributionKind {
68 match &self.0 {
69 cml_cip36::CIP36DelegationDistribution::Weighted { .. } => {
70 DelegationDistributionKind::Weighted
71 }
72 cml_cip36::CIP36DelegationDistribution::Legacy { .. } => {
73 DelegationDistributionKind::Legacy
74 }
75 }
76 }
77
78 pub fn as_weighted(&self) -> Option<CIP36DelegationList> {
79 match &self.0 {
80 cml_cip36::CIP36DelegationDistribution::Weighted { delegations, .. } => {
81 Some(delegations.clone().into())
82 }
83 _ => None,
84 }
85 }
86
87 pub fn as_legacy(&self) -> Option<LegacyKeyRegistration> {
88 match &self.0 {
89 cml_cip36::CIP36DelegationDistribution::Legacy { legacy, .. } => {
90 Some(legacy.clone().into())
91 }
92 _ => None,
93 }
94 }
95}
96
97#[wasm_bindgen]
98pub enum DelegationDistributionKind {
99 Weighted,
100 Legacy,
101}
102
103#[derive(Clone, Debug)]
104#[wasm_bindgen]
105pub struct CIP36DelegationList(Vec<cml_cip36::CIP36Delegation>);
106
107#[wasm_bindgen]
108impl CIP36DelegationList {
109 pub fn new() -> Self {
110 Self(Vec::new())
111 }
112
113 pub fn len(&self) -> usize {
114 self.0.len()
115 }
116
117 pub fn get(&self, index: usize) -> CIP36Delegation {
118 self.0[index].clone().into()
119 }
120
121 pub fn add(&mut self, elem: &CIP36Delegation) {
122 self.0.push(elem.clone().into());
123 }
124}
125
126impl From<Vec<cml_cip36::CIP36Delegation>> for CIP36DelegationList {
127 fn from(native: Vec<cml_cip36::CIP36Delegation>) -> Self {
128 Self(native)
129 }
130}
131
132impl From<CIP36DelegationList> for Vec<cml_cip36::CIP36Delegation> {
133 fn from(wasm: CIP36DelegationList) -> Self {
134 wasm.0
135 }
136}
137
138impl AsRef<Vec<cml_cip36::CIP36Delegation>> for CIP36DelegationList {
139 fn as_ref(&self) -> &Vec<cml_cip36::CIP36Delegation> {
140 &self.0
141 }
142}
143
144#[derive(Clone, Debug)]
145#[wasm_bindgen]
146pub struct CIP36DeregistrationCbor(cml_cip36::CIP36DeregistrationCbor);
147
148impl_wasm_json_api!(CIP36DeregistrationCbor);
150
151impl_wasm_conversions!(cml_cip36::CIP36DeregistrationCbor, CIP36DeregistrationCbor);
152
153#[wasm_bindgen]
154impl CIP36DeregistrationCbor {
155 pub fn key_deregistration(&self) -> CIP36KeyDeregistration {
156 self.0.key_deregistration.clone().into()
157 }
158
159 pub fn deregistration_witness(&self) -> CIP36DeregistrationWitness {
160 self.0.deregistration_witness.clone().into()
161 }
162
163 pub fn new(
164 key_deregistration: &CIP36KeyDeregistration,
165 deregistration_witness: &CIP36DeregistrationWitness,
166 ) -> Self {
167 Self(cml_cip36::CIP36DeregistrationCbor::new(
168 key_deregistration.clone().into(),
169 deregistration_witness.clone().into(),
170 ))
171 }
172}
173
174#[derive(Clone, Debug)]
175#[wasm_bindgen]
176pub struct CIP36DeregistrationWitness(cml_cip36::CIP36DeregistrationWitness);
177
178impl_wasm_cbor_json_api!(CIP36DeregistrationWitness);
179
180impl_wasm_conversions!(
181 cml_cip36::CIP36DeregistrationWitness,
182 CIP36DeregistrationWitness
183);
184
185#[wasm_bindgen]
186impl CIP36DeregistrationWitness {
187 pub fn stake_witness(&self) -> CIP36StakeWitness {
188 self.0.stake_witness.clone().into()
189 }
190
191 pub fn new(stake_witness: &CIP36StakeWitness) -> Self {
192 Self(cml_cip36::CIP36DeregistrationWitness::new(
193 stake_witness.clone().into(),
194 ))
195 }
196}
197
198#[derive(Clone, Debug)]
199#[wasm_bindgen]
200pub struct CIP36KeyDeregistration(cml_cip36::CIP36KeyDeregistration);
201
202impl_wasm_cbor_json_api!(CIP36KeyDeregistration);
203
204impl_wasm_conversions!(cml_cip36::CIP36KeyDeregistration, CIP36KeyDeregistration);
205
206#[wasm_bindgen]
207impl CIP36KeyDeregistration {
208 pub fn stake_credential(&self) -> CIP36StakeCredential {
209 self.0.stake_credential.clone().into()
210 }
211
212 pub fn nonce(&self) -> CIP36Nonce {
213 self.0.nonce
214 }
215
216 pub fn set_voting_purpose(&mut self, voting_purpose: CIP36VotingPurpose) {
217 self.0.voting_purpose = voting_purpose
218 }
219
220 pub fn voting_purpose(&self) -> CIP36VotingPurpose {
221 self.0.voting_purpose
222 }
223}
224
225#[derive(Clone, Debug)]
226#[wasm_bindgen]
227pub struct CIP36KeyRegistration(cml_cip36::CIP36KeyRegistration);
228
229impl_wasm_cbor_json_api!(CIP36KeyRegistration);
230
231impl_wasm_conversions!(cml_cip36::CIP36KeyRegistration, CIP36KeyRegistration);
232
233#[wasm_bindgen]
234impl CIP36KeyRegistration {
235 pub fn delegation(&self) -> CIP36DelegationDistribution {
236 self.0.delegation.clone().into()
237 }
238
239 pub fn stake_credential(&self) -> CIP36StakeCredential {
240 self.0.stake_credential.clone().into()
241 }
242
243 pub fn payment_address(&self) -> Address {
244 self.0.payment_address.clone().into()
245 }
246
247 pub fn nonce(&self) -> CIP36Nonce {
248 self.0.nonce
249 }
250
251 pub fn set_voting_purpose(&mut self, voting_purpose: CIP36VotingPurpose) {
252 self.0.voting_purpose = voting_purpose
253 }
254
255 pub fn voting_purpose(&self) -> CIP36VotingPurpose {
256 self.0.voting_purpose
257 }
258}
259
260pub type LegacyKeyRegistration = cml_crypto_wasm::PublicKey;
261
262pub type CIP36Nonce = u64;
263
264#[derive(Clone, Debug)]
265#[wasm_bindgen]
266pub struct CIP36RegistrationCbor(cml_cip36::CIP36RegistrationCbor);
267
268impl_wasm_json_api!(CIP36RegistrationCbor);
270
271impl_wasm_conversions!(cml_cip36::CIP36RegistrationCbor, CIP36RegistrationCbor);
272
273#[wasm_bindgen]
274impl CIP36RegistrationCbor {
275 pub fn key_registration(&self) -> CIP36KeyRegistration {
276 self.0.key_registration.clone().into()
277 }
278
279 pub fn registration_witness(&self) -> CIP36RegistrationWitness {
280 self.0.registration_witness.clone().into()
281 }
282
283 pub fn new(
284 key_registration: &CIP36KeyRegistration,
285 registration_witness: &CIP36RegistrationWitness,
286 ) -> Self {
287 Self(cml_cip36::CIP36RegistrationCbor::new(
288 key_registration.clone().into(),
289 registration_witness.clone().into(),
290 ))
291 }
292}
293
294#[derive(Clone, Debug)]
295#[wasm_bindgen]
296pub struct CIP36RegistrationWitness(cml_cip36::CIP36RegistrationWitness);
297
298impl_wasm_cbor_json_api!(CIP36RegistrationWitness);
299
300impl_wasm_conversions!(
301 cml_cip36::CIP36RegistrationWitness,
302 CIP36RegistrationWitness
303);
304
305#[wasm_bindgen]
306impl CIP36RegistrationWitness {
307 pub fn stake_witness(&self) -> CIP36StakeWitness {
308 self.0.stake_witness.clone().into()
309 }
310
311 pub fn new(stake_witness: &CIP36StakeWitness) -> Self {
312 Self(cml_cip36::CIP36RegistrationWitness::new(
313 stake_witness.clone().into(),
314 ))
315 }
316}
317
318pub type CIP36StakeCredential = cml_crypto_wasm::PublicKey;
319
320pub type CIP36StakeWitness = cml_crypto_wasm::Ed25519Signature;
321
322pub type CIP36StakingPubKey = cml_crypto_wasm::PublicKey;
323
324pub type CIP36VotingPubKey = cml_crypto_wasm::PublicKey;
325
326pub type CIP36VotingPurpose = u64;
327
328pub type CIP36Weight = u32;