Skip to content

Commit ed30b2c

Browse files
committed
Panic if signing fails in OnChainTx
Signatures in OnChainTx must not fail, or we stand to lose funds
1 parent 4a0a1ce commit ed30b2c

File tree

2 files changed

+25
-36
lines changed

2 files changed

+25
-36
lines changed

lightning/src/ln/onchaintx.rs

Lines changed: 24 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -612,17 +612,15 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
612612
chan_utils::get_revokeable_redeemscript(&chan_keys.revocation_key, *on_counterparty_tx_csv, &chan_keys.broadcaster_delayed_payment_key)
613613
};
614614

615-
if let Ok(sig) = self.key_storage.sign_justice_transaction(&bumped_tx, i, *amount, &per_commitment_key, htlc, &self.secp_ctx) {
616-
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
617-
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
618-
if htlc.is_some() {
619-
bumped_tx.input[i].witness.push(chan_keys.revocation_key.clone().serialize().to_vec());
620-
} else {
621-
bumped_tx.input[i].witness.push(vec!(1));
622-
}
623-
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
624-
} else { return None; }
625-
//TODO: panic ?
615+
let sig = self.key_storage.sign_justice_transaction(&bumped_tx, i, *amount, &per_commitment_key, htlc, &self.secp_ctx).expect("sign justice tx");
616+
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
617+
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
618+
if htlc.is_some() {
619+
bumped_tx.input[i].witness.push(chan_keys.revocation_key.clone().serialize().to_vec());
620+
} else {
621+
bumped_tx.input[i].witness.push(vec!(1));
622+
}
623+
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
626624

627625
log_trace!(logger, "Going to broadcast Penalty Transaction {} claiming revoked {} output {} from {} with new feerate {}...", bumped_tx.txid(), if *input_descriptor == InputDescriptors::RevokedOutput { "to_holder" } else if *input_descriptor == InputDescriptors::RevokedOfferedHTLC { "offered" } else if *input_descriptor == InputDescriptors::RevokedReceivedHTLC { "received" } else { "" }, outp.vout, outp.txid, new_feerate);
628626
}
@@ -632,17 +630,16 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
632630
let witness_script = chan_utils::get_htlc_redeemscript_with_explicit_keys(&htlc, &chan_keys.broadcaster_htlc_key, &chan_keys.countersignatory_htlc_key, &chan_keys.revocation_key);
633631

634632
if !preimage.is_some() { bumped_tx.lock_time = htlc.cltv_expiry }; // Right now we don't aggregate time-locked transaction, if we do we should set lock_time before to avoid breaking hash computation
635-
if let Ok(sig) = self.key_storage.sign_counterparty_htlc_transaction(&bumped_tx, i, &htlc.amount_msat / 1000, &per_commitment_point, htlc, &self.secp_ctx) {
636-
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
637-
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
638-
if let &Some(preimage) = preimage {
639-
bumped_tx.input[i].witness.push(preimage.0.to_vec());
640-
} else {
641-
// Due to BIP146 (MINIMALIF) this must be a zero-length element to relay.
642-
bumped_tx.input[i].witness.push(vec![]);
643-
}
644-
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
633+
let sig = self.key_storage.sign_counterparty_htlc_transaction(&bumped_tx, i, &htlc.amount_msat / 1000, &per_commitment_point, htlc, &self.secp_ctx).expect("sign counterparty HTLC tx");
634+
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
635+
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
636+
if let &Some(preimage) = preimage {
637+
bumped_tx.input[i].witness.push(preimage.0.to_vec());
638+
} else {
639+
// Due to BIP146 (MINIMALIF) this must be a zero-length element to relay.
640+
bumped_tx.input[i].witness.push(vec![]);
645641
}
642+
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
646643
log_trace!(logger, "Going to broadcast Claim Transaction {} claiming counterparty {} htlc output {} from {} with new feerate {}...", bumped_tx.txid(), if preimage.is_some() { "offered" } else { "received" }, outp.vout, outp.txid, new_feerate);
647644
}
648645
},
@@ -952,13 +949,9 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
952949
// to monitor before.
953950
pub(crate) fn get_fully_signed_holder_tx(&mut self, funding_redeemscript: &Script) -> Option<Transaction> {
954951
if let Some(ref mut holder_commitment) = self.holder_commitment {
955-
match self.key_storage.sign_holder_commitment(holder_commitment, &self.secp_ctx) {
956-
Ok((sig, htlc_sigs)) => {
957-
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(holder_commitment, htlc_sigs));
958-
Some(holder_commitment.add_holder_sig(funding_redeemscript, sig))
959-
},
960-
Err(_) => return None,
961-
}
952+
let (sig, htlc_sigs) = self.key_storage.sign_holder_commitment(holder_commitment, &self.secp_ctx).expect("signing holder commitment");
953+
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(holder_commitment, htlc_sigs));
954+
Some(holder_commitment.add_holder_sig(funding_redeemscript, sig))
962955
} else {
963956
None
964957
}
@@ -967,13 +960,9 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
967960
#[cfg(any(test, feature="unsafe_revoked_tx_signing"))]
968961
pub(crate) fn get_fully_signed_copy_holder_tx(&mut self, funding_redeemscript: &Script) -> Option<Transaction> {
969962
if let Some(ref mut holder_commitment) = self.holder_commitment {
970-
match self.key_storage.sign_holder_commitment(holder_commitment, &self.secp_ctx) {
971-
Ok((sig, htlc_sigs)) => {
972-
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(holder_commitment, htlc_sigs));
973-
Some(holder_commitment.add_holder_sig(funding_redeemscript, sig))
974-
},
975-
Err(_) => return None,
976-
}
963+
let (sig, htlc_sigs) = self.key_storage.sign_holder_commitment(holder_commitment, &self.secp_ctx).expect("sign holder commitment");
964+
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(holder_commitment, htlc_sigs));
965+
Some(holder_commitment.add_holder_sig(funding_redeemscript, sig))
977966
} else {
978967
None
979968
}

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
// licenses.
99

1010
use ln::chan_utils::{HTLCOutputInCommitment, ChannelPublicKeys, HolderCommitmentTransaction, CommitmentTransaction, ChannelTransactionParameters, TrustedCommitmentTransaction};
11-
use ln::{msgs, chan_utils};
11+
use ln::{chan_utils, msgs};
1212
use chain::keysinterface::{ChannelKeys, InMemoryChannelKeys};
1313

1414
use std::cmp;

0 commit comments

Comments
 (0)