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

Convert HRP to lower-case before calculating checksum within bech32Encode #50

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions ref/haskell/src/Codec/Binary/Bech32.hs
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,10 @@ bech32VerifyChecksum hrp dat = bech32Polymod (bech32HRPExpand hrp ++ dat) == 1
bech32Encode :: HRP -> [Word5] -> Maybe BS.ByteString
bech32Encode hrp dat = do
guard $ checkHRP hrp
let dat' = dat ++ bech32CreateChecksum hrp dat
let hrpLower = BSC.map toLower hrp
dat' = dat ++ bech32CreateChecksum hrpLower dat
rest = map (charset Arr.!) dat'
result = BSC.concat [BSC.map toLower hrp, BSC.pack "1", BSC.pack rest]
result = BSC.concat [hrpLower, BSC.pack "1", BSC.pack rest]
guard $ BS.length result <= 90
return result

Expand Down
2 changes: 1 addition & 1 deletion ref/haskell/test/Spec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,6 @@ tests = testGroup "Tests"
assertBool "empty HRP encode" $ isNothing $ bech32Encode (BSC.pack "") []
assertBool "empty HRP decode" $ isNothing $ bech32Decode (BSC.pack "10a06t8")
assertEqual "hrp lowercased"
(Just $ BSC.pack "hrp1g9xj8m")
(Just $ BSC.pack "hrp1vhqs52")
(bech32Encode (BSC.pack "HRP") [])
]