Safe Haskell | None |
---|---|
Language | Haskell2010 |
This package provides an implementation of the bech32 encoding. In essence, this encoding is comprised of three parts:
- A human readable prefix
- A base32-encoded payload (with a modified alphabet to avoid similar letters)
- A checksum calculated on the prefix and the encoded payload
The implementation is optimized on mainly three aspects:
- Low-level memory manipulation with typed pointers is used to traverse the input string and to write the encoded value. Each byte is encoded on-the-fly, which requires keeping track of a residue (since we encode from 8 bit to 5 bit, but can only read 8 bit at the same, the remainder is stored and passed to the next builder loop).
- Since the checksum is calculated by folding over the encoded bytestring, it can be computed in-place, as the string is being encoded. This is exactly what this implementation does as well. In such way that once the string is encoded, the checksum is already calculated and only need to be base32 encoded in turn.
- The first part of the checksum is pre-computed during the construction of the
HRP. It is indeed quite common for a single HRP to encode many payload. To
calculate the checksum, one must first expand the HRP which is in itself an
already
costly
operation. Pre-calculating the expansion and the beginning of the checksum shaves off some time .
Synopsis
- encodeBech32 :: HumanReadablePart -> ByteString -> Text
- data HumanReadablePart where
- pattern HumanReadablePart :: Text -> HumanReadablePart
- unHumanReadablePart :: HumanReadablePart -> Text
Encoding
encodeBech32 :: HumanReadablePart -> ByteString -> Text Source #
Encode some binary data to bech32 using the given human readable prefix.
HumanReadablePart
data HumanReadablePart where Source #
pattern HumanReadablePart :: Text -> HumanReadablePart | Construct a human readable part from a text string, and pre-calculate the checksum corresponding to it. |
Instances
Show HumanReadablePart Source # | |
Defined in Data.ByteString.Bech32 showsPrec :: Int -> HumanReadablePart -> ShowS # show :: HumanReadablePart -> String # showList :: [HumanReadablePart] -> ShowS # |