use std::collections::HashMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::{get_from_to_from_message, utils::hex_option};
pub trait HasFromAndTo {
fn get_from_to(&self) -> Result<FromTo, Box<dyn std::error::Error>>;
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DidCommPubKey {
pub id: String,
pub public_key_base_58: String,
pub r#type: Vec<String>,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DidCommService {
pub id: String,
pub r#type: String,
pub priority: u8,
pub service_endpoint: String,
pub recipient_keys: Vec<String>,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CommunicationDidDocument {
#[serde(rename(serialize = "@context", deserialize = "@context"))]
pub context: String,
pub id: String,
pub authentication: Vec<String>,
pub public_key: Vec<DidCommPubKey>,
pub service: Vec<DidCommService>,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct FromTo {
pub from: String,
pub to: String,
}
#[derive(Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ExchangeInfo {
pub from: String,
pub to: String,
pub did_id: String,
pub pub_key_hex: String,
pub service_endpoint: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct CommKeyPair {
pub pub_key: String,
pub secret_key: String,
pub key_agreement_key: String,
pub target_key_agreement_key: String,
pub target_pub_key: String,
pub target_service_endpoint: String,
}
#[derive(PartialEq)]
pub enum MessageDirection {
Send,
Receive,
}
pub struct ProtocolHandleOutput {
pub direction: MessageDirection,
pub encrypt: bool,
pub protocol: String,
pub metadata: String,
pub message: String,
pub step: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MessageWithType {
pub r#type: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct BaseMessage {
pub body: HashMap<String, Value>,
pub from: Option<String>,
pub r#type: String,
pub to: Option<Vec<String>>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct ExtendedMessage {
pub body: Option<HashMap<String, Value>>,
pub created_time: Option<u64>,
pub expires_time: Option<u64>,
pub from: Option<String>,
pub id: Option<String>,
pub pthid: Option<String>,
pub r#type: String,
pub thid: Option<String>,
pub to: Option<Vec<String>>,
#[serde(flatten, skip_serializing_if = "HashMap::is_empty")]
pub other: HashMap<String, String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Base64Container {
pub base64: String,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct DidDocumentBodyAttachment<T> {
#[serde(rename(serialize = "did_doc~attach", deserialize = "did_doc~attach"))]
pub did_doc_attach: T,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct MessageWithBody<T> {
pub body: Option<T>,
pub created_time: Option<u64>,
pub expires_time: Option<u64>,
pub from: Option<String>,
pub id: Option<String>,
pub pthid: Option<String>,
pub r#type: String,
pub thid: Option<String>,
pub to: Option<Vec<String>>,
#[serde(flatten, skip_serializing_if = "HashMap::is_empty")]
pub other: HashMap<String, String>,
}
impl<T> HasFromAndTo for MessageWithBody<T> {
fn get_from_to(&self) -> Result<FromTo, Box<dyn std::error::Error>> {
let base_message: BaseMessage = BaseMessage {
body: HashMap::new(),
from: self.from.to_owned(),
r#type: self.r#type.to_owned(),
to: Some(self.to.clone().ok_or("To DID not provided.")?.to_vec()),
};
get_from_to_from_message(&base_message)
}
}
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct EncryptedMessage {
pub from: Option<String>,
pub kid: Option<String>,
pub to: Option<Vec<String>>,
pub r#type: Option<String>,
pub ciphertext: Vec<u8>,
pub iv: Vec<u8>,
pub id: Option<u64>,
#[serde(flatten, skip_serializing_if = "HashMap::is_empty")]
pub other: HashMap<String, String>,
}
#[derive(Serialize, Deserialize)]
pub struct EncryptionKeyPair {
#[serde(with = "hex")]
pub secret: [u8; 32],
#[serde(with = "hex")]
pub public: [u8; 32],
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EncryptionKeys {
#[serde(with = "hex")]
pub encryption_my_secret: [u8; 32],
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(with = "hex_option")]
pub encryption_others_public: Option<[u8; 32]>,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SigningKeys {
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(with = "hex_option")]
pub signing_my_secret: Option<[u8; 32]>,
#[serde(default)]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(with = "hex_option")]
pub signing_others_public: Option<[u8; 32]>,
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub struct Data {
#[serde(flatten)]
#[serde(skip_serializing_if = "Option::is_none")]
pub json: Option<Value>,
#[serde(skip_serializing_if = "Option::is_none")]
pub base64: Option<String>,
}
#[derive(Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DidCommOptions {
pub encryption_keys: Option<EncryptionKeys>,
pub signing_keys: Option<SigningKeys>,
pub skip_message_packaging: Option<bool>,
pub skip_protocol_handling: Option<bool>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct VadeDidCommPluginSendOutput<T, TRaw = serde_json::Value> {
pub message: T,
pub message_raw: TRaw,
pub metadata: HashMap<String, String>,
}
#[derive(Debug, Serialize, Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct VadeDidCommPluginReceiveOutput<T> {
pub message: T,
pub metadata: HashMap<String, String>,
}