Skip to content

Commit 6f03e6e

Browse files
authored
Merge pull request #99 from MostroP2P/KM-on-add-invoice
keys managements(KM) implementation on add-invoice
2 parents e2f579a + 7bd6a44 commit 6f03e6e

File tree

3 files changed

+42
-13
lines changed

3 files changed

+42
-13
lines changed

src/cli/add_invoice.rs

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use crate::{db::Order, lightning::is_valid_invoice};
44
use anyhow::Result;
55
use lnurl::lightning_address::LightningAddress;
66
use mostro_core::message::{Action, Message, Payload};
7+
use mostro_core::order::Status;
78
use nostr_sdk::prelude::*;
89
use std::str::FromStr;
910
use uuid::Uuid;
@@ -16,32 +17,41 @@ pub async fn execute_add_invoice(
1617
client: &Client,
1718
) -> Result<()> {
1819
let pool = connect().await?;
19-
let order = Order::get_by_id(&pool, &order_id.to_string())
20+
let mut order = Order::get_by_id(&pool, &order_id.to_string())
2021
.await
2122
.unwrap();
22-
let trade_keys = order.trade_keys.unwrap();
23+
let trade_keys = order.trade_keys.clone().unwrap();
2324
let trade_keys = Keys::parse(trade_keys).unwrap();
2425

2526
println!(
2627
"Sending a lightning invoice {} to mostro pubId {}",
2728
order_id, mostro_key
2829
);
29-
let mut payload = None;
30+
// let mut payload = None;
3031
// Check invoice string
3132
let ln_addr = LightningAddress::from_str(invoice);
32-
if ln_addr.is_ok() {
33-
payload = Some(Payload::PaymentRequest(None, invoice.to_string(), None));
33+
let payload = if ln_addr.is_ok() {
34+
Some(Payload::PaymentRequest(None, invoice.to_string(), None))
3435
} else {
3536
match is_valid_invoice(invoice) {
36-
Ok(i) => payload = Some(Payload::PaymentRequest(None, i.to_string(), None)),
37-
Err(e) => println!("{}", e),
37+
Ok(i) => Some(Payload::PaymentRequest(None, i.to_string(), None)),
38+
Err(e) => {
39+
println!("Invalid invoice: {}", e);
40+
None
41+
}
3842
}
39-
}
43+
};
44+
let request_id = Uuid::new_v4().as_u128() as u64;
4045
// Create AddInvoice message
41-
let add_invoice_message =
42-
Message::new_order(Some(*order_id), None, None, Action::AddInvoice, payload);
46+
let add_invoice_message = Message::new_order(
47+
Some(*order_id),
48+
Some(request_id),
49+
None,
50+
Action::AddInvoice,
51+
payload,
52+
);
4353

44-
send_message_sync(
54+
let dm = send_message_sync(
4555
client,
4656
Some(identity_keys),
4757
&trade_keys,
@@ -52,5 +62,20 @@ pub async fn execute_add_invoice(
5262
)
5363
.await?;
5464

65+
dm.iter().for_each(|el| {
66+
let message = el.0.get_inner_message_kind();
67+
if message.request_id == Some(request_id) && message.action == Action::WaitingSellerToPay {
68+
println!("Now we should wait for the seller to pay the invoice");
69+
}
70+
});
71+
match order
72+
.set_status(Status::WaitingPayment.to_string())
73+
.save(&pool)
74+
.await
75+
{
76+
Ok(_) => println!("Order status updated"),
77+
Err(e) => println!("Failed to update order status: {}", e),
78+
}
79+
5580
Ok(())
5681
}

src/cli/take_buy.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ pub async fn execute_take_buy(
4848

4949
let order = dm.iter().find_map(|el| {
5050
let message = el.0.get_inner_message_kind();
51-
if message.request_id == Some(request_id) {
51+
if message.request_id == Some(request_id) && message.action == Action::PayInvoice {
5252
if let Some(Payload::PaymentRequest(order, invoice, _)) = &message.payload {
5353
println!(
5454
"Mostro sent you this hold invoice for order id: {}",

src/cli/take_sell.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,8 +73,12 @@ pub async fn execute_take_sell(
7373

7474
let order = dm.iter().find_map(|el| {
7575
let message = el.0.get_inner_message_kind();
76-
if message.request_id == Some(request_id) {
76+
if message.request_id == Some(request_id) && message.action == Action::AddInvoice {
7777
if let Some(Payload::Order(order)) = message.payload.as_ref() {
78+
println!(
79+
"Please add a lightning invoice with amount of {}",
80+
order.amount
81+
);
7882
return Some(order.clone());
7983
}
8084
}

0 commit comments

Comments
 (0)