]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Remove SecretKey from DynamicOuputP2WPKH descriptor
authorAntoine Riard <ariard@student.42.fr>
Tue, 24 Mar 2020 21:03:26 +0000 (17:03 -0400)
committerAntoine Riard <ariard@student.42.fr>
Tue, 12 May 2020 21:40:42 +0000 (17:40 -0400)
Add sign_payment_transaction in ChanSigner to be able to spend
SpendableOutputDescriptor in test framework

Extend ChannelKeys with remote_pubkeys to access remote revocation
basepoint for witnessScript construction.

lightning/src/chain/keysinterface.rs
lightning/src/ln/channelmonitor.rs
lightning/src/ln/functional_tests.rs

index 325a1c097063fb268fa4378b38db2a355f508917..366e78b57092a28e251ec34fc5218bb8750029fb 100644 (file)
@@ -7,6 +7,7 @@ use bitcoin::blockdata::script::{Script, Builder};
 use bitcoin::blockdata::opcodes;
 use bitcoin::network::constants::Network;
 use bitcoin::util::bip32::{ExtendedPrivKey, ExtendedPubKey, ChildNumber};
+use bitcoin::util::address::Address;
 use bitcoin::util::bip143;
 
 use bitcoin::hashes::{Hash, HashEngine};
@@ -90,16 +91,23 @@ pub enum SpendableOutputDescriptor {
        // this in favor of StaticOutput:
        /// An output to a P2WPKH, spendable exclusively by the given private key.
        /// The witness in the spending input, is, thus, simply:
-       /// <BIP 143 signature generated with the given key> <public key derived from the given key>
+       /// <BIP 143 signature> <payment key>
+       ///
        /// These are generally the result of our counterparty having broadcast the current state,
        /// allowing us to claim the non-HTLC-encumbered outputs immediately.
+       ///
+       /// To derive the payment key corresponding to the channel state, you must pass the
+       /// channel's payment_base_key and the provided per_commitment_point to
+       /// chan_utils::derive_private_key. The resulting key should be used to sign the spending
+       /// transaction.
        DynamicOutputP2WPKH {
                /// The outpoint which is spendable
                outpoint: OutPoint,
-               /// The secret key which must be used to sign the spending transaction
-               key: SecretKey,
                /// The output which is reference by the given outpoint
                output: TxOut,
+               /// The channel keys state used to proceed to derivation of signing key. Must
+               /// be pass to KeysInterface::derive_channel_keys.
+               key_derivation_params: (u64, u64),
        }
 }
 
@@ -121,11 +129,12 @@ impl Writeable for SpendableOutputDescriptor {
                                key_derivation_params.1.write(writer)?;
                                remote_revocation_pubkey.write(writer)?;
                        },
-                       &SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
+                       &SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref output, ref key_derivation_params } => {
                                2u8.write(writer)?;
                                outpoint.write(writer)?;
-                               key.write(writer)?;
                                output.write(writer)?;
+                               key_derivation_params.0.write(writer)?;
+                               key_derivation_params.1.write(writer)?;
                        },
                }
                Ok(())
@@ -149,8 +158,8 @@ impl Readable for SpendableOutputDescriptor {
                        }),
                        2u8 => Ok(SpendableOutputDescriptor::DynamicOutputP2WPKH {
                                outpoint: Readable::read(reader)?,
-                               key: Readable::read(reader)?,
                                output: Readable::read(reader)?,
+                               key_derivation_params: (Readable::read(reader)?, Readable::read(reader)?),
                        }),
                        _ => Err(DecodeError::InvalidValue),
                }
@@ -396,6 +405,8 @@ impl InMemoryChannelKeys {
                        htlc_basepoint: from_secret(&htlc_base_key),
                }
        }
+
+       fn remote_pubkeys<'a>(&'a self) -> &'a ChannelPublicKeys { self.remote_channel_pubkeys.as_ref().unwrap() }
 }
 
 impl ChannelKeys for InMemoryChannelKeys {
index 460c252cf19d3235b797800c349f0006547d4da7..fae8534dde534a5dbc51fa6d305f4485ef894ff3 100644 (file)
@@ -2119,8 +2119,8 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
                        } else if self.remote_payment_script == outp.script_pubkey {
                                spendable_output = Some(SpendableOutputDescriptor::DynamicOutputP2WPKH {
                                        outpoint: BitcoinOutPoint { txid: tx.txid(), vout: i as u32 },
-                                       key: self.keys.payment_key().clone(),
                                        output: outp.clone(),
+                                       key_derivation_params: self.keys.key_derivation_params(),
                                });
                                break;
                        } else if outp.script_pubkey == self.shutdown_script {
index 0df3742261c0cf3d9259a61ec5bb88eb6375a0ae..b62847a450e3b73a79e6b08608211ab6026e36c7 100644 (file)
@@ -4097,7 +4097,7 @@ macro_rules! check_spendable_outputs {
                                        Event::SpendableOutputs { ref outputs } => {
                                                for outp in outputs {
                                                        match *outp {
-                                                               SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref key, ref output } => {
+                                                               SpendableOutputDescriptor::DynamicOutputP2WPKH { ref outpoint, ref output, ref key_derivation_params } => {
                                                                        let input = TxIn {
                                                                                previous_output: outpoint.clone(),
                                                                                script_sig: Script::new(),
@@ -4115,10 +4115,11 @@ macro_rules! check_spendable_outputs {
                                                                                output: vec![outp],
                                                                        };
                                                                        let secp_ctx = Secp256k1::new();
-                                                                       let remotepubkey = PublicKey::from_secret_key(&secp_ctx, &key);
+                                                                       let keys = $keysinterface.derive_channel_keys($chan_value, key_derivation_params.0, key_derivation_params.1);
+                                                                       let remotepubkey = PublicKey::from_secret_key(&secp_ctx, &keys.payment_key());
                                                                        let witness_script = Address::p2pkh(&::bitcoin::PublicKey{compressed: true, key: remotepubkey}, Network::Testnet).script_pubkey();
                                                                        let sighash = Message::from_slice(&bip143::SighashComponents::new(&spend_tx).sighash_all(&spend_tx.input[0], &witness_script, output.value)[..]).unwrap();
-                                                                       let remotesig = secp_ctx.sign(&sighash, key);
+                                                                       let remotesig = secp_ctx.sign(&sighash, &keys.payment_key());
                                                                        spend_tx.input[0].witness.push(remotesig.serialize_der().to_vec());
                                                                        spend_tx.input[0].witness[0].push(SigHashType::All as u8);
                                                                        spend_tx.input[0].witness.push(remotepubkey.serialize().to_vec());