Learn Blockchain Programming with JavaScript
eBook - ePub

Learn Blockchain Programming with JavaScript

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

  1. 252 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Learn Blockchain Programming with JavaScript

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

About this book

Explore the essentials of blockchain technology with JavaScript to develop highly secure bitcoin-like applications

Key Features

  • 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

Book 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

Who this book is for

Learn Blockchain Programming with JavaScript is for JavaScript developers who wish to learn about blockchain programming or build their own blockchain using JavaScript frameworks.

Tools to learn more effectively

Saving Books

Saving Books

Keyword Search

Keyword Search

Annotating Text

Annotating Text

Listen to it instead

Listen to it instead

Information

Building a Blockchain

In the previous chapter, we learned about what a blockchain is and how it functions. In addition, we learned how to set up a project to build our blockchain. In this chapter, you will begin building the blockchain and all of its functionalities. First, let's create the blockchain data structure using a constructor function, and then we'll add a lot of different types of functionalities to our blockchain by adding different methods to its prototype.
We're then going to give the blockchain certain functionalities, such as creating new blocks and transactions, as well as the ability to hash data and blocks. We'll also give it the ability to do a proof of work and many other functionalities that a blockchain should be able to do. We'll then make sure that the blockchain is fully functional by testing the added functionalities as we progress.
By building each piece of the blockchain step by step, you will gain a better understanding of how blockchain actually works under the hood. You may also realize that once you dive into it, creating a blockchain is not as complicated as it sounds.
In this chapter, we'll cover the following topics:
  • Learning how to create a Blockchain constructor function
  • Building and testing various methods such as createNewBlock, createNewTransaction, and hashBlock to add functionalities to the blockchain
  • Understanding what proof of work is and learning how to implement it for our blockchain
  • Creating and testing a genesis block
So, let's get started!

Before we get building...

Before we get into building the blockchain, there are two crucial concepts that we need to familiarize ourselves with. These important concepts are as follows:
  • The JavaScript constructor function
  • The prototype object

An explanation of the JavaScript constructor function

Becoming familiar with the constructor function is important as we'll be using it to build our blockchain data structure. By now, you must be wondering what a constructor function is and what it actually does.
A constructor function is simply a function that creates an object class and allows you to easily create multiple instances of that particular class. What this actually means is that the constructor function allows you to create a lot of objects very quickly. All of these objects that are created will have the same properties and functionalities because they are all part of the same class. Now, all of this might seem a little bit confusing when you hear it for the first time, but don't worry — we'll try to understand what a constructor function is with the help of an example.
Let's take Facebook, for example. Facebook has over one-and-a half billion users, which are all objects of the same class and have similar properties such as name, email, password, birthday, and so on. For our example, let's assume that we are building the Facebook website and want to create a bunch of different users for it. Let's do this by creating a User constructor function.
To learn and explore the constructor functions, let's use the Google Chrome console. We can access the console by going to Google Chrome and simply pressing command + option + J for Mac users and Ctrl + Shift + I for Windows users. Alternatively, we can simply go to the menu option, go to More Tools, and then select the Developer Tools option, as shown in the following screenshot:
Following the aforementioned steps will open the console for you, as shown in the following screenshot:
The constructor function that we'll be coding in this example will allow us to create multiple users or multiple user objects that will have the same properties and functionalities. The code to create this User constructor function begins by defining it as follows:
function User() { 

}
Inside of the parentheses, (), let's pass the properties that we want each of our User objects to have. We're going to pass properties such as firstName, lastName, age, and gender because we want all of our user objects to have these components.
We then assign these parameters to our User objects by using the this keyword, as shown in the following block of code:
This is how we define the constructor function in JavaScript. Now, reading through the preceding code block, you might be wondering what we did and what the this keyword is all about.
We're going to use this constructor function to create a lot of user objects. The this keyword is simply referring to each of the user objects that we're going to create. This might all seem a bit overwhelming right now, but let's run through a couple of examples and try to gain more clarity on it.
Let's begin using our User constructor function. To make some User objects, also known as User instances, follow these steps:
  1. The first user that we are going to create let's call it user1 will be defined as follows:
var user1 = new User('John','Smith',26,'male');
In the preceding code, you may have noticed that we used the new keyword to invoke our constructor function and make a user object, which is how we get our constructor function to work.
  1. Then press Enter, and user1 is in the system. Now, if we type user1 in the console, we'll be able to see what we just created in the previous step:
In the preceding output screenshot, we can see that user1 is an object of the User class. We can also see that user1 has a firstName of John, a lastName of Smith, an age of 26, and gender of male because these are the parameters that we passed into the constructor function.
  1. For clarity, try adding one more user. This time, we'll create another user called user200 and pass in into the new User ( ) function with the user's properties, such as a first name of Jill, a last name of Robinson, an age of 25, and a female gender:
var user200 = new User('Jill', 'Robinson', 25, 'female');
  1. By pressing Enter, our new user200 will be in the system. Now, if we type user200 into the console and press Enter, we'll see the following output:
In the preceding output, we can see that user200 is an object of the User class, just like user1, and that she has a first name of Jill, a last name of Robinson, an age of 25, and a female gender because these were the parameters that we passed into our constructor function.
Now, you might be wondering how all of these properties that we mentioned got assigned correctly. This was all due to the this keyword that we mentioned earlier. When we create our constructor function, we use the this keyword to assign properties. When it comes to a constructor function, the this keyword does not refer to the function that it is in in our case, the User function. Instead, this refers to the object that will be created by the constructor function.
This signifies that if we use the constructor function to create an object, we must make sure that the property and their objects are first name, last name, age, and gender, or whenever you make your constructor function, set the firstName property as equal to the firstName parameter that is passed in, and do the same for the rest of the properties.
This is how a constructor function works and how the this keyword plays an important role in the constructor function.

Explanation of the prototype object

Another important concept that we need to discuss before getting into coding our blockchain data structure is the prototype object. The prototype object is simply an object that multiple other objects can refer to in order to get any information or functionality that they need. For our example, which we discussed in the previous section, each of our constructor functions will have a prototype that all of their instances will be able to refer to. Let's try to...

Table of contents

  1. Title Page
  2. Copyright and Credits
  3. About Packt
  4. Contributors
  5. Preface
  6. Setting up the Project
  7. Building a Blockchain
  8. Accessing the Blockchain through an API
  9. Creating a Decentralized Blockchain Network
  10. Synchronizing the Network
  11. Consensus Algorithms
  12. Block Explorer
  13. In conclusion...
  14. Other Books You May Enjoy

Frequently asked questions

Yes, you can cancel anytime from the Subscription tab in your account settings on the Perlego website. Your subscription will stay active until the end of your current billing period. Learn how to cancel your subscription
No, books cannot be downloaded as external files, such as PDFs, for use outside of Perlego. However, you can download books within the Perlego app for offline reading on mobile or tablet. Learn how to download books offline
Perlego offers two plans: Essential and Complete
  • Essential is ideal for learners and professionals who enjoy exploring a wide range of subjects. Access the Essential Library with 800,000+ trusted titles and best-sellers across business, personal growth, and the humanities. Includes unlimited reading time and Standard Read Aloud voice.
  • Complete: Perfect for advanced learners and researchers needing full, unrestricted access. Unlock 1.4M+ books across hundreds of subjects, including academic and specialized titles. The Complete Plan also includes advanced features like Premium Read Aloud and Research Assistant.
Both plans are available with monthly, semester, or annual billing cycles.
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 990+ topics, we’ve got you covered! Learn about our mission
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more about Read Aloud
Yes! You can use the Perlego app on both iOS and Android devices to read anytime, anywhere — even offline. Perfect for commutes or when you’re on the go.
Please note we cannot support devices running on iOS 13 and Android 7 or earlier. Learn more about using the app
Yes, you can access Learn Blockchain Programming with JavaScript by Eric Traub in PDF and/or ePUB format, as well as other popular books in Computer Science & Entreprise Applications. We have over one million books available in our catalogue for you to explore.