All Projects → input-output-hk → bech32

input-output-hk / bech32

Licence: other
Haskell implementation of the Bech32 address format (BIP 0173).

Programming Languages

haskell
3896 projects
Nix
1067 projects

bech32 command-line

A small executable is available for rapid conversion in the console. You can obtain a pre-compiled bech32 binary for Linux, macOS, and Windows as part of the cardano-wallet Releases.

Usage: bech32 [PREFIX]
  Convert to and from bech32 strings. Data are read from standard input.

Available options:
  -h,--help                Show this help text
  PREFIX                   An optional human-readable prefix (e.g. 'addr').
                             - When provided, the input text is decoded from
                               various encoding formats and re-encoded to
                               bech32 using the given prefix.
                             - When omitted, the input text is decoded from
                               bech32 to base16.
  -v,--version             output version information and exit

Supported encoding formats: Base16, Bech32 & Base58.

Examples

To Bech32:

$ bech32 base16_ <<< 706174617465
base16_1wpshgct5v5r5mxh0

$ bech32 base58_ <<< Ae2tdPwUPEYy
base58_1p58rejhd9592uusa8pzj2

$ bech32 new_prefix <<< old_prefix1wpshgcg2s33x3
new_prefix1wpshgcgeak9mv

From Bech32

$ bech32 <<< base16_1wpshgct5v5r5mxh0
706174617465

Bech32 Haskell Libraries

The repository provides Haskell libraries for working with the Bech32 address format, as specified by BIP-0173.

There are two libraries provided:

  • bech32

    Core primitives for encoding and decoding Bech32 strings.

  • bech32-th

    Template-Haskell specific extensions, including quasiquoters for compile-time parsing of string literals.

Contents

Documentation

For comprehensive instructions on how to use these libraries, see the Haddock documentation:

Usage

Encoding Data

>>> import Prelude
>>> import Codec.Binary.Bech32
>>> import Data.Text.Encoding

First, prepare a human-readable prefix:

>>> Right prefix = humanReadablePartFromText "example"

Next, prepare a data payload:

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

Finally, produce a Bech32 string:

>>> encode prefix dataPart
Right "example1f3hhyetdyp5hqum4d5sxgmmvdaezqumfwssxzmt9wsss9un3cx"

Decoding Data

>>> import Prelude
>>> import Codec.Binary.Bech32
>>> import Data.Text.Encoding

First, decode the input:

>>> input = "example1f3hhyetdyp5hqum4d5sxgmmvdaezqumfwssxzmt9wsss9un3cx"
>>> Right (prefix, dataPart) = decode input

Next, examine the decoded human-readable prefix:

>>> humanReadablePartToText prefix
"example"

Finally, examine the decoded data payload:

>>> decodeUtf8 <$> dataPartToBytes dataPart
Just "Lorem ipsum dolor sit amet!"

Contributing

If you find a bug or you'd like to propose a feature, please feel free to raise an issue on our issue tracker.

Pull requests are welcome!

When creating a pull request, please make sure that your code adheres to our coding standards.


Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].