use nostr_sdk::nips::{nip44, nip59};
use nostr_sdk::prelude::*;
use crate::error::{MostroError, ServiceError};
pub async fn wrap_chat_message(
sender_trade_keys: &Keys,
shared_pubkey: &PublicKey,
message: &str,
) -> Result<Event, MostroError> {
let inner = EventBuilder::text_note(message)
.build(sender_trade_keys.public_key())
.sign(sender_trade_keys)
.await
.map_err(|e| MostroError::MostroInternalErr(ServiceError::NostrError(e.to_string())))?;
let ephemeral = Keys::generate();
let encrypted = nip44::encrypt(
ephemeral.secret_key(),
shared_pubkey,
inner.as_json(),
nip44::Version::V2,
)
.map_err(|e| MostroError::MostroInternalErr(ServiceError::EncryptionError(e.to_string())))?;
EventBuilder::new(Kind::GiftWrap, encrypted)
.tag(Tag::public_key(*shared_pubkey))
.custom_created_at(Timestamp::tweaked(nip59::RANGE_RANDOM_TIMESTAMP_TWEAK))
.sign_with_keys(&ephemeral)
.map_err(|e| MostroError::MostroInternalErr(ServiceError::NostrError(e.to_string())))
}