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 18, 2023
1 parent ea644f0 commit 68c4dfd
Show file tree
Hide file tree
Showing 10 changed files with 686 additions and 1,217 deletions.
19 changes: 10 additions & 9 deletions embedded/no-allocator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#![no_main]
#![no_std]

use arrayvec::{ArrayString, ArrayVec};
use bech32::{self, u5, Hrp, Variant, ByteIterExt, Bech32};
use arrayvec::ArrayString;
use bech32::primitives::decode::CheckedHrpstring;
use bech32::{Bech32, Hrp};
use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln};
use panic_halt as _;
Expand All @@ -20,20 +20,21 @@ use panic_halt as _;
fn main() -> ! {
let mut encoded = ArrayString::<30>::new();

let base32 = [0x00u8, 0x01, 0x02].iter().copied().bytes_to_fes().collect::<ArrayVec<u5, 30>>();
let data = [0x00u8, 0x01, 0x02];
let hrp = Hrp::parse("bech32").expect("failed to parse hrp");

let hrp = Hrp::parse("bech32").unwrap();

bech32::encode_to_fmt_anycase(&mut encoded, hrp, &base32, Variant::Bech32).unwrap().unwrap();
bech32::encode_to_fmt::<Bech32, _>(&mut encoded, hrp, &data)
.expect("failed to encode");
test(&*encoded == "bech321qqqsyrhqy2a");

hprintln!("{}", encoded).unwrap();

let unchecked = CheckedHrpstring::new::<Bech32>(&encoded).unwrap();
let unchecked =
CheckedHrpstring::new::<Bech32>(&encoded).expect("failed to construct CheckedHrpstring");
let iter = unchecked.byte_iter();

test(unchecked.hrp() == hrp);
let res = unchecked.byte_iter().collect::<ArrayVec<u8, 30>>();
test(&res == [0x00, 0x01, 0x02].as_ref());
test(iter.eq(data.iter().map(|&b| b)));

debug::exit(debug::EXIT_SUCCESS);

Expand Down
20 changes: 10 additions & 10 deletions embedded/with-allocator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ extern crate alloc;
use core::alloc::Layout;

use alloc_cortex_m::CortexMHeap;
use bech32::{self, FromBase32, Hrp, ToBase32, Variant};
use bech32::{Bech32m, Hrp};
use cortex_m::asm;
use cortex_m_rt::entry;
use cortex_m_semihosting::{debug, hprintln};
use panic_halt as _;

use self::alloc::string::ToString;
use self::alloc::vec;
use self::alloc::vec::Vec;

#[global_allocator]
static ALLOCATOR: CortexMHeap = CortexMHeap::empty();
Expand All @@ -26,16 +24,18 @@ fn main() -> ! {
// Initialize the allocator BEFORE you use it
unsafe { ALLOCATOR.init(cortex_m_rt::heap_start() as usize, HEAP_SIZE) }

let hrp = Hrp::parse("bech32").unwrap();
let encoded = bech32::encode(hrp, vec![0x00, 0x01, 0x02].to_base32(), Variant::Bech32).unwrap();
test(encoded == "bech321qqqsyrhqy2a".to_string());
let data = [0x00u8, 0x01, 0x02];
let hrp = Hrp::parse("bech32").expect("failed to parse hrp");

let encoded = bech32::encode::<Bech32m>(hrp, &data).expect("failed to encode");
test(encoded == "bech321qqqsyktsg0l".to_string());

hprintln!("{}", encoded).unwrap();

let (got_hrp, data, variant) = bech32::decode(&encoded).unwrap();
let (got_hrp, got_data) = bech32::decode(&encoded).expect("failed to decode");

test(got_hrp == hrp);
test(Vec::<u8>::from_base32(&data).unwrap() == vec![0x00, 0x01, 0x02]);
test(variant == Variant::Bech32);
test(&got_data == &data);

debug::exit(debug::EXIT_SUCCESS);

Expand All @@ -51,7 +51,7 @@ fn test(result: bool) {
// define what happens in an Out Of Memory (OOM) condition
#[alloc_error_handler]
fn alloc_error(layout: Layout) -> ! {
hprintln!("{:?}", layout);
hprintln!("{:?}", layout).unwrap();
asm::bkpt();

loop {}
Expand Down
16 changes: 8 additions & 8 deletions fuzz/fuzz_targets/decode_rnd.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
extern crate bech32;

use bech32::primitives::decode::{CheckedHrpstring, SegwitHrpstring, UncheckedHrpstring};
use bech32::Bech32m;

// Checks that we do not crash if passed random data while decoding.
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,
Err(_) => return,
};

assert_eq!(bech32::encode(b32.0, b32.1, b32.2).unwrap(), data_str);
let _ = UncheckedHrpstring::new(&data_str);
let _ = CheckedHrpstring::new::<Bech32m>(&data_str);
let _ = SegwitHrpstring::new(&data_str);
}

#[cfg(feature = "afl")]
Expand Down Expand Up @@ -54,7 +54,7 @@ mod tests {
#[test]
fn duplicate_crash() {
let mut a = Vec::new();
extend_vec_from_hex("00000000", &mut a);
extend_vec_from_hex("39313131", &mut a);
super::do_test(&a);
}
}
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(&address).expect("should be able to decode own encoding");
assert_eq!(bech32::encode::<Bech32m>(hrp, &data).unwrap(), address);
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/hrp.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
// SPDX-License-Identifier: MIT

#[doc(inline)]
pub use crate::primitives::hrp::Hrp;
#[doc(inline)]
pub use crate::primitives::hrp::BC;
#[doc(inline)]
pub use crate::primitives::hrp::BCRT;
#[doc(inline)]
pub use crate::primitives::hrp::TB;
Loading

0 comments on commit 68c4dfd

Please sign in to comment.