From 3790a142685581a2f8692e41a4d91e829cd10144 Mon Sep 17 00:00:00 2001 From: Paul Lange Date: Tue, 28 Feb 2023 21:03:14 +0100 Subject: [PATCH] fix(rpc): accept jwt's starting with `0x` (#1589) Co-authored-by: Bjerg --- crates/rpc/rpc/src/layers/jwt_secret.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/rpc/rpc/src/layers/jwt_secret.rs b/crates/rpc/rpc/src/layers/jwt_secret.rs index 9a3491748..1f2d170c5 100644 --- a/crates/rpc/rpc/src/layers/jwt_secret.rs +++ b/crates/rpc/rpc/src/layers/jwt_secret.rs @@ -58,8 +58,10 @@ impl JwtSecret { /// Returns an error if one of the following applies: /// - `hex` is not a valid hexadecimal string /// - `hex` argument length is less than `JWT_SECRET_LEN` + /// + /// This strips the leading `0x`, if any. pub fn from_hex>(hex: S) -> Result { - let hex: &str = hex.as_ref().trim(); + let hex: &str = hex.as_ref().trim().trim_start_matches("0x"); if hex.len() != JWT_SECRET_LEN { Err(JwtError::InvalidLength(JWT_SECRET_LEN, hex.len())) } else { @@ -212,6 +214,14 @@ mod tests { assert_eq!(hex.len(), expected_len); } + #[test] + fn creation_ok_hex_string_with_0x() { + let hex: String = + "0x7365637265747365637265747365637265747365637265747365637265747365".into(); + let result = JwtSecret::from_hex(hex); + assert!(matches!(result, Ok(_))); + } + #[test] fn creation_error_wrong_len() { let hex = "f79ae8046";