1 use bitcoin::hashes::hex::FromHex;
2 use bitcoin::util::uint::Uint256;
4 pub fn hex_to_uint256(hex: &str) -> Result<Uint256, bitcoin::hashes::hex::Error> {
5 let bytes = <[u8; 32]>::from_hex(hex)?;
6 Ok(Uint256::from_be_bytes(bytes))
12 use bitcoin::util::uint::Uint256;
15 fn hex_to_uint256_empty_str() {
16 assert!(hex_to_uint256("").is_err());
20 fn hex_to_uint256_too_short_str() {
21 let hex = String::from_utf8(vec![b'0'; 32]).unwrap();
22 assert_eq!(hex_to_uint256(&hex), Err(bitcoin::hashes::hex::Error::InvalidLength(64, 32)));
26 fn hex_to_uint256_too_long_str() {
27 let hex = String::from_utf8(vec![b'0'; 128]).unwrap();
28 assert_eq!(hex_to_uint256(&hex), Err(bitcoin::hashes::hex::Error::InvalidLength(64, 128)));
32 fn hex_to_uint256_odd_length_str() {
33 let hex = String::from_utf8(vec![b'0'; 65]).unwrap();
34 assert_eq!(hex_to_uint256(&hex), Err(bitcoin::hashes::hex::Error::OddLengthString(65)));
38 fn hex_to_uint256_invalid_char() {
39 let hex = String::from_utf8(vec![b'G'; 64]).unwrap();
40 assert_eq!(hex_to_uint256(&hex), Err(bitcoin::hashes::hex::Error::InvalidChar(b'G')));
44 fn hex_to_uint256_lowercase_str() {
45 let hex: String = std::iter::repeat("0123456789abcdef").take(4).collect();
46 assert_eq!(hex_to_uint256(&hex).unwrap(), Uint256([0x0123456789abcdefu64; 4]));
50 fn hex_to_uint256_uppercase_str() {
51 let hex: String = std::iter::repeat("0123456789ABCDEF").take(4).collect();
52 assert_eq!(hex_to_uint256(&hex).unwrap(), Uint256([0x0123456789abcdefu64; 4]));