diff --git a/crates/sp_client/src/api.rs b/crates/sp_client/src/api.rs index 1b1b0a9..e118698 100644 --- a/crates/sp_client/src/api.rs +++ b/crates/sp_client/src/api.rs @@ -398,6 +398,40 @@ pub fn parse_network_msg(raw: String) -> ApiResult { message: ank_msg.content.to_owned(), }) } + AnkFlag::Unknown => { + let transaction_cache = lock_scanned_transactions()?; + // try to decrypt the cipher with all available keys + let mut plaintext: String = "".to_owned(); + for (txid, secret_vec) in transaction_cache.iter() { + for (shared_with, ank_secret) in secret_vec.iter() { + let shared_secret = ank_secret.to_byte_array(); + if let Ok(msg_decrypt) = Aes256Decryption::new( + Purpose::Arbitrary, + ank_msg.content.as_bytes().to_vec(), + shared_secret, + ) { + if let Ok(plain) = msg_decrypt.decrypt_with_key() { + plaintext = String::from_utf8(plain)?; + break; + } + } + continue; + } + } + if plaintext.is_empty() { + // keep the message in cache, just in case + // return an error + return Err(ApiError { + message: "No key found".to_owned(), + }); + } else { + // return the plain text + return Ok(parseNetworkMsgReturn { + topic: AnkFlag::Unknown.as_str().to_owned(), + message: plaintext, + }); + } + } _ => unimplemented!(), } } else { @@ -493,13 +527,12 @@ pub fn create_notification_transaction( address2secret.push((sp_address.into(), shared_secret)); // update our cache - lock_scanned_transactions()? - .insert(transaction.txid(), address2secret.clone()); + lock_scanned_transactions()?.insert(transaction.txid(), address2secret.clone()); Ok(createNotificationTransactionReturn { txid: transaction.txid().to_string(), transaction: serialize(&transaction).to_lower_hex_string(), - address2secret: address2secret.into_iter().collect() + address2secret: address2secret.into_iter().collect(), }) } @@ -515,7 +548,7 @@ pub struct encryptWithNewKeyResult { pub fn encrypt_with_key(plaintext: String, key: String) -> ApiResult { let nonce = Aes256Gcm::generate_nonce(&mut rand::thread_rng()); - let mut aes_key = [0u8;32]; + let mut aes_key = [0u8; 32]; aes_key.copy_from_slice(&Vec::from_hex(&key)?); // encrypt @@ -555,30 +588,25 @@ pub fn encrypt_with_new_key(plaintext: String) -> ApiResult ApiResult { +pub fn try_decrypt_with_key(cipher: String, key: String) -> ApiResult { let key_bin = Vec::from_hex(&key)?; if key_bin.len() != 32 { - return Err(ApiError { message: "key of invalid lenght".to_owned() }); + return Err(ApiError { + message: "key of invalid lenght".to_owned(), + }); } - let mut aes_key = [0u8;32]; + let mut aes_key = [0u8; 32]; aes_key.copy_from_slice(&Vec::from_hex(&key)?); - let aes_dec = Aes256Decryption::new( - Purpose::Arbitrary, - Vec::from_hex(&cipher)?, - aes_key - )?; + let aes_dec = Aes256Decryption::new(Purpose::Arbitrary, Vec::from_hex(&cipher)?, aes_key)?; let plain = String::from_utf8(aes_dec.decrypt_with_key()?)?; Ok(plain) } #[wasm_bindgen] -pub fn create_commitment(payload_to_hash: String) -> String{ - let mut engine = sha256::HashEngine::default(); - engine.write_all(&payload_to_hash.as_bytes()); - let hash = sha256::Hash::from_engine(engine); - String::from_utf8_lossy(hash.to_bytes()) +pub fn create_commitment(payload_to_hash: String) -> String { + let mut engine = sha256::HashEngine::default(); + engine.write_all(&payload_to_hash.as_bytes()); + let hash = sha256::Hash::from_engine(engine); + hash.to_byte_array().to_lower_hex_string() }