Skip to content

Commit fc39558

Browse files
committed
fix(rust): clamp a specific nonce, in case it was attempted to be set out of range.
1 parent 70cb89d commit fc39558

File tree

1 file changed

+15
-4
lines changed
  • rust/catalyst-types/src/id_uri

1 file changed

+15
-4
lines changed

rust/catalyst-types/src/id_uri/mod.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,12 +163,23 @@ impl IdUri {
163163
}
164164

165165
/// Add or change the nonce to a specific value in a Catalyst ID URI.
166+
///
167+
/// Note, this will not fail, but if the Datetime is < `MIN_NONCE`,
168+
/// or greater than `MAX_NONCE`, it will be clamped into that range.
166169
#[must_use]
167170
pub fn with_specific_nonce(self, nonce: DateTime<Utc>) -> Self {
168-
Self {
169-
nonce: Some(nonce),
170-
..self
171-
}
171+
let secs = nonce.timestamp();
172+
let clamped_secs = secs.clamp(Self::MIN_NONCE, Self::MAX_NONCE);
173+
174+
let nonce = {
175+
if clamped_secs == secs {
176+
Some(nonce)
177+
} else {
178+
DateTime::<Utc>::from_timestamp(clamped_secs, 0)
179+
}
180+
};
181+
182+
Self { nonce, ..self }
172183
}
173184

174185
/// Add or change the nonce in a Catalyst ID URI.

0 commit comments

Comments
 (0)