Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bech32Encode generates invalid output if the HRP contains upper case characters. #375

Closed
jonathanknowles opened this issue May 27, 2019 · 2 comments · Fixed by #378
Closed

Comments

@jonathanknowles
Copy link

Overview

When bech32Encode is called with a human-readable part that contains one or more upper-case characters, it produces in an invalid Bech32 string that cannot be decoded with the bech32Decode function.

Analysis

As part of the encoding process, the human-readable part is converted to lower case:

result = T.concat [T.toLower hrp, T.pack "1", T.pack rest]

However, the checksum is calculated before the conversion to lower case takes place:

let dat' = dat ++ bech32CreateChecksum hrp dat

This contradicts the Bech32 specification, which states:

"The lowercase form is used when determining a character's value for checksum purposes."

Therefore, if the original human-readable part contains one or more upper case characters:

  1. the generated checksum will be inconsistent with the human-readable prefix of the output string
  2. the output string will fail to decode.

Example

Consider the following two calls to bech32Encode, differing only in the case of the human-readable part:

> bech32Encode "test" []
> bech32Encode "TEST" []

Expected Behaviour

Both calls to bech32Encode should result in the same output string:

> bech32Encode "test" []
Just "test12hrzfj"
> bech32Encode "TEST" []
Just "test12hrzfj"
> bech32Encode "test" [] == bech32Encode "TEST" [] 
True

Actual Behaviour

The above calls to bech32Encode actually result in different output strings:

> bech32Encode "test" []
Just "test12hrzfj"
> bech32Encode "TEST" []
Just "test13jgcyw"
> bech32Encode "test" [] == bech32Encode "TEST" [] 
False

Attempting to decode the string produced by bech32Encode "TEST" [] results in Nothing:

> bech32Decode "test13jgcyw"
Nothing
@jonathanknowles
Copy link
Author

jonathanknowles commented May 27, 2019

This bug is also present in the Haskell reference implementation for Bech32, for which a fix is available.

@jprupp
Copy link
Member

jprupp commented Jun 24, 2019

Thank you for this report. I'll fix it for the next release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants