1 use bitcoin::hashes::hex::FromHex;
2 use bitcoin::pow::Work;
4 pub fn hex_to_work(hex: &str) -> Result<Work, bitcoin::hashes::hex::Error> {
5 let bytes = <[u8; 32]>::from_hex(hex)?;
6 Ok(Work::from_be_bytes(bytes))
12 use bitcoin::pow::Work;
15 fn hex_to_work_empty_str() {
16 assert!(hex_to_work("").is_err());
20 fn hex_to_work_too_short_str() {
21 let hex = String::from_utf8(vec![b'0'; 32]).unwrap();
22 assert_eq!(hex_to_work(&hex), Err(bitcoin::hashes::hex::Error::InvalidLength(64, 32)));
26 fn hex_to_work_too_long_str() {
27 let hex = String::from_utf8(vec![b'0'; 128]).unwrap();
28 assert_eq!(hex_to_work(&hex), Err(bitcoin::hashes::hex::Error::InvalidLength(64, 128)));
32 fn hex_to_work_odd_length_str() {
33 let hex = String::from_utf8(vec![b'0'; 65]).unwrap();
34 assert_eq!(hex_to_work(&hex), Err(bitcoin::hashes::hex::Error::OddLengthString(65)));
38 fn hex_to_work_invalid_char() {
39 let hex = String::from_utf8(vec![b'G'; 64]).unwrap();
40 assert_eq!(hex_to_work(&hex), Err(bitcoin::hashes::hex::Error::InvalidChar(b'G')));
44 fn hex_to_work_lowercase_str() {
45 let hex: String = std::iter::repeat("1a").take(32).collect();
46 assert_eq!(hex_to_work(&hex).unwrap(), Work::from_be_bytes([0x1a; 32]));
50 fn hex_to_work_uppercase_str() {
51 let hex: String = std::iter::repeat("1A").take(32).collect();
52 assert_eq!(hex_to_work(&hex).unwrap(), Work::from_be_bytes([0x1A; 32]));