Skip to content

Commit

Permalink
Implement LSPS2 invoice creation flow
Browse files Browse the repository at this point in the history
  • Loading branch information
tnull committed Jan 9, 2024
1 parent 3cc3c79 commit 6672ba2
Show file tree
Hide file tree
Showing 5 changed files with 201 additions and 4 deletions.
1 change: 1 addition & 0 deletions bindings/ldk_node.udl
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ enum NodeError {
"MessageSigningFailed",
"TxSyncFailed",
"GossipUpdateFailed",
"LiquidityRequestFailed",
"InvalidAddress",
"InvalidSocketAddress",
"InvalidPublicKey",
Expand Down
3 changes: 3 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ pub enum Error {
TxSyncFailed,
/// A gossip updating operation failed.
GossipUpdateFailed,
/// A liquidity request operation failed.
LiquidityRequestFailed,
/// The given address is invalid.
InvalidAddress,
/// The given network address is invalid.
Expand Down Expand Up @@ -91,6 +93,7 @@ impl fmt::Display for Error {
Self::MessageSigningFailed => write!(f, "Failed to sign given message."),
Self::TxSyncFailed => write!(f, "Failed to sync transactions."),
Self::GossipUpdateFailed => write!(f, "Failed to update gossip data."),
Self::LiquidityRequestFailed => write!(f, "Failed to request inbound liquidity."),
Self::InvalidAddress => write!(f, "The given address is invalid."),
Self::InvalidSocketAddress => write!(f, "The given network address is invalid."),
Self::InvalidPublicKey => write!(f, "The given public key is invalid."),
Expand Down
170 changes: 167 additions & 3 deletions src/liquidity.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
use crate::logger::{log_error, Logger};
use crate::logger::{log_debug, log_error, log_info, Logger};
use crate::types::{ChannelManager, KeysManager, LiquidityManager, PeerManager};
use crate::Config;
use crate::{Config, Error};

use lightning::ln::channelmanager::MIN_FINAL_CLTV_EXPIRY_DELTA;
use lightning::ln::features::{InitFeatures, NodeFeatures};
use lightning::ln::msgs::SocketAddress;
use lightning::ln::peer_handler::CustomMessageHandler;
use lightning::ln::wire::CustomMessageReader;
use lightning::routing::router::{RouteHint, RouteHintHop};
use lightning::util::persist::KVStore;
use lightning_invoice::{Bolt11Invoice, InvoiceBuilder, RoutingFees};
use lightning_liquidity::events::Event;
use lightning_liquidity::lsps0::msgs::RawLSPSMessage;
use lightning_liquidity::lsps2::event::LSPS2ClientEvent;
use lightning_liquidity::lsps2::msgs::OpeningFeeParams;
use lightning_liquidity::lsps2::utils::compute_opening_fee;

use bitcoin::secp256k1::PublicKey;
use bitcoin::hashes::{sha256, Hash};
use bitcoin::secp256k1::{PublicKey, Secp256k1};

use tokio::sync::oneshot;

use rand::Rng;

use std::collections::HashMap;
use std::ops::Deref;
use std::sync::{Arc, Mutex};
use std::time::Duration;

pub(crate) enum LiquiditySource<K: KVStore + Sync + Send + 'static, L: Deref>
where
Expand Down Expand Up @@ -191,6 +199,162 @@ where
},
}
}

pub(crate) async fn lsps2_receive_to_jit_channel(
&self, amount_msat: Option<u64>, description: &str, expiry_secs: u32,
) -> Result<Bolt11Invoice, Error> {
match self {
Self::None => Err(Error::LiquiditySourceUnavailable),
Self::LSPS2 {
node_id,
token,
pending_fee_requests,
pending_buy_requests,
channel_manager,
keys_manager,
liquidity_manager,
config,
logger,
..
} => {
let user_channel_id: u128 = rand::thread_rng().gen::<u128>();

let (fee_request_sender, fee_request_receiver) = oneshot::channel();
pending_fee_requests.lock().unwrap().insert(user_channel_id, fee_request_sender);

let client_handler = liquidity_manager.lsps2_client_handler().ok_or_else(|| {
log_error!(logger, "Liquidity client was not configured.",);
Error::LiquiditySourceUnavailable
})?;

client_handler.create_invoice(
*node_id,
amount_msat,
token.clone(),
user_channel_id,
);

let fee_response = fee_request_receiver.await.map_err(|e| {
log_error!(logger, "Failed to handle response from liquidity service: {:?}", e);
Error::LiquidityRequestFailed
})?;
debug_assert_eq!(fee_response.user_channel_id, user_channel_id);

if let Some(amount_msat) = amount_msat {
if amount_msat < fee_response.min_payment_size_msat
|| amount_msat > fee_response.max_payment_size_msat
{
log_error!(logger, "Failed to request inbound JIT channel as the payment of {}msat doesn't meet LSP limits (min: {}msat, max: {}msat)", amount_msat, fee_response.min_payment_size_msat, fee_response.max_payment_size_msat);
return Err(Error::LiquidityRequestFailed);
}
}

// If it's variable amount, we pick the cheapest opening fee with a dummy value.
let fee_computation_amount = amount_msat.unwrap_or(1_000_000);
let (min_opening_fee_msat, min_opening_params) = fee_response
.opening_fee_params_menu
.iter()
.flat_map(|params| {
if let Some(fee) = compute_opening_fee(
fee_computation_amount,
params.min_fee_msat,
params.proportional as u64,
) {
Some((fee, params))
} else {
None
}
})
.min_by_key(|p| p.0)
.ok_or_else(|| {
log_error!(logger, "Failed to handle response from liquidity service",);
Error::LiquidityRequestFailed
})?;

log_debug!(
logger,
"Choosing cheapest liquidity offer, will pay {}msat in LSP fees",
min_opening_fee_msat
);

let (buy_request_sender, buy_request_receiver) = oneshot::channel();
pending_buy_requests.lock().unwrap().insert(user_channel_id, buy_request_sender);

client_handler
.opening_fee_params_selected(
*node_id,
fee_response.jit_channel_id,
min_opening_params.clone(),
)
.map_err(|e| {
log_error!(
logger,
"Failed to send buy request to liquidity service: {:?}",
e
);
Error::LiquidityRequestFailed
})?;

let buy_response = buy_request_receiver.await.map_err(|e| {
log_error!(logger, "Failed to handle response from liquidity service: {:?}", e);
Error::LiquidityRequestFailed
})?;
debug_assert_eq!(buy_response.user_channel_id, user_channel_id);

// TODO: register in payment store
// LSPS2 requires min_final_cltv_expiry_delta to be at least 2 more than usual.
let min_final_cltv_expiry_delta = MIN_FINAL_CLTV_EXPIRY_DELTA + 2;
let (payment_hash, payment_secret) = channel_manager
.create_inbound_payment(None, expiry_secs, Some(min_final_cltv_expiry_delta))
.map_err(|e| {
log_error!(logger, "Failed to register inbound payment: {:?}", e);
Error::InvoiceCreationFailed
})?;

let route_hint = RouteHint(vec![RouteHintHop {
src_node_id: *node_id,
short_channel_id: buy_response.intercept_scid,
fees: RoutingFees { base_msat: 0, proportional_millionths: 0 },
cltv_expiry_delta: buy_response.cltv_expiry_delta as u16,
htlc_minimum_msat: None,
htlc_maximum_msat: None,
}]);

let payment_hash = sha256::Hash::from_slice(&payment_hash.0).map_err(|e| {
log_error!(logger, "Invalid payment hash: {:?}", e);
Error::InvoiceCreationFailed
})?;

let currency = config.network.into();
let mut invoice_builder = InvoiceBuilder::new(currency)
.description(description.to_string())
.payment_hash(payment_hash)
.payment_secret(payment_secret)
.current_timestamp()
.min_final_cltv_expiry_delta(min_final_cltv_expiry_delta.into())
.expiry_time(Duration::from_secs(expiry_secs.into()))
.private_route(route_hint);

if let Some(amount_msat) = amount_msat {
invoice_builder =
invoice_builder.amount_milli_satoshis(amount_msat).basic_mpp();
}

let invoice = invoice_builder
.build_signed(|hash| {
Secp256k1::new()
.sign_ecdsa_recoverable(hash, &keys_manager.get_node_secret_key())
})
.map_err(|e| {
log_error!(logger, "Failed to build and sign invoice: {}", e);
Error::InvoiceCreationFailed
})?;

log_info!(logger, "JIT-channel invoice created: {}", invoice);
Ok(invoice)
}
}
}
}

impl<K: KVStore + Sync + Send + 'static, L: Deref> CustomMessageReader for LiquiditySource<K, L>
Expand Down
25 changes: 25 additions & 0 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,31 @@ impl From<Network> for bitcoin::Network {
}
}

impl TryFrom<lightning_invoice::Currency> for Network {
type Error = ();

fn try_from(network: lightning_invoice::Currency) -> Result<Self, Self::Error> {
match network {
lightning_invoice::Currency::Bitcoin => Ok(Self::Bitcoin),
lightning_invoice::Currency::BitcoinTestnet => Ok(Self::Testnet),
lightning_invoice::Currency::Signet => Ok(Self::Signet),
lightning_invoice::Currency::Regtest => Ok(Self::Regtest),
_ => Err(()),
}
}
}

impl From<Network> for lightning_invoice::Currency {
fn from(network: Network) -> Self {
match network {
Network::Bitcoin => lightning_invoice::Currency::Bitcoin,
Network::Testnet => lightning_invoice::Currency::BitcoinTestnet,
Network::Signet => lightning_invoice::Currency::Signet,
Network::Regtest => lightning_invoice::Currency::Regtest,
}
}
}

impl fmt::Display for Network {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
bitcoin::Network::from(*self).fmt(f)
Expand Down
6 changes: 5 additions & 1 deletion src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use bitcoin::bech32::u5;
use bitcoin::blockdata::locktime::absolute::LockTime;
use bitcoin::secp256k1::ecdh::SharedSecret;
use bitcoin::secp256k1::ecdsa::{RecoverableSignature, Signature};
use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, Signing};
use bitcoin::secp256k1::{PublicKey, Scalar, Secp256k1, SecretKey, Signing};
use bitcoin::{ScriptBuf, Transaction, TxOut, Txid};

use std::ops::Deref;
Expand Down Expand Up @@ -299,6 +299,10 @@ where
.or(Err(Error::MessageSigningFailed))
}

pub fn get_node_secret_key(&self) -> SecretKey {
self.inner.get_node_secret_key()
}

pub fn verify_signature(&self, msg: &[u8], sig: &str, pkey: &PublicKey) -> bool {
message_signing::verify(msg, sig, pkey)
}
Expand Down

0 comments on commit 6672ba2

Please sign in to comment.