@@ -36,6 +36,7 @@ use common::{
3636 chain:: {
3737 block:: { timestamp:: BlockTimestamp , ConsensusData } ,
3838 config:: ChainConfig ,
39+ htlc:: HashedTimelockContract ,
3940 make_delegation_id, make_order_id, make_token_id,
4041 output_value:: OutputValue ,
4142 signature:: inputsig:: {
@@ -74,6 +75,8 @@ pub enum BlockchainStateError {
7475 PoolNotFound ,
7576 #[ error( "Order not found in DB" ) ]
7677 OrderNotFound ,
78+ #[ error( "Invalid HTLC signature in TX" ) ]
79+ InvalidHtlcSignature ,
7780 #[ error( "Id creation error: {0}" ) ]
7881 IdCreationError ( #[ from] IdCreationError ) ,
7982}
@@ -348,24 +351,8 @@ impl<S: ApiServerStorage + Send + Sync> LocalBlockchainState for BlockchainState
348351 transaction_tokens. insert ( token_id) ;
349352 }
350353 TxOutput :: Htlc ( output_value, htlc) => {
351- let address = if let InputWitness :: Standard ( sig) = sig {
352- let htlc_sig = AuthorizedHashedTimelockContractSpend :: decode_all (
353- & mut sig. raw_signature ( ) ,
354- )
355- . expect ( "proper signature" ) ;
356- let dest = match htlc_sig {
357- AuthorizedHashedTimelockContractSpend :: Spend ( _, _) => {
358- htlc. spend_key
359- }
360- AuthorizedHashedTimelockContractSpend :: Refund ( _) => {
361- htlc. refund_key
362- }
363- } ;
364- Address :: < Destination > :: new ( & self . chain_config , dest)
365- . expect ( "Unable to encode destination" )
366- } else {
367- panic ! ( "Empty signature for htlc" )
368- } ;
354+ let address =
355+ htlc_destination ( sig, * htlc, & self . chain_config ) . expect ( "valid tx" ) ;
369356 address_transactions. entry ( address. clone ( ) ) . or_default ( ) . insert ( tx_id) ;
370357
371358 if let OutputValue :: TokenV1 ( token_id, _) = output_value {
@@ -2098,25 +2085,8 @@ async fn update_tables_from_transaction_inputs<T: ApiServerStorageWrite>(
20982085 transaction_tokens. insert ( token_id) ;
20992086 }
21002087 TxOutput :: Htlc ( output_value, htlc) => {
2101- let address = if let InputWitness :: Standard ( sig) = sig {
2102- let htlc_sig = AuthorizedHashedTimelockContractSpend :: decode_all (
2103- & mut sig. raw_signature ( ) ,
2104- )
2105- . expect ( "proper signature" ) ;
2106-
2107- let dest = match htlc_sig {
2108- AuthorizedHashedTimelockContractSpend :: Spend ( _, _) => {
2109- htlc. spend_key
2110- }
2111- AuthorizedHashedTimelockContractSpend :: Refund ( _) => {
2112- htlc. refund_key
2113- }
2114- } ;
2115- Address :: < Destination > :: new ( & chain_config, dest)
2116- . expect ( "Unable to encode destination" )
2117- } else {
2118- panic ! ( "Empty signature for htlc" )
2119- } ;
2088+ let address =
2089+ htlc_destination ( sig, * htlc, & chain_config) . expect ( "valid tx" ) ;
21202090
21212091 address_transactions
21222092 . entry ( address. clone ( ) )
@@ -3102,3 +3072,22 @@ async fn decrease_mempool_balance<T: ApiServerStorageWrite + ApiServerStorageRea
31023072 . await
31033073 . map_err ( BlockchainStateError :: StorageError )
31043074}
3075+
3076+ fn htlc_destination (
3077+ sig : & InputWitness ,
3078+ htlc : HashedTimelockContract ,
3079+ chain_config : & ChainConfig ,
3080+ ) -> Result < Address < Destination > , BlockchainStateError > {
3081+ if let InputWitness :: Standard ( sig) = sig {
3082+ let htlc_sig = AuthorizedHashedTimelockContractSpend :: decode_all ( & mut sig. raw_signature ( ) )
3083+ . expect ( "proper signature" ) ;
3084+
3085+ let dest = match htlc_sig {
3086+ AuthorizedHashedTimelockContractSpend :: Spend ( _, _) => htlc. spend_key ,
3087+ AuthorizedHashedTimelockContractSpend :: Refund ( _) => htlc. refund_key ,
3088+ } ;
3089+ Ok ( Address :: < Destination > :: new ( chain_config, dest) . expect ( "Unable to encode destination" ) )
3090+ } else {
3091+ Err ( BlockchainStateError :: InvalidHtlcSignature )
3092+ }
3093+ }
0 commit comments