Skip to content

Commit 881c837

Browse files
agentboxagentbox
authored andcommitted
Replace panic with Secp256k1 variant in HpkeError
The From<secp256k1::Error> impl for HpkeError panicked on any variant other than InvalidPublicKey. If secp256k1 adds new Error variants, this would cause a runtime panic instead of returning a proper error. Add a Secp256k1(secp256k1::Error) variant to HpkeError that wraps unexpected secp256k1 errors, mirroring the existing Hpke(hpke::HpkeError) pattern. Update Display and Error impls to delegate to the wrapped error. Add a test verifying that non-InvalidPublicKey variants convert safely.
1 parent 3647419 commit 881c837

File tree

1 file changed

+16
-3
lines changed

1 file changed

+16
-3
lines changed

payjoin/src/core/hpke.rs

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,7 @@ fn pad_plaintext(msg: &mut Vec<u8>, padded_length: usize) -> Result<&[u8], HpkeE
274274
pub enum HpkeError {
275275
InvalidPublicKey,
276276
Hpke(hpke::HpkeError),
277+
Secp256k1(secp256k1::Error),
277278
InvalidKeyLength,
278279
PayloadTooLarge { actual: usize, max: usize },
279280
PayloadTooShort,
@@ -286,10 +287,8 @@ impl From<hpke::HpkeError> for HpkeError {
286287
impl From<secp256k1::Error> for HpkeError {
287288
fn from(value: secp256k1::Error) -> Self {
288289
match value {
289-
// As of writing, this is the only relevant variant that could arise here.
290-
// This may need to be updated if relevant variants are added to secp256k1
291290
secp256k1::Error::InvalidPublicKey => Self::InvalidPublicKey,
292-
_ => panic!("Unsupported variant of secp256k1::Error"),
291+
other => Self::Secp256k1(other),
293292
}
294293
}
295294
}
@@ -300,6 +299,7 @@ impl fmt::Display for HpkeError {
300299

301300
match &self {
302301
Hpke(e) => e.fmt(f),
302+
Secp256k1(e) => e.fmt(f),
303303
InvalidKeyLength => write!(f, "Invalid Length"),
304304
PayloadTooLarge { actual, max } => {
305305
write!(
@@ -319,6 +319,7 @@ impl error::Error for HpkeError {
319319

320320
match &self {
321321
Hpke(e) => Some(e),
322+
Secp256k1(e) => Some(e),
322323
PayloadTooLarge { .. } => None,
323324
InvalidKeyLength | PayloadTooShort => None,
324325
InvalidPublicKey => None,
@@ -492,6 +493,18 @@ mod test {
492493
);
493494
}
494495

496+
#[test]
497+
fn secp256k1_error_conversion_does_not_panic() {
498+
let invalid_pk: HpkeError = secp256k1::Error::InvalidPublicKey.into();
499+
assert_eq!(invalid_pk, HpkeError::InvalidPublicKey);
500+
501+
let invalid_sk: HpkeError = secp256k1::Error::InvalidSecretKey.into();
502+
assert_eq!(invalid_sk, HpkeError::Secp256k1(secp256k1::Error::InvalidSecretKey));
503+
504+
let invalid_sig: HpkeError = secp256k1::Error::InvalidSignature.into();
505+
assert_eq!(invalid_sig, HpkeError::Secp256k1(secp256k1::Error::InvalidSignature));
506+
}
507+
495508
/// Test that the encrypted payloads are uniform.
496509
///
497510
/// This randomized test will generate a false negative with negligible probability

0 commit comments

Comments
 (0)