Skip to content

Commit 8ec1b79

Browse files
committed
Drop redundant parameters in sign_local_commitment_tx
The ChanKeys is created with knowledge of the Channel's value and funding redeemscript up-front, so we should not be providing it when making signing requests.
1 parent ec728ab commit 8ec1b79

File tree

5 files changed

+33
-30
lines changed

5 files changed

+33
-30
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -222,15 +222,15 @@ pub trait ChannelKeys : Send+Clone {
222222
/// TODO: Add more input vars to enable better checking (preferably removing commitment_tx and
223223
/// TODO: Ensure test-only version doesn't enforce uniqueness of signature when it's enforced in this method
224224
/// making the callee generate it via some util function we expose)!
225-
fn sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>);
225+
fn sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>);
226226

227227
/// Create a signature for a local commitment transaction without enforcing one-time signing.
228228
///
229229
/// Testing revocation logic by our test framework needs to sign multiple local commitment
230230
/// transactions. This unsafe test-only version doesn't enforce one-time signing security
231231
/// requirement.
232232
#[cfg(test)]
233-
fn unsafe_sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>);
233+
fn unsafe_sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>);
234234

235235
/// Signs a transaction created by build_htlc_transaction. If the transaction is an
236236
/// HTLC-Success transaction, preimage must be set!
@@ -363,13 +363,21 @@ impl ChannelKeys for InMemoryChannelKeys {
363363
Ok((commitment_sig, htlc_sigs))
364364
}
365365

366-
fn sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) {
367-
local_commitment_tx.add_local_sig(&self.funding_key, funding_redeemscript, channel_value_satoshis, secp_ctx);
366+
fn sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>) {
367+
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
368+
let remote_channel_pubkeys = self.remote_channel_pubkeys.as_ref().expect("must set remote channel pubkeys before signing");
369+
let channel_funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &remote_channel_pubkeys.funding_pubkey);
370+
371+
local_commitment_tx.add_local_sig(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx);
368372
}
369373

370374
#[cfg(test)]
371-
fn unsafe_sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) {
372-
local_commitment_tx.add_local_sig(&self.funding_key, funding_redeemscript, channel_value_satoshis, secp_ctx);
375+
fn unsafe_sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>) {
376+
let funding_pubkey = PublicKey::from_secret_key(secp_ctx, &self.funding_key);
377+
let remote_channel_pubkeys = self.remote_channel_pubkeys.as_ref().expect("must set remote channel pubkeys before signing");
378+
let channel_funding_redeemscript = make_funding_redeemscript(&funding_pubkey, &remote_channel_pubkeys.funding_pubkey);
379+
380+
local_commitment_tx.add_local_sig(&self.funding_key, &channel_funding_redeemscript, self.channel_value_satoshis, secp_ctx);
373381
}
374382

375383
fn sign_htlc_transaction<T: secp256k1::Signing>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, htlc_index: u32, preimage: Option<PaymentPreimage>, local_csv: u16, secp_ctx: &Secp256k1<T>) {

lightning/src/ln/channel.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4418,7 +4418,7 @@ mod tests {
44184418
let logger : Arc<Logger> = Arc::new(test_utils::TestLogger::new());
44194419
let secp_ctx = Secp256k1::new();
44204420

4421-
let chan_keys = InMemoryChannelKeys::new(
4421+
let mut chan_keys = InMemoryChannelKeys::new(
44224422
&secp_ctx,
44234423
SecretKey::from_slice(&hex::decode("30ff4956bbdd3222d44cc5e8a1261dab1e07957bdac5ae88fe3261ef321f3749").unwrap()[..]).unwrap(),
44244424
SecretKey::from_slice(&hex::decode("0fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff").unwrap()[..]).unwrap(),
@@ -4428,7 +4428,7 @@ mod tests {
44284428

44294429
// These aren't set in the test vectors:
44304430
[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff],
4431-
7000000000,
4431+
10_000_000,
44324432
);
44334433

44344434
assert_eq!(PublicKey::from_secret_key(&secp_ctx, chan_keys.funding_key()).serialize()[..],
@@ -4438,7 +4438,7 @@ mod tests {
44384438
let their_node_id = PublicKey::from_secret_key(&secp_ctx, &SecretKey::from_slice(&[42; 32]).unwrap());
44394439
let mut config = UserConfig::default();
44404440
config.channel_options.announced_channel = false;
4441-
let mut chan = Channel::<InMemoryChannelKeys>::new_outbound(&&feeest, &&keys_provider, their_node_id, 10000000, 100000, 42, Arc::clone(&logger), &config).unwrap(); // Nothing uses their network key in this test
4441+
let mut chan = Channel::<InMemoryChannelKeys>::new_outbound(&&feeest, &&keys_provider, their_node_id, 10_000_000, 100000, 42, Arc::clone(&logger), &config).unwrap(); // Nothing uses their network key in this test
44424442
chan.their_to_self_delay = 144;
44434443
chan.our_dust_limit_satoshis = 546;
44444444

@@ -4452,6 +4452,7 @@ mod tests {
44524452
delayed_payment_basepoint: public_from_secret_hex(&secp_ctx, "1552dfba4f6cf29a62a0af13c8d6981d36d0ef8d61ba10fb0fe90da7634d7e13"),
44534453
htlc_basepoint: public_from_secret_hex(&secp_ctx, "4444444444444444444444444444444444444444444444444444444444444444")
44544454
};
4455+
chan_keys.set_remote_channel_pubkeys(&their_pubkeys);
44554456

44564457
assert_eq!(their_pubkeys.payment_basepoint.serialize()[..],
44574458
hex::decode("032c0b7cf95324a07d05398b240174dc0c2be444d96b159aa6c7f7b1e668680991").unwrap()[..]);
@@ -4491,7 +4492,7 @@ mod tests {
44914492
secp_ctx.verify(&sighash, &their_signature, chan.their_funding_pubkey()).unwrap();
44924493

44934494
localtx = LocalCommitmentTransaction::new_missing_local_sig(unsigned_tx.0.clone(), &their_signature, &PublicKey::from_secret_key(&secp_ctx, chan.local_keys.funding_key()), chan.their_funding_pubkey());
4494-
chan_keys.sign_local_commitment(&mut localtx, &redeemscript, chan.channel_value_satoshis, &chan.secp_ctx);
4495+
chan_keys.sign_local_commitment(&mut localtx, &chan.secp_ctx);
44954496

44964497
assert_eq!(serialize(localtx.with_valid_witness())[..],
44974498
hex::decode($tx_hex).unwrap()[..]);

lightning/src/ln/channelmonitor.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1086,7 +1086,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
10861086
onchain_events_waiting_threshold_conf: HashMap::new(),
10871087
outputs_to_watch: HashMap::new(),
10881088

1089-
onchain_tx_handler: OnchainTxHandler::new(destination_script.clone(), keys, funding_redeemscript, their_to_self_delay, logger.clone()),
1089+
onchain_tx_handler: OnchainTxHandler::new(destination_script.clone(), keys, their_to_self_delay, logger.clone()),
10901090

10911091
lockdown_from_offchain: false,
10921092

@@ -1743,7 +1743,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17431743
/// In any-case, choice is up to the user.
17441744
pub fn get_latest_local_commitment_txn(&mut self) -> Vec<Transaction> {
17451745
log_trace!(self, "Getting signed latest local commitment transaction!");
1746-
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_local_tx(self.channel_value_satoshis) {
1746+
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_local_tx() {
17471747
let txid = commitment_tx.txid();
17481748
let mut res = vec![commitment_tx];
17491749
if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
@@ -1769,7 +1769,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
17691769
#[cfg(test)]
17701770
pub fn unsafe_get_latest_local_commitment_txn(&mut self) -> Vec<Transaction> {
17711771
log_trace!(self, "Getting signed copy of latest local commitment transaction!");
1772-
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_copy_local_tx(self.channel_value_satoshis) {
1772+
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_copy_local_tx() {
17731773
let txid = commitment_tx.txid();
17741774
let mut res = vec![commitment_tx];
17751775
if let &Some(ref local_tx) = &self.current_local_signed_commitment_tx {
@@ -1855,7 +1855,7 @@ impl<ChanSigner: ChannelKeys> ChannelMonitor<ChanSigner> {
18551855
}
18561856
if let Some(ref cur_local_tx) = self.current_local_signed_commitment_tx {
18571857
if should_broadcast {
1858-
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_local_tx(self.channel_value_satoshis) {
1858+
if let Some(commitment_tx) = self.onchain_tx_handler.get_fully_signed_local_tx() {
18591859
let (mut new_outpoints, new_outputs, _) = self.broadcast_by_local_state(&commitment_tx, cur_local_tx);
18601860
if !new_outputs.is_empty() {
18611861
watch_outputs.push((cur_local_tx.txid.clone(), new_outputs));

lightning/src/ln/onchaintx.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,6 @@ macro_rules! subtract_high_prio_fee {
142142
/// do RBF bumping if possible.
143143
pub struct OnchainTxHandler<ChanSigner: ChannelKeys> {
144144
destination_script: Script,
145-
funding_redeemscript: Script,
146145
local_commitment: Option<LocalCommitmentTransaction>,
147146
prev_local_commitment: Option<LocalCommitmentTransaction>,
148147
local_csv: u16,
@@ -185,7 +184,6 @@ pub struct OnchainTxHandler<ChanSigner: ChannelKeys> {
185184
impl<ChanSigner: ChannelKeys + Writeable> OnchainTxHandler<ChanSigner> {
186185
pub(crate) fn write<W: Writer>(&self, writer: &mut W) -> Result<(), ::std::io::Error> {
187186
self.destination_script.write(writer)?;
188-
self.funding_redeemscript.write(writer)?;
189187
self.local_commitment.write(writer)?;
190188
self.prev_local_commitment.write(writer)?;
191189

@@ -231,7 +229,6 @@ impl<ChanSigner: ChannelKeys + Writeable> OnchainTxHandler<ChanSigner> {
231229
impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTxHandler<ChanSigner> {
232230
fn read<R: ::std::io::Read>(reader: &mut R, logger: Arc<Logger>) -> Result<Self, DecodeError> {
233231
let destination_script = Readable::read(reader)?;
234-
let funding_redeemscript = Readable::read(reader)?;
235232

236233
let local_commitment = Readable::read(reader)?;
237234
let prev_local_commitment = Readable::read(reader)?;
@@ -285,7 +282,6 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
285282

286283
Ok(OnchainTxHandler {
287284
destination_script,
288-
funding_redeemscript,
289285
local_commitment,
290286
prev_local_commitment,
291287
local_csv,
@@ -300,13 +296,12 @@ impl<ChanSigner: ChannelKeys + Readable> ReadableArgs<Arc<Logger>> for OnchainTx
300296
}
301297

302298
impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
303-
pub(super) fn new(destination_script: Script, keys: ChanSigner, funding_redeemscript: Script, local_csv: u16, logger: Arc<Logger>) -> Self {
299+
pub(super) fn new(destination_script: Script, keys: ChanSigner, local_csv: u16, logger: Arc<Logger>) -> Self {
304300

305301
let key_storage = keys;
306302

307303
OnchainTxHandler {
308304
destination_script,
309-
funding_redeemscript,
310305
local_commitment: None,
311306
prev_local_commitment: None,
312307
local_csv,
@@ -537,7 +532,7 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
537532
return None;
538533
},
539534
&InputMaterial::Funding { ref channel_value } => {
540-
let signed_tx = self.get_fully_signed_local_tx(*channel_value).unwrap();
535+
let signed_tx = self.get_fully_signed_local_tx().unwrap();
541536
let mut amt_outputs = 0;
542537
for outp in signed_tx.output.iter() {
543538
amt_outputs += outp.value;
@@ -790,19 +785,19 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
790785
// have empty local commitment transaction if a ChannelMonitor is asked to force-close just after Channel::get_outbound_funding_created,
791786
// before providing a initial commitment transaction. For outbound channel, init ChannelMonitor at Channel::funding_signed, there is nothing
792787
// to monitor before.
793-
pub(super) fn get_fully_signed_local_tx(&mut self, channel_value_satoshis: u64) -> Option<Transaction> {
788+
pub(super) fn get_fully_signed_local_tx(&mut self) -> Option<Transaction> {
794789
if let Some(ref mut local_commitment) = self.local_commitment {
795-
self.key_storage.sign_local_commitment(local_commitment, &self.funding_redeemscript, channel_value_satoshis, &self.secp_ctx);
790+
self.key_storage.sign_local_commitment(local_commitment, &self.secp_ctx);
796791
return Some(local_commitment.with_valid_witness().clone());
797792
}
798793
None
799794
}
800795

801796
#[cfg(test)]
802-
pub(super) fn get_fully_signed_copy_local_tx(&mut self, channel_value_satoshis: u64) -> Option<Transaction> {
797+
pub(super) fn get_fully_signed_copy_local_tx(&mut self) -> Option<Transaction> {
803798
if let Some(ref mut local_commitment) = self.local_commitment {
804799
let mut local_commitment = local_commitment.clone();
805-
self.key_storage.unsafe_sign_local_commitment(&mut local_commitment, &self.funding_redeemscript, channel_value_satoshis, &self.secp_ctx);
800+
self.key_storage.unsafe_sign_local_commitment(&mut local_commitment, &self.secp_ctx);
806801
return Some(local_commitment.with_valid_witness().clone());
807802
}
808803
None

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use std::cmp;
77
use std::sync::{Mutex, Arc};
88

99
use bitcoin::blockdata::transaction::Transaction;
10-
use bitcoin::blockdata::script::Script;
1110

1211
use secp256k1;
1312
use secp256k1::key::{SecretKey, PublicKey};
@@ -80,13 +79,13 @@ impl ChannelKeys for EnforcingChannelKeys {
8079
Ok(self.inner.sign_remote_commitment(feerate_per_kw, commitment_tx, keys, htlcs, to_self_delay, secp_ctx).unwrap())
8180
}
8281

83-
fn sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) {
84-
self.inner.sign_local_commitment(local_commitment_tx, funding_redeemscript, channel_value_satoshis, secp_ctx)
82+
fn sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>) {
83+
self.inner.sign_local_commitment(local_commitment_tx, secp_ctx)
8584
}
8685

8786
#[cfg(test)]
88-
fn unsafe_sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, funding_redeemscript: &Script, channel_value_satoshis: u64, secp_ctx: &Secp256k1<T>) {
89-
self.inner.unsafe_sign_local_commitment(local_commitment_tx, funding_redeemscript, channel_value_satoshis, secp_ctx);
87+
fn unsafe_sign_local_commitment<T: secp256k1::Signing + secp256k1::Verification>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, secp_ctx: &Secp256k1<T>) {
88+
self.inner.unsafe_sign_local_commitment(local_commitment_tx, secp_ctx);
9089
}
9190

9291
fn sign_htlc_transaction<T: secp256k1::Signing>(&self, local_commitment_tx: &mut LocalCommitmentTransaction, htlc_index: u32, preimage: Option<PaymentPreimage>, local_csv: u16, secp_ctx: &Secp256k1<T>) {

0 commit comments

Comments
 (0)