Skip to content

Commit 608ed12

Browse files
committed
Allow BaseSign validation functions to return an Err
1 parent a016bb1 commit 608ed12

File tree

3 files changed

+19
-10
lines changed

3 files changed

+19
-10
lines changed

lightning/src/chain/keysinterface.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ pub trait BaseSign {
218218
/// secret won't leave us without a broadcastable holder transaction.
219219
/// Policy checks should be implemented in this function, including checking the amount
220220
/// sent to us and checking the HTLCs.
221-
fn validate_holder_commitment(&self, holder_tx: &HolderCommitmentTransaction);
221+
fn validate_holder_commitment(&self, holder_tx: &HolderCommitmentTransaction) -> Result<(), ()>;
222222
/// Gets the holder's channel public keys and basepoints
223223
fn pubkeys(&self) -> &ChannelPublicKeys;
224224
/// Gets an arbitrary identifier describing the set of keys which are provided back to you in
@@ -239,7 +239,7 @@ pub trait BaseSign {
239239
///
240240
/// This is required in order for the signer to make sure that the state has moved
241241
/// forward and it is safe to sign the next counterparty commitment.
242-
fn validate_counterparty_revocation(&self, idx: u64, secret: &SecretKey);
242+
fn validate_counterparty_revocation(&self, idx: u64, secret: &SecretKey) -> Result<(), ()>;
243243

244244
/// Create a signatures for a holder's commitment transaction and its claiming HTLC transactions.
245245
/// This will only ever be called with a non-revoked commitment_tx. This will be called with the
@@ -573,7 +573,8 @@ impl BaseSign for InMemorySigner {
573573
chan_utils::build_commitment_secret(&self.commitment_seed, idx)
574574
}
575575

576-
fn validate_holder_commitment(&self, _holder_tx: &HolderCommitmentTransaction) {
576+
fn validate_holder_commitment(&self, _holder_tx: &HolderCommitmentTransaction) -> Result<(), ()> {
577+
Ok(())
577578
}
578579

579580
fn pubkeys(&self) -> &ChannelPublicKeys { &self.holder_channel_pubkeys }
@@ -602,7 +603,8 @@ impl BaseSign for InMemorySigner {
602603
Ok((commitment_sig, htlc_sigs))
603604
}
604605

605-
fn validate_counterparty_revocation(&self, _idx: u64, _secret: &SecretKey) {
606+
fn validate_counterparty_revocation(&self, _idx: u64, _secret: &SecretKey) -> Result<(), ()> {
607+
Ok(())
606608
}
607609

608610
fn sign_holder_commitment_and_htlcs(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<(Signature, Vec<Signature>), ()> {

lightning/src/ln/channel.rs

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1791,7 +1791,8 @@ impl<Signer: Sign> Channel<Signer> {
17911791
self.counterparty_funding_pubkey()
17921792
);
17931793

1794-
self.holder_signer.validate_holder_commitment(&holder_commitment_tx);
1794+
self.holder_signer.validate_holder_commitment(&holder_commitment_tx)
1795+
.map_err(|_| ChannelError::Close("Failed to validate our commitment".to_owned()))?;
17951796

17961797
// Now that we're past error-generating stuff, update our local state:
17971798

@@ -1867,7 +1868,9 @@ impl<Signer: Sign> Channel<Signer> {
18671868
self.counterparty_funding_pubkey()
18681869
);
18691870

1870-
self.holder_signer.validate_holder_commitment(&holder_commitment_tx);
1871+
self.holder_signer.validate_holder_commitment(&holder_commitment_tx)
1872+
.map_err(|_| ChannelError::Close("Failed to validate our commitment".to_owned()))?;
1873+
18711874

18721875
let funding_redeemscript = self.get_funding_redeemscript();
18731876
let funding_txo = self.get_funding_txo().unwrap();
@@ -2505,7 +2508,8 @@ impl<Signer: Sign> Channel<Signer> {
25052508
);
25062509

25072510
let next_per_commitment_point = self.holder_signer.get_per_commitment_point(self.cur_holder_commitment_transaction_number - 1, &self.secp_ctx);
2508-
self.holder_signer.validate_holder_commitment(&holder_commitment_tx);
2511+
self.holder_signer.validate_holder_commitment(&holder_commitment_tx)
2512+
.map_err(|_| (None, ChannelError::Close("Failed to validate our commitment".to_owned())))?;
25092513
let per_commitment_secret = self.holder_signer.release_commitment_secret(self.cur_holder_commitment_transaction_number + 1);
25102514

25112515
// Update state now that we've passed all the can-fail calls...
@@ -2770,7 +2774,8 @@ impl<Signer: Sign> Channel<Signer> {
27702774
self.holder_signer.validate_counterparty_revocation(
27712775
self.cur_counterparty_commitment_transaction_number + 1,
27722776
&secret
2773-
);
2777+
).map_err(|_| ChannelError::Close("Failed to validate revocation from peer".to_owned()))?;
2778+
27742779
self.commitment_secrets.provide_secret(self.cur_counterparty_commitment_transaction_number + 1, msg.per_commitment_secret)
27752780
.map_err(|_| ChannelError::Close("Previous secrets did not match new one".to_owned()))?;
27762781
self.latest_monitor_update_id += 1;

lightning/src/util/enforcing_trait_impls.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,11 +100,12 @@ impl BaseSign for EnforcingSigner {
100100
self.inner.release_commitment_secret(idx)
101101
}
102102

103-
fn validate_holder_commitment(&self, holder_tx: &HolderCommitmentTransaction) {
103+
fn validate_holder_commitment(&self, holder_tx: &HolderCommitmentTransaction) -> Result<(), ()> {
104104
let mut state = self.state.lock().unwrap();
105105
let idx = holder_tx.commitment_number();
106106
assert!(idx == state.last_holder_commitment || idx == state.last_holder_commitment - 1, "expecting to validate the current or next holder commitment - trying {}, current {}", idx, state.last_holder_commitment);
107107
state.last_holder_commitment = idx;
108+
Ok(())
108109
}
109110

110111
fn pubkeys(&self) -> &ChannelPublicKeys { self.inner.pubkeys() }
@@ -129,10 +130,11 @@ impl BaseSign for EnforcingSigner {
129130
Ok(self.inner.sign_counterparty_commitment(commitment_tx, secp_ctx).unwrap())
130131
}
131132

132-
fn validate_counterparty_revocation(&self, idx: u64, _secret: &SecretKey) {
133+
fn validate_counterparty_revocation(&self, idx: u64, _secret: &SecretKey) -> Result<(), ()> {
133134
let mut state = self.state.lock().unwrap();
134135
assert!(idx == state.last_counterparty_revoked_commitment || idx == state.last_counterparty_revoked_commitment - 1, "expecting to validate the current or next counterparty revocation - trying {}, current {}", idx, state.last_counterparty_revoked_commitment);
135136
state.last_counterparty_revoked_commitment = idx;
137+
Ok(())
136138
}
137139

138140
fn sign_holder_commitment_and_htlcs(&self, commitment_tx: &HolderCommitmentTransaction, secp_ctx: &Secp256k1<secp256k1::All>) -> Result<(Signature, Vec<Signature>), ()> {

0 commit comments

Comments
 (0)