Skip to content

Commit

Permalink
Re-write crate level API
Browse files Browse the repository at this point in the history
Now we have all the new `primitives` pieces in place we can re-write the
public API to take advantage of them - WIN!
  • Loading branch information
tcharding committed Sep 11, 2023
1 parent 1867dcf commit 421713e
Show file tree
Hide file tree
Showing 8 changed files with 324 additions and 1,240 deletions.
9 changes: 5 additions & 4 deletions fuzz/fuzz_targets/decode_rnd.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
extern crate bech32;

use bech32::Bech32m;

fn do_test(data: &[u8]) {
let data_str = String::from_utf8_lossy(data);
let decoded = bech32::decode(&data_str);
let b32 = match decoded {
Ok(b32) => b32,
let (hrp, data) = match bech32::decode_bech32m(&data_str) {
Ok((hrp, data)) => (hrp, data),
Err(_) => return,
};

assert_eq!(bech32::encode(b32.0, b32.1, b32.2).unwrap(), data_str);
assert_eq!(bech32::encode::<Bech32m>(hrp, &data).unwrap(), data_str);
}

#[cfg(feature = "afl")]
Expand Down
22 changes: 5 additions & 17 deletions fuzz/fuzz_targets/encode_decode.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
extern crate bech32;

use std::convert::TryFrom;
use std::str;

use bech32::Hrp;
use bech32::{Bech32m, Hrp};

fn do_test(data: &[u8]) {
if data.len() < 1 {
Expand All @@ -16,28 +15,17 @@ fn do_test(data: &[u8]) {
return;
}

let dp = data[hrp_end..]
.iter()
.map(|b| bech32::u5::try_from(b % 32).unwrap())
.collect::<Vec<_>>();

let variant = if data[0] > 0x0f {
bech32::Variant::Bech32m
} else {
bech32::Variant::Bech32
};
let dp = &data[hrp_end..];

match str::from_utf8(&data[1..hrp_end]) {
Err(_) => return,
Ok(s) => {
match Hrp::parse(&s) {
Err(_) => return,
Ok(hrp) => {
if let Ok(data_str) = bech32::encode(hrp, &dp, variant).map(|b32| b32.to_string()) {
let decoded = bech32::decode(&data_str);
let b32 = decoded.expect("should be able to decode own encoding");

assert_eq!(bech32::encode(b32.0, &b32.1, b32.2).unwrap(), data_str);
if let Ok(address) = bech32::encode::<Bech32m>(hrp, dp) {
let (hrp, data) = bech32::decode_bech32m(&address).expect("should be able to decode own encoding");
assert_eq!(bech32::encode::<Bech32m>(hrp, &data).unwrap(), address);
}
}
}
Expand Down
15 changes: 15 additions & 0 deletions src/hrp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
// SPDX-License-Identifier: MIT

//! Re-export the hrp types to make importing ergonomic for the top level APIs.

/// The human-readable part used by the Bitcoin mainnet network.
#[doc(inline)]
pub use crate::primitives::hrp::BC;
/// The human-readable part used when running a Bitcoin regtest network.
#[doc(inline)]
pub use crate::primitives::hrp::BCRT;
/// The human-readable part used by the Bitcoin testnet networks (testnet, signet).
#[doc(inline)]
pub use crate::primitives::hrp::TB;
/// The human-readable part (human readable prefix before the '1' separator).
pub use crate::primitives::hrp::Hrp;
Loading

0 comments on commit 421713e

Please sign in to comment.