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
#![allow(dead_code)]

use crate::jcli::JCli;
use assert_fs::{prelude::*, TempDir};
use jormungandr_lib::interfaces::TaxType;
use jortestkit::file;
use std::path::{Path, PathBuf};

pub struct CertificateBuilder {
    jcli: JCli,
}

impl CertificateBuilder {
    pub fn new(jcli: JCli) -> Self {
        Self { jcli }
    }

    pub fn new_signed_stake_pool_cert(self) -> SignedStakePoolCertBuilder {
        SignedStakePoolCertBuilder::new(self.jcli)
    }

    pub fn new_signed_stake_pool_delegation(
        self,
        stake_pool_id: &str,
        stake_key_pub: &str,
        stake_key_file: &Path,
    ) -> Result<String, std::io::Error> {
        let temp_dir = TempDir::new().unwrap();

        let stake_delegation_cert = self
            .jcli
            .certificate()
            .new_stake_delegation(stake_pool_id, stake_key_pub);

        let stake_delegation_cert_file = temp_dir.child("stake_delegation.cert");
        stake_delegation_cert_file
            .write_str(&stake_delegation_cert)
            .unwrap();
        let stake_delegation_signcert_file = temp_dir.child("stake_delegation.signcert");

        self.jcli.certificate().sign(
            stake_key_file,
            stake_delegation_cert_file.path(),
            stake_delegation_signcert_file.path(),
        );
        file::read_file(stake_delegation_signcert_file.path())
    }

    pub fn new_signed_vote_plan<P: AsRef<Path>, Q: AsRef<Path>>(
        self,
        proposal_file: P,
        stake_key_file: Q,
    ) -> PathBuf {
        let temp_dir = TempDir::new().unwrap().into_persistent();
        let cert = self.jcli.certificate().new_vote_plan(proposal_file);

        let cert_file = temp_dir.child("vote_plan.cert");
        cert_file.write_str(&cert).unwrap();

        let signcert_file = temp_dir.child("vote_plan.signcert");
        self.jcli
            .certificate()
            .sign(&stake_key_file, cert_file.path(), signcert_file.path());
        PathBuf::from(signcert_file.path())
    }
}

pub struct SignedStakePoolCertBuilder {
    jcli: JCli,
    pool_kes_pk: String,
    pool_vrf_pk: String,
    stake_key_file: PathBuf,
    start_validity: u32,
    management_threshold: u32,
    owner_pk: String,
    tax_type: Option<TaxType>,
}

impl SignedStakePoolCertBuilder {
    pub fn new(jcli: JCli) -> Self {
        Self {
            jcli,
            pool_kes_pk: "".to_owned(),
            pool_vrf_pk: "".to_string(),
            stake_key_file: PathBuf::new(),
            start_validity: 0u32,
            management_threshold: 0u32,
            owner_pk: "".to_string(),
            tax_type: None,
        }
    }

    pub fn pool_kes_pk<S: Into<String>>(&mut self, pool_kes_pk: S) -> &mut Self {
        self.pool_kes_pk = pool_kes_pk.into();
        self
    }

    pub fn pool_vrf_pk<S: Into<String>>(&mut self, pool_vrf_pk: S) -> &mut Self {
        self.pool_vrf_pk = pool_vrf_pk.into();
        self
    }

    pub fn owner_pk<S: Into<String>>(&mut self, owner_pk: S) -> &mut Self {
        self.owner_pk = owner_pk.into();
        self
    }

    pub fn stake_key_file<P: AsRef<Path>>(&mut self, stake_key_file: P) -> &mut Self {
        self.stake_key_file = stake_key_file.as_ref().to_path_buf();
        self
    }

    pub fn start_validity(&mut self, start_validity: u32) -> &mut Self {
        self.start_validity = start_validity;
        self
    }

    pub fn management_threshold(&mut self, management_threshold: u32) -> &mut Self {
        self.management_threshold = management_threshold;
        self
    }

    pub fn tax_type(&mut self, tax_type: TaxType) -> &mut Self {
        self.tax_type = Some(tax_type);
        self
    }

    pub fn build(self) {
        let temp_dir = TempDir::new().unwrap();

        let stake_pool_cert = self.jcli.certificate().new_stake_pool_registration(
            &self.pool_kes_pk,
            &self.pool_vrf_pk,
            self.start_validity,
            self.management_threshold,
            &self.owner_pk,
            self.tax_type,
        );
        let stake_pool_cert_file = temp_dir.child("stake_pool.cert");
        stake_pool_cert_file.write_str(&stake_pool_cert).unwrap();

        let stake_pool_signcert_file = temp_dir.child("stake_pool.signcert");
        self.jcli.certificate().sign(
            &self.stake_key_file,
            stake_pool_cert_file.path(),
            stake_pool_signcert_file.path(),
        );
    }
}