mostro_client/cli/
add_invoice.rs

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

pub async fn execute_add_invoice(
    order_id: &Uuid,
    invoice: &str,
    identity_keys: &Keys,
    mostro_key: PublicKey,
    client: &Client,
) -> Result<()> {
    let pool = connect().await?;
    let order = Order::get_by_id(&pool, &order_id.to_string())
        .await
        .unwrap();
    let trade_keys = order.trade_keys.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;
    // Check invoice string
    let ln_addr = LightningAddress::from_str(invoice);
    if ln_addr.is_ok() {
        payload = 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),
        }
    }
    // Create AddInvoice message
    let add_invoice_message =
        Message::new_order(Some(*order_id), None, None, Action::AddInvoice, payload)
            .as_json()
            .unwrap();

    send_order_id_cmd(
        client,
        Some(identity_keys),
        &trade_keys,
        mostro_key,
        add_invoice_message,
        true,
        false,
    )
    .await?;

    Ok(())
}