Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions payjoin-ffi/src/receive/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,8 +266,8 @@ impl ReceiverBuilder {
Self(self.0.clone().with_amount(Amount::from_sat(amount_sats)))
}

pub fn with_expiry(&self, expiry: u64) -> Self {
Self(self.0.clone().with_expiry(Duration::from_secs(expiry)))
pub fn with_expiration(&self, expiration: u64) -> Self {
Self(self.0.clone().with_expiration(Duration::from_secs(expiration)))
}

/// Set the maximum effective fee rate the receiver is willing to pay for their own input/output contributions
Expand Down
2 changes: 1 addition & 1 deletion payjoin/src/core/receive/v2/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ impl fmt::Display for SessionError {

match &self.0 {
ParseUrl(e) => write!(f, "URL parsing failed: {e}"),
Expired(expiry) => write!(f, "Session expired at {expiry:?}"),
Expired(expiration) => write!(f, "Session expired at {expiration:?}"),
OhttpEncapsulation(e) => write!(f, "OHTTP Encapsulation Error: {e}"),
Hpke(e) => write!(f, "Hpke decryption failed: {e}"),
DirectoryResponse(e) => write!(f, "Directory response error: {e}"),
Expand Down
54 changes: 29 additions & 25 deletions payjoin/src/core/receive/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ mod session;

const SUPPORTED_VERSIONS: &[Version] = &[Version::One, Version::Two];

static TWENTY_FOUR_HOURS_DEFAULT_EXPIRY: Duration = Duration::from_secs(60 * 60 * 24);
static TWENTY_FOUR_HOURS_DEFAULT_EXPIRATION: Duration = Duration::from_secs(60 * 60 * 24);

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SessionContext {
Expand All @@ -69,7 +69,7 @@ pub struct SessionContext {
directory: url::Url,
mailbox: Option<url::Url>,
ohttp_keys: OhttpKeys,
expiry: SystemTime,
expiration: SystemTime,
amount: Option<Amount>,
receiver_key: HpkeKeyPair,
reply_key: Option<HpkePublicKey>,
Expand Down Expand Up @@ -252,8 +252,8 @@ fn extract_err_req(
ohttp_relay: impl IntoUrl,
session_context: &SessionContext,
) -> Result<(Request, ohttp::ClientResponse), SessionError> {
if SystemTime::now() > session_context.expiry {
return Err(InternalSessionError::Expired(session_context.expiry).into());
if SystemTime::now() > session_context.expiration {
return Err(InternalSessionError::Expired(session_context.expiration).into());
}
let mailbox = mailbox_endpoint(&session_context.directory, &session_context.reply_mailbox_id());
let (body, ohttp_ctx) = ohttp_encapsulate(
Expand Down Expand Up @@ -299,7 +299,7 @@ impl ReceiverBuilder {
directory,
ohttp_keys,
receiver_key: HpkeKeyPair::gen_keypair(),
expiry: SystemTime::now() + TWENTY_FOUR_HOURS_DEFAULT_EXPIRY,
expiration: SystemTime::now() + TWENTY_FOUR_HOURS_DEFAULT_EXPIRATION,
amount: None,
mailbox: None,
reply_key: None,
Expand All @@ -308,8 +308,8 @@ impl ReceiverBuilder {
Ok(Self(session_context))
}

pub fn with_expiry(self, expiry: Duration) -> Self {
Self(SessionContext { expiry: SystemTime::now() + expiry, ..self.0 })
pub fn with_expiration(self, expiration: Duration) -> Self {
Self(SessionContext { expiration: SystemTime::now() + expiration, ..self.0 })
}

pub fn with_amount(self, amount: Amount) -> Self {
Expand Down Expand Up @@ -342,8 +342,8 @@ impl Receiver<Initialized> {
&mut self,
ohttp_relay: impl IntoUrl,
) -> Result<(Request, ohttp::ClientResponse), Error> {
if SystemTime::now() > self.session_context.expiry {
return Err(InternalSessionError::Expired(self.session_context.expiry).into());
if SystemTime::now() > self.session_context.expiration {
return Err(InternalSessionError::Expired(self.session_context.expiration).into());
}
let (body, ohttp_ctx) =
self.fallback_req_body().map_err(InternalSessionError::OhttpEncapsulation)?;
Expand Down Expand Up @@ -1066,7 +1066,7 @@ pub(crate) fn pj_uri<'a>(
let pj_param = crate::uri::PjParam::V2(crate::uri::v2::PjParam::new(
session_context.directory.clone(),
session_context.proposal_mailbox_id(),
session_context.expiry,
session_context.expiration,
session_context.ohttp_keys.clone(),
session_context.receiver_key.public_key().clone(),
));
Expand Down Expand Up @@ -1104,7 +1104,7 @@ pub mod test {
ohttp_keys: OhttpKeys(
ohttp::KeyConfig::new(KEY_ID, KEM, Vec::from(SYMMETRIC)).expect("valid key config"),
),
expiry: SystemTime::now() + Duration::from_secs(60),
expiration: SystemTime::now() + Duration::from_secs(60),
receiver_key: HpkeKeyPair::gen_keypair(),
reply_key: None,
amount: None,
Expand Down Expand Up @@ -1327,7 +1327,7 @@ pub mod test {
fn test_extract_err_req_expiry() -> Result<(), BoxError> {
let now = SystemTime::now();
let noop_persister = NoopSessionPersister::default();
let context = SessionContext { expiry: now, ..SHARED_CONTEXT.clone() };
let context = SessionContext { expiration: now, ..SHARED_CONTEXT.clone() };
let receiver = Receiver {
state: UncheckedOriginalPayload {
original: crate::receive::tests::original_from_test_vector(),
Expand All @@ -1346,9 +1346,9 @@ pub mod test {
let res = error.api_error().expect("check_broadcast error should propagate to api error");
let actual_json = JsonReply::from(&res);

let expiry = extract_err_req(&actual_json, EXAMPLE_URL.as_str(), &context);
let expiration = extract_err_req(&actual_json, EXAMPLE_URL.as_str(), &context);

match expiry {
match expiration {
Err(error) => assert_eq!(
error.to_string(),
SessionError::from(InternalSessionError::Expired(now)).to_string()
Expand All @@ -1359,7 +1359,7 @@ pub mod test {
}

#[test]
fn default_expiry() {
fn default_expiration() {
let now = SystemTime::now();
let noop_persister = NoopSessionPersister::default();

Expand All @@ -1372,11 +1372,15 @@ pub mod test {
.build()
.save(&noop_persister)
.expect("Noop persister shouldn't fail");
let session_expiry = session.session_context.expiry.duration_since(now).unwrap().as_secs();
let default_expiry = Duration::from_secs(86400);
if let Some(expected_expiry) = now.checked_add(default_expiry) {
assert_eq!(TWENTY_FOUR_HOURS_DEFAULT_EXPIRY, default_expiry);
assert_eq!(session_expiry, expected_expiry.duration_since(now).unwrap().as_secs());
let session_expiration =
session.session_context.expiration.duration_since(now).unwrap().as_secs();
let default_expiration = Duration::from_secs(86400);
if let Some(expected_expiration) = now.checked_add(default_expiration) {
assert_eq!(TWENTY_FOUR_HOURS_DEFAULT_EXPIRATION, default_expiration);
assert_eq!(
session_expiration,
expected_expiration.duration_since(now).unwrap().as_secs()
);
}
}

Expand Down Expand Up @@ -1411,23 +1415,23 @@ pub mod test {
}

#[test]
fn build_receiver_with_non_default_expiry() {
fn build_receiver_with_non_default_expiration() {
let now = SystemTime::now();
let expiry = Duration::from_secs(60);
let expiration = Duration::from_secs(60);
let noop_persister = NoopSessionPersister::default();
let receiver = ReceiverBuilder::new(
SHARED_CONTEXT.address.clone(),
SHARED_CONTEXT.directory.as_str(),
SHARED_CONTEXT.ohttp_keys.clone(),
)
.expect("constructor on test vector should not fail")
.with_expiry(expiry)
.with_expiration(expiration)
.build()
.save(&noop_persister)
.expect("Noop persister shouldn't fail");
assert_eq!(
receiver.session_context.expiry.duration_since(now).unwrap().as_secs(),
expiry.as_secs()
receiver.session_context.expiration.duration_since(now).unwrap().as_secs(),
expiration.as_secs()
);
}

Expand Down
6 changes: 3 additions & 3 deletions payjoin/src/core/receive/v2/session.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ where

let history = SessionHistory::new(session_events.clone());
let ctx = history.session_context();
if SystemTime::now() > ctx.expiry {
if SystemTime::now() > ctx.expiration {
// Session has expired: close the session and persist a fatal error
let err = SessionError(InternalSessionError::Expired(ctx.expiry));
let err = SessionError(InternalSessionError::Expired(ctx.expiration));
persister
.save_event(SessionEvent::SessionInvalid(err.to_string(), None).into())
.map_err(|e| InternalReplayError::PersistenceFailure(ImplementationError::new(e)))?;
Expand Down Expand Up @@ -334,7 +334,7 @@ mod tests {
#[test]
fn test_replaying_session_creation_with_expired_session() -> Result<(), BoxError> {
let session_context = SessionContext {
expiry: SystemTime::now() - Duration::from_secs(1),
expiration: SystemTime::now() - Duration::from_secs(1),
..SHARED_CONTEXT.clone()
};
let test = SessionHistoryTest {
Expand Down
6 changes: 3 additions & 3 deletions payjoin/src/core/send/v2/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub(crate) enum InternalCreateRequestError {
Url(crate::into_url::Error),
Hpke(crate::hpke::HpkeError),
OhttpEncapsulation(crate::ohttp::OhttpEncapsulationError),
Expired(std::time::SystemTime),
Expiration(std::time::SystemTime),
}

impl fmt::Display for CreateRequestError {
Expand All @@ -26,7 +26,7 @@ impl fmt::Display for CreateRequestError {
Url(e) => write!(f, "cannot parse url: {e:#?}"),
Hpke(e) => write!(f, "v2 error: {e}"),
OhttpEncapsulation(e) => write!(f, "v2 error: {e}"),
Expired(expiry) => write!(f, "session expired at {expiry:?}"),
Expiration(expiration) => write!(f, "session expired at {expiration:?}"),
}
}
}
Expand All @@ -39,7 +39,7 @@ impl std::error::Error for CreateRequestError {
Url(error) => Some(error),
Hpke(error) => Some(error),
OhttpEncapsulation(error) => Some(error),
Expired(_) => None,
Expiration(_) => None,
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions payjoin/src/core/send/v2/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ impl Sender<WithReplyKey> {
ohttp_relay: impl IntoUrl,
) -> Result<(Request, V2PostContext), CreateRequestError> {
if std::time::SystemTime::now() > self.pj_param.expiration() {
return Err(InternalCreateRequestError::Expired(self.pj_param.expiration()).into());
return Err(InternalCreateRequestError::Expiration(self.pj_param.expiration()).into());
}

let mut sanitized_psbt = self.psbt_ctx.original_psbt.clone();
Expand Down Expand Up @@ -596,7 +596,7 @@ mod test {
let sender = create_sender_context(SystemTime::now() - Duration::from_secs(60))?;
let ohttp_relay = EXAMPLE_URL.as_str();
let result = sender.create_v2_post_request(ohttp_relay);
assert!(result.is_err(), "Extract v2 expected expiry error, but it succeeded");
assert!(result.is_err(), "Extract v2 expected expiration error, but it succeeded");

match result {
Ok(_) => panic!("Expected error, got success"),
Expand Down
2 changes: 1 addition & 1 deletion payjoin/src/core/uri/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -468,7 +468,7 @@ mod tests {
set_exp(&mut url, &exp_time);
assert_eq!(url.fragment(), Some("EX1C4UC6ES"));

assert_eq!(exp(&url).expect("Expiry has been set but is missing on get"), exp_time);
assert_eq!(exp(&url).expect("Expiration has been set but is missing on get"), exp_time);
}

#[test]
Expand Down
2 changes: 1 addition & 1 deletion payjoin/tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ mod integration {
// test session with expiry in the past
let mut expired_receiver =
ReceiverBuilder::new(address, services.directory_url().as_str(), ohttp_keys)?
.with_expiry(Duration::from_secs(0))
.with_expiration(Duration::from_secs(0))
.build()
.save(&recv_noop_persister)?;
match expired_receiver.create_poll_request(services.ohttp_relay_url().as_str()) {
Expand Down
Loading