Skip to content

Commit 8a44b49

Browse files
committed
derive Hash for Invoice
1 parent 79e2af9 commit 8a44b49

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

lightning-invoice/src/lib.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ pub struct InvoiceBuilder<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, S:
236236
/// 1. using `InvoiceBuilder`
237237
/// 2. using `Invoice::from_signed(SignedRawInvoice)`
238238
/// 3. using `str::parse::<Invoice>(&str)`
239-
#[derive(Eq, PartialEq, Debug, Clone)]
239+
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
240240
pub struct Invoice {
241241
signed_invoice: SignedRawInvoice,
242242
}
@@ -260,7 +260,7 @@ pub enum InvoiceDescription<'f> {
260260
///
261261
/// # Invariants
262262
/// The hash has to be either from the deserialized invoice or from the serialized `raw_invoice`.
263-
#[derive(Eq, PartialEq, Debug, Clone)]
263+
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
264264
pub struct SignedRawInvoice {
265265
/// The rawInvoice that the signature belongs to
266266
raw_invoice: RawInvoice,
@@ -283,7 +283,7 @@ pub struct SignedRawInvoice {
283283
/// De- and encoding should not lead to information loss but may lead to different hashes.
284284
///
285285
/// For methods without docs see the corresponding methods in `Invoice`.
286-
#[derive(Eq, PartialEq, Debug, Clone)]
286+
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
287287
pub struct RawInvoice {
288288
/// human readable part
289289
pub hrp: RawHrp,
@@ -295,7 +295,7 @@ pub struct RawInvoice {
295295
/// Data of the `RawInvoice` that is encoded in the human readable part
296296
///
297297
/// (C-not exported) As we don't yet support Option<Enum>
298-
#[derive(Eq, PartialEq, Debug, Clone)]
298+
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
299299
pub struct RawHrp {
300300
/// The currency deferred from the 3rd and 4th character of the bech32 transaction
301301
pub currency: Currency,
@@ -308,7 +308,7 @@ pub struct RawHrp {
308308
}
309309

310310
/// Data of the `RawInvoice` that is encoded in the data part
311-
#[derive(Eq, PartialEq, Debug, Clone)]
311+
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
312312
pub struct RawDataPart {
313313
/// generation time of the invoice
314314
pub timestamp: PositiveTimestamp,
@@ -323,11 +323,11 @@ pub struct RawDataPart {
323323
///
324324
/// The Unix timestamp representing the stored time has to be positive and no greater than
325325
/// [`MAX_TIMESTAMP`].
326-
#[derive(Eq, PartialEq, Debug, Clone)]
326+
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
327327
pub struct PositiveTimestamp(Duration);
328328

329329
/// SI prefixes for the human readable part
330-
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
330+
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
331331
pub enum SiPrefix {
332332
/// 10^-3
333333
Milli,
@@ -1415,6 +1415,15 @@ impl Deref for SignedRawInvoice {
14151415
}
14161416
}
14171417

1418+
impl core::hash::Hash for InvoiceSignature {
1419+
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
1420+
let (recovery_id, sig) = self.0.serialize_compact();
1421+
let recovery_id = recovery_id.to_i32();
1422+
state.write(&sig);
1423+
state.write_i32(recovery_id);
1424+
}
1425+
}
1426+
14181427
/// Errors that may occur when constructing a new `RawInvoice` or `Invoice`
14191428
#[derive(Eq, PartialEq, Debug, Clone)]
14201429
pub enum CreationError {

0 commit comments

Comments
 (0)