base58: Run formatter

Run `cargo +nightly fmt` and commit the changes to the `base58` module.
No manual changes, only those done by the formatter.
This commit is contained in:
Tobin C. Harding 2022-11-29 09:49:02 +11:00
parent 2780e6cdaa
commit 4c8570b512
1 changed files with 37 additions and 51 deletions

View File

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