Skip to content

Commit

Permalink
keys managements(KM) implementation on add-invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
grunch committed Dec 18, 2024
1 parent e2f579a commit 637e78c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
41 changes: 30 additions & 11 deletions src/cli/add_invoice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{db::Order, lightning::is_valid_invoice};
use anyhow::Result;
use lnurl::lightning_address::LightningAddress;
use mostro_core::message::{Action, Message, Payload};
use mostro_core::order::Status;
use nostr_sdk::prelude::*;
use std::str::FromStr;
use uuid::Uuid;
Expand All @@ -16,32 +17,38 @@ pub async fn execute_add_invoice(
client: &Client,
) -> Result<()> {
let pool = connect().await?;
let order = Order::get_by_id(&pool, &order_id.to_string())
let mut order = Order::get_by_id(&pool, &order_id.to_string())
.await
.unwrap();
let trade_keys = order.trade_keys.unwrap();
let trade_keys = order.trade_keys.clone().unwrap();
let trade_keys = Keys::parse(trade_keys).unwrap();

println!(
"Sending a lightning invoice {} to mostro pubId {}",
order_id, mostro_key
);
let mut payload = None;
// let mut payload = None;
// Check invoice string
let ln_addr = LightningAddress::from_str(invoice);
if ln_addr.is_ok() {
payload = Some(Payload::PaymentRequest(None, invoice.to_string(), None));
let payload = if ln_addr.is_ok() {
Some(Payload::PaymentRequest(None, invoice.to_string(), None))
} else {
match is_valid_invoice(invoice) {
Ok(i) => payload = Some(Payload::PaymentRequest(None, i.to_string(), None)),
Err(e) => println!("{}", e),
Ok(i) => Some(Payload::PaymentRequest(None, i.to_string(), None)),
Err(_) => None,
}
}
};
let request_id = Uuid::new_v4().as_u128() as u64;
// Create AddInvoice message
let add_invoice_message =
Message::new_order(Some(*order_id), None, None, Action::AddInvoice, payload);
let add_invoice_message = Message::new_order(
Some(*order_id),
Some(request_id),
None,
Action::AddInvoice,
payload,
);

send_message_sync(
let dm = send_message_sync(
client,
Some(identity_keys),
&trade_keys,
Expand All @@ -52,5 +59,17 @@ pub async fn execute_add_invoice(
)
.await?;

dm.iter().for_each(|el| {
let message = el.0.get_inner_message_kind();
if message.request_id == Some(request_id) && message.action == Action::WaitingSellerToPay {
println!("Now we should wait for the seller to pay the invoice");
}
});
order
.set_status(Status::WaitingPayment.to_string())
.save(&pool)
.await
.unwrap();

Ok(())
}
2 changes: 1 addition & 1 deletion src/cli/take_buy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ pub async fn execute_take_buy(

let order = dm.iter().find_map(|el| {
let message = el.0.get_inner_message_kind();
if message.request_id == Some(request_id) {
if message.request_id == Some(request_id) && message.action == Action::PayInvoice {
if let Some(Payload::PaymentRequest(order, invoice, _)) = &message.payload {
println!(
"Mostro sent you this hold invoice for order id: {}",
Expand Down
6 changes: 5 additions & 1 deletion src/cli/take_sell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,12 @@ pub async fn execute_take_sell(

let order = dm.iter().find_map(|el| {
let message = el.0.get_inner_message_kind();
if message.request_id == Some(request_id) {
if message.request_id == Some(request_id) && message.action == Action::AddInvoice {
if let Some(Payload::Order(order)) = message.payload.as_ref() {
println!(
"Please add a lightning invoice with amount of {}",
order.amount
);
return Some(order.clone());
}
}
Expand Down

0 comments on commit 637e78c

Please sign in to comment.