Generator of cryptocurrency addresses
This is a Django application for generating and displaying cryptocurrency addresses via REST API.
The addresses are generated by using hierarchical deterministic key derivation with a single master private key. The derivation path is the input parameter of the address generator.
Currently supported crypto currencies:
- BTC bitcoin
- ETH ethereum
- LTC litecoin
- TRX tron
Follow this instructions to run locally.
-
Install Python 3.6 or newer.
-
Create a python virtual environment:
python -m venv .venv
- Activate the virtual environment:
source .venv/bin/activate
- Update pip
pip install --upgrade pip
- Install Python library dependencies:
pip install -r requirements.txt
- Create the migrations
python manage.py makemigrations
python manage.py migrate
- Before running the service, set the environment variable:
export ZEPLY_XPRIVATE_KEY='xprv9s21ZrQH143K24t96gCaezzt1QQmnqiEGm8m6TP8yb8e3TmGfkCgcLEVsskufMW9R4KH27pD1kyyEfJkYz1eiPwjhFzB4gtabH3PzMSmXSM'
In this case ZEPLY_XPRIVATE_KEY was taken from HD wallet examples but it can be generated with a library function or algorithm. This is a master / root private key for generating a private key for certain address.
Start the application:
python manage.py runserver
The application can be run within a docker container:
docker run -it --rm --name address_generator -v "$PWD":/home/app -p hostport:8000 -w /home/app python:3.8 bash
Then inside the container run the server using this command:
python manage.py runserver 0.0.0.0:8000
This API supports JSON file format in the payload for responses and for address creation request.
The application accepts the following HTTP requests:
- Create a BTC address:
POST /addresses/ HTTP/1.1
Host: localhost:8000
Content-Type: application/json
{
"currency": "BTC",
"path": "m/44/0"
}
Response:
HTTP/1.1 201 OK
Content-Type: application/json
{
"id": 1,
"currency": "BTC",
"address": "1EuXukqwKVKxPrv8xfUsu5Jq5anmM4XWkp"
}
- List addresses:
GET /addresses/ HTTP/1.1
Host: localhost:8000
Response:
HTTP/1.1 200 OK
Content-Type: application/json
[
{
"id": 1,
"currency": "BTC",
"address": "1EuXukqwKVKxPrv8xfUsu5Jq5anmM4XWkp"
},
{
"id": 2,
"currency": "ETH",
"address": "0xEb054b3e35a62e949eC0B41411375d19426338bA"
}
]
- Retrieve an address with a given ID, i.e.
1
:
GET /addresses/1/ HTTP/1.1
Host: localhost:8000
Response:
HTTP/1.1 200 OK
Content-Type: application/json
{
"id": 1,
"currency": "BTC",
"address": "1EuXukqwKVKxPrv8xfUsu5Jq5anmM4XWkp"
}
To run all tests, run from the root directory of the repository:
python manage.py test
To run only the unit test:
python mange.py test -v3 addresses.tests
A unit test tests the address generator module with the generate_address
API.
In the unit test, all the dependencies (usually the libraries imported in the module under test) are patched / mocked.
Only the functionalities of the module under test have to be tested, but not the dependencies,
e.g. that hdwallet
can generate an address must not be tested.
A REST API end-to-end test tests that each endpoint can be requested and the response is the proper.
The paths and ZEPLY_XPRIVATE_KEY
master / root private key are sensitive data. Although the private key is always needed to generate an address, the paths are never returned in any response.
To increase security, django can be used with SSL.
For using SSL, a certificate needs to be generated:
openssl req -x509 -newkey rsa:4096 -keyout key.pem -out cert.pem -sha256 -days 365 -nodes -subj '/CN=localhost'
The ZEPLY_XPRIVATE_KEY
environment variable could be provided by a secret manager service like
AWS Secrets Manager.
But how does bitcoin actually work?
Python - Hierarchical Deterministic Wallet
Bitcoin and Ethereum Address Validators
Bitpanda - What are public keys, private keys and wallet addresses?
Django REST framework - API Guide
Angela Checa - Trjegul84