diff --git a/src/crypto.rs b/src/crypto.rs index e4d1945..6caefa5 100644 --- a/src/crypto.rs +++ b/src/crypto.rs @@ -4,12 +4,14 @@ use anyhow::{Error, Result}; use serde::{Deserialize, Serialize}; use sp_client::{ bitcoin::{ - hex::{DisplayHex, FromHex}, key::constants::SECRET_KEY_SIZE, Txid + hex::{DisplayHex, FromHex}, + key::constants::SECRET_KEY_SIZE, + Txid, }, silentpayments::{ bitcoin_hashes::{sha256t_hash_newtype, Hash, HashEngine}, + secp256k1::PublicKey, utils::SilentPaymentAddress, - secp256k1::PublicKey }, }; use tsify::Tsify; @@ -41,15 +43,17 @@ pub struct AnkSharedSecret { impl AnkSharedSecret { pub fn new(shared_point: PublicKey) -> Self { - let mut shared_point_bin = [0u8;64]; + let mut shared_point_bin = [0u8; 64]; shared_point_bin.copy_from_slice(&shared_point.serialize_uncompressed()[1..]); let secret = AnkSharedSecretHash::from_shared_point(shared_point_bin).to_byte_array(); - Self { secret: secret.to_lower_hex_string() } + Self { + secret: secret.to_lower_hex_string(), + } } pub fn to_byte_array(&self) -> [u8; 32] { let bytes = Vec::from_hex(&self.secret).unwrap(); - let mut buf = [0u8;32]; + let mut buf = [0u8; 32]; buf.copy_from_slice(&bytes); buf } @@ -113,11 +117,7 @@ pub struct Aes256Decryption { } impl Aes256Decryption { - pub fn new( - purpose: Purpose, - cipher_text: CipherText, - aes_key: [u8;32], - ) -> Result { + pub fn new(purpose: Purpose, cipher_text: CipherText, aes_key: [u8; 32]) -> Result { if cipher_text.len() <= 12 { return Err(Error::msg("cipher_text is shorter than nonce length")); } @@ -254,7 +254,7 @@ impl Aes256Encryption { }) } - pub fn export_key(&self) -> [u8;32] { + pub fn export_key(&self) -> [u8; 32] { self.aes_key } @@ -376,8 +376,7 @@ mod tests { let mut plain_key = [0u8; 32]; plain_key.copy_from_slice(&aes_key.to_vec()); - let aes_dec = - Aes256Decryption::new(Purpose::Login, cipher.unwrap(), plain_key); + let aes_dec = Aes256Decryption::new(Purpose::Login, cipher.unwrap(), plain_key); assert!(aes_dec.is_ok()); } diff --git a/src/error.rs b/src/error.rs index dcb6304..438540d 100644 --- a/src/error.rs +++ b/src/error.rs @@ -1,5 +1,5 @@ -use std::fmt; use std::error::Error; +use std::fmt; use serde::{Deserialize, Serialize}; diff --git a/src/lib.rs b/src/lib.rs index d9d184f..7d693bb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,6 +1,6 @@ pub use sp_client; pub mod crypto; +pub mod error; pub mod network; pub mod silentpayments; -pub mod error; diff --git a/src/network.rs b/src/network.rs index 54f3c94..f1d82b6 100644 --- a/src/network.rs +++ b/src/network.rs @@ -1,9 +1,16 @@ +use std::str::FromStr; + +use aes_gcm::Aes256Gcm; use anyhow::{Error, Result}; use js_sys::Date; use rand::{thread_rng, RngCore}; use serde::{Deserialize, Serialize}; +use sp_client::bitcoin::consensus::serialize; +use sp_client::bitcoin::hashes::Hash; use sp_client::bitcoin::hex::{DisplayHex, FromHex}; -use sp_client::bitcoin::OutPoint; +use sp_client::bitcoin::secp256k1::PublicKey; +use sp_client::bitcoin::{BlockHash, OutPoint, Transaction}; +use sp_client::silentpayments::utils::SilentPaymentAddress; use tsify::Tsify; use crate::crypto::{Aes256Decryption, Purpose}; @@ -65,9 +72,13 @@ pub struct FaucetMessage { impl FaucetMessage { pub fn new(sp_address: String) -> Self { - let mut buf = [0u8;64]; + let mut buf = [0u8; 64]; thread_rng().fill_bytes(&mut buf); - Self { sp_address, commitment: buf.to_lower_hex_string(), error: None } + Self { + sp_address, + commitment: buf.to_lower_hex_string(), + error: None, + } } } @@ -150,7 +161,7 @@ pub enum CachedMessageStatus { Complete, } -/// Unique struct for both 3nk messages and notification/key exchange, both rust and ts +/// Unique struct for both 4nk messages and notification/key exchange, both rust and ts /// 0. Faucet: commited_in with nothing else, status is NoStatus /// 1. notification: /// 0. sender: ciphertext, plaintext, commited_in, sender, recipient, shared_secret, key @@ -184,7 +195,7 @@ pub struct CachedMessage { impl CachedMessage { pub fn new() -> Self { let mut new = Self::default(); - let mut buf = [0u8;4]; + 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; @@ -203,12 +214,11 @@ impl CachedMessage { pub fn try_decrypt_cipher(&self, cipher: Vec) -> Result> { if self.ciphertext.is_some() || self.shared_secret.is_none() { return Err(Error::msg( - "Can't try decrypt this message, there's already a ciphertext or no shared secret" + "Can't try decrypt this message, there's already a ciphertext or no shared secret", )); } let mut shared_secret = [0u8; 32]; - shared_secret - .copy_from_slice(&Vec::from_hex(self.shared_secret.as_ref().unwrap())?); + shared_secret.copy_from_slice(&Vec::from_hex(self.shared_secret.as_ref().unwrap())?); let aes_decrypt = Aes256Decryption::new(Purpose::Arbitrary, cipher, shared_secret)?; aes_decrypt.decrypt_with_key() @@ -217,12 +227,11 @@ impl CachedMessage { pub fn try_decrypt_with_shared_secret(&self, shared_secret: [u8; 32]) -> Result> { if self.ciphertext.is_none() || self.shared_secret.is_some() { return Err(Error::msg( - "Can't try decrypt this message, ciphertext is none or shared_secret already found" + "Can't try decrypt this message, ciphertext is none or shared_secret already found", )); } let cipher_bin = Vec::from_hex(self.ciphertext.as_ref().unwrap())?; - let aes_decrypt = - Aes256Decryption::new(Purpose::Arbitrary, cipher_bin, shared_secret)?; + let aes_decrypt = Aes256Decryption::new(Purpose::Arbitrary, cipher_bin, shared_secret)?; aes_decrypt.decrypt_with_key() }