use super::*;
use crate::internal_prelude::*;
#[derive(Debug, Clone, Eq, PartialEq, ManifestSbor, ScryptoDescribe, Default)]
pub enum MessageV1 {
#[default]
None,
Plaintext(PlaintextMessageV1),
Encrypted(EncryptedMessageV1),
}
#[derive(Debug, Clone, PartialEq, Eq, ManifestSbor, ScryptoDescribe)]
pub struct PlaintextMessageV1 {
pub mime_type: String,
pub message: MessageContentsV1,
}
impl PlaintextMessageV1 {
pub fn text(message: impl Into<String>) -> Self {
Self {
mime_type: "text/plain".to_string(),
message: MessageContentsV1::String(message.into()),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, ManifestSbor, ScryptoDescribe)]
pub enum MessageContentsV1 {
String(String),
Bytes(Vec<u8>),
}
impl MessageContentsV1 {
#[allow(clippy::len_without_is_empty)]
pub fn len(&self) -> usize {
match self {
MessageContentsV1::String(message) => message.len(),
MessageContentsV1::Bytes(message) => message.len(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, ManifestSbor, ScryptoDescribe)]
pub struct EncryptedMessageV1 {
pub encrypted: AesGcmPayload,
pub decryptors_by_curve: IndexMap<CurveType, DecryptorsByCurve>,
}
#[derive(
Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, ManifestSbor, ScryptoDescribe,
)]
pub enum CurveType {
Ed25519,
Secp256k1,
}
#[derive(Debug, Clone, PartialEq, Eq, ManifestSbor, ScryptoDescribe)]
pub enum DecryptorsByCurve {
Ed25519 {
dh_ephemeral_public_key: Ed25519PublicKey,
decryptors: IndexMap<PublicKeyFingerprint, AesWrapped128BitKey>,
},
Secp256k1 {
dh_ephemeral_public_key: Secp256k1PublicKey,
decryptors: IndexMap<PublicKeyFingerprint, AesWrapped128BitKey>,
},
}
impl DecryptorsByCurve {
pub fn curve_type(&self) -> CurveType {
match self {
DecryptorsByCurve::Ed25519 { .. } => CurveType::Ed25519,
DecryptorsByCurve::Secp256k1 { .. } => CurveType::Secp256k1,
}
}
pub fn number_of_decryptors(&self) -> usize {
match self {
DecryptorsByCurve::Ed25519 { decryptors, .. } => decryptors.len(),
DecryptorsByCurve::Secp256k1 { decryptors, .. } => decryptors.len(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, ManifestSbor, ScryptoDescribe)]
#[sbor(transparent)]
pub struct PublicKeyFingerprint(pub [u8; Self::LENGTH]);
impl PublicKeyFingerprint {
pub const LENGTH: usize = 8;
}
impl From<PublicKey> for PublicKeyFingerprint {
fn from(value: PublicKey) -> Self {
value.get_hash().into()
}
}
impl From<PublicKeyHash> for PublicKeyFingerprint {
fn from(value: PublicKeyHash) -> Self {
let hash_bytes = value.get_hash_bytes();
let fingerprint_bytes = &hash_bytes[(hash_bytes.len() - Self::LENGTH)..hash_bytes.len()];
PublicKeyFingerprint(copy_u8_array(fingerprint_bytes))
}
}
#[derive(Debug, Clone, Eq, PartialEq, ManifestSbor, ScryptoDescribe)]
#[sbor(transparent)]
pub struct AesGcmPayload(pub Vec<u8>);
#[derive(Debug, Clone, Eq, PartialEq, ManifestSbor, ScryptoDescribe)]
#[sbor(transparent)]
pub struct AesWrapped128BitKey(pub [u8; Self::LENGTH]);
impl AesWrapped128BitKey {
pub const LENGTH: usize = 24;
}
#[allow(deprecated)]
pub type PreparedMessageV1 = SummarizedRawFullValue<MessageV1>;