HORIZON HASKELLDocslts/ghc-9.10.x248f8f02026-10-05Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · 248f8f0 · 2026-10-05

Modulebech32-1.1.7Haskell2010

Codec.Binary.Bech32.Internal

Implementation of the Bech32 address format.

From an original implementation by Marko Bencun:

sipa/bech32

  • 7 types
  • 34 values
  • Packagebech32-1.1.7
  • Exports41
  • LanguageHaskell2010
  • LicenceApache-2.0
  • SourceInternal.hs

Encoding & Decoding

11 declarations

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
valuechecksumLength :: Int
#

The length of the checksum portion of an encoded string, in bytes.

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.

valueseparatorLength :: Int
#

The length of the separator portion of an encoded string, in bytes.

Data Part

11 declarations
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

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

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

10 declarations

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

Constructors

Instances3Eq, Show, Exception

Returns true iff. the specified character is permitted to appear within the human-readable part of a Bech32 string.

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.

Bit Manipulation

8 declarations
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.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

Character Manipulation

1 declaration
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