Skip to content

Commit

Permalink
[Custom Transactions] Define ChannelParameters, TxBuilder traits
Browse files Browse the repository at this point in the history
This commit defines the necessary interfaces to give users the ability
to customize the build of the lightning commitment transactions.

The `ChannelParameters` trait will be used for any type that
requires the static channel parameters during its lifetime. This
includes any `TxBuilder` types as shown here.

We choose to separate the provision of the counterparty
parameters from the funding output for the case of Musig2 key
aggregation. The type will need to run key aggregation given the
parties' pubkeys before the type knows about the funding outpoint of the
channel.

In addition to using the `get_{*}_parameters` methods to provide a
default implementation of a commitment transaction build compliant with
the Lightning Specification, these methods would also be used in the
`impl` shown below:
```
impl<T> WitnessChannelSigner for T
    where T: ChannelParameters + EcdsaChannelSigner
```
where `EcdsaChannelSigner` is the `EcdsaChannelSigner` currently shipped
in LDK with no modifications. `WitnessChannelSigner` would return
witnesses instead of signatures, and hence be generic over any
particular segwit script used.

The `TxBuilder` trait has a single method,
`build_commitment_transaction`, which builds the commitment transaction,
and populates the output indices of the HTLCs passed in. Note that the
method itself does not impose any sorting.
  • Loading branch information
tankyleo committed Feb 18, 2025
1 parent b05402a commit 81b0ecb
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 0 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,5 @@ check-cfg = [
"cfg(splicing)",
"cfg(async_payments)",
"cfg(dual_funding)",
"cfg(custom_tx)",
]
2 changes: 2 additions & 0 deletions lightning/src/sign/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,8 @@ pub(crate) mod type_resolver;
pub mod ecdsa;
#[cfg(taproot)]
pub mod taproot;
#[cfg(custom_tx)]
pub mod tx_builder;

/// Information about a spendable output to a P2WSH script.
///
Expand Down
97 changes: 97 additions & 0 deletions lightning/src/sign/tx_builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
//! Defines ChannelParameters, TxBuilder traits
#![allow(dead_code)]
#![allow(unused_variables)]

use bitcoin::secp256k1::{self, PublicKey, Secp256k1};
use bitcoin::{Amount, Transaction};

use crate::chain::transaction::OutPoint;
use crate::ln::chan_utils::{
ChannelTransactionParameters, CounterpartyChannelTransactionParameters, HTLCOutputInCommitment,
TxCreationKeys,
};
use crate::prelude::*;

/// Defines a trait for types that hold the static channel parameters.
pub trait ChannelParameters {
/// Provides the parameters of the channel counterparty.
fn provide_counterparty_parameters(
&mut self, channel_parameters: &CounterpartyChannelTransactionParameters,
);
/// Provides the outpoint of the channel's funding output.
fn provide_funding_outpoint(&mut self, funding_outpoint: OutPoint);

/// This may be called at any point.
fn get_holder_parameters(&self) -> &ChannelTransactionParameters;
/// `channel_parameters.counterparty_parameters.is_some()` MUST be true.
/// This will be called only after the counterparty parameters have been provided.
fn get_holder_and_counterparty_parameters(&self) -> &ChannelTransactionParameters;
/// `ChannelTransactionParameters::is_populated()` MUST be true.
/// This will be called only after both the counterparty parameters, and the funding outpoint have been provided.
fn get_populated_parameters(&self) -> &ChannelTransactionParameters;
}

/// Defines a trait for types that can build commitment transactions, both for the holder, and the counterparty.
pub trait TxBuilder: ChannelParameters {
/// Builds a commitment transaction, and populates the elements of `htlcs` with their output indices.
/// Note that this function does not need to sort `htlcs`; this will be done by the caller as needed.
/// This will be called only after all channel parameters have been provided, including the funding outpoint.
fn build_commitment_transaction(
&self, is_holder_tx: bool, commitment_number: u64, per_commitment_point: &PublicKey,
to_broadcaster_value_sat: Amount, to_countersignatory_value_sat: Amount,
htlcs: Vec<&mut HTLCOutputInCommitment>, secp_ctx: &Secp256k1<secp256k1::All>,
) -> Transaction {
// Provide a default implementation that conforms to the LN specification.

let params = if is_holder_tx {
self.get_populated_parameters().as_holder_broadcastable()
} else {
self.get_populated_parameters().as_counterparty_broadcastable()
};
let keys = TxCreationKeys::from_channel_static_keys(
per_commitment_point,
params.broadcaster_pubkeys(),
params.countersignatory_pubkeys(),
secp_ctx,
);

// At this point, we have everything we need to build the transaction.
todo!();
}
}

/// A type that builds commitment transactions according to the Lightning Specification.
#[derive(Clone, Debug)]
pub struct SpecTxBuilder {
channel_parameters: ChannelTransactionParameters,
}

impl From<ChannelTransactionParameters> for SpecTxBuilder {
/// May be called when only the holder channel parameters are known; the counterparty parameters and the funding
/// outpoint will be provided later.
fn from(channel_parameters: ChannelTransactionParameters) -> Self {
SpecTxBuilder { channel_parameters }
}
}

impl ChannelParameters for SpecTxBuilder {
fn provide_counterparty_parameters(
&mut self, channel_parameters: &CounterpartyChannelTransactionParameters,
) {
todo!();
}
fn provide_funding_outpoint(&mut self, funding_outpoint: OutPoint) {
todo!();
}
fn get_holder_parameters(&self) -> &ChannelTransactionParameters {
todo!();
}
fn get_holder_and_counterparty_parameters(&self) -> &ChannelTransactionParameters {
todo!();
}
fn get_populated_parameters(&self) -> &ChannelTransactionParameters {
todo!();
}
}

impl TxBuilder for SpecTxBuilder {}

0 comments on commit 81b0ecb

Please sign in to comment.