karak_kms/keypair/bn254/
encryption.rs1use super::{Bn254Error, Keypair};
2use crate::{
3 encryption::{self, EncryptDataV3Error},
4 keypair::traits::Encryptable,
5};
6use thiserror::Error;
7
8#[derive(Debug, Error)]
9pub enum KeypairEncryptionError {
10 #[error("Keypair error: {0}")]
11 KeypairError(#[from] Bn254Error),
12
13 #[error("Encryption error: {0}")]
14 EncryptionError(#[from] EncryptDataV3Error),
15
16 #[error("Serialization error: {0}")]
17 SerializationError(#[from] bincode::Error),
18}
19
20impl Encryptable for Keypair {
21 type EncryptionError = KeypairEncryptionError;
22
23 fn encrypt(&self, passphrase: &str) -> Result<Vec<u8>, KeypairEncryptionError> {
24 let serialized_keypair = self.to_bytes()?;
25 let encrypted_keypair =
28 encryption::encrypt_data_v3(&serialized_keypair, passphrase.as_bytes(), 14, 1)?;
29
30 Ok(bincode::serialize(&encrypted_keypair)?)
31 }
32
33 fn decrypt(encrypted_keypair: &[u8], passphrase: &str) -> Result<Self, KeypairEncryptionError> {
34 let serialized_keypair = encryption::decrypt_data_v3(
35 &bincode::deserialize(encrypted_keypair)?,
36 passphrase.as_bytes(),
37 )?;
38
39 Ok(Keypair::try_from(serialized_keypair.as_slice())?)
40 }
41}