Merge pull request #37 from TheBlueMatt/master
[rust-lightning] / src / chain / transaction.rs
1 use bitcoin::util::hash::Sha256dHash;
2 use bitcoin::util::uint::Uint256;
3
4 /// A reference to a transaction output.
5 /// Differs from bitcoin::blockdata::transaction::TxOutRef as the index is a u16 instead of usize
6 /// due to LN's restrictions on index values. Should reduce (possibly) unsafe conversions this way.
7 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
8 pub struct OutPoint {
9         /// The referenced transaction's txid.
10         pub txid: Sha256dHash,
11         /// The index of the referenced output in its transaction's vout.
12         pub index: u16,
13 }
14
15 impl OutPoint {
16         /// Creates a new `OutPoint` from the txid an the index.
17         pub fn new(txid: Sha256dHash, index: u16) -> OutPoint {
18                 OutPoint { txid, index }
19         }
20
21         /// Convert an `OutPoint` to a lightning channel id.
22         pub fn to_channel_id(&self) -> Uint256 {
23                 // TODO: or le?
24                 self.txid.into_be() ^ Uint256::from_u64(self.index as u64).unwrap()
25         }
26 }