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