A toy HTTP-based Proof of Authority cryptocurrency written in Python with Django and Django REST Framework.
Welcome to the blockchain, Django!
Let's just get this out here now, this is obviously not meant to be a robust cryptocurrency. It was just a fun experiment to try out. Use something real if you're looking for a blockchain to fork.
Ready to run your own blockchain on Django? You'll find it's incredibly simple, just make sure you have Docker installed first.
From the project root, run:
./scripts/provision_test_nodes.sh
That's it! In a few moments you'll have a blockchain network running with 3 miners. Here's what that command did for you:
- Create three key pairs (one for each miner)
- Create a genesis block with the public keys of the miners in it. Only these miners will be authorized to mine new blocks. (In case you missed it, this is Proof of Authority, not Proof of Work.)
- Set up each miner's database and configuration settings.
- Start up the 3 miners.
You can press Ctrl + C
when you're ready to stop the miners. If you want to run them again in the future, simply run:
docker-compose up
Running provision_test_nodes.sh
again will wipe out your existing blockchain, so keep that in mind.
So you have some miners, awesome. While you could idly watch them mine blocks every 10 minutes, that could get boring. Good news my friend, there are APIs you can use! We are using Django after all.
Fire up your favorite HTTP client (I recommend Paw if you're on a Mac), and point it to https://localhost:9811
. This assumes that you provisioned your miners with the script above of course.
You can change the port to determine which miner you talk to:
9811: miner 1
9812: miner 2
9813: miner 3
GET /api/block_count/
This will give you the number of blocks in the longest chain. Keep in mind there may be more blocks in the database, but they're not important right now.
GET /api/block/{block_hash}/
This will give you all of the block's data, along with all of the data of the transactions that belong to it.
GET /api/transaction/{transaction_hash}/
This will give you all of the transaction's data.
POST /api/submit_transaction/
This will create a transaction and send it off to the pool of unconfirmed transactions. (I've heard it's sunny there!)
This API expects you to send some extra data along in JSON format:
hash
- the hash of the transactionfrom_account
- the public key of the wallet to send coins fromto_account
- the public key of the wallet to send coins tocoins
- the number of coins to sendextra_data
- (optional) base64 encoded data to include in the transactiontime
- the time associated with the transactionsignature
- a signature of thehash
signed by the private key of thefrom_account
Boocoin comes with a very simple command line wallet that allows you to store keys and send transactions to the blockchain. This prevents you from needing to manually call the submit_transaction API.
To use the wallet, run:
./scripts/wallet.sh
You'll have the option to:
- Add (or generate) a new keypair
- List your existing keys
- Send coins (create a transaction)
There are other API endpoints that are used by the miners to communicate with eachother and send blocks/transactions around. I won't write about them here, but just know they exist if you really want them.