Anonymous user
Bitcoin/address validation: Difference between revisions
→{{header|Haskell}}
Line 895:
</pre>
=={{header|Haskell}}==
<lang haskell>import
import Data.
import
import qualified Data.ByteString
import Data.ByteString (ByteString)
import Crypto.Hash.SHA256 (hash) -- from package cryptohash
-- Convert from base58 encoded value to Integer
decode58 :: String -> Maybe Integer
decode58 = fmap combine . traverse parseDigit
where
where c58 = "123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"▼
combine = foldl (\acc digit -> 58 * acc + digit) 0 -- should be foldl', but this trips up the highlighting
parseDigit char = toInteger <$> elemIndex char c58
toBytes = BS.reverse . BS.pack . map (fromIntegral . (`mod` 256)) . takeWhile (> 0) . iterate (`div` 256)
-- Check if the hash of the first 21 (padded) bytes matches the last 4 bytes
checksumValid :: ByteString -> Bool
checksumValid address =
let (value, checksum) = BS.splitAt 21 $ leftPad address
in and $ BS.zipWith (==) checksum $ hash $ hash $ value
where
leftPad bs = BS.replicate (25 - BS.length bs) 0 <> bs
-- utility
▲-- Convert from base58 encoded value to list of bytes
withError :: e -> Maybe a -> Either e a
▲toBytes :: Integer -> [Word8]
withError e = maybe (Left e) Right
-- Check validity of base58 encoded bitcoin address.
-- Result is either an error string (Left) or
validityCheck :: String -> Either String
validityCheck
num <- withError "Invalid base 58 encoding" $ decode58 encoded
when (BS.length address > 25)
when (not $ checksumValid address) $ Left "Invalid checksum"
▲ let address = toBytes ev
▲ then Left "Address length less than 4 bytes"
-- Run one validity check and display results.
validate :: String -> IO ()
validate encodedAddress = do
let
▲ putStrLn $ show encodedAddress ++ " -> " ++ show err
-- Run some validity check tests.
Line 947 ⟶ 952:
validate "1A Na15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" -- invalid chars
validate "1ANa55215ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" -- too long
validate "i55j" -- too short
</lang>
{{out}}
<pre style="font-size:80%">"1AGNa15ZQXAZUgFiqJ2i7Z2DPU2J6hW62i" -> Valid
|