modality_utils/
keypair.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
use anyhow::{anyhow, Result};
use base58::ToBase58;
use libp2p::identity::{ed25519, Keypair as Libp2pKeypair, PublicKey as Libp2pPublicKey};
use libp2p_identity::PeerId;
use regex::Regex;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::fs;

use crate::encrypted_text::EncryptedText;
use crate::json_stringify_deterministic::stringify_deterministic;

#[derive(Clone)]
pub enum KeypairOrPublicKey {
    Keypair(Libp2pKeypair),
    PublicKey(Libp2pPublicKey),
}

#[derive(Clone)]
pub struct Keypair {
    pub inner: KeypairOrPublicKey,
}

#[derive(Serialize, Deserialize)]
pub struct KeypairJSON {
    pub id: String,
    pub public_key: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub private_key: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub encrypted_private_key: Option<String>,
}

impl KeypairJSON {
    pub fn id(&self) -> &str {
        &self.id
    }

    pub fn public_key(&self) -> &str {
        &self.public_key
    }

    pub fn private_key(&self) -> Option<&str> {
        self.private_key.as_deref()
    }

    pub fn encrypted_private_key(&self) -> Option<&str> {
        self.encrypted_private_key.as_deref()
    }
}

impl Keypair {
    pub fn new(key: KeypairOrPublicKey) -> Self {
        Self { inner: key }
    }

    pub fn generate() -> Result<Self> {
        let key = ed25519::Keypair::generate();
        let libp2p_keypair = Libp2pKeypair::from(key);
        Ok(Self::new(KeypairOrPublicKey::Keypair(libp2p_keypair)))
    }

    pub async fn as_ssh_private_pem(&self, _comment: &str) -> Result<String> {
        // TODO: Implement SSH PEM conversion
        unimplemented!("SSH PEM conversion not implemented yet")
    }

    pub fn as_ssh_dot_pub(&self, _comment: &str) -> Result<String> {
        // TODO: Implement SSH public key conversion
        unimplemented!("SSH public key conversion not implemented yet")
    }

    pub fn from_ssh_dot_pub(_public_key_str: &str, _key_type: &str) -> Result<Self> {
        // TODO: Implement SSH public key parsing
        unimplemented!("SSH public key parsing not implemented yet")
    }

    fn uint8_array_as_base58_identity(bytes: &[u8]) -> String {
        let mut identity_hash = vec![0x00, bytes.len() as u8];
        identity_hash.extend_from_slice(bytes);
        identity_hash.to_base58()
    }

    fn public_key_bytes(&self) -> Vec<u8> {
        match &self.inner {
            KeypairOrPublicKey::Keypair(k) => k.public().encode_protobuf(),
            KeypairOrPublicKey::PublicKey(pk) => pk.encode_protobuf(),
        }
    }

    pub fn public_key_as_base58_identity(&self) -> String {
        Self::uint8_array_as_base58_identity(&self.public_key_bytes())
    }

    pub fn as_public_key_id(&self) -> String {
        self.public_key_as_base58_identity()
    }

    pub fn as_public_address(&self) -> String {
        self.public_key_as_base58_identity()
    }

    fn uint8_array_as_base64_pad(bytes: &[u8]) -> String {
        base64::encode(bytes)
    }

    pub fn public_key_as_base64_pad(&self) -> String {
        Self::uint8_array_as_base64_pad(&self.public_key_bytes())
    }

    pub fn private_key_as_base64_pad(&self) -> Result<String> {
        match &self.inner {
            KeypairOrPublicKey::Keypair(k) => {
                Ok(Self::uint8_array_as_base64_pad(&k.to_protobuf_encoding()?))
            }
            KeypairOrPublicKey::PublicKey(_) => Err(anyhow!("No private key available")),
        }
    }

    pub fn public_key_to_multiaddr_string(&self) -> String {
        let public_key_id = self.public_key_as_base58_identity();
        format!("/ed25519-pub/{}", public_key_id)
    }

    pub fn as_public_multiaddress(&self) -> String {
        self.public_key_to_multiaddr_string()
    }

    pub fn from_public_key(public_key_id: &str, key_type: &str) -> Result<Self> {
        Self::from_public_multiaddress(&format!("/{}-pub/{}", key_type, public_key_id))
    }

    pub fn from_public_multiaddress(multiaddress: &str) -> Result<Self> {
        let re = Regex::new(r"^(.+)-pub/(.+)$").unwrap();
        let captures = re
            .captures(multiaddress)
            .ok_or_else(|| anyhow!("Invalid multiaddress format"))?;

        // let _key_type = captures.get(1)
        //     .ok_or_else(|| anyhow!("Failed to extract key type"))?
        //     .as_str();
        let peer_id_str = captures
            .get(2)
            .ok_or_else(|| anyhow!("Failed to extract public key ID"))?
            .as_str();

        // Parse the peer ID
        let peer_id = peer_id_str
            .parse::<PeerId>()
            .map_err(|e| anyhow!("Failed to parse peer ID: {:?}", e))?;

        let public_key = libp2p::identity::PublicKey::try_decode_protobuf(&peer_id.to_bytes())
            .map_err(|e| anyhow!("Failed to decode public key from peer ID: {}", e))?;

        Ok(Self::new(KeypairOrPublicKey::PublicKey(public_key)))
    }

    pub fn from_json(json: &KeypairJSON) -> Result<Self> {
        if let Some(private_key) = &json.private_key {
            let key_bytes = base64::decode(private_key)?;
            let key = Libp2pKeypair::from_protobuf_encoding(&key_bytes)?;
            Ok(Self::new(KeypairOrPublicKey::Keypair(key)))
        } else {
            let key_bytes = base64::decode(&json.public_key)?;
            let public_key = Libp2pPublicKey::try_decode_protobuf(&key_bytes)?;
            Ok(Self::new(KeypairOrPublicKey::PublicKey(public_key)))
        }
    }

    pub fn from_json_string(json_str: &str) -> Result<Self> {
        let json: KeypairJSON = serde_json::from_str(json_str)?;
        Self::from_json(&json)
    }

    pub fn from_encrypted_json_file(filepath: &str, password: &str) -> Result<Self> {
        let json_str = fs::read_to_string(filepath)?;
        let json: KeypairJSON = serde_json::from_str(&json_str)?;

        println!("keypair json: {}", json_str);

        if let Some(encrypted_key) = json.encrypted_private_key() {
            let decrypted_key = EncryptedText::decrypt(encrypted_key, password).ok();
            let decrypted_json = KeypairJSON {
                id: json.id().to_string(),
                public_key: json.public_key().to_string(),
                private_key: decrypted_key,
                encrypted_private_key: None,
            };
            Self::from_json(&decrypted_json)
        } else {
            Self::from_json(&json)
        }
    }

    pub fn from_json_file(filepath: &str) -> Result<Self> {
        let json_str = fs::read_to_string(filepath)?;
        Self::from_json_string(&json_str)
    }

    pub fn as_public_json(&self) -> Result<KeypairJSON> {
        Ok(KeypairJSON {
            id: self.public_key_as_base58_identity(),
            public_key: self.public_key_as_base64_pad(),
            private_key: None,
            encrypted_private_key: None,
        })
    }

    pub fn as_public_json_string(&self) -> Result<String> {
        let json = self.as_public_json()?;
        Ok(serde_json::to_string(&json)?)
    }

    pub fn as_public_json_file(&self, path: &str) -> Result<()> {
        let json_string = self.as_public_json_string()?;
        fs::write(path, json_string)?;
        Ok(())
    }

    pub fn as_json(&self) -> Result<KeypairJSON> {
        let private_key = self.private_key_as_base64_pad().ok();
        let encrypted_private_key = if private_key.is_some() {
            None
        } else {
            Some("".to_string()) // Use actual encrypted_private_key if available
        };

        Ok(KeypairJSON {
            id: self.public_key_as_base58_identity(),
            public_key: self.public_key_as_base64_pad(),
            private_key,
            encrypted_private_key,
        })
    }

    pub fn as_json_string(&self) -> Result<String> {
        let json = self.as_json()?;
        Ok(serde_json::to_string(&json)?)
    }

    pub fn as_json_file(&self, path: &str) -> Result<()> {
        let json_string = self.as_json_string()?;
        fs::write(path, json_string)?;
        Ok(())
    }

    pub fn as_encrypted_json(&self, password: &str) -> Result<KeypairJSON> {
        let enc_pk = EncryptedText::encrypt(&self.private_key_as_base64_pad()?, password).ok();
        Ok(KeypairJSON {
            id: self.public_key_as_base58_identity(),
            public_key: self.public_key_as_base64_pad(),
            private_key: None,
            encrypted_private_key: enc_pk,
        })
    }

    pub fn as_encrypted_json_string(&self, password: &str) -> Result<String> {
        let json = self.as_encrypted_json(password)?;
        Ok(serde_json::to_string(&json)?)
    }

    pub fn as_encrypted_json_file(&self, path: &str, password: &str) -> Result<()> {
        let json_string = self.as_encrypted_json_string(password)?;
        fs::write(path, json_string)?;
        Ok(())
    }

    pub fn sign_bytes(&self, bytes: &[u8]) -> Result<Vec<u8>> {
        match &self.inner {
            KeypairOrPublicKey::Keypair(k) => Ok(k.sign(bytes)?),
            KeypairOrPublicKey::PublicKey(_) => Err(anyhow!("Cannot sign with public key only")),
        }
    }

    pub fn sign_string(&self, s: &str) -> Result<Vec<u8>> {
        self.sign_bytes(s.as_bytes())
    }

    pub fn sign_string_as_base64_pad(&self, s: &str) -> Result<String> {
        let signature = self.sign_string(s)?;
        Ok(base64::encode(signature))
    }

    pub fn sign_json(&self, json: &Value) -> Result<String> {
        let str = stringify_deterministic(json, None);
        self.sign_string_as_base64_pad(&str)
    }

    pub fn sign_json_element(&self, json: &mut Value, name: &str, suffix: &str) -> Result<()> {
        let signature = self.sign_json(&json[name])?;
        json[format!("{}{}", name, suffix)] = Value::String(signature);
        Ok(())
    }

    pub fn sign_json_as_key(&self, json: &mut Value, key: &str) -> Result<()> {
        let signature = self.sign_json(json)?;
        json[key] = Value::String(signature);
        Ok(())
    }

    pub fn verify_signature_for_bytes(&self, signature: &str, bytes: &[u8]) -> Result<bool> {
        let signature_bytes = base64::decode(signature)?;
        match &self.inner {
            KeypairOrPublicKey::Keypair(k) => Ok(k.public().verify(bytes, &signature_bytes)),
            KeypairOrPublicKey::PublicKey(pk) => Ok(pk.verify(bytes, &signature_bytes)),
        }
    }

    pub fn verify_signature_for_string(&self, signature: &str, s: &str) -> Result<bool> {
        self.verify_signature_for_bytes(signature, s.as_bytes())
    }

    pub fn verify_json(&self, signature: &str, json: &Value) -> Result<bool> {
        let str = stringify_deterministic(json, None);
        self.verify_signature_for_string(signature, &str)
    }

    pub fn verify_json_with_signature_key(
        &self,
        json: &Value,
        signature_key: &str,
    ) -> Result<bool> {
        if let Value::Object(map) = json {
            let signature = map
                .get(signature_key)
                .ok_or_else(|| anyhow!("Signature key not found"))?
                .as_str()
                .ok_or_else(|| anyhow!("Signature must be a string"))?;

            let mut json_without_signature = json.clone();
            if let Value::Object(map_without_signature) = &mut json_without_signature {
                map_without_signature.remove(signature_key);
            }

            let stringified = stringify_deterministic(&json_without_signature, None);
            self.verify_signature_for_string(signature, &stringified)
        } else {
            Err(anyhow!("Input must be a JSON object"))
        }
    }

    pub fn verify_signatures_in_json(&self, json: &Value, suffix: Option<&str>) -> Result<bool> {
        let suffix = suffix.unwrap_or(".signature");
        let suffix_regex = Regex::new(&format!(r"(.+){}$", regex::escape(suffix)))?;

        if let Value::Object(map) = json {
            for (key, value) in map {
                if let Some(captures) = suffix_regex.captures(key) {
                    let original_key = captures.get(1).unwrap().as_str();
                    if let Some(original_value) = map.get(original_key) {
                        let signature = value
                            .as_str()
                            .ok_or_else(|| anyhow!("Signature must be a string"))?;
                        let stringified = stringify_deterministic(original_value, None);

                        if !self.verify_signature_for_string(signature, &stringified)? {
                            return Ok(false);
                        }
                    } else {
                        return Ok(false); // Original value not found
                    }
                }
            }
            Ok(true) // All signatures verified successfully
        } else {
            Err(anyhow!("Input must be a JSON object"))
        }
    }
}