Skip to content
Merged
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
29 changes: 26 additions & 3 deletions payjoin/src/core/hpke.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,7 @@ pub enum HpkeError {
InvalidKeyLength,
PayloadTooLarge { actual: usize, max: usize },
PayloadTooShort,
UnexpectedSecp256k1Error,
}

impl From<hpke::HpkeError> for HpkeError {
Expand All @@ -286,10 +287,11 @@ impl From<hpke::HpkeError> for HpkeError {
impl From<secp256k1::Error> for HpkeError {
fn from(value: secp256k1::Error) -> Self {
match value {
// As of writing, this is the only relevant variant that could arise here.
// This may need to be updated if relevant variants are added to secp256k1
// As of writing, this is the only relevant variant that could arise here. The other variant has
// been added due to new secp256k1::Error variants that may be added in the future. update this
// match statement if relevant error variants that are needed are added to secp256k1
secp256k1::Error::InvalidPublicKey => Self::InvalidPublicKey,
_ => panic!("Unsupported variant of secp256k1::Error"),
_other => Self::UnexpectedSecp256k1Error,
}
}
}
Expand All @@ -309,6 +311,7 @@ impl fmt::Display for HpkeError {
}
PayloadTooShort => write!(f, "Payload too small"),
InvalidPublicKey => write!(f, "Invalid public key"),
UnexpectedSecp256k1Error => write!(f, "Unexpected secp256k1 error"),
}
}
}
Expand All @@ -322,6 +325,7 @@ impl error::Error for HpkeError {
PayloadTooLarge { .. } => None,
InvalidKeyLength | PayloadTooShort => None,
InvalidPublicKey => None,
UnexpectedSecp256k1Error => None,
}
}
}
Expand All @@ -330,6 +334,25 @@ impl error::Error for HpkeError {
mod test {
use super::*;

#[test]
fn secp256k1_error_conversion_no_panic() {
// Test the known variant that maps to InvalidPublicKey(update if new variants are added)
let err = secp256k1::Error::InvalidPublicKey;
let hpke_err: HpkeError = err.into();
assert_eq!(hpke_err, HpkeError::InvalidPublicKey);
// Test other variants that may arise
let other_variants = [
secp256k1::Error::InvalidSecretKey,
secp256k1::Error::InvalidRecoveryId,
secp256k1::Error::InvalidTweak,
secp256k1::Error::NotEnoughMemory,
];
for err in other_variants {
let hpke_err: HpkeError = err.into();
assert_eq!(hpke_err, HpkeError::UnexpectedSecp256k1Error);
}
}

#[test]
fn message_a_round_trip() {
let mut plaintext = "foo".as_bytes().to_vec();
Expand Down
Loading