ExBech32 (ExBech32 v0.6.3)

Nif for Bech32 format encoding and decoding.

It uses https://github.com/rust-bitcoin/rust-bech32 rust library

Summary

Functions

Decodes bech32 decoded string with witness version

Encodes into Bech32 format appending the version to data

Functions

decode_with_version(encoded)

@spec decode_with_version(String.t()) ::
  {:ok, {String.t(), non_neg_integer(), binary()}} | {:error, atom()}

Decodes bech32 decoded string with witness version

Examples

iex> ExBech32.decode_with_version("bc1qnv5fzufzf3l4uj9ey95w6zw32nevwjxn9497vk")
{:ok, {"bc", 0, <<155, 40, 145, 113, 34, 76, 127, 94, 72, 185, 33, 104, 237, 9, 209, 84, 242, 199, 72, 211>>}}

encode_with_version(hrp, version, data)

@spec encode_with_version(String.t(), non_neg_integer(), binary()) ::
  {:ok, String.t()} | {:error, atom()}

Encodes into Bech32 format appending the version to data

It accepts the following three paramters:

  • human-readable part
  • witness version
  • data to be encoded
  • bech32 variant. it can be :bech32 (BIP-0173) or :bech32m (BIP-0350)

Examples

iex> hash = <<155, 40, 145, 113, 34, 76, 127, 94, 72, 185, 33, 104, 237, 9, 209, 84, 242, 199, 72, 211>>
iex> ExBech32.encode_with_version("bc", 0, hash)
{:ok, "bc1qnv5fzufzf3l4uj9ey95w6zw32nevwjxn9497vk"}

iex> ExBech32.encode_with_version("bc", 0, <<1>>)
{:error, :encode_error}