2014-08-25 06:03:47 +00:00
|
|
|
// Rust Bitcoin Library
|
|
|
|
// Written in 2014 by
|
|
|
|
// Andrew Poelstra <apoelstra@wpsoftware.net>
|
|
|
|
//
|
|
|
|
// To the extent possible under law, the author(s) have dedicated all
|
|
|
|
// copyright and related and neighboring rights to this software to
|
|
|
|
// the public domain worldwide. This software is distributed without
|
|
|
|
// any warranty.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the CC0 Public Domain Dedication
|
|
|
|
// along with this software.
|
|
|
|
// If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
|
|
|
|
//
|
|
|
|
|
2021-11-05 21:58:18 +00:00
|
|
|
//! Base58 encoder and decoder.
|
|
|
|
//!
|
|
|
|
//! This module provides functions for encoding and decoding base58 slices and
|
|
|
|
//! strings respectively.
|
|
|
|
//!
|
2014-08-25 06:03:47 +00:00
|
|
|
|
2021-06-09 10:34:44 +00:00
|
|
|
use prelude::*;
|
|
|
|
|
2021-06-09 10:40:41 +00:00
|
|
|
use core::{fmt, str, iter, slice};
|
2015-10-25 15:16:05 +00:00
|
|
|
|
2022-02-13 08:06:51 +00:00
|
|
|
use hashes::{sha256d, Hash, hex};
|
2020-11-18 13:01:28 +00:00
|
|
|
use secp256k1;
|
2014-08-25 06:03:47 +00:00
|
|
|
|
2020-11-18 13:01:28 +00:00
|
|
|
use util::{endian, key};
|
2019-10-17 21:40:45 +00:00
|
|
|
|
2014-08-25 06:03:47 +00:00
|
|
|
/// An error that might occur during base58 decoding
|
2021-01-30 13:17:14 +00:00
|
|
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone)]
|
2015-04-05 17:58:49 +00:00
|
|
|
pub enum Error {
|
2015-04-07 22:51:57 +00:00
|
|
|
/// Invalid character encountered
|
|
|
|
BadByte(u8),
|
|
|
|
/// Checksum was not correct (expected, actual)
|
|
|
|
BadChecksum(u32, u32),
|
|
|
|
/// The length (in bytes) of the object was not correct
|
2018-05-14 15:07:30 +00:00
|
|
|
/// Note that if the length is excessively long the provided length may be
|
|
|
|
/// an estimate (and the checksum step may be skipped).
|
2015-04-07 22:51:57 +00:00
|
|
|
InvalidLength(usize),
|
2020-11-18 13:05:55 +00:00
|
|
|
/// Extended Key version byte(s) were not recognized
|
|
|
|
InvalidExtendedKeyVersion([u8; 4]),
|
|
|
|
/// Address version byte were not recognized
|
|
|
|
InvalidAddressVersion(u8),
|
2015-04-07 22:51:57 +00:00
|
|
|
/// Checked data was less than 4 bytes
|
|
|
|
TooShort(usize),
|
2020-11-18 13:01:28 +00:00
|
|
|
/// Secp256k1 error while parsing a secret key
|
|
|
|
Secp256k1(secp256k1::Error),
|
2022-02-13 08:06:51 +00:00
|
|
|
/// Hex decoding error
|
|
|
|
// TODO: Remove this as part of crate-smashing, there should not be any key related errors in this module
|
|
|
|
Hex(hex::Error)
|
2014-08-25 06:03:47 +00:00
|
|
|
}
|
|
|
|
|
2015-10-25 15:16:05 +00:00
|
|
|
impl fmt::Display for Error {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
match *self {
|
|
|
|
Error::BadByte(b) => write!(f, "invalid base58 character 0x{:x}", b),
|
|
|
|
Error::BadChecksum(exp, actual) => write!(f, "base58ck checksum 0x{:x} does not match expected 0x{:x}", actual, exp),
|
|
|
|
Error::InvalidLength(ell) => write!(f, "length {} invalid for this base58 type", ell),
|
2021-01-21 09:56:26 +00:00
|
|
|
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),
|
2015-10-25 15:16:05 +00:00
|
|
|
Error::TooShort(_) => write!(f, "base58ck data not even long enough for a checksum"),
|
2020-11-18 13:01:28 +00:00
|
|
|
Error::Secp256k1(ref e) => fmt::Display::fmt(&e, f),
|
2022-02-13 08:06:51 +00:00
|
|
|
Error::Hex(ref e) => write!(f, "Hexadecimal decoding error: {}", e)
|
2015-10-25 15:16:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-09 10:34:44 +00:00
|
|
|
#[cfg(feature = "std")]
|
2021-07-28 19:38:58 +00:00
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "std")))]
|
2021-06-09 10:34:44 +00:00
|
|
|
impl ::std::error::Error for Error {}
|
2015-10-25 15:16:05 +00:00
|
|
|
|
2018-11-09 03:13:55 +00:00
|
|
|
/// Vector-like object that holds the first 100 elements on the stack. If more space is needed it
|
|
|
|
/// will be allocated on the heap.
|
|
|
|
struct SmallVec<T> {
|
|
|
|
len: usize,
|
|
|
|
stack: [T; 100],
|
|
|
|
heap: Vec<T>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Default + Copy> SmallVec<T> {
|
|
|
|
pub fn new() -> SmallVec<T> {
|
|
|
|
SmallVec {
|
|
|
|
len: 0,
|
|
|
|
stack: [T::default(); 100],
|
|
|
|
heap: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn push(&mut self, val: T) {
|
|
|
|
if self.len < 100 {
|
|
|
|
self.stack[self.len] = val;
|
|
|
|
self.len += 1;
|
|
|
|
} else {
|
|
|
|
self.heap.push(val);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter(&self) -> iter::Chain<slice::Iter<T>, slice::Iter<T>> {
|
|
|
|
// If len<100 then we just append an empty vec
|
|
|
|
self.stack[0..self.len].iter().chain(self.heap.iter())
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn iter_mut(&mut self) -> iter::Chain<slice::IterMut<T>, slice::IterMut<T>> {
|
|
|
|
// If len<100 then we just append an empty vec
|
|
|
|
self.stack[0..self.len].iter_mut().chain(self.heap.iter_mut())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-05 19:41:07 +00:00
|
|
|
static BASE58_CHARS: &[u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
|
2014-08-25 06:03:47 +00:00
|
|
|
|
2015-01-18 18:16:01 +00:00
|
|
|
static BASE58_DIGITS: [Option<u8>; 128] = [
|
2015-04-07 22:51:57 +00:00
|
|
|
None, None, None, None, None, None, None, None, // 0-7
|
|
|
|
None, None, None, None, None, None, None, None, // 8-15
|
|
|
|
None, None, None, None, None, None, None, None, // 16-23
|
|
|
|
None, None, None, None, None, None, None, None, // 24-31
|
|
|
|
None, None, None, None, None, None, None, None, // 32-39
|
|
|
|
None, None, None, None, None, None, None, None, // 40-47
|
|
|
|
None, Some(0), Some(1), Some(2), Some(3), Some(4), Some(5), Some(6), // 48-55
|
|
|
|
Some(7), Some(8), None, None, None, None, None, None, // 56-63
|
|
|
|
None, Some(9), Some(10), Some(11), Some(12), Some(13), Some(14), Some(15), // 64-71
|
|
|
|
Some(16), None, Some(17), Some(18), Some(19), Some(20), Some(21), None, // 72-79
|
|
|
|
Some(22), Some(23), Some(24), Some(25), Some(26), Some(27), Some(28), Some(29), // 80-87
|
|
|
|
Some(30), Some(31), Some(32), None, None, None, None, None, // 88-95
|
|
|
|
None, Some(33), Some(34), Some(35), Some(36), Some(37), Some(38), Some(39), // 96-103
|
|
|
|
Some(40), Some(41), Some(42), Some(43), None, Some(44), Some(45), Some(46), // 104-111
|
|
|
|
Some(47), Some(48), Some(49), Some(50), Some(51), Some(52), Some(53), Some(54), // 112-119
|
|
|
|
Some(55), Some(56), Some(57), None, None, None, None, None, // 120-127
|
2014-08-25 06:03:47 +00:00
|
|
|
];
|
|
|
|
|
2018-03-09 17:53:20 +00:00
|
|
|
/// Decode base58-encoded string into a byte vector
|
|
|
|
pub fn from(data: &str) -> Result<Vec<u8>, Error> {
|
|
|
|
// 11/15 is just over log_256(58)
|
|
|
|
let mut scratch = vec![0u8; 1 + data.len() * 11 / 15];
|
|
|
|
// Build in base 256
|
|
|
|
for d58 in data.bytes() {
|
|
|
|
// Compute "X = X * 58 + next_digit" in base 256
|
2020-01-21 11:36:53 +00:00
|
|
|
if d58 as usize >= BASE58_DIGITS.len() {
|
2018-03-09 17:53:20 +00:00
|
|
|
return Err(Error::BadByte(d58));
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
2018-03-09 17:53:20 +00:00
|
|
|
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;
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
|
|
|
assert_eq!(carry, 0);
|
|
|
|
}
|
|
|
|
|
2015-04-11 01:55:59 +00:00
|
|
|
// Copy leading zeroes directly
|
2018-03-09 17:53:20 +00:00
|
|
|
let mut ret: Vec<u8> = data.bytes().take_while(|&x| x == BASE58_CHARS[0])
|
|
|
|
.map(|_| 0)
|
|
|
|
.collect();
|
2015-04-11 01:55:59 +00:00
|
|
|
// Copy rest of string
|
2018-03-09 17:53:20 +00:00
|
|
|
ret.extend(scratch.into_iter().skip_while(|&x| x == 0));
|
|
|
|
Ok(ret)
|
2014-08-28 16:49:03 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 17:53:20 +00:00
|
|
|
/// Decode a base58check-encoded string
|
|
|
|
pub fn from_check(data: &str) -> Result<Vec<u8>, Error> {
|
2018-08-12 16:47:31 +00:00
|
|
|
let mut ret: Vec<u8> = from(data)?;
|
2018-03-09 17:53:20 +00:00
|
|
|
if ret.len() < 4 {
|
|
|
|
return Err(Error::TooShort(ret.len()));
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
2018-03-09 17:53:20 +00:00
|
|
|
let ck_start = ret.len() - 4;
|
2019-10-17 21:40:45 +00:00
|
|
|
let expected = endian::slice_to_u32_le(&sha256d::Hash::hash(&ret[..ck_start])[..4]);
|
|
|
|
let actual = endian::slice_to_u32_le(&ret[ck_start..(ck_start + 4)]);
|
2018-03-09 17:53:20 +00:00
|
|
|
if expected != actual {
|
|
|
|
return Err(Error::BadChecksum(expected, actual));
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
2018-03-09 17:53:20 +00:00
|
|
|
|
|
|
|
ret.truncate(ck_start);
|
|
|
|
Ok(ret)
|
2014-08-28 16:49:03 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 03:13:55 +00:00
|
|
|
fn format_iter<I, W>(writer: &mut W, data: I) -> Result<(), fmt::Error>
|
2018-03-09 17:53:20 +00:00
|
|
|
where
|
2022-03-16 20:22:03 +00:00
|
|
|
I: Iterator<Item=u8> + Clone,
|
2018-11-09 03:13:55 +00:00
|
|
|
W: fmt::Write
|
2018-03-09 17:53:20 +00:00
|
|
|
{
|
2018-11-09 03:13:55 +00:00
|
|
|
let mut ret = SmallVec::new();
|
2018-03-09 17:53:20 +00:00
|
|
|
|
|
|
|
let mut leading_zero_count = 0;
|
|
|
|
let mut leading_zeroes = true;
|
|
|
|
// Build string in little endian with 0-58 in place of characters...
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ... then reverse it and convert to chars
|
|
|
|
for _ in 0..leading_zero_count {
|
|
|
|
ret.push(0);
|
|
|
|
}
|
2018-11-09 03:13:55 +00:00
|
|
|
|
|
|
|
for ch in ret.iter().rev() {
|
|
|
|
writer.write_char(BASE58_CHARS[*ch as usize] as char)?;
|
2018-03-09 17:53:20 +00:00
|
|
|
}
|
2018-11-09 03:13:55 +00:00
|
|
|
|
|
|
|
Ok(())
|
2018-08-20 19:40:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn encode_iter<I>(data: I) -> String
|
|
|
|
where
|
2022-03-16 20:22:03 +00:00
|
|
|
I: Iterator<Item=u8> + Clone,
|
2018-08-20 19:40:00 +00:00
|
|
|
{
|
2018-11-09 03:13:55 +00:00
|
|
|
let mut ret = String::new();
|
|
|
|
format_iter(&mut ret, data).expect("writing into string shouldn't fail");
|
|
|
|
ret
|
2014-08-28 16:49:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 19:40:00 +00:00
|
|
|
|
2018-03-09 17:53:20 +00:00
|
|
|
/// Directly encode a slice as base58
|
|
|
|
pub fn encode_slice(data: &[u8]) -> String {
|
|
|
|
encode_iter(data.iter().cloned())
|
2015-04-08 22:23:45 +00:00
|
|
|
}
|
|
|
|
|
2018-03-09 17:53:20 +00:00
|
|
|
/// Obtain a string with the base58check encoding of a slice
|
|
|
|
/// (Tack the first 4 256-digits of the object's Bitcoin hash onto the end.)
|
|
|
|
pub fn check_encode_slice(data: &[u8]) -> String {
|
2021-11-03 09:20:34 +00:00
|
|
|
let checksum = sha256d::Hash::hash(data);
|
2018-03-09 17:53:20 +00:00
|
|
|
encode_iter(
|
|
|
|
data.iter()
|
|
|
|
.cloned()
|
|
|
|
.chain(checksum[0..4].iter().cloned())
|
|
|
|
)
|
2014-08-28 16:49:03 +00:00
|
|
|
}
|
|
|
|
|
2018-08-20 19:40:00 +00:00
|
|
|
/// Obtain a string with the base58check encoding of a slice
|
|
|
|
/// (Tack the first 4 256-digits of the object's Bitcoin hash onto the end.)
|
|
|
|
pub fn check_encode_slice_to_fmt(fmt: &mut fmt::Formatter, data: &[u8]) -> fmt::Result {
|
2021-11-03 09:20:34 +00:00
|
|
|
let checksum = sha256d::Hash::hash(data);
|
2018-08-20 19:40:00 +00:00
|
|
|
let iter = data.iter()
|
|
|
|
.cloned()
|
|
|
|
.chain(checksum[0..4].iter().cloned());
|
2018-11-09 03:13:55 +00:00
|
|
|
format_iter(fmt, iter)
|
2018-08-20 19:40:00 +00:00
|
|
|
}
|
|
|
|
|
2020-11-18 13:01:28 +00:00
|
|
|
#[doc(hidden)]
|
|
|
|
impl From<key::Error> for Error {
|
|
|
|
fn from(e: key::Error) -> Self {
|
|
|
|
match e {
|
|
|
|
key::Error::Secp256k1(e) => Error::Secp256k1(e),
|
|
|
|
key::Error::Base58(e) => e,
|
2022-02-13 08:06:51 +00:00
|
|
|
key::Error::InvalidKeyPrefix(_) => Error::Secp256k1(secp256k1::Error::InvalidPublicKey),
|
|
|
|
key::Error::Hex(e) => Error::Hex(e)
|
2020-11-18 13:01:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-08 17:02:30 +00:00
|
|
|
|
2014-08-25 06:03:47 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2018-03-09 17:53:20 +00:00
|
|
|
use super::*;
|
2020-01-08 17:02:30 +00:00
|
|
|
use hashes::hex::FromHex;
|
2015-04-07 22:51:57 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_base58_encode() {
|
|
|
|
// Basics
|
2018-03-09 17:53:20 +00:00
|
|
|
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");
|
2015-04-07 22:51:57 +00:00
|
|
|
|
|
|
|
// Leading zeroes
|
2018-03-09 17:53:20 +00:00
|
|
|
assert_eq!(&encode_slice(&[0, 13, 36][..]), "1211");
|
|
|
|
assert_eq!(&encode_slice(&[0, 0, 0, 0, 13, 36][..]), "1111211");
|
2015-04-07 22:51:57 +00:00
|
|
|
|
2018-11-09 23:11:04 +00:00
|
|
|
// Long input (>100 bytes => has to use heap)
|
|
|
|
let res = encode_slice(&"BitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBit\
|
|
|
|
coinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoinBitcoin".as_bytes());
|
|
|
|
let exp = "ZqC5ZdfpZRi7fjA8hbhX5pEE96MdH9hEaC1YouxscPtbJF16qVWksHWR4wwvx7MotFcs2ChbJqK8KJ9X\
|
|
|
|
wZznwWn1JFDhhTmGo9v6GjAVikzCsBWZehu7bm22xL8b5zBR5AsBygYRwbFJsNwNkjpyFuDKwmsUTKvkULCvucPJrN5\
|
|
|
|
QUdxpGakhqkZFL7RU4yT";
|
|
|
|
assert_eq!(&res, exp);
|
|
|
|
|
2015-04-07 22:51:57 +00:00
|
|
|
// Addresses
|
2020-01-08 17:02:30 +00:00
|
|
|
let addr = Vec::from_hex("00f8917303bfa8ef24f292e8fa1419b20460ba064d").unwrap();
|
2018-03-09 17:53:20 +00:00
|
|
|
assert_eq!(&check_encode_slice(&addr[..]), "1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH");
|
2022-01-24 01:36:46 +00:00
|
|
|
}
|
2015-04-07 22:51:57 +00:00
|
|
|
|
2022-01-24 01:36:46 +00:00
|
|
|
#[test]
|
|
|
|
fn test_base58_decode() {
|
2015-04-07 22:51:57 +00:00
|
|
|
// Basics
|
2018-03-09 17:53:20 +00:00
|
|
|
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]));
|
2015-04-07 22:51:57 +00:00
|
|
|
|
|
|
|
// Leading zeroes
|
2018-03-09 17:53:20 +00:00
|
|
|
assert_eq!(from("1211").ok(), Some(vec![0u8, 13, 36]));
|
|
|
|
assert_eq!(from("111211").ok(), Some(vec![0u8, 0, 0, 13, 36]));
|
2015-04-07 22:51:57 +00:00
|
|
|
|
|
|
|
// Addresses
|
2018-03-09 17:53:20 +00:00
|
|
|
assert_eq!(from_check("1PfJpZsjreyVrqeoAfabrRwwjQyoSQMmHH").ok(),
|
2020-01-20 18:08:07 +00:00
|
|
|
Some(Vec::from_hex("00f8917303bfa8ef24f292e8fa1419b20460ba064d").unwrap()));
|
|
|
|
// Non Base58 char.
|
|
|
|
assert_eq!(from("¢").unwrap_err(), Error::BadByte(194));
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_base58_roundtrip() {
|
|
|
|
let s = "xprv9wTYmMFdV23N2TdNG573QoEsfRrWKQgWeibmLntzniatZvR9BmLnvSxqu53Kw1UmYPxLgboyZQaXwTCg8MSY3H2EU4pWcQDnRnrVA1xe8fs";
|
2018-03-09 17:53:20 +00:00
|
|
|
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));
|
2020-01-20 18:08:07 +00:00
|
|
|
|
|
|
|
// Check that empty slice passes roundtrip.
|
|
|
|
assert_eq!(from_check(&check_encode_slice(&[])), Ok(vec![]));
|
|
|
|
// Check that `len > 4` is enforced.
|
|
|
|
assert_eq!(from_check(&encode_slice(&[1,2,3])), Err(Error::TooShort(3)));
|
|
|
|
|
2015-04-07 22:51:57 +00:00
|
|
|
}
|
2014-08-25 06:03:47 +00:00
|
|
|
}
|
|
|
|
|