paulfioravanti/bitcoin_address
Demos for generating Bitcoin addresses from private keys
Bitcoin Address
This is a demo repository that presents implementations in Elixir for
generating a Bitcoin address from a private key.
The following implementations are currently supplied:
- Pure Elixir, using
bitcoin-elixirfor converting public keys into
Bitcoin addresses. - Python, using Export to interface with
pybitcointoolslibraries. - C++, using Cure to interface with
libbitcoinlibraries.
Installation
The following instructions will focus on installation for MacOS.
Dependencies
Use Homebrew to install Bitcoin packages needed for non-Elixir code:
libbitcoin: Various Bitcoin-related helper functions.gcc: Needed to compile the C++ files into thepriv/directorypython: Needed to run Python files in thepriv/directory.
Install either via brew or via a version control manager like asdf.
Install brew packages (includes the C++ libraries):
brew install bitcoin gcc [python]Since Pybitcointools is not maintained any more, in order to get things working,
the library has to be installed using pip at the specific commit hash before
the entire repository was deleted:
pip install git+https://github.com/vbuterin/pybitcointools.git@aeb0a2bbb8bbfe421432d776c649650eaeb882a5Repository
git clone git@github.com:paulfioravanti/bitcoin_address.git
cd bitcoin_address
mix deps.getRun the Code
Open up an iex terminal and confirm that each implementation gives you the
same result for a given private key:
iex -S mix
iex(1)> private_key = BitcoinAddress.Secp256k1.generate_private_key()
"66f08a4c251147c0f9f39881e4b8290bc60f398aa1c5335f60359d1d90d123c7"
iex(2)> BitcoinAddress.Elixir.generate(private_key)
Private key: "66f08a4c251147c0f9f39881e4b8290bc60f398aa1c5335f60359d1d90d123c7"
Public key: "0266321e7bcdf4b912104401202d674954a64679eec11f18e766199f4744bc0b51"
Bitcoin address: "1FXpStf9Ma2Wxe6QrwjZCHnG9SvwGCEPJf"
"1FXpStf9Ma2Wxe6QrwjZCHnG9SvwGCEPJf"
iex(3)> BitcoinAddress.Python.generate(private_key)
Private key: "66f08a4c251147c0f9f39881e4b8290bc60f398aa1c5335f60359d1d90d123c7"
Public key: "0266321e7bcdf4b912104401202d674954a64679eec11f18e766199f4744bc0b51"
Bitcoin address: "1FXpStf9Ma2Wxe6QrwjZCHnG9SvwGCEPJf"
"1FXpStf9Ma2Wxe6QrwjZCHnG9SvwGCEPJf"
iex(4)> BitcoinAddress.CPlusPlus.generate(private_key)
Private key: "66f08a4c251147c0f9f39881e4b8290bc60f398aa1c5335f60359d1d90d123c7"
Public key: "0266321e7bcdf4b912104401202d674954a64679eec11f18e766199f4744bc0b51"
Bitcoin address: "1FXpStf9Ma2Wxe6QrwjZCHnG9SvwGCEPJf"
"1FXpStf9Ma2Wxe6QrwjZCHnG9SvwGCEPJf"For each of the different implementations, rather than create your own private
key manually, you can:
- get the function to generate one for you, or
- use a static pre-generated one as a
:test
iex(5)> BitcoinAddress.Elixir.generate()
Private key: "d11d04584bd82b6a7b290b86b88070391149ab44a446a49b92c0ac40b80ab430"
Public key: "027af4aef662ba5aa1fc100105db5160e70b8d05ffd9eda96b3e308a98e0d9c6ce"
Bitcoin address: "1C14kvagjp1Rvu1PAN5uxija6cR128yEaG"
"1C14kvagjp1Rvu1PAN5uxija6cR128yEaG"
iex(6)> BitcoinAddress.Elixir.generate(:test)
Private key: "038109007313a5807b2eccc082c8c3fbb988a973cacf1a7df9ce725c31b14776"
Public key: "0202a406624211f2abbdc68da3df929f938c3399dd79fac1b51b0e4ad1d26a47aa"
Bitcoin address: "1PRTTaJesdNovgne6Ehcdu1fpEdX7913CK"
"1PRTTaJesdNovgne6Ehcdu1fpEdX7913CK"Tests
Each of the implementations has a sanity test check to just make sure it works,
and continues to work as expected.
Run the test suite
mix testmix test.watch is included in this project, and hence all the tests,
as well as Credo and Dogma checks, can be continuously run when file
changes are made:
mix test.watchGenerate Documentation
This project uses ExDoc to generate HTML documentation.
Generate the documentation with the following command:
mix docsHelpful Resources
Code
- Mastering Bitcoin repo: Code samples from
Mastering Bitcoin 2nd Edition that I ported over to Elixir, using similar
techniques as this repo.
Blog Posts
These are blog posts I wrote documenting the things I needed to do to get
Elixir communicating with Python and C++.
Presentation
I did a talk about this project at the Elixir Sydney Meetup on
7 Feb 2018 (slide deck).
