invoice/
base58.rs

1// Written in 2014 by Andrew Poelstra <apoelstra@wpsoftware.net>
2// SPDX-License-Identifier: CC0-1.0
3
4//! Base58 encoder and decoder.
5//!
6//! This module provides functions for encoding and decoding base58 slices and
7//! strings respectively.
8
9use core::{fmt, iter, slice, str};
10
11use commit_verify::{Digest, Sha256};
12
13static BASE58_CHARS: &[u8] = b"123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz";
14
15#[rustfmt::skip]
16static BASE58_DIGITS: [Option<u8>; 128] = [
17    None,     None,     None,     None,     None,     None,     None,     None,     // 0-7
18    None,     None,     None,     None,     None,     None,     None,     None,     // 8-15
19    None,     None,     None,     None,     None,     None,     None,     None,     // 16-23
20    None,     None,     None,     None,     None,     None,     None,     None,     // 24-31
21    None,     None,     None,     None,     None,     None,     None,     None,     // 32-39
22    None,     None,     None,     None,     None,     None,     None,     None,     // 40-47
23    None,     Some(0),  Some(1),  Some(2),  Some(3),  Some(4),  Some(5),  Some(6),  // 48-55
24    Some(7),  Some(8),  None,     None,     None,     None,     None,     None,     // 56-63
25    None,     Some(9),  Some(10), Some(11), Some(12), Some(13), Some(14), Some(15), // 64-71
26    Some(16), None,     Some(17), Some(18), Some(19), Some(20), Some(21), None,     // 72-79
27    Some(22), Some(23), Some(24), Some(25), Some(26), Some(27), Some(28), Some(29), // 80-87
28    Some(30), Some(31), Some(32), None,     None,     None,     None,     None,     // 88-95
29    None,     Some(33), Some(34), Some(35), Some(36), Some(37), Some(38), Some(39), // 96-103
30    Some(40), Some(41), Some(42), Some(43), None,     Some(44), Some(45), Some(46), // 104-111
31    Some(47), Some(48), Some(49), Some(50), Some(51), Some(52), Some(53), Some(54), // 112-119
32    Some(55), Some(56), Some(57), None,     None,     None,     None,     None,     // 120-127
33];
34
35/// Decodes a base58-encoded string into a byte vector.
36pub fn decode(data: &str) -> Result<Vec<u8>, Error> {
37    // 11/15 is just over log_256(58)
38    let mut scratch = vec![0u8; 1 + data.len() * 11 / 15];
39    // Build in base 256
40    for d58 in data.bytes() {
41        // Compute "X = X * 58 + next_digit" in base 256
42        if d58 as usize >= BASE58_DIGITS.len() {
43            return Err(Error::BadByte(d58));
44        }
45        let mut carry = match BASE58_DIGITS[d58 as usize] {
46            Some(d58) => d58 as u32,
47            None => {
48                return Err(Error::BadByte(d58));
49            }
50        };
51        for d256 in scratch.iter_mut().rev() {
52            carry += *d256 as u32 * 58;
53            *d256 = carry as u8;
54            carry /= 256;
55        }
56        assert_eq!(carry, 0);
57    }
58
59    // Copy leading zeroes directly
60    let mut ret: Vec<u8> = data.bytes().take_while(|&x| x == BASE58_CHARS[0]).map(|_| 0).collect();
61    // Copy rest of string
62    ret.extend(scratch.into_iter().skip_while(|&x| x == 0));
63    Ok(ret)
64}
65
66/// Decodes a base58check-encoded string into a byte vector verifying the checksum.
67pub fn decode_check(data: &str) -> Result<Vec<u8>, Error> {
68    let mut ret: Vec<u8> = decode(data)?;
69    if ret.len() < 4 {
70        return Err(Error::TooShort(ret.len()));
71    }
72    let check_start = ret.len() - 4;
73
74    let hash_check = Sha256::digest(&ret[..check_start]);
75    let hash_check = Sha256::digest(hash_check)[..4].try_into().expect("4 byte slice");
76    let data_check = ret[check_start..].try_into().expect("4 byte slice");
77
78    let expected = u32::from_le_bytes(hash_check);
79    let actual = u32::from_le_bytes(data_check);
80
81    if expected != actual {
82        return Err(Error::BadChecksum(expected, actual));
83    }
84
85    ret.truncate(check_start);
86    Ok(ret)
87}
88
89/// Encodes `data` as a base58 string (see also `base58::encode_check()`).
90pub fn encode(data: &[u8]) -> String { encode_iter(data.iter().cloned()) }
91
92/// Encodes `data` as a base58 string including the checksum.
93///
94/// The checksum is the first four bytes of the sha256d of the data, concatenated onto the end.
95pub fn encode_check(data: &[u8]) -> String {
96    let checksum = Sha256::digest(data);
97    let checksum = Sha256::digest(checksum);
98    encode_iter(data.iter().cloned().chain(checksum[0..4].iter().cloned()))
99}
100
101/// Encodes a slice as base58, including the checksum, into a formatter.
102///
103/// The checksum is the first four bytes of the sha256d of the data, concatenated onto the end.
104pub fn encode_check_to_fmt(fmt: &mut fmt::Formatter, data: &[u8]) -> fmt::Result {
105    let checksum = Sha256::digest(data);
106    let checksum = Sha256::digest(checksum);
107    let iter = data.iter().cloned().chain(checksum[0..4].iter().cloned());
108    format_iter(fmt, iter)
109}
110
111fn encode_iter<I>(data: I) -> String
112where I: Iterator<Item = u8> + Clone {
113    let mut ret = String::new();
114    format_iter(&mut ret, data).expect("writing into string shouldn't fail");
115    ret
116}
117
118fn format_iter<I, W>(writer: &mut W, data: I) -> Result<(), fmt::Error>
119where
120    I: Iterator<Item = u8> + Clone,
121    W: fmt::Write,
122{
123    let mut ret = SmallVec::new();
124
125    let mut leading_zero_count = 0;
126    let mut leading_zeroes = true;
127    // Build string in little endian with 0-58 in place of characters...
128    for d256 in data {
129        let mut carry = d256 as usize;
130        if leading_zeroes && carry == 0 {
131            leading_zero_count += 1;
132        } else {
133            leading_zeroes = false;
134        }
135
136        for ch in ret.iter_mut() {
137            let new_ch = *ch as usize * 256 + carry;
138            *ch = (new_ch % 58) as u8;
139            carry = new_ch / 58;
140        }
141        while carry > 0 {
142            ret.push((carry % 58) as u8);
143            carry /= 58;
144        }
145    }
146
147    // ... then reverse it and convert to chars
148    for _ in 0..leading_zero_count {
149        ret.push(0);
150    }
151
152    for ch in ret.iter().rev() {
153        writer.write_char(BASE58_CHARS[*ch as usize] as char)?;
154    }
155
156    Ok(())
157}
158
159/// Vector-like object that holds the first 100 elements on the stack. If more space is needed it
160/// will be allocated on the heap.
161struct SmallVec<T> {
162    len: usize,
163    stack: [T; 100],
164    heap: Vec<T>,
165}
166
167impl<T: Default + Copy> SmallVec<T> {
168    fn new() -> SmallVec<T> {
169        SmallVec {
170            len: 0,
171            stack: [T::default(); 100],
172            heap: Vec::new(),
173        }
174    }
175
176    fn push(&mut self, val: T) {
177        if self.len < 100 {
178            self.stack[self.len] = val;
179            self.len += 1;
180        } else {
181            self.heap.push(val);
182        }
183    }
184
185    fn iter(&self) -> iter::Chain<slice::Iter<T>, slice::Iter<T>> {
186        // If len<100 then we just append an empty vec
187        self.stack[0..self.len].iter().chain(self.heap.iter())
188    }
189
190    fn iter_mut(&mut self) -> iter::Chain<slice::IterMut<T>, slice::IterMut<T>> {
191        // If len<100 then we just append an empty vec
192        self.stack[0..self.len].iter_mut().chain(self.heap.iter_mut())
193    }
194}
195
196/// An error that might occur during base58 decoding.
197#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy)]
198pub enum Error {
199    /// Invalid character encountered.
200    BadByte(u8),
201    /// Checksum was not correct (expected, actual).
202    BadChecksum(u32, u32),
203    /// The length (in bytes) of the object was not correct.
204    ///
205    /// Note that if the length is excessively long the provided length may be an estimate (and the
206    /// checksum step may be skipped).
207    InvalidLength(usize),
208    /// Extended Key version byte(s) were not recognized.
209    InvalidExtendedKeyVersion([u8; 4]),
210    /// Address version byte were not recognized.
211    InvalidAddressVersion(u8),
212    /// Checked data was less than 4 bytes.
213    TooShort(usize),
214}
215
216impl fmt::Display for Error {
217    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
218        match *self {
219            Error::BadByte(b) => write!(f, "invalid base58 character {:#x}", b),
220            Error::BadChecksum(exp, actual) => {
221                write!(f, "base58ck checksum {:#x} does not match expected {:#x}", actual, exp)
222            }
223            Error::InvalidLength(ell) => write!(f, "length {} invalid for this base58 type", ell),
224            Error::InvalidExtendedKeyVersion(ref v) => {
225                write!(f, "extended key version {:#04x?} is invalid for this base58 type", v)
226            }
227            Error::InvalidAddressVersion(ref v) => {
228                write!(f, "address version {} is invalid for this base58 type", v)
229            }
230            Error::TooShort(_) => write!(f, "base58ck data not even long enough for a checksum"),
231        }
232    }
233}