This repository was archived by the owner on May 27, 2019. It is now read-only.
forked from sipa/bech32
-
Notifications
You must be signed in to change notification settings - Fork 0
ref/haskell: refactor error handling for bech32Decode #1
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Haskell stack build artifacts | ||
| .stack-work/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| module Codec.Binary.Bech32 | ||
| ( bech32Encode | ||
| ( DecodeError(..) | ||
|
|
||
| , bech32Encode | ||
| , bech32Decode | ||
| , toBase32 | ||
| , toBase256 | ||
|
|
@@ -10,15 +12,15 @@ module Codec.Binary.Bech32 | |
| , fromWord5 | ||
| ) where | ||
|
|
||
| import Control.Monad (guard) | ||
| import Control.Monad (guard, when) | ||
| import qualified Data.Array as Arr | ||
| import Data.Bits (Bits, unsafeShiftL, unsafeShiftR, (.&.), (.|.), xor, testBit) | ||
| import Data.Bits (Bits, testBit, unsafeShiftL, unsafeShiftR, xor, (.&.), (.|.)) | ||
| import qualified Data.ByteString as BS | ||
| import qualified Data.ByteString.Char8 as BSC | ||
| import Data.Char (toLower, toUpper) | ||
| import Data.Foldable (foldl') | ||
| import Data.Functor.Identity (Identity, runIdentity) | ||
| import Data.Ix (Ix(..)) | ||
| import Data.Ix (Ix (..)) | ||
| import Data.Word (Word8) | ||
|
|
||
| type HRP = BS.ByteString | ||
|
|
@@ -29,7 +31,7 @@ type Data = [Word8] | |
| (.<<.) = unsafeShiftL | ||
|
|
||
| newtype Word5 = UnsafeWord5 Word8 | ||
| deriving (Eq, Ord) | ||
| deriving (Eq, Ord, Show) | ||
|
|
||
| instance Ix Word5 where | ||
| range (UnsafeWord5 m, UnsafeWord5 n) = map UnsafeWord5 $ range (m, n) | ||
|
|
@@ -79,32 +81,43 @@ bech32VerifyChecksum hrp dat = bech32Polymod (bech32HRPExpand hrp ++ dat) == 1 | |
|
|
||
| bech32Encode :: HRP -> [Word5] -> Maybe BS.ByteString | ||
| bech32Encode hrp dat = do | ||
| guard $ checkHRP hrp | ||
| guard $ validHRP hrp | ||
| let dat' = dat ++ bech32CreateChecksum hrp dat | ||
| rest = map (charset Arr.!) dat' | ||
| result = BSC.concat [BSC.map toLower hrp, BSC.pack "1", BSC.pack rest] | ||
| guard $ BS.length result <= 90 | ||
| return result | ||
|
|
||
| checkHRP :: BS.ByteString -> Bool | ||
| checkHRP hrp = not (BS.null hrp) && BS.all (\char -> char >= 33 && char <= 126) hrp | ||
| validHRP :: BS.ByteString -> Bool | ||
| validHRP hrp = not (BS.null hrp) && BS.all (\char -> char >= 33 && char <= 126) hrp | ||
|
|
||
| data DecodeError = | ||
| Bech32StringLengthExceeded | ||
| | CaseInconsistency | ||
| | TooShortDataPart | ||
| | InvalidHRP | ||
| | ChecksumVerificationFail | ||
| | InvalidCharsetMap | ||
| deriving (Show, Eq) | ||
|
|
||
| bech32Decode :: BS.ByteString -> Maybe (HRP, [Word5]) | ||
| bech32Decode :: BS.ByteString -> Either DecodeError (HRP, [Word5]) | ||
| 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) | ||
| when (not $ validCase bech32) (Left CaseInconsistency) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
| let (hrp, dat) = BSC.breakEnd (== '1') $ BSC.map toLower bech32 | ||
| guard $ BS.length dat >= 6 | ||
| hrp' <- BSC.stripSuffix (BSC.pack "1") hrp | ||
| guard $ checkHRP hrp' | ||
| dat' <- mapM charsetMap $ BSC.unpack dat | ||
| guard $ bech32VerifyChecksum hrp' dat' | ||
| 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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you implemented locally |
||
| when (not $ bech32VerifyChecksum hrp' dat') (Left ChecksumVerificationFail) | ||
| return (hrp', take (BS.length dat - 6) dat') | ||
| where | ||
| validCase b32 = BSC.map toUpper b32 == b32 || BSC.map toLower b32 == b32 | ||
|
|
||
| type Pad f = Int -> Int -> Word -> [[Word]] -> f [[Word]] | ||
|
|
||
| yesPadding :: Pad Identity | ||
| yesPadding _ 0 _ result = return result | ||
| yesPadding _ 0 _ result = return result | ||
| yesPadding _ _ padValue result = return $ [padValue] : result | ||
| {-# INLINE yesPadding #-} | ||
|
|
||
|
|
@@ -146,7 +159,7 @@ segwitCheck witver witprog = | |
|
|
||
| segwitDecode :: HRP -> BS.ByteString -> Maybe (Word8, Data) | ||
| segwitDecode hrp addr = do | ||
| (hrp', dat) <- bech32Decode addr | ||
| (hrp', dat) <- rightToMaybe $ bech32Decode addr | ||
| guard $ (hrp == hrp') && not (null dat) | ||
| let (UnsafeWord5 witver : datBase32) = dat | ||
| decoded <- toBase256 datBase32 | ||
|
|
@@ -157,3 +170,6 @@ segwitEncode :: HRP -> Word8 -> Data -> Maybe BS.ByteString | |
| segwitEncode hrp witver witprog = do | ||
| guard $ segwitCheck witver witprog | ||
| bech32Encode hrp $ UnsafeWord5 witver : toBase32 witprog | ||
|
|
||
| rightToMaybe :: Either l r -> Maybe r | ||
| rightToMaybe = either (const Nothing) Just | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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:
It's like
guardbut forEither.