-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathadd_invoice.rs
More file actions
84 lines (76 loc) · 2.57 KB
/
add_invoice.rs
File metadata and controls
84 lines (76 loc) · 2.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::parser::dms::print_commands_results;
use crate::parser::parse_dm_events;
use crate::util::{send_dm, wait_for_dm};
use crate::{cli::Context, db::Order, lightning::is_valid_invoice};
use anyhow::Result;
use lnurl::lightning_address::LightningAddress;
use mostro_core::prelude::*;
use nostr_sdk::prelude::*;
use std::str::FromStr;
use uuid::Uuid;
pub async fn execute_add_invoice(order_id: &Uuid, invoice: &str, ctx: &Context) -> Result<()> {
let order = Order::get_by_id(&ctx.pool, &order_id.to_string()).await?;
let trade_keys = order
.trade_keys
.clone()
.ok_or(anyhow::anyhow!("Missing trade keys"))?;
let order_trade_keys = Keys::parse(&trade_keys)?;
println!(
"Order trade keys: {:?}",
order_trade_keys.public_key().to_hex()
);
println!(
"Sending a lightning invoice for order {} to mostro pubId {}",
order_id, ctx.mostro_pubkey
);
// Check invoice string
let ln_addr = LightningAddress::from_str(invoice);
let payload = if ln_addr.is_ok() {
Some(Payload::PaymentRequest(None, invoice.to_string(), None))
} else {
match is_valid_invoice(invoice) {
Ok(i) => Some(Payload::PaymentRequest(None, i.to_string(), None)),
Err(e) => {
return Err(anyhow::anyhow!("Invalid invoice: {}", e));
}
}
};
// Create request id
let request_id = Uuid::new_v4().as_u128() as u64;
// Create AddInvoice message
let add_invoice_message = Message::new_order(
Some(*order_id),
Some(request_id),
None,
Action::AddInvoice,
payload,
);
// Serialize the message
let message_json = add_invoice_message
.as_json()
.map_err(|_| anyhow::anyhow!("Failed to serialize message"))?;
// Send the DM
send_dm(
&ctx.client,
Some(&ctx.identity_keys),
&order_trade_keys,
&ctx.mostro_pubkey,
message_json,
None,
false,
)
.await?;
// Wait for the DM to be sent from mostro
let recv_event = wait_for_dm(ctx, Some(&order_trade_keys)).await?;
// Parse the incoming DM
let messages = parse_dm_events(recv_event, &order_trade_keys, None).await;
if let Some(message) = messages.first() {
let message = message.0.get_inner_message_kind();
if message.request_id == Some(request_id) {
if let Err(e) = print_commands_results(message, Some(order.clone()), ctx).await {
println!("Error in print_commands_results: {}", e);
}
}
}
Ok(())
}