/address_generator-insolent-rave

Address generator for cryto currencies

Primary LanguagePythonMIT LicenseMIT

Address generator

Generator of cryptocurrency addresses

Description

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

Setup

Follow this instructions to run locally.

  1. Install Python 3.6 or newer.

  2. Create a python virtual environment:

python -m venv .venv
  1. Activate the virtual environment:
source .venv/bin/activate
  1. Update pip
pip install --upgrade pip
  1. Install Python library dependencies:
pip install -r requirements.txt
  1. Create the migrations
python manage.py makemigrations

python manage.py migrate
  1. 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.

Usage

Start the application:

python manage.py runserver

Docker

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

API

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"
}

Tests

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.

Security

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.

Useful links

But how does bitcoin actually work?

Python - Hierarchical Deterministic Wallet

Bitcoin and Ethereum Address Validators

Litecoin Address Validator

Tron Address Validator

Crypto Address Formats

Bitpanda - What are public keys, private keys and wallet addresses?

Django REST framework - API Guide

Author

Angela Checa - Trjegul84