Fix various spec bugs, can now open channels with real nodes!
[rust-lightning] / src / chain / transaction.rs
index 00728b45033a53844ba94451d4d6907475b1d419..42a4f952e4150550f240c9c59d5d89a9a885c7de 100644 (file)
@@ -2,6 +2,8 @@ use bitcoin::util::hash::Sha256dHash;
 use bitcoin::util::uint::Uint256;
 
 /// A reference to a transaction output.
+/// Differs from bitcoin::blockdata::transaction::TxOutRef as the index is a u16 instead of usize
+/// due to LN's restrictions on index values. Should reduce (possibly) unsafe conversions this way.
 #[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Hash)]
 pub struct OutPoint {
        /// The referenced transaction's txid.
@@ -16,9 +18,11 @@ impl OutPoint {
                OutPoint { txid, index }
        }
 
-    /// Convert an `OutPoint` to a lightning channel id.
-    pub fn to_channel_id(&self) -> Uint256 {
-        // TODO: or le?
-        self.txid.into_be() ^ Uint256::from_u64(self.index as u64).unwrap()
-    }
+       /// Convert an `OutPoint` to a lightning channel id.
+       pub fn to_channel_id(&self) -> Uint256 {
+               let mut index = [0; 32];
+               index[30] = ((self.index >> 8) & 0xff) as u8;
+               index[31] = ((self.index >> 0) & 0xff) as u8;
+               self.txid.into_le() ^ Sha256dHash::from(&index[..]).into_le()
+       }
 }