From 660aa3c9bd62fed42637e79a12aef69443e4563d Mon Sep 17 00:00:00 2001 From: creating2morrow Date: Thu, 1 Jun 2023 11:20:13 -0400 Subject: [PATCH] add exchange and export multisig info orchestration --- nevmes-core/src/message.rs | 53 +++++++++++++++++++++++++++++++++++++- src/controller.rs | 13 ++++++---- src/main.rs | 1 + 3 files changed, 61 insertions(+), 6 deletions(-) diff --git a/nevmes-core/src/message.rs b/nevmes-core/src/message.rs index 2f2b027..93bc1da 100644 --- a/nevmes-core/src/message.rs +++ b/nevmes-core/src/message.rs @@ -136,6 +136,7 @@ fn parse_multisig_message(mid: String) -> MultisigMessageData { /// /// ```rust /// // lookup prepare info for vendor +/// use nevmes_core::db; /// let s = db::Interface::open(); /// let key = "prepare-o123-test.b32.i2p"; /// db::Interface::read(&s.env, &s.handle, &key); @@ -168,7 +169,7 @@ pub async fn rx_multisig(m: Json) { debug!("writing multisig message type {} for order {}", &data.sub_type, &data.orid); // lookup msig message data by {type}-{order id}-{contact .b32.i2p address} let msig_key = format!("{}-{}-{}", &data.sub_type, &data.orid, &m.from); - db::Interface::write(&s.env, &s.handle, &msig_key, &data.info); + db::Interface::async_write(&s.env, &s.handle, &msig_key, &data.info).await; } /// Message lookup @@ -430,6 +431,9 @@ fn is_fts_clear(r: String) -> bool { v.len() >= 2 && v[v.len() - 1] == utils::empty_string() && v[0] == utils::empty_string() } +/// Encrypts and sends the output from the monero-rpc +/// +/// `prepare_multisig_info` method. pub async fn send_prepare_info(orid: &String, contact: &String) { let s = db::Interface::open(); let prepare_info = monero::prepare_wallet().await; @@ -448,6 +452,9 @@ pub async fn send_prepare_info(orid: &String, contact: &String) { create(j_message, jwp, MessageType::Multisig).await; } +/// Encrypts and sends the output from the monero-rpc +/// +/// `make_multisig_info` method. pub async fn send_make_info(orid: &String, contact: &String, info: Vec) { let s = db::Interface::open(); let make_info = monero::make_wallet(info).await; @@ -466,6 +473,50 @@ pub async fn send_make_info(orid: &String, contact: &String, info: Vec) create(j_message, jwp, MessageType::Multisig).await; } +/// Encrypts and sends the output from the monero-rpc +/// +/// `exchange_multisig_keys` method. +pub async fn send_exchange_info(orid: &String, contact: &String, info: Vec) { + let s = db::Interface::open(); + let wallet_password = + std::env::var(crate::MONERO_WALLET_PASSWORD).unwrap_or(String::from("password")); + let exchange_info = monero::exchange_multisig_keys(false, info, wallet_password).await; + let k = format!("{}-{}", "fts-jwp", contact); + let jwp = db::Interface::read(&s.env, &s.handle, &k); + let body_str = format!("{}:{}:{}", EXCHANGE_MSIG, orid, &exchange_info.result.multisig_info); + let message: Message = Message { + mid: utils::empty_string(), + uid: utils::empty_string(), + body: body_str.into_bytes(), + created: chrono::Utc::now().timestamp(), + from: utils::empty_string(), + to: String::from(contact), + }; + let j_message: Json = utils::message_to_json(&message); + create(j_message, jwp, MessageType::Multisig).await; +} + +/// Encrypts and sends the output from the monero-rpc +/// +/// `export_multisig_info` method. +pub async fn send_export_info(orid: &String, contact: &String) { + let s = db::Interface::open(); + let exchange_info = monero::export_multisig_info().await; + let k = format!("{}-{}", "fts-jwp", contact); + let jwp = db::Interface::read(&s.env, &s.handle, &k); + let body_str = format!("{}:{}:{}", EXPORT_MSIG, orid, &exchange_info.result.info); + let message: Message = Message { + mid: utils::empty_string(), + uid: utils::empty_string(), + body: body_str.into_bytes(), + created: chrono::Utc::now().timestamp(), + from: utils::empty_string(), + to: String::from(contact), + }; + let j_message: Json = utils::message_to_json(&message); + create(j_message, jwp, MessageType::Multisig).await; +} + // Tests //------------------------------------------------------------------------------- diff --git a/src/controller.rs b/src/controller.rs index 89ddef3..ce590c8 100644 --- a/src/controller.rs +++ b/src/controller.rs @@ -120,17 +120,20 @@ pub async fn retrieve_order( /// Send multisig info for contact's order /// /// Protected: true -#[post("/multisig/info", data = "")] +#[post("/", data = "")] pub async fn get_multisig_info( r_info: Json, _jwp: proof::PaymentProof) -> Custom> { + let info: Vec = r_info.info.iter().cloned().collect(); if r_info.msig_type == String::from(message::PREPARE_MSIG) { - message::send_prepare_info(&r_info.orid, &r_info.contact ).await; - } else { - let info: Vec = r_info.info.iter().cloned().collect(); + message::send_prepare_info(&r_info.orid, &r_info.contact).await; + } else if r_info.msig_type == String::from(message::MAKE_MSIG) { message::send_make_info(&r_info.orid, &r_info.contact, info).await; + } else if r_info.msig_type == String::from(message::EXPORT_MSIG) { + message::send_export_info(&r_info.orid, &r_info.contact).await; + } else { + message::send_exchange_info(&r_info.orid, &r_info.contact, info).await; } - Custom(Status::Ok, Json(Default::default())) } diff --git a/src/main.rs b/src/main.rs index 6fb5825..61becc7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -52,6 +52,7 @@ async fn rocket() -> _ { utils::start_up().await; rocket::custom(&config) .register("/", catchers![internal_error, not_found, payment_required]) + .mount("/multisig/info", routes![controller::get_multisig_info]) .mount("/invoice", routes![controller::gen_invoice]) .mount("/message/rx", routes![controller::rx_message]) .mount("/message/rx/multisig", routes![controller::rx_multisig_message])