Switch Sha256 to using bitcoin_hashes and our own HKDF
[rust-lightning] / src / ln / channel.rs
index 3ddf44afa6a195981f870548d03de7078e4b690c..5bc5fdf8be126063437f3ffe65dc7f5076a4b882 100644 (file)
@@ -6,16 +6,17 @@ use bitcoin::util::hash::{BitcoinHash, Sha256dHash, Hash160};
 use bitcoin::util::bip143;
 use bitcoin::consensus::encode::{self, Encodable, Decodable};
 
+use bitcoin_hashes::{Hash, HashEngine};
+use bitcoin_hashes::sha256::Hash as Sha256;
+
 use secp256k1::key::{PublicKey,SecretKey};
 use secp256k1::{Secp256k1,Message,Signature};
 use secp256k1;
 
-use crypto::digest::Digest;
-
 use ln::msgs;
 use ln::msgs::DecodeError;
 use ln::channelmonitor::ChannelMonitor;
-use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingForwardHTLCInfo, RAACommitmentOrder};
+use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingForwardHTLCInfo, RAACommitmentOrder, PaymentPreimage, PaymentHash};
 use ln::chan_utils::{TxCreationKeys,HTLCOutputInCommitment,HTLC_SUCCESS_TX_WEIGHT,HTLC_TIMEOUT_TX_WEIGHT};
 use ln::chan_utils;
 use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
@@ -23,7 +24,6 @@ use chain::transaction::OutPoint;
 use chain::keysinterface::{ChannelKeys, KeysInterface};
 use util::{transaction_utils,rng};
 use util::ser::{Readable, ReadableArgs, Writeable, Writer, WriterWriteAdaptor};
-use util::sha2::Sha256;
 use util::logger::Logger;
 use util::errors::APIError;
 use util::config::{UserConfig,ChannelConfig};
@@ -48,7 +48,7 @@ pub struct ChannelValueStat {
 enum InboundHTLCRemovalReason {
        FailRelay(msgs::OnionErrorPacket),
        FailMalformed(([u8; 32], u16)),
-       Fulfill([u8; 32]),
+       Fulfill(PaymentPreimage),
 }
 
 enum InboundHTLCState {
@@ -84,7 +84,7 @@ struct InboundHTLCOutput {
        htlc_id: u64,
        amount_msat: u64,
        cltv_expiry: u32,
-       payment_hash: [u8; 32],
+       payment_hash: PaymentHash,
        state: InboundHTLCState,
 }
 
@@ -124,7 +124,7 @@ struct OutboundHTLCOutput {
        htlc_id: u64,
        amount_msat: u64,
        cltv_expiry: u32,
-       payment_hash: [u8; 32],
+       payment_hash: PaymentHash,
        state: OutboundHTLCState,
        source: HTLCSource,
        /// If we're in a removed state, set if they failed, otherwise None
@@ -149,13 +149,13 @@ enum HTLCUpdateAwaitingACK {
                // always outbound
                amount_msat: u64,
                cltv_expiry: u32,
-               payment_hash: [u8; 32],
+               payment_hash: PaymentHash,
                source: HTLCSource,
                onion_routing_packet: msgs::OnionPacket,
                time_created: Instant, //TODO: Some kind of timeout thing-a-majig
        },
        ClaimHTLC {
-               payment_preimage: [u8; 32],
+               payment_preimage: PaymentPreimage,
                htlc_id: u64,
        },
        FailHTLC {
@@ -263,7 +263,7 @@ pub(super) struct Channel {
        monitor_pending_commitment_signed: bool,
        monitor_pending_order: Option<RAACommitmentOrder>,
        monitor_pending_forwards: Vec<(PendingForwardHTLCInfo, u64)>,
-       monitor_pending_failures: Vec<(HTLCSource, [u8; 32], HTLCFailReason)>,
+       monitor_pending_failures: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>,
 
        // pending_update_fee is filled when sending and receiving update_fee
        // For outbound channel, feerate_per_kw is updated with the value from
@@ -364,6 +364,12 @@ const B_OUTPUT_PLUS_SPENDING_INPUT_WEIGHT: u64 = 104; // prevout: 40, nSequence:
 /// it's 2^24.
 pub const MAX_FUNDING_SATOSHIS: u64 = (1 << 24);
 
+#[cfg(test)]
+pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 138; //Here we have a diff due to HTLC CLTV expiry being < 2^15 in test
+#[cfg(not(test))]
+pub const ACCEPTED_HTLC_SCRIPT_WEIGHT: usize = 139;
+pub const OFFERED_HTLC_SCRIPT_WEIGHT: usize = 133;
+
 /// Used to return a simple Error back to ChannelManager. Will get converted to a
 /// msgs::ErrorAction::SendErrorMessage or msgs::ErrorAction::IgnoreError as appropriate with our
 /// channel_id in ChannelManager.
@@ -716,7 +722,7 @@ impl Channel {
        // Utilities to build transactions:
 
        fn get_commitment_transaction_number_obscure_factor(&self) -> u64 {
-               let mut sha = Sha256::new();
+               let mut sha = Sha256::engine();
                let our_payment_basepoint = PublicKey::from_secret_key(&self.secp_ctx, &self.local_keys.payment_base_key);
 
                if self.channel_outbound {
@@ -726,8 +732,7 @@ impl Channel {
                        sha.input(&self.their_payment_basepoint.unwrap().serialize());
                        sha.input(&our_payment_basepoint.serialize());
                }
-               let mut res = [0; 32];
-               sha.result(&mut res);
+               let res = Sha256::from_engine(sha).into_inner();
 
                ((res[26] as u64) << 5*8) |
                ((res[27] as u64) << 4*8) |
@@ -751,7 +756,7 @@ impl Channel {
        /// generated by the peer which proposed adding the HTLCs, and thus we need to understand both
        /// which peer generated this transaction and "to whom" this transaction flows.
        #[inline]
-       fn build_commitment_transaction(&self, commitment_number: u64, keys: &TxCreationKeys, local: bool, generated_by_local: bool, feerate_per_kw: u64) -> (Transaction, Vec<HTLCOutputInCommitment>) {
+       fn build_commitment_transaction(&self, commitment_number: u64, keys: &TxCreationKeys, local: bool, generated_by_local: bool, feerate_per_kw: u64) -> (Transaction, Vec<HTLCOutputInCommitment>, Vec<(PaymentHash, &HTLCSource, Option<u32>)>) {
                let obscured_commitment_transaction_number = self.get_commitment_transaction_number_obscure_factor() ^ (INITIAL_COMMITMENT_NUMBER - commitment_number);
 
                let txins = {
@@ -765,7 +770,8 @@ impl Channel {
                        ins
                };
 
-               let mut txouts: Vec<(TxOut, Option<HTLCOutputInCommitment>)> = Vec::with_capacity(self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len() + 2);
+               let mut txouts: Vec<(TxOut, Option<(HTLCOutputInCommitment, Option<&HTLCSource>)>)> = Vec::with_capacity(self.pending_inbound_htlcs.len() + self.pending_outbound_htlcs.len() + 2);
+               let mut unincluded_htlc_sources: Vec<(PaymentHash, &HTLCSource, Option<u32>)> = Vec::new();
 
                let dust_limit_satoshis = if local { self.our_dust_limit_satoshis } else { self.their_dust_limit_satoshis };
                let mut remote_htlc_total_msat = 0;
@@ -773,14 +779,18 @@ impl Channel {
                let mut value_to_self_msat_offset = 0;
 
                macro_rules! add_htlc_output {
-                       ($htlc: expr, $outbound: expr) => {
+                       ($htlc: expr, $outbound: expr, $source: expr) => {
                                if $outbound == local { // "offered HTLC output"
                                        if $htlc.amount_msat / 1000 >= dust_limit_satoshis + (feerate_per_kw * HTLC_TIMEOUT_TX_WEIGHT / 1000) {
                                                let htlc_in_tx = get_htlc_in_commitment!($htlc, true);
                                                txouts.push((TxOut {
                                                        script_pubkey: chan_utils::get_htlc_redeemscript(&htlc_in_tx, &keys).to_v0_p2wsh(),
                                                        value: $htlc.amount_msat / 1000
-                                               }, Some(htlc_in_tx)));
+                                               }, Some((htlc_in_tx, $source))));
+                                       } else {
+                                               if let Some(source) = $source {
+                                                       unincluded_htlc_sources.push(($htlc.payment_hash, source, None));
+                                               }
                                        }
                                } else {
                                        if $htlc.amount_msat / 1000 >= dust_limit_satoshis + (feerate_per_kw * HTLC_SUCCESS_TX_WEIGHT / 1000) {
@@ -788,7 +798,11 @@ impl Channel {
                                                txouts.push((TxOut { // "received HTLC output"
                                                        script_pubkey: chan_utils::get_htlc_redeemscript(&htlc_in_tx, &keys).to_v0_p2wsh(),
                                                        value: $htlc.amount_msat / 1000
-                                               }, Some(htlc_in_tx)));
+                                               }, Some((htlc_in_tx, $source))));
+                                       } else {
+                                               if let Some(source) = $source {
+                                                       unincluded_htlc_sources.push(($htlc.payment_hash, source, None));
+                                               }
                                        }
                                }
                        }
@@ -804,7 +818,7 @@ impl Channel {
                        };
 
                        if include {
-                               add_htlc_output!(htlc, false);
+                               add_htlc_output!(htlc, false, None);
                                remote_htlc_total_msat += htlc.amount_msat;
                        } else {
                                match &htlc.state {
@@ -830,7 +844,7 @@ impl Channel {
                        };
 
                        if include {
-                               add_htlc_output!(htlc, true);
+                               add_htlc_output!(htlc, true, Some(&htlc.source));
                                local_htlc_total_msat += htlc.amount_msat;
                        } else {
                                match htlc.state {
@@ -901,21 +915,26 @@ impl Channel {
                transaction_utils::sort_outputs(&mut txouts);
 
                let mut outputs: Vec<TxOut> = Vec::with_capacity(txouts.len());
-               let mut htlcs_used: Vec<HTLCOutputInCommitment> = Vec::with_capacity(txouts.len());
+               let mut htlcs_included: Vec<HTLCOutputInCommitment> = Vec::with_capacity(txouts.len());
+               let mut htlc_sources: Vec<(PaymentHash, &HTLCSource, Option<u32>)> = Vec::with_capacity(txouts.len() + unincluded_htlc_sources.len());
                for (idx, out) in txouts.drain(..).enumerate() {
                        outputs.push(out.0);
-                       if let Some(out_htlc) = out.1 {
-                               htlcs_used.push(out_htlc);
-                               htlcs_used.last_mut().unwrap().transaction_output_index = idx as u32;
+                       if let Some((mut htlc, source_option)) = out.1 {
+                               htlc.transaction_output_index = idx as u32;
+                               if let Some(source) = source_option {
+                                       htlc_sources.push((htlc.payment_hash, source, Some(idx as u32)));
+                               }
+                               htlcs_included.push(htlc);
                        }
                }
+               htlc_sources.append(&mut unincluded_htlc_sources);
 
                (Transaction {
                        version: 2,
                        lock_time: ((0x20 as u32) << 8*3) | ((obscured_commitment_transaction_number & 0xffffffu64) as u32),
                        input: txins,
                        output: outputs,
-               }, htlcs_used)
+               }, htlcs_included, htlc_sources)
        }
 
        #[inline]
@@ -1004,6 +1023,7 @@ impl Channel {
        #[inline]
        /// Creates a set of keys for build_commitment_transaction to generate a transaction which we
        /// will sign and send to our counterparty.
+       /// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
        fn build_remote_transaction_keys(&self) -> Result<TxCreationKeys, ChannelError> {
                //TODO: Ensure that the payment_key derived here ends up in the library users' wallet as we
                //may see payments to it!
@@ -1084,7 +1104,7 @@ impl Channel {
 
        /// Signs a transaction created by build_htlc_transaction. If the transaction is an
        /// HTLC-Success transaction (ie htlc.offered is false), preimate must be set!
-       fn sign_htlc_transaction(&self, tx: &mut Transaction, their_sig: &Signature, preimage: &Option<[u8; 32]>, htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys) -> Result<Signature, ChannelError> {
+       fn sign_htlc_transaction(&self, tx: &mut Transaction, their_sig: &Signature, preimage: &Option<PaymentPreimage>, htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys) -> Result<Signature, ChannelError> {
                if tx.input.len() != 1 {
                        panic!("Tried to sign HTLC transaction that had input count != 1!");
                }
@@ -1109,7 +1129,7 @@ impl Channel {
                if htlc.offered {
                        tx.input[0].witness.push(Vec::new());
                } else {
-                       tx.input[0].witness.push(preimage.unwrap().to_vec());
+                       tx.input[0].witness.push(preimage.unwrap().0.to_vec());
                }
 
                tx.input[0].witness.push(htlc_redeemscript.into_bytes());
@@ -1120,7 +1140,7 @@ impl Channel {
        /// Per HTLC, only one get_update_fail_htlc or get_update_fulfill_htlc call may be made.
        /// In such cases we debug_assert!(false) and return an IgnoreError. Thus, will always return
        /// Ok(_) if debug assertions are turned on and preconditions are met.
-       fn get_update_fulfill_htlc(&mut self, htlc_id_arg: u64, payment_preimage_arg: [u8; 32]) -> Result<(Option<msgs::UpdateFulfillHTLC>, Option<ChannelMonitor>), ChannelError> {
+       fn get_update_fulfill_htlc(&mut self, htlc_id_arg: u64, payment_preimage_arg: PaymentPreimage) -> Result<(Option<msgs::UpdateFulfillHTLC>, Option<ChannelMonitor>), ChannelError> {
                // Either ChannelFunded got set (which means it wont bet unset) or there is no way any
                // caller thought we could have something claimed (cause we wouldn't have accepted in an
                // incoming HTLC anyway). If we got to ShutdownComplete, callers aren't allowed to call us,
@@ -1130,26 +1150,35 @@ impl Channel {
                }
                assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
 
-               let mut sha = Sha256::new();
-               sha.input(&payment_preimage_arg);
-               let mut payment_hash_calc = [0; 32];
-               sha.result(&mut payment_hash_calc);
+               let payment_hash_calc = PaymentHash(Sha256::hash(&payment_preimage_arg.0[..]).into_inner());
+
+               // ChannelManager may generate duplicate claims/fails due to HTLC update events from
+               // on-chain ChannelsMonitors during block rescan. Ideally we'd figure out a way to drop
+               // these, but for now we just have to treat them as normal.
 
                let mut pending_idx = std::usize::MAX;
                for (idx, htlc) in self.pending_inbound_htlcs.iter().enumerate() {
                        if htlc.htlc_id == htlc_id_arg {
                                assert_eq!(htlc.payment_hash, payment_hash_calc);
-                               if let InboundHTLCState::Committed = htlc.state {
-                               } else {
-                                       debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
-                                       // Don't return in release mode here so that we can update channel_monitor
+                               match htlc.state {
+                                       InboundHTLCState::Committed => {},
+                                       InboundHTLCState::LocalRemoved(ref reason) => {
+                                               if let &InboundHTLCRemovalReason::Fulfill(_) = reason {
+                                               } else {
+                                                       log_warn!(self, "Have preimage and want to fulfill HTLC with payment hash {} we already failed against channel {}", log_bytes!(htlc.payment_hash.0), log_bytes!(self.channel_id()));
+                                               }
+                                               return Ok((None, None));
+                                       },
+                                       _ => {
+                                               debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
+                                               // Don't return in release mode here so that we can update channel_monitor
+                                       }
                                }
                                pending_idx = idx;
                                break;
                        }
                }
                if pending_idx == std::usize::MAX {
-                       debug_assert!(false, "Unable to find a pending HTLC which matched the given HTLC ID");
                        return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
                }
 
@@ -1164,15 +1193,14 @@ impl Channel {
                                match pending_update {
                                        &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
                                                if htlc_id_arg == htlc_id {
-                                                       debug_assert!(false, "Tried to fulfill an HTLC we already had a pending fulfill for");
                                                        return Ok((None, None));
                                                }
                                        },
                                        &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
                                                if htlc_id_arg == htlc_id {
-                                                       debug_assert!(false, "Tried to fulfill an HTLC we already had a holding-cell failure on");
-                                                       // Return the new channel monitor in a last-ditch effort to hit the
-                                                       // chain and claim the funds
+                                                       log_warn!(self, "Have preimage and want to fulfill HTLC with pending failure against channel {}", log_bytes!(self.channel_id()));
+                                                       // TODO: We may actually be able to switch to a fulfill here, though its
+                                                       // rare enough it may not be worth the complexity burden.
                                                        return Ok((None, Some(self.channel_monitor.clone())));
                                                }
                                        },
@@ -1202,7 +1230,7 @@ impl Channel {
                }), Some(self.channel_monitor.clone())))
        }
 
-       pub fn get_update_fulfill_htlc_and_commit(&mut self, htlc_id: u64, payment_preimage: [u8; 32]) -> Result<(Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)>, Option<ChannelMonitor>), ChannelError> {
+       pub fn get_update_fulfill_htlc_and_commit(&mut self, htlc_id: u64, payment_preimage: PaymentPreimage) -> Result<(Option<(msgs::UpdateFulfillHTLC, msgs::CommitmentSigned)>, Option<ChannelMonitor>), ChannelError> {
                match self.get_update_fulfill_htlc(htlc_id, payment_preimage)? {
                        (Some(update_fulfill_htlc), _) => {
                                let (commitment, monitor_update) = self.send_commitment_no_status_check()?;
@@ -1222,19 +1250,27 @@ impl Channel {
                }
                assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
 
+               // ChannelManager may generate duplicate claims/fails due to HTLC update events from
+               // on-chain ChannelsMonitors during block rescan. Ideally we'd figure out a way to drop
+               // these, but for now we just have to treat them as normal.
+
                let mut pending_idx = std::usize::MAX;
                for (idx, htlc) in self.pending_inbound_htlcs.iter().enumerate() {
                        if htlc.htlc_id == htlc_id_arg {
-                               if let InboundHTLCState::Committed = htlc.state {
-                               } else {
-                                       debug_assert!(false, "Have an inbound HTLC we tried to fail before it was fully committed to");
-                                       return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
+                               match htlc.state {
+                                       InboundHTLCState::Committed => {},
+                                       InboundHTLCState::LocalRemoved(_) => {
+                                               return Ok(None);
+                                       },
+                                       _ => {
+                                               debug_assert!(false, "Have an inbound HTLC we tried to claim before it was fully committed to");
+                                               return Err(ChannelError::Ignore("Unable to find a pending HTLC which matchd the given HTLC ID"));
+                                       }
                                }
                                pending_idx = idx;
                        }
                }
                if pending_idx == std::usize::MAX {
-                       debug_assert!(false, "Unable to find a pending HTLC which matched the given HTLC ID");
                        return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
                }
 
@@ -1244,14 +1280,12 @@ impl Channel {
                                match pending_update {
                                        &HTLCUpdateAwaitingACK::ClaimHTLC { htlc_id, .. } => {
                                                if htlc_id_arg == htlc_id {
-                                                       debug_assert!(false, "Unable to find a pending HTLC which matched the given HTLC ID");
                                                        return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
                                                }
                                        },
                                        &HTLCUpdateAwaitingACK::FailHTLC { htlc_id, .. } => {
                                                if htlc_id_arg == htlc_id {
-                                                       debug_assert!(false, "Tried to fail an HTLC that we already had a pending failure for");
-                                                       return Ok(None);
+                                                       return Err(ChannelError::Ignore("Unable to find a pending HTLC which matched the given HTLC ID"));
                                                }
                                        },
                                        _ => {}
@@ -1423,9 +1457,9 @@ impl Channel {
 
                // Now that we're past error-generating stuff, update our local state:
 
-               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
+               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_initial_commitment_tx, Vec::new(), Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
                self.last_local_commitment_txn = vec![local_initial_commitment_tx.clone()];
-               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx, local_keys, self.feerate_per_kw, Vec::new());
+               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx, local_keys, self.feerate_per_kw, Vec::new(), Vec::new());
                self.channel_state = ChannelState::FundingSent as u32;
                self.channel_id = funding_txo.to_channel_id();
                self.cur_remote_commitment_transaction_number -= 1;
@@ -1462,7 +1496,7 @@ impl Channel {
                secp_check!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid funding_signed signature from peer");
 
                self.sign_commitment_transaction(&mut local_initial_commitment_tx, &msg.signature);
-               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx.clone(), local_keys, self.feerate_per_kw, Vec::new());
+               self.channel_monitor.provide_latest_local_commitment_tx_info(local_initial_commitment_tx.clone(), local_keys, self.feerate_per_kw, Vec::new(), Vec::new());
                self.last_local_commitment_txn = vec![local_initial_commitment_tx];
                self.channel_state = ChannelState::FundingSent as u32;
                self.cur_local_commitment_transaction_number -= 1;
@@ -1578,7 +1612,7 @@ impl Channel {
 
        /// Marks an outbound HTLC which we have received update_fail/fulfill/malformed
        #[inline]
-       fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<[u8; 32]>, fail_reason: Option<HTLCFailReason>) -> Result<&HTLCSource, ChannelError> {
+       fn mark_outbound_htlc_removed(&mut self, htlc_id: u64, check_preimage: Option<PaymentHash>, fail_reason: Option<HTLCFailReason>) -> Result<&HTLCSource, ChannelError> {
                for htlc in self.pending_outbound_htlcs.iter_mut() {
                        if htlc.htlc_id == htlc_id {
                                match check_preimage {
@@ -1604,7 +1638,7 @@ impl Channel {
                Err(ChannelError::Close("Remote tried to fulfill/fail an HTLC we couldn't find"))
        }
 
-       pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<&HTLCSource, ChannelError> {
+       pub fn update_fulfill_htlc(&mut self, msg: &msgs::UpdateFulfillHTLC) -> Result<HTLCSource, ChannelError> {
                if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
                        return Err(ChannelError::Close("Got fulfill HTLC message when channel was not in an operational state"));
                }
@@ -1612,15 +1646,11 @@ impl Channel {
                        return Err(ChannelError::Close("Peer sent update_fulfill_htlc when we needed a channel_reestablish"));
                }
 
-               let mut sha = Sha256::new();
-               sha.input(&msg.payment_preimage);
-               let mut payment_hash = [0; 32];
-               sha.result(&mut payment_hash);
-
-               self.mark_outbound_htlc_removed(msg.htlc_id, Some(payment_hash), None)
+               let payment_hash = PaymentHash(Sha256::hash(&msg.payment_preimage.0[..]).into_inner());
+               self.mark_outbound_htlc_removed(msg.htlc_id, Some(payment_hash), None).map(|source| source.clone())
        }
 
-       pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<&HTLCSource, ChannelError> {
+       pub fn update_fail_htlc(&mut self, msg: &msgs::UpdateFailHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> {
                if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
                        return Err(ChannelError::Close("Got fail HTLC message when channel was not in an operational state"));
                }
@@ -1628,10 +1658,11 @@ impl Channel {
                        return Err(ChannelError::Close("Peer sent update_fail_htlc when we needed a channel_reestablish"));
                }
 
-               self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))
+               self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))?;
+               Ok(())
        }
 
-       pub fn update_fail_malformed_htlc<'a>(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<&HTLCSource, ChannelError> {
+       pub fn update_fail_malformed_htlc<'a>(&mut self, msg: &msgs::UpdateFailMalformedHTLC, fail_reason: HTLCFailReason) -> Result<(), ChannelError> {
                if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
                        return Err(ChannelError::Close("Got fail malformed HTLC message when channel was not in an operational state"));
                }
@@ -1639,7 +1670,8 @@ impl Channel {
                        return Err(ChannelError::Close("Peer sent update_fail_malformed_htlc when we needed a channel_reestablish"));
                }
 
-               self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))
+               self.mark_outbound_htlc_removed(msg.htlc_id, None, Some(fail_reason))?;
+               Ok(())
        }
 
        pub fn commitment_signed(&mut self, msg: &msgs::CommitmentSigned, fee_estimator: &FeeEstimator) -> Result<(msgs::RevokeAndACK, Option<msgs::CommitmentSigned>, Option<msgs::ClosingSigned>, ChannelMonitor), ChannelError> {
@@ -1665,7 +1697,11 @@ impl Channel {
                        self.feerate_per_kw
                };
 
-               let mut local_commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, feerate_per_kw);
+               let mut local_commitment_tx = {
+                       let mut commitment_tx = self.build_commitment_transaction(self.cur_local_commitment_transaction_number, &local_keys, true, false, feerate_per_kw);
+                       let htlcs_cloned: Vec<_> = commitment_tx.2.drain(..).map(|htlc_source| (htlc_source.0, htlc_source.1.clone(), htlc_source.2)).collect();
+                       (commitment_tx.0, commitment_tx.1, htlcs_cloned)
+               };
                let local_commitment_txid = local_commitment_tx.0.txid();
                let local_sighash = Message::from_slice(&bip143::SighashComponents::new(&local_commitment_tx.0).sighash_all(&local_commitment_tx.0.input[0], &funding_script, self.channel_value_satoshis)[..]).unwrap();
                secp_check!(self.secp_ctx.verify(&local_sighash, &msg.signature, &self.their_funding_pubkey.unwrap()), "Invalid commitment tx signature from peer");
@@ -1689,19 +1725,19 @@ impl Channel {
                new_local_commitment_txn.push(local_commitment_tx.0.clone());
 
                let mut htlcs_and_sigs = Vec::with_capacity(local_commitment_tx.1.len());
-               for (idx, ref htlc) in local_commitment_tx.1.iter().enumerate() {
-                       let mut htlc_tx = self.build_htlc_transaction(&local_commitment_txid, htlc, true, &local_keys, feerate_per_kw);
+               for (idx, htlc) in local_commitment_tx.1.drain(..).enumerate() {
+                       let mut htlc_tx = self.build_htlc_transaction(&local_commitment_txid, &htlc, true, &local_keys, feerate_per_kw);
                        let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&htlc, &local_keys);
                        let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
                        secp_check!(self.secp_ctx.verify(&htlc_sighash, &msg.htlc_signatures[idx], &local_keys.b_htlc_key), "Invalid HTLC tx signature from peer");
                        let htlc_sig = if htlc.offered {
-                               let htlc_sig = self.sign_htlc_transaction(&mut htlc_tx, &msg.htlc_signatures[idx], &None, htlc, &local_keys)?;
+                               let htlc_sig = self.sign_htlc_transaction(&mut htlc_tx, &msg.htlc_signatures[idx], &None, &htlc, &local_keys)?;
                                new_local_commitment_txn.push(htlc_tx);
                                htlc_sig
                        } else {
-                               self.create_htlc_tx_signature(&htlc_tx, htlc, &local_keys)?.1
+                               self.create_htlc_tx_signature(&htlc_tx, &htlc, &local_keys)?.1
                        };
-                       htlcs_and_sigs.push(((*htlc).clone(), msg.htlc_signatures[idx], htlc_sig));
+                       htlcs_and_sigs.push((htlc, msg.htlc_signatures[idx], htlc_sig));
                }
 
                let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &self.build_local_commitment_secret(self.cur_local_commitment_transaction_number - 1));
@@ -1728,7 +1764,7 @@ impl Channel {
                        self.monitor_pending_order = None;
                }
 
-               self.channel_monitor.provide_latest_local_commitment_tx_info(local_commitment_tx.0, local_keys, self.feerate_per_kw, htlcs_and_sigs);
+               self.channel_monitor.provide_latest_local_commitment_tx_info(local_commitment_tx.0, local_keys, self.feerate_per_kw, htlcs_and_sigs, local_commitment_tx.2);
 
                for htlc in self.pending_inbound_htlcs.iter_mut() {
                        let new_forward = if let &InboundHTLCState::RemoteAnnounced(ref forward_info) = &htlc.state {
@@ -1794,16 +1830,16 @@ impl Channel {
                                        self.holding_cell_htlc_updates.push(htlc_update);
                                } else {
                                        match &htlc_update {
-                                               &HTLCUpdateAwaitingACK::AddHTLC {amount_msat, cltv_expiry, payment_hash, ref source, ref onion_routing_packet, ..} => {
-                                                       match self.send_htlc(amount_msat, payment_hash, cltv_expiry, source.clone(), onion_routing_packet.clone()) {
+                                               &HTLCUpdateAwaitingACK::AddHTLC {amount_msat, cltv_expiry, ref payment_hash, ref source, ref onion_routing_packet, ..} => {
+                                                       match self.send_htlc(amount_msat, *payment_hash, cltv_expiry, source.clone(), onion_routing_packet.clone()) {
                                                                Ok(update_add_msg_option) => update_add_htlcs.push(update_add_msg_option.unwrap()),
                                                                Err(e) => {
                                                                        err = Some(e);
                                                                }
                                                        }
                                                },
-                                               &HTLCUpdateAwaitingACK::ClaimHTLC { payment_preimage, htlc_id, .. } => {
-                                                       match self.get_update_fulfill_htlc(htlc_id, payment_preimage) {
+                                               &HTLCUpdateAwaitingACK::ClaimHTLC { ref payment_preimage, htlc_id, .. } => {
+                                                       match self.get_update_fulfill_htlc(htlc_id, *payment_preimage) {
                                                                Ok(update_fulfill_msg_option) => update_fulfill_htlcs.push(update_fulfill_msg_option.0.unwrap()),
                                                                Err(e) => {
                                                                        if let ChannelError::Ignore(_) = e {}
@@ -1871,7 +1907,7 @@ impl Channel {
        /// waiting on this revoke_and_ack. The generation of this new commitment_signed may also fail,
        /// generating an appropriate error *after* the channel state has been updated based on the
        /// revoke_and_ack message.
-       pub fn revoke_and_ack(&mut self, msg: &msgs::RevokeAndACK, fee_estimator: &FeeEstimator) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, [u8; 32], HTLCFailReason)>, Option<msgs::ClosingSigned>, ChannelMonitor), ChannelError> {
+       pub fn revoke_and_ack(&mut self, msg: &msgs::RevokeAndACK, fee_estimator: &FeeEstimator) -> Result<(Option<msgs::CommitmentUpdate>, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, Option<msgs::ClosingSigned>, ChannelMonitor), ChannelError> {
                if (self.channel_state & (ChannelState::ChannelFunded as u32)) != (ChannelState::ChannelFunded as u32) {
                        return Err(ChannelError::Close("Got revoke/ACK message when channel was not in an operational state"));
                }
@@ -2081,7 +2117,7 @@ impl Channel {
        /// implicitly dropping) and the payment_hashes of HTLCs we tried to add but are dropping.
        /// No further message handling calls may be made until a channel_reestablish dance has
        /// completed.
-       pub fn remove_uncommitted_htlcs_and_mark_paused(&mut self) -> Vec<(HTLCSource, [u8; 32])> {
+       pub fn remove_uncommitted_htlcs_and_mark_paused(&mut self) -> Vec<(HTLCSource, PaymentHash)> {
                let mut outbound_drops = Vec::new();
 
                assert_eq!(self.channel_state & ChannelState::ShutdownComplete as u32, 0);
@@ -2151,7 +2187,7 @@ impl Channel {
        /// commitment update or a revoke_and_ack generation). The messages which were generated from
        /// that original call must *not* have been sent to the remote end, and must instead have been
        /// dropped. They will be regenerated when monitor_updating_restored is called.
-       pub fn monitor_update_failed(&mut self, order: RAACommitmentOrder) {
+       pub fn monitor_update_failed(&mut self, order: RAACommitmentOrder, mut pending_forwards: Vec<(PendingForwardHTLCInfo, u64)>, mut pending_fails: Vec<(HTLCSource, PaymentHash, HTLCFailReason)>, raa_first_dropped_cs: bool) {
                assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, 0);
                match order {
                        RAACommitmentOrder::CommitmentFirst => {
@@ -2160,9 +2196,13 @@ impl Channel {
                        },
                        RAACommitmentOrder::RevokeAndACKFirst => {
                                self.monitor_pending_revoke_and_ack = true;
-                               self.monitor_pending_commitment_signed = false;
+                               self.monitor_pending_commitment_signed = raa_first_dropped_cs;
                        },
                }
+               assert!(self.monitor_pending_forwards.is_empty());
+               mem::swap(&mut pending_forwards, &mut self.monitor_pending_forwards);
+               assert!(self.monitor_pending_failures.is_empty());
+               mem::swap(&mut pending_fails, &mut self.monitor_pending_failures);
                self.monitor_pending_order = Some(order);
                self.channel_state |= ChannelState::MonitorUpdateFailed as u32;
        }
@@ -2170,7 +2210,7 @@ impl Channel {
        /// Indicates that the latest ChannelMonitor update has been committed by the client
        /// successfully and we should restore normal operation. Returns messages which should be sent
        /// to the remote side.
-       pub fn monitor_updating_restored(&mut self) -> (Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, RAACommitmentOrder, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, [u8; 32], HTLCFailReason)>) {
+       pub fn monitor_updating_restored(&mut self) -> (Option<msgs::RevokeAndACK>, Option<msgs::CommitmentUpdate>, RAACommitmentOrder, Vec<(PendingForwardHTLCInfo, u64)>, Vec<(HTLCSource, PaymentHash, HTLCFailReason)>) {
                assert_eq!(self.channel_state & ChannelState::MonitorUpdateFailed as u32, ChannelState::MonitorUpdateFailed as u32);
                self.channel_state &= !(ChannelState::MonitorUpdateFailed as u32);
 
@@ -2286,7 +2326,8 @@ impl Channel {
                        return Err(ChannelError::Close("Peer sent a loose channel_reestablish not after reconnect"));
                }
 
-               if msg.next_local_commitment_number >= INITIAL_COMMITMENT_NUMBER || msg.next_remote_commitment_number >= INITIAL_COMMITMENT_NUMBER {
+               if msg.next_local_commitment_number >= INITIAL_COMMITMENT_NUMBER || msg.next_remote_commitment_number >= INITIAL_COMMITMENT_NUMBER ||
+                       msg.next_local_commitment_number == 0 {
                        return Err(ChannelError::Close("Peer sent a garbage channel_reestablish"));
                }
 
@@ -2301,15 +2342,15 @@ impl Channel {
                        })
                } else { None };
 
-               if self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) == ChannelState::FundingSent as u32 {
-                       // Short circuit the whole handler as there is nothing we can resend them
-                       return Ok((None, None, None, None, RAACommitmentOrder::CommitmentFirst, shutdown_msg));
-               }
-
-               if msg.next_local_commitment_number == 0 || msg.next_remote_commitment_number == 0 {
-                       if self.channel_state & (ChannelState::FundingSent as u32) != ChannelState::FundingSent as u32 {
-                               return Err(ChannelError::Close("Peer sent a pre-funding channel_reestablish after we exchanged funding_locked"));
+               if self.channel_state & (ChannelState::FundingSent as u32) == ChannelState::FundingSent as u32 {
+                       if self.channel_state & ChannelState::OurFundingLocked as u32 == 0 {
+                               if msg.next_remote_commitment_number != 0 {
+                                       return Err(ChannelError::Close("Peer claimed they saw a revoke_and_ack but we haven't sent funding_locked yet"));
+                               }
+                               // Short circuit the whole handler as there is nothing we can resend them
+                               return Ok((None, None, None, None, RAACommitmentOrder::CommitmentFirst, shutdown_msg));
                        }
+
                        // We have OurFundingLocked set!
                        let next_per_commitment_secret = self.build_local_commitment_secret(self.cur_local_commitment_transaction_number);
                        let next_per_commitment_point = PublicKey::from_secret_key(&self.secp_ctx, &next_per_commitment_secret);
@@ -2319,11 +2360,11 @@ impl Channel {
                        }), None, None, None, RAACommitmentOrder::CommitmentFirst, shutdown_msg));
                }
 
-               let required_revoke = if msg.next_remote_commitment_number == INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number {
+               let required_revoke = if msg.next_remote_commitment_number + 1 == INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number {
                        // Remote isn't waiting on any RevokeAndACK from us!
                        // Note that if we need to repeat our FundingLocked we'll do that in the next if block.
                        None
-               } else if msg.next_remote_commitment_number == (INITIAL_COMMITMENT_NUMBER - 1) - self.cur_local_commitment_transaction_number {
+               } else if msg.next_remote_commitment_number + 1 == (INITIAL_COMMITMENT_NUMBER - 1) - self.cur_local_commitment_transaction_number {
                        if self.channel_state & (ChannelState::MonitorUpdateFailed as u32) != 0 {
                                self.monitor_pending_revoke_and_ack = true;
                                None
@@ -2402,8 +2443,7 @@ impl Channel {
        fn maybe_propose_first_closing_signed(&mut self, fee_estimator: &FeeEstimator) -> Option<msgs::ClosingSigned> {
                if !self.channel_outbound || !self.pending_inbound_htlcs.is_empty() || !self.pending_outbound_htlcs.is_empty() ||
                                self.channel_state & (BOTH_SIDES_SHUTDOWN_MASK | ChannelState::AwaitingRemoteRevoke as u32) != BOTH_SIDES_SHUTDOWN_MASK ||
-                               self.last_sent_closing_fee.is_some() ||
-                               self.cur_remote_commitment_transaction_number != self.cur_local_commitment_transaction_number{
+                               self.last_sent_closing_fee.is_some() || self.pending_update_fee.is_some() {
                        return None;
                }
 
@@ -2426,7 +2466,7 @@ impl Channel {
                })
        }
 
-       pub fn shutdown(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>, Vec<(HTLCSource, [u8; 32])>), ChannelError> {
+       pub fn shutdown(&mut self, fee_estimator: &FeeEstimator, msg: &msgs::Shutdown) -> Result<(Option<msgs::Shutdown>, Option<msgs::ClosingSigned>, Vec<(HTLCSource, PaymentHash)>), ChannelError> {
                if self.channel_state & (ChannelState::PeerDisconnected as u32) == ChannelState::PeerDisconnected as u32 {
                        return Err(ChannelError::Close("Peer sent shutdown when we needed a channel_reestablish"));
                }
@@ -2947,6 +2987,7 @@ impl Channel {
                }
        }
 
+       /// If an Err is returned, it is a ChannelError::Close (for get_outbound_funding_created)
        fn get_outbound_funding_created_signature(&mut self) -> Result<(Signature, Transaction), ChannelError> {
                let funding_script = self.get_funding_redeemscript();
 
@@ -2964,6 +3005,7 @@ impl Channel {
        /// or if called on an inbound channel.
        /// Note that channel_id changes during this call!
        /// Do NOT broadcast the funding transaction until after a successful funding_signed call!
+       /// If an Err is returned, it is a ChannelError::Close.
        pub fn get_outbound_funding_created(&mut self, funding_txo: OutPoint) -> Result<(msgs::FundingCreated, ChannelMonitor), ChannelError> {
                if !self.channel_outbound {
                        panic!("Tried to create outbound funding_created message on an inbound channel!");
@@ -2992,7 +3034,7 @@ impl Channel {
                let temporary_channel_id = self.channel_id;
 
                // Now that we're past error-generating stuff, update our local state:
-               self.channel_monitor.provide_latest_remote_commitment_tx_info(&commitment_tx, Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
+               self.channel_monitor.provide_latest_remote_commitment_tx_info(&commitment_tx, Vec::new(), Vec::new(), self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
                self.channel_state = ChannelState::FundingCreated as u32;
                self.channel_id = funding_txo.to_channel_id();
                self.cur_remote_commitment_transaction_number -= 1;
@@ -3048,11 +3090,27 @@ impl Channel {
        /// self.remove_uncommitted_htlcs_and_mark_paused()'d
        pub fn get_channel_reestablish(&self) -> msgs::ChannelReestablish {
                assert_eq!(self.channel_state & ChannelState::PeerDisconnected as u32, ChannelState::PeerDisconnected as u32);
+               assert_ne!(self.cur_remote_commitment_transaction_number, INITIAL_COMMITMENT_NUMBER);
                msgs::ChannelReestablish {
                        channel_id: self.channel_id(),
+                       // The protocol has two different commitment number concepts - the "commitment
+                       // transaction number", which starts from 0 and counts up, and the "revocation key
+                       // index" which starts at INITIAL_COMMITMENT_NUMBER and counts down. We track
+                       // commitment transaction numbers by the index which will be used to reveal the
+                       // revocation key for that commitment transaction, which means we have to convert them
+                       // to protocol-level commitment numbers here...
+
+                       // next_local_commitment_number is the next commitment_signed number we expect to
+                       // receive (indicating if they need to resend one that we missed).
                        next_local_commitment_number: INITIAL_COMMITMENT_NUMBER - self.cur_local_commitment_transaction_number,
-                       next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.cur_remote_commitment_transaction_number -
-                               if self.channel_state & (ChannelState::FundingSent as u32 | ChannelState::OurFundingLocked as u32) == (ChannelState::FundingSent as u32) { 1 } else { 0 },
+                       // We have to set next_remote_commitment_number to the next revoke_and_ack we expect to
+                       // receive, however we track it by the next commitment number for a remote transaction
+                       // (which is one further, as they always revoke previous commitment transaction, not
+                       // the one we send) so we have to decrement by 1. Note that if
+                       // cur_remote_commitment_transaction_number is INITIAL_COMMITMENT_NUMBER we will have
+                       // dropped this channel on disconnect as it hasn't yet reached FundingSent so we can't
+                       // overflow here.
+                       next_remote_commitment_number: INITIAL_COMMITMENT_NUMBER - self.cur_remote_commitment_transaction_number - 1,
                        data_loss_protect: None,
                }
        }
@@ -3066,7 +3124,7 @@ impl Channel {
        /// waiting on the remote peer to send us a revoke_and_ack during which time we cannot add new
        /// HTLCs on the wire or we wouldn't be able to determine what they actually ACK'ed.
        /// You MUST call send_commitment prior to any other calls on this Channel
-       pub fn send_htlc(&mut self, amount_msat: u64, payment_hash: [u8; 32], cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket) -> Result<Option<msgs::UpdateAddHTLC>, ChannelError> {
+       pub fn send_htlc(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket) -> Result<Option<msgs::UpdateAddHTLC>, ChannelError> {
                if (self.channel_state & (ChannelState::ChannelFunded as u32 | BOTH_SIDES_SHUTDOWN_MASK)) != (ChannelState::ChannelFunded as u32) {
                        return Err(ChannelError::Ignore("Cannot send HTLC until channel is fully established and we haven't started shutting down"));
                }
@@ -3201,20 +3259,23 @@ impl Channel {
                        }
                }
 
-               match self.send_commitment_no_state_update() {
-                       Ok((res, remote_commitment_tx)) => {
+               let (res, remote_commitment_tx, htlcs, htlc_sources) = match self.send_commitment_no_state_update() {
+                       Ok((res, (remote_commitment_tx, htlcs, mut htlc_sources))) => {
                                // Update state now that we've passed all the can-fail calls...
-                               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx.0, remote_commitment_tx.1, self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
-                               self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
-                               Ok((res, self.channel_monitor.clone()))
+                               let htlc_sources_no_ref = htlc_sources.drain(..).map(|htlc_source| (htlc_source.0, htlc_source.1.clone(), htlc_source.2)).collect();
+                               (res, remote_commitment_tx, htlcs, htlc_sources_no_ref)
                        },
-                       Err(e) => Err(e),
-               }
+                       Err(e) => return Err(e),
+               };
+
+               self.channel_monitor.provide_latest_remote_commitment_tx_info(&remote_commitment_tx, htlcs, htlc_sources, self.cur_remote_commitment_transaction_number, self.their_cur_commitment_point.unwrap());
+               self.channel_state |= ChannelState::AwaitingRemoteRevoke as u32;
+               Ok((res, self.channel_monitor.clone()))
        }
 
        /// Only fails in case of bad keys. Used for channel_reestablish commitment_signed generation
        /// when we shouldn't change HTLC/channel state.
-       fn send_commitment_no_state_update(&self) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<HTLCOutputInCommitment>)), ChannelError> {
+       fn send_commitment_no_state_update(&self) -> Result<(msgs::CommitmentSigned, (Transaction, Vec<HTLCOutputInCommitment>, Vec<(PaymentHash, &HTLCSource, Option<u32>)>)), ChannelError> {
                let funding_script = self.get_funding_redeemscript();
 
                let mut feerate_per_kw = self.feerate_per_kw;
@@ -3251,7 +3312,7 @@ impl Channel {
        /// to send to the remote peer in one go.
        /// Shorthand for calling send_htlc() followed by send_commitment(), see docs on those for
        /// more info.
-       pub fn send_htlc_and_commit(&mut self, amount_msat: u64, payment_hash: [u8; 32], cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket) -> Result<Option<(msgs::UpdateAddHTLC, msgs::CommitmentSigned, ChannelMonitor)>, ChannelError> {
+       pub fn send_htlc_and_commit(&mut self, amount_msat: u64, payment_hash: PaymentHash, cltv_expiry: u32, source: HTLCSource, onion_routing_packet: msgs::OnionPacket) -> Result<Option<(msgs::UpdateAddHTLC, msgs::CommitmentSigned, ChannelMonitor)>, ChannelError> {
                match self.send_htlc(amount_msat, payment_hash, cltv_expiry, source, onion_routing_packet)? {
                        Some(update_add_htlc) => {
                                let (commitment_signed, monitor_update) = self.send_commitment_no_status_check()?;
@@ -3263,7 +3324,7 @@ impl Channel {
 
        /// Begins the shutdown process, getting a message for the remote peer and returning all
        /// holding cell HTLCs for payment failure.
-       pub fn get_shutdown(&mut self) -> Result<(msgs::Shutdown, Vec<(HTLCSource, [u8; 32])>), APIError> {
+       pub fn get_shutdown(&mut self) -> Result<(msgs::Shutdown, Vec<(HTLCSource, PaymentHash)>), APIError> {
                for htlc in self.pending_outbound_htlcs.iter() {
                        if let OutboundHTLCState::LocalAnnounced(_) = htlc.state {
                                return Err(APIError::APIMisuseError{err: "Cannot begin shutdown with pending HTLCs. Process pending events first"});
@@ -3317,7 +3378,7 @@ impl Channel {
        /// those explicitly stated to be allowed after shutdown completes, eg some simple getters).
        /// Also returns the list of payment_hashes for channels which we can safely fail backwards
        /// immediately (others we will have to allow to time out).
-       pub fn force_shutdown(&mut self) -> (Vec<Transaction>, Vec<(HTLCSource, [u8; 32])>) {
+       pub fn force_shutdown(&mut self) -> (Vec<Transaction>, Vec<(HTLCSource, PaymentHash)>) {
                assert!(self.channel_state != ChannelState::ShutdownComplete as u32);
 
                // We go ahead and "free" any holding cell HTLCs or HTLCs we haven't yet committed to and
@@ -3846,7 +3907,7 @@ mod tests {
        use bitcoin::blockdata::transaction::Transaction;
        use bitcoin::blockdata::opcodes;
        use hex;
-       use ln::channelmanager::HTLCSource;
+       use ln::channelmanager::{HTLCSource, PaymentPreimage, PaymentHash};
        use ln::channel::{Channel,ChannelKeys,InboundHTLCOutput,OutboundHTLCOutput,InboundHTLCState,OutboundHTLCState,HTLCOutputInCommitment,TxCreationKeys};
        use ln::channel::MAX_FUNDING_SATOSHIS;
        use ln::chan_utils;
@@ -3858,8 +3919,8 @@ mod tests {
        use util::logger::Logger;
        use secp256k1::{Secp256k1,Message,Signature};
        use secp256k1::key::{SecretKey,PublicKey};
-       use crypto::sha2::Sha256;
-       use crypto::digest::Digest;
+       use bitcoin_hashes::sha256::Hash as Sha256;
+       use bitcoin_hashes::Hash;
        use std::sync::Arc;
 
        struct TestFeeEstimator {
@@ -3896,6 +3957,7 @@ mod tests {
                }
 
                fn get_channel_keys(&self, _inbound: bool) -> ChannelKeys { self.chan_keys.clone() }
+               fn get_session_key(&self) -> SecretKey { panic!(); }
        }
 
        #[test]
@@ -3956,7 +4018,10 @@ mod tests {
 
                macro_rules! test_commitment {
                        ( $their_sig_hex: expr, $our_sig_hex: expr, $tx_hex: expr) => {
-                               unsigned_tx = chan.build_commitment_transaction(0xffffffffffff - 42, &keys, true, false, chan.feerate_per_kw);
+                               unsigned_tx = {
+                                       let res = chan.build_commitment_transaction(0xffffffffffff - 42, &keys, true, false, chan.feerate_per_kw);
+                                       (res.0, res.1)
+                               };
                                let their_signature = Signature::from_der(&secp_ctx, &hex::decode($their_sig_hex).unwrap()[..]).unwrap();
                                let sighash = Message::from_slice(&bip143::SighashComponents::new(&unsigned_tx.0).sighash_all(&unsigned_tx.0.input[0], &chan.get_funding_redeemscript(), chan.channel_value_satoshis)[..]).unwrap();
                                secp_ctx.verify(&sighash, &their_signature, &chan.their_funding_pubkey.unwrap()).unwrap();
@@ -3978,17 +4043,12 @@ mod tests {
                                let htlc_sighash = Message::from_slice(&bip143::SighashComponents::new(&htlc_tx).sighash_all(&htlc_tx.input[0], &htlc_redeemscript, htlc.amount_msat / 1000)[..]).unwrap();
                                secp_ctx.verify(&htlc_sighash, &remote_signature, &keys.b_htlc_key).unwrap();
 
-                               let mut preimage: Option<[u8; 32]> = None;
+                               let mut preimage: Option<PaymentPreimage> = None;
                                if !htlc.offered {
                                        for i in 0..5 {
-                                               let mut sha = Sha256::new();
-                                               sha.input(&[i; 32]);
-
-                                               let mut out = [0; 32];
-                                               sha.result(&mut out);
-
+                                               let out = PaymentHash(Sha256::hash(&[i; 32]).into_inner());
                                                if out == htlc.payment_hash {
-                                                       preimage = Some([i; 32]);
+                                                       preimage = Some(PaymentPreimage([i; 32]));
                                                }
                                        }
 
@@ -4015,12 +4075,10 @@ mod tests {
                                htlc_id: 0,
                                amount_msat: 1000000,
                                cltv_expiry: 500,
-                               payment_hash: [0; 32],
+                               payment_hash: PaymentHash([0; 32]),
                                state: InboundHTLCState::Committed,
                        };
-                       let mut sha = Sha256::new();
-                       sha.input(&hex::decode("0000000000000000000000000000000000000000000000000000000000000000").unwrap());
-                       sha.result(&mut out.payment_hash);
+                       out.payment_hash.0 = Sha256::hash(&hex::decode("0000000000000000000000000000000000000000000000000000000000000000").unwrap()).into_inner();
                        out
                });
                chan.pending_inbound_htlcs.push({
@@ -4028,12 +4086,10 @@ mod tests {
                                htlc_id: 1,
                                amount_msat: 2000000,
                                cltv_expiry: 501,
-                               payment_hash: [0; 32],
+                               payment_hash: PaymentHash([0; 32]),
                                state: InboundHTLCState::Committed,
                        };
-                       let mut sha = Sha256::new();
-                       sha.input(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap());
-                       sha.result(&mut out.payment_hash);
+                       out.payment_hash.0 = Sha256::hash(&hex::decode("0101010101010101010101010101010101010101010101010101010101010101").unwrap()).into_inner();
                        out
                });
                chan.pending_outbound_htlcs.push({
@@ -4041,14 +4097,12 @@ mod tests {
                                htlc_id: 2,
                                amount_msat: 2000000,
                                cltv_expiry: 502,
-                               payment_hash: [0; 32],
+                               payment_hash: PaymentHash([0; 32]),
                                state: OutboundHTLCState::Committed,
                                source: HTLCSource::dummy(),
                                fail_reason: None,
                        };
-                       let mut sha = Sha256::new();
-                       sha.input(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap());
-                       sha.result(&mut out.payment_hash);
+                       out.payment_hash.0 = Sha256::hash(&hex::decode("0202020202020202020202020202020202020202020202020202020202020202").unwrap()).into_inner();
                        out
                });
                chan.pending_outbound_htlcs.push({
@@ -4056,14 +4110,12 @@ mod tests {
                                htlc_id: 3,
                                amount_msat: 3000000,
                                cltv_expiry: 503,
-                               payment_hash: [0; 32],
+                               payment_hash: PaymentHash([0; 32]),
                                state: OutboundHTLCState::Committed,
                                source: HTLCSource::dummy(),
                                fail_reason: None,
                        };
-                       let mut sha = Sha256::new();
-                       sha.input(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap());
-                       sha.result(&mut out.payment_hash);
+                       out.payment_hash.0 = Sha256::hash(&hex::decode("0303030303030303030303030303030303030303030303030303030303030303").unwrap()).into_inner();
                        out
                });
                chan.pending_inbound_htlcs.push({
@@ -4071,12 +4123,10 @@ mod tests {
                                htlc_id: 4,
                                amount_msat: 4000000,
                                cltv_expiry: 504,
-                               payment_hash: [0; 32],
+                               payment_hash: PaymentHash([0; 32]),
                                state: InboundHTLCState::Committed,
                        };
-                       let mut sha = Sha256::new();
-                       sha.input(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap());
-                       sha.result(&mut out.payment_hash);
+                       out.payment_hash.0 = Sha256::hash(&hex::decode("0404040404040404040404040404040404040404040404040404040404040404").unwrap()).into_inner();
                        out
                });