whisky/utils/
blueprint.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
use sidan_csl_rs::{
    core::utils::{
        apply_params_to_script, get_script_hash, script_hash_to_stake_address, script_to_address,
    },
    csl::JsError,
    model::{BuilderDataType, LanguageVersion},
};

#[derive(Debug, Clone)]
pub struct MintingBlueprint {
    pub version: LanguageVersion,
    pub cbor: String,
    pub hash: String,
}

impl MintingBlueprint {
    pub fn new(version: LanguageVersion) -> Self {
        Self {
            version,
            cbor: "".to_string(),
            hash: "".to_string(),
        }
    }

    pub fn param_script(
        &mut self,
        compiled_code: &str,
        params: &[&str],
        params_type: BuilderDataType,
    ) -> Result<&mut Self, JsError> {
        let cbor = apply_params_to_script(compiled_code, params, params_type)?;
        let hash = get_script_hash(&cbor, self.version.clone())?;
        self.hash = hash;
        self.cbor = cbor;
        Ok(self)
    }

    pub fn no_param_script(&mut self, compiled_code: &str) -> Result<&mut Self, JsError> {
        let cbor = apply_params_to_script(compiled_code, &[], BuilderDataType::CBOR)?;
        let hash = get_script_hash(&cbor, self.version.clone())?;
        self.hash = hash;
        self.cbor = cbor;
        Ok(self)
    }
}

#[derive(Debug, Clone)]
pub struct WithdrawalBlueprint {
    pub version: LanguageVersion,
    pub network_id: u8,
    pub cbor: String,
    pub hash: String,
    pub address: String,
}

impl WithdrawalBlueprint {
    pub fn new(version: LanguageVersion, network_id: u8) -> Self {
        Self {
            version,
            network_id,
            cbor: "".to_string(),
            hash: "".to_string(),
            address: "".to_string(),
        }
    }

    pub fn param_script(
        &mut self,
        compiled_code: &str,
        params: &[&str],
        params_type: BuilderDataType,
    ) -> Result<&mut Self, JsError> {
        let cbor = apply_params_to_script(compiled_code, params, params_type).unwrap();
        let hash = get_script_hash(&cbor, self.version.clone()).unwrap();
        self.address = script_hash_to_stake_address(&hash, self.network_id)?;
        self.hash = hash;
        self.cbor = cbor;
        Ok(self)
    }

    pub fn no_param_script(&mut self, compiled_code: &str) -> Result<&mut Self, JsError> {
        let cbor = apply_params_to_script(compiled_code, &[], BuilderDataType::CBOR)?;
        let hash = get_script_hash(&cbor, self.version.clone())?;
        self.address = script_hash_to_stake_address(&hash, self.network_id)?;
        self.hash = hash;
        self.cbor = cbor;
        Ok(self)
    }
}

#[derive(Debug, Clone)]
pub struct SpendingBlueprint {
    pub version: LanguageVersion,
    pub network_id: u8,
    pub stake_hash: Option<(String, bool)>,
    pub cbor: String,
    pub hash: String,
    pub address: String,
}

impl SpendingBlueprint {
    pub fn new(
        version: LanguageVersion,
        network_id: u8,
        stake_hash: Option<(String, bool)>,
    ) -> Self {
        Self {
            version,
            network_id,
            stake_hash,
            cbor: "".to_string(),
            hash: "".to_string(),
            address: "".to_string(),
        }
    }

    pub fn param_script(
        &mut self,
        compiled_code: &str,
        params: &[&str],
        params_type: BuilderDataType,
    ) -> Result<&mut Self, JsError> {
        let cbor = apply_params_to_script(compiled_code, params, params_type)?;
        let hash = get_script_hash(&cbor, self.version.clone())?;
        let stake_hash: Option<(&str, bool)> = self
            .stake_hash
            .as_ref()
            .map(|(hash, is_script)| (hash.as_str(), *is_script));

        let address = script_to_address(self.network_id, &hash, stake_hash);
        self.hash = hash;
        self.cbor = cbor;
        self.address = address;
        Ok(self)
    }

    pub fn no_param_script(&mut self, compiled_code: &str) -> Result<&mut Self, JsError> {
        let cbor = apply_params_to_script(compiled_code, &[], BuilderDataType::CBOR)?;
        let hash = get_script_hash(&cbor, self.version.clone())?;
        let stake_hash: Option<(&str, bool)> = self
            .stake_hash
            .as_ref()
            .map(|(hash, is_script)| (hash.as_str(), *is_script));
        let address = script_to_address(self.network_id, &hash, stake_hash);
        self.hash = hash;
        self.cbor = cbor;
        self.address = address;
        Ok(self)
    }
}