sidan_csl_rs/core/utils/
address.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
use serde_json::{from_str, Value};

use crate::{csl, model::*, *};

#[wasm_bindgen]
pub fn wasm_script_to_address(
    network_id: u8,
    script_hash: String,
    stake_hash: Option<String>,
    is_script_stake_key: bool,
) -> String {
    match stake_hash {
        Some(stake) => script_to_address(
            network_id,
            &script_hash,
            Some((&stake, is_script_stake_key)),
        ),
        None => script_to_address(network_id, &script_hash, None),
    }
}

pub fn script_to_address(
    network_id: u8,
    script_hash: &str,
    stake_hash: Option<(&str, bool)>,
) -> String {
    match stake_hash {
        Some((stake, is_script)) => {
            let stake_cred = if is_script {
                csl::Credential::from_scripthash(&csl::ScriptHash::from_hex(stake).unwrap())
            } else {
                csl::Credential::from_keyhash(&csl::Ed25519KeyHash::from_hex(stake).unwrap())
            };

            csl::BaseAddress::new(
                network_id,
                &csl::Credential::from_scripthash(&csl::ScriptHash::from_hex(script_hash).unwrap()),
                &stake_cred,
            )
            .to_address()
            .to_bech32(None)
            .unwrap()
        }

        None => csl::EnterpriseAddress::new(
            network_id,
            &csl::Credential::from_scripthash(&csl::ScriptHash::from_hex(script_hash).unwrap()),
        )
        .to_address()
        .to_bech32(None)
        .unwrap(),
    }
}

#[wasm_bindgen]
pub fn deserialize_bech32_address(bech32_addr: &str) -> DeserializedAddress {
    let csl_address =
        csl::BaseAddress::from_address(&csl::Address::from_bech32(bech32_addr).unwrap());
    match csl_address {
        Some(address) => {
            let csl_key_hash = address
                .payment_cred()
                .to_keyhash()
                .map(|key_hash| key_hash.to_hex());

            let csl_script_hash = address
                .payment_cred()
                .to_scripthash()
                .map(|script_hash| script_hash.to_hex());

            let csl_stake_key_hash = address
                .stake_cred()
                .to_keyhash()
                .map(|stake_key_hash| stake_key_hash.to_hex());

            let csl_stake_key_script_hash = address
                .stake_cred()
                .to_scripthash()
                .map(|stake_key_script_hash| stake_key_script_hash.to_hex());

            DeserializedAddress::new(
                &csl_key_hash.unwrap_or("".to_string()),
                &csl_script_hash.unwrap_or("".to_string()),
                &csl_stake_key_hash.unwrap_or("".to_string()),
                &csl_stake_key_script_hash.unwrap_or("".to_string()),
            )
        }
        None => {
            let csl_enterprize_address = csl::EnterpriseAddress::from_address(
                &csl::Address::from_bech32(bech32_addr).unwrap(),
            )
            .unwrap();

            let csl_key_hash = csl_enterprize_address
                .payment_cred()
                .to_keyhash()
                .map(|key_hash| key_hash.to_hex());

            let csl_script_hash = csl_enterprize_address
                .payment_cred()
                .to_scripthash()
                .map(|script_hash| script_hash.to_hex());

            DeserializedAddress::new(
                &csl_key_hash.unwrap_or("".to_string()),
                &csl_script_hash.unwrap_or("".to_string()),
                "",
                "",
            )
        }
    }
}

#[wasm_bindgen]
pub fn address_bech32_to_obj(bech32: &str) -> String {
    let csl_address = csl::Address::from_bech32(bech32).unwrap();
    csl::PlutusData::from_address(&csl_address)
        .unwrap()
        .to_json(csl::PlutusDatumSchema::DetailedSchema)
        .unwrap()
}

#[wasm_bindgen]
pub fn parse_plutus_address_obj_to_bech32(plutus_data_address_obj: &str, network_id: u8) -> String {
    let plutus_data_address: Value =
        from_str(plutus_data_address_obj).expect("Invalid json string");
    let plutus_data_key_obj = plutus_data_address.get("fields").unwrap();
    let plutus_data_key_list = plutus_data_key_obj.as_array().unwrap();

    let plutus_data_payment_key_obj = &plutus_data_key_list[0];
    let plutus_data_stake_key_obj = &plutus_data_key_list[1];

    let payment_key_hash = plutus_data_payment_key_obj["fields"][0]["bytes"]
        .as_str()
        .unwrap();

    let csl_payment_credential =
        if plutus_data_payment_key_obj["constructor"].as_u64().unwrap() == 0 {
            csl::Credential::from_keyhash(&csl::Ed25519KeyHash::from_hex(payment_key_hash).unwrap())
        } else {
            csl::Credential::from_scripthash(&csl::ScriptHash::from_hex(payment_key_hash).unwrap())
        };

    if plutus_data_stake_key_obj["constructor"].as_u64().unwrap() == 0 {
        let stake_key_hash = plutus_data_stake_key_obj["fields"][0]["fields"][0]["fields"][0]
            ["bytes"]
            .as_str()
            .unwrap();
        if plutus_data_stake_key_obj["fields"][0]["fields"][0]["constructor"]
            .as_u64()
            .unwrap()
            == 0
        {
            csl::BaseAddress::new(
                network_id,
                &csl_payment_credential,
                &csl::Credential::from_keyhash(
                    &csl::Ed25519KeyHash::from_hex(stake_key_hash).unwrap(),
                ),
            )
            .to_address()
            .to_bech32(None)
            .unwrap()
        } else {
            csl::BaseAddress::new(
                network_id,
                &csl_payment_credential,
                &csl::Credential::from_scripthash(
                    &csl::ScriptHash::from_hex(stake_key_hash).unwrap(),
                ),
            )
            .to_address()
            .to_bech32(None)
            .unwrap()
        }
    } else {
        csl::EnterpriseAddress::new(network_id, &csl_payment_credential)
            .to_address()
            .to_bech32(None)
            .unwrap()
    }
}

#[wasm_bindgen]
pub fn parse_plutus_address_to_bech32(plutus_hex: &str, network_id: u8) -> String {
    let csl_plutus_data_address = csl::PlutusData::from_hex(plutus_hex).unwrap();
    parse_plutus_address_obj_to_bech32(
        &csl_plutus_data_address
            .to_json(csl::PlutusDatumSchema::DetailedSchema)
            .unwrap(),
        network_id,
    )
}

#[wasm_bindgen]
pub fn parse_native_script_address_to_bech32(native_script: &str, network_id: u8) -> String {
    let csl_native_script = csl::NativeScript::from_hex(native_script).unwrap();
    let csl_payment_credential = csl::Credential::from_scripthash(&csl_native_script.hash());
    csl::EnterpriseAddress::new(network_id, &csl_payment_credential)
        .to_address()
        .to_bech32(None)
        .unwrap()
}

#[wasm_bindgen]
pub fn parse_native_script_address_with_stake_key_to_bech32(
    native_script: &str,
    stake_key_hash: &str,
    network_id: u8,
) -> String {
    let csl_native_script = csl::NativeScript::from_hex(native_script).unwrap();
    let csl_payment_credential = csl::Credential::from_scripthash(&csl_native_script.hash());
    let csl_stake_credential =
        csl::Credential::from_keyhash(&csl::Ed25519KeyHash::from_hex(stake_key_hash).unwrap());
    csl::BaseAddress::new(network_id, &csl_payment_credential, &csl_stake_credential)
        .to_address()
        .to_bech32(None)
        .unwrap()
}