Skip to content

Commit

Permalink
Anchors support
Browse files Browse the repository at this point in the history
  • Loading branch information
benthecarman committed Sep 16, 2023
1 parent d200111 commit df72410
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
7 changes: 6 additions & 1 deletion mutiny-core/src/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::fees::MutinyFeeEstimator;
use crate::keymanager::PhantomKeysManager;
use crate::ldkstorage::{MutinyNodePersister, PhantomChannelManager};
use crate::logging::MutinyLogger;
use crate::node::BumpTxEventHandler;
use crate::nodemanager::ChannelClosure;
use crate::onchain::OnChainWallet;
use crate::redshift::RedshiftStorage;
Expand Down Expand Up @@ -81,17 +82,20 @@ pub struct EventHandler<S: MutinyStorage> {
wallet: Arc<OnChainWallet<S>>,
keys_manager: Arc<PhantomKeysManager<S>>,
persister: Arc<MutinyNodePersister<S>>,
bump_tx_event_handler: Arc<BumpTxEventHandler<S>>,
lsp_client_pubkey: Option<PublicKey>,
logger: Arc<MutinyLogger>,
}

impl<S: MutinyStorage> EventHandler<S> {
#[allow(clippy::too_many_arguments)]
pub(crate) fn new(
channel_manager: Arc<PhantomChannelManager<S>>,
fee_estimator: Arc<MutinyFeeEstimator<S>>,
wallet: Arc<OnChainWallet<S>>,
keys_manager: Arc<PhantomKeysManager<S>>,
persister: Arc<MutinyNodePersister<S>>,
bump_tx_event_handler: Arc<BumpTxEventHandler<S>>,
lsp_client_pubkey: Option<PublicKey>,
logger: Arc<MutinyLogger>,
) -> Self {
Expand All @@ -102,6 +106,7 @@ impl<S: MutinyStorage> EventHandler<S> {
keys_manager,
lsp_client_pubkey,
persister,
bump_tx_event_handler,
logger,
}
}
Expand Down Expand Up @@ -563,7 +568,7 @@ impl<S: MutinyStorage> EventHandler<S> {
}
}
Event::HTLCIntercepted { .. } => {}
Event::BumpTransaction(_) => {} // we do not support anchors
Event::BumpTransaction(event) => self.bump_tx_event_handler.handle_event(&event),
}
}

Expand Down
17 changes: 17 additions & 0 deletions mutiny-core/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ use lightning::{

use crate::multiesplora::MultiEsploraClient;
use bitcoin::util::bip32::ExtendedPrivKey;
use lightning::events::bump_transaction::{BumpTransactionEventHandler, Wallet};
use lightning::ln::PaymentSecret;
use lightning::sign::{EntropySource, InMemorySigner, NodeSigner, Recipient};
use lightning::util::config::MaxDustHTLCExposure;
Expand Down Expand Up @@ -81,6 +82,13 @@ const DEFAULT_PAYMENT_TIMEOUT: u64 = 30;
const INITIAL_RECONNECTION_DELAY: u64 = 5;
const MAX_RECONNECTION_DELAY: u64 = 60;

pub(crate) type BumpTxEventHandler<S: MutinyStorage> = BumpTransactionEventHandler<
Arc<MutinyChain<S>>,
Arc<Wallet<Arc<OnChainWallet<S>>, Arc<MutinyLogger>>>,
Arc<PhantomKeysManager<S>>,
Arc<MutinyLogger>,
>;

pub(crate) type RapidGossipSync =
lightning_rapid_gossip_sync::RapidGossipSync<Arc<NetworkGraph>, Arc<MutinyLogger>>;

Expand Down Expand Up @@ -328,6 +336,13 @@ impl<S: MutinyStorage> Node<S> {
Some(ref lsp) => lsp_clients.iter().find(|c| &c.url == lsp).cloned(),
};

let bump_tx_event_handler = Arc::new(BumpTransactionEventHandler::new(
Arc::clone(&chain),
Arc::new(Wallet::new(Arc::clone(&wallet), Arc::clone(&logger))),
Arc::clone(&keys_manager),
Arc::clone(&logger),
));

let lsp_client_pubkey = lsp_client.clone().map(|lsp| lsp.pubkey);

// init event handler
Expand All @@ -337,6 +352,7 @@ impl<S: MutinyStorage> Node<S> {
wallet.clone(),
keys_manager.clone(),
persister.clone(),
bump_tx_event_handler,
lsp_client_pubkey,
logger.clone(),
);
Expand Down Expand Up @@ -1764,6 +1780,7 @@ pub(crate) fn default_user_config() -> UserConfig {
announced_channel: false,
negotiate_scid_privacy: true,
commit_upfront_shutdown_pubkey: false,
negotiate_anchors_zero_fee_htlc_tx: true,
max_inbound_htlc_value_in_flight_percent_of_channel: 100,
..Default::default()
},
Expand Down
35 changes: 35 additions & 0 deletions mutiny-core/src/onchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::sync::{Arc, RwLock};
use bdk::chain::{BlockId, ConfirmationTime};
use bdk::psbt::PsbtUtils;
use bdk::template::DescriptorTemplateOut;
use bdk::wallet::AddressIndex;
use bdk::{FeeRate, LocalUtxo, SignOptions, TransactionDetails, Wallet};
use bdk_esplora::EsploraAsyncExt;
use bitcoin::consensus::serialize;
Expand All @@ -15,6 +16,7 @@ use bitcoin::psbt::PartiallySignedTransaction;
use bitcoin::util::bip32::{ChildNumber, DerivationPath, ExtendedPrivKey};
use bitcoin::{Address, Network, OutPoint, Script, Transaction, Txid};
use lightning::chain::chaininterface::{ConfirmationTarget, FeeEstimator};
use lightning::events::bump_transaction::{Utxo, WalletSource};
use lightning::util::logger::Logger;
use lightning::{log_debug, log_error, log_info, log_warn};

Expand Down Expand Up @@ -629,6 +631,39 @@ pub(crate) fn get_esplora_url(network: Network, user_provided_url: Option<String
}
}

impl<S: MutinyStorage> WalletSource for OnChainWallet<S> {
fn list_confirmed_utxos(&self) -> Result<Vec<Utxo>, ()> {
let wallet = self.wallet.try_read().map_err(|_| ())?;
let utxos = wallet
.list_unspent()
.map(|u| Utxo {
outpoint: u.outpoint,
output: u.txout,
satisfaction_weight: 4 + 2 + 64,
})
.collect();

Ok(utxos)
}

fn get_change_script(&self) -> Result<Script, ()> {
let mut wallet = self.wallet.try_write().map_err(|_| ())?;
let addr = wallet.get_internal_address(AddressIndex::New).address;
Ok(addr.script_pubkey())
}

fn sign_tx(&self, tx: Transaction) -> Result<Transaction, ()> {
let wallet = self.wallet.try_read().map_err(|_| ())?;
// fixme will this work?
let mut psbt = PartiallySignedTransaction::from_unsigned_tx(tx).map_err(|_| ())?;
wallet
.sign(&mut psbt, SignOptions::default())
.map_err(|_| ())?;

Ok(psbt.extract_tx())
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit df72410

Please sign in to comment.