base58_monero/
lib.rs

1// Rust Monero Base58 Library
2// Written in 2019-2023 by
3//   Monero Rust Contributors
4//
5// Permission is hereby granted, free of charge, to any person obtaining a copy
6// of this software and associated documentation files (the "Software"), to deal
7// in the Software without restriction, including without limitation the rights
8// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9// copies of the Software, and to permit persons to whom the Software is
10// furnished to do so, subject to the following conditions:
11//
12// The above copyright notice and this permission notice shall be included in all
13// copies or substantial portions of the Software.
14//
15
16//! # Monero Base58
17//!
18//! Monero base58 is not like Bitcoin base58, bytes are converted in 8-byte blocks. The last block
19//! can have less than 8 bytes, but at least 1 byte. Eight bytes converts to 11 or less Base58
20//! characters; if a particular block converts to `<11` characters, the conversion pads it with
21//! "1"s (1 is 0 in Base58). Likewise, the final block can convert to 11 or less Base58 digits.
22//!
23//! Due to the conditional padding, the 69-byte string, like Monero addresses, will always convert
24//! to 95 Base58 characters `(8 * 11 + 7)`; where 7 is length of the last block of 5 bytes.
25//!
26//! The alphabet is composed of 58 characters visually not similar to avoid confusion, e.g. both
27//! `1` and `l` are not part of the alphabet together, only `1` is present. The full alphabet is
28//! composed of: `123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz`
29//!
30//! ## Features
31//!
32//!  * `std`: enable std error implementation on the Error enum.
33//!  * `check`: enable encoding/decoding base58 strings with a 4 bytes tail checksum.
34//!  * `stream`: enable encoding/decoding base58 asyncronous streams of data.
35//!
36//! Only the `std` feature is enabled by default, to use this crate in `no_std` environment use:
37//!
38//! ```text
39//! base58-monero = { version = "2", default-features = false }
40//! ```
41//!
42//! or to enable `stream` one use:
43//!
44//! ```text
45//! base58-monero = { version = "2", features = ["stream"] }
46//! ```
47//!
48//! ## Examples
49//!
50//! Encoding and decoding an array of bytes with Monero base58 format:
51//!
52//! ```rust
53//! use base58_monero::{encode, decode, Error};
54//!
55//! let input = b"Hello World";
56//! let encoded_input = encode(input)?;
57//!
58//! let decoded_input = decode(&encoded_input)?;
59//!
60//! assert_eq!(&input[..], &decoded_input[..]);
61//! # Ok::<(), Error>(())
62//! ```
63//!
64//! With the `check` feature Monero base58 also comes with a `checksum` mode. The checksum is
65//! composed with the first 4 bytes of a `Keccak256` result of the string. Encoding and decoding
66//! with a checksum:
67//!
68//! ```rust
69//! use base58_monero::{encode_check, decode_check, Error};
70//!
71//! let input = b"Hello World";
72//! let encoded_input = encode_check(input)?;
73//!
74//! let decoded_input = decode_check(&encoded_input)?;
75//!
76//! assert_eq!(&input[..], &decoded_input[..]);
77//! # Ok::<(), Error>(())
78//! ```
79
80#![cfg_attr(docsrs, feature(doc_cfg))]
81#![recursion_limit = "256"]
82// Coding conventions
83#![forbid(unsafe_code)]
84#![deny(missing_docs)]
85// Use a no_std environment when std feature is not enabled
86#![cfg_attr(not(feature = "std"), no_std)]
87
88pub mod base58;
89
90pub use base58::decode;
91#[cfg(feature = "check")]
92pub use base58::decode_check;
93#[cfg(feature = "stream")]
94pub use base58::decode_stream;
95#[cfg(all(feature = "check", feature = "stream"))]
96pub use base58::decode_stream_check;
97pub use base58::encode;
98#[cfg(feature = "check")]
99pub use base58::encode_check;
100#[cfg(feature = "stream")]
101pub use base58::encode_stream;
102#[cfg(all(feature = "check", feature = "stream"))]
103pub use base58::encode_stream_check;
104pub use base58::Error;