Skip to content

Commit 5f8d7c5

Browse files
authored
Merge pull request #1760 from TheBlueMatt/2022-10-invoice-builder-round
2 parents 7544030 + 008da77 commit 5f8d7c5

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

lightning-invoice/src/lib.rs

+16-10
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,8 @@ impl<D: tb::Bool, H: tb::Bool, T: tb::Bool, C: tb::Bool, S: tb::Bool> InvoiceBui
540540
self
541541
}
542542

543-
/// Sets the expiry time
543+
/// Sets the expiry time, dropping the subsecond part (which is not representable in BOLT 11
544+
/// invoices).
544545
pub fn expiry_time(mut self, expiry_time: Duration) -> Self {
545546
self.tagged_fields.push(TaggedField::ExpiryTime(ExpiryTime::from_duration(expiry_time)));
546547
self
@@ -632,7 +633,8 @@ impl<D: tb::Bool, H: tb::Bool, C: tb::Bool, S: tb::Bool> InvoiceBuilder<D, H, tb
632633
self.set_flags()
633634
}
634635

635-
/// Sets the timestamp to a duration since the Unix epoch.
636+
/// Sets the timestamp to a duration since the Unix epoch, dropping the subsecond part (which
637+
/// is not representable in BOLT 11 invoices).
636638
pub fn duration_since_epoch(mut self, time: Duration) -> InvoiceBuilder<D, H, tb::True, C, S> {
637639
match PositiveTimestamp::from_duration_since_epoch(time) {
638640
Ok(t) => self.timestamp = Some(t),
@@ -960,12 +962,18 @@ impl PositiveTimestamp {
960962
///
961963
/// Otherwise, returns a [`CreationError::TimestampOutOfBounds`].
962964
pub fn from_unix_timestamp(unix_seconds: u64) -> Result<Self, CreationError> {
963-
Self::from_duration_since_epoch(Duration::from_secs(unix_seconds))
965+
if unix_seconds <= MAX_TIMESTAMP {
966+
Ok(Self(Duration::from_secs(unix_seconds)))
967+
} else {
968+
Err(CreationError::TimestampOutOfBounds)
969+
}
964970
}
965971

966972
/// Creates a `PositiveTimestamp` from a [`SystemTime`] with a corresponding Unix timestamp in
967973
/// the range `0..=MAX_TIMESTAMP`.
968974
///
975+
/// Note that the subsecond part is dropped as it is not representable in BOLT 11 invoices.
976+
///
969977
/// Otherwise, returns a [`CreationError::TimestampOutOfBounds`].
970978
#[cfg(feature = "std")]
971979
pub fn from_system_time(time: SystemTime) -> Result<Self, CreationError> {
@@ -977,13 +985,11 @@ impl PositiveTimestamp {
977985
/// Creates a `PositiveTimestamp` from a [`Duration`] since the Unix epoch in the range
978986
/// `0..=MAX_TIMESTAMP`.
979987
///
988+
/// Note that the subsecond part is dropped as it is not representable in BOLT 11 invoices.
989+
///
980990
/// Otherwise, returns a [`CreationError::TimestampOutOfBounds`].
981991
pub fn from_duration_since_epoch(duration: Duration) -> Result<Self, CreationError> {
982-
if duration.as_secs() <= MAX_TIMESTAMP {
983-
Ok(PositiveTimestamp(duration))
984-
} else {
985-
Err(CreationError::TimestampOutOfBounds)
986-
}
992+
Self::from_unix_timestamp(duration.as_secs())
987993
}
988994

989995
/// Returns the Unix timestamp representing the stored time
@@ -1356,9 +1362,9 @@ impl ExpiryTime {
13561362
ExpiryTime(Duration::from_secs(seconds))
13571363
}
13581364

1359-
/// Construct an `ExpiryTime` from a `Duration`.
1365+
/// Construct an `ExpiryTime` from a `Duration`, dropping the sub-second part.
13601366
pub fn from_duration(duration: Duration) -> ExpiryTime {
1361-
ExpiryTime(duration)
1367+
Self::from_seconds(duration.as_secs())
13621368
}
13631369

13641370
/// Returns the expiry time in seconds

0 commit comments

Comments
 (0)