Skip to content

Commit b36617d

Browse files
committed
Panic if signing fails in OnChainTx
Signatures in OnChainTx must not fail, or we stand to lose funds
1 parent 31e0514 commit b36617d

File tree

1 file changed

+24
-35
lines changed

1 file changed

+24
-35
lines changed

lightning/src/ln/onchaintx.rs

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

599-
if let Ok(sig) = self.key_storage.sign_justice_transaction(&bumped_tx, i, *amount, &per_commitment_key, htlc, &self.secp_ctx) {
600-
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
601-
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
602-
if htlc.is_some() {
603-
bumped_tx.input[i].witness.push(chan_keys.revocation_key.clone().serialize().to_vec());
604-
} else {
605-
bumped_tx.input[i].witness.push(vec!(1));
606-
}
607-
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
608-
} else { return None; }
609-
//TODO: panic ?
599+
let sig = self.key_storage.sign_justice_transaction(&bumped_tx, i, *amount, &per_commitment_key, htlc, &self.secp_ctx).expect("sign justice tx");
600+
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
601+
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
602+
if htlc.is_some() {
603+
bumped_tx.input[i].witness.push(chan_keys.revocation_key.clone().serialize().to_vec());
604+
} else {
605+
bumped_tx.input[i].witness.push(vec!(1));
606+
}
607+
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
610608

611609
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);
612610
}
@@ -616,17 +614,16 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
616614
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);
617615

618616
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
619-
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) {
620-
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
621-
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
622-
if let &Some(preimage) = preimage {
623-
bumped_tx.input[i].witness.push(preimage.0.to_vec());
624-
} else {
625-
// Due to BIP146 (MINIMALIF) this must be a zero-length element to relay.
626-
bumped_tx.input[i].witness.push(vec![]);
627-
}
628-
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
617+
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");
618+
bumped_tx.input[i].witness.push(sig.serialize_der().to_vec());
619+
bumped_tx.input[i].witness[0].push(SigHashType::All as u8);
620+
if let &Some(preimage) = preimage {
621+
bumped_tx.input[i].witness.push(preimage.0.to_vec());
622+
} else {
623+
// Due to BIP146 (MINIMALIF) this must be a zero-length element to relay.
624+
bumped_tx.input[i].witness.push(vec![]);
629625
}
626+
bumped_tx.input[i].witness.push(witness_script.clone().into_bytes());
630627
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);
631628
}
632629
},
@@ -936,13 +933,9 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
936933
// to monitor before.
937934
pub(crate) fn get_fully_signed_holder_tx(&mut self, funding_redeemscript: &Script) -> Option<Transaction> {
938935
if let Some(ref mut holder_commitment) = self.holder_commitment {
939-
match self.key_storage.sign_holder_commitment(holder_commitment, &self.secp_ctx) {
940-
Ok((sig, htlc_sigs)) => {
941-
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(holder_commitment, htlc_sigs));
942-
Some(holder_commitment.add_holder_sig(funding_redeemscript, sig))
943-
},
944-
Err(_) => return None,
945-
}
936+
let (sig, htlc_sigs) = self.key_storage.sign_holder_commitment(holder_commitment, &self.secp_ctx).expect("signing holder commitment");
937+
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(holder_commitment, htlc_sigs));
938+
Some(holder_commitment.add_holder_sig(funding_redeemscript, sig))
946939
} else {
947940
None
948941
}
@@ -951,13 +944,9 @@ impl<ChanSigner: ChannelKeys> OnchainTxHandler<ChanSigner> {
951944
#[cfg(any(test, feature="unsafe_revoked_tx_signing"))]
952945
pub(crate) fn get_fully_signed_copy_holder_tx(&mut self, funding_redeemscript: &Script) -> Option<Transaction> {
953946
if let Some(ref mut holder_commitment) = self.holder_commitment {
954-
match self.key_storage.sign_holder_commitment(holder_commitment, &self.secp_ctx) {
955-
Ok((sig, htlc_sigs)) => {
956-
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(holder_commitment, htlc_sigs));
957-
Some(holder_commitment.add_holder_sig(funding_redeemscript, sig))
958-
},
959-
Err(_) => return None,
960-
}
947+
let (sig, htlc_sigs) = self.key_storage.sign_holder_commitment(holder_commitment, &self.secp_ctx).expect("sign holder commitment");
948+
self.holder_htlc_sigs = Some(Self::extract_holder_sigs(holder_commitment, htlc_sigs));
949+
Some(holder_commitment.add_holder_sig(funding_redeemscript, sig))
961950
} else {
962951
None
963952
}

0 commit comments

Comments
 (0)