Move justice transaction signature behind ChanSigner
[rust-lightning] / lightning / src / chain / keysinterface.rs
index 9ed28e12fe8e983dc40f3a2abb7d00a36a018763..2dc42abad3ae4cb92bd6284de47c816b4a801d1f 100644 (file)
@@ -20,14 +20,12 @@ use bitcoin::secp256k1::{Secp256k1, Signature, Signing};
 use bitcoin::secp256k1;
 
 use util::byte_utils;
-use util::logger::Logger;
 use util::ser::{Writeable, Writer, Readable};
 
 use ln::chan_utils;
 use ln::chan_utils::{TxCreationKeys, HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, LocalCommitmentTransaction};
 use ln::msgs;
 
-use std::sync::Arc;
 use std::sync::atomic::{AtomicUsize, Ordering};
 use std::io::Error;
 use ln::msgs::DecodeError;
@@ -248,6 +246,27 @@ pub trait ChannelKeys : Send+Clone {
        /// return value must contain a signature.
        fn sign_local_commitment_htlc_transactions<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &LocalCommitmentTransaction, local_csv: u16, secp_ctx: &Secp256k1<T>) -> Result<Vec<Option<Signature>>, ()>;
 
+       /// Create a signature for a transaction spending an HTLC or commitment transaction output
+       /// when our counterparty broadcast an old state.
+       ///
+       /// Justice transaction may claim multiples outputs at same time if timelock are similar.
+       /// It may be called multiples time for same output(s) if a fee-bump is needed with regards
+       /// to an upcoming timelock expiration.
+       ///
+       /// Witness_script is a revokable witness script as defined in BOLT3 for `to_local`/HTLC
+       /// outputs.
+       ///
+       /// Input index is a pointer towards outpoint spent, commited by sigs (BIP 143).
+       ///
+       /// Amount is value of the output spent by this input, committed by sigs (BIP 143).
+       ///
+       /// Per_commitment key is revocation secret such as provided by remote party while
+       /// revocating detected onchain transaction. It's not a _local_ secret key, therefore
+       /// it may cross interfaces, a node compromise won't allow to spend revoked output without
+       /// also compromissing revocation key.
+       //TODO: dry-up witness_script and pass pubkeys
+       fn sign_justice_transaction<T: secp256k1::Signing>(&self, justice_tx: &Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_key: &SecretKey, revocation_pubkey: &PublicKey, is_htlc: bool, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()>;
+
        /// Create a signature for a (proposed) closing transaction.
        ///
        /// Note that, due to rounding, there may be one "missing" satoshi, and either party may have
@@ -396,6 +415,15 @@ impl ChannelKeys for InMemoryChannelKeys {
                local_commitment_tx.get_htlc_sigs(&self.htlc_base_key, local_csv, secp_ctx)
        }
 
+       fn sign_justice_transaction<T: secp256k1::Signing>(&self, justice_tx: &Transaction, input: usize, witness_script: &Script, amount: u64, per_commitment_key: &SecretKey, revocation_pubkey: &PublicKey, is_htlc: bool, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
+               if let Ok(revocation_key) = chan_utils::derive_private_revocation_key(&secp_ctx, &per_commitment_key, &self.revocation_base_key) {
+                       let sighash_parts = bip143::SighashComponents::new(&justice_tx);
+                       let sighash = hash_to_message!(&sighash_parts.sighash_all(&justice_tx.input[input], &witness_script, amount)[..]);
+                       return Ok(secp_ctx.sign(&sighash, &revocation_key))
+               }
+               Err(())
+       }
+
        fn sign_closing_transaction<T: secp256k1::Signing>(&self, closing_tx: &Transaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
                if closing_tx.input.len() != 1 { return Err(()); }
                if closing_tx.input[0].witness.len() != 0 { return Err(()); }
@@ -486,7 +514,6 @@ pub struct KeysManager {
        channel_id_child_index: AtomicUsize,
 
        unique_start: Sha256State,
-       logger: Arc<Logger>,
 }
 
 impl KeysManager {
@@ -509,7 +536,7 @@ impl KeysManager {
        /// Note that until the 0.1 release there is no guarantee of backward compatibility between
        /// versions. Once the library is more fully supported, the docs will be updated to include a
        /// detailed description of the guarantee.
-       pub fn new(seed: &[u8; 32], network: Network, logger: Arc<Logger>, starting_time_secs: u64, starting_time_nanos: u32) -> KeysManager {
+       pub fn new(seed: &[u8; 32], network: Network, starting_time_secs: u64, starting_time_nanos: u32) -> KeysManager {
                let secp_ctx = Secp256k1::signing_only();
                match ExtendedPrivKey::new_master(network.clone(), seed) {
                        Ok(master_key) => {
@@ -549,7 +576,6 @@ impl KeysManager {
                                        channel_id_child_index: AtomicUsize::new(0),
 
                                        unique_start,
-                                       logger,
                                }
                        },
                        Err(_) => panic!("Your rng is busted"),