Merge pull request #191 from TheBlueMatt/2018-09-chanmon-ser-framework
[rust-lightning] / src / chain / transaction.rs
1 //! Contains simple structs describing parts of transactions on the chain.
2
3 use bitcoin::util::hash::Sha256dHash;
4 use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
5
6 /// A reference to a transaction output.
7 /// Differs from bitcoin::blockdata::transaction::OutPoint as the index is a u16 instead of u32
8 /// due to LN's restrictions on index values. Should reduce (possibly) unsafe conversions this way.
9 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
10 pub struct OutPoint {
11         /// The referenced transaction's txid.
12         pub txid: Sha256dHash,
13         /// The index of the referenced output in its transaction's vout.
14         pub index: u16,
15 }
16
17 impl OutPoint {
18         /// Creates a new `OutPoint` from the txid and the index.
19         pub fn new(txid: Sha256dHash, index: u16) -> OutPoint {
20                 OutPoint { txid, index }
21         }
22
23         /// Convert an `OutPoint` to a lightning channel id.
24         pub fn to_channel_id(&self) -> [u8; 32] {
25                 let mut res = [0; 32];
26                 res[..].copy_from_slice(&self.txid[..]);
27                 res[30] ^= ((self.index >> 8) & 0xff) as u8;
28                 res[31] ^= ((self.index >> 0) & 0xff) as u8;
29                 res
30         }
31
32         /// Converts this OutPoint into the OutPoint field as used by rust-bitcoin
33         pub fn into_bitcoin_outpoint(self) -> BitcoinOutPoint {
34                 BitcoinOutPoint {
35                         txid: self.txid,
36                         vout: self.index as u32,
37                 }
38         }
39 }
40
41 #[cfg(test)]
42 mod tests {
43         use chain::transaction::OutPoint;
44
45         use bitcoin::blockdata::transaction::Transaction;
46         use bitcoin::network::serialize;
47
48         use hex;
49
50         #[test]
51         fn test_channel_id_calculation() {
52                 let tx: Transaction = serialize::deserialize(&hex::decode("020000000001010e0adef48412e4361325ac1c6e36411299ab09d4f083b9d8ddb55fbc06e1b0c00000000000feffffff0220a1070000000000220020f81d95e040bd0a493e38bae27bff52fe2bb58b93b293eb579c01c31b05c5af1dc072cfee54a3000016001434b1d6211af5551905dc2642d05f5b04d25a8fe80247304402207f570e3f0de50546aad25a872e3df059d277e776dda4269fa0d2cc8c2ee6ec9a022054e7fae5ca94d47534c86705857c24ceea3ad51c69dd6051c5850304880fc43a012103cb11a1bacc223d98d91f1946c6752e358a5eb1a1c983b3e6fb15378f453b76bd00000000").unwrap()[..]).unwrap();
53                 assert_eq!(&OutPoint {
54                         txid: tx.txid(),
55                         index: 0
56                 }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
57                 assert_eq!(&OutPoint {
58                         txid: tx.txid(),
59                         index: 1
60                 }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
61         }
62 }