Skip to content

Commit

Permalink
derive Hash for Invoice
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolaLS committed Jun 27, 2022
1 parent 79e2af9 commit 8a44b49
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lightning-invoice/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ pub struct InvoiceBuilder<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, S:
/// 1. using `InvoiceBuilder`
/// 2. using `Invoice::from_signed(SignedRawInvoice)`
/// 3. using `str::parse::<Invoice>(&str)`
#[derive(Eq, PartialEq, Debug, Clone)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct Invoice {
signed_invoice: SignedRawInvoice,
}
Expand All @@ -260,7 +260,7 @@ pub enum InvoiceDescription<'f> {
///
/// # Invariants
/// The hash has to be either from the deserialized invoice or from the serialized `raw_invoice`.
#[derive(Eq, PartialEq, Debug, Clone)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct SignedRawInvoice {
/// The rawInvoice that the signature belongs to
raw_invoice: RawInvoice,
Expand All @@ -283,7 +283,7 @@ pub struct SignedRawInvoice {
/// De- and encoding should not lead to information loss but may lead to different hashes.
///
/// For methods without docs see the corresponding methods in `Invoice`.
#[derive(Eq, PartialEq, Debug, Clone)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct RawInvoice {
/// human readable part
pub hrp: RawHrp,
Expand All @@ -295,7 +295,7 @@ pub struct RawInvoice {
/// Data of the `RawInvoice` that is encoded in the human readable part
///
/// (C-not exported) As we don't yet support Option<Enum>
#[derive(Eq, PartialEq, Debug, Clone)]
#[derive(Eq, PartialEq, Debug, Clone, Hash)]
pub struct RawHrp {
/// The currency deferred from the 3rd and 4th character of the bech32 transaction
pub currency: Currency,
Expand All @@ -308,7 +308,7 @@ pub struct RawHrp {
}

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

/// SI prefixes for the human readable part
#[derive(Eq, PartialEq, Debug, Clone, Copy)]
#[derive(Eq, PartialEq, Debug, Clone, Copy, Hash)]
pub enum SiPrefix {
/// 10^-3
Milli,
Expand Down Expand Up @@ -1415,6 +1415,15 @@ impl Deref for SignedRawInvoice {
}
}

impl core::hash::Hash for InvoiceSignature {
fn hash<H: core::hash::Hasher>(&self, state: &mut H) {
let (recovery_id, sig) = self.0.serialize_compact();
let recovery_id = recovery_id.to_i32();
state.write(&sig);
state.write_i32(recovery_id);
}
}

/// Errors that may occur when constructing a new `RawInvoice` or `Invoice`
#[derive(Eq, PartialEq, Debug, Clone)]
pub enum CreationError {
Expand Down

0 comments on commit 8a44b49

Please sign in to comment.