X-Git-Url: http://git.bitcoin.ninja/index.cgi?p=ldk-sample;a=blobdiff_plain;f=src%2Fhex_utils.rs;h=70fe28b13f30d8c5cf13ae3c6257bc3f3ece701c;hp=c9c55b9b7da09cccf9955ee7cd011464eca591b5;hb=d9e9c01d33f291cf4846c0406a3e00c9ecbd543d;hpb=80916829ec20eb8a510462e765a03c3ed051f2e0 diff --git a/src/hex_utils.rs b/src/hex_utils.rs index c9c55b9..70fe28b 100644 --- a/src/hex_utils.rs +++ b/src/hex_utils.rs @@ -1,42 +1,42 @@ use bitcoin::secp256k1::key::PublicKey; pub fn to_vec(hex: &str) -> Option> { - let mut out = Vec::with_capacity(hex.len() / 2); + let mut out = Vec::with_capacity(hex.len() / 2); - let mut b = 0; - for (idx, c) in hex.as_bytes().iter().enumerate() { - b <<= 4; - match *c { - b'A'..=b'F' => b |= c - b'A' + 10, - b'a'..=b'f' => b |= c - b'a' + 10, - b'0'..=b'9' => b |= c - b'0', - _ => return None, - } - if (idx & 1) == 1 { - out.push(b); - b = 0; - } - } + let mut b = 0; + for (idx, c) in hex.as_bytes().iter().enumerate() { + b <<= 4; + match *c { + b'A'..=b'F' => b |= c - b'A' + 10, + b'a'..=b'f' => b |= c - b'a' + 10, + b'0'..=b'9' => b |= c - b'0', + _ => return None, + } + if (idx & 1) == 1 { + out.push(b); + b = 0; + } + } - Some(out) + Some(out) } #[inline] pub fn hex_str(value: &[u8]) -> String { - let mut res = String::with_capacity(64); - for v in value { - res += &format!("{:02x}", v); - } - res + let mut res = String::with_capacity(64); + for v in value { + res += &format!("{:02x}", v); + } + res } pub fn to_compressed_pubkey(hex: &str) -> Option { - let data = match to_vec(&hex[0..33*2]) { - Some(bytes) => bytes, - None => return None - }; - match PublicKey::from_slice(&data) { - Ok(pk) => Some(pk), - Err(_) => None, - } + let data = match to_vec(&hex[0..33 * 2]) { + Some(bytes) => bytes, + None => return None, + }; + match PublicKey::from_slice(&data) { + Ok(pk) => Some(pk), + Err(_) => None, + } }