Rm CachedMessage
This commit is contained in:
parent
a7245f3083
commit
7f096ddc62
@ -194,97 +194,3 @@ impl Envelope {
|
||||
serde_json::to_string(self).unwrap()
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Serialize, Deserialize, PartialEq, Tsify, Clone)]
|
||||
pub enum CachedMessageStatus {
|
||||
#[default]
|
||||
NoStatus,
|
||||
CipherWaitingTx,
|
||||
TxWaitingPrd,
|
||||
}
|
||||
|
||||
/// Unique struct for both 4nk messages and notification/key exchange, both rust and ts
|
||||
#[derive(Debug, Default, Serialize, Deserialize, Tsify, Clone, PartialEq)]
|
||||
#[tsify(into_wasm_abi, from_wasm_abi)]
|
||||
#[allow(non_camel_case_types)]
|
||||
pub struct CachedMessage {
|
||||
pub id: u32,
|
||||
pub status: CachedMessageStatus,
|
||||
pub transaction: Option<String>,
|
||||
pub commitment: Option<String>, // content of the op_return
|
||||
pub sender: Option<Member>, // Never None when message sent
|
||||
pub shared_secrets: Vec<String>, // Max 2 secrets in case we send to both address of the recipient
|
||||
pub cipher: Vec<String>, // Max 2 ciphers in case we send to both address of the recipient
|
||||
pub timestamp: u64,
|
||||
}
|
||||
|
||||
impl CachedMessage {
|
||||
pub fn new() -> Self {
|
||||
let mut new = Self::default();
|
||||
let mut buf = [0u8; 4];
|
||||
thread_rng().fill_bytes(&mut buf);
|
||||
new.id = u32::from_be_bytes(buf);
|
||||
new.timestamp = Date::now().floor() as u64;
|
||||
new
|
||||
}
|
||||
|
||||
pub fn from_string(json: &str) -> Result<Self> {
|
||||
let res = serde_json::from_str(json)?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
pub fn to_string(&self) -> String {
|
||||
serde_json::to_string(self).unwrap()
|
||||
}
|
||||
|
||||
pub fn try_decrypt_message(&self, cipher: &[u8]) -> Result<Vec<u8>> {
|
||||
if self.shared_secrets.is_empty() {
|
||||
return Err(Error::msg(
|
||||
"Can't try decrypt this message, there's no shared secret",
|
||||
));
|
||||
}
|
||||
for shared_secret in &self.shared_secrets {
|
||||
let mut key = [0u8; 32];
|
||||
let mut nonce = [0u8; 12];
|
||||
key.copy_from_slice(&Vec::from_hex(shared_secret)?);
|
||||
nonce.copy_from_slice(&cipher[..12]);
|
||||
|
||||
let engine = Aes256Gcm::new(&key.into());
|
||||
let payload = Payload {
|
||||
msg: &cipher[12..],
|
||||
aad: AAD,
|
||||
};
|
||||
match engine.decrypt(&nonce.into(), payload) {
|
||||
Ok(plain) => return Ok(plain),
|
||||
Err(_) => continue,
|
||||
}
|
||||
}
|
||||
|
||||
Err(Error::msg("Failed to decrypt message"))
|
||||
}
|
||||
|
||||
pub fn try_decrypt_with_shared_secret(&self, shared_secret: [u8; 32]) -> Result<Vec<u8>> {
|
||||
if self.cipher.is_empty() || !self.shared_secrets.is_empty() {
|
||||
return Err(Error::msg(
|
||||
"Can't try decrypt this message, ciphertext is none or shared_secret already found",
|
||||
));
|
||||
}
|
||||
for prd_cipher in &self.cipher {
|
||||
let cipher = Vec::from_hex(prd_cipher)?;
|
||||
let mut nonce = [0u8; 12];
|
||||
nonce.copy_from_slice(&cipher[..12]);
|
||||
|
||||
let engine = Aes256Gcm::new(&shared_secret.into());
|
||||
let payload = Payload {
|
||||
msg: &cipher[12..],
|
||||
aad: AAD,
|
||||
};
|
||||
match engine.decrypt(&nonce.into(), payload) {
|
||||
Ok(plain) => return Ok(plain),
|
||||
Err(_) => continue,
|
||||
}
|
||||
}
|
||||
|
||||
Err(Error::msg("Failed to decrypt message"))
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user