@@ -65,16 +65,33 @@ pub fn tag_txin(txin: &TxIn, script_pubkey: &ScriptBuf) -> Option<SpInputs> {
6565}
6666
6767pub fn get_smallest_lexicographic_outpoint ( outpoints : & [ OutPoint ] ) -> [ u8 ; 36 ] {
68- outpoints
68+ // Find the outpoint with the smallest lexicographic order
69+ let smallest = outpoints
6970 . iter ( )
70- . map ( |x| {
71- let mut outpoint_bytes = [ 0u8 ; 36 ] ;
72- outpoint_bytes[ ..32 ] . copy_from_slice ( x. txid . to_raw_hash ( ) . as_byte_array ( ) ) ;
73- outpoint_bytes[ 32 ..36 ] . copy_from_slice ( & x. vout . to_le_bytes ( ) ) ;
74- outpoint_bytes
71+ . min_by ( |a, b| {
72+ // Compare txids first
73+ let a_txid = a. txid . to_raw_hash ( ) ;
74+ let b_txid = b. txid . to_raw_hash ( ) ;
75+
76+ // If txids are different, compare them
77+ match a_txid. as_byte_array ( ) . cmp ( b_txid. as_byte_array ( ) ) {
78+ std:: cmp:: Ordering :: Equal => {
79+ // If txids are equal, compare vouts directly
80+ let a_vout_bytes = a. vout . to_le_bytes ( ) ;
81+ let b_vout_bytes = b. vout . to_le_bytes ( ) ;
82+ a_vout_bytes. cmp ( & b_vout_bytes)
83+ }
84+ other => other,
85+ }
7586 } )
76- . min ( )
77- . expect ( "cannot create silent payment script pubkey without outpoints" )
87+ . expect ( "cannot create silent payment script pubkey without outpoints" ) ;
88+
89+ // Only allocate the result array once we have the smallest outpoint
90+ let mut result = [ 0u8 ; 36 ] ;
91+ result[ ..32 ] . copy_from_slice ( smallest. txid . to_raw_hash ( ) . as_byte_array ( ) ) ;
92+ result[ 32 ..36 ] . copy_from_slice ( & smallest. vout . to_le_bytes ( ) ) ;
93+
94+ result
7895}
7996
8097pub fn compute_shared_secret ( sk : & SecretKey , pk : & PublicKey ) -> PublicKey {
0 commit comments