Bech32 (bip0173 v0.2.0)

Encode and decode the Bech32 and Bech32m format, with checksums.

Link to this section Summary

Types

Base32 code point

Encoding type

Functions

Decode a Bech32/Bech32m string.

Encode a Bech32/Bech32m string.

Link to this section Types

Link to this type

code_point_t()

Specs

code_point_t() :: 0..31

Base32 code point

Link to this type

encoding_t()

Specs

encoding_t() :: :bech32 | :bech32m

Encoding type

:bech32 defined in BIP0173 :bech32m defined in BIP0350

Link to this section Functions

Specs

decode(String.t()) ::
  {:ok, {String.t(), [code_point_t()], encoding_t()}} | {:error, String.t()}

Decode a Bech32/Bech32m string.

Examples

iex> Bech32.decode("bech321qpz4nc4pe")
{:ok, {"bech32", [0, 1, 2], :bech32}}

iex> Bech32.decode("bech321qpzq0geym")
{:ok, {"bech32", [0, 1, 2], :bech32m}}

iex> Bech32.decode("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4")
{:ok, {"bc", [0, 14, 20, 15, 7, 13, 26, 0, 25, 18, 6, 11, 13, 8, 21,
  4, 20, 3, 17, 2, 29, 3, 12, 29, 3, 4, 15, 24, 20, 6, 14, 30, 22], :bech32}}
Link to this function

encode(hrp, data, encoding)

Specs

encode(String.t(), [code_point_t()], encoding_t()) :: String.t()
encode(String.t(), String.t(), encoding_t()) :: String.t()

Encode a Bech32/Bech32m string.

Examples

iex> Bech32.encode("bech32", [0, 1, 2], :bech32)
"bech321qpz4nc4pe"

iex> Bech32.encode("bech32", [0, 1, 2], :bech32m)
"bech321qpzq0geym"

iex> Bech32.encode("bc", [0, 14, 20, 15, 7, 13, 26, 0, 25, 18, 6, 11, 13,
...> 8, 21, 4, 20, 3, 17, 2, 29, 3, 12, 29, 3, 4, 15, 24, 20, 6, 14, 30, 22], :bech32)
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"