X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fchain%2Ftransaction.rs;h=e8ebae7092556286e4a6f22a8bfde7baa0ef9b30;hb=d33cb3cca560ec33b33542c1ef6e47f15b35e30a;hp=42a4f952e4150550f240c9c59d5d89a9a885c7de;hpb=de523c4ca38c4f671df11fa4c7f5781ba0ab00fc;p=rust-lightning diff --git a/src/chain/transaction.rs b/src/chain/transaction.rs index 42a4f952..e8ebae70 100644 --- a/src/chain/transaction.rs +++ b/src/chain/transaction.rs @@ -1,8 +1,11 @@ +//! Contains simple structs describing parts of transactions on the chain. + use bitcoin::util::hash::Sha256dHash; -use bitcoin::util::uint::Uint256; +use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint; /// A reference to a transaction output. -/// Differs from bitcoin::blockdata::transaction::TxOutRef as the index is a u16 instead of usize +/// +/// Differs from bitcoin::blockdata::transaction::OutPoint as the index is a u16 instead of u32 /// 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 { @@ -13,16 +16,48 @@ pub struct OutPoint { } impl OutPoint { - /// Creates a new `OutPoint` from the txid an the index. + /// Creates a new `OutPoint` from the txid and the index. pub fn new(txid: Sha256dHash, index: u16) -> OutPoint { OutPoint { txid, index } } /// 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() + pub fn to_channel_id(&self) -> [u8; 32] { + let mut res = [0; 32]; + res[..].copy_from_slice(&self.txid[..]); + res[30] ^= ((self.index >> 8) & 0xff) as u8; + res[31] ^= ((self.index >> 0) & 0xff) as u8; + res + } + + /// Converts this OutPoint into the OutPoint field as used by rust-bitcoin + pub fn into_bitcoin_outpoint(self) -> BitcoinOutPoint { + BitcoinOutPoint { + txid: self.txid, + vout: self.index as u32, + } + } +} + +#[cfg(test)] +mod tests { + use chain::transaction::OutPoint; + + use bitcoin::blockdata::transaction::Transaction; + use bitcoin::network::serialize; + + use hex; + + #[test] + fn test_channel_id_calculation() { + let tx: Transaction = serialize::deserialize(&hex::decode("020000000001010e0adef48412e4361325ac1c6e36411299ab09d4f083b9d8ddb55fbc06e1b0c00000000000feffffff0220a1070000000000220020f81d95e040bd0a493e38bae27bff52fe2bb58b93b293eb579c01c31b05c5af1dc072cfee54a3000016001434b1d6211af5551905dc2642d05f5b04d25a8fe80247304402207f570e3f0de50546aad25a872e3df059d277e776dda4269fa0d2cc8c2ee6ec9a022054e7fae5ca94d47534c86705857c24ceea3ad51c69dd6051c5850304880fc43a012103cb11a1bacc223d98d91f1946c6752e358a5eb1a1c983b3e6fb15378f453b76bd00000000").unwrap()[..]).unwrap(); + assert_eq!(&OutPoint { + txid: tx.txid(), + index: 0 + }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]); + assert_eq!(&OutPoint { + txid: tx.txid(), + index: 1 + }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]); } }