Add utility function to add a change output to a transaction
[rust-lightning] / lightning / src / chain / channelmonitor.rs
index b46a2df13eea8d8c462e1df72dd6dca9fac2b45f..91c2a3c50c3531b813164eb4dc8c88287c4ce296 100644 (file)
@@ -43,7 +43,7 @@ use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
 use ln::onchaintx::{OnchainTxHandler, InputDescriptors};
 use chain::chaininterface::{BroadcasterInterface, FeeEstimator};
 use chain::transaction::{OutPoint, TransactionData};
-use chain::keysinterface::{SpendableOutputDescriptor, ChannelKeys, KeysInterface};
+use chain::keysinterface::{SpendableOutputDescriptor, StaticCounterpartyPaymentOutputDescriptor, DynamicP2WSHOutputDescriptor, ChannelKeys, KeysInterface};
 use util::logger::Logger;
 use util::ser::{Readable, ReadableArgs, MaybeReadable, Writer, Writeable, U48};
 use util::byte_utils;
@@ -174,7 +174,7 @@ pub enum ChannelMonitorUpdateErr {
 /// means you tried to update a monitor for a different channel or the ChannelMonitorUpdate was
 /// corrupted.
 /// Contains a developer-readable error message.
-#[derive(Debug)]
+#[derive(Clone, Debug)]
 pub struct MonitorUpdateError(pub &'static str);
 
 /// An event to be processed by the ChannelManager.
@@ -632,7 +632,7 @@ pub struct ChannelMonitor<ChanSigner: ChannelKeys> {
        counterparty_payment_script: Script,
        shutdown_script: Script,
 
-       key_derivation_params: (u64, u64),
+       channel_keys_id: [u8; 32],
        holder_revocation_basepoint: PublicKey,
        funding_info: (OutPoint, Script),
        current_counterparty_commitment_txid: Option<Txid>,
@@ -728,7 +728,7 @@ impl<ChanSigner: ChannelKeys> PartialEq for ChannelMonitor<ChanSigner> {
                        self.destination_script != other.destination_script ||
                        self.broadcasted_holder_revokable_script != other.broadcasted_holder_revokable_script ||
                        self.counterparty_payment_script != other.counterparty_payment_script ||
-                       self.key_derivation_params != other.key_derivation_params ||
+                       self.channel_keys_id != other.channel_keys_id ||
                        self.holder_revocation_basepoint != other.holder_revocation_basepoint ||
                        self.funding_info != other.funding_info ||
                        self.current_counterparty_commitment_txid != other.current_counterparty_commitment_txid ||
@@ -786,7 +786,7 @@ impl<ChanSigner: ChannelKeys> Writeable for ChannelMonitor<ChanSigner> {
                self.counterparty_payment_script.write(writer)?;
                self.shutdown_script.write(writer)?;
 
-               self.key_derivation_params.write(writer)?;
+               self.channel_keys_id.write(writer)?;
                self.holder_revocation_basepoint.write(writer)?;
                writer.write_all(&self.funding_info.0.txid[..])?;
                writer.write_all(&byte_utils::be16_to_array(self.funding_info.0.index))?;
@@ -967,7 +967,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                let counterparty_htlc_base_key = counterparty_channel_parameters.pubkeys.htlc_basepoint;
                let counterparty_tx_cache = CounterpartyCommitmentTransaction { counterparty_delayed_payment_base_key, counterparty_htlc_base_key, on_counterparty_tx_csv, per_htlc: HashMap::new() };
 
-               let key_derivation_params = keys.key_derivation_params();
+               let channel_keys_id = keys.channel_keys_id();
                let holder_revocation_basepoint = keys.pubkeys().revocation_basepoint;
 
                let secp_ctx = Secp256k1::new();
@@ -1006,7 +1006,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        counterparty_payment_script,
                        shutdown_script,
 
-                       key_derivation_params,
+                       channel_keys_id,
                        holder_revocation_basepoint,
                        funding_info,
                        current_counterparty_commitment_txid: None,
@@ -2201,22 +2201,24 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                                break;
                        } else if let Some(ref broadcasted_holder_revokable_script) = self.broadcasted_holder_revokable_script {
                                if broadcasted_holder_revokable_script.0 == outp.script_pubkey {
-                                       spendable_output =  Some(SpendableOutputDescriptor::DynamicOutputP2WSH {
+                                       spendable_output =  Some(SpendableOutputDescriptor::DynamicOutputP2WSH(DynamicP2WSHOutputDescriptor {
                                                outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
                                                per_commitment_point: broadcasted_holder_revokable_script.1,
                                                to_self_delay: self.on_holder_tx_csv,
                                                output: outp.clone(),
-                                               key_derivation_params: self.key_derivation_params,
                                                revocation_pubkey: broadcasted_holder_revokable_script.2.clone(),
-                                       });
+                                               channel_keys_id: self.channel_keys_id,
+                                               channel_value_satoshis: self.channel_value_satoshis,
+                                       }));
                                        break;
                                }
                        } else if self.counterparty_payment_script == outp.script_pubkey {
-                               spendable_output = Some(SpendableOutputDescriptor::StaticOutputCounterpartyPayment {
+                               spendable_output = Some(SpendableOutputDescriptor::StaticOutputCounterpartyPayment(StaticCounterpartyPaymentOutputDescriptor {
                                        outpoint: OutPoint { txid: tx.txid(), index: i as u16 },
                                        output: outp.clone(),
-                                       key_derivation_params: self.key_derivation_params,
-                               });
+                                       channel_keys_id: self.channel_keys_id,
+                                       channel_value_satoshis: self.channel_value_satoshis,
+                               }));
                                break;
                        } else if outp.script_pubkey == self.shutdown_script {
                                spendable_output = Some(SpendableOutputDescriptor::StaticOutput {
@@ -2332,7 +2334,7 @@ impl<'a, ChanSigner: ChannelKeys, K: KeysInterface<ChanKeySigner = ChanSigner>>
                let counterparty_payment_script = Readable::read(reader)?;
                let shutdown_script = Readable::read(reader)?;
 
-               let key_derivation_params = Readable::read(reader)?;
+               let channel_keys_id = Readable::read(reader)?;
                let holder_revocation_basepoint = Readable::read(reader)?;
                // Technically this can fail and serialize fail a round-trip, but only for serialization of
                // barely-init'd ChannelMonitors that we can't do anything with.
@@ -2547,7 +2549,7 @@ impl<'a, ChanSigner: ChannelKeys, K: KeysInterface<ChanKeySigner = ChanSigner>>
                        counterparty_payment_script,
                        shutdown_script,
 
-                       key_derivation_params,
+                       channel_keys_id,
                        holder_revocation_basepoint,
                        funding_info,
                        current_counterparty_commitment_txid,
@@ -2675,7 +2677,7 @@ mod tests {
                        SecretKey::from_slice(&[41; 32]).unwrap(),
                        [41; 32],
                        0,
-                       (0, 0)
+                       [0; 32]
                );
 
                let counterparty_pubkeys = ChannelPublicKeys {