use serde_json::json; use std::{collections::hash_map::DefaultHasher, hash::Hash, hash::Hasher}; use serde::{Deserialize, Serialize}; use crate::workflows::workflow_pcd_create_and_send_all::PcdItemEncAttributeRoleConfidentialExportKey; use super::{ commitment_method::CommitmentMethod, deposit_method::DepositMethod, key_encryption::KeyEncryption, message_client::MessageClient, payment_method::PaymentMethod, request_prd::RequestPrd, shared_peer::SharedPeer, shared_process::SharedProcess, }; #[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct RequestPrdResponse { pub prd: RequestPrd, pub sig_value: String, pub pcd_origin_hash: Option, pub payment_method_enc_by_shared_secret: Option, pub deposit_method_enc_by_shared_secret: Option, pub commitment_method_enc_by_shared_secret: Option, pub certif_key_enc_by_shared_secret: Option, pub shared_secret_key: Option, } impl RequestPrdResponse { pub const TYPE: &'static str = "prd_response"; pub fn new( process_public_enc_key: KeyEncryption, member_private_enc_key: KeyEncryption, request_item_name: Option, request_version: i64, request_process_hash: String, request_pcd_reference_hash: Option, request_item_reference_hash: Option, pcd_keys_role_confidential_list: Vec, message_public_string: Option, message_confidential_string: Option, message_private_string: Option, sp_address_to: String, sp_address_from: String, sp_address_reply: String, timestamp_declared: u64, role_name_from: String, role_name_to: String, sig_value: String, pcd_origin_hash: Option, payment_method: Option, deposit_method: Option, commitment_method: Option, certif_key: Option, ) -> Self { let mut shared_secret_key = KeyEncryption::new(Some("secret_shared".to_owned()), None, None); if let Ok(_new) = shared_secret_key.key_new_random() { let payment_method_enc_by_shared_secret = match payment_method { Some(ref _msg) => { Some(shared_secret_key.enc(serde_json::to_value(payment_method).unwrap())) } None => None, }; let deposit_method_enc_by_shared_secret = match deposit_method { Some(ref _msg) => { Some(shared_secret_key.enc(serde_json::to_value(deposit_method).unwrap())) } None => None, }; let commitment_method_enc_by_shared_secret = match commitment_method { Some(ref _msg) => { Some(shared_secret_key.enc(serde_json::to_value(commitment_method).unwrap())) } None => None, }; let certif_key_enc_by_shared_secret = match certif_key { Some(ref _msg) => { Some(shared_secret_key.enc(serde_json::to_value(certif_key).unwrap())) } None => None, }; let message_confidential = message_confidential_string .as_ref() .map(|msg| shared_secret_key.enc_string(msg.clone())); let message_public = message_public_string .as_ref() .map(|msg| process_public_enc_key.enc_string(msg.clone())); let message_private = message_private_string .as_ref() .map(|msg| member_private_enc_key.enc_string(msg.clone())); let pcd_keys_role_confidential_list_enc_by_shared_secret = shared_secret_key.enc( serde_json::to_value(pcd_keys_role_confidential_list).unwrap_or_else(|_| json!({})), ); let request_type = Self::TYPE.to_string(); let prd = RequestPrd::new( request_item_name, request_type, request_version, request_process_hash, request_pcd_reference_hash, request_item_reference_hash, pcd_keys_role_confidential_list_enc_by_shared_secret, message_public, message_confidential, message_private, sp_address_to, sp_address_from, sp_address_reply, timestamp_declared, role_name_from, role_name_to, ); RequestPrdResponse { prd, sig_value, pcd_origin_hash, payment_method_enc_by_shared_secret, deposit_method_enc_by_shared_secret, commitment_method_enc_by_shared_secret, certif_key_enc_by_shared_secret, shared_secret_key: Some(shared_secret_key), } } else { panic!("Error: Could not create shared secret key"); } } pub fn to_message( &self, process_public_enc_key: KeyEncryption, message_shared_peer_list: Vec, message_shared_process_list: Vec, message_faucet_sp_address: String, pow_pathern: String, pow_difficulty: usize, ) -> MessageClient { let mut hasher: DefaultHasher = DefaultHasher::new(); self.hash(&mut hasher); let request_hash = hasher.finish().to_string(); let request_enc: String = process_public_enc_key .enc_prd_response(self.clone()) .to_string(); MessageClient::new( request_enc, request_hash, message_shared_peer_list, message_shared_process_list, message_faucet_sp_address, pow_pathern, pow_difficulty, ) } // Fonction pour afficher ou retourner les informations pub fn display_info(&self) -> String { format!( "PRD: {:?}, Sig Value: {}, PCD Origin Hash: {}, Payment Method Encrypted: {}, Deposit Method Encrypted: {}, Commitment Method Encrypted: {}, Certification Key Encrypted: {}", self.prd, self.sig_value, self.pcd_origin_hash.as_ref().unwrap(), self.payment_method_enc_by_shared_secret.as_ref().unwrap(), self.deposit_method_enc_by_shared_secret.as_ref().unwrap(), self.commitment_method_enc_by_shared_secret.as_ref().unwrap(), self.certif_key_enc_by_shared_secret.as_ref().unwrap() ) } }