This commit is contained in:
Sosthene 2024-07-19 22:48:15 +02:00
parent 9087e0a535
commit e1f70cf849
4 changed files with 34 additions and 26 deletions

View File

@ -4,12 +4,14 @@ use anyhow::{Error, Result};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sp_client::{ use sp_client::{
bitcoin::{ bitcoin::{
hex::{DisplayHex, FromHex}, key::constants::SECRET_KEY_SIZE, Txid hex::{DisplayHex, FromHex},
key::constants::SECRET_KEY_SIZE,
Txid,
}, },
silentpayments::{ silentpayments::{
bitcoin_hashes::{sha256t_hash_newtype, Hash, HashEngine}, bitcoin_hashes::{sha256t_hash_newtype, Hash, HashEngine},
secp256k1::PublicKey,
utils::SilentPaymentAddress, utils::SilentPaymentAddress,
secp256k1::PublicKey
}, },
}; };
use tsify::Tsify; use tsify::Tsify;
@ -41,15 +43,17 @@ pub struct AnkSharedSecret {
impl AnkSharedSecret { impl AnkSharedSecret {
pub fn new(shared_point: PublicKey) -> Self { 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..]); shared_point_bin.copy_from_slice(&shared_point.serialize_uncompressed()[1..]);
let secret = AnkSharedSecretHash::from_shared_point(shared_point_bin).to_byte_array(); 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] { pub fn to_byte_array(&self) -> [u8; 32] {
let bytes = Vec::from_hex(&self.secret).unwrap(); let bytes = Vec::from_hex(&self.secret).unwrap();
let mut buf = [0u8;32]; let mut buf = [0u8; 32];
buf.copy_from_slice(&bytes); buf.copy_from_slice(&bytes);
buf buf
} }
@ -113,11 +117,7 @@ pub struct Aes256Decryption {
} }
impl Aes256Decryption { impl Aes256Decryption {
pub fn new( pub fn new(purpose: Purpose, cipher_text: CipherText, aes_key: [u8; 32]) -> Result<Self> {
purpose: Purpose,
cipher_text: CipherText,
aes_key: [u8;32],
) -> Result<Self> {
if cipher_text.len() <= 12 { if cipher_text.len() <= 12 {
return Err(Error::msg("cipher_text is shorter than nonce length")); 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 self.aes_key
} }
@ -376,8 +376,7 @@ mod tests {
let mut plain_key = [0u8; 32]; let mut plain_key = [0u8; 32];
plain_key.copy_from_slice(&aes_key.to_vec()); plain_key.copy_from_slice(&aes_key.to_vec());
let aes_dec = let aes_dec = Aes256Decryption::new(Purpose::Login, cipher.unwrap(), plain_key);
Aes256Decryption::new(Purpose::Login, cipher.unwrap(), plain_key);
assert!(aes_dec.is_ok()); assert!(aes_dec.is_ok());
} }

View File

@ -1,5 +1,5 @@
use std::fmt;
use std::error::Error; use std::error::Error;
use std::fmt;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};

View File

@ -1,6 +1,6 @@
pub use sp_client; pub use sp_client;
pub mod crypto; pub mod crypto;
pub mod error;
pub mod network; pub mod network;
pub mod silentpayments; pub mod silentpayments;
pub mod error;

View File

@ -1,9 +1,16 @@
use std::str::FromStr;
use aes_gcm::Aes256Gcm;
use anyhow::{Error, Result}; use anyhow::{Error, Result};
use js_sys::Date; use js_sys::Date;
use rand::{thread_rng, RngCore}; use rand::{thread_rng, RngCore};
use serde::{Deserialize, Serialize}; 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::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 tsify::Tsify;
use crate::crypto::{Aes256Decryption, Purpose}; use crate::crypto::{Aes256Decryption, Purpose};
@ -65,9 +72,13 @@ pub struct FaucetMessage {
impl FaucetMessage { impl FaucetMessage {
pub fn new(sp_address: String) -> Self { pub fn new(sp_address: String) -> Self {
let mut buf = [0u8;64]; let mut buf = [0u8; 64];
thread_rng().fill_bytes(&mut buf); 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, 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 /// 0. Faucet: commited_in with nothing else, status is NoStatus
/// 1. notification: /// 1. notification:
/// 0. sender: ciphertext, plaintext, commited_in, sender, recipient, shared_secret, key /// 0. sender: ciphertext, plaintext, commited_in, sender, recipient, shared_secret, key
@ -184,7 +195,7 @@ pub struct CachedMessage {
impl CachedMessage { impl CachedMessage {
pub fn new() -> Self { pub fn new() -> Self {
let mut new = Self::default(); let mut new = Self::default();
let mut buf = [0u8;4]; let mut buf = [0u8; 4];
thread_rng().fill_bytes(&mut buf); thread_rng().fill_bytes(&mut buf);
new.id = u32::from_be_bytes(buf); new.id = u32::from_be_bytes(buf);
new.timestamp = Date::now().floor() as u64; new.timestamp = Date::now().floor() as u64;
@ -203,12 +214,11 @@ impl CachedMessage {
pub fn try_decrypt_cipher(&self, cipher: Vec<u8>) -> Result<Vec<u8>> { pub fn try_decrypt_cipher(&self, cipher: Vec<u8>) -> Result<Vec<u8>> {
if self.ciphertext.is_some() || self.shared_secret.is_none() { if self.ciphertext.is_some() || self.shared_secret.is_none() {
return Err(Error::msg( 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]; let mut shared_secret = [0u8; 32];
shared_secret shared_secret.copy_from_slice(&Vec::from_hex(self.shared_secret.as_ref().unwrap())?);
.copy_from_slice(&Vec::from_hex(self.shared_secret.as_ref().unwrap())?);
let aes_decrypt = Aes256Decryption::new(Purpose::Arbitrary, cipher, shared_secret)?; let aes_decrypt = Aes256Decryption::new(Purpose::Arbitrary, cipher, shared_secret)?;
aes_decrypt.decrypt_with_key() aes_decrypt.decrypt_with_key()
@ -217,12 +227,11 @@ impl CachedMessage {
pub fn try_decrypt_with_shared_secret(&self, shared_secret: [u8; 32]) -> Result<Vec<u8>> { pub fn try_decrypt_with_shared_secret(&self, shared_secret: [u8; 32]) -> Result<Vec<u8>> {
if self.ciphertext.is_none() || self.shared_secret.is_some() { if self.ciphertext.is_none() || self.shared_secret.is_some() {
return Err(Error::msg( 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 cipher_bin = Vec::from_hex(self.ciphertext.as_ref().unwrap())?;
let aes_decrypt = let aes_decrypt = Aes256Decryption::new(Purpose::Arbitrary, cipher_bin, shared_secret)?;
Aes256Decryption::new(Purpose::Arbitrary, cipher_bin, shared_secret)?;
aes_decrypt.decrypt_with_key() aes_decrypt.decrypt_with_key()
} }