Cryptocurrencies
Cryptocurrencies are made from four components:
- The data structure (most of the time a blockchain)
- Cryptography
- Rewards for running a nodes
- Decentralized (Peer-to-peer) consensus
Cryptographic Hash function
Before starting with cryptocurrencies, let's see the definition of one of the core element of Cryptocurrencies: the cryptographic hash function. They are used in many ways in cryptocurrencies, such as identifier but also to verify that data are not modified.
It is a mathematical algorithm that maps data of arbitrary size to a bit string of a fixed size (a hash) and is designed to be a one-way function, that is, a function which is infeasible to invert.
There are many cryptographic hash functions, such as SHA-256 or Scrypt, X11 (using 11 different hashing functions).
You can test SHA-256 here: https://jsfiddle.net/sandoche/e0jmqLok/
Data structure
The data structure is the skeleton of every cryptocurrency. Let's talk about the most famous one: the Blockchain.
Blocks
Most of the cryptocurrencies are using the blockchain data structure. As its name sounds, it is a chain of blocks containing data. Easy right? Find below an example of a simple block's structure:
- previousHash ? the hash of the previous block
- timetamp ? the date time code
- data ? in the case of cryptocurrencies it will contain the transactions
- nonce ? an integer (it's used for mining we will explain it later)
- hash ? the hash of the current block calculated like this:
Hash of Blockchain
Now that you have seen the block structure it gets easier. The power of this data structure is that, if you change the data of one block, you will have to re-calculate the hash of the block which will invalidate the value previousHash of the next block.
Note that there is one very special block, the first block, it doesn't have any previousHash, it's called the GenesisBlock.
The blockchain data structure is used by most of the coins such as Bitcoin, Litecoin or Ethereum.
By adding transactions to the blocks of a blockchain you can turn it into a ledger.
Now we know what is a blockchain, let's talk about the transactions and how they are processed.
Transactions
Here is the structure of a transaction before it's made:
- transactionInputs[] ? Array of transactionInput
- transactionOutputs[] ? Array of transactionOutput
- id ? Hash calculated from the content of transactionInputs[] and transactionOutputs[]
Now the structure of transactionOutput and transactionInput.
transactionOutput:
- address - Address of the receiver of the transaction
- amount - Amount of the cryptocurrency to be sent to the address
transactionInput:
transactionOutputId ? id / hash of a previous transaction where the output is taken and use as an input
transactionOutputIndex ? index of the output to find the right output from the transactionOutputs[] array
The rule is the following in every transaction the sum of inputs should be equal to the sum of outputs! So what should be done is: if, let's say Alice, has received 30 and she wants to send 10, she has to send also 20 to herself! This is how bitcoin works in order to verify transactions faster. It avoids checking the full history every time a transaction is issued.
Once the transaction is created it has to be signed before being broadcast to the nodes to be part of a block.