|
| 1 | +//! Defines the `TxBuilder` trait, and the `SpecTxBuilder` type |
| 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::ln::chan_utils::{ChannelTransactionParameters, HTLCOutputInCommitment, TxCreationKeys}; |
| 9 | +use crate::prelude::*; |
| 10 | + |
| 11 | +/// A trait for types that can build commitment transactions, both for the holder, and the counterparty. |
| 12 | +pub trait TxBuilder { |
| 13 | + /// Set the counterparty static channel data, including basepoints, |
| 14 | + /// `counterparty_selected`/`holder_selected_contest_delay` and funding outpoint. |
| 15 | + /// |
| 16 | + /// This data is static, and will never change for a channel once set. |
| 17 | + /// |
| 18 | + /// channel_parameters.is_populated() MUST be true. |
| 19 | + fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters); |
| 20 | + /// Build a commitment transaction, and populate the elements of `htlcs` with their output indices. |
| 21 | + /// Do not sort `htlcs`; this will be done by the caller as needed. |
| 22 | + /// This method will be called only after all the channel parameters have been provided via `provide_channel_parameters`. |
| 23 | + fn build_commitment_transaction( |
| 24 | + &self, is_holder_tx: bool, commitment_number: u64, per_commitment_point: &PublicKey, |
| 25 | + to_broadcaster_value_sat: Amount, to_countersignatory_value_sat: Amount, |
| 26 | + trimmed_value_sat: Amount, htlcs: Vec<&mut HTLCOutputInCommitment>, |
| 27 | + secp_ctx: &Secp256k1<secp256k1::All>, |
| 28 | + ) -> Transaction; |
| 29 | +} |
| 30 | + |
| 31 | +/// A type that builds commitment transactions according to the Lightning Specification. |
| 32 | +#[derive(Clone, Debug, Default)] |
| 33 | +pub struct SpecTxBuilder { |
| 34 | + channel_parameters: Option<ChannelTransactionParameters>, |
| 35 | +} |
| 36 | + |
| 37 | +impl TxBuilder for SpecTxBuilder { |
| 38 | + fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters) { |
| 39 | + assert!( |
| 40 | + self.channel_parameters.is_none() |
| 41 | + || self.channel_parameters.as_ref().unwrap() == channel_parameters |
| 42 | + ); |
| 43 | + if self.channel_parameters.is_some() { |
| 44 | + // The channel parameters were already set and they match, return early. |
| 45 | + return; |
| 46 | + } |
| 47 | + assert!(channel_parameters.is_populated(), "Channel parameters must be fully populated"); |
| 48 | + self.channel_parameters = Some(channel_parameters.clone()); |
| 49 | + } |
| 50 | + fn build_commitment_transaction( |
| 51 | + &self, is_holder_tx: bool, commitment_number: u64, per_commitment_point: &PublicKey, |
| 52 | + to_broadcaster_value_sat: Amount, to_countersignatory_value_sat: Amount, |
| 53 | + trimmed_value_sat: Amount, htlcs: Vec<&mut HTLCOutputInCommitment>, |
| 54 | + secp_ctx: &Secp256k1<secp256k1::All>, |
| 55 | + ) -> Transaction { |
| 56 | + let params = if is_holder_tx { |
| 57 | + self.channel_parameters.as_ref().unwrap().as_holder_broadcastable() |
| 58 | + } else { |
| 59 | + self.channel_parameters.as_ref().unwrap().as_counterparty_broadcastable() |
| 60 | + }; |
| 61 | + let keys = TxCreationKeys::from_channel_static_keys( |
| 62 | + per_commitment_point, |
| 63 | + params.broadcaster_pubkeys(), |
| 64 | + params.countersignatory_pubkeys(), |
| 65 | + secp_ctx, |
| 66 | + ); |
| 67 | + /* |
| 68 | + let (obscured_commitment_transaction_number, txins) = |
| 69 | + internal_build_inputs(commitment_number, ¶ms); |
| 70 | + let txouts = internal_build_outputs( |
| 71 | + &keys, |
| 72 | + to_broadcaster_value_sat, |
| 73 | + to_countersignatory_value_sat, |
| 74 | + htlcs, |
| 75 | + ¶ms, |
| 76 | + ); |
| 77 | + make_transaction(obscured_commitment_transaction_number, txins, txouts) |
| 78 | + */ |
| 79 | + todo!(); |
| 80 | + } |
| 81 | +} |
0 commit comments