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