sdk_common/src/models/item_artefact.rs

132 lines
4.8 KiB
Rust

use serde::{Deserialize, Serialize};
use serde_json::json;
use std::hash::Hash;
use super::item::Item;
use super::key_encryption::KeyEncryption;
use super::pcd_item_enc_attribute_private::PcdItemEncAttributePrivate;
use super::pcd_item_enc_attribute_public::PcdItemEncAttributePublic;
use super::pcd_item_enc_attribute_role_confidential::PcdItemEncAttributeRoleConfidential;
use super::pcd_item_generic_enc::PcdItemGenericEnc;
#[derive(Debug, Serialize, Deserialize, Default, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ItemArtefact {
pub item: Item,
pub public_attribute_group: Vec<String>, // Assuming list of attributes
pub role_confidential_attribute_group: Vec<String>,
pub private_attribute_group: Vec<String>,
}
impl ItemArtefact {
pub fn new(
item: Item,
public_attribute_group: Vec<String>,
role_confidential_attribute_group: Vec<String>,
private_attribute_group: Vec<String>,
) -> Self {
ItemArtefact {
item,
public_attribute_group,
role_confidential_attribute_group,
private_attribute_group,
}
}
pub fn enc_public_attribute_group(
&self,
process_public_enc_key: KeyEncryption,
) -> Vec<PcdItemEncAttributePublic> {
let mut enc_attribute_list: Vec<PcdItemEncAttributePublic> = Vec::new();
let mut count = 0;
for public_attribute in &self.public_attribute_group {
let name = "index".to_owned() + &count.to_string();
let value = serde_json::to_value(public_attribute).unwrap_or_else(|_| json!({}));
let enc_attribute =
PcdItemEncAttributePublic::new(name, process_public_enc_key.enc(value));
enc_attribute_list.push(enc_attribute);
count += 1;
}
enc_attribute_list
}
pub fn enc_role_confidential_attribute_group(
&self,
) -> Vec<PcdItemEncAttributeRoleConfidential> {
let mut enc_attribute_list: Vec<PcdItemEncAttributeRoleConfidential> = Vec::new();
let mut count = 0;
for role_confidential_attribute in &self.role_confidential_attribute_group {
let name = "index".to_owned() + &count.to_string();
let value =
serde_json::to_value(role_confidential_attribute).unwrap_or_else(|_| json!({}));
let mut role_confidential_attribute_key =
KeyEncryption::new(Some(name.clone()), None, None);
if let Ok(_new) = role_confidential_attribute_key.key_new_random() {
let enc_attribute = PcdItemEncAttributeRoleConfidential::new(
name,
role_confidential_attribute_key.enc(value),
role_confidential_attribute_key,
);
enc_attribute_list.push(enc_attribute);
}
count += 1;
}
enc_attribute_list
}
pub fn enc_private_attribute_group(
&self,
member_private_enc_key: KeyEncryption,
) -> Vec<PcdItemEncAttributePrivate> {
let mut enc_attribute_list: Vec<PcdItemEncAttributePrivate> = Vec::new();
let mut count = 0;
for private_attribute in &self.public_attribute_group {
let name = "index".to_owned() + &count.to_string();
let value = serde_json::to_value(private_attribute).unwrap_or_else(|_| json!({}));
let enc_attribute =
PcdItemEncAttributePrivate::new(name, member_private_enc_key.enc(value));
enc_attribute_list.push(enc_attribute);
count += 1;
}
enc_attribute_list
}
pub fn enc(
&self,
process_public_enc_key: KeyEncryption,
member_private_enc_key: KeyEncryption,
) -> PcdItemGenericEnc {
let enc_metadata_contract_public =
self.enc_public_attribute_group(process_public_enc_key.clone());
let enc_role_confidential_attribute_group = self.enc_role_confidential_attribute_group();
let enc_metadata_private = self.enc_private_attribute_group(member_private_enc_key.clone());
PcdItemGenericEnc::new(
self.item
.enc(process_public_enc_key, member_private_enc_key),
Some(enc_metadata_contract_public),
Some(enc_role_confidential_attribute_group),
Some(enc_metadata_private),
)
}
pub fn display_info(&self) {
println!("ItemArtefact:");
println!("Item:");
self.item.display_info(); // Appelle display_info sur item
println!("Public Attribute Group: {:?}", self.public_attribute_group);
println!(
"Role Confidential Attribute Group: {:?}",
self.role_confidential_attribute_group
);
println!(
"Private Attribute Group: {:?}",
self.private_attribute_group
);
}
}