Skip to content

Commit

Permalink
Bech32m Support (#50)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
clarkmoody committed Feb 14, 2021
1 parent 1495801 commit 6c87d18
Show file tree
Hide file tree
Showing 7 changed files with 200 additions and 83 deletions.
65 changes: 65 additions & 0 deletions .github/workflows/rust.yml
@@ -0,0 +1,65 @@
on: [pull_request]

name: Continuous Integration

jobs:
test:
name: Test Suite
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- 1.29.0
- stable
- nightly
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- uses: actions-rs/cargo@v1
with:
command: test
args: --verbose --features strict

fmt:
name: Rustfmt
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- run: rustup component add rustfmt
- uses: actions-rs/cargo@v1
with:
command: fmt
args: --all -- --check

clippy:
name: Clippy
runs-on: ubuntu-latest
strategy:
matrix:
rust:
- stable
steps:
- uses: actions/checkout@v2
- uses: actions-rs/toolchain@v1
with:
profile: minimal
toolchain: ${{ matrix.rust }}
override: true
- run: rustup component add clippy
- uses: actions-rs/cargo@v1
with:
command: clippy
args: -- -D warnings
27 changes: 0 additions & 27 deletions .travis.yml

This file was deleted.

2 changes: 1 addition & 1 deletion Cargo.toml
@@ -1,6 +1,6 @@
[package]
name = "bech32"
version = "0.7.3"
version = "0.8.0"
authors = ["Clark Moody"]
repository = "https://github.com/rust-bitcoin/rust-bech32"
description = "Encodes and decodes the Bech32 format"
Expand Down
1 change: 1 addition & 0 deletions clippy.toml
@@ -0,0 +1 @@
msrv = "1.29.0"
5 changes: 3 additions & 2 deletions fuzz/fuzz_targets/decode_rnd.rs
Expand Up @@ -10,7 +10,7 @@ fn do_test(data: &[u8]) {
Err(_) => return,
};

assert_eq!(bech32::encode(&b32.0, b32.1).unwrap(), data_str);
assert_eq!(bech32::encode(&b32.0, b32.1, b32.2).unwrap(), data_str);
}

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

#[cfg(feature = "honggfuzz")]
#[macro_use] extern crate honggfuzz;
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
Expand Down
17 changes: 13 additions & 4 deletions fuzz/fuzz_targets/encode_decode.rs
Expand Up @@ -13,18 +13,26 @@ fn do_test(data: &[u8]) {
return;
}

let hrp = String::from_utf8_lossy(&data[1..hrp_end]).to_lowercase().to_string();
let hrp = String::from_utf8_lossy(&data[1..hrp_end])
.to_lowercase()
.to_string();

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

if let Ok(data_str) = bech32::encode(&hrp, &dp).map(|b32| b32.to_string()) {
let variant = if data[0] > 0x0f {
bech32::Variant::Bech32m
} else {
bech32::Variant::Bech32
};

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).unwrap(), data_str);
assert_eq!(bech32::encode(&b32.0, &b32.1, b32.2).unwrap(), data_str);
}
}

Expand All @@ -38,7 +46,8 @@ fn main() {
}

#[cfg(feature = "honggfuzz")]
#[macro_use] extern crate honggfuzz;
#[macro_use]
extern crate honggfuzz;
#[cfg(feature = "honggfuzz")]
fn main() {
loop {
Expand Down

0 comments on commit 6c87d18

Please sign in to comment.