]> git.bitcoin.ninja Git - rust-lightning/blob - lightning-block-sync/src/utils.rs
Remove `sign/taproot.rs` from `rustfmt` exclusion list
[rust-lightning] / lightning-block-sync / src / utils.rs
1 use bitcoin::hashes::hex::FromHex;
2 use bitcoin::pow::Work;
3
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))
7 }
8
9 #[cfg(test)]
10 mod tests {
11         use super::*;
12         use bitcoin::pow::Work;
13
14         #[test]
15         fn hex_to_work_empty_str() {
16                 assert!(hex_to_work("").is_err());
17         }
18
19         #[test]
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)));
23         }
24
25         #[test]
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)));
29         }
30
31         #[test]
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)));
35         }
36
37         #[test]
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')));
41         }
42
43         #[test]
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]));
47         }
48
49         #[test]
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]));
53         }
54 }