| 
 | 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