bech32

Pure Haskell bech32, bech32m encodings (docs.ppad.tech/bech32).
git clone git://git.ppad.tech/bech32.git
Log | Files | Refs | README | LICENSE

Main.hs (890B)


      1 module Main where
      2 
      3 import qualified Data.ByteString as BS
      4 import qualified Data.ByteString.Bech32 as Bech32
      5 import Test.Tasty
      6 import qualified Test.Tasty.QuickCheck as Q
      7 import qualified Reference.Bech32 as R
      8 
      9 data Input = Input BS.ByteString BS.ByteString
     10   deriving (Eq, Show)
     11 
     12 instance Q.Arbitrary Input where
     13   arbitrary = do
     14     h <- hrp
     15     b <- bytes (83 - BS.length h)
     16     pure (Input h b)
     17 
     18 hrp :: Q.Gen BS.ByteString
     19 hrp = do
     20   l <- Q.chooseInt (1, 83)
     21   v <- Q.vectorOf l (Q.choose (33, 126))
     22   pure (BS.pack v)
     23 
     24 bytes :: Int -> Q.Gen BS.ByteString
     25 bytes k = do
     26   l <- Q.chooseInt (0, k)
     27   v <- Q.vectorOf l Q.arbitrary
     28   pure (BS.pack v)
     29 
     30 matches :: Input -> Bool
     31 matches (Input h b) =
     32   let ref = R.bech32Encode h (R.toBase32 (BS.unpack b))
     33       our = Bech32.encode h b
     34   in  ref == our
     35 
     36 main :: IO ()
     37 main = defaultMain $
     38   Q.testProperty "encoding matches reference" matches