Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

keys managements(KM) implementation on add-invoice #99

Merged
merged 4 commits into from
Dec 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 36 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,41 @@ 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(e) => {
println!("Invalid invoice: {}", e);
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 +62,20 @@ 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");
}
});
match order
.set_status(Status::WaitingPayment.to_string())
.save(&pool)
.await
{
Ok(_) => println!("Order status updated"),
Err(e) => println!("Failed to update order status: {}", e),
}

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
Loading