cardano_serialization_lib/protocol_types/certificates/
certificate.rs

1use crate::*;
2
3#[wasm_bindgen]
4#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd)]
5pub enum CertificateKind {
6    StakeRegistration,
7    StakeDeregistration,
8    StakeDelegation,
9    PoolRegistration,
10    PoolRetirement,
11    GenesisKeyDelegation,
12    MoveInstantaneousRewardsCert,
13    CommitteeHotAuth,
14    CommitteeColdResign,
15    DRepDeregistration,
16    DRepRegistration,
17    DRepUpdate,
18    StakeAndVoteDelegation,
19    StakeRegistrationAndDelegation,
20    StakeVoteRegistrationAndDelegation,
21    VoteDelegation,
22    VoteRegistrationAndDelegation,
23}
24
25#[derive(
26    Clone,
27    Debug,
28    Hash,
29    Eq,
30    Ord,
31    PartialEq,
32    PartialOrd,
33    serde::Serialize,
34    serde::Deserialize,
35    JsonSchema,
36)]
37pub enum CertificateEnum {
38    StakeRegistration(StakeRegistration),
39    StakeDeregistration(StakeDeregistration),
40    StakeDelegation(StakeDelegation),
41    PoolRegistration(PoolRegistration),
42    PoolRetirement(PoolRetirement),
43    GenesisKeyDelegation(GenesisKeyDelegation),
44    MoveInstantaneousRewardsCert(MoveInstantaneousRewardsCert),
45    CommitteeHotAuth(CommitteeHotAuth),
46    CommitteeColdResign(CommitteeColdResign),
47    DRepDeregistration(DRepDeregistration),
48    DRepRegistration(DRepRegistration),
49    DRepUpdate(DRepUpdate),
50    StakeAndVoteDelegation(StakeAndVoteDelegation),
51    StakeRegistrationAndDelegation(StakeRegistrationAndDelegation),
52    StakeVoteRegistrationAndDelegation(StakeVoteRegistrationAndDelegation),
53    VoteDelegation(VoteDelegation),
54    VoteRegistrationAndDelegation(VoteRegistrationAndDelegation),
55}
56
57#[wasm_bindgen]
58#[derive(
59    Clone,
60    Debug,
61    Hash,
62    Eq,
63    Ord,
64    PartialEq,
65    PartialOrd,
66    serde::Serialize,
67    serde::Deserialize,
68    JsonSchema,
69)]
70pub struct Certificate(pub(crate) CertificateEnum);
71
72impl_to_from!(Certificate);
73
74#[wasm_bindgen]
75impl Certificate {
76
77    pub fn new_stake_registration(stake_registration: &StakeRegistration) -> Self {
78        Self(CertificateEnum::StakeRegistration(
79            stake_registration.clone(),
80        ))
81    }
82
83    /// Since StakeRegistration can represent stake_registration certificate or reg_cert certificate, because both certificates have the same semantics.
84    /// And in some cases you want to create a reg_cert, this function is used to create a reg_cert.
85    /// The function will return an error if StakeRegistration represents a stake_registration certificate.
86    pub fn new_reg_cert(stake_registration: &StakeRegistration) -> Result<Certificate, JsError> {
87        if stake_registration.coin.is_none() {
88            return Err(JsError::from_str("coin is required"));
89        } else {
90            Ok(Self(CertificateEnum::StakeRegistration(
91                stake_registration.clone(),
92            )))
93        }
94    }
95
96    pub fn new_stake_deregistration(stake_deregistration: &StakeDeregistration) -> Self {
97        Self(CertificateEnum::StakeDeregistration(
98            stake_deregistration.clone(),
99        ))
100    }
101
102    /// Since StakeDeregistration can represent stake_deregistration certificate or unreg_cert certificate, because both certificates have the same semantics.
103    /// And in some cases you want to create an unreg_cert, this function is used to create an unreg_cert.
104    /// The function will return an error if StakeDeregistration represents a stake_deregistration certificate.
105    pub fn new_unreg_cert(stake_deregistration: &StakeDeregistration) -> Result<Certificate, JsError> {
106        if stake_deregistration.coin.is_none() {
107            return Err(JsError::from_str("coin is required"));
108        } else {
109            Ok(Self(CertificateEnum::StakeDeregistration(
110                stake_deregistration.clone(),
111            )))
112        }
113    }
114
115    pub fn new_stake_delegation(stake_delegation: &StakeDelegation) -> Self {
116        Self(CertificateEnum::StakeDelegation(stake_delegation.clone()))
117    }
118
119    pub fn new_pool_registration(pool_registration: &PoolRegistration) -> Self {
120        Self(CertificateEnum::PoolRegistration(pool_registration.clone()))
121    }
122
123    pub fn new_pool_retirement(pool_retirement: &PoolRetirement) -> Self {
124        Self(CertificateEnum::PoolRetirement(pool_retirement.clone()))
125    }
126
127    pub fn new_genesis_key_delegation(genesis_key_delegation: &GenesisKeyDelegation) -> Self {
128        Self(CertificateEnum::GenesisKeyDelegation(
129            genesis_key_delegation.clone(),
130        ))
131    }
132
133    pub fn new_move_instantaneous_rewards_cert(
134        move_instantaneous_rewards_cert: &MoveInstantaneousRewardsCert,
135    ) -> Self {
136        Self(CertificateEnum::MoveInstantaneousRewardsCert(
137            move_instantaneous_rewards_cert.clone(),
138        ))
139    }
140
141    pub fn new_committee_hot_auth(
142        committee_hot_auth: &CommitteeHotAuth,
143    ) -> Self {
144        Self(CertificateEnum::CommitteeHotAuth(
145            committee_hot_auth.clone(),
146        ))
147    }
148
149    pub fn new_committee_cold_resign(
150        committee_cold_resign: &CommitteeColdResign,
151    ) -> Self {
152        Self(CertificateEnum::CommitteeColdResign(
153            committee_cold_resign.clone(),
154        ))
155    }
156
157    pub fn new_drep_deregistration(drep_deregistration: &DRepDeregistration) -> Self {
158        Self(CertificateEnum::DRepDeregistration(
159            drep_deregistration.clone(),
160        ))
161    }
162
163    pub fn new_drep_registration(drep_registration: &DRepRegistration) -> Self {
164        Self(CertificateEnum::DRepRegistration(drep_registration.clone()))
165    }
166
167    pub fn new_drep_update(drep_update: &DRepUpdate) -> Self {
168        Self(CertificateEnum::DRepUpdate(drep_update.clone()))
169    }
170
171    pub fn new_stake_and_vote_delegation(
172        stake_and_vote_delegation: &StakeAndVoteDelegation,
173    ) -> Self {
174        Self(CertificateEnum::StakeAndVoteDelegation(
175            stake_and_vote_delegation.clone(),
176        ))
177    }
178
179    pub fn new_stake_registration_and_delegation(
180        stake_registration_and_delegation: &StakeRegistrationAndDelegation,
181    ) -> Self {
182        Self(CertificateEnum::StakeRegistrationAndDelegation(
183            stake_registration_and_delegation.clone(),
184        ))
185    }
186
187    pub fn new_stake_vote_registration_and_delegation(
188        stake_vote_registration_and_delegation: &StakeVoteRegistrationAndDelegation,
189    ) -> Self {
190        Self(CertificateEnum::StakeVoteRegistrationAndDelegation(
191            stake_vote_registration_and_delegation.clone(),
192        ))
193    }
194
195    pub fn new_vote_delegation(vote_delegation: &VoteDelegation) -> Self {
196        Self(CertificateEnum::VoteDelegation(vote_delegation.clone()))
197    }
198
199    pub fn new_vote_registration_and_delegation(
200        vote_registration_and_delegation: &VoteRegistrationAndDelegation,
201    ) -> Self {
202        Self(CertificateEnum::VoteRegistrationAndDelegation(
203            vote_registration_and_delegation.clone(),
204        ))
205    }
206
207    pub fn kind(&self) -> CertificateKind {
208        match &self.0 {
209            CertificateEnum::StakeRegistration(_) => CertificateKind::StakeRegistration,
210            CertificateEnum::StakeDeregistration(_) => CertificateKind::StakeDeregistration,
211            CertificateEnum::StakeDelegation(_) => CertificateKind::StakeDelegation,
212            CertificateEnum::PoolRegistration(_) => CertificateKind::PoolRegistration,
213            CertificateEnum::PoolRetirement(_) => CertificateKind::PoolRetirement,
214            CertificateEnum::GenesisKeyDelegation(_) => CertificateKind::GenesisKeyDelegation,
215            CertificateEnum::MoveInstantaneousRewardsCert(_) => {
216                CertificateKind::MoveInstantaneousRewardsCert
217            }
218            CertificateEnum::CommitteeHotAuth(_) => {
219                CertificateKind::CommitteeHotAuth
220            }
221            CertificateEnum::CommitteeColdResign(_) => {
222                CertificateKind::CommitteeColdResign
223            }
224            CertificateEnum::DRepDeregistration(_) => CertificateKind::DRepDeregistration,
225            CertificateEnum::DRepRegistration(_) => CertificateKind::DRepRegistration,
226            CertificateEnum::DRepUpdate(_) => CertificateKind::DRepUpdate,
227            CertificateEnum::StakeAndVoteDelegation(_) => CertificateKind::StakeAndVoteDelegation,
228            CertificateEnum::StakeRegistrationAndDelegation(_) => {
229                CertificateKind::StakeRegistrationAndDelegation
230            }
231            CertificateEnum::StakeVoteRegistrationAndDelegation(_) => {
232                CertificateKind::StakeVoteRegistrationAndDelegation
233            }
234            CertificateEnum::VoteDelegation(_) => CertificateKind::VoteDelegation,
235            CertificateEnum::VoteRegistrationAndDelegation(_) => {
236                CertificateKind::VoteRegistrationAndDelegation
237            }
238        }
239    }
240
241    pub fn as_stake_registration(&self) -> Option<StakeRegistration> {
242        match &self.0 {
243            CertificateEnum::StakeRegistration(x) => Some(x.clone()),
244            _ => None,
245        }
246    }
247
248    /// Since StakeRegistration can represent stake_registration certificate or reg_cert certificate, because both certificates have the same semantics.
249    /// And in some cases you want to get a reg_cert, this function is used to get a reg_cert.
250    /// The function will return None if StakeRegistration represents a stake_registration certificate or Certificate is not a StakeRegistration.
251    pub fn as_reg_cert(&self) -> Option<StakeRegistration> {
252        match &self.0 {
253            CertificateEnum::StakeRegistration(x) => {
254                return if x.coin.is_some() {
255                    Some(x.clone())
256                } else {
257                    None
258                }
259            }
260            _ => None,
261        }
262    }
263
264    pub fn as_stake_deregistration(&self) -> Option<StakeDeregistration> {
265        match &self.0 {
266            CertificateEnum::StakeDeregistration(x) => Some(x.clone()),
267            _ => None,
268        }
269    }
270
271    /// Since StakeDeregistration can represent stake_deregistration certificate or unreg_cert certificate, because both certificates have the same semantics.
272    /// And in some cases you want to get an unreg_cert, this function is used to get an unreg_cert.
273    /// The function will return None if StakeDeregistration represents a stake_deregistration certificate or Certificate is not a StakeDeregistration.
274    pub fn as_unreg_cert(&self) -> Option<StakeDeregistration> {
275        match &self.0 {
276            CertificateEnum::StakeDeregistration(x) => {
277                return if x.coin.is_some() {
278                    Some(x.clone())
279                } else {
280                    None
281                }
282            }
283            _ => None,
284        }
285    }
286
287    pub fn as_stake_delegation(&self) -> Option<StakeDelegation> {
288        match &self.0 {
289            CertificateEnum::StakeDelegation(x) => Some(x.clone()),
290            _ => None,
291        }
292    }
293
294    pub fn as_pool_registration(&self) -> Option<PoolRegistration> {
295        match &self.0 {
296            CertificateEnum::PoolRegistration(x) => Some(x.clone()),
297            _ => None,
298        }
299    }
300
301    pub fn as_pool_retirement(&self) -> Option<PoolRetirement> {
302        match &self.0 {
303            CertificateEnum::PoolRetirement(x) => Some(x.clone()),
304            _ => None,
305        }
306    }
307
308    pub fn as_genesis_key_delegation(&self) -> Option<GenesisKeyDelegation> {
309        match &self.0 {
310            CertificateEnum::GenesisKeyDelegation(x) => Some(x.clone()),
311            _ => None,
312        }
313    }
314
315    pub fn as_move_instantaneous_rewards_cert(&self) -> Option<MoveInstantaneousRewardsCert> {
316        match &self.0 {
317            CertificateEnum::MoveInstantaneousRewardsCert(x) => Some(x.clone()),
318            _ => None,
319        }
320    }
321
322    pub fn as_committee_hot_auth(&self) -> Option<CommitteeHotAuth> {
323        match &self.0 {
324            CertificateEnum::CommitteeHotAuth(x) => Some(x.clone()),
325            _ => None,
326        }
327    }
328
329    pub fn as_committee_cold_resign(&self) -> Option<CommitteeColdResign> {
330        match &self.0 {
331            CertificateEnum::CommitteeColdResign(x) => Some(x.clone()),
332            _ => None,
333        }
334    }
335
336    pub fn as_drep_deregistration(&self) -> Option<DRepDeregistration> {
337        match &self.0 {
338            CertificateEnum::DRepDeregistration(x) => Some(x.clone()),
339            _ => None,
340        }
341    }
342
343    pub fn as_drep_registration(&self) -> Option<DRepRegistration> {
344        match &self.0 {
345            CertificateEnum::DRepRegistration(x) => Some(x.clone()),
346            _ => None,
347        }
348    }
349
350    pub fn as_drep_update(&self) -> Option<DRepUpdate> {
351        match &self.0 {
352            CertificateEnum::DRepUpdate(x) => Some(x.clone()),
353            _ => None,
354        }
355    }
356
357    pub fn as_stake_and_vote_delegation(&self) -> Option<StakeAndVoteDelegation> {
358        match &self.0 {
359            CertificateEnum::StakeAndVoteDelegation(x) => Some(x.clone()),
360            _ => None,
361        }
362    }
363
364    pub fn as_stake_registration_and_delegation(&self) -> Option<StakeRegistrationAndDelegation> {
365        match &self.0 {
366            CertificateEnum::StakeRegistrationAndDelegation(x) => Some(x.clone()),
367            _ => None,
368        }
369    }
370
371    pub fn as_stake_vote_registration_and_delegation(
372        &self,
373    ) -> Option<StakeVoteRegistrationAndDelegation> {
374        match &self.0 {
375            CertificateEnum::StakeVoteRegistrationAndDelegation(x) => Some(x.clone()),
376            _ => None,
377        }
378    }
379
380    pub fn as_vote_delegation(&self) -> Option<VoteDelegation> {
381        match &self.0 {
382            CertificateEnum::VoteDelegation(x) => Some(x.clone()),
383            _ => None,
384        }
385    }
386
387    pub fn as_vote_registration_and_delegation(&self) -> Option<VoteRegistrationAndDelegation> {
388        match &self.0 {
389            CertificateEnum::VoteRegistrationAndDelegation(x) => Some(x.clone()),
390            _ => None,
391        }
392    }
393
394    pub fn has_required_script_witness(&self) -> bool {
395        match &self.0 {
396            CertificateEnum::StakeRegistration(x) => {
397                if x.coin.is_some() {
398                    return x.has_script_credentials();
399                } else {
400                    return false;
401                }
402            }
403            CertificateEnum::StakeDeregistration(x) => x.has_script_credentials(),
404            CertificateEnum::StakeDelegation(x) => x.has_script_credentials(),
405            CertificateEnum::VoteDelegation(x) => x.has_script_credentials(),
406            CertificateEnum::StakeAndVoteDelegation(x) => x.has_script_credentials(),
407            CertificateEnum::StakeRegistrationAndDelegation(x) => x.has_script_credentials(),
408            CertificateEnum::StakeVoteRegistrationAndDelegation(x) => x.has_script_credentials(),
409            CertificateEnum::VoteRegistrationAndDelegation(x) => x.has_script_credentials(),
410            CertificateEnum::CommitteeHotAuth(x) => x.has_script_credentials(),
411            CertificateEnum::CommitteeColdResign(x) => x.has_script_credentials(),
412            CertificateEnum::DRepRegistration(x) => x.has_script_credentials(),
413            CertificateEnum::DRepDeregistration(x) => x.has_script_credentials(),
414            CertificateEnum::DRepUpdate(x) => x.has_script_credentials(),
415            _ => false,
416        }
417    }
418}