Provide redeem scripts to sign_remote_commitment
[rust-lightning] / lightning / src / util / transaction_utils.rs
1 use bitcoin::blockdata::transaction::TxOut;
2
3 use std::cmp::Ordering;
4
5 pub fn sort_outputs<T, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T)>, tie_breaker: C) {
6         outputs.sort_unstable_by(|a, b| {
7                 a.0.value.cmp(&b.0.value).then_with(|| {
8                         a.0.script_pubkey[..].cmp(&b.0.script_pubkey[..]).then_with(|| {
9                                 tie_breaker(&a.1, &b.1)
10                         })
11                 })
12         });
13 }
14
15 pub fn sort_outputs2<T, T1, C : Fn(&T, &T) -> Ordering>(outputs: &mut Vec<(TxOut, T, T1)>, tie_breaker: C) {
16         outputs.sort_unstable_by(|a, b| {
17                 a.0.value.cmp(&b.0.value).then_with(|| {
18                         a.0.script_pubkey[..].cmp(&b.0.script_pubkey[..]).then_with(|| {
19                                 tie_breaker(&a.1, &b.1)
20                         })
21                 })
22         });
23 }
24
25 #[cfg(test)]
26 mod tests {
27         use super::*;
28
29         use bitcoin::blockdata::script::{Script, Builder};
30         use bitcoin::blockdata::transaction::TxOut;
31
32         use hex::decode;
33
34         #[test]
35         fn sort_output_by_value() {
36                 let txout1 = TxOut {
37                         value:  100,
38                         script_pubkey: Builder::new().push_int(0).into_script()
39                 };
40                 let txout1_ = txout1.clone();
41
42                 let txout2 = TxOut {
43                         value: 99,
44                         script_pubkey: Builder::new().push_int(0).into_script()
45                 };
46                 let txout2_ = txout2.clone();
47
48                 let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
49                 sort_outputs(&mut outputs, |_, _| { unreachable!(); });
50
51                 assert_eq!(
52                         &outputs,
53                         &vec![(txout2_, "ignore"), (txout1_, "ignore")]
54                         );
55         }
56
57         #[test]
58         fn sort_output_by_script_pubkey() {
59                 let txout1 = TxOut {
60                         value:  100,
61                         script_pubkey: Builder::new().push_int(3).into_script(),
62                 };
63                 let txout1_ = txout1.clone();
64
65                 let txout2 = TxOut {
66                         value: 100,
67                         script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
68                 };
69                 let txout2_ = txout2.clone();
70
71                 let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
72                 sort_outputs(&mut outputs, |_, _| { unreachable!(); });
73
74                 assert_eq!(
75                         &outputs,
76                         &vec![(txout2_, "ignore"), (txout1_, "ignore")]
77                         );
78         }
79
80         #[test]
81         fn sort_output_by_bip_test() {
82                 let txout1 = TxOut {
83                         value: 100000000,
84                         script_pubkey: script_from_hex("41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac")
85                 };
86                 let txout1_ = txout1.clone();
87
88                 // doesn't deserialize cleanly:
89                 let txout2 = TxOut {
90                         value: 2400000000,
91                         script_pubkey: script_from_hex("41044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac")
92                 };
93                 let txout2_ = txout2.clone();
94
95                 let mut outputs = vec![(txout1, "ignore"), (txout2, "ignore")];
96                 sort_outputs(&mut outputs, |_, _| { unreachable!(); });
97
98                 assert_eq!(&outputs, &vec![(txout1_, "ignore"), (txout2_, "ignore")]);
99         }
100
101         #[test]
102         fn sort_output_tie_breaker_test() {
103                 let txout1 = TxOut {
104                         value:  100,
105                         script_pubkey: Builder::new().push_int(1).push_int(2).into_script()
106                 };
107                 let txout1_ = txout1.clone();
108
109                 let txout2 = txout1.clone();
110                 let txout2_ = txout1.clone();
111
112                 let mut outputs = vec![(txout1, 420), (txout2, 69)];
113                 sort_outputs(&mut outputs, |a, b| { a.cmp(b) });
114
115                 assert_eq!(
116                         &outputs,
117                         &vec![(txout2_, 69), (txout1_, 420)]
118                 );
119         }
120
121         fn script_from_hex(hex_str: &str) -> Script {
122                 Script::from(decode(hex_str).unwrap())
123         }
124
125         macro_rules! bip_txout_tests {
126                 ($($name:ident: $value:expr,)*) => {
127                         $(
128                                 #[test]
129                                 fn $name() {
130                                         let expected_raw: Vec<(u64, &str)> = $value;
131                                         let expected: Vec<(TxOut, &str)> = expected_raw.iter()
132                                                 .map(|txout_raw| TxOut {
133                                                         value: txout_raw.0,
134                                                         script_pubkey: script_from_hex(txout_raw.1)
135                                                 }).map(|txout| (txout, "ignore"))
136                                         .collect();
137
138                                         let mut outputs = expected.clone();
139                                         outputs.reverse(); // prep it
140
141                                         // actually do the work!
142                                         sort_outputs(&mut outputs, |_, _| { unreachable!(); });
143
144                                         assert_eq!(outputs, expected);
145                                 }
146                         )*
147                 }
148         }
149
150         const TXOUT1: [(u64, &str); 2] = [
151                 (400057456, "76a9144a5fba237213a062f6f57978f796390bdcf8d01588ac"),
152                 (40000000000, "76a9145be32612930b8323add2212a4ec03c1562084f8488ac"),
153         ];
154         const TXOUT2: [(u64, &str); 2] = [
155                 (100000000, "41046a0765b5865641ce08dd39690aade26dfbf5511430ca428a3089261361cef170e3929a68aee3d8d4848b0c5111b0a37b82b86ad559fd2a745b44d8e8d9dfdc0cac"),
156                 (2400000000, "41044a656f065871a353f216ca26cef8dde2f03e8c16202d2e8ad769f02032cb86a5eb5e56842e92e19141d60a01928f8dd2c875a390f67c1f6c94cfc617c0ea45afac"),
157         ];
158         bip_txout_tests! {
159                 bip69_txout_test_1: TXOUT1.to_vec(),
160                 bip69_txout_test_2: TXOUT2.to_vec(),
161         }
162 }