@@ -31,7 +31,7 @@ use bitcoin::hash_types::{Txid, BlockHash, WPubkeyHash};
31
31
32
32
use bitcoin:: secp256k1:: { Secp256k1 , ecdsa:: Signature } ;
33
33
use bitcoin:: secp256k1:: { SecretKey , PublicKey } ;
34
- use bitcoin:: secp256k1;
34
+ use bitcoin:: { secp256k1, EcdsaSighashType } ;
35
35
36
36
use crate :: ln:: channel:: INITIAL_COMMITMENT_NUMBER ;
37
37
use crate :: ln:: { PaymentHash , PaymentPreimage } ;
@@ -1430,7 +1430,8 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1430
1430
/// This is provided so that watchtower clients in the persistence pipeline are able to build
1431
1431
/// justice transactions for each counterparty commitment upon each update. It's intended to be
1432
1432
/// used within an implementation of [`Persist::update_persisted_channel`], which is provided
1433
- /// with a monitor and an update.
1433
+ /// with a monitor and an update. Once revoked, signing a justice transaction can be done using
1434
+ /// [`Self::sign_justice_tx`].
1434
1435
///
1435
1436
/// It is expected that a watchtower may use this method to retrieve the latest counterparty
1436
1437
/// commitment transaction(s), and then hold the necessary data until a later update in which
@@ -1446,6 +1447,25 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
1446
1447
self . inner . lock ( ) . unwrap ( ) . counterparty_commitment_txs_from_update ( update)
1447
1448
}
1448
1449
1450
+ /// Wrapper around [`EcdsaChannelSigner::sign_justice_revoked_output`] to make
1451
+ /// signing the justice transaction easier for implementors of
1452
+ /// [`chain::chainmonitor::Persist`]. On success this method returns the provided transaction
1453
+ /// signing the input at `input_idx`.
1454
+ ///
1455
+ /// `Value` is the value of the output being spent by the input at `input_idx`, committed
1456
+ /// in the BIP 143 signature.
1457
+ ///
1458
+ /// This method will only succeed if this monitor has received the revocation secret for the
1459
+ /// provided `commitment_number`. If a commitment number is provided that does not correspond
1460
+ /// to the commitment transaction being revoked, this will return a signed transaction, but
1461
+ /// the signature will not be valid.
1462
+ ///
1463
+ /// [`EcdsaChannelSigner::sign_justice_revoked_output`]: crate::sign::EcdsaChannelSigner::sign_justice_revoked_output
1464
+ /// [`Persist`]: crate::chain::chainmonitor::Persist
1465
+ pub fn sign_justice_tx ( & self , justice_tx : Transaction , input_idx : usize , value : u64 , commitment_number : u64 ) -> Result < Transaction , ( ) > {
1466
+ self . inner . lock ( ) . unwrap ( ) . sign_justice_tx ( justice_tx, input_idx, value, commitment_number)
1467
+ }
1468
+
1449
1469
pub ( crate ) fn get_min_seen_secret ( & self ) -> u64 {
1450
1470
self . inner . lock ( ) . unwrap ( ) . get_min_seen_secret ( )
1451
1471
}
@@ -2810,6 +2830,31 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
2810
2830
} ) . collect ( )
2811
2831
}
2812
2832
2833
+ pub ( crate ) fn sign_justice_tx (
2834
+ & self , mut justice_tx : Transaction , input_idx : usize , value : u64 , commitment_number : u64
2835
+ ) -> Result < Transaction , ( ) > {
2836
+ let secret = self . get_secret ( commitment_number) . ok_or ( ( ) ) ?;
2837
+ let per_commitment_key = SecretKey :: from_slice ( & secret) . map_err ( |_| ( ) ) ?;
2838
+ let their_per_commitment_point = PublicKey :: from_secret_key (
2839
+ & self . onchain_tx_handler . secp_ctx , & per_commitment_key) ;
2840
+
2841
+ let revocation_pubkey = chan_utils:: derive_public_revocation_key (
2842
+ & self . onchain_tx_handler . secp_ctx , & their_per_commitment_point,
2843
+ & self . holder_revocation_basepoint ) ;
2844
+ let delayed_key = chan_utils:: derive_public_key ( & self . onchain_tx_handler . secp_ctx ,
2845
+ & their_per_commitment_point,
2846
+ & self . counterparty_commitment_params . counterparty_delayed_payment_base_key ) ;
2847
+ let revokeable_redeemscript = chan_utils:: get_revokeable_redeemscript ( & revocation_pubkey,
2848
+ self . counterparty_commitment_params . on_counterparty_tx_csv , & delayed_key) ;
2849
+
2850
+ let sig = self . onchain_tx_handler . signer . sign_justice_revoked_output (
2851
+ & justice_tx, input_idx, value, & per_commitment_key, & self . onchain_tx_handler . secp_ctx ) ?;
2852
+ justice_tx. input [ input_idx] . witness . push_bitcoin_signature ( & sig. serialize_der ( ) , EcdsaSighashType :: All ) ;
2853
+ justice_tx. input [ input_idx] . witness . push ( & [ 1u8 ] ) ;
2854
+ justice_tx. input [ input_idx] . witness . push ( revokeable_redeemscript. as_bytes ( ) ) ;
2855
+ Ok ( justice_tx)
2856
+ }
2857
+
2813
2858
/// Can only fail if idx is < get_min_seen_secret
2814
2859
fn get_secret ( & self , idx : u64 ) -> Option < [ u8 ; 32 ] > {
2815
2860
self . commitment_secrets . get_secret ( idx)
0 commit comments