Another Way To Build A Blockchain
Guide on how to build a fully functional Blockchain using JavaScript.
Create the Block Class
Create the block class with a file called block.js. Each black has a hash, lastHash, and timestamp attribute. Create block.js In block.js:
Test the new block class. Create dev-test.js alongside block.js:
In package.json
, find the scripts section and add:
"dev-test": "nodemon dev-test"
Then in the command line, run: $ npm run dev-test
Create the Genesis Block
Every blockchain starts with the "genesis block" - a default dummy block to originate the chain. Add a static genesis function to the Block
class in block.js
;
Back in dev-test.js, test out the new genesis block:
$ npm run dev-test
Create the Mine Block
Add a function to add a generate a block based off of some provided data
to store, and a given lastBlock.
Call the function mineBlock
. Generating a block is equated to an act of mining since it takes computational power to "mine" the block. Later on, we’ll make the demand for spending computational power more explicit. Here we're going to add the static mineBlock()
function to the Block
class:
Now test the new mineBlock
function. In dev-test.js, delete all of the lines except for the first line that requires the Block
class.
Setup the Blockchain App
Make a directory for the project called sf-chain. Set up a project in node: $ mkdir sf-chain $ cd-chain $ npm init -y
Install nodemon as a development dependency. Nodemon is a node engine with a live development server. $ npm i nodemon --save-dev
SHA256 Hash
A hashing function generates a unique value for the combination of data attributes in the block. The hash for a new block is based on its own timestamp, the data it stores, and the hash of the block that came before it. Install the crypto-js
module which has the SHA256 (Secure Hashing Algorithm 256-bit) function: $ npm i crypto-js --save
In block.js, require the sha256 from the crypto-js
module at the top:
Then add a new static hash function to the Block class:
Now replace the 'todo-hash’ stub in the mineBlock
function:
Check the command line - npm run dev-test
should still be running. Notice the generated hash of the block.
Create the Test Block
To enforce code consistency as the project develops, add a true testing environment. Install Jest as development dependency, a test runner for JavaScript: $ npm i jest --save-dev
Jest finds files with a test.js
extension. Create a testing file for the block class: Create block.test.js The tests in this file will assert that the block attributes are created properly. In block.test.js:
Add the test
script. In the "scripts" section of package.json:
npm run test
If all goes well, you should see two green check marks.
Create the Blockchain Class
Create the Blockchain
that creates a chain based on the Block
class: Create blockchain.js
Chain Validation
Chain validation ensures that incoming chains are not corrupt once there are multiple contributors to the blockchain. To validate a chain, make sure it starts with the genesis block. Also, ensure that its hashes are generated properly. In the Blockchain
class, add an isValidChain
method:
This method depends on a static blockHash
function in the Block
class. This will generate the hash of a block based only on its instance. In block.js, in the Block
class:
Replace Chain
If another contributor to a blockchain submits a valid chain, replace the current chain with the incoming one. Only replace chains that are actually longer than the current chain. Handle this with a replaceChain
function in the Blockchain
class:
Testing Blockchain
To test the blockchain first create blockchain.test.js
$ npm run test
Test Chain Validation
In blockchain.test.js, add a second blockchain instance: let bc, bc2; … bc = new Blockchain(); bc2 = new Blockchain(); Add new unit tests:
$ npm run test
Test Replace Chain
Test the new chain replacement functionality. In blockchain.test.js:
$ npm run test
In the next section we'll deal with the API.
Get Blocks
Add the express module to create a Node API: $ npm i express --save Create a blockchain instance in the main app file. Then create a GET request to get the blockchain’s block. In app/index.js:
Now in package.json, add the start
and dev
scripts to the “scripts” section:
$ npm run dev
Now open the Postman application. Hit localhost:3001, and notice the response. If all goes well, you’ll find the array of blocks of the blockchain.
Mine Blocks POST
Add a POST request, for users to add blocks to the chain. First install the bodyParser middleware to handle incoming json in express: $ npm i body-parser --save In app/index.js:
Re-open Postman. Open a tab for a new request. Make sure it’s a POST request. Select body → Raw → json/application. Write in the json to post.
Enter localhost:3001/mine
for the endpoint. Send. See the newly posted block in the chain.
Connect to Peers
The same class that creates the original websocket server will be used to connect to existing servers. In p2p-server.js:
Start a P2pServer instance. Head to app/index.js, and require the P2pServer module:
In one command line tab: $ npm run dev
In a second command line tab or window: $ HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001 npm run dev
Expect 'socket connected’ to print in both tabs. In a third command line tab or window: $ HTTP_PORT=3003 P2P_PORT=5003 PEERS=ws://localhost:5001,ws://localhost:5002 npm run dev
Expect 'socket connected’ to be printed two times in each tab. #### Handle Peer Messages Allow the sockets to send messages to each other. In the P2pServer
class:
In this.connectSocket
:
Kill all the running instances on the command line. Fire up one instance with $ npm run dev
Grow this blockchain a little. Open Postman, and fire two post requests to the mine endpoint. The endpoint is localhost:3001/mine
, and the Raw→ Body → Type → application/json: { “data”: “foo” }
Send. Send. Run a second instance in a second command line tab: $ HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001 npm run dev
Observe the received message - the blockchain of the original instance. #### P2P Server Install the Websocket module: ws
. This will allow us to create real-time connections between multiple users of the blockchain: $ npm i ws --save
Create a file called p2p-server.js (peer-to-peer) to write the P2pServer
class. Right now the P2pServer
class will open a websocket server, waiting for connections.
Syncronize the Chain
Use the received chain to synchronize chains across all instances with the replaceChain
function. In the P2pServer
class, in the messageHandler
function:
Add a syncChains function to P2pServer
class. Also cut the existing socket.send(JSON.stringify(this.blockchain.chain));
code into a helper method called sendChain
. Then fix the connectSocket
to use the helper function:
Within app/index.js, call syncChains() within the .post('/mine’)
method:
Confirm the chain synchronization. Kill all the running instances on the command line. Fire up one instance with $ npm run dev
Grow this blockchain a little. Open Postman, and fire two post requests to the mine endpoint. The endpoint is localhost:3001/mine
, and the Raw→ Body → Type → application/json: { “data”: “foo” }
Send. Send. Run a second instance in a second command line tab: $ HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001 npm run dev
Hit localhost:3002/blocks
. Notice the synchronization. Check that the post method also synchronization. Add a new block, with localhost:3001/mine
: Hit localhost:3001/mine Now localhost:3002/blocks
and localhost:3002/blocks
should return the same chain.
Next, onto Proof of Work.
Dynamic Difficulty
Create a system that automatically adjusts the difficulty as more miners are added to the blockchain. In config.js, create a MINE_RATE
constant to represent the millisecond rate that blocks should be mined:
Add difficulty attributes to each block in the chain. In the Block
class of block.test.js
:
The difficulty of each block will be based on the difficulty of the block that came before it. Update the static mineBlock
function:
Add the adjustDifficulty
function:
Proof of Work
The proof-of-work system will deter dishonest contributors to the blockchain by requiring them to do computational work. In block.js, declare a DIFFICULTY constant - the “difficulty” of the system for mining blocks:
Update the constructor:
Update the hash functions:
Include the nonce in toString()
:
Include a default nonce for the genesis
block:
Update the static mineBlock
function to use the proof-of-work system:
Test the Dynamic Difficulty
Test difficulty adjustment in block.test.js
:
Also update the test that previously depended on DIFFICULTY
, since blocks have their own difficulty
attribute:
Also delete this unnecessary line:
Test the Proof of Work
Test the proof-of-work system. First make a config.js file at the root of the project so that the DIFFICULTY
constant can be shared. Cut and paste the DIFFICULTY
constant from block.js: Create config.js:
In block.js, require the DIFFICULTY
constant:
Then in block.test.js, add new unit tests:
$ npm run test
Next we will dive into making the wallet.
Create the Util Key Gen
To create the keyPair and publicKey objects objects, use a module called 'elliptic’. Elliptic is a module in node that contains classes and methods that enable elliptic-curve based cryptography. Elliptic cryptography is an advanced mathematical subject, but essentially, it centers around the idea in that it is computationally infeasible and impossibly expensive to guess the answer to a randomly generated elliptic curve. Install elliptic as a dependency on the command line: $ npm i elliptic --save
With the elliptic module installed, create a new file at the root of the project called chain-util.js. Within chain-util.js, create a ChainUtil class. The class will collect helper methods that expose common functions that wrap around functions from elliptic. A few classes in the project will use these functions. Also create an EC instance:
Side note: in case you’re wondering, sec stands for standards of efficient cryptography. The p stands for prime, and 256 for 256 bits. In the elliptic-based algorithm itself, a key component is a prime number to generate the curve, and this prime number will be 256 bits, or 32 bytes/characters. K stands for Koblitz which is the name of a notable mathematician in the field of cryptography. And 1 stands for this being the first implementation of the curve algorithm in this standard. Add a new static genKeyPair method to this ChainUtil class, that returns a call to the identically-named genKeyPair method of the ec instance:
Now within the wallet/index.js class, require the ChainUtil class. Use the static genKeyPair
method to create a keyPair
object within the constructor of the wallet. And then use this keyPair
object to set the publicKey
:
Create the Transaction
Transactions objects represent exchanges in the cryptocurrency. They will consist of three primary components: 1) an input field which provides information about the sender of the transaction. 2) output fields which detail how much currency the sender is giving to other wallets, and 3) a unique id
to identify the transaction object. To generate an id for transactions, use a module called uuid which stands for universally unique identifier: $ npm i uuid --save
Use the new uuid module in chain-util.js, and create a static id
function within the ChainUtil
class.
Move on to creating the actual transaction class. Create a transaction.js file within the wallet directory: Create the Transaction class, with the ability to generate a new transaction within wallet/transaction.js:
Create the Wallet
To extend this blockchain with a cryptocurrency, we’ll need wallet objects. Wallets will store the balance of an individual, and approve transactions (exchanges of currency) by generating signatures. - Create wallet/ - Create wallet/index.js In the default wallet/index.js file, create the Wallet class. Every wallet instance will have three fields. First is a balance, which is set to INITIAL_BALANCE
. This will variable will be a value every wallet begins with. This helps get the economy flowing. Second, it has a keyPair
object. The keyPair object will contain methods that can return the private key for the wallet, as well as its public key. Third and finally, it has a publicKey field. The public key also serves as the public address for the wallet, and is what other individuals in the network use to send currency to the wallet.
Right now, the balance of each wallet has been set to a global variable that doesn’t exist yet. So in config.js, declare the INITIAL_BALANCE, and set it to 500:
Sign The Transaction
Create the vital input object which provides information about the sender in the transaction. This information includes the sender’s original balance, his or her public key, and most important, his or her signature for the transaction. To generate signatures, the elliptic module gives a convenient sign method within the keyPair object. This takes any data in its hash form, and returns a signature based on the keyPair. Later on, this generated signature and the matching public key can be used to verify the authenticity of the signature. Add this signing feature to the Wallet class called sign. In wallet/index.js:
Also create a static signTransaction method, that will generate the input object for a transaction:
The sign method wants the data to be in a hash. It’s much more efficient to have a hash value with a fixed number of characters, rather than a potentially large object that could be full of long pieces of data. Luckily, there is a hash function that is being used to generate the hashes for blocks in the blockchain. Let’s take advantage of that function, and place it in the ChainUtil class. - Head to block.js. Cut the SHA256 requirement and stick it into the chain util file. Then declare a static hash function within here that takes an arbitrary piece of data, then stringifies it, passes it into the SHA256 hashing function, and returns the string form of the hash object:
Since we’ve the SHA256 function was removed from the block class, use the new ChainUtil hash method in its place. In block.js, import the ChainUtil class:
Then the static hash function of the block class will call the ChainUtil hash function in place of the previous SHA256 function call:
Do the same thing in the transaction class to create a hash for the transaction outputs. Head to transaction.js. Now within the sign function call from the sender wallet, make sure to create a hash for the transaction outputs, before we generate the signature:
Lastly, make sure to generate an input by using this function whenever a new transaction is created. Therefore, call the signTransaction
function in newTransaction
, passing in the transaction instance, and the senderWallet:
Test Transaction Input
Add a test to make sure that this input object was created along with our transaction. In transaction.test.js:
$ npm run test
Test Transaction Updates
Test the new updating functionality. In transaction.test.js:
$ npm run test
Test Transaction Verification
Add some tests to ensure that the signature verification works properly. In transaction.test.js
$ npm run test
Test the Transaction
Create a new file called transaction.test.js: Write the test, transaction.test.js:
Make sure to change the testing environment for Jest from the default mode to “node”. In package.json, add this rule:
$ npm run test
Transaction Updates
To handle the addition of new output objects to an existing transaction, define a function in the Transaction class called update. In transaction.js:
Verify Transactions
Now that transactions generate signatures based upon their outputs and their private keys, provide a way to verify the authenticity of those signatures. Within Chain Util, we’ll provide a new static method called verifySignature. In chain-util.js:
Utilize this method within the transaction class to will the entire transaction. Make a static function called verifyTransaction. Go to transaction.js:
In the next section we will be dealing with the transaction pool.
Get Transactions
By giving each of users their own wallet, users of the application will have the ability to conduct transactions with each other, thus putting the cryptocurrency into action. Start in the index file of the app directory, where holds the main code for the interactive application: - Go to app/index.js
$ npm run dev
- hit localhost:3001/transactions
Handle Messages in P2P Server
Update the messageHandler in the peer to peer server to handle different types of messages. In p2p-server.js:
Now in app/index.js, use the broadcastTransaction function in the '/transact’ endpoint. That way, transactions are broadcasted to the network whenever new ones are made:
Now start up a couple instances to test the new endpoint: $ npm run dev
Second command line tab: $ HTTP_PORT=3002 P2P_PORT=5002 PEERS=ws://localhost:5001 npm run dev
Then post transactions. Hit localhost:3001/transact. Select POST for the type of request, and make sure the type of data is Raw, application/json. Then have a json body that consists of an arbitrary recipient, and a value around 50:
With those inputs loaded in, go ahead and click Send
a couple times. Hit the transactions endpoint on both instances: - localhost:3001/transactions - localhost:3002/transactions Make a second POST transaction, this time with the second instance, http://localhost:3002/transact:
Hit the transactions endpoint on both instances: - localhost:3001/transactions - localhost:3002/transactions
POST Transactions
Create the equivalent method that actually adds new transactions to the transaction pool, in app/index.js:
$ npm run dev
Test a POST request in postman, with raw application/json set as the Body data. Use some json similar to this for the data:
Hit the endpoint a couple times
Public Key Endpoint
Make a new get request, under the endpoint, public-key
to expose the address of an instance:
Test Transaction Pool
Test the Transaction Pool by creating a file called transaction-pool.test.js alongside the transaction-pool.js file.
$ npm run test
Test Wallet Transactions
Create wallet/index.test.js
$ npm run test
Add to Transaction Pool
A transaction pool will collect all transactions submitted by individuals in the cryptocurrency network. Then, miners will do the work of taking transactions from the pool and including them in the blockchain. Create the transaction pool in the wallet directory with a file called transaction-pool.js:
The updateOrAddTransaction
method by default will add an incoming transaction to the transactions array. However, there is the possibility that a transaction could come in that already exists in the array. Why? Recall that there is the ability to update transactions to have additional outputs. This means that a transaction could exist in the pool. However, if it gets updated, and is resubmitted to the pool, that transaction shouldn’t appear twice.
Transaction Pool to P2P Server
Create the equivalent method that actually adds new transactions to the transaction pool, in app/index.js:
$ npm run dev
Test a POST request in postman, with raw application/json set as the Body data. Use some json similar to this for the data:
Hit the endpoint a couple times
Transactions with Wallet
Create transactions with the Wallet class. Define a new method within the Wallet class called createTransaction with three parameters, a recipient, the amount for the transaction, and a transactionPool object. The function will assume an existingTransaction
function exists for the transactionPool, to help replace existing transactions in the pool: In wallet/index.js:
Make sure to import the Transaction class in wallet/index.js
Add the existingTransaction function to transaction-pool.js:
Now onto the Miner.
Create the Miner Class
Create a Miner class to tie all the concepts together. Create app/miner.js:
Grab Valid Transactions
The first step to writing the mine function that we have in the Miner class is to add a validTransactions function for the TransactionPool. With this validTransactions function, return any transaction within the array of transactions that meets the following conditions. First, its total output amount matches the original balance specified in the input amount. Second, we’ll also verify the signature of every transaction to make sure that the data has not been corrupted after it was sent by the original sender. Add a function called validTransactions to transaction-pool.js:
Note that there is a dependency though on the Transaction class. So import the Transaction class at the top of the file:
jsconst Transaction = require('../wallet/transaction');
Test Valid Transactions
Test the validTransactions function with transaction-pool.test.js. There is actually a feature that can shorten down on the number of lines. Recall that there is a function within the wallet class create a transaction based on a given address, amount, and transaction pool. The createTransaction also does the job of adding the created transaction to the pool. This is what is already done manually by creating the transaction and adding it to the pool. So we can reduce this with one call to wallet.createTransaction, and the same random address, amount, and tp transaction pool instance:
To begin testing the validTransactions functionality, create a situation where there is a mix of valid and corrupt transactions. Capture the scenario with a new describe block:
$ npm run test
Find the completed project on my Github.