Skip to content

Commit 79918f6

Browse files
committed
Re-write crate level API
Now we have all the new `primitives` pieces in place we can re-write the public API to take advantage of them - WIN!
1 parent 41cf1bc commit 79918f6

File tree

10 files changed

+653
-1223
lines changed

10 files changed

+653
-1223
lines changed

embedded/no-allocator/src/main.rs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77
#![no_main]
88
#![no_std]
99

10-
use arrayvec::{ArrayString, ArrayVec};
11-
use bech32::{self, u5, Hrp, Variant, ByteIterExt, Bech32};
10+
use arrayvec::ArrayString;
1211
use bech32::primitives::decode::CheckedHrpstring;
12+
use bech32::{Bech32, Hrp};
1313
use cortex_m_rt::entry;
1414
use cortex_m_semihosting::{debug, hprintln};
1515
use panic_halt as _;
@@ -20,20 +20,21 @@ use panic_halt as _;
2020
fn main() -> ! {
2121
let mut encoded = ArrayString::<30>::new();
2222

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

25-
let hrp = Hrp::parse("bech32").unwrap();
26-
27-
bech32::encode_to_fmt_anycase(&mut encoded, hrp, &base32, Variant::Bech32).unwrap().unwrap();
26+
bech32::encode_to_fmt::<Bech32, _>(&mut encoded, hrp, &data)
27+
.expect("failed to encode");
2828
test(&*encoded == "bech321qqqsyrhqy2a");
2929

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

32-
let unchecked = CheckedHrpstring::new::<Bech32>(&encoded).unwrap();
32+
let unchecked =
33+
CheckedHrpstring::new::<Bech32>(&encoded).expect("failed to construct CheckedHrpstring");
34+
let iter = unchecked.byte_iter();
3335

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

3839
debug::exit(debug::EXIT_SUCCESS);
3940

embedded/with-allocator/src/main.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ extern crate alloc;
66
use core::alloc::Layout;
77

88
use alloc_cortex_m::CortexMHeap;
9-
use bech32::{self, FromBase32, Hrp, ToBase32, Variant};
9+
use bech32::{Bech32m, Hrp};
1010
use cortex_m::asm;
1111
use cortex_m_rt::entry;
1212
use cortex_m_semihosting::{debug, hprintln};
1313
use panic_halt as _;
1414

1515
use self::alloc::string::ToString;
16-
use self::alloc::vec;
17-
use self::alloc::vec::Vec;
1816

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

29-
let hrp = Hrp::parse("bech32").unwrap();
30-
let encoded = bech32::encode(hrp, vec![0x00, 0x01, 0x02].to_base32(), Variant::Bech32).unwrap();
31-
test(encoded == "bech321qqqsyrhqy2a".to_string());
27+
let data = [0x00u8, 0x01, 0x02];
28+
let hrp = Hrp::parse("bech32").expect("failed to parse hrp");
29+
30+
let encoded = bech32::encode::<Bech32m>(hrp, &data).expect("failed to encode");
31+
test(encoded == "bech321qqqsyktsg0l".to_string());
3232

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

35-
let (got_hrp, data, variant) = bech32::decode(&encoded).unwrap();
35+
let (got_hrp, got_data) = bech32::decode(&encoded).expect("failed to decode");
36+
3637
test(got_hrp == hrp);
37-
test(Vec::<u8>::from_base32(&data).unwrap() == vec![0x00, 0x01, 0x02]);
38-
test(variant == Variant::Bech32);
38+
test(&got_data == &data);
3939

4040
debug::exit(debug::EXIT_SUCCESS);
4141

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

5757
loop {}

fuzz/fuzz_targets/decode_rnd.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
extern crate bech32;
22

3+
use bech32::primitives::decode::{CheckedHrpstring, SegwitHrpstring, UncheckedHrpstring};
4+
use bech32::Bech32m;
5+
6+
// Checks that we do not crash if passed random data while decoding.
37
fn do_test(data: &[u8]) {
48
let data_str = String::from_utf8_lossy(data);
5-
let decoded = bech32::decode(&data_str);
6-
let b32 = match decoded {
7-
Ok(b32) => b32,
8-
Err(_) => return,
9-
};
10-
11-
assert_eq!(bech32::encode(b32.0, b32.1, b32.2).unwrap(), data_str);
9+
let _ = UncheckedHrpstring::new(&data_str);
10+
let _ = CheckedHrpstring::new::<Bech32m>(&data_str);
11+
let _ = SegwitHrpstring::new(&data_str);
1212
}
1313

1414
#[cfg(feature = "afl")]
@@ -54,7 +54,7 @@ mod tests {
5454
#[test]
5555
fn duplicate_crash() {
5656
let mut a = Vec::new();
57-
extend_vec_from_hex("00000000", &mut a);
57+
extend_vec_from_hex("39313131", &mut a);
5858
super::do_test(&a);
5959
}
6060
}

fuzz/fuzz_targets/encode_decode.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
extern crate bech32;
22

3-
use std::convert::TryFrom;
43
use std::str;
54

6-
use bech32::Hrp;
5+
use bech32::{Bech32m, Hrp};
76

87
fn do_test(data: &[u8]) {
98
if data.len() < 1 {
@@ -16,28 +15,17 @@ fn do_test(data: &[u8]) {
1615
return;
1716
}
1817

19-
let dp = data[hrp_end..]
20-
.iter()
21-
.map(|b| bech32::u5::try_from(b % 32).unwrap())
22-
.collect::<Vec<_>>();
23-
24-
let variant = if data[0] > 0x0f {
25-
bech32::Variant::Bech32m
26-
} else {
27-
bech32::Variant::Bech32
28-
};
18+
let dp = &data[hrp_end..];
2919

3020
match str::from_utf8(&data[1..hrp_end]) {
3121
Err(_) => return,
3222
Ok(s) => {
3323
match Hrp::parse(&s) {
3424
Err(_) => return,
3525
Ok(hrp) => {
36-
if let Ok(data_str) = bech32::encode(hrp, &dp, variant).map(|b32| b32.to_string()) {
37-
let decoded = bech32::decode(&data_str);
38-
let b32 = decoded.expect("should be able to decode own encoding");
39-
40-
assert_eq!(bech32::encode(b32.0, &b32.1, b32.2).unwrap(), data_str);
26+
if let Ok(address) = bech32::encode::<Bech32m>(hrp, dp) {
27+
let (hrp, data) = bech32::decode(&address).expect("should be able to decode own encoding");
28+
assert_eq!(bech32::encode::<Bech32m>(hrp, &data).unwrap(), address);
4129
}
4230
}
4331
}

src/hrp.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
// SPDX-License-Identifier: MIT
2+
3+
#[doc(inline)]
4+
pub use crate::primitives::hrp::Hrp;
5+
#[doc(inline)]
6+
pub use crate::primitives::hrp::BC;
7+
#[doc(inline)]
8+
pub use crate::primitives::hrp::BCRT;
9+
#[doc(inline)]
10+
pub use crate::primitives::hrp::TB;

0 commit comments

Comments
 (0)