pbech32
Bech32 encoding and decoding library.
Links:
What is Bech32?
Bech32 is a fast and user-friendly base 32 encoding format that includes a namespace and checksum. Bech32m is an update to Bech32 with an improved checksum algorithm.
A Bech32 string contains a human-readable part (HRP), an encoded data part, and a 6 character checksum. The data part and checksum are base 32-encoded with a user-friendly alphabet that only contains lowercase ASCII alphanumeric characters.
Here is an example Bech32m string:
hello1vehkc6mn27xpct
Bech32 and Bech32m are specified in BIP173 and BIP350, respectively.
Library Features
- Bech32 (BIP173) and Bech32m (BIP350) support.
- Idiomatic encoding and decoding via the
DisplayandFromStrtraits. - Decode arbitrarily long strings.
- Streamed, allocation-free encoding to any writer.
- No external dependencies.
Examples
Decode from string:
use Bech32;
let s = "a1qypqxpq9mqr2hj"; // bech32m-encoded string
let got: Bech32 = s.parse?; // decode string
assert_eq!; // check human-readable part
assert_eq!; // check data
Encode to string:
use ;
let scheme = Bech32m; // checksum scheme
let hrp: Hrp = "a".parse?; // human-readable part
let data = vec!; // data
let got = Bech32 .to_string; // encode as string
assert_eq!; // check result
Decoding a string verifies the checksum to catch mistakes:
use ;
let s = "a1wypqxpq9mqr2hj"; // string with error ("q" changed to "w")
let got = s.; // try to decode string
assert_eq!; // check result
Encode to a writer:
use Write;
use ;
let mut vec: = Vecnew; // output vector
let hrp: Hrp = "hello".parse?; // human readable part
let mut encoder = new?; // create encoder
encoder.write_all?; // write data
encoder.flush?; // flush encoder (REQUIRED)
let got = strfrom_utf8?; // convert output vector to string
assert_eq!; // check result
Many error variants have a context field. Try to decode a string which has an invalid character at position 1:
use ;
let s = "a 1xxxxxx"; // string with invalid character at position 1
assert_eq!; // check result
More examples are available in examples/.
Install
pbech32 package page on crates.io
Run cargo add pbech32 to add pbech32 as a dependency to an
exiting Rust project:
Run cargo install pbech32 to install the bech32 tool:
# install bech32 tool in cargo bin dir (e.g. `~/.cargo/bin`)
Build
Run cargo build to create a debug build of the bech32 tool in
target/debug/:
| ;
Run cargo build --release to create a release build of the bech32
tool in target/release/:
| ;
You can also build the bech32 tool in a container using
Podman or Docker like this:
| ;
To build a static binary of the example bech32 tool in a container:
| ;
Documentation
pbech32 API documentation on docs.rs
Run cargo doc to build the API documentation locally in
target/doc/pbech32/:
Run cargo doc --lib build the library documentation and exclude the
bech32 tool documentation:
# remove generated docs
# (needed to clean up stale artifacts)
# generate library-only docs
Tests
Use cargo test to run the test suite:
; ; ; ; ;
;
Use cargo clippy to run the linter:
)
Install cargo-tarpaulin and use cargo tarpaulin to check code
coverage:
||
||
||
||
||
||
||
Note: Some of the tests in src/bin/bech32.rs are ignored by
default because the tests set environment variables which will cause
tests in other threads to fail sporadically.
You can run the tests in a single thread and enable the ignored tests like this:
||
||
||
||
||
||
||