Skip to content

Commit

Permalink
cli: Add cli command to pay offer
Browse files Browse the repository at this point in the history
  • Loading branch information
orbitalturtle committed Feb 8, 2024
1 parent ca31636 commit 59b479f
Showing 1 changed file with 86 additions and 3 deletions.
89 changes: 86 additions & 3 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
use clap::{Parser, Subcommand};
use lndk::lnd::{get_lnd_client, string_to_network, LndCfg};
use lndk::lndk_offers::decode;
use lndk::{Cfg, LifecycleSignals, LndkOnionMessenger, OfferHandler, PayOfferParams};
use std::ffi::OsString;
use tokio::select;
use tokio::sync::mpsc;
use tokio::sync::mpsc::{Receiver, Sender};

fn get_cert_path_default() -> OsString {
home::home_dir()
Expand Down Expand Up @@ -60,23 +65,101 @@ enum Commands {
/// The offer string to decode.
offer_string: String,
},
/// PayOffer pays a BOLT 12 offer, provided as a 'lno'-prefaced offer string.
PayOffer {
/// The offer string.
offer_string: String,

/// Amount the user would like to pay. If this isn't set, we'll assume the user is paying
/// whatever the offer amount is.
#[arg(required = false)]
amount: Option<u64>,
},
}

fn main() {
#[tokio::main]
async fn main() -> Result<(), ()> {
let args = Cli::parse();
match args.command {
Commands::Decode { offer_string } => {
println!("Decoding offer: {offer_string}.");
match decode(offer_string) {
Ok(offer) => println!("Decoded offer: {:?}.", offer),
Ok(offer) => {
println!("Decoded offer: {:?}.", offer);
Ok(())
}
Err(e) => {
println!(
"ERROR please provide offer starting with lno. Provided offer is \
invalid, failed to decode with error: {:?}.",
e
)
);
Err(())
}
}
}
Commands::PayOffer {
offer_string,
amount,
} => {
let offer = match decode(offer_string) {
Ok(offer) => offer,
Err(e) => {
println!(
"ERROR: please provide offer starting with lno. Provided offer is \
invalid, failed to decode with error: {:?}.",
e
);
return Err(());
}
};

let network = string_to_network(&args.network).map_err(|e| {
println!("ERROR: invalid network string: {}", e);
})?;
let lnd_cfg = LndCfg::new(args.address, args.tls_cert.into(), args.macaroon.into());
let client = get_lnd_client(lnd_cfg.clone()).map_err(|e| {
println!("ERROR: failed to connect to lnd: {}", e);
})?;

let (shutdown, listener) = triggered::trigger();
let (tx, rx): (Sender<u32>, Receiver<u32>) = mpsc::channel(1);
let signals = LifecycleSignals {
shutdown: shutdown.clone(),
listener,
started: tx,
};
let lndk_cfg = Cfg {
lnd: lnd_cfg,
log_dir: None,
signals,
};

let handler = OfferHandler::new();
let messenger = LndkOnionMessenger::new(handler);
let pay_cfg = PayOfferParams {
offer: offer.clone(),
amount,
network,
client,
blinded_path: offer.paths()[0].clone(),
reply_path: None,
};
select! {
_ = messenger.run(lndk_cfg) => {
println!("ERROR: lndk stopped running before pay offer finished.");
},
res = messenger.offer_handler.pay_offer(pay_cfg, rx) => {
match res {
Ok(_) => println!("Successfully paid for offer!"),
Err(_) => println!("Error paying for offer."),
}

shutdown.trigger();
}
}

Ok(())
}
}
}

0 comments on commit 59b479f

Please sign in to comment.