BIP-0173 
Elixir implementation of Bitcoin's address format for native SegWit and Taproot outputs.
Upstream GitHub repository: lukaszsamson/elixir-bip0173
Differences from stampery/elixir-bip0173
This fork adds support for BIP-0350 Bech32m Taproot address format and fixes validation issues in SegWit addresses basing on testcases from BIP-0173. It also introduces support for Litecoin MimbleWimble addresses (witness 0, length 66)
About BIP-0173, BIP-0350 and Bech32 and Bech32m
BIP-0173 proposes a checksummed base32 format, "Bech32", and a standard for native segregated witness output addresses using it.
BIP-0141 defines Segregated Witness v0 and witness programs.
BIP-0350 Bech32m format for v1+ witness addresses.
You can find more information in the original proposal by @sipa and @gmaxwell.
Installation
-
Add
bip0173to your list of dependencies inmix.exs:
def deps do
[{:bip0173, "~> 0.2.0", hex: :bip0173_0350}]
endHow to use
You can find the full API reference and examples in the online documentation at Hexdocs.
Bech32
Encoding data to Bech32 and Bech32m string
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"Decoding data from Bech32 and Bech32m string
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}}SegwitAddr
Encoding a SegWit program into BIP-0350 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-0350 Taproot address into a Tapscript 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 testRunning 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 dialyzerCredits
This project was forked from stampery/elixir-bip0173 and original code is copyrighted to Adán Sánchez de Pedro Crespo.