Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Learn Blockchain Programming with JavaScript
Learn Blockchain Programming with JavaScript

Learn Blockchain Programming with JavaScript: Build your very own Blockchain and decentralized network with JavaScript and Node.js

By Eric Traub
$35.99 $24.99
Book Nov 2018 252 pages 1st Edition
eBook
$35.99 $24.99
Print
$43.99
Subscription
$15.99 Monthly
eBook
$35.99 $24.99
Print
$43.99
Subscription
$15.99 Monthly

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Nov 30, 2018
Length 252 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781789618822
Category :
Concepts :
Table of content icon View table of contents Preview book icon Preview Book

Learn Blockchain Programming with JavaScript

Setting up the Project

Welcome to Learning Blockchain Programming with JavaScript. As the name suggests, in this book, you'll learn how to build a fully functional blockchain from scratch using the JavaScript programming language. The blockchain that you build will have functionalities that are similar to those you would find in a production-level blockchain for examples such as Bitcoin or Ethereum.

In this book, you will understand how blockchain technology actually workes by learning to build your own blockchain and understanding the decentralized network. Toward the conclusion of the book, you will have a full-fledged blockchain prototype that is hosted on a decentralized network, and you'll have gained a great deal of knowledge and understanding as to how blockchains actually work under the hood.

The blockchain that we will create throughout this book will be able to carry out the following functionalities:

  • Perform a proof of work to secure the blockchain
  • Create new blocks through a mining process
  • Create new, immutable transactions
  • Validate the entire blockchain and all of the data within each block
  • Retrieve address/transaction/block data

Along with these, the blockchain will have many other important features. You'll get to explore those as you read further through the chapters.

To follow this book, all you'll need is a computer and some basic knowledge of the JavaScript programming language.

Firstly, in this introductory chapter, let's try to understand what blockchain actually is. This will help you to become familiar with the concept of blockchain, as this is a prerequisite for the book. Then we'll move on to learn how to set up the project to create our own blockchain.

So, let's get started!

What is a blockchain?

In this section, let's go through a brief explanation of what a blockchain is. Simply put, a blockchain is an immutable, distributed ledger. Now, these words may seem quite complex, but when we try to break them down, it is very easy to understand them. Let's begin by exploring what a ledger actually is. A ledger is simply a collection of financial accounts or transactions (or in other words, a record of transactions that people have made).

Let's take a look at the following example to get a better understanding of ledgers. In this example, Kim paid Joe $30 and Kevin paid Jen $80. A ledger is simply a document that is used to keep track of these transactions. You can see this depicted in the following screenshot:

Now, what does it mean for a blockchain to be immutable? This means that it cannot be changedever. Consequently, when a transaction is recorded, it cannot be undone. Other factors that cannot be changed include the amount of money that was sent or the people who took part in the transaction. Once a transaction is made, no aspects of that transaction can be changed because it is immutable.

In the world today, we see many applications, platforms, and networks that are all centralized. Take Facebook, for example. Everyone who uses Facebook has to trust this company is protecting their data and not abusing it. Compared to this, blockchain is different. Blockchain technology is not centralized like Facebook, Google, or most other entities. Instead, it is a distributed network, which means that any given blockchain network is not controlled by a single entity, but is run by normal, everyday people. Blockchains, such as Bitcoin, are supported and hosted by thousands of people worldwide. Consequently, all of our data, or the ledger in this case, is not at the mercy of a single company or entity. This proves to be a great benefit of blockchain technology because by being distributed, we do not have to trust a single company with our data. Instead, our data is persisted by the entire network of thousands of different people who are all acting independently.

Each individual who contributes to the blockchain network is called a node, and each node has the exact same copy of the ledger. Therefore, the ledger data is hosted and synchronized across the entire network.

So, a blockchain is an immutable distributed ledger. This means that it is a ledger in which the transactions can never be changed and the blockchain itself is distributed across the network and run by thousands of independent people, groups, or nodes.

The blockchain is a very powerful technology which is still in its infancy, but its future is very exciting. There are many ways that blockchain technology can be applied to our world today to make certain industries more secure, efficient, and trustworthy. Some industries that could be transformed with the help of blockchain technology include financial services, healthcare, credit, governments, energy industries, and many others. Pretty much every industry out there could benefit from a more secure, distributed form of data management. You can observe that blockchain technology is at a very exciting stage right now, and many people are excited about what the future holds for it.

Now that we're aware of what blockchain is, let's move onto setting up our project environment to build our blockchain.

What you will learn...

This book will help you to gain a deeper understanding of blockchain technology by building your own blockchain from scratch. Blockchain is a fairly new technology, and while it can seem tough and slightly overwhelming to learn at first, we're going to take a step-by-step approach and break it down in order to understand how it works under the hood. By the time you finish this book, you will have a very solid understanding of how blockchain technology works, and you will have built your own entire blockchain as well.

In this book, we will start by building the blockchain itself. At this point, we will build a blockchain data structure that has the following abilities:

  • Proofing work
  • Mining new blocks
  • Creating transactions
  • Validating the chain
  • Retrieving address data and other functionalities

Thereafter, we will create an API or a server that will allow us to interact with our blockchain from the internet. Through our API, we will be able to use all of the functionality that we have built into our blockchain data structure.

Furthermore, you'll be learning to create a decentralized network. This means that we'll have multiple servers running and acting as separate nodes. We'll also make sure that all of the nodes interact with each other properly and share data with each other in the correct format. In addition, you'll learn how to synchronize the entire network by making sure that any new nodes or transactions that are created are broadcast throughout the entire network.

We'll then move onto creating a consensus algorithm. This algorithm will be used to make sure that our entire blockchain stays synchronized and that this algorithm will be used to make sure that each node in our network has the correct blockchain data.

Finally, we will create a block explorer. This will be a user interface that will allow us to explore our blockchain in a user-friendly manner, and it will also allow us to query our blockchain for specific block transactions and addresses.

Firstly, however, we need to set up our development environment.

Environment setup

Let's get started with building our blockchain project. The first thing we're going to do is open our terminal and create our blockchain directory by typing commands into the terminal, as seen in the following screenshot:

Let's begin by creating a folder called programs. Inside this folder, let's create a directory called blockchain. This directory is currently empty. Inside of this blockchain directory is where we're going to be doing all of our programming. We are going to be building our entire blockchain inside of this blockchain directory.

Now our blockchain directory is ready, and the first thing that we need to do is to add some folders and files into it. The first folder that we want to put into the directory will be called dev, so we want to make sure that we are inside of the blockchain directory, and then let's type the following command into the terminal:

mkdir dev

Inside this dev directory is where we are going to be doing most of our coding. This is where we're going to build our blockchain data structure and create our API to interact with our blockchain, test it, and fulfill other similar tasks. Next, inside this dev folder, let's create two files: blockchain.js and test.js. To do this, enter the following command:

cd dev
touch blockchain.js test.js

The touch term in the preceding command line will help us in creating the mentioned files. The blockchain.js file is where we will type our code to create the blockchain and the test.js file is where we will write code to test our blockchain.

Next, let's return back to our blockchain directory by typing the following command in the terminal:

cd .. 

In the blockchain directory, let's run the following command to create the npm project:

npm init 

After running the preceding command, you will get some options on your terminal. To set up the project, you can just press Enter through those options.

So, this is pretty much all we need to do in order to set up our project folder structure. Now, if you go to our blockchain directory and open it with a text editor such as Sublime or Atom (or whatever you would like), you will get to see the file structure, as seen in the following screenshot:

The blockchain directory consists of the dev folder that we just created. Inside the dev folder, we can observe our blockchain.js and test.js files. Also, when we run the npm init command, it creates the package.json file for us. This .json file will keep track of our project and any dependencies that we need, allowing us to run scripts. We'll be working more inside of this package.json file in further chapters, so you'll become more familiar with it as we progress through the book.

Project source code

Before we start coding our blockchain, it is worth noting that the entire source code for this book can be found on GitHub at the following link: https://github.com/PacktPublishing/Learn-Blockchain-Programming-with-JavaScript. In this repository, you'll find the completed code for the entire project, and you will also be able to explore all of the files that we will be building in further chapters. Therefore, this may be a good resource for you to use as you make your way through the book.

Summary

To summarize this introductory chapter, we began by exploring what a blockchain actually is and understanding how it functions. Then we moved onto setting up our project to create our very own blockchain. We also had a quick overview of all of the topics you'll get to learn about in this book.

In the next chapter, we'll build our blockchain by learning about the constructor function, prototype object, block method, transaction method, and many more important concepts.

Left arrow icon Right arrow icon
Download code icon Download Code

Key benefits

  • Develop bitcoin and blockchain-based cryptocurrencies using JavaScript
  • Create secure and high-performant blockchain networks
  • Build custom APIs and decentralized networks to host blockchain applications

Description

Learn Blockchain Programming with JavaScript begins by giving you a clear understanding of what blockchain technology is. You’ll then set up an environment to build your very own blockchain and you’ll add various functionalities to it. By adding functionalities to your blockchain such as the ability to mine new blocks, create transactions, and secure your blockchain through a proof-of-work you’ll gain an in-depth understanding of how blockchain technology functions. As you make your way through the chapters, you’ll learn how to build an API server to interact with your blockchain and how to host your blockchain on a decentralized network. You’ll also build a consensus algorithm and use it to verify data and keep the entire blockchain network synchronized. In the concluding chapters, you’ll finish building your blockchain prototype and gain a thorough understanding of why blockchain technology is so secure and valuable. By the end of this book, you'll understand how decentralized blockchain networks function and why decentralization is such an important feature for securing a blockchain.

What you will learn

  • Gain an in-depth understanding of blockchain and the environment setup
  • Create your very own decentralized blockchain network from scratch
  • Build and test the various endpoints necessary to create a decentralized network
  • Learn about proof-of-work and the hashing algorithm used to secure data
  • Mine new blocks, create new transactions, and store the transactions in blocks
  • Explore the consensus algorithm and use it to synchronize the blockchain network

What do you get with eBook?

Product feature icon Instant access to your Digital eBook purchase
Product feature icon Download this book in EPUB and PDF formats
Product feature icon Access this title in our online reader with advanced features
Product feature icon DRM FREE - Read whenever, wherever and however you want
Buy Now

Product Details


Publication date : Nov 30, 2018
Length 252 pages
Edition : 1st Edition
Language : English
ISBN-13 : 9781789618822
Category :
Concepts :

Table of Contents

10 Chapters
Preface Chevron down icon Chevron up icon
Setting up the Project Chevron down icon Chevron up icon
Building a Blockchain Chevron down icon Chevron up icon
Accessing the Blockchain through an API Chevron down icon Chevron up icon
Creating a Decentralized Blockchain Network Chevron down icon Chevron up icon
Synchronizing the Network Chevron down icon Chevron up icon
Consensus Algorithms Chevron down icon Chevron up icon
Block Explorer Chevron down icon Chevron up icon
In conclusion... Chevron down icon Chevron up icon
Other Books You May Enjoy Chevron down icon Chevron up icon

Customer reviews

Filter icon Filter
Top Reviews
Rating distribution
Empty star icon Empty star icon Empty star icon Empty star icon Empty star icon 0
(0 Ratings)
5 star 0%
4 star 0%
3 star 0%
2 star 0%
1 star 0%

Filter reviews by


No reviews found
Get free access to Packt library with over 7500+ books and video courses for 7 days!
Start Free Trial

FAQs

How do I buy and download an eBook? Chevron down icon Chevron up icon

Where there is an eBook version of a title available, you can buy it from the book details for that title. Add either the standalone eBook or the eBook and print book bundle to your shopping cart. Your eBook will show in your cart as a product on its own. After completing checkout and payment in the normal way, you will receive your receipt on the screen containing a link to a personalised PDF download file. This link will remain active for 30 days. You can download backup copies of the file by logging in to your account at any time.

If you already have Adobe reader installed, then clicking on the link will download and open the PDF file directly. If you don't, then save the PDF file on your machine and download the Reader to view it.

Please Note: Packt eBooks are non-returnable and non-refundable.

Packt eBook and Licensing When you buy an eBook from Packt Publishing, completing your purchase means you accept the terms of our licence agreement. Please read the full text of the agreement. In it we have tried to balance the need for the ebook to be usable for you the reader with our needs to protect the rights of us as Publishers and of our authors. In summary, the agreement says:

  • You may make copies of your eBook for your own use onto any machine
  • You may not pass copies of the eBook on to anyone else
How can I make a purchase on your website? Chevron down icon Chevron up icon

If you want to purchase a video course, eBook or Bundle (Print+eBook) please follow below steps:

  1. Register on our website using your email address and the password.
  2. Search for the title by name or ISBN using the search option.
  3. Select the title you want to purchase.
  4. Choose the format you wish to purchase the title in; if you order the Print Book, you get a free eBook copy of the same title. 
  5. Proceed with the checkout process (payment to be made using Credit Card, Debit Cart, or PayPal)
Where can I access support around an eBook? Chevron down icon Chevron up icon
  • If you experience a problem with using or installing Adobe Reader, the contact Adobe directly.
  • To view the errata for the book, see www.packtpub.com/support and view the pages for the title you have.
  • To view your account details or to download a new copy of the book go to www.packtpub.com/account
  • To contact us directly if a problem is not resolved, use www.packtpub.com/contact-us
What eBook formats do Packt support? Chevron down icon Chevron up icon

Our eBooks are currently available in a variety of formats such as PDF and ePubs. In the future, this may well change with trends and development in technology, but please note that our PDFs are not Adobe eBook Reader format, which has greater restrictions on security.

You will need to use Adobe Reader v9 or later in order to read Packt's PDF eBooks.

What are the benefits of eBooks? Chevron down icon Chevron up icon
  • You can get the information you need immediately
  • You can easily take them with you on a laptop
  • You can download them an unlimited number of times
  • You can print them out
  • They are copy-paste enabled
  • They are searchable
  • There is no password protection
  • They are lower price than print
  • They save resources and space
What is an eBook? Chevron down icon Chevron up icon

Packt eBooks are a complete electronic version of the print edition, available in PDF and ePub formats. Every piece of content down to the page numbering is the same. Because we save the costs of printing and shipping the book to you, we are able to offer eBooks at a lower cost than print editions.

When you have purchased an eBook, simply login to your account and click on the link in Your Download Area. We recommend you saving the file to your hard drive before opening it.

For optimal viewing of our eBooks, we recommend you download and install the free Adobe Reader version 9.