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