Fold sign_holder_commitment_htlc_transactions into sign_holder_commitment
[rust-lightning] / lightning / src / chain / keysinterface.rs
index 085a2f87154b1b5a09c9cd9a32757bf8f164dc49..80f734edbde3e0def17dcd23ceb903d564d969e2 100644 (file)
@@ -204,7 +204,7 @@ impl Readable for SpendableOutputDescriptor {
 // routine).
 // TODO: We should remove Clone by instead requesting a new ChannelKeys copy when we create
 // ChannelMonitors instead of expecting to clone the one out of the Channel into the monitors.
-pub trait ChannelKeys : Send+Clone {
+pub trait ChannelKeys : Send+Clone + Writeable {
        /// Gets the per-commitment point for a specific commitment number
        ///
        /// Note that the commitment number starts at (1 << 48) - 1 and counts backwards.
@@ -233,13 +233,21 @@ pub trait ChannelKeys : Send+Clone {
        // TODO: Document the things someone using this interface should enforce before signing.
        fn sign_counterparty_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &CommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()>;
 
-       /// Create a signature for a holder's commitment transaction. This will only ever be called with
-       /// the same commitment_tx (or a copy thereof), though there are currently no guarantees
-       /// that it will not be called multiple times.
+       /// Create a signatures for a holder's commitment transaction and its claiming HTLC transactions.
+       /// This will only ever be called with a non-revoked commitment_tx.  This will be called with the
+       /// latest commitment_tx when we initiate a force-close.
+       /// This will be called with the previous latest, just to get claiming HTLC signatures, if we are
+       /// reacting to a ChannelMonitor replica that decided to broadcast before it had been updated to
+       /// the latest.
+       /// This may be called multiple times for the same transaction.
+       ///
        /// An external signer implementation should check that the commitment has not been revoked.
+       ///
+       /// May return Err if key derivation fails.  Callers, such as ChannelMonitor, will panic in such a case.
        //
        // TODO: Document the things someone using this interface should enforce before signing.
-       fn sign_holder_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()>;
+       // TODO: Key derivation failure should panic rather than Err
+       fn sign_holder_commitment_and_htlcs<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()>;
 
        /// Same as sign_holder_commitment, but exists only for tests to get access to holder commitment
        /// transactions which will be broadcasted later, after the channel has moved on to a newer
@@ -248,18 +256,6 @@ pub trait ChannelKeys : Send+Clone {
        #[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
        fn unsafe_sign_holder_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()>;
 
-       /// Create a signature for each HTLC transaction spending a holder's commitment transaction.
-       ///
-       /// Unlike sign_holder_commitment, this may be called multiple times with *different*
-       /// commitment_tx values. While this will never be called with a revoked
-       /// commitment_tx, it is possible that it is called with the second-latest
-       /// commitment_tx (only if we haven't yet revoked it) if some watchtower/secondary
-       /// ChannelMonitor decided to broadcast before it had been updated to the latest.
-       ///
-       /// Either an Err should be returned, or a Vec with one entry for each HTLC which exists in
-       /// commitment_tx.
-       fn sign_holder_commitment_htlc_transactions<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Vec<Signature>, ()>;
-
        /// Create a signature for the given input in a transaction spending an HTLC or commitment
        /// transaction output when our counterparty broadcasts an old state.
        ///
@@ -344,6 +340,14 @@ pub trait KeysInterface: Send + Sync {
        /// onion packets and for temporary channel IDs. There is no requirement that these be
        /// persisted anywhere, though they must be unique across restarts.
        fn get_secure_random_bytes(&self) -> [u8; 32];
+
+       /// Reads a `ChanKeySigner` for this `KeysInterface` from the given input stream.
+       /// This is only called during deserialization of other objects which contain
+       /// `ChannelKeys`-implementing objects (ie `ChannelMonitor`s and `ChannelManager`s).
+       /// The bytes are exactly those which `<Self::ChanKeySigner as Writeable>::write()` writes, and
+       /// contain no versioning scheme. You may wish to include your own version prefix and ensure
+       /// you've read all of the provided bytes to ensure no corruption occurred.
+       fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::ChanKeySigner, DecodeError>;
 }
 
 #[derive(Clone)]
@@ -492,11 +496,14 @@ impl ChannelKeys for InMemoryChannelKeys {
                Ok((commitment_sig, htlc_sigs))
        }
 
-       fn sign_holder_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
+       fn sign_holder_commitment_and_htlcs<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<(Signature, Vec<Signature>), ()> {
                let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
                let funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
                let sig = commitment_tx.trust().built_transaction().sign(&self.funding_key, &funding_redeemscript, self.channel_value_satoshis, secp_ctx);
-               Ok(sig)
+               let channel_parameters = self.get_channel_parameters();
+               let trusted_tx = commitment_tx.trust();
+               let htlc_sigs = trusted_tx.get_htlc_sigs(&self.htlc_base_key, &channel_parameters.as_holder_broadcastable(), secp_ctx)?;
+               Ok((sig, htlc_sigs))
        }
 
        #[cfg(any(test,feature = "unsafe_revoked_tx_signing"))]
@@ -506,12 +513,6 @@ impl ChannelKeys for InMemoryChannelKeys {
                Ok(commitment_tx.trust().built_transaction().sign(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx))
        }
 
-       fn sign_holder_commitment_htlc_transactions<T: secp256k1::Signing + secp256k1::Verification>(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<T>) -> Result<Vec<Signature>, ()> {
-               let channel_parameters = self.get_channel_parameters();
-               let trusted_tx = commitment_tx.trust();
-               trusted_tx.get_htlc_sigs(&self.htlc_base_key, &channel_parameters.as_holder_broadcastable(), secp_ctx)
-       }
-
        fn sign_justice_transaction<T: secp256k1::Signing + secp256k1::Verification>(&self, justice_tx: &Transaction, input: usize, amount: u64, per_commitment_key: &SecretKey, htlc: &Option<HTLCOutputInCommitment>, secp_ctx: &Secp256k1<T>) -> Result<Signature, ()> {
                let revocation_key = match chan_utils::derive_private_revocation_key(&secp_ctx, &per_commitment_key, &self.revocation_base_key) {
                        Ok(revocation_key) => revocation_key,
@@ -809,4 +810,8 @@ impl KeysInterface for KeysManager {
                sha.input(b"Unique Secure Random Bytes Salt");
                Sha256::from_engine(sha).into_inner()
        }
+
+       fn read_chan_signer(&self, reader: &[u8]) -> Result<Self::ChanKeySigner, DecodeError> {
+               InMemoryChannelKeys::read(&mut std::io::Cursor::new(reader))
+       }
 }