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
$15.99 per month
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 a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

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 a Packt Subscription?

Free for first 7 days. $15.99 p/m after that. Cancel any time!
Product feature icon Unlimited ad-free access to the largest independent learning library in tech. Access this title and thousands more!
Product feature icon 50+ new titles added per month, including many first-to-market concepts and exclusive early access to books as they are being written.
Product feature icon Innovative learning tools, including AI book assistants, code context explainers, and text-to-speech.
Product feature icon Thousands of reference materials covering every tech concept you need to stay up to date.
Subscribe now
View plans & pricing

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

What is included in a Packt subscription? Chevron down icon Chevron up icon

A subscription provides you with full access to view all Packt and licnesed content online, this includes exclusive access to Early Access titles. Depending on the tier chosen you can also earn credits and discounts to use for owning content

How can I cancel my subscription? Chevron down icon Chevron up icon

To cancel your subscription with us simply go to the account page - found in the top right of the page or at https://subscription.packtpub.com/my-account/subscription - From here you will see the ‘cancel subscription’ button in the grey box with your subscription information in.

What are credits? Chevron down icon Chevron up icon

Credits can be earned from reading 40 section of any title within the payment cycle - a month starting from the day of subscription payment. You also earn a Credit every month if you subscribe to our annual or 18 month plans. Credits can be used to buy books DRM free, the same way that you would pay for a book. Your credits can be found in the subscription homepage - subscription.packtpub.com - clicking on ‘the my’ library dropdown and selecting ‘credits’.

What happens if an Early Access Course is cancelled? Chevron down icon Chevron up icon

Projects are rarely cancelled, but sometimes it's unavoidable. If an Early Access course is cancelled or excessively delayed, you can exchange your purchase for another course. For further details, please contact us here.

Where can I send feedback about an Early Access title? Chevron down icon Chevron up icon

If you have any feedback about the product you're reading, or Early Access in general, then please fill out a contact form here and we'll make sure the feedback gets to the right team. 

Can I download the code files for Early Access titles? Chevron down icon Chevron up icon

We try to ensure that all books in Early Access have code available to use, download, and fork on GitHub. This helps us be more agile in the development of the book, and helps keep the often changing code base of new versions and new technologies as up to date as possible. Unfortunately, however, there will be rare cases when it is not possible for us to have downloadable code samples available until publication.

When we publish the book, the code files will also be available to download from the Packt website.

How accurate is the publication date? Chevron down icon Chevron up icon

The publication date is as accurate as we can be at any point in the project. Unfortunately, delays can happen. Often those delays are out of our control, such as changes to the technology code base or delays in the tech release. We do our best to give you an accurate estimate of the publication date at any given time, and as more chapters are delivered, the more accurate the delivery date will become.

How will I know when new chapters are ready? Chevron down icon Chevron up icon

We'll let you know every time there has been an update to a course that you've bought in Early Access. You'll get an email to let you know there has been a new chapter, or a change to a previous chapter. The new chapters are automatically added to your account, so you can also check back there any time you're ready and download or read them online.

I am a Packt subscriber, do I get Early Access? Chevron down icon Chevron up icon

Yes, all Early Access content is fully available through your subscription. You will need to have a paid for or active trial subscription in order to access all titles.

How is Early Access delivered? Chevron down icon Chevron up icon

Early Access is currently only available as a PDF or through our online reader. As we make changes or add new chapters, the files in your Packt account will be updated so you can download them again or view them online immediately.

How do I buy Early Access content? Chevron down icon Chevron up icon

Early Access is a way of us getting our content to you quicker, but the method of buying the Early Access course is still the same. Just find the course you want to buy, go through the check-out steps, and you’ll get a confirmation email from us with information and a link to the relevant Early Access courses.

What is Early Access? Chevron down icon Chevron up icon

Keeping up to date with the latest technology is difficult; new versions, new frameworks, new techniques. This feature gives you a head-start to our content, as it's being created. With Early Access you'll receive each chapter as it's written, and get regular updates throughout the product's development, as well as the final course as soon as it's ready.We created Early Access as a means of giving you the information you need, as soon as it's available. As we go through the process of developing a course, 99% of it can be ready but we can't publish until that last 1% falls in to place. Early Access helps to unlock the potential of our content early, to help you start your learning when you need it most. You not only get access to every chapter as it's delivered, edited, and updated, but you'll also get the finalized, DRM-free product to download in any format you want when it's published. As a member of Packt, you'll also be eligible for our exclusive offers, including a free course every day, and discounts on new and popular titles.