]> git.bitcoin.ninja Git - rust-lightning/commitdiff
more checking
authorDevrandom <c1.devrandom@niftybox.net>
Mon, 13 Jan 2020 20:16:26 +0000 (12:16 -0800)
committerDevrandom <c1.devrandom@niftybox.net>
Mon, 13 Jan 2020 20:16:26 +0000 (12:16 -0800)
lightning/src/chain/keysinterface.rs

index cc5a9580b4db342d5a69b400fbe50a1e3a2f8a7a..3e4cb612f01ba4c3c560e78e12efe080d410c1e3 100644 (file)
@@ -139,7 +139,7 @@ pub trait ChannelKeys : Send {
        ///
        /// Note that if signing fails or is rejected, the channel will be force-closed.
        ///
-       /// The commitment_tx follows BIP-69 lexicographical ordering.
+       /// The commitment_tx follows BOLT-3 lexicographical output ordering and has a single input.
        ///
        /// The redeem_scripts vector is 1-1 mapped to commitment_tx outputs.  For p2wpkh, the
        /// redeem script should be empty.
@@ -147,11 +147,18 @@ pub trait ChannelKeys : Send {
        /// TODO: Document the things someone using this interface should enforce before signing.
        /// TODO: Add more input vars to enable better checking (preferably removing commitment_tx and
        /// making the callee generate it via some util function we expose)!
-       fn sign_remote_commitment<T: secp256k1::Signing>(&self, channel_value_satoshis: u64, channel_funding_redeemscript: &Script, feerate_per_kw: u64, commitment_tx: &Transaction, keys: &TxCreationKeys, htlcs: &[&HTLCOutputInCommitment], to_self_delay: u16, secp_ctx: &Secp256k1<T>, redeem_scripts: &Vec<Script>, remote_per_commitment_point: &PublicKey) -> Result<(Signature, Vec<Signature>), ()>;
+       fn sign_remote_commitment<T: secp256k1::Signing>(&self, channel_value_satoshis: u64,
+                                                        channel_funding_redeemscript: &Script,
+                                                        feerate_per_kw: u64, commitment_tx: &Transaction,
+                                                        keys: &TxCreationKeys,
+                                                        htlcs: &[&HTLCOutputInCommitment],
+                                                        to_self_delay: u16, secp_ctx: &Secp256k1<T>,
+                                                        redeem_scripts: &Vec<Script>,
+                                                        remote_per_commitment_point: &PublicKey) -> Result<(Signature, Vec<Signature>), ()>;
 
        /// Create a signature for a (proposed) closing transaction.
        ///
-       /// The closing_tx follows BIP-69 lexicographical ordering.
+       /// The closing_tx follows BOLT-3 lexicographical output ordering and has a single input.
        ///
        /// Note that, due to rounding, there may be one "missing" satoshi, and either party may have
        /// chosen to forgo their output as dust.
@@ -194,6 +201,18 @@ impl ChannelKeys for InMemoryChannelKeys {
        fn sign_remote_commitment<T: secp256k1::Signing>(&self, channel_value_satoshis: u64, channel_funding_redeemscript: &Script, feerate_per_kw: u64, commitment_tx: &Transaction, keys: &TxCreationKeys, htlcs: &[&HTLCOutputInCommitment], to_self_delay: u16, secp_ctx: &Secp256k1<T>, redeem_scripts: &Vec<Script>, remote_per_commitment_point: &PublicKey) -> Result<(Signature, Vec<Signature>), ()> {
                if commitment_tx.input.len() != 1 { return Err(()); }
                if commitment_tx.output.len() != redeem_scripts.len() { return Err(()); }
+
+               for (out, redeem_script) in commitment_tx.output.iter().zip(redeem_scripts.iter()) {
+                       if out.script_pubkey.is_v0_p2wpkh() {
+                               if !redeem_script.is_empty() {
+                                       return Err(())
+                               }
+                       } else {
+                               if out.script_pubkey != redeem_script.to_v0_p2wsh() {
+                                       return Err(())
+                               }
+                       }
+               }
                let commitment_sighash = hash_to_message!(&bip143::SighashComponents::new(&commitment_tx).sighash_all(&commitment_tx.input[0], &channel_funding_redeemscript, channel_value_satoshis)[..]);
                let commitment_sig = secp_ctx.sign(&commitment_sighash, &self.funding_key);