X-Git-Url: http://git.bitcoin.ninja/index.cgi?a=blobdiff_plain;f=src%2Fhex_utils.rs;h=8b853dbb6330826132096a7c2c55f204592be486;hb=f0eb724681f30b24cfd437f966f1b6ecc8a1061e;hp=70fe28b13f30d8c5cf13ae3c6257bc3f3ece701c;hpb=d9e9c01d33f291cf4846c0406a3e00c9ecbd543d;p=ldk-sample diff --git a/src/hex_utils.rs b/src/hex_utils.rs index 70fe28b..8b853db 100644 --- a/src/hex_utils.rs +++ b/src/hex_utils.rs @@ -1,4 +1,5 @@ -use bitcoin::secp256k1::key::PublicKey; +use bitcoin::secp256k1::PublicKey; +use std::fmt::Write; pub fn to_vec(hex: &str) -> Option> { let mut out = Vec::with_capacity(hex.len() / 2); @@ -23,14 +24,17 @@ pub fn to_vec(hex: &str) -> Option> { #[inline] pub fn hex_str(value: &[u8]) -> String { - let mut res = String::with_capacity(64); + let mut res = String::with_capacity(2 * value.len()); for v in value { - res += &format!("{:02x}", v); + write!(&mut res, "{:02x}", v).expect("Unable to write"); } res } pub fn to_compressed_pubkey(hex: &str) -> Option { + if hex.len() != 33 * 2 { + return None; + } let data = match to_vec(&hex[0..33 * 2]) { Some(bytes) => bytes, None => return None,