Skip to content

Commit 6c87d18

Browse files
authored
Bech32m Support (#50)
* Bech32m encode / decode support This breaks the API by requiring a `variant` parameter in a few places, which instructs the library whether to use Bech32 or Bech32m. Test cases from BIP-0350 * Update fuzzer for Bech32m * Revert Self idiom (stabilized in Rust 1.32) * Bump version to 0.8.0 * Switch to GitHub Actions for CI
1 parent 1495801 commit 6c87d18

File tree

7 files changed

+200
-83
lines changed

7 files changed

+200
-83
lines changed

.github/workflows/rust.yml

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
on: [pull_request]
2+
3+
name: Continuous Integration
4+
5+
jobs:
6+
test:
7+
name: Test Suite
8+
runs-on: ubuntu-latest
9+
strategy:
10+
matrix:
11+
rust:
12+
- 1.29.0
13+
- stable
14+
- nightly
15+
steps:
16+
- uses: actions/checkout@v2
17+
- uses: actions-rs/toolchain@v1
18+
with:
19+
profile: minimal
20+
toolchain: ${{ matrix.rust }}
21+
override: true
22+
- uses: actions-rs/cargo@v1
23+
with:
24+
command: test
25+
args: --verbose --features strict
26+
27+
fmt:
28+
name: Rustfmt
29+
runs-on: ubuntu-latest
30+
strategy:
31+
matrix:
32+
rust:
33+
- stable
34+
steps:
35+
- uses: actions/checkout@v2
36+
- uses: actions-rs/toolchain@v1
37+
with:
38+
profile: minimal
39+
toolchain: ${{ matrix.rust }}
40+
override: true
41+
- run: rustup component add rustfmt
42+
- uses: actions-rs/cargo@v1
43+
with:
44+
command: fmt
45+
args: --all -- --check
46+
47+
clippy:
48+
name: Clippy
49+
runs-on: ubuntu-latest
50+
strategy:
51+
matrix:
52+
rust:
53+
- stable
54+
steps:
55+
- uses: actions/checkout@v2
56+
- uses: actions-rs/toolchain@v1
57+
with:
58+
profile: minimal
59+
toolchain: ${{ matrix.rust }}
60+
override: true
61+
- run: rustup component add clippy
62+
- uses: actions-rs/cargo@v1
63+
with:
64+
command: clippy
65+
args: -- -D warnings

.travis.yml

-27
This file was deleted.

Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "bech32"
3-
version = "0.7.3"
3+
version = "0.8.0"
44
authors = ["Clark Moody"]
55
repository = "https://github.com/rust-bitcoin/rust-bech32"
66
description = "Encodes and decodes the Bech32 format"

clippy.toml

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
msrv = "1.29.0"

fuzz/fuzz_targets/decode_rnd.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ fn do_test(data: &[u8]) {
1010
Err(_) => return,
1111
};
1212

13-
assert_eq!(bech32::encode(&b32.0, b32.1).unwrap(), data_str);
13+
assert_eq!(bech32::encode(&b32.0, b32.1, b32.2).unwrap(), data_str);
1414
}
1515

1616
#[cfg(feature = "afl")]
@@ -23,7 +23,8 @@ fn main() {
2323
}
2424

2525
#[cfg(feature = "honggfuzz")]
26-
#[macro_use] extern crate honggfuzz;
26+
#[macro_use]
27+
extern crate honggfuzz;
2728
#[cfg(feature = "honggfuzz")]
2829
fn main() {
2930
loop {

fuzz/fuzz_targets/encode_decode.rs

+13-4
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,26 @@ fn do_test(data: &[u8]) {
1313
return;
1414
}
1515

16-
let hrp = String::from_utf8_lossy(&data[1..hrp_end]).to_lowercase().to_string();
16+
let hrp = String::from_utf8_lossy(&data[1..hrp_end])
17+
.to_lowercase()
18+
.to_string();
1719

1820
let dp = data[hrp_end..]
1921
.iter()
2022
.map(|b| bech32::u5::try_from_u8(b % 32).unwrap())
2123
.collect::<Vec<_>>();
2224

23-
if let Ok(data_str) = bech32::encode(&hrp, &dp).map(|b32| b32.to_string()) {
25+
let variant = if data[0] > 0x0f {
26+
bech32::Variant::Bech32m
27+
} else {
28+
bech32::Variant::Bech32
29+
};
30+
31+
if let Ok(data_str) = bech32::encode(&hrp, &dp, variant).map(|b32| b32.to_string()) {
2432
let decoded = bech32::decode(&data_str);
2533
let b32 = decoded.expect("should be able to decode own encoding");
2634

27-
assert_eq!(bech32::encode(&b32.0, &b32.1).unwrap(), data_str);
35+
assert_eq!(bech32::encode(&b32.0, &b32.1, b32.2).unwrap(), data_str);
2836
}
2937
}
3038

@@ -38,7 +46,8 @@ fn main() {
3846
}
3947

4048
#[cfg(feature = "honggfuzz")]
41-
#[macro_use] extern crate honggfuzz;
49+
#[macro_use]
50+
extern crate honggfuzz;
4251
#[cfg(feature = "honggfuzz")]
4352
fn main() {
4453
loop {

0 commit comments

Comments
 (0)