sdk_common/src/models/request_prd_key_backup.rs
2024-03-25 14:24:33 +01:00

109 lines
3.5 KiB
Rust

use std::{
collections::hash_map::DefaultHasher,
hash::{Hash, Hasher},
};
use serde::{Deserialize, Serialize};
use super::{
key_encryption::KeyEncryption, message_client::MessageClient, request_prd::Prd,
shared_peer::Peer, shared_process::Process,
};
#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct PrdKeyBackup {
pub prd: Prd,
pub device_footprint_enc_by_sp_shared_secret: String,
pub part_1_enc_hash_enc_by_sp_shared_secret: String,
pub shard_enc_by_sp_shared_secret: String,
}
impl PrdKeyBackup {
pub const TYPE: &'static str = "prd_key_backup";
pub fn new(
request_item_name: Option<String>,
request_version: i64,
request_process_hash: String,
request_pcd_reference_hash: Option<String>,
request_item_reference_hash: Option<String>,
pcd_keys_role_confidential_list_confidential: String,
message_public: Option<String>,
message_confidential: Option<String>,
message_private: Option<String>,
sp_address_to: String,
sp_address_from: String,
sp_address_reply: String,
timestamp_declared: u64,
role_name_from: String,
role_name_to: String,
device_footprint_enc_by_sp_shared_secret: String,
part_1_enc_hash_enc_by_sp_shared_secret: String,
shard_enc_by_sp_shared_secret: String,
) -> Self {
let request_type = Self::TYPE.to_string();
let prd = Prd::new(
request_item_name,
request_type,
request_version,
request_process_hash,
request_pcd_reference_hash,
request_item_reference_hash,
pcd_keys_role_confidential_list_confidential,
message_public,
message_confidential,
message_private,
sp_address_to,
sp_address_from,
sp_address_reply,
timestamp_declared,
role_name_from,
role_name_to,
);
PrdKeyBackup {
prd,
device_footprint_enc_by_sp_shared_secret,
part_1_enc_hash_enc_by_sp_shared_secret,
shard_enc_by_sp_shared_secret,
}
}
pub fn to_message(
&self,
process_public_enc_key: KeyEncryption,
message_shared_peer_list: Vec<Peer>,
message_shared_process_list: Vec<Process>,
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_key_backup(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: {:?}, Device Footprint Encrypted: {}, Part 1 Encrypted Hash: {}, Shard Encrypted: {}",
self.prd,
self.device_footprint_enc_by_sp_shared_secret,
self.part_1_enc_hash_enc_by_sp_shared_secret,
self.shard_enc_by_sp_shared_secret
)
}
}