This repository implements a simple Blockchain using TypeScript. It is inspired by the following article. It is initially made for Zagreb JS Meetup.
Run npm start -- --port <port-number> --nodes <node-list>
to start the Blockchain node.
<node-list>
Is a comma separated string of node IPs.
npm start -- --port 3002 --nodes 127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3003
Run npm run tsc:watch
to start TypeScript compiler and run npm run watch
to start node in watch mode.
Run npm test
to run unit tests.
This is a simple Blockchain created to showcase some basic Blokchain concepts. It runs as an express server with REST interface.
Network is created by creating multiple servers and providing them with the list of neighbouring nodes via the --nodes
console parameter.
Blockchain implements SHA-256 hashing function from Nodes native crypto
package. It is used for verifying transactions and blocks.
Blockchain implements asymmetric cryptography to sign and verify transactions. Private/public key pair is generated using Elliptic Curve Digital Signature Algorithm from the rsasign
package.
Blockchain can create simple transactions. It uses the public key of the miner as a sender
address. Once created, the transaction is hashed and signed with the miners private key. Afterwards signature is included in the transaction.
Other nodes can verify the transaction by rehashing the transaction data, decrypting the signature with the address in the sender
field and comparing these two hashes.
Blocks are basic building blocks of the Blockchain. They contain transactions. Once the block is added to the Blockchain its transactions are confirmed. Blocks are created valid the process called mining.
Blockchain implements basic Proof of Work algorithm. Miners need to find a number called nonce
that, when added to the block and hashed, produces a hash with difficulty
number of leading 0s ('0000').
Blockchain class methods proofOfWork
and validateBlock
implement Proof of Work algorithm.
Proof of work is used to make the creating of the blocks computationally expensive. Reason for that is to discourage the creation of fake blocks which would undermine the integrity of the network.
Mining is used to create a new block and new coins. It is done by using the mine
method.
First, a basic block is created using the createBlock
method. It includes all the pending transactions.
Second, the miner is granted the reward
, amount of coins for its service which is achieved by creating a special transaction called coinbase trasaction
that is later added to the front of the transactions in the block. This process creates new coins in the network.
Following that, the correct timestamp
is added and the Proof of Work algorithm starts. When it is accomplished, the calculated nonce
is added to the block. At that point the block is finished.
Once created transactions and blocks are broadcast through the whole network. It is achieved by each node sending REST calls to each other from the list of nodes given during initialization.
POST /transactions/new
Creates a new transaction using the miners public key as a sender address and private key to sign it. After creation, the transaction is added to the pending transaction pool and broadcast to neighboring nodes.
Parameter | Description |
---|---|
recipient |
Hexadecimal hash address of a transaction recipient |
amount |
Amount of coins to be transferred |
POST /mine
If there are pending transactions, the mining process creates a new block. When created, the new block is added to chain and broadcast to neighboring nodes. Otherwise, it returns 400.
GET /chain
Returns the current chain state and validates it.
POST /transactions
Used for broadcasting of transactions. If the transaction is valid and not duplicated, it is added to the pending transaction pool. Transaction is then broadcast again. Invalid or duplicate transactions are discarded.
Parameter | Description |
---|---|
recipient |
Public key address of a transaction recipient |
sender |
Public key address of a transaction sender |
amount |
Amount of coins to be transferred |
timestamp |
Unix time when transaction was created |
signature |
Transaction hash encrypted with the private key of the sender |
POST /blocks
Used for broadcasting of blocks. If the block is valid and not duplicated, it is added to the chain. All the transactions included in the block are removed from the pending transaction pool. Block is then broadcast again. Invalid or duplicate blocks are discarded.
Parameter | Description |
---|---|
transactions |
Array of transactions |
difficulty |
Number representing the mining difficulty |
previousHash |
Hexadecimal hash of a previous block in the chain |
timestamp |
Unix time when block was mined |
nonce |
Cryptographic nonce number used to prove work that is done to create a block |