X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=lightning%2Fsrc%2Futil%2Fenforcing_trait_impls.rs;h=948b871bc89989f8b6cee91b7e9f8d14c7384f0e;hb=328407351c6aeb84e9407281ec5d35b1a4690650;hp=3221d6a80bc59b04be046840337237d6072c754e;hpb=f49662caa4a9cb845ae1451b0baf85b80b8dc711;p=rust-lightning diff --git a/lightning/src/util/enforcing_trait_impls.rs b/lightning/src/util/enforcing_trait_impls.rs index 3221d6a8..948b871b 100644 --- a/lightning/src/util/enforcing_trait_impls.rs +++ b/lightning/src/util/enforcing_trait_impls.rs @@ -7,23 +7,26 @@ // You may not use this file except in accordance with one or both of these // licenses. -use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction, ClosingTransaction}; -use ln::{chan_utils, msgs, PaymentPreimage}; -use chain::keysinterface::{Sign, InMemorySigner, BaseSign}; +use crate::ln::channel::{ANCHOR_OUTPUT_VALUE_SATOSHI, MIN_CHAN_DUST_LIMIT_SATOSHIS}; +use crate::ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction, ClosingTransaction}; +use crate::ln::{chan_utils, msgs, PaymentPreimage}; +use crate::chain::keysinterface::{WriteableEcdsaChannelSigner, InMemorySigner, ChannelSigner, EcdsaChannelSigner}; -use prelude::*; +use crate::prelude::*; use core::cmp; -use sync::{Mutex, Arc}; -#[cfg(test)] use sync::MutexGuard; +use crate::sync::{Mutex, Arc}; +#[cfg(test)] use crate::sync::MutexGuard; -use bitcoin::blockdata::transaction::{Transaction, SigHashType}; -use bitcoin::util::bip143; +use bitcoin::blockdata::transaction::{Transaction, EcdsaSighashType}; +use bitcoin::util::sighash; use bitcoin::secp256k1; -use bitcoin::secp256k1::key::{SecretKey, PublicKey}; -use bitcoin::secp256k1::{Secp256k1, Signature}; -use util::ser::{Writeable, Writer}; -use io::Error; +use bitcoin::secp256k1::{SecretKey, PublicKey}; +use bitcoin::secp256k1::{Secp256k1, ecdsa::Signature}; +#[cfg(anchors)] +use crate::events::bump_transaction::HTLCDescriptor; +use crate::util::ser::{Writeable, Writer}; +use crate::io::Error; /// Initial value for revoked commitment downward counter pub const INITIAL_REVOKED_COMMITMENT_NUMBER: u64 = 1 << 48; @@ -55,6 +58,12 @@ pub struct EnforcingSigner { pub disable_revocation_policy_check: bool, } +impl PartialEq for EnforcingSigner { + fn eq(&self, o: &Self) -> bool { + Arc::ptr_eq(&self.state, &o.state) + } +} + impl EnforcingSigner { /// Construct an EnforcingSigner pub fn new(inner: InMemorySigner) -> Self { @@ -87,7 +96,7 @@ impl EnforcingSigner { } } -impl BaseSign for EnforcingSigner { +impl ChannelSigner for EnforcingSigner { fn get_per_commitment_point(&self, idx: u64, secp_ctx: &Secp256k1) -> PublicKey { self.inner.get_per_commitment_point(idx, secp_ctx) } @@ -111,8 +120,15 @@ impl BaseSign for EnforcingSigner { } fn pubkeys(&self) -> &ChannelPublicKeys { self.inner.pubkeys() } + fn channel_keys_id(&self) -> [u8; 32] { self.inner.channel_keys_id() } + fn provide_channel_parameters(&mut self, channel_parameters: &ChannelTransactionParameters) { + self.inner.provide_channel_parameters(channel_parameters) + } +} + +impl EcdsaChannelSigner for EnforcingSigner { fn sign_counterparty_commitment(&self, commitment_tx: &CommitmentTransaction, preimages: Vec, secp_ctx: &Secp256k1) -> Result<(Signature, Vec), ()> { self.verify_counterparty_commitment_tx(commitment_tx, secp_ctx); @@ -156,12 +172,21 @@ impl BaseSign for EnforcingSigner { for (this_htlc, sig) in trusted_tx.htlcs().iter().zip(&commitment_tx.counterparty_htlc_sigs) { assert!(this_htlc.transaction_output_index.is_some()); let keys = trusted_tx.keys(); - let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, trusted_tx.feerate_per_kw(), holder_csv, &this_htlc, self.opt_anchors(), &keys.broadcaster_delayed_payment_key, &keys.revocation_key); + let htlc_tx = chan_utils::build_htlc_transaction(&commitment_txid, trusted_tx.feerate_per_kw(), holder_csv, &this_htlc, self.opt_anchors(), false, &keys.broadcaster_delayed_payment_key, &keys.revocation_key); let htlc_redeemscript = chan_utils::get_htlc_redeemscript(&this_htlc, self.opt_anchors(), &keys); - let sighash = hash_to_message!(&bip143::SigHashCache::new(&htlc_tx).signature_hash(0, &htlc_redeemscript, this_htlc.amount_msat / 1000, SigHashType::All)[..]); - secp_ctx.verify(&sighash, sig, &keys.countersignatory_htlc_key).unwrap(); + let sighash_type = if self.opt_anchors() { + EcdsaSighashType::SinglePlusAnyoneCanPay + } else { + EcdsaSighashType::All + }; + let sighash = hash_to_message!( + &sighash::SighashCache::new(&htlc_tx).segwit_signature_hash( + 0, &htlc_redeemscript, this_htlc.amount_msat / 1000, sighash_type, + ).unwrap()[..] + ); + secp_ctx.verify_ecdsa(&sighash, sig, &keys.countersignatory_htlc_key).unwrap(); } Ok(self.inner.sign_holder_commitment_and_htlcs(commitment_tx, secp_ctx).unwrap()) @@ -180,6 +205,17 @@ impl BaseSign for EnforcingSigner { Ok(self.inner.sign_justice_revoked_htlc(justice_tx, input, amount, per_commitment_key, htlc, secp_ctx).unwrap()) } + #[cfg(anchors)] + fn sign_holder_htlc_transaction( + &self, htlc_tx: &Transaction, input: usize, htlc_descriptor: &HTLCDescriptor, + secp_ctx: &Secp256k1 + ) -> Result { + let per_commitment_point = self.get_per_commitment_point(htlc_descriptor.per_commitment_number, secp_ctx); + assert_eq!(htlc_tx.input[input], htlc_descriptor.unsigned_tx_input()); + assert_eq!(htlc_tx.output[input], htlc_descriptor.tx_output(&per_commitment_point, secp_ctx)); + Ok(self.inner.sign_holder_htlc_transaction(htlc_tx, input, htlc_descriptor, secp_ctx).unwrap()) + } + fn sign_counterparty_htlc_transaction(&self, htlc_tx: &Transaction, input: usize, amount: u64, per_commitment_point: &PublicKey, htlc: &HTLCOutputInCommitment, secp_ctx: &Secp256k1) -> Result { Ok(self.inner.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap()) } @@ -190,22 +226,30 @@ impl BaseSign for EnforcingSigner { Ok(self.inner.sign_closing_transaction(closing_tx, secp_ctx).unwrap()) } - fn sign_channel_announcement(&self, msg: &msgs::UnsignedChannelAnnouncement, secp_ctx: &Secp256k1) -> Result { - self.inner.sign_channel_announcement(msg, secp_ctx) + fn sign_holder_anchor_input( + &self, anchor_tx: &Transaction, input: usize, secp_ctx: &Secp256k1, + ) -> Result { + debug_assert!(MIN_CHAN_DUST_LIMIT_SATOSHIS > ANCHOR_OUTPUT_VALUE_SATOSHI); + // As long as our minimum dust limit is enforced and is greater than our anchor output + // value, an anchor output can only have an index within [0, 1]. + assert!(anchor_tx.input[input].previous_output.vout == 0 || anchor_tx.input[input].previous_output.vout == 1); + self.inner.sign_holder_anchor_input(anchor_tx, input, secp_ctx) } - fn ready_channel(&mut self, channel_parameters: &ChannelTransactionParameters) { - self.inner.ready_channel(channel_parameters) + fn sign_channel_announcement_with_funding_key( + &self, msg: &msgs::UnsignedChannelAnnouncement, secp_ctx: &Secp256k1 + ) -> Result { + self.inner.sign_channel_announcement_with_funding_key(msg, secp_ctx) } } -impl Sign for EnforcingSigner {} +impl WriteableEcdsaChannelSigner for EnforcingSigner {} impl Writeable for EnforcingSigner { fn write(&self, writer: &mut W) -> Result<(), Error> { // EnforcingSigner has two fields - `inner` ([`InMemorySigner`]) and `state` // ([`EnforcementState`]). `inner` is serialized here and deserialized by - // [`KeysInterface::read_chan_signer`]. `state` is managed by [`KeysInterface`] + // [`SignerProvider::read_chan_signer`]. `state` is managed by [`SignerProvider`] // and will be serialized as needed by the implementation of that trait. self.inner.write(writer)?; Ok(())