Merge pull request #2818 from valentinewallace/2024-01-blinded-path-retries
[rust-lightning] / lightning / src / chain / transaction.rs
index 0219ebbe8cad9631e1f2fef8c7b05600828e9f24..5bef97792d3944252837f67d8e7ebefe8a797e57 100644 (file)
@@ -9,7 +9,9 @@
 
 //! Types describing on-chain transactions.
 
+use crate::ln::ChannelId;
 use bitcoin::hash_types::Txid;
+use bitcoin::hashes::Hash;
 use bitcoin::blockdata::transaction::OutPoint as BitcoinOutPoint;
 use bitcoin::blockdata::transaction::Transaction;
 
@@ -57,16 +59,14 @@ pub struct OutPoint {
 
 impl OutPoint {
        /// Convert an `OutPoint` to a lightning channel id.
-       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
+       pub fn to_channel_id(&self) -> ChannelId {
+               ChannelId::v1_from_funding_txid(self.txid.as_byte_array(), self.index)
        }
 
        /// Converts this OutPoint into the OutPoint field as used by rust-bitcoin
-       /// (C-not exported) as the same type is used universally in the C bindings for all outpoints
+       ///
+       /// This is not exported to bindings users as the same type is used universally in the C bindings 
+       /// for all outpoints
        pub fn into_bitcoin_outpoint(self) -> BitcoinOutPoint {
                BitcoinOutPoint {
                        txid: self.txid,
@@ -75,27 +75,32 @@ impl OutPoint {
        }
 }
 
+impl core::fmt::Display for OutPoint {
+       fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
+               write!(f, "{}:{}", self.txid, self.index)
+       }
+}
+
 impl_writeable!(OutPoint, { txid, index });
 
 #[cfg(test)]
 mod tests {
-       use chain::transaction::OutPoint;
+       use crate::chain::transaction::OutPoint;
 
        use bitcoin::blockdata::transaction::Transaction;
        use bitcoin::consensus::encode;
-
-       use hex;
+       use bitcoin::hashes::hex::FromHex;
 
        #[test]
        fn test_channel_id_calculation() {
-               let tx: Transaction = encode::deserialize(&hex::decode("020000000001010e0adef48412e4361325ac1c6e36411299ab09d4f083b9d8ddb55fbc06e1b0c00000000000feffffff0220a1070000000000220020f81d95e040bd0a493e38bae27bff52fe2bb58b93b293eb579c01c31b05c5af1dc072cfee54a3000016001434b1d6211af5551905dc2642d05f5b04d25a8fe80247304402207f570e3f0de50546aad25a872e3df059d277e776dda4269fa0d2cc8c2ee6ec9a022054e7fae5ca94d47534c86705857c24ceea3ad51c69dd6051c5850304880fc43a012103cb11a1bacc223d98d91f1946c6752e358a5eb1a1c983b3e6fb15378f453b76bd00000000").unwrap()[..]).unwrap();
+               let tx: Transaction = encode::deserialize(&<Vec<u8>>::from_hex("020000000001010e0adef48412e4361325ac1c6e36411299ab09d4f083b9d8ddb55fbc06e1b0c00000000000feffffff0220a1070000000000220020f81d95e040bd0a493e38bae27bff52fe2bb58b93b293eb579c01c31b05c5af1dc072cfee54a3000016001434b1d6211af5551905dc2642d05f5b04d25a8fe80247304402207f570e3f0de50546aad25a872e3df059d277e776dda4269fa0d2cc8c2ee6ec9a022054e7fae5ca94d47534c86705857c24ceea3ad51c69dd6051c5850304880fc43a012103cb11a1bacc223d98d91f1946c6752e358a5eb1a1c983b3e6fb15378f453b76bd00000000").unwrap()[..]).unwrap();
                assert_eq!(&OutPoint {
                        txid: tx.txid(),
                        index: 0
-               }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
+               }.to_channel_id().0[..], &<Vec<u8>>::from_hex("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25e").unwrap()[..]);
                assert_eq!(&OutPoint {
                        txid: tx.txid(),
                        index: 1
-               }.to_channel_id(), &hex::decode("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
+               }.to_channel_id().0[..], &<Vec<u8>>::from_hex("3e88dd7165faf7be58b3c5bb2c9c452aebef682807ea57080f62e6f6e113c25f").unwrap()[..]);
        }
 }