Bech32.hs (1850B)
1 {-# OPTIONS_HADDOCK prune #-} 2 3 -- | 4 -- Module: Data.ByteString.Bech32 5 -- Copyright: (c) 2024 Jared Tobin 6 -- License: MIT 7 -- Maintainer: Jared Tobin <jared@ppad.tech> 8 -- 9 -- The 10 -- [BIP0173](https://github.com/bitcoin/bips/blob/master/bip-0173.mediawiki) 11 -- bech32 checksummed base32 encoding, with decoding and checksum verification. 12 13 module Data.ByteString.Bech32 ( 14 -- * Encoding and Decoding 15 encode 16 , decode 17 18 -- * Checksum 19 , verify 20 ) where 21 22 import qualified Data.ByteString as BS 23 import qualified Data.ByteString.Bech32.Internal as BI 24 25 -- | Encode a base256 human-readable part and input as bech32. 26 -- 27 -- >>> let Just bech32 = encode "bc" "my string" 28 -- >>> bech32 29 -- "bc1d4ujqum5wf5kuecmu02w2" 30 encode 31 :: BS.ByteString -- ^ base256-encoded human-readable part 32 -> BS.ByteString -- ^ base256-encoded data part 33 -> Maybe BS.ByteString -- ^ bech32-encoded bytestring 34 encode = BI.encode BI.Bech32 35 36 -- | Decode a bech32-encoded 'ByteString' into its human-readable and data 37 -- parts. 38 -- 39 -- All-uppercase input is accepted, and its human-readable part is 40 -- returned in lowercase. Mixed-case input produces 'Nothing'. 41 -- 42 -- >>> decode "hi1df6x7cnfdcs8wctnyp5x2un9wed5st" 43 -- Just ("hi","jtobin was here") 44 -- >>> decode "hey1df6x7cnfdcs8wctnyp5x2un9wed5st" -- s/hi/hey 45 -- Nothing 46 decode 47 :: BS.ByteString -- ^ bech32-encoded bytestring 48 -> Maybe (BS.ByteString, BS.ByteString) -- ^ (hrp, data less checksum) 49 decode = BI.decode BI.Bech32 50 51 -- | Verify that a bech32 string has a valid checksum. 52 -- 53 -- All-uppercase input is accepted; mixed-case input is not. 54 -- 55 -- >>> verify "bc1d4ujqum5wf5kuecmu02w2" 56 -- True 57 -- >>> verify "bc1d4ujquw5wf5kuecmu02w2" -- s/m/w 58 -- False 59 verify 60 :: BS.ByteString -- ^ bech32-encoded bytestring 61 -> Bool 62 verify = BI.verify BI.Bech32 63