Skip to content
This repository has been archived by the owner on May 27, 2019. It is now read-only.

ref/haskell: refactor error handling for bech32Decode #1

Merged
merged 4 commits into from
Mar 24, 2018

Conversation

manifoldhiker
Copy link

The main reason to refactor was the Maybe in a signature. It is not clear why the given bech32 string is invalid.

Reopen of sipa#41

bech32Decode bech32 = do
guard $ BS.length bech32 <= 90
guard $ BSC.map toUpper bech32 == bech32 || BSC.map toLower bech32 == bech32
when (BS.length bech32 > 90) (Left Bech32StringLengthExceeded)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since you use this pattern a lot I would recomment implementing local function like:

verify :: Bool -> a -> Either a b

It's like guard but for Either.

guard $ BS.length bech32 <= 90
guard $ BSC.map toUpper bech32 == bech32 || BSC.map toLower bech32 == bech32
when (BS.length bech32 > 90) (Left Bech32StringLengthExceeded)
when (not $ validCase bech32) (Left CaseInconsistency)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

when (not x) is unless x

when (BS.length dat < 6) (Left TooShortDataPart)
hrp' <- maybe (Left InvalidHRP) Right $ BSC.stripSuffix (BSC.pack "1") hrp
when (not $ validHRP hrp') (Left InvalidHRP)
dat' <- maybe (Left InvalidCharsetMap) Right $ mapM charsetMap $ BSC.unpack dat

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@manifoldhiker
Copy link
Author

@kirelagin please, review this. I am not quite sure about that, especially about naming

@manifoldhiker manifoldhiker merged commit c1fa93b into master Mar 24, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants