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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
#![allow(
    clippy::len_without_is_empty,
    clippy::too_many_arguments,
    clippy::new_without_default
)]
// This file was code-generated using an experimental CDDL to rust tool:
// https://github.com/dcSpark/cddl-codegen

use wasm_bindgen::prelude::wasm_bindgen;

use cml_core_wasm::{impl_wasm_cbor_json_api, impl_wasm_conversions, impl_wasm_json_api};

use cml_chain_wasm::address::Address;
pub mod utils;

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct CIP36Delegation(cml_cip36::CIP36Delegation);

impl_wasm_cbor_json_api!(CIP36Delegation);

impl_wasm_conversions!(cml_cip36::CIP36Delegation, CIP36Delegation);

#[wasm_bindgen]
impl CIP36Delegation {
    pub fn voting_pub_key(&self) -> CIP36VotingPubKey {
        self.0.voting_pub_key.clone().into()
    }

    pub fn weight(&self) -> CIP36Weight {
        self.0.weight
    }

    pub fn new(voting_pub_key: &CIP36VotingPubKey, weight: CIP36Weight) -> Self {
        Self(cml_cip36::CIP36Delegation::new(
            voting_pub_key.clone().into(),
            weight,
        ))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct CIP36DelegationDistribution(cml_cip36::CIP36DelegationDistribution);

impl_wasm_cbor_json_api!(CIP36DelegationDistribution);

impl_wasm_conversions!(
    cml_cip36::CIP36DelegationDistribution,
    CIP36DelegationDistribution
);

#[wasm_bindgen]
impl CIP36DelegationDistribution {
    pub fn new_weighted(delegations: &CIP36DelegationList) -> Self {
        Self(cml_cip36::CIP36DelegationDistribution::new_weighted(
            delegations.clone().into(),
        ))
    }

    pub fn new_legacy(legacy: &LegacyKeyRegistration) -> Self {
        Self(cml_cip36::CIP36DelegationDistribution::new_legacy(
            legacy.clone().into(),
        ))
    }

    pub fn kind(&self) -> DelegationDistributionKind {
        match &self.0 {
            cml_cip36::CIP36DelegationDistribution::Weighted { .. } => {
                DelegationDistributionKind::Weighted
            }
            cml_cip36::CIP36DelegationDistribution::Legacy { .. } => {
                DelegationDistributionKind::Legacy
            }
        }
    }

    pub fn as_weighted(&self) -> Option<CIP36DelegationList> {
        match &self.0 {
            cml_cip36::CIP36DelegationDistribution::Weighted { delegations, .. } => {
                Some(delegations.clone().into())
            }
            _ => None,
        }
    }

    pub fn as_legacy(&self) -> Option<LegacyKeyRegistration> {
        match &self.0 {
            cml_cip36::CIP36DelegationDistribution::Legacy { legacy, .. } => {
                Some(legacy.clone().into())
            }
            _ => None,
        }
    }
}

#[wasm_bindgen]
pub enum DelegationDistributionKind {
    Weighted,
    Legacy,
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct CIP36DelegationList(Vec<cml_cip36::CIP36Delegation>);

#[wasm_bindgen]
impl CIP36DelegationList {
    pub fn new() -> Self {
        Self(Vec::new())
    }

    pub fn len(&self) -> usize {
        self.0.len()
    }

    pub fn get(&self, index: usize) -> CIP36Delegation {
        self.0[index].clone().into()
    }

    pub fn add(&mut self, elem: &CIP36Delegation) {
        self.0.push(elem.clone().into());
    }
}

impl From<Vec<cml_cip36::CIP36Delegation>> for CIP36DelegationList {
    fn from(native: Vec<cml_cip36::CIP36Delegation>) -> Self {
        Self(native)
    }
}

impl From<CIP36DelegationList> for Vec<cml_cip36::CIP36Delegation> {
    fn from(wasm: CIP36DelegationList) -> Self {
        wasm.0
    }
}

impl AsRef<Vec<cml_cip36::CIP36Delegation>> for CIP36DelegationList {
    fn as_ref(&self) -> &Vec<cml_cip36::CIP36Delegation> {
        &self.0
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct CIP36DeregistrationCbor(cml_cip36::CIP36DeregistrationCbor);

// CIP36DeregistrationCbor does not implement Serialize as it may be a subset of metadata
impl_wasm_json_api!(CIP36DeregistrationCbor);

impl_wasm_conversions!(cml_cip36::CIP36DeregistrationCbor, CIP36DeregistrationCbor);

#[wasm_bindgen]
impl CIP36DeregistrationCbor {
    pub fn key_deregistration(&self) -> CIP36KeyDeregistration {
        self.0.key_deregistration.clone().into()
    }

    pub fn deregistration_witness(&self) -> CIP36DeregistrationWitness {
        self.0.deregistration_witness.clone().into()
    }

    pub fn new(
        key_deregistration: &CIP36KeyDeregistration,
        deregistration_witness: &CIP36DeregistrationWitness,
    ) -> Self {
        Self(cml_cip36::CIP36DeregistrationCbor::new(
            key_deregistration.clone().into(),
            deregistration_witness.clone().into(),
        ))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct CIP36DeregistrationWitness(cml_cip36::CIP36DeregistrationWitness);

impl_wasm_cbor_json_api!(CIP36DeregistrationWitness);

impl_wasm_conversions!(
    cml_cip36::CIP36DeregistrationWitness,
    CIP36DeregistrationWitness
);

#[wasm_bindgen]
impl CIP36DeregistrationWitness {
    pub fn stake_witness(&self) -> CIP36StakeWitness {
        self.0.stake_witness.clone().into()
    }

    pub fn new(stake_witness: &CIP36StakeWitness) -> Self {
        Self(cml_cip36::CIP36DeregistrationWitness::new(
            stake_witness.clone().into(),
        ))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct CIP36KeyDeregistration(cml_cip36::CIP36KeyDeregistration);

impl_wasm_cbor_json_api!(CIP36KeyDeregistration);

impl_wasm_conversions!(cml_cip36::CIP36KeyDeregistration, CIP36KeyDeregistration);

#[wasm_bindgen]
impl CIP36KeyDeregistration {
    pub fn stake_credential(&self) -> CIP36StakeCredential {
        self.0.stake_credential.clone().into()
    }

    pub fn nonce(&self) -> CIP36Nonce {
        self.0.nonce
    }

    pub fn set_voting_purpose(&mut self, voting_purpose: CIP36VotingPurpose) {
        self.0.voting_purpose = voting_purpose
    }

    pub fn voting_purpose(&self) -> CIP36VotingPurpose {
        self.0.voting_purpose
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct CIP36KeyRegistration(cml_cip36::CIP36KeyRegistration);

impl_wasm_cbor_json_api!(CIP36KeyRegistration);

impl_wasm_conversions!(cml_cip36::CIP36KeyRegistration, CIP36KeyRegistration);

#[wasm_bindgen]
impl CIP36KeyRegistration {
    pub fn delegation(&self) -> CIP36DelegationDistribution {
        self.0.delegation.clone().into()
    }

    pub fn stake_credential(&self) -> CIP36StakeCredential {
        self.0.stake_credential.clone().into()
    }

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

    pub fn nonce(&self) -> CIP36Nonce {
        self.0.nonce
    }

    pub fn set_voting_purpose(&mut self, voting_purpose: CIP36VotingPurpose) {
        self.0.voting_purpose = voting_purpose
    }

    pub fn voting_purpose(&self) -> CIP36VotingPurpose {
        self.0.voting_purpose
    }
}

pub type LegacyKeyRegistration = cml_crypto_wasm::PublicKey;

pub type CIP36Nonce = u64;

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct CIP36RegistrationCbor(cml_cip36::CIP36RegistrationCbor);

// not implemented since CIP36RegistrationCbor doesn't implement Serialize as it's a subset of metadata
impl_wasm_json_api!(CIP36RegistrationCbor);

impl_wasm_conversions!(cml_cip36::CIP36RegistrationCbor, CIP36RegistrationCbor);

#[wasm_bindgen]
impl CIP36RegistrationCbor {
    pub fn key_registration(&self) -> CIP36KeyRegistration {
        self.0.key_registration.clone().into()
    }

    pub fn registration_witness(&self) -> CIP36RegistrationWitness {
        self.0.registration_witness.clone().into()
    }

    pub fn new(
        key_registration: &CIP36KeyRegistration,
        registration_witness: &CIP36RegistrationWitness,
    ) -> Self {
        Self(cml_cip36::CIP36RegistrationCbor::new(
            key_registration.clone().into(),
            registration_witness.clone().into(),
        ))
    }
}

#[derive(Clone, Debug)]
#[wasm_bindgen]
pub struct CIP36RegistrationWitness(cml_cip36::CIP36RegistrationWitness);

impl_wasm_cbor_json_api!(CIP36RegistrationWitness);

impl_wasm_conversions!(
    cml_cip36::CIP36RegistrationWitness,
    CIP36RegistrationWitness
);

#[wasm_bindgen]
impl CIP36RegistrationWitness {
    pub fn stake_witness(&self) -> CIP36StakeWitness {
        self.0.stake_witness.clone().into()
    }

    pub fn new(stake_witness: &CIP36StakeWitness) -> Self {
        Self(cml_cip36::CIP36RegistrationWitness::new(
            stake_witness.clone().into(),
        ))
    }
}

pub type CIP36StakeCredential = cml_crypto_wasm::PublicKey;

pub type CIP36StakeWitness = cml_crypto_wasm::Ed25519Signature;

pub type CIP36StakingPubKey = cml_crypto_wasm::PublicKey;

pub type CIP36VotingPubKey = cml_crypto_wasm::PublicKey;

pub type CIP36VotingPurpose = u64;

pub type CIP36Weight = u32;