Clean up ChannelMonitor object serialization to use TLVs/versions
[rust-lightning] / lightning / src / chain / transaction.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 //! Types describing on-chain transactions.
11
12 use bitcoin::hash_types::Txid;
13 use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
14 use bitcoin::blockdata::transaction::Transaction;
15
16 /// Transaction data where each item consists of a transaction reference paired with the index of
17 /// the transaction within a block.
18 ///
19 /// Useful for passing enumerated transactions from a block, possibly filtered, in order to retain
20 /// the transaction index.
21 ///
22 /// ```
23 /// extern crate bitcoin;
24 /// extern crate lightning;
25 ///
26 /// use bitcoin::blockdata::block::Block;
27 /// use bitcoin::blockdata::constants::genesis_block;
28 /// use bitcoin::network::constants::Network;
29 /// use lightning::chain::transaction::TransactionData;
30 ///
31 /// let block = genesis_block(Network::Bitcoin);
32 /// let txdata: Vec<_> = block.txdata.iter().enumerate().collect();
33 /// check_block(&block, &txdata);
34 ///
35 /// fn check_block(block: &Block, txdata: &TransactionData) {
36 ///     assert_eq!(block.txdata.len(), 1);
37 ///     assert_eq!(txdata.len(), 1);
38 ///
39 ///     let (index, tx) = txdata[0];
40 ///     assert_eq!(index, 0);
41 ///     assert_eq!(tx, &block.txdata[0]);
42 /// }
43 /// ```
44 pub type TransactionData<'a> = [(usize, &'a Transaction)];
45
46 /// A reference to a transaction output.
47 ///
48 /// Differs from bitcoin::blockdata::transaction::OutPoint as the index is a u16 instead of u32
49 /// due to LN's restrictions on index values. Should reduce (possibly) unsafe conversions this way.
50 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
51 pub struct OutPoint {
52         /// The referenced transaction's txid.
53         pub txid: Txid,
54         /// The index of the referenced output in its transaction's vout.
55         pub index: u16,
56 }
57
58 impl OutPoint {
59         /// Convert an `OutPoint` to a lightning channel id.
60         pub fn to_channel_id(&self) -> [u8; 32] {
61                 let mut res = [0; 32];
62                 res[..].copy_from_slice(&self.txid[..]);
63                 res[30] ^= ((self.index >> 8) & 0xff) as u8;
64                 res[31] ^= ((self.index >> 0) & 0xff) as u8;
65                 res
66         }
67
68         /// Converts this OutPoint into the OutPoint field as used by rust-bitcoin
69         /// (C-not exported) as the same type is used universally in the C bindings for all outpoints
70         pub fn into_bitcoin_outpoint(self) -> BitcoinOutPoint {
71                 BitcoinOutPoint {
72                         txid: self.txid,
73                         vout: self.index as u32,
74                 }
75         }
76 }
77
78 impl_writeable!(OutPoint, 0, { txid, index });
79
80 #[cfg(test)]
81 mod tests {
82         use chain::transaction::OutPoint;
83
84         use bitcoin::blockdata::transaction::Transaction;
85         use bitcoin::consensus::encode;
86
87         use hex;
88
89         #[test]
90         fn test_channel_id_calculation() {
91                 let tx: Transaction = encode::deserialize(&hex::decode("020000000001010e0adef48412e4361325ac1c6e36411299ab09d4f083b9d8ddb55fbc06e1b0c00000000000feffffff0220a1070000000000220020f81d95e040bd0a493e38bae27bff52fe2bb58b93b293eb579c01c31b05c5af1dc072cfee54a3000016001434b1d6211af5551905dc2642d05f5b04d25a8fe80247304402207f570e3f0de50546aad25a872e3df059d277e776dda4269fa0d2cc8c2ee6ec9a022054e7fae5ca94d47534c86705857c24ceea3ad51c69dd6051c5850304880fc43a012103cb11a1bacc223d98d91f1946c6752e358a5eb1a1c983b3e6fb15378f453b76bd00000000").unwrap()[..]).unwrap();
92                 assert_eq!(&OutPoint {
93                         txid: tx.txid(),
94                         index: 0
95                 }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
96                 assert_eq!(&OutPoint {
97                         txid: tx.txid(),
98                         index: 1
99                 }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
100         }
101 }