Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
duesee committed Jan 4, 2020
2 parents dcfcf10 + ccbb003 commit cc12906
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "bech32"
version = "0.7.1"
version = "0.7.2"
authors = ["Clark Moody"]
repository = "https://github.com/rust-bitcoin/rust-bech32"
description = "Encodes and decodes the Bech32 format"
Expand Down
19 changes: 16 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#![cfg_attr(feature = "strict", deny(warnings))]

use std::{error, fmt};
use std::borrow::Cow;

// AsciiExt is needed for Rust 1.14 but not for newer versions
#[allow(unused_imports, deprecated)]
Expand Down Expand Up @@ -334,7 +335,6 @@ enum Case {
}

/// Check if the HRP is valid. Returns the case of the HRP, if any.
/// True for uppercase, false for lowercase, None for only digits.
///
/// # Errors
/// * **MixedCase**: If the HRP contains both uppercase and lowercase characters.
Expand Down Expand Up @@ -384,8 +384,12 @@ pub fn encode_to_fmt<T: AsRef<[u5]>>(
hrp: &str,
data: T,
) -> Result<fmt::Result, Error> {
check_hrp(&hrp)?;
match Bech32Writer::new(hrp, fmt) {
let hrp_lower = match check_hrp(&hrp)? {
Case::Upper => Cow::Owned(hrp.to_lowercase()),
Case::Lower | Case::None => Cow::Borrowed(hrp),
};

match Bech32Writer::new(&hrp_lower, fmt) {
Ok(mut writer) => {
Ok(writer.write(data.as_ref()).and_then(|_| {
// Finalize manually to avoid panic on drop if write fails
Expand Down Expand Up @@ -874,4 +878,13 @@ mod tests {

assert_eq!(encoded_str, written_str);
}

#[test]
fn test_hrp_case() {
// Tests for issue with HRP case checking being ignored for encoding
use ToBase32;
let encoded_str = encode("HRP", [0x00, 0x00].to_base32()).unwrap();

assert_eq!(encoded_str, "hrp1qqqq40atq3");
}
}

0 comments on commit cc12906

Please sign in to comment.