HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Modulebech32-1.1.7Haskell2010

Codec.Binary.Bech32

Implementation of the Bech32 address format.

Based on an original implementation by Marko Bencun.

  • 7 types
  • 20 values
  • Packagebech32-1.1.7
  • Exports27
  • LanguageHaskell2010
  • LicenceApache-2.0
  • SourceBech32.hs

Basic Usage

0 declarations

Encoding

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"

Decoding

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!"

Error Handling

0 declarations

Encoding

Decoding

datadata DecodingError
#

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

Constructors

Instances2Eq, Show

Core Types

0 declarations

Data Payloads

newtypenewtype DataPart
#

Represents the data part of a Bech32 string, as defined here: https://git.io/fj8FS

Instances4Eq, Show, Semigroup, Monoid
  • Eq DataPartDefined in bech32-1.1.7 · Codec.Binary.Bech32.Internal
  • Show DataPartDefined in bech32-1.1.7 · Codec.Binary.Bech32.Internal
  • Semigroup DataPartDefined in bech32-1.1.7 · Codec.Binary.Bech32.Internal
  • Monoid DataPartDefined in bech32-1.1.7 · Codec.Binary.Bech32.Internal

Conversion to and from Words

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
valuedataPartToWords :: DataPart -> [Word5]
#

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

Conversion to and from Bytes

Conversion to and from Text

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
valuedataPartToText :: DataPart -> Text
#

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

Human-Readable Prefixes

Conversion to and from Text

Error Handling

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

Constructors

Instances3Eq, Show, Exception

Additional Types

0 declarations

Character Positions

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

Data Words

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.7 · Codec.Binary.Bech32.Internal
  • Enum Word5Defined in bech32-1.1.7 · Codec.Binary.Bech32.Internal
  • Eq Word5Defined in bech32-1.1.7 · Codec.Binary.Bech32.Internal
  • Ord Word5Defined in bech32-1.1.7 · Codec.Binary.Bech32.Internal
  • Show Word5Defined in bech32-1.1.7 · Codec.Binary.Bech32.Internal
  • Ix Word5Defined in bech32-1.1.7 · Codec.Binary.Bech32.Internal

Advanced Usage

0 declarations

Encoding and Decoding with Greater Leniency

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)."

Fundamental Constants

0 declarations

Data Payloads

Human-Readable Prefixes

The lower bound of the set of characters permitted to appear within the human-readable part of a Bech32 string.

The upper bound of the set of characters permitted to appear within the human-readable part of a Bech32 string.

Encoded Strings

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.

valueseparatorChar :: Char
#

The separator character.

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