Skip to content

Commit 81b0ecb

Browse files
committed
[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.
1 parent b05402a commit 81b0ecb

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,5 @@ check-cfg = [
6767
"cfg(splicing)",
6868
"cfg(async_payments)",
6969
"cfg(dual_funding)",
70+
"cfg(custom_tx)",
7071
]

lightning/src/sign/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,8 @@ pub(crate) mod type_resolver;
8181
pub mod ecdsa;
8282
#[cfg(taproot)]
8383
pub mod taproot;
84+
#[cfg(custom_tx)]
85+
pub mod tx_builder;
8486

8587
/// Information about a spendable output to a P2WSH script.
8688
///

lightning/src/sign/tx_builder.rs

+97
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
//! Defines ChannelParameters, TxBuilder traits
2+
#![allow(dead_code)]
3+
#![allow(unused_variables)]
4+
5+
use bitcoin::secp256k1::{self, PublicKey, Secp256k1};
6+
use bitcoin::{Amount, Transaction};
7+
8+
use crate::chain::transaction::OutPoint;
9+
use crate::ln::chan_utils::{
10+
ChannelTransactionParameters, CounterpartyChannelTransactionParameters, HTLCOutputInCommitment,
11+
TxCreationKeys,
12+
};
13+
use crate::prelude::*;
14+
15+
/// Defines a trait for types that hold the static channel parameters.
16+
pub trait ChannelParameters {
17+
/// Provides the parameters of the channel counterparty.
18+
fn provide_counterparty_parameters(
19+
&mut self, channel_parameters: &CounterpartyChannelTransactionParameters,
20+
);
21+
/// Provides the outpoint of the channel's funding output.
22+
fn provide_funding_outpoint(&mut self, funding_outpoint: OutPoint);
23+
24+
/// This may be called at any point.
25+
fn get_holder_parameters(&self) -> &ChannelTransactionParameters;
26+
/// `channel_parameters.counterparty_parameters.is_some()` MUST be true.
27+
/// This will be called only after the counterparty parameters have been provided.
28+
fn get_holder_and_counterparty_parameters(&self) -> &ChannelTransactionParameters;
29+
/// `ChannelTransactionParameters::is_populated()` MUST be true.
30+
/// This will be called only after both the counterparty parameters, and the funding outpoint have been provided.
31+
fn get_populated_parameters(&self) -> &ChannelTransactionParameters;
32+
}
33+
34+
/// Defines a trait for types that can build commitment transactions, both for the holder, and the counterparty.
35+
pub trait TxBuilder: ChannelParameters {
36+
/// Builds a commitment transaction, and populates the elements of `htlcs` with their output indices.
37+
/// Note that this function does not need to sort `htlcs`; this will be done by the caller as needed.
38+
/// This will be called only after all channel parameters have been provided, including the funding outpoint.
39+
fn build_commitment_transaction(
40+
&self, is_holder_tx: bool, commitment_number: u64, per_commitment_point: &PublicKey,
41+
to_broadcaster_value_sat: Amount, to_countersignatory_value_sat: Amount,
42+
htlcs: Vec<&mut HTLCOutputInCommitment>, secp_ctx: &Secp256k1<secp256k1::All>,
43+
) -> Transaction {
44+
// Provide a default implementation that conforms to the LN specification.
45+
46+
let params = if is_holder_tx {
47+
self.get_populated_parameters().as_holder_broadcastable()
48+
} else {
49+
self.get_populated_parameters().as_counterparty_broadcastable()
50+
};
51+
let keys = TxCreationKeys::from_channel_static_keys(
52+
per_commitment_point,
53+
params.broadcaster_pubkeys(),
54+
params.countersignatory_pubkeys(),
55+
secp_ctx,
56+
);
57+
58+
// At this point, we have everything we need to build the transaction.
59+
todo!();
60+
}
61+
}
62+
63+
/// A type that builds commitment transactions according to the Lightning Specification.
64+
#[derive(Clone, Debug)]
65+
pub struct SpecTxBuilder {
66+
channel_parameters: ChannelTransactionParameters,
67+
}
68+
69+
impl From<ChannelTransactionParameters> for SpecTxBuilder {
70+
/// May be called when only the holder channel parameters are known; the counterparty parameters and the funding
71+
/// outpoint will be provided later.
72+
fn from(channel_parameters: ChannelTransactionParameters) -> Self {
73+
SpecTxBuilder { channel_parameters }
74+
}
75+
}
76+
77+
impl ChannelParameters for SpecTxBuilder {
78+
fn provide_counterparty_parameters(
79+
&mut self, channel_parameters: &CounterpartyChannelTransactionParameters,
80+
) {
81+
todo!();
82+
}
83+
fn provide_funding_outpoint(&mut self, funding_outpoint: OutPoint) {
84+
todo!();
85+
}
86+
fn get_holder_parameters(&self) -> &ChannelTransactionParameters {
87+
todo!();
88+
}
89+
fn get_holder_and_counterparty_parameters(&self) -> &ChannelTransactionParameters {
90+
todo!();
91+
}
92+
fn get_populated_parameters(&self) -> &ChannelTransactionParameters {
93+
todo!();
94+
}
95+
}
96+
97+
impl TxBuilder for SpecTxBuilder {}

0 commit comments

Comments
 (0)