Skip to content

Commit 3a643df

Browse files
authored
Merge pull request #2217 from alecchendev/2023-04-expose-hash-in-balance
Expose `PaymentHash` and `PaymentPreimage` in `Balance`
2 parents 41e94f9 + 29b9eb3 commit 3a643df

File tree

3 files changed

+114
-148
lines changed

3 files changed

+114
-148
lines changed

lightning/src/chain/channelmonitor.rs

+15-1
Original file line numberDiff line numberDiff line change
@@ -606,6 +606,10 @@ pub enum Balance {
606606
/// The height at which the counterparty may be able to claim the balance if we have not
607607
/// done so.
608608
timeout_height: u32,
609+
/// The payment hash that locks this HTLC.
610+
payment_hash: PaymentHash,
611+
/// The preimage that can be used to claim this HTLC.
612+
payment_preimage: PaymentPreimage,
609613
},
610614
/// HTLCs which we sent to our counterparty which are claimable after a timeout (less on-chain
611615
/// fees) if the counterparty does not know the preimage for the HTLCs. These are somewhat
@@ -617,6 +621,8 @@ pub enum Balance {
617621
/// The height at which we will be able to claim the balance if our counterparty has not
618622
/// done so.
619623
claimable_height: u32,
624+
/// The payment hash whose preimage our counterparty needs to claim this HTLC.
625+
payment_hash: PaymentHash,
620626
},
621627
/// HTLCs which we received from our counterparty which are claimable with a preimage which we
622628
/// do not currently have. This will only be claimable if we receive the preimage from the node
@@ -628,6 +634,8 @@ pub enum Balance {
628634
/// The height at which our counterparty will be able to claim the balance if we have not
629635
/// yet received the preimage and claimed it ourselves.
630636
expiry_height: u32,
637+
/// The payment hash whose preimage we need to claim this HTLC.
638+
payment_hash: PaymentHash,
631639
},
632640
/// The channel has been closed, and our counterparty broadcasted a revoked commitment
633641
/// transaction.
@@ -1623,9 +1631,10 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
16231631
return Some(Balance::MaybeTimeoutClaimableHTLC {
16241632
claimable_amount_satoshis: htlc.amount_msat / 1000,
16251633
claimable_height: htlc.cltv_expiry,
1634+
payment_hash: htlc.payment_hash,
16261635
});
16271636
}
1628-
} else if self.payment_preimages.get(&htlc.payment_hash).is_some() {
1637+
} else if let Some(payment_preimage) = self.payment_preimages.get(&htlc.payment_hash) {
16291638
// Otherwise (the payment was inbound), only expose it as claimable if
16301639
// we know the preimage.
16311640
// Note that if there is a pending claim, but it did not use the
@@ -1641,12 +1650,15 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitorImpl<Signer> {
16411650
return Some(Balance::ContentiousClaimable {
16421651
claimable_amount_satoshis: htlc.amount_msat / 1000,
16431652
timeout_height: htlc.cltv_expiry,
1653+
payment_hash: htlc.payment_hash,
1654+
payment_preimage: *payment_preimage,
16441655
});
16451656
}
16461657
} else if htlc_resolved.is_none() {
16471658
return Some(Balance::MaybePreimageClaimableHTLC {
16481659
claimable_amount_satoshis: htlc.amount_msat / 1000,
16491660
expiry_height: htlc.cltv_expiry,
1661+
payment_hash: htlc.payment_hash,
16501662
});
16511663
}
16521664
None
@@ -1808,6 +1820,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
18081820
res.push(Balance::MaybeTimeoutClaimableHTLC {
18091821
claimable_amount_satoshis: htlc.amount_msat / 1000,
18101822
claimable_height: htlc.cltv_expiry,
1823+
payment_hash: htlc.payment_hash,
18111824
});
18121825
} else if us.payment_preimages.get(&htlc.payment_hash).is_some() {
18131826
claimable_inbound_htlc_value_sat += htlc.amount_msat / 1000;
@@ -1817,6 +1830,7 @@ impl<Signer: WriteableEcdsaChannelSigner> ChannelMonitor<Signer> {
18171830
res.push(Balance::MaybePreimageClaimableHTLC {
18181831
claimable_amount_satoshis: htlc.amount_msat / 1000,
18191832
expiry_height: htlc.cltv_expiry,
1833+
payment_hash: htlc.payment_hash,
18201834
});
18211835
}
18221836
}

lightning/src/ln/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -73,11 +73,13 @@ pub use self::peer_channel_encryptor::LN_MAX_MSG_LEN;
7373
///
7474
/// This is not exported to bindings users as we just use [u8; 32] directly
7575
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
76+
#[cfg_attr(test, derive(PartialOrd, Ord))]
7677
pub struct PaymentHash(pub [u8; 32]);
7778
/// payment_preimage type, use to route payment between hop
7879
///
7980
/// This is not exported to bindings users as we just use [u8; 32] directly
8081
#[derive(Hash, Copy, Clone, PartialEq, Eq, Debug)]
82+
#[cfg_attr(test, derive(PartialOrd, Ord))]
8183
pub struct PaymentPreimage(pub [u8; 32]);
8284
/// payment_secret type, use to authenticate sender to the receiver and tie MPP HTLCs together
8385
///

0 commit comments

Comments
 (0)