@@ -17,9 +17,12 @@ use bitcoin::PackedLockTime;
17
17
use bitcoin:: blockdata:: transaction:: Transaction ;
18
18
use bitcoin:: blockdata:: transaction:: OutPoint as BitcoinOutPoint ;
19
19
use bitcoin:: blockdata:: script:: Script ;
20
-
20
+ use bitcoin:: hashes:: Hash ;
21
+ #[ cfg( anchors) ]
22
+ use bitcoin:: hashes:: HashEngine ;
23
+ #[ cfg( anchors) ]
24
+ use bitcoin:: hashes:: sha256:: Hash as Sha256 ;
21
25
use bitcoin:: hash_types:: { Txid , BlockHash } ;
22
-
23
26
use bitcoin:: secp256k1:: { Secp256k1 , ecdsa:: Signature } ;
24
27
use bitcoin:: secp256k1;
25
28
@@ -48,7 +51,6 @@ use core::ops::Deref;
48
51
use core:: mem:: replace;
49
52
#[ cfg( anchors) ]
50
53
use core:: mem:: swap;
51
- use bitcoin:: hashes:: Hash ;
52
54
53
55
const MAX_ALLOC_SIZE : usize = 64 * 1024 ;
54
56
@@ -774,19 +776,24 @@ impl<ChannelSigner: WriteableEcdsaChannelSigner> OnchainTxHandler<ChannelSigner>
774
776
OnchainClaim :: Event ( claim_event) => {
775
777
log_info ! ( logger, "Yielding onchain event to spend inputs {:?}" , req. outpoints( ) ) ;
776
778
let package_id = match claim_event {
777
- ClaimEvent :: BumpCommitment { ref commitment_tx, .. } => commitment_tx. txid ( ) . into_inner ( ) ,
779
+ ClaimEvent :: BumpCommitment { ref commitment_tx, .. } =>
780
+ // For commitment claims, we can just use their txid as it should
781
+ // already be unique.
782
+ commitment_tx. txid ( ) . into_inner ( ) ,
778
783
ClaimEvent :: BumpHTLC { ref htlcs, .. } => {
779
- // Use the same construction as a lightning channel id to generate
780
- // the package id for this request based on the first HTLC. It
781
- // doesn't matter what we use as long as it's unique per request.
782
- let mut package_id = [ 0 ; 32 ] ;
783
- package_id[ ..] . copy_from_slice ( & htlcs[ 0 ] . commitment_txid [ ..] ) ;
784
- let htlc_output_index = htlcs[ 0 ] . htlc . transaction_output_index . unwrap ( ) ;
785
- package_id[ 30 ] ^= ( ( htlc_output_index >> 8 ) & 0xff ) as u8 ;
786
- package_id[ 31 ] ^= ( ( htlc_output_index >> 0 ) & 0xff ) as u8 ;
787
- package_id
784
+ // For HTLC claims, commit to the entire set of HTLC outputs to
785
+ // claim, which will always be unique per request. Note that, even
786
+ // if the request has HTLCs removed due to the counterparty claiming
787
+ // it instead, the identifier returned here remains unchanged.
788
+ let mut engine = Sha256 :: engine ( ) ;
789
+ for htlc in htlcs {
790
+ engine. input ( & htlc. commitment_txid . into_inner ( ) ) ;
791
+ engine. input ( & htlc. htlc . transaction_output_index . unwrap ( ) . to_be_bytes ( ) ) ;
792
+ }
793
+ Sha256 :: from_engine ( engine) . into_inner ( )
788
794
} ,
789
795
} ;
796
+ debug_assert ! ( self . pending_claim_requests. get( & package_id) . is_none( ) ) ;
790
797
debug_assert_eq ! ( self . pending_claim_events. iter( ) . filter( |entry| entry. 0 == package_id) . count( ) , 0 ) ;
791
798
self . pending_claim_events . push ( ( package_id, claim_event) ) ;
792
799
package_id
0 commit comments