whisky/builder/
certificate.rs

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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
use sidan_csl_rs::model::*;

use super::{TxBuilder, WRedeemer};

impl TxBuilder {
    /// ## Transaction building method
    ///
    /// Add a pool registration certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `pool_params` - Parameters of pool to be registered
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn register_pool_certificate(&mut self, pool_params: &PoolParams) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::RegisterPool(RegisterPool {
                    pool_params: pool_params.clone(),
                }),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add a stake registration certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `stake_key_address` - Address of the stake key
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn register_stake_certificate(&mut self, stake_key_address: &str) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::RegisterStake(RegisterStake {
                    stake_key_address: stake_key_address.to_string(),
                    coin: self.protocol_params.clone().unwrap_or_default().key_deposit,
                }),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add a stake delegation certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `stake_key_address` - Address of the stake key
    /// * `pool_id` - id of the pool that will be delegated to
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn delegate_stake_certificate(
        &mut self,
        stake_key_address: &str,
        pool_id: &str,
    ) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::DelegateStake(DelegateStake {
                    stake_key_address: stake_key_address.to_string(),
                    pool_id: pool_id.to_string(),
                }),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add a stake deregistration certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `stake_key_address` - Address of the stake key
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn deregister_stake_certificate(&mut self, stake_key_address: &str) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::DeregisterStake(DeregisterStake {
                    stake_key_address: stake_key_address.to_string(),
                }),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add a pool retire certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `pool_id` - id of the pool that will be retired
    /// * `epoch` - The epoch that the pool will be retired from
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn retire_pool_certificate(&mut self, pool_id: &str, epoch: u32) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(CertificateType::RetirePool(
                RetirePool {
                    pool_id: pool_id.to_string(),
                    epoch,
                },
            )));
        self
    }

    /// ## Transaction building method
    ///
    /// Add a vote delegation certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `stake_key_address` - Address of the stake key
    /// * `drep` - The drep that will be voted for, or always abstain / always no confidence
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn vote_delegation_certificate(
        &mut self,
        stake_key_address: &str,
        drep: &DRep,
    ) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::VoteDelegation(VoteDelegation {
                    stake_key_address: stake_key_address.to_string(),
                    drep: drep.clone(),
                }),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add a stake and vote delegation certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `stake_key_address` - Address of the stake key
    /// * `pool_key_hash` - Hash of pool key that will be delegated to, same as pool id
    /// * `drep` - The drep that will be voted for, or always abstain / always no confidence
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn stake_and_vote_delegation_certificate(
        &mut self,
        stake_key_address: &str,
        pool_key_hash: &str,
        drep: &DRep,
    ) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::StakeAndVoteDelegation(StakeAndVoteDelegation {
                    stake_key_address: stake_key_address.to_string(),
                    pool_key_hash: pool_key_hash.to_string(),
                    drep: drep.clone(),
                }),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add a stake registration and delegation certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `stake_key_address` - Address of the stake key
    /// * `pool_key_hash` - Hash of pool key that will be delegated to, same as pool id
    /// * `coin` - Deposit for certificate registration
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn stake_registration_and_delegation(
        &mut self,
        stake_key_address: &str,
        pool_key_hash: &str,
        coin: u64,
    ) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::StakeRegistrationAndDelegation(StakeRegistrationAndDelegation {
                    stake_key_address: stake_key_address.to_string(),
                    pool_key_hash: pool_key_hash.to_string(),
                    coin,
                }),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add a vote registration and delegation certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `stake_key_address` - Address of the stake key
    /// * `drep` - The drep that will be voted for, or always abstain / always no confidence
    /// * `coin` - Deposit for certificate registration
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn vote_registration_and_delegation(
        &mut self,
        stake_key_address: &str,
        drep: &DRep,
        coin: u64,
    ) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::VoteRegistrationAndDelegation(VoteRegistrationAndDelegation {
                    stake_key_address: stake_key_address.to_string(),
                    drep: drep.clone(),
                    coin,
                }),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add a stake vote registration and delegation certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `stake_key_address` - Address of the stake key
    /// * `pool_key_hash` - Hash of pool key that will be delegated to, same as pool id
    /// * `drep` - The drep that will be voted for, or always abstain / always no confidence
    /// * `coin` - Deposit for certificate registration
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn stake_vote_registration_and_delegation(
        &mut self,
        stake_key_address: &str,
        pool_key_hash: &str,
        drep: &DRep,
        coin: u64,
    ) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::StakeVoteRegistrationAndDelegation(
                    StakeVoteRegistrationAndDelegation {
                        stake_key_address: stake_key_address.to_string(),
                        pool_key_hash: pool_key_hash.to_string(),
                        drep: drep.clone(),
                        coin,
                    },
                ),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add commitee hot auth certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `committee_cold_key_address` - Address of the committee cold key
    /// * `committee_hot_key_address` - Address of the commitee hot key
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn committee_hot_auth(
        &mut self,
        committee_cold_key_address: &str,
        committee_hot_key_address: &str,
    ) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::CommitteeHotAuth(CommitteeHotAuth {
                    committee_cold_key_address: committee_cold_key_address.to_string(),
                    committee_hot_key_address: committee_hot_key_address.to_string(),
                }),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add commitee cold resign certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `committee_cold_key_address` - Address of the committee cold key
    /// * `anchor` - The Anchor, this is a URL and a hash of the doc at this URL
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn commitee_cold_resign(
        &mut self,
        committee_cold_key_address: &str,
        anchor: Option<Anchor>,
    ) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::CommitteeColdResign(CommitteeColdResign {
                    committee_cold_key_address: committee_cold_key_address.to_string(),
                    anchor,
                }),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add DRep registration certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `voting_key_address` - Address of the voting key
    /// * `coin` - Deposit for certificate registration
    /// * `anchor` - The Anchor, this is a URL and a hash of the doc at this URL
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn drep_registration(
        &mut self,
        drep_id: &str,
        coin: u64,
        anchor: Option<Anchor>,
    ) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::DRepRegistration(DRepRegistration {
                    drep_id: drep_id.to_string(),
                    coin,
                    anchor,
                }),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add DRep deregistration certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `voting_key_address` - Address of the voting key
    /// * `coin` - Deposit for certificate registration
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn drep_deregistration(&mut self, drep_id: &str, coin: u64) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(
                CertificateType::DRepDeregistration(DRepDeregistration {
                    drep_id: drep_id.to_string(),
                    coin,
                }),
            ));
        self
    }

    /// ## Transaction building method
    ///
    /// Add DRep update certificate to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `voting_key_address` - Address of the voting key
    /// * `anchor` - The Anchor, this is a URL and a hash of the doc at this URL
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn drep_update(&mut self, drep_id: &str, anchor: Option<Anchor>) -> &mut Self {
        self.core
            .tx_builder_body
            .certificates
            .push(Certificate::BasicCertificate(CertificateType::DRepUpdate(
                DRepUpdate {
                    drep_id: drep_id.to_string(),
                    anchor,
                },
            )));
        self
    }

    /// ## Transaction building method
    ///
    /// Add script witness to certificate
    ///
    /// ### Arguments
    ///
    /// * `script_cbor` - The script in CBOR format
    /// * `version` - The language version, if the language version is None, the script is assumed to be a Native Script
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn certificate_script(
        &mut self,
        script_cbor: &str,
        version: Option<LanguageVersion>,
    ) -> &mut Self {
        let last_cert = self.core.tx_builder_body.certificates.pop();
        if last_cert.is_none() {
            panic!("Undefined certificate");
        }
        let last_cert = last_cert.unwrap();
        match last_cert {
            Certificate::BasicCertificate(basic_cert) => match version {
                Some(lang_ver) => self.core.tx_builder_body.certificates.push(
                    Certificate::ScriptCertificate(ScriptCertificate {
                        cert: basic_cert,
                        redeemer: None,
                        script_source: Some(ScriptSource::ProvidedScriptSource(
                            ProvidedScriptSource {
                                script_cbor: script_cbor.to_string(),
                                language_version: lang_ver,
                            },
                        )),
                    }),
                ),
                None => self.core.tx_builder_body.certificates.push(
                    Certificate::SimpleScriptCertificate(SimpleScriptCertificate {
                        cert: basic_cert,
                        simple_script_source: Some(SimpleScriptSource::ProvidedSimpleScriptSource(
                            ProvidedSimpleScriptSource {
                                script_cbor: script_cbor.to_string(),
                            },
                        )),
                    }),
                ),
            },
            Certificate::ScriptCertificate(script_cert) => match version {
                Some(lang_ver) => self.core.tx_builder_body.certificates.push(
                    Certificate::ScriptCertificate(ScriptCertificate {
                        cert: script_cert.cert,
                        redeemer: script_cert.redeemer,
                        script_source: Some(ScriptSource::ProvidedScriptSource(
                            ProvidedScriptSource {
                                script_cbor: script_cbor.to_string(),
                                language_version: lang_ver,
                            },
                        )),
                    }),
                ),
                None => panic!("Language version has to be defined for plutus certificates"),
            },
            Certificate::SimpleScriptCertificate(_) => {
                panic!("Native script cert had its script defined twice")
            }
        }

        self
    }

    /// ## Transaction building method
    ///
    /// Add a Certificate transaction input reference to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `tx_hash` - The transaction hash
    /// * `tx_index` - The transaction index
    /// * `script_hash` - The script hash
    /// * `version` - The language version, if the language version is None, the script is assumed to be a Native Script
    /// * `script_size` - Size of the script
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn certificate_tx_in_reference(
        &mut self,
        tx_hash: &str,
        tx_index: u32,
        script_hash: &str,
        version: Option<LanguageVersion>,
        script_size: usize,
    ) -> &mut Self {
        let last_cert = self.core.tx_builder_body.certificates.pop();
        if last_cert.is_none() {
            panic!("Undefined certificate");
        }
        let last_cert = last_cert.unwrap();
        match last_cert {
            Certificate::BasicCertificate(basic_cert) => match version {
                Some(lang_ver) => self.core.tx_builder_body.certificates.push(
                    Certificate::ScriptCertificate(ScriptCertificate {
                        cert: basic_cert,
                        redeemer: None,
                        script_source: Some(ScriptSource::InlineScriptSource(InlineScriptSource {
                            ref_tx_in: RefTxIn {
                                tx_hash: tx_hash.to_string(),
                                tx_index,
                                // Script size is already accounted for in script source
                                script_size: None,
                            },
                            script_hash: script_hash.to_string(),
                            language_version: lang_ver,
                            script_size,
                        })),
                    }),
                ),
                None => self.core.tx_builder_body.certificates.push(
                    Certificate::SimpleScriptCertificate(SimpleScriptCertificate {
                        cert: basic_cert,
                        simple_script_source: Some(SimpleScriptSource::InlineSimpleScriptSource(
                            InlineSimpleScriptSource {
                                ref_tx_in: RefTxIn {
                                    tx_hash: tx_hash.to_string(),
                                    tx_index,
                                    // Script size is already accounted for in script source
                                    script_size: None,
                                },
                                simple_script_hash: script_hash.to_string(),
                                script_size,
                            },
                        )),
                    }),
                ),
            },
            Certificate::ScriptCertificate(script_cert) => match version {
                Some(lang_ver) => self.core.tx_builder_body.certificates.push(
                    Certificate::ScriptCertificate(ScriptCertificate {
                        cert: script_cert.cert,
                        redeemer: script_cert.redeemer,
                        script_source: Some(ScriptSource::InlineScriptSource(InlineScriptSource {
                            ref_tx_in: RefTxIn {
                                tx_hash: tx_hash.to_string(),
                                tx_index,
                                // Script size is already accounted for in script source
                                script_size: None,
                            },
                            script_hash: script_hash.to_string(),
                            language_version: lang_ver,
                            script_size,
                        })),
                    }),
                ),
                None => panic!("Language version has to be defined for plutus certificates"),
            },
            Certificate::SimpleScriptCertificate(_) => {
                panic!("Native script cert had its script defined twice")
            }
        }

        self
    }

    /// ## Transaction building method
    ///
    /// Add a Certificate Redeemer to the TxBuilder instance
    ///
    /// ### Arguments
    ///
    /// * `redeemer` - The redeemer value
    ///
    /// ### Returns
    ///
    /// * `Self` - The TxBuilder instance
    pub fn certificate_redeemer_value(&mut self, redeemer: &WRedeemer) -> &mut Self {
        let last_cert = self.core.tx_builder_body.certificates.pop();
        if last_cert.is_none() {
            panic!("Undefined certificate");
        }
        let last_cert = last_cert.unwrap();
        let current_redeemer = match redeemer.data.to_cbor() {
            Ok(raw_redeemer) => Some(Redeemer {
                data: raw_redeemer,
                ex_units: redeemer.clone().ex_units,
            }),
            Err(_) => {
                panic!("Error converting certificate redeemer to CBOR")
            }
        };
        match last_cert {
            Certificate::BasicCertificate(basic_cert) => self
                .core
                .tx_builder_body
                .certificates
                .push(Certificate::ScriptCertificate(ScriptCertificate {
                    cert: basic_cert,
                    redeemer: current_redeemer,
                    script_source: None,
                })),

            Certificate::ScriptCertificate(script_cert) => self
                .core
                .tx_builder_body
                .certificates
                .push(Certificate::ScriptCertificate(ScriptCertificate {
                    cert: script_cert.cert,
                    redeemer: current_redeemer,
                    script_source: script_cert.script_source,
                })),

            Certificate::SimpleScriptCertificate(_) => {
                panic!("Native script cert cannot use redeemers")
            }
        }

        self
    }
}