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
use crate::lightning::is_valid_invoice;
use crate::util::{get_keys, send_order_id_cmd};
use anyhow::Result;
use lnurl::lightning_address::LightningAddress;
use mostro_core::message::{Action, Content, Message};
use nostr_sdk::prelude::*;
use std::str::FromStr;
use uuid::Uuid;

pub async fn execute_add_invoice(
    order_id: &Uuid,
    invoice: &str,
    my_key: &Keys,
    mostro_key: PublicKey,
    client: &Client,
) -> Result<()> {
    println!(
        "Sending a lightning invoice {} to mostro pubId {}",
        order_id, mostro_key
    );
    let mut content = None;
    // Check invoice string
    let ln_addr = LightningAddress::from_str(invoice);
    if ln_addr.is_ok() {
        content = Some(Content::PaymentRequest(None, invoice.to_string()));
    } else {
        match is_valid_invoice(invoice) {
            Ok(i) => content = Some(Content::PaymentRequest(None, i.to_string())),
            Err(e) => println!("{}", e),
        }
    }

    let keys = get_keys()?;
    // This should be the master pubkey
    let master_pubkey = keys.public_key().to_string();
    // Create AddInvoice message
    let add_invoice_message = Message::new_order(
        Some(*order_id),
        Some(master_pubkey),
        Action::AddInvoice,
        content,
    )
    .as_json()
    .unwrap();

    send_order_id_cmd(client, my_key, mostro_key, add_invoice_message, true).await?;

    Ok(())
}