HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

  • Packagebech32-1.1.11
  • Exports41
  • LanguageHaskell2010
  • LicenceApache-2.0
  • SourceInternal.hs

Encoding & Decoding

Encode a Bech32 string from a human-readable prefix and data payload.

Example

Example3 expressions
import Preludeimport Codec.Binary.Bech32import Data.Text.Encoding

First, prepare a human-readable prefix:

Example1 expression
Right prefix = humanReadablePartFromText "example"

Next, prepare a data payload:

Example2 expressions
messageToEncode = "Lorem ipsum dolor sit amet!"dataPart = dataPartFromBytes $ encodeUtf8 messageToEncode

Finally, produce a Bech32 string:

Example1 expression
encode prefix dataPartRight "example1f3hhyetdyp5hqum4d5sxgmmvdaezqumfwssxzmt9wsss9un3cx"

Like encode but allows output to be longer than 90 characters.

This isn't ideal, as Bech32 error detection becomes worse as strings get longer, but it may be useful in certain circumstances.

From BIP-0173:

"Even though the chosen code performs reasonably well up to 1023 characters, other designs are preferable for lengths above 89 characters (excluding the separator)."

Decode a Bech32 string into a human-readable prefix and data payload.

Example

Example3 expressions
import Preludeimport Codec.Binary.Bech32import Data.Text.Encoding

First, decode the input:

Example2 expressions
input = "example1f3hhyetdyp5hqum4d5sxgmmvdaezqumfwssxzmt9wsss9un3cx"Right (prefix, dataPart) = decode input

Next, examine the decoded human-readable prefix:

Example1 expression
humanReadablePartToText prefix"example"

Finally, examine the decoded data payload:

Example1 expression
decodeUtf8 <$> dataPartToBytes dataPartJust "Lorem ipsum dolor sit amet!"
datadata DecodingError

Represents the set of errors that may occur while decoding a Bech32 string with the decode function.

Constructors

Instances2Eq, Show

The maximum length of an encoded string, in bytes.

This length includes the human-readable part, the separator character, the encoded data portion, and the checksum.

The minimum length of an encoded string, in bytes.

This length includes the human-readable part, the separator character, the encoded data portion, and the checksum.

The separator character.

This character appears immediately after the human-readable part and before the data part in an encoded string.

Data Part

Constructs a DataPart from textual input.

All characters in the input must be a member of dataCharList, the set of characters permitted to appear within the data part of a Bech32 string.

Returns Nothing if any character in the input is not a member of dataCharList.

This function guarantees to satisfy the following property:

dataPartFromText (dataPartToText d) == Just d

Construct a DataPart directly from a list of words.

This function guarantees to satisfy the following properties:

dataPartFromWords (dataPartToWords d) == d
dataPartToWords (dataPartFromWords w) == w

Converts a DataPart to Text, using the Bech32 character set to render the data.

This function guarantees to satisfy the following property:

dataPartFromText (dataPartToText d) == Just d

Unpack a DataPart into a list of its constituent words.

This function guarantees to satisfy the following properties:

dataPartFromWords (dataPartToWords d) == d
dataPartToWords (dataPartFromWords w) == w

If the specified character is permitted to appear within the data part of a Bech32 string, this function returns that character's corresponding Word5 value. If the specified character is not permitted, or if the specified character is upper-case, returns Nothing.

Human-Readable Part

Represents the set of error conditions that may occur while parsing the human-readable part of a Bech32 string.

Constructors

Instances3Eq, Show, Exception

Bit Manipulation

valueconvertBits :: Functor f => [Word] -> Int -> Int -> Pad f -> f [Word]

Big-endian conversion of a word string from base '2^frombits' to base '2^tobits'. The frombits and twobits parameters must be positive, while '2^frombits' and '2^tobits' must be smaller than the size of Word. Every value in dat must be strictly smaller than '2^frombits'.

newtypenewtype Word5

Represents a data word of 5 bits in width.

Each character in the data portion of a Bech32 string encodes exactly 5 bits of data.

Construction and Deconstruction

Use the toEnum and fromEnum functions to construct and deconstruct Word5 values.

Packing Words into Data Payloads

Use the dataPartFromWords and dataPartToWords functions to pack and unpack Word5 values into and out of data payloads.

Instances6Bounded, Enum, Eq, Ord, Show, Ix
  • Bounded Word5Defined in bech32-1.1.11 · Codec.Binary.Bech32.Internal
  • Enum Word5Defined in bech32-1.1.11 · Codec.Binary.Bech32.Internal
  • Eq Word5Defined in bech32-1.1.11 · Codec.Binary.Bech32.Internal
  • Ord Word5Defined in bech32-1.1.11 · Codec.Binary.Bech32.Internal
  • Show Word5Defined in bech32-1.1.11 · Codec.Binary.Bech32.Internal
  • Ix Word5Defined in bech32-1.1.11 · Codec.Binary.Bech32.Internal

Character Manipulation

newtypenewtype CharPosition

The zero-based position of a character in a string, counting from the left.

Values of this type are typically used to reflect the positions of errors.

See DecodingError.

Constructors

Instances3Eq, Ord, Show