Skip to content
Elixir implementation of Bitcoin's address format for native SegWit outputs.
Elixir
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
lib
test
.gitignore
.travis.yml
README.md
mix.exs

README.md

BIP-0173 Build Status

Elixir implementation of Bitcoin's address format for native SegWit outputs.

Upstream GitHub repository: stampery/elixir-bip0173

About BIP-0173 and Bech32

BIP-0173 proposes a checksummed base32 format, "Bech32", and a standard for native segregated witness output addresses using it.

You can find more information in the original proposal by @sipa and @gmaxwell.

Installation

  1. Add bip0173 to your list of dependencies in mix.exs:
  def deps do
    [{:bip0173, "~> 0.1.2"}]
  end

How to use

You can find the full API reference and examples in the online documentation at Hexdocs.

Bech32

Encoding data to Bech32 string

iex> Bech32.encode("bech32", [0, 1, 2])
"bech321qpz4nc4pe"
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])
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"

Decoding data from Bech32 string

iex> Bech32.decode("bech321qpz4nc4pe")
{:ok, {"bech32", [0, 1, 2]}}
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]}}

SegwitAddr

Encoding a SegWit program into BIP-0173 format

iex> SegwitAddr.encode("bc", "0014751e76e8199196d454941c45d1b3a323f1433bd6")
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"
iex> SegwitAddr.encode("bc", 0, [117, 30, 118, 232, 25, 145, 150, 212,
...> 84, 148, 28, 69, 209, 179, 163, 35, 241, 67, 59, 214])
"bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4"

Decoding a BIP-0173 address into a SegWit program and formatting it as an hexadecimal ScriptPubKey

iex> SegwitAddr.decode("bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t4")
{:ok, {"bc", 0, [117, 30, 118, 232, 25, 145, 150, 212,
84, 148, 28, 69, 209, 179, 163, 35, 241, 67, 59, 214]}}
iex> SegwitAddr.to_script_pub_key(0, [117, 30, 118, 232, 25, 145, 150,
...> 212, 84, 148, 28, 69, 209, 179, 163, 35, 241, 67, 59, 214])
"0014751e76e8199196d454941c45d1b3a323f1433bd6"

Development

Running tests

$ mix deps.get
$ mix test

Running static analysis

This package uses Erlang's dialyzer to find software discrepancies such as definite type errors, code which has become dead or unreachable due to some programming error, unnecessary tests, etc.

$ mix deps.get
$ mix dialyzer
You can’t perform that action at this time.