Add annotations for things which we cannot (yet) expose
[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 //! Contains simple structs describing parts of transactions on the chain.
11
12 use bitcoin::hash_types::Txid;
13 use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
14
15 /// A reference to a transaction output.
16 ///
17 /// Differs from bitcoin::blockdata::transaction::OutPoint as the index is a u16 instead of u32
18 /// due to LN's restrictions on index values. Should reduce (possibly) unsafe conversions this way.
19 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
20 pub struct OutPoint {
21         /// The referenced transaction's txid.
22         pub txid: Txid,
23         /// The index of the referenced output in its transaction's vout.
24         pub index: u16,
25 }
26
27 impl OutPoint {
28         /// Convert an `OutPoint` to a lightning channel id.
29         pub fn to_channel_id(&self) -> [u8; 32] {
30                 let mut res = [0; 32];
31                 res[..].copy_from_slice(&self.txid[..]);
32                 res[30] ^= ((self.index >> 8) & 0xff) as u8;
33                 res[31] ^= ((self.index >> 0) & 0xff) as u8;
34                 res
35         }
36
37         /// Converts this OutPoint into the OutPoint field as used by rust-bitcoin
38         /// (C-not exported) as the same type is used universally in the C bindings for all outpoints
39         pub fn into_bitcoin_outpoint(self) -> BitcoinOutPoint {
40                 BitcoinOutPoint {
41                         txid: self.txid,
42                         vout: self.index as u32,
43                 }
44         }
45 }
46
47 impl_writeable!(OutPoint, 0, { txid, index });
48
49 #[cfg(test)]
50 mod tests {
51         use chain::transaction::OutPoint;
52
53         use bitcoin::blockdata::transaction::Transaction;
54         use bitcoin::consensus::encode;
55
56         use hex;
57
58         #[test]
59         fn test_channel_id_calculation() {
60                 let tx: Transaction = encode::deserialize(&hex::decode("020000000001010e0adef48412e4361325ac1c6e36411299ab09d4f083b9d8ddb55fbc06e1b0c00000000000feffffff0220a1070000000000220020f81d95e040bd0a493e38bae27bff52fe2bb58b93b293eb579c01c31b05c5af1dc072cfee54a3000016001434b1d6211af5551905dc2642d05f5b04d25a8fe80247304402207f570e3f0de50546aad25a872e3df059d277e776dda4269fa0d2cc8c2ee6ec9a022054e7fae5ca94d47534c86705857c24ceea3ad51c69dd6051c5850304880fc43a012103cb11a1bacc223d98d91f1946c6752e358a5eb1a1c983b3e6fb15378f453b76bd00000000").unwrap()[..]).unwrap();
61                 assert_eq!(&OutPoint {
62                         txid: tx.txid(),
63                         index: 0
64                 }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
65                 assert_eq!(&OutPoint {
66                         txid: tx.txid(),
67                         index: 1
68                 }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
69         }
70 }