diff --git a/bitcoin/src/base58.rs b/bitcoin/src/base58.rs index a69d84bf..5e1658ea 100644 --- a/bitcoin/src/base58.rs +++ b/bitcoin/src/base58.rs @@ -7,12 +7,11 @@ //! strings respectively. //! -use crate::prelude::*; - -use core::{fmt, str, iter, slice}; use core::convert::TryInto; +use core::{fmt, iter, slice, str}; use crate::hashes::{sha256d, Hash}; +use crate::prelude::*; static BASE58_CHARS: &[u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"; @@ -38,9 +37,7 @@ static BASE58_DIGITS: [Option; 128] = [ /// Decodes a base58-encoded string into a byte vector. #[deprecated(since = "0.30.0", note = "Use base58::decode() instead")] -pub fn from(data: &str) -> Result, Error> { - decode(data) -} +pub fn from(data: &str) -> Result, Error> { decode(data) } /// Decodes a base58-encoded string into a byte vector. pub fn decode(data: &str) -> Result, Error> { @@ -54,7 +51,9 @@ pub fn decode(data: &str) -> Result, Error> { } let mut carry = match BASE58_DIGITS[d58 as usize] { Some(d58) => d58 as u32, - None => { return Err(Error::BadByte(d58)); } + None => { + return Err(Error::BadByte(d58)); + } }; for d256 in scratch.iter_mut().rev() { carry += *d256 as u32 * 58; @@ -65,9 +64,7 @@ pub fn decode(data: &str) -> Result, Error> { } // Copy leading zeroes directly - let mut ret: Vec = data.bytes().take_while(|&x| x == BASE58_CHARS[0]) - .map(|_| 0) - .collect(); + let mut ret: Vec = data.bytes().take_while(|&x| x == BASE58_CHARS[0]).map(|_| 0).collect(); // Copy rest of string ret.extend(scratch.into_iter().skip_while(|&x| x == 0)); Ok(ret) @@ -75,9 +72,7 @@ pub fn decode(data: &str) -> Result, Error> { /// Decodes a base58check-encoded string into a byte vector verifying the checksum. #[deprecated(since = "0.30.0", note = "Use base58::decode_check() instead")] -pub fn from_check(data: &str) -> Result, Error> { - decode_check(data) -} +pub fn from_check(data: &str) -> Result, Error> { decode_check(data) } /// Decodes a base58check-encoded string into a byte vector verifying the checksum. pub fn decode_check(data: &str) -> Result, Error> { @@ -87,7 +82,8 @@ pub fn decode_check(data: &str) -> Result, Error> { } let check_start = ret.len() - 4; - let hash_check = sha256d::Hash::hash(&ret[..check_start])[..4].try_into().expect("4 byte slice"); + let hash_check = + sha256d::Hash::hash(&ret[..check_start])[..4].try_into().expect("4 byte slice"); let data_check = ret[check_start..].try_into().expect("4 byte slice"); let expected = u32::from_le_bytes(hash_check); @@ -103,33 +99,23 @@ pub fn decode_check(data: &str) -> Result, Error> { /// Encodes `data` as a base58 string. #[deprecated(since = "0.30.0", note = "Use base58::encode() instead")] -pub fn encode_slice(data: &[u8]) -> String { - encode(data) -} +pub fn encode_slice(data: &[u8]) -> String { encode(data) } /// Encodes `data` as a base58 string (see also `base58::encode_check()`). -pub fn encode(data: &[u8]) -> String { - encode_iter(data.iter().cloned()) -} +pub fn encode(data: &[u8]) -> String { encode_iter(data.iter().cloned()) } /// Encodes `data` as a base58 string including the checksum. /// /// The checksum is the first four bytes of the sha256d of the data, concatenated onto the end. #[deprecated(since = "0.30.0", note = "Use base58::encode_check() instead")] -pub fn check_encode_slice(data: &[u8]) -> String { - encode_check(data) -} +pub fn check_encode_slice(data: &[u8]) -> String { encode_check(data) } /// Encodes `data` as a base58 string including the checksum. /// /// The checksum is the first four bytes of the sha256d of the data, concatenated onto the end. pub fn encode_check(data: &[u8]) -> String { let checksum = sha256d::Hash::hash(data); - encode_iter( - data.iter() - .cloned() - .chain(checksum[0..4].iter().cloned()) - ) + encode_iter(data.iter().cloned().chain(checksum[0..4].iter().cloned())) } /// Encodes `data` as base58, including the checksum, into a formatter. @@ -145,15 +131,13 @@ pub fn check_encode_slice_to_fmt(fmt: &mut fmt::Formatter, data: &[u8]) -> fmt:: /// The checksum is the first four bytes of the sha256d of the data, concatenated onto the end. pub fn encode_check_to_fmt(fmt: &mut fmt::Formatter, data: &[u8]) -> fmt::Result { let checksum = sha256d::Hash::hash(data); - let iter = data.iter() - .cloned() - .chain(checksum[0..4].iter().cloned()); + let iter = data.iter().cloned().chain(checksum[0..4].iter().cloned()); format_iter(fmt, iter) } fn encode_iter(data: I) -> String where - I: Iterator + Clone, + I: Iterator + Clone, { let mut ret = String::new(); format_iter(&mut ret, data).expect("writing into string shouldn't fail"); @@ -162,8 +146,8 @@ where fn format_iter(writer: &mut W, data: I) -> Result<(), fmt::Error> where - I: Iterator + Clone, - W: fmt::Write + I: Iterator + Clone, + W: fmt::Write, { let mut ret = SmallVec::new(); @@ -210,13 +194,7 @@ struct SmallVec { } impl SmallVec { - fn new() -> SmallVec { - SmallVec { - len: 0, - stack: [T::default(); 100], - heap: Vec::new(), - } - } + fn new() -> SmallVec { SmallVec { len: 0, stack: [T::default(); 100], heap: Vec::new() } } fn push(&mut self, val: T) { if self.len < 100 { @@ -263,10 +241,13 @@ impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { match *self { Error::BadByte(b) => write!(f, "invalid base58 character {:#x}", b), - Error::BadChecksum(exp, actual) => write!(f, "base58ck checksum {:#x} does not match expected {:#x}", actual, exp), + Error::BadChecksum(exp, actual) => + write!(f, "base58ck checksum {:#x} does not match expected {:#x}", actual, exp), Error::InvalidLength(ell) => write!(f, "length {} invalid for this base58 type", ell), - Error::InvalidExtendedKeyVersion(ref v) => write!(f, "extended key version {:#04x?} is invalid for this base58 type", v), - Error::InvalidAddressVersion(ref v) => write!(f, "address version {} is invalid for this base58 type", v), + Error::InvalidExtendedKeyVersion(ref v) => + write!(f, "extended key version {:#04x?} is invalid for this base58 type", v), + Error::InvalidAddressVersion(ref v) => + write!(f, "address version {} is invalid for this base58 type", v), Error::TooShort(_) => write!(f, "base58ck data not even long enough for a checksum"), } } @@ -307,9 +288,13 @@ mod tests { assert_eq!(&encode(&[0, 0, 0, 0, 13, 36][..]), "1111211"); // Long input (>100 bytes => has to use heap) - let res = encode("BitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBit\ - coinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoin".as_bytes()); - let exp = "ZqC5ZdfpZRi7fjA8hbhX5pEE96MdH9hEaC1YouxscPtbJF16qVWksHWR4wwvx7MotFcs2ChbJqK8KJ9X\ + let res = encode( + "BitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBit\ + coinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoin" + .as_bytes(), + ); + let exp = + "ZqC5ZdfpZRi7fjA8hbhX5pEE96MdH9hEaC1YouxscPtbJF16qVWksHWR4wwvx7MotFcs2ChbJqK8KJ9X\ wZznwWn1JFDhhTmGo9v6GjAVikzCsBWZehu7bm22xL8b5zBR5AsBygYRwbFJsNwNkjpyFuDKwmsUTKvkULCvucPJrN5\ QUdxpGakhqkZFL7RU4yT"; assert_eq!(&res, exp); @@ -332,8 +317,10 @@ mod tests { assert_eq!(decode("111211").ok(), Some(vec![0u8, 0, 0, 13, 36])); // Addresses - assert_eq!(decode_check("1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH").ok(), - Some(Vec::from_hex("00f8917303bfa8ef24f292e8fa1419b20460ba064d").unwrap())); + assert_eq!( + decode_check("1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH").ok(), + Some(Vec::from_hex("00f8917303bfa8ef24f292e8fa1419b20460ba064d").unwrap()) + ); // Non Base58 char. assert_eq!(decode("ยข").unwrap_err(), Error::BadByte(194)); } @@ -348,7 +335,6 @@ mod tests { // Check that empty slice passes roundtrip. assert_eq!(decode_check(&encode_check(&[])), Ok(vec![])); // Check that `len > 4` is enforced. - assert_eq!(decode_check(&encode(&[1,2,3])), Err(Error::TooShort(3))); - + assert_eq!(decode_check(&encode(&[1, 2, 3])), Err(Error::TooShort(3))); } }