Merge pull request #928 from TheBlueMatt/2021-05-really-tlv-ser
[rust-lightning] / lightning / src / ln / chan_utils.rs
1 // This file is Copyright its original authors, visible in version control
2 // history.
3 //
4 // This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE
5 // or http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
6 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your option.
7 // You may not use this file except in accordance with one or both of these
8 // licenses.
9
10 //! Various utilities for building scripts and deriving keys related to channels. These are
11 //! largely of interest for those implementing chain::keysinterface::Sign message signing by hand.
12
13 use bitcoin::blockdata::script::{Script,Builder};
14 use bitcoin::blockdata::opcodes;
15 use bitcoin::blockdata::transaction::{TxIn,TxOut,OutPoint,Transaction, SigHashType};
16 use bitcoin::util::bip143;
17
18 use bitcoin::hashes::{Hash, HashEngine};
19 use bitcoin::hashes::sha256::Hash as Sha256;
20 use bitcoin::hashes::ripemd160::Hash as Ripemd160;
21 use bitcoin::hash_types::{Txid, PubkeyHash};
22
23 use ln::{PaymentHash, PaymentPreimage};
24 use ln::msgs::DecodeError;
25 use util::ser::{Readable, Writeable, Writer, MAX_BUF_SIZE};
26 use util::byte_utils;
27
28 use bitcoin::hash_types::WPubkeyHash;
29 use bitcoin::secp256k1::key::{SecretKey, PublicKey};
30 use bitcoin::secp256k1::{Secp256k1, Signature, Message};
31 use bitcoin::secp256k1::Error as SecpError;
32 use bitcoin::secp256k1;
33
34 use prelude::*;
35 use core::cmp;
36 use ln::chan_utils;
37 use util::transaction_utils::sort_outputs;
38 use ln::channel::INITIAL_COMMITMENT_NUMBER;
39 use std::io::Read;
40 use core::ops::Deref;
41 use chain;
42
43 // Maximum size of a serialized HTLCOutputInCommitment
44 pub(crate) const HTLC_OUTPUT_IN_COMMITMENT_SIZE: usize = 1 + 8 + 4 + 32 + 5;
45
46 pub(crate) const MAX_HTLCS: u16 = 483;
47
48 // This checks that the buffer size is greater than the maximum possible size for serialized HTLCS
49 const _EXCESS_BUFFER_SIZE: usize = MAX_BUF_SIZE - MAX_HTLCS as usize * HTLC_OUTPUT_IN_COMMITMENT_SIZE;
50
51 pub(super) const HTLC_SUCCESS_TX_WEIGHT: u64 = 703;
52 pub(super) const HTLC_TIMEOUT_TX_WEIGHT: u64 = 663;
53
54 #[derive(PartialEq)]
55 pub(crate) enum HTLCType {
56         AcceptedHTLC,
57         OfferedHTLC
58 }
59
60 impl HTLCType {
61         /// Check if a given tx witnessScript len matchs one of a pre-signed HTLC
62         pub(crate) fn scriptlen_to_htlctype(witness_script_len: usize) ->  Option<HTLCType> {
63                 if witness_script_len == 133 {
64                         Some(HTLCType::OfferedHTLC)
65                 } else if witness_script_len >= 136 && witness_script_len <= 139 {
66                         Some(HTLCType::AcceptedHTLC)
67                 } else {
68                         None
69                 }
70         }
71 }
72
73 // Various functions for key derivation and transaction creation for use within channels. Primarily
74 // used in Channel and ChannelMonitor.
75
76 /// Build the commitment secret from the seed and the commitment number
77 pub fn build_commitment_secret(commitment_seed: &[u8; 32], idx: u64) -> [u8; 32] {
78         let mut res: [u8; 32] = commitment_seed.clone();
79         for i in 0..48 {
80                 let bitpos = 47 - i;
81                 if idx & (1 << bitpos) == (1 << bitpos) {
82                         res[bitpos / 8] ^= 1 << (bitpos & 7);
83                         res = Sha256::hash(&res).into_inner();
84                 }
85         }
86         res
87 }
88
89 /// Implements the per-commitment secret storage scheme from
90 /// [BOLT 3](https://github.com/lightningnetwork/lightning-rfc/blob/dcbf8583976df087c79c3ce0b535311212e6812d/03-transactions.md#efficient-per-commitment-secret-storage).
91 ///
92 /// Allows us to keep track of all of the revocation secrets of counterarties in just 50*32 bytes
93 /// or so.
94 #[derive(Clone)]
95 pub(crate) struct CounterpartyCommitmentSecrets {
96         old_secrets: [([u8; 32], u64); 49],
97 }
98
99 impl PartialEq for CounterpartyCommitmentSecrets {
100         fn eq(&self, other: &Self) -> bool {
101                 for (&(ref secret, ref idx), &(ref o_secret, ref o_idx)) in self.old_secrets.iter().zip(other.old_secrets.iter()) {
102                         if secret != o_secret || idx != o_idx {
103                                 return false
104                         }
105                 }
106                 true
107         }
108 }
109
110 impl CounterpartyCommitmentSecrets {
111         pub(crate) fn new() -> Self {
112                 Self { old_secrets: [([0; 32], 1 << 48); 49], }
113         }
114
115         #[inline]
116         fn place_secret(idx: u64) -> u8 {
117                 for i in 0..48 {
118                         if idx & (1 << i) == (1 << i) {
119                                 return i
120                         }
121                 }
122                 48
123         }
124
125         pub(crate) fn get_min_seen_secret(&self) -> u64 {
126                 //TODO This can be optimized?
127                 let mut min = 1 << 48;
128                 for &(_, idx) in self.old_secrets.iter() {
129                         if idx < min {
130                                 min = idx;
131                         }
132                 }
133                 min
134         }
135
136         #[inline]
137         fn derive_secret(secret: [u8; 32], bits: u8, idx: u64) -> [u8; 32] {
138                 let mut res: [u8; 32] = secret;
139                 for i in 0..bits {
140                         let bitpos = bits - 1 - i;
141                         if idx & (1 << bitpos) == (1 << bitpos) {
142                                 res[(bitpos / 8) as usize] ^= 1 << (bitpos & 7);
143                                 res = Sha256::hash(&res).into_inner();
144                         }
145                 }
146                 res
147         }
148
149         pub(crate) fn provide_secret(&mut self, idx: u64, secret: [u8; 32]) -> Result<(), ()> {
150                 let pos = Self::place_secret(idx);
151                 for i in 0..pos {
152                         let (old_secret, old_idx) = self.old_secrets[i as usize];
153                         if Self::derive_secret(secret, pos, old_idx) != old_secret {
154                                 return Err(());
155                         }
156                 }
157                 if self.get_min_seen_secret() <= idx {
158                         return Ok(());
159                 }
160                 self.old_secrets[pos as usize] = (secret, idx);
161                 Ok(())
162         }
163
164         /// Can only fail if idx is < get_min_seen_secret
165         pub(crate) fn get_secret(&self, idx: u64) -> Option<[u8; 32]> {
166                 for i in 0..self.old_secrets.len() {
167                         if (idx & (!((1 << i) - 1))) == self.old_secrets[i].1 {
168                                 return Some(Self::derive_secret(self.old_secrets[i].0, i as u8, idx))
169                         }
170                 }
171                 assert!(idx < self.get_min_seen_secret());
172                 None
173         }
174 }
175
176 impl Writeable for CounterpartyCommitmentSecrets {
177         fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
178                 for &(ref secret, ref idx) in self.old_secrets.iter() {
179                         writer.write_all(secret)?;
180                         writer.write_all(&byte_utils::be64_to_array(*idx))?;
181                 }
182                 write_tlv_fields!(writer, {}, {});
183                 Ok(())
184         }
185 }
186 impl Readable for CounterpartyCommitmentSecrets {
187         fn read<R: ::std::io::Read>(reader: &mut R) -> Result<Self, DecodeError> {
188                 let mut old_secrets = [([0; 32], 1 << 48); 49];
189                 for &mut (ref mut secret, ref mut idx) in old_secrets.iter_mut() {
190                         *secret = Readable::read(reader)?;
191                         *idx = Readable::read(reader)?;
192                 }
193                 read_tlv_fields!(reader, {}, {});
194                 Ok(Self { old_secrets })
195         }
196 }
197
198 /// Derives a per-commitment-transaction private key (eg an htlc key or delayed_payment key)
199 /// from the base secret and the per_commitment_point.
200 ///
201 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
202 /// generated (ie our own).
203 pub fn derive_private_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_secret: &SecretKey) -> Result<SecretKey, SecpError> {
204         let mut sha = Sha256::engine();
205         sha.input(&per_commitment_point.serialize());
206         sha.input(&PublicKey::from_secret_key(&secp_ctx, &base_secret).serialize());
207         let res = Sha256::from_engine(sha).into_inner();
208
209         let mut key = base_secret.clone();
210         key.add_assign(&res)?;
211         Ok(key)
212 }
213
214 /// Derives a per-commitment-transaction public key (eg an htlc key or a delayed_payment key)
215 /// from the base point and the per_commitment_key. This is the public equivalent of
216 /// derive_private_key - using only public keys to derive a public key instead of private keys.
217 ///
218 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
219 /// generated (ie our own).
220 pub fn derive_public_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, base_point: &PublicKey) -> Result<PublicKey, SecpError> {
221         let mut sha = Sha256::engine();
222         sha.input(&per_commitment_point.serialize());
223         sha.input(&base_point.serialize());
224         let res = Sha256::from_engine(sha).into_inner();
225
226         let hashkey = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&res)?);
227         base_point.combine(&hashkey)
228 }
229
230 /// Derives a per-commitment-transaction revocation key from its constituent parts.
231 ///
232 /// Only the cheating participant owns a valid witness to propagate a revoked 
233 /// commitment transaction, thus per_commitment_secret always come from cheater
234 /// and revocation_base_secret always come from punisher, which is the broadcaster
235 /// of the transaction spending with this key knowledge.
236 ///
237 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
238 /// generated (ie our own).
239 pub fn derive_private_revocation_key<T: secp256k1::Signing>(secp_ctx: &Secp256k1<T>, per_commitment_secret: &SecretKey, countersignatory_revocation_base_secret: &SecretKey) -> Result<SecretKey, SecpError> {
240         let countersignatory_revocation_base_point = PublicKey::from_secret_key(&secp_ctx, &countersignatory_revocation_base_secret);
241         let per_commitment_point = PublicKey::from_secret_key(&secp_ctx, &per_commitment_secret);
242
243         let rev_append_commit_hash_key = {
244                 let mut sha = Sha256::engine();
245                 sha.input(&countersignatory_revocation_base_point.serialize());
246                 sha.input(&per_commitment_point.serialize());
247
248                 Sha256::from_engine(sha).into_inner()
249         };
250         let commit_append_rev_hash_key = {
251                 let mut sha = Sha256::engine();
252                 sha.input(&per_commitment_point.serialize());
253                 sha.input(&countersignatory_revocation_base_point.serialize());
254
255                 Sha256::from_engine(sha).into_inner()
256         };
257
258         let mut countersignatory_contrib = countersignatory_revocation_base_secret.clone();
259         countersignatory_contrib.mul_assign(&rev_append_commit_hash_key)?;
260         let mut broadcaster_contrib = per_commitment_secret.clone();
261         broadcaster_contrib.mul_assign(&commit_append_rev_hash_key)?;
262         countersignatory_contrib.add_assign(&broadcaster_contrib[..])?;
263         Ok(countersignatory_contrib)
264 }
265
266 /// Derives a per-commitment-transaction revocation public key from its constituent parts. This is
267 /// the public equivalend of derive_private_revocation_key - using only public keys to derive a
268 /// public key instead of private keys.
269 ///
270 /// Only the cheating participant owns a valid witness to propagate a revoked 
271 /// commitment transaction, thus per_commitment_point always come from cheater
272 /// and revocation_base_point always come from punisher, which is the broadcaster
273 /// of the transaction spending with this key knowledge.
274 ///
275 /// Note that this is infallible iff we trust that at least one of the two input keys are randomly
276 /// generated (ie our own).
277 pub fn derive_public_revocation_key<T: secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, countersignatory_revocation_base_point: &PublicKey) -> Result<PublicKey, SecpError> {
278         let rev_append_commit_hash_key = {
279                 let mut sha = Sha256::engine();
280                 sha.input(&countersignatory_revocation_base_point.serialize());
281                 sha.input(&per_commitment_point.serialize());
282
283                 Sha256::from_engine(sha).into_inner()
284         };
285         let commit_append_rev_hash_key = {
286                 let mut sha = Sha256::engine();
287                 sha.input(&per_commitment_point.serialize());
288                 sha.input(&countersignatory_revocation_base_point.serialize());
289
290                 Sha256::from_engine(sha).into_inner()
291         };
292
293         let mut countersignatory_contrib = countersignatory_revocation_base_point.clone();
294         countersignatory_contrib.mul_assign(&secp_ctx, &rev_append_commit_hash_key)?;
295         let mut broadcaster_contrib = per_commitment_point.clone();
296         broadcaster_contrib.mul_assign(&secp_ctx, &commit_append_rev_hash_key)?;
297         countersignatory_contrib.combine(&broadcaster_contrib)
298 }
299
300 /// The set of public keys which are used in the creation of one commitment transaction.
301 /// These are derived from the channel base keys and per-commitment data.
302 ///
303 /// A broadcaster key is provided from potential broadcaster of the computed transaction.
304 /// A countersignatory key is coming from a protocol participant unable to broadcast the
305 /// transaction.
306 ///
307 /// These keys are assumed to be good, either because the code derived them from
308 /// channel basepoints via the new function, or they were obtained via
309 /// CommitmentTransaction.trust().keys() because we trusted the source of the
310 /// pre-calculated keys.
311 #[derive(PartialEq, Clone)]
312 pub struct TxCreationKeys {
313         /// The broadcaster's per-commitment public key which was used to derive the other keys.
314         pub per_commitment_point: PublicKey,
315         /// The revocation key which is used to allow the broadcaster of the commitment
316         /// transaction to provide their counterparty the ability to punish them if they broadcast
317         /// an old state.
318         pub revocation_key: PublicKey,
319         /// Broadcaster's HTLC Key
320         pub broadcaster_htlc_key: PublicKey,
321         /// Countersignatory's HTLC Key
322         pub countersignatory_htlc_key: PublicKey,
323         /// Broadcaster's Payment Key (which isn't allowed to be spent from for some delay)
324         pub broadcaster_delayed_payment_key: PublicKey,
325 }
326
327 impl_writeable_tlv_based!(TxCreationKeys, {
328         (0, per_commitment_point),
329         (2, revocation_key),
330         (4, broadcaster_htlc_key),
331         (6, countersignatory_htlc_key),
332         (8, broadcaster_delayed_payment_key),
333 }, {}, {});
334
335 /// One counterparty's public keys which do not change over the life of a channel.
336 #[derive(Clone, PartialEq)]
337 pub struct ChannelPublicKeys {
338         /// The public key which is used to sign all commitment transactions, as it appears in the
339         /// on-chain channel lock-in 2-of-2 multisig output.
340         pub funding_pubkey: PublicKey,
341         /// The base point which is used (with derive_public_revocation_key) to derive per-commitment
342         /// revocation keys. This is combined with the per-commitment-secret generated by the
343         /// counterparty to create a secret which the counterparty can reveal to revoke previous
344         /// states.
345         pub revocation_basepoint: PublicKey,
346         /// The public key on which the non-broadcaster (ie the countersignatory) receives an immediately
347         /// spendable primary channel balance on the broadcaster's commitment transaction. This key is
348         /// static across every commitment transaction.
349         pub payment_point: PublicKey,
350         /// The base point which is used (with derive_public_key) to derive a per-commitment payment
351         /// public key which receives non-HTLC-encumbered funds which are only available for spending
352         /// after some delay (or can be claimed via the revocation path).
353         pub delayed_payment_basepoint: PublicKey,
354         /// The base point which is used (with derive_public_key) to derive a per-commitment public key
355         /// which is used to encumber HTLC-in-flight outputs.
356         pub htlc_basepoint: PublicKey,
357 }
358
359 impl_writeable_tlv_based!(ChannelPublicKeys, {
360         (0, funding_pubkey),
361         (2, revocation_basepoint),
362         (4, payment_point),
363         (6, delayed_payment_basepoint),
364         (8, htlc_basepoint),
365 }, {}, {});
366
367 impl TxCreationKeys {
368         /// Create per-state keys from channel base points and the per-commitment point.
369         /// Key set is asymmetric and can't be used as part of counter-signatory set of transactions.
370         pub fn derive_new<T: secp256k1::Signing + secp256k1::Verification>(secp_ctx: &Secp256k1<T>, per_commitment_point: &PublicKey, broadcaster_delayed_payment_base: &PublicKey, broadcaster_htlc_base: &PublicKey, countersignatory_revocation_base: &PublicKey, countersignatory_htlc_base: &PublicKey) -> Result<TxCreationKeys, SecpError> {
371                 Ok(TxCreationKeys {
372                         per_commitment_point: per_commitment_point.clone(),
373                         revocation_key: derive_public_revocation_key(&secp_ctx, &per_commitment_point, &countersignatory_revocation_base)?,
374                         broadcaster_htlc_key: derive_public_key(&secp_ctx, &per_commitment_point, &broadcaster_htlc_base)?,
375                         countersignatory_htlc_key: derive_public_key(&secp_ctx, &per_commitment_point, &countersignatory_htlc_base)?,
376                         broadcaster_delayed_payment_key: derive_public_key(&secp_ctx, &per_commitment_point, &broadcaster_delayed_payment_base)?,
377                 })
378         }
379
380         /// Generate per-state keys from channel static keys.
381         /// Key set is asymmetric and can't be used as part of counter-signatory set of transactions.
382         pub fn from_channel_static_keys<T: secp256k1::Signing + secp256k1::Verification>(per_commitment_point: &PublicKey, broadcaster_keys: &ChannelPublicKeys, countersignatory_keys: &ChannelPublicKeys, secp_ctx: &Secp256k1<T>) -> Result<TxCreationKeys, SecpError> {
383                 TxCreationKeys::derive_new(
384                         &secp_ctx,
385                         &per_commitment_point,
386                         &broadcaster_keys.delayed_payment_basepoint,
387                         &broadcaster_keys.htlc_basepoint,
388                         &countersignatory_keys.revocation_basepoint,
389                         &countersignatory_keys.htlc_basepoint,
390                 )
391         }
392 }
393
394 /// The maximum length of a script returned by get_revokeable_redeemscript.
395 // Calculated as 6 bytes of opcodes, 1 byte push plus 2 bytes for contest_delay, and two public
396 // keys of 33 bytes (+ 1 push).
397 pub const REVOKEABLE_REDEEMSCRIPT_MAX_LENGTH: usize = 6 + 3 + 34*2;
398
399 /// A script either spendable by the revocation
400 /// key or the broadcaster_delayed_payment_key and satisfying the relative-locktime OP_CSV constrain.
401 /// Encumbering a `to_holder` output on a commitment transaction or 2nd-stage HTLC transactions.
402 pub fn get_revokeable_redeemscript(revocation_key: &PublicKey, contest_delay: u16, broadcaster_delayed_payment_key: &PublicKey) -> Script {
403         let res = Builder::new().push_opcode(opcodes::all::OP_IF)
404                       .push_slice(&revocation_key.serialize())
405                       .push_opcode(opcodes::all::OP_ELSE)
406                       .push_int(contest_delay as i64)
407                       .push_opcode(opcodes::all::OP_CSV)
408                       .push_opcode(opcodes::all::OP_DROP)
409                       .push_slice(&broadcaster_delayed_payment_key.serialize())
410                       .push_opcode(opcodes::all::OP_ENDIF)
411                       .push_opcode(opcodes::all::OP_CHECKSIG)
412                       .into_script();
413         debug_assert!(res.len() <= REVOKEABLE_REDEEMSCRIPT_MAX_LENGTH);
414         res
415 }
416
417 #[derive(Clone, PartialEq)]
418 /// Information about an HTLC as it appears in a commitment transaction
419 pub struct HTLCOutputInCommitment {
420         /// Whether the HTLC was "offered" (ie outbound in relation to this commitment transaction).
421         /// Note that this is not the same as whether it is ountbound *from us*. To determine that you
422         /// need to compare this value to whether the commitment transaction in question is that of
423         /// the counterparty or our own.
424         pub offered: bool,
425         /// The value, in msat, of the HTLC. The value as it appears in the commitment transaction is
426         /// this divided by 1000.
427         pub amount_msat: u64,
428         /// The CLTV lock-time at which this HTLC expires.
429         pub cltv_expiry: u32,
430         /// The hash of the preimage which unlocks this HTLC.
431         pub payment_hash: PaymentHash,
432         /// The position within the commitment transactions' outputs. This may be None if the value is
433         /// below the dust limit (in which case no output appears in the commitment transaction and the
434         /// value is spent to additional transaction fees).
435         pub transaction_output_index: Option<u32>,
436 }
437
438 impl_writeable_tlv_based!(HTLCOutputInCommitment, {
439         (0, offered),
440         (2, amount_msat),
441         (4, cltv_expiry),
442         (6, payment_hash),
443 }, {
444         (8, transaction_output_index)
445 }, {});
446
447 #[inline]
448 pub(crate) fn get_htlc_redeemscript_with_explicit_keys(htlc: &HTLCOutputInCommitment, broadcaster_htlc_key: &PublicKey, countersignatory_htlc_key: &PublicKey, revocation_key: &PublicKey) -> Script {
449         let payment_hash160 = Ripemd160::hash(&htlc.payment_hash.0[..]).into_inner();
450         if htlc.offered {
451                 Builder::new().push_opcode(opcodes::all::OP_DUP)
452                               .push_opcode(opcodes::all::OP_HASH160)
453                               .push_slice(&PubkeyHash::hash(&revocation_key.serialize())[..])
454                               .push_opcode(opcodes::all::OP_EQUAL)
455                               .push_opcode(opcodes::all::OP_IF)
456                               .push_opcode(opcodes::all::OP_CHECKSIG)
457                               .push_opcode(opcodes::all::OP_ELSE)
458                               .push_slice(&countersignatory_htlc_key.serialize()[..])
459                               .push_opcode(opcodes::all::OP_SWAP)
460                               .push_opcode(opcodes::all::OP_SIZE)
461                               .push_int(32)
462                               .push_opcode(opcodes::all::OP_EQUAL)
463                               .push_opcode(opcodes::all::OP_NOTIF)
464                               .push_opcode(opcodes::all::OP_DROP)
465                               .push_int(2)
466                               .push_opcode(opcodes::all::OP_SWAP)
467                               .push_slice(&broadcaster_htlc_key.serialize()[..])
468                               .push_int(2)
469                               .push_opcode(opcodes::all::OP_CHECKMULTISIG)
470                               .push_opcode(opcodes::all::OP_ELSE)
471                               .push_opcode(opcodes::all::OP_HASH160)
472                               .push_slice(&payment_hash160)
473                               .push_opcode(opcodes::all::OP_EQUALVERIFY)
474                               .push_opcode(opcodes::all::OP_CHECKSIG)
475                               .push_opcode(opcodes::all::OP_ENDIF)
476                               .push_opcode(opcodes::all::OP_ENDIF)
477                               .into_script()
478         } else {
479                 Builder::new().push_opcode(opcodes::all::OP_DUP)
480                               .push_opcode(opcodes::all::OP_HASH160)
481                               .push_slice(&PubkeyHash::hash(&revocation_key.serialize())[..])
482                               .push_opcode(opcodes::all::OP_EQUAL)
483                               .push_opcode(opcodes::all::OP_IF)
484                               .push_opcode(opcodes::all::OP_CHECKSIG)
485                               .push_opcode(opcodes::all::OP_ELSE)
486                               .push_slice(&countersignatory_htlc_key.serialize()[..])
487                               .push_opcode(opcodes::all::OP_SWAP)
488                               .push_opcode(opcodes::all::OP_SIZE)
489                               .push_int(32)
490                               .push_opcode(opcodes::all::OP_EQUAL)
491                               .push_opcode(opcodes::all::OP_IF)
492                               .push_opcode(opcodes::all::OP_HASH160)
493                               .push_slice(&payment_hash160)
494                               .push_opcode(opcodes::all::OP_EQUALVERIFY)
495                               .push_int(2)
496                               .push_opcode(opcodes::all::OP_SWAP)
497                               .push_slice(&broadcaster_htlc_key.serialize()[..])
498                               .push_int(2)
499                               .push_opcode(opcodes::all::OP_CHECKMULTISIG)
500                               .push_opcode(opcodes::all::OP_ELSE)
501                               .push_opcode(opcodes::all::OP_DROP)
502                               .push_int(htlc.cltv_expiry as i64)
503                               .push_opcode(opcodes::all::OP_CLTV)
504                               .push_opcode(opcodes::all::OP_DROP)
505                               .push_opcode(opcodes::all::OP_CHECKSIG)
506                               .push_opcode(opcodes::all::OP_ENDIF)
507                               .push_opcode(opcodes::all::OP_ENDIF)
508                               .into_script()
509         }
510 }
511
512 /// Gets the witness redeemscript for an HTLC output in a commitment transaction. Note that htlc
513 /// does not need to have its previous_output_index filled.
514 #[inline]
515 pub fn get_htlc_redeemscript(htlc: &HTLCOutputInCommitment, keys: &TxCreationKeys) -> Script {
516         get_htlc_redeemscript_with_explicit_keys(htlc, &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key)
517 }
518
519 /// Gets the redeemscript for a funding output from the two funding public keys.
520 /// Note that the order of funding public keys does not matter.
521 pub fn make_funding_redeemscript(broadcaster: &PublicKey, countersignatory: &PublicKey) -> Script {
522         let broadcaster_funding_key = broadcaster.serialize();
523         let countersignatory_funding_key = countersignatory.serialize();
524
525         let builder = Builder::new().push_opcode(opcodes::all::OP_PUSHNUM_2);
526         if broadcaster_funding_key[..] < countersignatory_funding_key[..] {
527                 builder.push_slice(&broadcaster_funding_key)
528                         .push_slice(&countersignatory_funding_key)
529         } else {
530                 builder.push_slice(&countersignatory_funding_key)
531                         .push_slice(&broadcaster_funding_key)
532         }.push_opcode(opcodes::all::OP_PUSHNUM_2).push_opcode(opcodes::all::OP_CHECKMULTISIG).into_script()
533 }
534
535 /// panics if htlc.transaction_output_index.is_none()!
536 pub fn build_htlc_transaction(prev_hash: &Txid, feerate_per_kw: u32, contest_delay: u16, htlc: &HTLCOutputInCommitment, broadcaster_delayed_payment_key: &PublicKey, revocation_key: &PublicKey) -> Transaction {
537         let mut txins: Vec<TxIn> = Vec::new();
538         txins.push(TxIn {
539                 previous_output: OutPoint {
540                         txid: prev_hash.clone(),
541                         vout: htlc.transaction_output_index.expect("Can't build an HTLC transaction for a dust output"),
542                 },
543                 script_sig: Script::new(),
544                 sequence: 0,
545                 witness: Vec::new(),
546         });
547
548         let total_fee = if htlc.offered {
549                         feerate_per_kw as u64 * HTLC_TIMEOUT_TX_WEIGHT / 1000
550                 } else {
551                         feerate_per_kw as u64 * HTLC_SUCCESS_TX_WEIGHT / 1000
552                 };
553
554         let mut txouts: Vec<TxOut> = Vec::new();
555         txouts.push(TxOut {
556                 script_pubkey: get_revokeable_redeemscript(revocation_key, contest_delay, broadcaster_delayed_payment_key).to_v0_p2wsh(),
557                 value: htlc.amount_msat / 1000 - total_fee //TODO: BOLT 3 does not specify if we should add amount_msat before dividing or if we should divide by 1000 before subtracting (as we do here)
558         });
559
560         Transaction {
561                 version: 2,
562                 lock_time: if htlc.offered { htlc.cltv_expiry } else { 0 },
563                 input: txins,
564                 output: txouts,
565         }
566 }
567
568 /// Per-channel data used to build transactions in conjunction with the per-commitment data (CommitmentTransaction).
569 /// The fields are organized by holder/counterparty.
570 ///
571 /// Normally, this is converted to the broadcaster/countersignatory-organized DirectedChannelTransactionParameters
572 /// before use, via the as_holder_broadcastable and as_counterparty_broadcastable functions.
573 #[derive(Clone)]
574 pub struct ChannelTransactionParameters {
575         /// Holder public keys
576         pub holder_pubkeys: ChannelPublicKeys,
577         /// The contest delay selected by the holder, which applies to counterparty-broadcast transactions
578         pub holder_selected_contest_delay: u16,
579         /// Whether the holder is the initiator of this channel.
580         /// This is an input to the commitment number obscure factor computation.
581         pub is_outbound_from_holder: bool,
582         /// The late-bound counterparty channel transaction parameters.
583         /// These parameters are populated at the point in the protocol where the counterparty provides them.
584         pub counterparty_parameters: Option<CounterpartyChannelTransactionParameters>,
585         /// The late-bound funding outpoint
586         pub funding_outpoint: Option<chain::transaction::OutPoint>,
587 }
588
589 /// Late-bound per-channel counterparty data used to build transactions.
590 #[derive(Clone)]
591 pub struct CounterpartyChannelTransactionParameters {
592         /// Counter-party public keys
593         pub pubkeys: ChannelPublicKeys,
594         /// The contest delay selected by the counterparty, which applies to holder-broadcast transactions
595         pub selected_contest_delay: u16,
596 }
597
598 impl ChannelTransactionParameters {
599         /// Whether the late bound parameters are populated.
600         pub fn is_populated(&self) -> bool {
601                 self.counterparty_parameters.is_some() && self.funding_outpoint.is_some()
602         }
603
604         /// Convert the holder/counterparty parameters to broadcaster/countersignatory-organized parameters,
605         /// given that the holder is the broadcaster.
606         ///
607         /// self.is_populated() must be true before calling this function.
608         pub fn as_holder_broadcastable(&self) -> DirectedChannelTransactionParameters {
609                 assert!(self.is_populated(), "self.late_parameters must be set before using as_holder_broadcastable");
610                 DirectedChannelTransactionParameters {
611                         inner: self,
612                         holder_is_broadcaster: true
613                 }
614         }
615
616         /// Convert the holder/counterparty parameters to broadcaster/countersignatory-organized parameters,
617         /// given that the counterparty is the broadcaster.
618         ///
619         /// self.is_populated() must be true before calling this function.
620         pub fn as_counterparty_broadcastable(&self) -> DirectedChannelTransactionParameters {
621                 assert!(self.is_populated(), "self.late_parameters must be set before using as_counterparty_broadcastable");
622                 DirectedChannelTransactionParameters {
623                         inner: self,
624                         holder_is_broadcaster: false
625                 }
626         }
627 }
628
629 impl_writeable_tlv_based!(CounterpartyChannelTransactionParameters, {
630         (0, pubkeys),
631         (2, selected_contest_delay),
632 }, {}, {});
633
634 impl_writeable_tlv_based!(ChannelTransactionParameters, {
635         (0, holder_pubkeys),
636         (2, holder_selected_contest_delay),
637         (4, is_outbound_from_holder),
638 }, {
639         (6, counterparty_parameters),
640         (8, funding_outpoint),
641 }, {});
642
643 /// Static channel fields used to build transactions given per-commitment fields, organized by
644 /// broadcaster/countersignatory.
645 ///
646 /// This is derived from the holder/counterparty-organized ChannelTransactionParameters via the
647 /// as_holder_broadcastable and as_counterparty_broadcastable functions.
648 pub struct DirectedChannelTransactionParameters<'a> {
649         /// The holder's channel static parameters
650         inner: &'a ChannelTransactionParameters,
651         /// Whether the holder is the broadcaster
652         holder_is_broadcaster: bool,
653 }
654
655 impl<'a> DirectedChannelTransactionParameters<'a> {
656         /// Get the channel pubkeys for the broadcaster
657         pub fn broadcaster_pubkeys(&self) -> &ChannelPublicKeys {
658                 if self.holder_is_broadcaster {
659                         &self.inner.holder_pubkeys
660                 } else {
661                         &self.inner.counterparty_parameters.as_ref().unwrap().pubkeys
662                 }
663         }
664
665         /// Get the channel pubkeys for the countersignatory
666         pub fn countersignatory_pubkeys(&self) -> &ChannelPublicKeys {
667                 if self.holder_is_broadcaster {
668                         &self.inner.counterparty_parameters.as_ref().unwrap().pubkeys
669                 } else {
670                         &self.inner.holder_pubkeys
671                 }
672         }
673
674         /// Get the contest delay applicable to the transactions.
675         /// Note that the contest delay was selected by the countersignatory.
676         pub fn contest_delay(&self) -> u16 {
677                 let counterparty_parameters = self.inner.counterparty_parameters.as_ref().unwrap();
678                 if self.holder_is_broadcaster { counterparty_parameters.selected_contest_delay } else { self.inner.holder_selected_contest_delay }
679         }
680
681         /// Whether the channel is outbound from the broadcaster.
682         ///
683         /// The boolean representing the side that initiated the channel is
684         /// an input to the commitment number obscure factor computation.
685         pub fn is_outbound(&self) -> bool {
686                 if self.holder_is_broadcaster { self.inner.is_outbound_from_holder } else { !self.inner.is_outbound_from_holder }
687         }
688
689         /// The funding outpoint
690         pub fn funding_outpoint(&self) -> OutPoint {
691                 self.inner.funding_outpoint.unwrap().into_bitcoin_outpoint()
692         }
693 }
694
695 /// Information needed to build and sign a holder's commitment transaction.
696 ///
697 /// The transaction is only signed once we are ready to broadcast.
698 #[derive(Clone)]
699 pub struct HolderCommitmentTransaction {
700         inner: CommitmentTransaction,
701         /// Our counterparty's signature for the transaction
702         pub counterparty_sig: Signature,
703         /// All non-dust counterparty HTLC signatures, in the order they appear in the transaction
704         pub counterparty_htlc_sigs: Vec<Signature>,
705         // Which order the signatures should go in when constructing the final commitment tx witness.
706         // The user should be able to reconstruct this themselves, so we don't bother to expose it.
707         holder_sig_first: bool,
708 }
709
710 impl Deref for HolderCommitmentTransaction {
711         type Target = CommitmentTransaction;
712
713         fn deref(&self) -> &Self::Target { &self.inner }
714 }
715
716 impl PartialEq for HolderCommitmentTransaction {
717         // We dont care whether we are signed in equality comparison
718         fn eq(&self, o: &Self) -> bool {
719                 self.inner == o.inner
720         }
721 }
722
723 impl_writeable_tlv_based!(HolderCommitmentTransaction, {
724         (0, inner),
725         (2, counterparty_sig),
726         (4, holder_sig_first),
727 }, {}, {
728         (6, counterparty_htlc_sigs),
729 });
730
731 impl HolderCommitmentTransaction {
732         #[cfg(test)]
733         pub fn dummy() -> Self {
734                 let secp_ctx = Secp256k1::new();
735                 let dummy_key = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
736                 let dummy_sig = secp_ctx.sign(&secp256k1::Message::from_slice(&[42; 32]).unwrap(), &SecretKey::from_slice(&[42; 32]).unwrap());
737
738                 let keys = TxCreationKeys {
739                         per_commitment_point: dummy_key.clone(),
740                         revocation_key: dummy_key.clone(),
741                         broadcaster_htlc_key: dummy_key.clone(),
742                         countersignatory_htlc_key: dummy_key.clone(),
743                         broadcaster_delayed_payment_key: dummy_key.clone(),
744                 };
745                 let channel_pubkeys = ChannelPublicKeys {
746                         funding_pubkey: dummy_key.clone(),
747                         revocation_basepoint: dummy_key.clone(),
748                         payment_point: dummy_key.clone(),
749                         delayed_payment_basepoint: dummy_key.clone(),
750                         htlc_basepoint: dummy_key.clone()
751                 };
752                 let channel_parameters = ChannelTransactionParameters {
753                         holder_pubkeys: channel_pubkeys.clone(),
754                         holder_selected_contest_delay: 0,
755                         is_outbound_from_holder: false,
756                         counterparty_parameters: Some(CounterpartyChannelTransactionParameters { pubkeys: channel_pubkeys.clone(), selected_contest_delay: 0 }),
757                         funding_outpoint: Some(chain::transaction::OutPoint { txid: Default::default(), index: 0 })
758                 };
759                 let mut htlcs_with_aux: Vec<(_, ())> = Vec::new();
760                 let inner = CommitmentTransaction::new_with_auxiliary_htlc_data(0, 0, 0, keys, 0, &mut htlcs_with_aux, &channel_parameters.as_counterparty_broadcastable());
761                 HolderCommitmentTransaction {
762                         inner,
763                         counterparty_sig: dummy_sig,
764                         counterparty_htlc_sigs: Vec::new(),
765                         holder_sig_first: false
766                 }
767         }
768
769         /// Create a new holder transaction with the given counterparty signatures.
770         /// The funding keys are used to figure out which signature should go first when building the transaction for broadcast.
771         pub fn new(commitment_tx: CommitmentTransaction, counterparty_sig: Signature, counterparty_htlc_sigs: Vec<Signature>, holder_funding_key: &PublicKey, counterparty_funding_key: &PublicKey) -> Self {
772                 Self {
773                         inner: commitment_tx,
774                         counterparty_sig,
775                         counterparty_htlc_sigs,
776                         holder_sig_first: holder_funding_key.serialize()[..] < counterparty_funding_key.serialize()[..],
777                 }
778         }
779
780         pub(crate) fn add_holder_sig(&self, funding_redeemscript: &Script, holder_sig: Signature) -> Transaction {
781                 // First push the multisig dummy, note that due to BIP147 (NULLDUMMY) it must be a zero-length element.
782                 let mut tx = self.inner.built.transaction.clone();
783                 tx.input[0].witness.push(Vec::new());
784
785                 if self.holder_sig_first {
786                         tx.input[0].witness.push(holder_sig.serialize_der().to_vec());
787                         tx.input[0].witness.push(self.counterparty_sig.serialize_der().to_vec());
788                 } else {
789                         tx.input[0].witness.push(self.counterparty_sig.serialize_der().to_vec());
790                         tx.input[0].witness.push(holder_sig.serialize_der().to_vec());
791                 }
792                 tx.input[0].witness[1].push(SigHashType::All as u8);
793                 tx.input[0].witness[2].push(SigHashType::All as u8);
794
795                 tx.input[0].witness.push(funding_redeemscript.as_bytes().to_vec());
796                 tx
797         }
798 }
799
800 /// A pre-built Bitcoin commitment transaction and its txid.
801 #[derive(Clone)]
802 pub struct BuiltCommitmentTransaction {
803         /// The commitment transaction
804         pub transaction: Transaction,
805         /// The txid for the commitment transaction.
806         ///
807         /// This is provided as a performance optimization, instead of calling transaction.txid()
808         /// multiple times.
809         pub txid: Txid,
810 }
811
812 impl_writeable_tlv_based!(BuiltCommitmentTransaction, {
813         (0, transaction),
814         (2, txid)
815 }, {}, {});
816
817 impl BuiltCommitmentTransaction {
818         /// Get the SIGHASH_ALL sighash value of the transaction.
819         ///
820         /// This can be used to verify a signature.
821         pub fn get_sighash_all(&self, funding_redeemscript: &Script, channel_value_satoshis: u64) -> Message {
822                 let sighash = &bip143::SigHashCache::new(&self.transaction).signature_hash(0, funding_redeemscript, channel_value_satoshis, SigHashType::All)[..];
823                 hash_to_message!(sighash)
824         }
825
826         /// Sign a transaction, either because we are counter-signing the counterparty's transaction or
827         /// because we are about to broadcast a holder transaction.
828         pub fn sign<T: secp256k1::Signing>(&self, funding_key: &SecretKey, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) -> Signature {
829                 let sighash = self.get_sighash_all(funding_redeemscript, channel_value_satoshis);
830                 secp_ctx.sign(&sighash, funding_key)
831         }
832 }
833
834 /// This class tracks the per-transaction information needed to build a commitment transaction and to
835 /// actually build it and sign.  It is used for holder transactions that we sign only when needed
836 /// and for transactions we sign for the counterparty.
837 ///
838 /// This class can be used inside a signer implementation to generate a signature given the relevant
839 /// secret key.
840 #[derive(Clone)]
841 pub struct CommitmentTransaction {
842         commitment_number: u64,
843         to_broadcaster_value_sat: u64,
844         to_countersignatory_value_sat: u64,
845         feerate_per_kw: u32,
846         htlcs: Vec<HTLCOutputInCommitment>,
847         // A cache of the parties' pubkeys required to construct the transaction, see doc for trust()
848         keys: TxCreationKeys,
849         // For access to the pre-built transaction, see doc for trust()
850         built: BuiltCommitmentTransaction,
851 }
852
853 impl PartialEq for CommitmentTransaction {
854         fn eq(&self, o: &Self) -> bool {
855                 let eq = self.commitment_number == o.commitment_number &&
856                         self.to_broadcaster_value_sat == o.to_broadcaster_value_sat &&
857                         self.to_countersignatory_value_sat == o.to_countersignatory_value_sat &&
858                         self.feerate_per_kw == o.feerate_per_kw &&
859                         self.htlcs == o.htlcs &&
860                         self.keys == o.keys;
861                 if eq {
862                         debug_assert_eq!(self.built.transaction, o.built.transaction);
863                         debug_assert_eq!(self.built.txid, o.built.txid);
864                 }
865                 eq
866         }
867 }
868
869 /// (C-not exported) as users never need to call this directly
870 impl Writeable for Vec<HTLCOutputInCommitment> {
871         #[inline]
872         fn write<W: Writer>(&self, w: &mut W) -> Result<(), ::std::io::Error> {
873                 (self.len() as u16).write(w)?;
874                 for e in self.iter() {
875                         e.write(w)?;
876                 }
877                 Ok(())
878         }
879 }
880
881 /// (C-not exported) as users never need to call this directly
882 impl Readable for Vec<HTLCOutputInCommitment> {
883         #[inline]
884         fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
885                 let len: u16 = Readable::read(r)?;
886                 let byte_size = (len as usize)
887                         .checked_mul(HTLC_OUTPUT_IN_COMMITMENT_SIZE)
888                         .ok_or(DecodeError::BadLengthDescriptor)?;
889                 if byte_size > MAX_BUF_SIZE {
890                         return Err(DecodeError::BadLengthDescriptor);
891                 }
892                 let mut ret = Vec::with_capacity(len as usize);
893                 for _ in 0..len { ret.push(HTLCOutputInCommitment::read(r)?); }
894                 Ok(ret)
895         }
896 }
897
898 impl_writeable_tlv_based!(CommitmentTransaction, {
899         (0, commitment_number),
900         (2, to_broadcaster_value_sat),
901         (4, to_countersignatory_value_sat),
902         (6, feerate_per_kw),
903         (8, htlcs),
904         (10, keys),
905         (12, built),
906 }, {}, {});
907
908 impl CommitmentTransaction {
909         /// Construct an object of the class while assigning transaction output indices to HTLCs.
910         ///
911         /// Populates HTLCOutputInCommitment.transaction_output_index in htlcs_with_aux.
912         ///
913         /// The generic T allows the caller to match the HTLC output index with auxiliary data.
914         /// This auxiliary data is not stored in this object.
915         ///
916         /// Only include HTLCs that are above the dust limit for the channel.
917         ///
918         /// (C-not exported) due to the generic though we likely should expose a version without
919         pub fn new_with_auxiliary_htlc_data<T>(commitment_number: u64, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, keys: TxCreationKeys, feerate_per_kw: u32, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters) -> CommitmentTransaction {
920                 // Sort outputs and populate output indices while keeping track of the auxiliary data
921                 let (outputs, htlcs) = Self::internal_build_outputs(&keys, to_broadcaster_value_sat, to_countersignatory_value_sat, htlcs_with_aux, channel_parameters).unwrap();
922
923                 let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(commitment_number, channel_parameters);
924                 let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
925                 let txid = transaction.txid();
926                 CommitmentTransaction {
927                         commitment_number,
928                         to_broadcaster_value_sat,
929                         to_countersignatory_value_sat,
930                         feerate_per_kw,
931                         htlcs,
932                         keys,
933                         built: BuiltCommitmentTransaction {
934                                 transaction,
935                                 txid
936                         },
937                 }
938         }
939
940         fn internal_rebuild_transaction(&self, keys: &TxCreationKeys, channel_parameters: &DirectedChannelTransactionParameters) -> Result<BuiltCommitmentTransaction, ()> {
941                 let (obscured_commitment_transaction_number, txins) = Self::internal_build_inputs(self.commitment_number, channel_parameters);
942
943                 let mut htlcs_with_aux = self.htlcs.iter().map(|h| (h.clone(), ())).collect();
944                 let (outputs, _) = Self::internal_build_outputs(keys, self.to_broadcaster_value_sat, self.to_countersignatory_value_sat, &mut htlcs_with_aux, channel_parameters)?;
945
946                 let transaction = Self::make_transaction(obscured_commitment_transaction_number, txins, outputs);
947                 let txid = transaction.txid();
948                 let built_transaction = BuiltCommitmentTransaction {
949                         transaction,
950                         txid
951                 };
952                 Ok(built_transaction)
953         }
954
955         fn make_transaction(obscured_commitment_transaction_number: u64, txins: Vec<TxIn>, outputs: Vec<TxOut>) -> Transaction {
956                 Transaction {
957                         version: 2,
958                         lock_time: ((0x20 as u32) << 8 * 3) | ((obscured_commitment_transaction_number & 0xffffffu64) as u32),
959                         input: txins,
960                         output: outputs,
961                 }
962         }
963
964         // This is used in two cases:
965         // - initial sorting of outputs / HTLCs in the constructor, in which case T is auxiliary data the
966         //   caller needs to have sorted together with the HTLCs so it can keep track of the output index
967         // - building of a bitcoin transaction during a verify() call, in which case T is just ()
968         fn internal_build_outputs<T>(keys: &TxCreationKeys, to_broadcaster_value_sat: u64, to_countersignatory_value_sat: u64, htlcs_with_aux: &mut Vec<(HTLCOutputInCommitment, T)>, channel_parameters: &DirectedChannelTransactionParameters) -> Result<(Vec<TxOut>, Vec<HTLCOutputInCommitment>), ()> {
969                 let countersignatory_pubkeys = channel_parameters.countersignatory_pubkeys();
970                 let contest_delay = channel_parameters.contest_delay();
971
972                 let mut txouts: Vec<(TxOut, Option<&mut HTLCOutputInCommitment>)> = Vec::new();
973
974                 if to_countersignatory_value_sat > 0 {
975                         let script = script_for_p2wpkh(&countersignatory_pubkeys.payment_point);
976                         txouts.push((
977                                 TxOut {
978                                         script_pubkey: script.clone(),
979                                         value: to_countersignatory_value_sat,
980                                 },
981                                 None,
982                         ))
983                 }
984
985                 if to_broadcaster_value_sat > 0 {
986                         let redeem_script = get_revokeable_redeemscript(
987                                 &keys.revocation_key,
988                                 contest_delay,
989                                 &keys.broadcaster_delayed_payment_key,
990                         );
991                         txouts.push((
992                                 TxOut {
993                                         script_pubkey: redeem_script.to_v0_p2wsh(),
994                                         value: to_broadcaster_value_sat,
995                                 },
996                                 None,
997                         ));
998                 }
999
1000                 let mut htlcs = Vec::with_capacity(htlcs_with_aux.len());
1001                 for (htlc, _) in htlcs_with_aux {
1002                         let script = chan_utils::get_htlc_redeemscript(&htlc, &keys);
1003                         let txout = TxOut {
1004                                 script_pubkey: script.to_v0_p2wsh(),
1005                                 value: htlc.amount_msat / 1000,
1006                         };
1007                         txouts.push((txout, Some(htlc)));
1008                 }
1009
1010                 // Sort output in BIP-69 order (amount, scriptPubkey).  Tie-breaks based on HTLC
1011                 // CLTV expiration height.
1012                 sort_outputs(&mut txouts, |a, b| {
1013                         if let &Some(ref a_htlcout) = a {
1014                                 if let &Some(ref b_htlcout) = b {
1015                                         a_htlcout.cltv_expiry.cmp(&b_htlcout.cltv_expiry)
1016                                                 // Note that due to hash collisions, we have to have a fallback comparison
1017                                                 // here for fuzztarget mode (otherwise at least chanmon_fail_consistency
1018                                                 // may fail)!
1019                                                 .then(a_htlcout.payment_hash.0.cmp(&b_htlcout.payment_hash.0))
1020                                 // For non-HTLC outputs, if they're copying our SPK we don't really care if we
1021                                 // close the channel due to mismatches - they're doing something dumb:
1022                                 } else { cmp::Ordering::Equal }
1023                         } else { cmp::Ordering::Equal }
1024                 });
1025
1026                 let mut outputs = Vec::with_capacity(txouts.len());
1027                 for (idx, out) in txouts.drain(..).enumerate() {
1028                         if let Some(htlc) = out.1 {
1029                                 htlc.transaction_output_index = Some(idx as u32);
1030                                 htlcs.push(htlc.clone());
1031                         }
1032                         outputs.push(out.0);
1033                 }
1034                 Ok((outputs, htlcs))
1035         }
1036
1037         fn internal_build_inputs(commitment_number: u64, channel_parameters: &DirectedChannelTransactionParameters) -> (u64, Vec<TxIn>) {
1038                 let broadcaster_pubkeys = channel_parameters.broadcaster_pubkeys();
1039                 let countersignatory_pubkeys = channel_parameters.countersignatory_pubkeys();
1040                 let commitment_transaction_number_obscure_factor = get_commitment_transaction_number_obscure_factor(
1041                         &broadcaster_pubkeys.payment_point,
1042                         &countersignatory_pubkeys.payment_point,
1043                         channel_parameters.is_outbound(),
1044                 );
1045
1046                 let obscured_commitment_transaction_number =
1047                         commitment_transaction_number_obscure_factor ^ (INITIAL_COMMITMENT_NUMBER - commitment_number);
1048
1049                 let txins = {
1050                         let mut ins: Vec<TxIn> = Vec::new();
1051                         ins.push(TxIn {
1052                                 previous_output: channel_parameters.funding_outpoint(),
1053                                 script_sig: Script::new(),
1054                                 sequence: ((0x80 as u32) << 8 * 3)
1055                                         | ((obscured_commitment_transaction_number >> 3 * 8) as u32),
1056                                 witness: Vec::new(),
1057                         });
1058                         ins
1059                 };
1060                 (obscured_commitment_transaction_number, txins)
1061         }
1062
1063         /// The backwards-counting commitment number
1064         pub fn commitment_number(&self) -> u64 {
1065                 self.commitment_number
1066         }
1067
1068         /// The value to be sent to the broadcaster
1069         pub fn to_broadcaster_value_sat(&self) -> u64 {
1070                 self.to_broadcaster_value_sat
1071         }
1072
1073         /// The value to be sent to the counterparty
1074         pub fn to_countersignatory_value_sat(&self) -> u64 {
1075                 self.to_countersignatory_value_sat
1076         }
1077
1078         /// The feerate paid per 1000-weight-unit in this commitment transaction.
1079         pub fn feerate_per_kw(&self) -> u32 {
1080                 self.feerate_per_kw
1081         }
1082
1083         /// The non-dust HTLCs (direction, amt, height expiration, hash, transaction output index)
1084         /// which were included in this commitment transaction in output order.
1085         /// The transaction index is always populated.
1086         ///
1087         /// (C-not exported) as we cannot currently convert Vec references to/from C, though we should
1088         /// expose a less effecient version which creates a Vec of references in the future.
1089         pub fn htlcs(&self) -> &Vec<HTLCOutputInCommitment> {
1090                 &self.htlcs
1091         }
1092
1093         /// Trust our pre-built transaction and derived transaction creation public keys.
1094         ///
1095         /// Applies a wrapper which allows access to these fields.
1096         ///
1097         /// This should only be used if you fully trust the builder of this object.  It should not
1098         ///     be used by an external signer - instead use the verify function.
1099         pub fn trust(&self) -> TrustedCommitmentTransaction {
1100                 TrustedCommitmentTransaction { inner: self }
1101         }
1102
1103         /// Verify our pre-built transaction and derived transaction creation public keys.
1104         ///
1105         /// Applies a wrapper which allows access to these fields.
1106         ///
1107         /// An external validating signer must call this method before signing
1108         /// or using the built transaction.
1109         pub fn verify<T: secp256k1::Signing + secp256k1::Verification>(&self, channel_parameters: &DirectedChannelTransactionParameters, broadcaster_keys: &ChannelPublicKeys, countersignatory_keys: &ChannelPublicKeys, secp_ctx: &Secp256k1<T>) -> Result<TrustedCommitmentTransaction, ()> {
1110                 // This is the only field of the key cache that we trust
1111                 let per_commitment_point = self.keys.per_commitment_point;
1112                 let keys = TxCreationKeys::from_channel_static_keys(&per_commitment_point, broadcaster_keys, countersignatory_keys, secp_ctx).unwrap();
1113                 if keys != self.keys {
1114                         return Err(());
1115                 }
1116                 let tx = self.internal_rebuild_transaction(&keys, channel_parameters)?;
1117                 if self.built.transaction != tx.transaction || self.built.txid != tx.txid {
1118                         return Err(());
1119                 }
1120                 Ok(TrustedCommitmentTransaction { inner: self })
1121         }
1122 }
1123
1124 /// A wrapper on CommitmentTransaction indicating that the derived fields (the built bitcoin
1125 /// transaction and the transaction creation keys) are trusted.
1126 ///
1127 /// See trust() and verify() functions on CommitmentTransaction.
1128 ///
1129 /// This structure implements Deref.
1130 pub struct TrustedCommitmentTransaction<'a> {
1131         inner: &'a CommitmentTransaction,
1132 }
1133
1134 impl<'a> Deref for TrustedCommitmentTransaction<'a> {
1135         type Target = CommitmentTransaction;
1136
1137         fn deref(&self) -> &Self::Target { self.inner }
1138 }
1139
1140 impl<'a> TrustedCommitmentTransaction<'a> {
1141         /// The transaction ID of the built Bitcoin transaction
1142         pub fn txid(&self) -> Txid {
1143                 self.inner.built.txid
1144         }
1145
1146         /// The pre-built Bitcoin commitment transaction
1147         pub fn built_transaction(&self) -> &BuiltCommitmentTransaction {
1148                 &self.inner.built
1149         }
1150
1151         /// The pre-calculated transaction creation public keys.
1152         pub fn keys(&self) -> &TxCreationKeys {
1153                 &self.inner.keys
1154         }
1155
1156         /// Get a signature for each HTLC which was included in the commitment transaction (ie for
1157         /// which HTLCOutputInCommitment::transaction_output_index.is_some()).
1158         ///
1159         /// The returned Vec has one entry for each HTLC, and in the same order.
1160         pub fn get_htlc_sigs<T: secp256k1::Signing>(&self, htlc_base_key: &SecretKey, channel_parameters: &DirectedChannelTransactionParameters, secp_ctx: &Secp256k1<T>) -> Result<Vec<Signature>, ()> {
1161                 let inner = self.inner;
1162                 let keys = &inner.keys;
1163                 let txid = inner.built.txid;
1164                 let mut ret = Vec::with_capacity(inner.htlcs.len());
1165                 let holder_htlc_key = derive_private_key(secp_ctx, &inner.keys.per_commitment_point, htlc_base_key).map_err(|_| ())?;
1166
1167                 for this_htlc in inner.htlcs.iter() {
1168                         assert!(this_htlc.transaction_output_index.is_some());
1169                         let htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
1170
1171                         let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(&this_htlc, &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key);
1172
1173                         let sighash = hash_to_message!(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, this_htlc.amount_msat / 1000, SigHashType::All)[..]);
1174                         ret.push(secp_ctx.sign(&sighash, &holder_htlc_key));
1175                 }
1176                 Ok(ret)
1177         }
1178
1179         /// Gets a signed HTLC transaction given a preimage (for !htlc.offered) and the holder HTLC transaction signature.
1180         pub(crate) fn get_signed_htlc_tx(&self, channel_parameters: &DirectedChannelTransactionParameters, htlc_index: usize, counterparty_signature: &Signature, signature: &Signature, preimage: &Option<PaymentPreimage>) -> Transaction {
1181                 let inner = self.inner;
1182                 let keys = &inner.keys;
1183                 let txid = inner.built.txid;
1184                 let this_htlc = &inner.htlcs[htlc_index];
1185                 assert!(this_htlc.transaction_output_index.is_some());
1186                 // if we don't have preimage for an HTLC-Success, we can't generate an HTLC transaction.
1187                 if !this_htlc.offered && preimage.is_none() { unreachable!(); }
1188                 // Further, we should never be provided the preimage for an HTLC-Timeout transaction.
1189                 if  this_htlc.offered && preimage.is_some() { unreachable!(); }
1190
1191                 let mut htlc_tx = build_htlc_transaction(&txid, inner.feerate_per_kw, channel_parameters.contest_delay(), &this_htlc, &keys.broadcaster_delayed_payment_key, &keys.revocation_key);
1192
1193                 let htlc_redeemscript = get_htlc_redeemscript_with_explicit_keys(&this_htlc, &keys.broadcaster_htlc_key, &keys.countersignatory_htlc_key, &keys.revocation_key);
1194
1195                 // First push the multisig dummy, note that due to BIP147 (NULLDUMMY) it must be a zero-length element.
1196                 htlc_tx.input[0].witness.push(Vec::new());
1197
1198                 htlc_tx.input[0].witness.push(counterparty_signature.serialize_der().to_vec());
1199                 htlc_tx.input[0].witness.push(signature.serialize_der().to_vec());
1200                 htlc_tx.input[0].witness[1].push(SigHashType::All as u8);
1201                 htlc_tx.input[0].witness[2].push(SigHashType::All as u8);
1202
1203                 if this_htlc.offered {
1204                         // Due to BIP146 (MINIMALIF) this must be a zero-length element to relay.
1205                         htlc_tx.input[0].witness.push(Vec::new());
1206                 } else {
1207                         htlc_tx.input[0].witness.push(preimage.unwrap().0.to_vec());
1208                 }
1209
1210                 htlc_tx.input[0].witness.push(htlc_redeemscript.as_bytes().to_vec());
1211                 htlc_tx
1212         }
1213 }
1214
1215 /// Get the transaction number obscure factor
1216 pub fn get_commitment_transaction_number_obscure_factor(
1217         broadcaster_payment_basepoint: &PublicKey,
1218         countersignatory_payment_basepoint: &PublicKey,
1219         outbound_from_broadcaster: bool,
1220 ) -> u64 {
1221         let mut sha = Sha256::engine();
1222
1223         if outbound_from_broadcaster {
1224                 sha.input(&broadcaster_payment_basepoint.serialize());
1225                 sha.input(&countersignatory_payment_basepoint.serialize());
1226         } else {
1227                 sha.input(&countersignatory_payment_basepoint.serialize());
1228                 sha.input(&broadcaster_payment_basepoint.serialize());
1229         }
1230         let res = Sha256::from_engine(sha).into_inner();
1231
1232         ((res[26] as u64) << 5 * 8)
1233                 | ((res[27] as u64) << 4 * 8)
1234                 | ((res[28] as u64) << 3 * 8)
1235                 | ((res[29] as u64) << 2 * 8)
1236                 | ((res[30] as u64) << 1 * 8)
1237                 | ((res[31] as u64) << 0 * 8)
1238 }
1239
1240 fn script_for_p2wpkh(key: &PublicKey) -> Script {
1241         Builder::new().push_opcode(opcodes::all::OP_PUSHBYTES_0)
1242                 .push_slice(&WPubkeyHash::hash(&key.serialize())[..])
1243                 .into_script()
1244 }
1245
1246 #[cfg(test)]
1247 mod tests {
1248         use super::CounterpartyCommitmentSecrets;
1249         use hex;
1250         use prelude::*;
1251
1252         #[test]
1253         fn test_per_commitment_storage() {
1254                 // Test vectors from BOLT 3:
1255                 let mut secrets: Vec<[u8; 32]> = Vec::new();
1256                 let mut monitor;
1257
1258                 macro_rules! test_secrets {
1259                         () => {
1260                                 let mut idx = 281474976710655;
1261                                 for secret in secrets.iter() {
1262                                         assert_eq!(monitor.get_secret(idx).unwrap(), *secret);
1263                                         idx -= 1;
1264                                 }
1265                                 assert_eq!(monitor.get_min_seen_secret(), idx + 1);
1266                                 assert!(monitor.get_secret(idx).is_none());
1267                         };
1268                 }
1269
1270                 {
1271                         // insert_secret correct sequence
1272                         monitor = CounterpartyCommitmentSecrets::new();
1273                         secrets.clear();
1274
1275                         secrets.push([0; 32]);
1276                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
1277                         monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
1278                         test_secrets!();
1279
1280                         secrets.push([0; 32]);
1281                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
1282                         monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
1283                         test_secrets!();
1284
1285                         secrets.push([0; 32]);
1286                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
1287                         monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
1288                         test_secrets!();
1289
1290                         secrets.push([0; 32]);
1291                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
1292                         monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
1293                         test_secrets!();
1294
1295                         secrets.push([0; 32]);
1296                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c65716add7aa98ba7acb236352d665cab17345fe45b55fb879ff80e6bd0c41dd").unwrap());
1297                         monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
1298                         test_secrets!();
1299
1300                         secrets.push([0; 32]);
1301                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
1302                         monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
1303                         test_secrets!();
1304
1305                         secrets.push([0; 32]);
1306                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a5a64476122ca0925fb344bdc1854c1c0a59fc614298e50a33e331980a220f32").unwrap());
1307                         monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
1308                         test_secrets!();
1309
1310                         secrets.push([0; 32]);
1311                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
1312                         monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).unwrap();
1313                         test_secrets!();
1314                 }
1315
1316                 {
1317                         // insert_secret #1 incorrect
1318                         monitor = CounterpartyCommitmentSecrets::new();
1319                         secrets.clear();
1320
1321                         secrets.push([0; 32]);
1322                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148").unwrap());
1323                         monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
1324                         test_secrets!();
1325
1326                         secrets.push([0; 32]);
1327                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
1328                         assert!(monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).is_err());
1329                 }
1330
1331                 {
1332                         // insert_secret #2 incorrect (#1 derived from incorrect)
1333                         monitor = CounterpartyCommitmentSecrets::new();
1334                         secrets.clear();
1335
1336                         secrets.push([0; 32]);
1337                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148").unwrap());
1338                         monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
1339                         test_secrets!();
1340
1341                         secrets.push([0; 32]);
1342                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("dddc3a8d14fddf2b68fa8c7fbad2748274937479dd0f8930d5ebb4ab6bd866a3").unwrap());
1343                         monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
1344                         test_secrets!();
1345
1346                         secrets.push([0; 32]);
1347                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
1348                         monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
1349                         test_secrets!();
1350
1351                         secrets.push([0; 32]);
1352                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
1353                         assert!(monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).is_err());
1354                 }
1355
1356                 {
1357                         // insert_secret #3 incorrect
1358                         monitor = CounterpartyCommitmentSecrets::new();
1359                         secrets.clear();
1360
1361                         secrets.push([0; 32]);
1362                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
1363                         monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
1364                         test_secrets!();
1365
1366                         secrets.push([0; 32]);
1367                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
1368                         monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
1369                         test_secrets!();
1370
1371                         secrets.push([0; 32]);
1372                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c51a18b13e8527e579ec56365482c62f180b7d5760b46e9477dae59e87ed423a").unwrap());
1373                         monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
1374                         test_secrets!();
1375
1376                         secrets.push([0; 32]);
1377                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
1378                         assert!(monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).is_err());
1379                 }
1380
1381                 {
1382                         // insert_secret #4 incorrect (1,2,3 derived from incorrect)
1383                         monitor = CounterpartyCommitmentSecrets::new();
1384                         secrets.clear();
1385
1386                         secrets.push([0; 32]);
1387                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("02a40c85b6f28da08dfdbe0926c53fab2de6d28c10301f8f7c4073d5e42e3148").unwrap());
1388                         monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
1389                         test_secrets!();
1390
1391                         secrets.push([0; 32]);
1392                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("dddc3a8d14fddf2b68fa8c7fbad2748274937479dd0f8930d5ebb4ab6bd866a3").unwrap());
1393                         monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
1394                         test_secrets!();
1395
1396                         secrets.push([0; 32]);
1397                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c51a18b13e8527e579ec56365482c62f180b7d5760b46e9477dae59e87ed423a").unwrap());
1398                         monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
1399                         test_secrets!();
1400
1401                         secrets.push([0; 32]);
1402                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("ba65d7b0ef55a3ba300d4e87af29868f394f8f138d78a7011669c79b37b936f4").unwrap());
1403                         monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
1404                         test_secrets!();
1405
1406                         secrets.push([0; 32]);
1407                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c65716add7aa98ba7acb236352d665cab17345fe45b55fb879ff80e6bd0c41dd").unwrap());
1408                         monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
1409                         test_secrets!();
1410
1411                         secrets.push([0; 32]);
1412                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
1413                         monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
1414                         test_secrets!();
1415
1416                         secrets.push([0; 32]);
1417                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a5a64476122ca0925fb344bdc1854c1c0a59fc614298e50a33e331980a220f32").unwrap());
1418                         monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
1419                         test_secrets!();
1420
1421                         secrets.push([0; 32]);
1422                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
1423                         assert!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).is_err());
1424                 }
1425
1426                 {
1427                         // insert_secret #5 incorrect
1428                         monitor = CounterpartyCommitmentSecrets::new();
1429                         secrets.clear();
1430
1431                         secrets.push([0; 32]);
1432                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
1433                         monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
1434                         test_secrets!();
1435
1436                         secrets.push([0; 32]);
1437                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
1438                         monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
1439                         test_secrets!();
1440
1441                         secrets.push([0; 32]);
1442                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
1443                         monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
1444                         test_secrets!();
1445
1446                         secrets.push([0; 32]);
1447                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
1448                         monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
1449                         test_secrets!();
1450
1451                         secrets.push([0; 32]);
1452                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("631373ad5f9ef654bb3dade742d09504c567edd24320d2fcd68e3cc47e2ff6a6").unwrap());
1453                         monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
1454                         test_secrets!();
1455
1456                         secrets.push([0; 32]);
1457                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
1458                         assert!(monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).is_err());
1459                 }
1460
1461                 {
1462                         // insert_secret #6 incorrect (5 derived from incorrect)
1463                         monitor = CounterpartyCommitmentSecrets::new();
1464                         secrets.clear();
1465
1466                         secrets.push([0; 32]);
1467                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
1468                         monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
1469                         test_secrets!();
1470
1471                         secrets.push([0; 32]);
1472                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
1473                         monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
1474                         test_secrets!();
1475
1476                         secrets.push([0; 32]);
1477                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
1478                         monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
1479                         test_secrets!();
1480
1481                         secrets.push([0; 32]);
1482                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
1483                         monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
1484                         test_secrets!();
1485
1486                         secrets.push([0; 32]);
1487                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("631373ad5f9ef654bb3dade742d09504c567edd24320d2fcd68e3cc47e2ff6a6").unwrap());
1488                         monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
1489                         test_secrets!();
1490
1491                         secrets.push([0; 32]);
1492                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("b7e76a83668bde38b373970155c868a653304308f9896692f904a23731224bb1").unwrap());
1493                         monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
1494                         test_secrets!();
1495
1496                         secrets.push([0; 32]);
1497                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a5a64476122ca0925fb344bdc1854c1c0a59fc614298e50a33e331980a220f32").unwrap());
1498                         monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
1499                         test_secrets!();
1500
1501                         secrets.push([0; 32]);
1502                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
1503                         assert!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).is_err());
1504                 }
1505
1506                 {
1507                         // insert_secret #7 incorrect
1508                         monitor = CounterpartyCommitmentSecrets::new();
1509                         secrets.clear();
1510
1511                         secrets.push([0; 32]);
1512                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
1513                         monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
1514                         test_secrets!();
1515
1516                         secrets.push([0; 32]);
1517                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
1518                         monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
1519                         test_secrets!();
1520
1521                         secrets.push([0; 32]);
1522                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
1523                         monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
1524                         test_secrets!();
1525
1526                         secrets.push([0; 32]);
1527                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
1528                         monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
1529                         test_secrets!();
1530
1531                         secrets.push([0; 32]);
1532                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c65716add7aa98ba7acb236352d665cab17345fe45b55fb879ff80e6bd0c41dd").unwrap());
1533                         monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
1534                         test_secrets!();
1535
1536                         secrets.push([0; 32]);
1537                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
1538                         monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
1539                         test_secrets!();
1540
1541                         secrets.push([0; 32]);
1542                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("e7971de736e01da8ed58b94c2fc216cb1dca9e326f3a96e7194fe8ea8af6c0a3").unwrap());
1543                         monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
1544                         test_secrets!();
1545
1546                         secrets.push([0; 32]);
1547                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("05cde6323d949933f7f7b78776bcc1ea6d9b31447732e3802e1f7ac44b650e17").unwrap());
1548                         assert!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).is_err());
1549                 }
1550
1551                 {
1552                         // insert_secret #8 incorrect
1553                         monitor = CounterpartyCommitmentSecrets::new();
1554                         secrets.clear();
1555
1556                         secrets.push([0; 32]);
1557                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("7cc854b54e3e0dcdb010d7a3fee464a9687be6e8db3be6854c475621e007a5dc").unwrap());
1558                         monitor.provide_secret(281474976710655, secrets.last().unwrap().clone()).unwrap();
1559                         test_secrets!();
1560
1561                         secrets.push([0; 32]);
1562                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c7518c8ae4660ed02894df8976fa1a3659c1a8b4b5bec0c4b872abeba4cb8964").unwrap());
1563                         monitor.provide_secret(281474976710654, secrets.last().unwrap().clone()).unwrap();
1564                         test_secrets!();
1565
1566                         secrets.push([0; 32]);
1567                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("2273e227a5b7449b6e70f1fb4652864038b1cbf9cd7c043a7d6456b7fc275ad8").unwrap());
1568                         monitor.provide_secret(281474976710653, secrets.last().unwrap().clone()).unwrap();
1569                         test_secrets!();
1570
1571                         secrets.push([0; 32]);
1572                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("27cddaa5624534cb6cb9d7da077cf2b22ab21e9b506fd4998a51d54502e99116").unwrap());
1573                         monitor.provide_secret(281474976710652, secrets.last().unwrap().clone()).unwrap();
1574                         test_secrets!();
1575
1576                         secrets.push([0; 32]);
1577                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("c65716add7aa98ba7acb236352d665cab17345fe45b55fb879ff80e6bd0c41dd").unwrap());
1578                         monitor.provide_secret(281474976710651, secrets.last().unwrap().clone()).unwrap();
1579                         test_secrets!();
1580
1581                         secrets.push([0; 32]);
1582                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("969660042a28f32d9be17344e09374b379962d03db1574df5a8a5a47e19ce3f2").unwrap());
1583                         monitor.provide_secret(281474976710650, secrets.last().unwrap().clone()).unwrap();
1584                         test_secrets!();
1585
1586                         secrets.push([0; 32]);
1587                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a5a64476122ca0925fb344bdc1854c1c0a59fc614298e50a33e331980a220f32").unwrap());
1588                         monitor.provide_secret(281474976710649, secrets.last().unwrap().clone()).unwrap();
1589                         test_secrets!();
1590
1591                         secrets.push([0; 32]);
1592                         secrets.last_mut().unwrap()[0..32].clone_from_slice(&hex::decode("a7efbc61aac46d34f77778bac22c8a20c6a46ca460addc49009bda875ec88fa4").unwrap());
1593                         assert!(monitor.provide_secret(281474976710648, secrets.last().unwrap().clone()).is_err());
1594                 }
1595         }
1596 }