-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Custom Transactions] Define
ChannelParameters
, TxBuilder
traits
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
Showing
3 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,4 +67,5 @@ check-cfg = [ | |
"cfg(splicing)", | ||
"cfg(async_payments)", | ||
"cfg(dual_funding)", | ||
"cfg(custom_tx)", | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 {} |