X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fchain%2Ftransaction.rs;h=42a4f952e4150550f240c9c59d5d89a9a885c7de;hb=b02176c86bfa991a7c4856e427355d6e034e1eaa;hp=00728b45033a53844ba94451d4d6907475b1d419;hpb=d0e9137bc5f8f31cc5e9c63b94f1450bdb18b524;p=rust-lightning diff --git a/src/chain/transaction.rs b/src/chain/transaction.rs index 00728b45..42a4f952 100644 --- a/src/chain/transaction.rs +++ b/src/chain/transaction.rs @@ -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() + } }