Skip to content

Commit e6e5898

Browse files
average-garyGitGab19
authored andcommitted
fix HexError(OddLength)
Use utils in try_from to parse leading 0s correctly.
1 parent 55f4fbe commit e6e5898

File tree

1 file changed

+34
-3
lines changed

1 file changed

+34
-3
lines changed

protocols/v1/src/methods/client_to_server.rs

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,7 @@ impl TryFrom<StandardRequest> for Subscribe<'_> {
319319
[JString(a), Null, JString(_), Null] => (a.into(), None),
320320
// bosminer subscribe message
321321
[JString(a), Null] => (a.into(), None),
322-
[JString(a), JString(b)] => {
323-
(a.into(), Some(Extranonce::try_from(hex::decode(b)?)?))
324-
}
322+
[JString(a), JString(b)] => (a.into(), Some(Extranonce::try_from(b.as_str())?)),
325323
[JString(a)] => (a.into(), None),
326324
[] => ("".to_string(), None),
327325
_ => return Err(ParsingMethodError::wrong_args_from_value(msg.params)),
@@ -716,3 +714,36 @@ fn test_version_extension_with_no_bit_count() {
716714
_ => panic!(),
717715
};
718716
}
717+
718+
#[test]
719+
fn test_subscribe_with_odd_length_extranonce() {
720+
// Test that odd-length hex strings (with leading zeroes) are handled correctly
721+
let client_message = r#"{"id":1,
722+
"method": "mining.subscribe",
723+
"params":["test-agent", "abc"]
724+
}"#;
725+
let client_message: StandardRequest = serde_json::from_str(client_message).unwrap();
726+
let subscribe = Subscribe::try_from(client_message).unwrap();
727+
728+
// Should successfully parse odd-length hex string by prepending "0"
729+
assert_eq!(subscribe.agent_signature, "test-agent");
730+
assert!(subscribe.extranonce1.is_some());
731+
let extranonce = subscribe.extranonce1.unwrap();
732+
assert_eq!(extranonce.0.inner_as_ref(), &[0x0a, 0xbc]); // "0abc" -> [10, 188]
733+
}
734+
735+
#[test]
736+
fn test_subscribe_with_even_length_extranonce() {
737+
// Test that even-length hex strings work as before
738+
let client_message = r#"{"id":1,
739+
"method": "mining.subscribe",
740+
"params":["test-agent", "abcd"]
741+
}"#;
742+
let client_message: StandardRequest = serde_json::from_str(client_message).unwrap();
743+
let subscribe = Subscribe::try_from(client_message).unwrap();
744+
745+
assert_eq!(subscribe.agent_signature, "test-agent");
746+
assert!(subscribe.extranonce1.is_some());
747+
let extranonce = subscribe.extranonce1.unwrap();
748+
assert_eq!(extranonce.0.inner_as_ref(), &[0xab, 0xcd]); // "abcd" -> [171, 205]
749+
}

0 commit comments

Comments
 (0)