adding BIP69 test-cases
[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                 let mut index = [0; 32];
24                 index[30] = ((self.index >> 8) & 0xff) as u8;
25                 index[31] = ((self.index >> 0) & 0xff) as u8;
26                 self.txid.into_le() ^ Sha256dHash::from(&index[..]).into_le()
27         }
28 }