BitcoinElixir

Bitcoin Cash for Elixir.

No native dependencies: everything runs on Erlang's :crypto and :ssl. Tested against libauth vectors.

Install

def deps do
  [{:bitcoin_elixir, "~> 0.1"}]
end

Examples

alias BitcoinElixir.{CashAddr, HD, Script, PaymentURI, Electrum}

# A fresh receive address for invoice 42, from the shop's account xpub
{:ok, account} = HD.parse("xpub6C…")
address = HD.address(account, [0, 42])
token_address = HD.address(account, [0, 42], tokens: true)

# Addresses
{:ok, %CashAddr{type: :p2pkh, hash: hash}} = CashAddr.decode(address)
{:ok, z} = CashAddr.to_token_aware(address)

# A payment link for 0.0015 BCH (amounts are whole satoshis)
PaymentURI.build(address, amount: 150_000, label: "Order 42")

# Look the address up on Electrum servers
servers = Electrum.default_servers(:mainnet)
{:ok, utxos} =
  Electrum.request("blockchain.scripthash.listunspent", [Script.scripthash!(address)], servers: servers)

# Or watch it live
{:ok, conn} = Electrum.Connection.start_link(servers: servers)
{:ok, _status} = Electrum.Connection.subscribe_scripthash(conn, Script.scripthash!(address))

receive do
  {:electrum, ^conn, {:scripthash, _scripthash, _new_status}} -> :check_the_address_again
end

Put Electrum.Connection in your supervision tree with a :name to share one connection across your app. Public servers are fine to start with; run your own Fulcrum for production.

Status

0.1 is watch-only: addresses, derivation, lookups and payment links. It does not build or sign transactions. Planned next: a supervised payment watcher (seen, confirmed, double-spend events), CashTokens data in outputs, and transaction decoding.

Development

mix deps.get
mix test
mix docs

Test vectors in test/fixtures were generated with libauth 3.0.

Licence

MIT. Built by Septem Labs for Cashlet and Enkaji.