paint-brush
A cryptocurrency implementation in less than 1500 lines of codeby@conradoqg
10,726 reads
10,726 reads

A cryptocurrency implementation in less than 1500 lines of code

by Conrado Quilles GomesMay 1st, 2017
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

Cryptocurrencies and smart-contracts on top of a <a href="https://hackernoon.com/tagged/blockchain" target="_blank">blockchain</a> aren’t the most trivial concepts to understand, things like wallets, addresses, block proof-of-work, transactions and their signatures, make more sense when they are in a broad context. Inspired by <a href="https://github.com/lhartikk/naivechain" target="_blank">Naivechain</a> from <a href="https://medium.com/@lhartikk" data-anchor-type="2" data-user-id="c429710f50ea" data-action-value="c429710f50ea" data-action="show-user-card" data-action-type="hover" target="_blank">Lauri Hartikka</a>, the <a href="https://github.com/conradoqg/naivecoin" target="_blank">Naivecoin</a> project is an attempt to provide as concise and simple an implementation of a cryptocurrency as possible.

Coin Mentioned

Mention Thumbnail
featured image - A cryptocurrency implementation in less than 1500 lines of code
Conrado Quilles Gomes HackerNoon profile picture


Motivation

Cryptocurrencies and smart-contracts on top of a blockchain aren’t the most trivial concepts to understand, things like wallets, addresses, block proof-of-work, transactions and their signatures, make more sense when they are in a broad context. Inspired by Naivechain from Lauri Hartikka, the Naivecoin project is an attempt to provide as concise and simple an implementation of a cryptocurrency as possible.

What is cryptocurrency

From Wikipedia : A cryptocurrency (or crypto currency) is a digital asset designed to work as a medium of exchange using cryptography to secure the transactions and to control the creation of additional units of the currency.

Key concepts of Naivecoin

Components

  • HTTP Server
  • Node
  • Blockchain
  • Operator
  • Miner

Features

  • HTTP API interface to control everything
  • Synchronization of blockchain and transactions
  • Simple proof-of-work (The difficulty increases every 5 blocks)
  • Addresses creation using a deterministic approach EdDSA
  • Data is persisted to a folder

Naivechain uses websocket for p2p communication, but it was dropped to simplify the understanding of message exchange. It is relying only on REST communication.

Components communication

Not all components in this implementation follow the complete list of requirements for a secure and scalable cryptocurrency. Inside the source-code, you can find comments with INFO: that describes what parts could be improved (and how) and what techniques were used to solve that specific challenge.

HTTP Server

Provides an API to manage the blockchain, wallets, addresses, transaction creation, mining request and peer connectivity.

It’s the starting point to interact with the Naivecoin, and every node provides a swagger API to make this interaction easier.

From the Swagger UI is possible to access a simple UI to visualize the blockchain and the unconfirmed transactions.

Blockchain

The blockchain holds two pieces of information, the block list (a linked list), and the transaction list (a hash map).

It’s responsible for:

  • Verification of arriving blocks;
  • Verification of arriving transactions;
  • Synchronization of the transaction list;
  • Synchronization of the block list;

The blockchain is a linked list where the hash of the next block is calculated based on the hash of the previous block plus the data inside the block itself:

A block is added to the block list if:

  1. The block is the last one (previous index + 1);
  2. The previous block is correct (previous hash == block.previousHash);
  3. The hash is correct (calculated block hash == block.hash);
  4. The difficulty level of the proof-of-work challenge is correct (difficulty at blockchain index n < block difficulty);
  5. All transactions inside the block are valid;
  6. The sum of output transactions are equal the sum of input transactions + 50 coins representing the reward for the block miner;
  7. Check if there is a double spending in that block
  8. There is only 1 fee transaction and 1 reward transaction.

A transaction inside a block is valid if:

  1. The transaction hash is correct (calculated transaction hash == transaction.hash);
  2. The signature of all input transactions are correct (transaction data is signed by the public key of the address);
  3. The sum of input transactions are greater than output transactions, it needs to leave some room for the transaction fee;
  4. The transaction isn’t already in the blockchain
  5. All input transactions are unspent in the blockchain.

You can read this post from Naivechain for more details about how the blockchain works.

Transactions is a list of unconfirmed transactions. Nothing special about it. In this implementation, the list of transactions contains only the unconfirmed transactions. As soon as a transaction is confirmed, the blockchain removes it from this list.

[    transaction 1,    transaction 2,    transaction 3]

A transaction is added to the transaction list if:

  1. It’s not already in the transaction list;
  2. The transaction hash is correct (calculated transaction hash == transaction.hash);
  3. The signature of all input transactions are correct (transaction data is signed by the public key of the address);
  4. The sum of input transactions are greater than output transactions, it needs to leave some room for the transaction fee;
  5. The transaction isn’t already in the blockchain
  6. All input transactions are unspent in the blockchain;

Block structure:

A block represents a group of transactions and contains information that links it to the previous block.

The details about the nonce and the proof-of-work algorithm used to generate the block will be described somewhere ahead.

Transaction structure:

A transaction contains a list of inputs and outputs representing a transfer of coins between the coin owner and an address. The input list contains a list of existing unspent output transactions and it is signed by the address owner. The output list contains amounts to other addresses, including or not a change to the owner address.

Operator

The operator handles wallets and addresses as well the transaction creation. Most of its operation are CRUD related. Each operator has its list of wallets and addresses, meaning that they aren’t synchronized between nodes.

Wallet structure:

A wallet contains a random id number, the password hash and the secret generated from that password. It contains a list of key pairs each one representing an address.

Address structure:

The address is created in a deterministic way, meaning that for a given password, the next address is created based on the previous address (or the password secret if it’s the first address).

It uses the EdDSA algorithm to generate a secret public key pair using a seed that can come from a random generated value from the password hash (also in a deterministic way) or from the last secret key.

Only the public key is exposed as the user’s address.

Miner

The Miner gets the list of pending transactions and creates a new block containing the transactions. By configuration, every block has at most 2 transactions in it.

Assembling a new block:

  1. From the list of unconfirmed transaction selected candidate transactions that are not already in the blockchain or is not already selected;
  2. Get the first two transactions from the candidate list of transactions;
  3. Add a new transaction containing the fee value to the miner’s address, 1 satoshi per transaction;
  4. Add a reward transaction containing 50 coins to the miner’s address;
  5. Prove work for this block;

Proof-of-work:

The proof-of-work is done by calculating the 14 first hex values for a given transaction hash and increases the nonce until it reaches the minimal difficulty level required. The difficulty increases by an exponential value (power of 5) every 5 blocks created. Around the 70th block created it starts to spend around 50 seconds to generate a new block with this configuration. All these values can be tweaked.

The this.blockchain.getDifficulty() returns the hex value of the current blockchain's index difficulty. This value is calculated by powering the initial difficulty by 5 every 5 blocks.

The block.getDifficulty() returns the hex value of the first 14 bytes of block's hash and compares it to the currently accepted difficulty.

When the hash generated reaches the desired difficulty level, it returns the block as it is.

Node

The node contains a list of connected peers and does all the data exchange between nodes, including:

  1. Receive new peers and check what to do with it
  2. Receive new blocks and check what to do with it
  3. Receive new transactions and check what to do with it

The node rebroadcasts every information it receives unless it doesn’t do anything with it, for example, if it already has the peer/transaction/blockchain.

An extra responsibility is to get a number of confirmations for a given transaction. It does that by asking every node if it has that transaction in its blockchain.

Check it out:


conradoqg/naivecoin_naivecoin - A cryptocurrency implementation in less than 1500 lines of code_github.com

That’s it.