get the nmpk from lmdb on contact share

This commit is contained in:
creating2morrow 2024-04-09 23:57:14 -04:00
parent 42532e00eb
commit 3b6e34119d
3 changed files with 44 additions and 20 deletions

View file

@ -1,10 +1,10 @@
// Contact repo/service layer //! contact operations module
use crate::{ use crate::{
db, db,
i2p, i2p,
models::*, models::*,
monero, monero,
neveko25519,
reqres, reqres,
utils, utils,
}; };
@ -128,8 +128,7 @@ pub async fn share() -> Contact {
monero::open_wallet(&wallet_name, &wallet_password).await; monero::open_wallet(&wallet_name, &wallet_password).await;
let m_address: reqres::XmrRpcAddressResponse = monero::get_address().await; let m_address: reqres::XmrRpcAddressResponse = monero::get_address().await;
monero::close_wallet(&wallet_name, &wallet_password).await; monero::close_wallet(&wallet_name, &wallet_password).await;
let nmk = neveko25519::generate_neveko_message_keys().await; let nmpk = utils::get_nmpk();
let nmpk = nmk.hex_nmpk;
let i2p_address = i2p::get_destination(None); let i2p_address = i2p::get_destination(None);
let xmr_address = m_address.result.address; let xmr_address = m_address.result.address;
Contact { Contact {

View file

@ -1,3 +1,5 @@
//! NEVEKO modified ed25519 library extending curve25519-dalek
use curve25519_dalek::{ use curve25519_dalek::{
edwards::{ edwards::{
CompressedEdwardsY, CompressedEdwardsY,
@ -69,7 +71,14 @@ fn hash_to_scalar(s: Vec<&str>) -> Scalar {
hasher.update(&result); hasher.update(&result);
let hash = hasher.finalize().to_owned(); let hash = hasher.finalize().to_owned();
let mut hash_container: [u8; 32] = [0u8; 32]; let mut hash_container: [u8; 32] = [0u8; 32];
hex::decode_to_slice(result, &mut hash_container as &mut [u8]).unwrap_or_default(); let mut index = 0;
for byte in result.as_bytes() {
if index == hash_container.len() - 1 {
break;
}
hash_container[index] = *byte;
index += 1;
}
let hash_value = BigInt::from_bytes_le(Sign::Plus, &hash_container); let hash_value = BigInt::from_bytes_le(Sign::Plus, &hash_container);
if hash_value < curve_l_as_big_int() { if hash_value < curve_l_as_big_int() {
return Scalar::from_bytes_mod_order(hash_container); return Scalar::from_bytes_mod_order(hash_container);
@ -98,6 +107,7 @@ pub async fn generate_neveko_message_keys() -> NevekoMessageKeys {
monero::close_wallet(&filename, &password).await; monero::close_wallet(&filename, &password).await;
let svk = svk_res.result.key; let svk = svk_res.result.key;
let scalar_nmsk = hash_to_scalar(vec![&svk[..], crate::APP_NAME]); let scalar_nmsk = hash_to_scalar(vec![&svk[..], crate::APP_NAME]);
log::debug!("scalar_nmsk: {:?}", &scalar_nmsk);
let point_nmpk = EdwardsPoint::mul_base(&scalar_nmsk); let point_nmpk = EdwardsPoint::mul_base(&scalar_nmsk);
let nmsk = *scalar_nmsk.as_bytes(); let nmsk = *scalar_nmsk.as_bytes();
let nmpk: [u8; 32] = *point_nmpk.compress().as_bytes(); let nmpk: [u8; 32] = *point_nmpk.compress().as_bytes();
@ -173,13 +183,12 @@ mod tests {
hex_nmpk, hex_nmpk,
hex_nmsk, hex_nmsk,
}; };
// shared secret = pvk * svk // shared secret = nmpk * nmks
let scalar_svk = Scalar::from_bytes_mod_order(keys.nmsk); let scalar_nmsk = Scalar::from_bytes_mod_order(keys.nmsk);
let compress_y = CompressedEdwardsY::from_slice(&nmpk).unwrap_or_default(); let compress_y = CompressedEdwardsY::from_slice(&nmpk).unwrap_or_default();
let pvk = compress_y.decompress().unwrap_or_default(); let nmpk_compress = compress_y.decompress().unwrap_or_default();
let shared_secret = pvk * scalar_svk; let shared_secret = nmpk_compress * scalar_nmsk;
let ss_hex = hex::encode(shared_secret.compress().as_bytes()); let ss_hex = hex::encode(shared_secret.compress().as_bytes());
log::debug!("shared_secret: {:?}", ss_hex);
// x = m + h or x = m - h' // x = m + h or x = m - h'
let h = hash_to_scalar(vec![&ss_hex[..]]); let h = hash_to_scalar(vec![&ss_hex[..]]);
let h_bi = BigInt::from_bytes_le(Sign::Plus, h.as_bytes()); let h_bi = BigInt::from_bytes_le(Sign::Plus, h.as_bytes());

View file

@ -1,14 +1,7 @@
//! Generic functions for startup and convenience
use crate::{ use crate::{
args, args, contact, db, dispute, i2p, message, models, monero, neveko25519, reqres, utils
contact,
db,
dispute,
i2p,
message,
models,
monero,
reqres,
utils,
}; };
use clap::Parser; use clap::Parser;
use log::{ use log::{
@ -460,6 +453,28 @@ pub fn get_jwp_secret_key() -> String {
r r
} }
/// Returns the hex encoded neveko message public key from LMDB
pub fn get_nmpk() -> String {
let s = db::Interface::open();
let r = db::Interface::read(&s.env, &s.handle, crate::NEVEKO_NMPK);
if r == utils::empty_string() {
error!("neveko message public key not found");
return Default::default();
}
r
}
async fn generate_nmpk() {
info!("generating neveko message public key");
let nmpk: String = get_nmpk();
// send to db
let s = db::Interface::open();
if nmpk == utils::empty_string() {
let nmk: neveko25519::NevekoMessageKeys = neveko25519::generate_neveko_message_keys().await;
db::Interface::write(&s.env, &s.handle, crate::NEVEKO_NMPK, &nmk.hex_nmpk);
}
}
/// Put all app pre-checks here /// Put all app pre-checks here
pub async fn start_up() { pub async fn start_up() {
info!("neveko is starting up"); info!("neveko is starting up");
@ -492,6 +507,7 @@ pub async fn start_up() {
wallet_password = read_password().unwrap(); wallet_password = read_password().unwrap();
std::env::set_var(crate::MONERO_WALLET_PASSWORD, &wallet_password); std::env::set_var(crate::MONERO_WALLET_PASSWORD, &wallet_password);
} }
generate_nmpk().await;
let env: String = get_release_env().value(); let env: String = get_release_env().value();
if !args.i2p_advanced { if !args.i2p_advanced {
i2p::start().await; i2p::start().await;