use digest::Digest;
use sha2::Sha256;
use std::{fmt, str};
use byteorder::{ByteOrder, LittleEndian};
pub fn sha256d_hash(data: &[u8]) -> [u8; 32] {
let mut ret = [0; 32];
let mut sha2 = Sha256::new();
sha2.update(data);
ret.copy_from_slice(sha2.finalize().as_slice());
sha2 = Sha256::new();
sha2.update(&ret);
ret.copy_from_slice(sha2.finalize().as_slice());
ret
}
#[inline]
pub fn into_le_low_u32(data: &[u8; 32]) -> u32 {
LittleEndian::read_u64(&data[0..8]) as u32
}
#[derive(thiserror::Error, Debug, PartialEq, Eq, Clone)]
pub enum Error {
#[error("invalid base58 character 0x{0:x}")]
BadByte(u8),
#[error("base58ck checksum 0x{0:x} does not match expected 0x{1:x}")]
BadChecksum(u32, u32),
#[error("length {0} invalid for this base58 type")]
InvalidLength(usize),
#[error("version {0:?} invalid for this base58 type")]
InvalidVersion(Vec<u8>),
#[error("b58ck checksum less than 4 bytes, get {0}")]
TooShort(usize),
#[error("base58 error, {0}")]
Other(String),
}
static BASE58_CHARS: &[u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
static BASE58_DIGITS: [Option<u8>; 128] = [
None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
None,
None,
None,
None,
None,
None,
None, None,
Some(0),
Some(1),
Some(2),
Some(3),
Some(4),
Some(5),
Some(6), Some(7),
Some(8),
None,
None,
None,
None,
None,
None, None,
Some(9),
Some(10),
Some(11),
Some(12),
Some(13),
Some(14),
Some(15), Some(16),
None,
Some(17),
Some(18),
Some(19),
Some(20),
Some(21),
None, Some(22),
Some(23),
Some(24),
Some(25),
Some(26),
Some(27),
Some(28),
Some(29), Some(30),
Some(31),
Some(32),
None,
None,
None,
None,
None, None,
Some(33),
Some(34),
Some(35),
Some(36),
Some(37),
Some(38),
Some(39), Some(40),
Some(41),
Some(42),
Some(43),
None,
Some(44),
Some(45),
Some(46), Some(47),
Some(48),
Some(49),
Some(50),
Some(51),
Some(52),
Some(53),
Some(54), Some(55),
Some(56),
Some(57),
None,
None,
None,
None,
None, ];
pub fn from(data: &str) -> Result<Vec<u8>, Error> {
let mut scratch = vec![0u8; 1 + data.len() * 11 / 15];
for d58 in data.bytes() {
if d58 as usize > BASE58_DIGITS.len() {
return Err(Error::BadByte(d58));
}
let mut carry = match BASE58_DIGITS[d58 as usize] {
Some(d58) => d58 as u32,
None => {
return Err(Error::BadByte(d58));
}
};
for d256 in scratch.iter_mut().rev() {
carry += *d256 as u32 * 58;
*d256 = carry as u8;
carry /= 256;
}
assert_eq!(carry, 0);
}
let mut ret: Vec<u8> = data
.bytes()
.take_while(|&x| x == BASE58_CHARS[0])
.map(|_| 0)
.collect();
ret.extend(scratch.into_iter().skip_while(|&x| x == 0));
Ok(ret)
}
pub fn from_check(data: &str) -> Result<Vec<u8>, Error> {
let mut ret: Vec<u8> = from(data)?;
if ret.len() < 4 {
return Err(Error::TooShort(ret.len()));
}
let ck_start = ret.len() - 4;
let expected = sha256d_hash(&ret[..ck_start]);
let expected = into_le_low_u32(&expected);
let actual = LittleEndian::read_u32(&ret[ck_start..(ck_start + 4)]);
if expected != actual {
return Err(Error::BadChecksum(expected, actual));
}
ret.truncate(ck_start);
Ok(ret)
}
fn encode_iter_utf8<I>(data: I) -> Vec<u8>
where
I: Iterator<Item = u8> + Clone,
{
let (len, _) = data.size_hint();
let mut ret = Vec::with_capacity(1 + len * 7 / 5);
let mut leading_zero_count = 0;
let mut leading_zeroes = true;
for d256 in data {
let mut carry = d256 as usize;
if leading_zeroes && carry == 0 {
leading_zero_count += 1;
} else {
leading_zeroes = false;
}
for ch in ret.iter_mut() {
let new_ch = *ch as usize * 256 + carry;
*ch = (new_ch % 58) as u8;
carry = new_ch / 58;
}
while carry > 0 {
ret.push((carry % 58) as u8);
carry /= 58;
}
}
for _ in 0..leading_zero_count {
ret.push(0);
}
ret.reverse();
for ch in ret.iter_mut() {
*ch = BASE58_CHARS[*ch as usize];
}
ret
}
fn encode_iter<I>(data: I) -> String
where
I: Iterator<Item = u8> + Clone,
{
let ret = encode_iter_utf8(data);
String::from_utf8(ret).unwrap()
}
fn _encode_iter_to_fmt<I>(fmt: &mut fmt::Formatter<'_>, data: I) -> fmt::Result
where
I: Iterator<Item = u8> + Clone,
{
let ret = encode_iter_utf8(data);
fmt.write_str(str::from_utf8(&ret).unwrap())
}
pub fn _encode_slice(data: &[u8]) -> String {
encode_iter(data.iter().cloned())
}
pub fn check_encode_slice(data: &[u8]) -> String {
let checksum = sha256d_hash(&data);
encode_iter(data.iter().cloned().chain(checksum[0..4].iter().cloned()))
}
pub fn _check_encode_slice_to_fmt(fmt: &mut fmt::Formatter<'_>, data: &[u8]) -> fmt::Result {
let checksum = sha256d_hash(&data);
let iter = data.iter().cloned().chain(checksum[0..4].iter().cloned());
_encode_iter_to_fmt(fmt, iter)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::util::from_hex;
#[test]
fn test_base58_encode() {
assert_eq!(&_encode_slice(&[0][..]), "1");
assert_eq!(&_encode_slice(&[1][..]), "2");
assert_eq!(&_encode_slice(&[58][..]), "21");
assert_eq!(&_encode_slice(&[13, 36][..]), "211");
assert_eq!(&_encode_slice(&[0, 13, 36][..]), "1211");
assert_eq!(&_encode_slice(&[0, 0, 0, 0, 13, 36][..]), "1111211");
let addr = from_hex("00f8917303bfa8ef24f292e8fa1419b20460ba064d").unwrap();
assert_eq!(
&check_encode_slice(&addr[..]),
"1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH"
);
}
#[test]
fn test_base58_decode() {
assert_eq!(from("1").ok(), Some(vec![0u8]));
assert_eq!(from("2").ok(), Some(vec![1u8]));
assert_eq!(from("21").ok(), Some(vec![58u8]));
assert_eq!(from("211").ok(), Some(vec![13u8, 36]));
assert_eq!(from("1211").ok(), Some(vec![0u8, 13, 36]));
assert_eq!(from("111211").ok(), Some(vec![0u8, 0, 0, 13, 36]));
assert_eq!(
from_check("1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH").ok(),
Some(from_hex("00f8917303bfa8ef24f292e8fa1419b20460ba064d").unwrap())
)
}
#[test]
fn test_base58_roundtrip() {
let s = "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs";
let v: Vec<u8> = from_check(s).unwrap();
assert_eq!(check_encode_slice(&v[..]), s);
assert_eq!(from_check(&check_encode_slice(&v[..])).ok(), Some(v));
}
}