fix(rpc): accept jwt's starting with 0x (#1589)

Co-authored-by: Bjerg <onbjerg@users.noreply.github.com>
This commit is contained in:
Paul Lange
2023-02-28 21:03:14 +01:00
committed by GitHub
parent 7a01e1e231
commit 3790a14268

View File

@ -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<S: AsRef<str>>(hex: S) -> Result<Self, JwtError> {
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";