Skip to content

Commit eebc0a9

Browse files
committed
Use ClosingTransaction in BaseSign
1 parent 54907a0 commit eebc0a9

File tree

4 files changed

+29
-34
lines changed

4 files changed

+29
-34
lines changed

lightning/src/chain/keysinterface.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use util::ser::{Writeable, Writer, Readable};
3434

3535
use chain::transaction::OutPoint;
3636
use ln::chan_utils;
37-
use ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction};
37+
use ln::chan_utils::{HTLCOutputInCommitment, make_funding_redeemscript, ChannelPublicKeys, HolderCommitmentTransaction, ChannelTransactionParameters, CommitmentTransaction, ClosingTransaction};
3838
use ln::msgs::UnsignedChannelAnnouncement;
3939
use ln::script::ShutdownScript;
4040

@@ -322,7 +322,7 @@ pub trait BaseSign {
322322
///
323323
/// Note that, due to rounding, there may be one "missing" satoshi, and either party may have
324324
/// chosen to forgo their output as dust.
325-
fn sign_closing_transaction(&self, closing_tx: &Transaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
325+
fn sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()>;
326326

327327
/// Signs a channel announcement message with our funding key, proving it comes from one
328328
/// of the channel participants.
@@ -671,17 +671,10 @@ impl BaseSign for InMemorySigner {
671671
Err(())
672672
}
673673

674-
fn sign_closing_transaction(&self, closing_tx: &Transaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
675-
if closing_tx.input.len() != 1 { return Err(()); }
676-
if closing_tx.input[0].witness.len() != 0 { return Err(()); }
677-
if closing_tx.output.len() > 2 { return Err(()); }
678-
674+
fn sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
679675
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
680676
let channel_funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &self.counterparty_pubkeys().funding_pubkey);
681-
682-
let sighash = hash_to_message!(&bip143::SigHashCache::new(closing_tx)
683-
.signature_hash(0, &channel_funding_redeemscript, self.channel_value_satoshis, SigHashType::All)[..]);
684-
Ok(secp_ctx.sign(&sighash, &self.funding_key))
677+
Ok(closing_tx.trust().sign(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx))
685678
}
686679

687680
fn sign_channel_announcement(&self, msg: &UnsignedChannelAnnouncement, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {

lightning/src/ln/chan_utils.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ pub fn build_commitment_secret(commitment_seed: &[u8; 32], idx: u64) -> [u8; 32]
8181
}
8282

8383
/// Build a closing transaction
84-
pub fn build_closing_transaction(value_to_holder: u64, value_to_counterparty: u64, holder_shutdown_script: Script, counterparty_shutdown_script: Script, funding_outpoint: OutPoint) -> Transaction {
84+
pub fn build_closing_transaction(to_holder_value_sat: u64, to_counterparty_value_sat: u64, to_holder_script: Script, to_counterparty_script: Script, funding_outpoint: OutPoint) -> Transaction {
8585
let txins = {
8686
let mut ins: Vec<TxIn> = Vec::new();
8787
ins.push(TxIn {
@@ -95,17 +95,17 @@ pub fn build_closing_transaction(value_to_holder: u64, value_to_counterparty: u6
9595

9696
let mut txouts: Vec<(TxOut, ())> = Vec::new();
9797

98-
if value_to_counterparty > 0 {
98+
if to_counterparty_value_sat > 0 {
9999
txouts.push((TxOut {
100-
script_pubkey: counterparty_shutdown_script,
101-
value: value_to_counterparty
100+
script_pubkey: to_counterparty_script,
101+
value: to_counterparty_value_sat
102102
}, ()));
103103
}
104104

105-
if value_to_holder > 0 {
105+
if to_holder_value_sat > 0 {
106106
txouts.push((TxOut {
107-
script_pubkey: holder_shutdown_script,
108-
value: value_to_holder
107+
script_pubkey: to_holder_script,
108+
value: to_holder_value_sat
109109
}, ()));
110110
}
111111

lightning/src/ln/channel.rs

+14-14
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use ln::msgs;
2828
use ln::msgs::{DecodeError, OptionalField, DataLossProtect};
2929
use ln::script::ShutdownScript;
3030
use ln::channelmanager::{PendingHTLCStatus, HTLCSource, HTLCFailReason, HTLCFailureMsg, PendingHTLCInfo, RAACommitmentOrder, BREAKDOWN_TIMEOUT, MIN_CLTV_EXPIRY_DELTA, MAX_LOCAL_BREAKDOWN_TIMEOUT};
31-
use ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction, HolderCommitmentTransaction, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, MAX_HTLCS, get_commitment_transaction_number_obscure_factor, build_closing_transaction};
31+
use ln::chan_utils::{CounterpartyCommitmentSecrets, TxCreationKeys, HTLCOutputInCommitment, HTLC_SUCCESS_TX_WEIGHT, HTLC_TIMEOUT_TX_WEIGHT, make_funding_redeemscript, ChannelPublicKeys, CommitmentTransaction, HolderCommitmentTransaction, ChannelTransactionParameters, CounterpartyChannelTransactionParameters, MAX_HTLCS, get_commitment_transaction_number_obscure_factor, ClosingTransaction};
3232
use ln::chan_utils;
3333
use chain::BestBlock;
3434
use chain::chaininterface::{FeeEstimator,ConfirmationTarget};
@@ -1285,7 +1285,7 @@ impl<Signer: Sign> Channel<Signer> {
12851285
}
12861286

12871287
#[inline]
1288-
fn build_closing_transaction(&self, proposed_total_fee_satoshis: u64, skip_remote_output: bool) -> (Transaction, u64) {
1288+
fn build_closing_transaction(&self, proposed_total_fee_satoshis: u64, skip_remote_output: bool) -> (ClosingTransaction, u64) {
12891289
assert!(self.pending_inbound_htlcs.is_empty());
12901290
assert!(self.pending_outbound_htlcs.is_empty());
12911291
assert!(self.pending_update_fee.is_none());
@@ -1315,7 +1315,8 @@ impl<Signer: Sign> Channel<Signer> {
13151315
let counterparty_shutdown_script = self.counterparty_shutdown_scriptpubkey.clone().unwrap();
13161316
let funding_outpoint = self.funding_outpoint().into_bitcoin_outpoint();
13171317

1318-
(build_closing_transaction(value_to_holder as u64, value_to_counterparty as u64, holder_shutdown_script, counterparty_shutdown_script, funding_outpoint), total_fee_satoshis)
1318+
let closing_transaction = ClosingTransaction::new(value_to_holder as u64, value_to_counterparty as u64, holder_shutdown_script, counterparty_shutdown_script, funding_outpoint);
1319+
(closing_transaction, total_fee_satoshis)
13191320
}
13201321

13211322
fn funding_outpoint(&self) -> OutPoint {
@@ -3583,10 +3584,8 @@ impl<Signer: Sign> Channel<Signer> {
35833584
Ok((shutdown, monitor_update, dropped_outbound_htlcs))
35843585
}
35853586

3586-
fn build_signed_closing_transaction(&self, tx: &mut Transaction, counterparty_sig: &Signature, sig: &Signature) {
3587-
if tx.input.len() != 1 { panic!("Tried to sign closing transaction that had input count != 1!"); }
3588-
if tx.input[0].witness.len() != 0 { panic!("Tried to re-sign closing transaction"); }
3589-
if tx.output.len() > 2 { panic!("Tried to sign bogus closing transaction"); }
3587+
fn build_signed_closing_transaction(&self, closing_tx: &ClosingTransaction, counterparty_sig: &Signature, sig: &Signature) -> Transaction {
3588+
let mut tx = closing_tx.trust().built_transaction().clone();
35903589

35913590
tx.input[0].witness.push(Vec::new()); // First is the multisig dummy
35923591

@@ -3603,6 +3602,7 @@ impl<Signer: Sign> Channel<Signer> {
36033602
tx.input[0].witness[2].push(SigHashType::All as u8);
36043603

36053604
tx.input[0].witness.push(self.get_funding_redeemscript().into_bytes());
3605+
tx
36063606
}
36073607

36083608
pub fn closing_signed<F: Deref>(&mut self, fee_estimator: &F, msg: &msgs::ClosingSigned) -> Result<(Option<msgs::ClosingSigned>, Option<Transaction>), ChannelError>
@@ -3635,47 +3635,47 @@ impl<Signer: Sign> Channel<Signer> {
36353635
if used_total_fee != msg.fee_satoshis {
36363636
return Err(ChannelError::Close(format!("Remote sent us a closing_signed with a fee other than the value they can claim. Fee in message: {}. Actual closing tx fee: {}", msg.fee_satoshis, used_total_fee)));
36373637
}
3638-
let mut sighash = hash_to_message!(&bip143::SigHashCache::new(&closing_tx).signature_hash(0, &funding_redeemscript, self.channel_value_satoshis, SigHashType::All)[..]);
3638+
let sighash = closing_tx.trust().get_sighash_all(&funding_redeemscript, self.channel_value_satoshis);
36393639

36403640
match self.secp_ctx.verify(&sighash, &msg.signature, &self.get_counterparty_pubkeys().funding_pubkey) {
36413641
Ok(_) => {},
36423642
Err(_e) => {
36433643
// The remote end may have decided to revoke their output due to inconsistent dust
36443644
// limits, so check for that case by re-checking the signature here.
36453645
closing_tx = self.build_closing_transaction(msg.fee_satoshis, true).0;
3646-
sighash = hash_to_message!(&bip143::SigHashCache::new(&closing_tx).signature_hash(0, &funding_redeemscript, self.channel_value_satoshis, SigHashType::All)[..]);
3646+
let sighash = closing_tx.trust().get_sighash_all(&funding_redeemscript, self.channel_value_satoshis);
36473647
secp_check!(self.secp_ctx.verify(&sighash, &msg.signature, self.counterparty_funding_pubkey()), "Invalid closing tx signature from peer".to_owned());
36483648
},
36493649
};
36503650

36513651
assert!(self.shutdown_scriptpubkey.is_some());
36523652
if let Some((last_fee, sig)) = self.last_sent_closing_fee {
36533653
if last_fee == msg.fee_satoshis {
3654-
self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
3654+
let tx = self.build_signed_closing_transaction(&mut closing_tx, &msg.signature, &sig);
36553655
self.channel_state = ChannelState::ShutdownComplete as u32;
36563656
self.update_time_counter += 1;
3657-
return Ok((None, Some(closing_tx)));
3657+
return Ok((None, Some(tx)));
36583658
}
36593659
}
36603660

36613661
let (our_min_fee, our_max_fee) = self.calculate_closing_fee_limits(fee_estimator);
36623662

36633663
macro_rules! propose_fee {
36643664
($new_fee: expr) => {
3665-
let (mut tx, used_fee) = if $new_fee == msg.fee_satoshis {
3665+
let (closing_tx, used_fee) = if $new_fee == msg.fee_satoshis {
36663666
(closing_tx, $new_fee)
36673667
} else {
36683668
self.build_closing_transaction($new_fee, false)
36693669
};
36703670

36713671
let sig = self.holder_signer
3672-
.sign_closing_transaction(&tx, &self.secp_ctx)
3672+
.sign_closing_transaction(&closing_tx, &self.secp_ctx)
36733673
.map_err(|_| ChannelError::Close("External signer refused to sign closing transaction".to_owned()))?;
36743674

36753675
let signed_tx = if $new_fee == msg.fee_satoshis {
36763676
self.channel_state = ChannelState::ShutdownComplete as u32;
36773677
self.update_time_counter += 1;
3678-
self.build_signed_closing_transaction(&mut tx, &msg.signature, &sig);
3678+
let tx = self.build_signed_closing_transaction(&closing_tx, &msg.signature, &sig);
36793679
Some(tx)
36803680
} else { None };
36813681

lightning/src/util/enforcing_trait_impls.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
// You may not use this file except in accordance with one or both of these
88
// licenses.
99

10-
use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction};
10+
use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction, ClosingTransaction};
1111
use ln::{chan_utils, msgs};
1212
use chain::keysinterface::{Sign, InMemorySigner, BaseSign};
1313

@@ -182,7 +182,9 @@ impl BaseSign for EnforcingSigner {
182182
Ok(self.inner.sign_counterparty_htlc_transaction(htlc_tx, input, amount, per_commitment_point, htlc, secp_ctx).unwrap())
183183
}
184184

185-
fn sign_closing_transaction(&self, closing_tx: &Transaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
185+
fn sign_closing_transaction(&self, closing_tx: &ClosingTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<Signature, ()> {
186+
closing_tx.verify(self.inner.funding_outpoint().into_bitcoin_outpoint())
187+
.expect("derived different closing transaction");
186188
Ok(self.inner.sign_closing_transaction(closing_tx, secp_ctx).unwrap())
187189
}
188190

0 commit comments

Comments
 (0)