Building Blockchain Projects
2/5
()
About this ebook
- Create powerful, end-to-end applications for Blockchain using Ethereum
- Write your first program using the Solidity programming language
- Change the way you think and design your applications by using the all new database-Blockchain
This book is for JavaScript developers who now want to create tamper-proof data (and transaction) applications using Blockchain and Ethereum. Those who are interested in cryptocurrencies and the logic and database empowering it will find this book extremely useful.
Narayan Prusty
Narayan Prusty is a web and mobile app developer. He specializes in WordPress, HTML5, JavaScript, PHP, Solr, and Cordova. He has been learning and building applications using these technologies for many years. He is the founder of QScutter.com, which provides courses on various application development topics and has more than 10,000 subscribers worldwide. His personal blog, http://www.QNimate.com, is among the top Intel XDK and WordPress blogs. He also works as a consultant and freelance developer for many companies worldwide. Visit him online at http://www.twitter.com/narayanprusty.
Read more from Narayan Prusty
Building Blockchain Projects: Building decentralized Blockchain applications with Ethereum and Solidity Rating: 0 out of 5 stars0 ratingsReact: Building Modern Web Applications Rating: 5 out of 5 stars5/5Blockchain for Enterprise: Build scalable blockchain applications with privacy, interoperability, and permissioned features Rating: 0 out of 5 stars0 ratingsAdvanced Blockchain Development: Build highly secure, decentralized applications and conduct secure transactions Rating: 0 out of 5 stars0 ratingsModern JavaScript Applications Rating: 0 out of 5 stars0 ratings
Related to Building Blockchain Projects
Related ebooks
Blockchain for Business with Hyperledger Fabric: A complete guide to enterprise blockchain implementation using Hyperledger Fabric Rating: 0 out of 5 stars0 ratingsBuilding Ethereum Dapps: Decentralized applications on the Ethereum blockchain Rating: 5 out of 5 stars5/5Ethereum Blockchain Developer - The Bootcamp Rating: 5 out of 5 stars5/5The Solidity Programmer's Handbook Rating: 0 out of 5 stars0 ratingsSolidity Smart Contracts: Build DApps In The Ethereum Blockchain Rating: 4 out of 5 stars4/5Mastering Blockchain Rating: 5 out of 5 stars5/5Blockchain: A Practical Guide to Developing Business, Law, and Technology Solutions Rating: 5 out of 5 stars5/5Blockchain: Real-World Applications And Understanding Rating: 4 out of 5 stars4/5Blockchain Unlocked: Business Transformation with Blockchain Technology Rating: 0 out of 5 stars0 ratingsSolidity A Complete Guide Rating: 0 out of 5 stars0 ratingsBlockchain: Step By Step Guide To Understanding The Blockchain Revolution And The Technology Behind It Rating: 5 out of 5 stars5/5Blockchain in Action Rating: 0 out of 5 stars0 ratingsMastering Ethereum And Blockchain - A Beginner's Guide To Start Making Money With Ethereum And Blockchain Rating: 3 out of 5 stars3/5Blockchain: The complete guide to understanding Blockchain Technology for beginners in record time Rating: 4 out of 5 stars4/5Hashgraph VS Blockchain: The Future of Cryptocurrency Rating: 0 out of 5 stars0 ratingsBlockchain: An Illustrated Guidebook to Understanding Blockchain Rating: 4 out of 5 stars4/5Blockchain Foundations: For the Internet of Value Rating: 0 out of 5 stars0 ratingsBlockchain Fundamentals for Web 3.0: - Rating: 0 out of 5 stars0 ratingsThe Business Blockchain: Promise, Practice, and Application of the Next Internet Technology Rating: 4 out of 5 stars4/5Ethereum: A New World of Possibilities Rating: 5 out of 5 stars5/5Blockchain: A Guide to Understanding Blockchain Rating: 0 out of 5 stars0 ratingsSmart Contract Development with Solidity and Ethereum: Building Smart Contracts with the Azure Blockchain Rating: 0 out of 5 stars0 ratingsBlockchain: The Next Everything Rating: 4 out of 5 stars4/5Token Economy: Money, NFTs & DEFI Rating: 0 out of 5 stars0 ratingsBlockchain Technologies Rating: 0 out of 5 stars0 ratings
Computers For You
Algorithms to Live By: The Computer Science of Human Decisions Rating: 4 out of 5 stars4/5Data Analytics for Beginners: Introduction to Data Analytics Rating: 4 out of 5 stars4/5Creating Online Courses with ChatGPT | A Step-by-Step Guide with Prompt Templates Rating: 4 out of 5 stars4/5Mastering ChatGPT: 21 Prompts Templates for Effortless Writing Rating: 4 out of 5 stars4/5Black Holes: The Key to Understanding the Universe Rating: 5 out of 5 stars5/5Storytelling with Data: Let's Practice! Rating: 4 out of 5 stars4/5Django Building Dynamic Website With Django : A Complete Step By Step Guide To Learn to Build Modern Web Application with a Python Rating: 0 out of 5 stars0 ratingsPython for Beginners: A Crash Course to Learn Python Programming in 1 Week Rating: 0 out of 5 stars0 ratingsThe Alignment Problem: How Can Machines Learn Human Values? Rating: 4 out of 5 stars4/5The Unaccountability Machine: Why Big Systems Make Terrible Decisions - and How The World Lost its Mind Rating: 0 out of 5 stars0 ratingsPython for Finance Cookbook: Over 50 recipes for applying modern Python libraries to financial data analysis Rating: 0 out of 5 stars0 ratingsGrokking Algorithms: An illustrated guide for programmers and other curious people Rating: 4 out of 5 stars4/5Advances in Financial Machine Learning Rating: 5 out of 5 stars5/5How Do I Do That In InDesign? Rating: 5 out of 5 stars5/5Learn Algorithmic Trading: Build and deploy algorithmic trading systems and strategies using Python and advanced data analysis Rating: 0 out of 5 stars0 ratingsNarrative Design for Indies: Getting Started Rating: 4 out of 5 stars4/5SQL QuickStart Guide: The Simplified Beginner's Guide to Managing, Analyzing, and Manipulating Data With SQL Rating: 4 out of 5 stars4/5Get Into UX: A foolproof guide to getting your first user experience job Rating: 4 out of 5 stars4/5Deep Learning with PyTorch Rating: 5 out of 5 stars5/5Artificial Intelligence: The Complete Beginner’s Guide to the Future of A.I. Rating: 4 out of 5 stars4/5Learn SAP MM in 24 Hours Rating: 0 out of 5 stars0 ratingsPractical Data Analysis Rating: 4 out of 5 stars4/5ITIL Foundation Essentials ITIL 4 Edition - The ultimate revision guide, second edition Rating: 5 out of 5 stars5/5Blender 3D Basics Beginner's Guide Second Edition Rating: 5 out of 5 stars5/5
Reviews for Building Blockchain Projects
1 rating0 reviews
Book preview
Building Blockchain Projects - Narayan Prusty
Title Page
Building Blockchain Projects
Develop real-time practical DApps using Ethereum and JavaScript
Narayan Prusty
BIRMINGHAM - MUMBAI
Copyright
Building Blockchain Projects
Copyright © 2017 Packt Publishing
All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews.
Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book.
Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information.
First published: April 2017
Production reference: 1240417
Published by Packt Publishing Ltd.
Livery Place
35 Livery Street
Birmingham
B3 2PB, UK.
ISBN 978-1-78712-214-7
www.packtpub.com
Credits
About the Author
Narayan Prusty is a full-stack developer, with five years of experience in the field. He specializes in Blockchain and JavaScript. His commitment has led him to build scalable products for startups, the government, and enterprises across India, Singapore, USA, and UAE.
At present, Ethereum, Bitcoin, Hyperledger, IPFS, Ripple, and so on are some of the things he uses on a regular basis to build decentralized applications. Currently, he is a full-time Blockchain SME (Subject-Matter Expert) at Emirates National Bank of Dubai.
He has already written two books on JavaScript titled Learning ECMAScript 6 and Modern JavaScript Applications. Both these books were reviewed and published by Packt.
He starts working on something immediately if he feels it’s exciting and solves real work problems. He built an MP3 search engine at the age of 18, and since then, he has built various other applications, which are used by people around the globe. His ability to to build scalable applications from top to bottom is what makes him special.
Currently, he is on a mission to make things easier, faster, and cheaper using the blockchain technology. Also, he is looking at possibilities to prevent corruptions, fraud, and to bring transparency to the world using blockchain technology.
You can learn more from him from his blog http://qnimate.com and you can reach him out at LinkedIn https://www.linkedin.com/in/narayanprusty/.
About the Reviewers
Imran Bashir has an M.Sc. degree in Information Security from Royal Holloway, University of London, and has a background in software development, solution architecture, infrastructure management, and IT service management. He is also a member of the Institute of Electrical and Electronics Engineers (IEEE) and the British Computer Society (BCS). Imran has sixteen years of experience in public and financial sector. He had worked on large-scale IT projects for the public sector before moving to the financial services industry. Since then, he worked in various technical roles for different financial companies in Europe’s financial capital, London. He is currently working for an investment bank in London as Vice President in the technology department.
Daniel Kraft has studied mathematics and physics and holds a PhD degree in applied mathematics from the University of Graz in Austria. He has been involved in development with cryptocurrencies since 2013, has been the lead developer and chief scientist for both Namecoin and Huntercoin since 2014, and has published two research papers about cryptocurrency in peer-reviewed journals. He works as a software engineer and is a cofounder of Crypto Realities Ltd, a start-up that works on building decentralized multiplayer game worlds with blockchain technology.
Gaurang Torvekar has a master's degree in Information Systems from Singapore Management University. He is the cofounder and CTO of Attores, a Smart Contracts as a Service company, based in Singapore. He has extensive experience in Ethereum and Hyperledger application development. He has been a speaker at several blockchain conferences, conducted many hands on blockchain courses in Polytechnics in Singapore, and is also a Blockchain mentor at Angelhack.
www.PacktPub.com
For support files and downloads related to your book, please visit www.PacktPub.com.
Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.com and as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details.
At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks.
https://www.packtpub.com/mapt
Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career.
Why subscribe?
Fully searchable across every book published by Packt
Copy and paste, print, and bookmark content
On demand and accessible via a web browser
Customer Feedback
Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at https://www.amazon.com/dp/178712214X.
If you'd like to join our team of regular reviewers, you can e-mail us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Table of Contents
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Reader feedback
Customer support
Downloading the example code
Downloading the color images of this book
Errata
Piracy
Questions
Understanding Decentralized Applications
What is a DApp?
Advantages of decentralized applications
Disadvantages of decentralized applications
Decentralized autonomous organization
User identity in DApps
User accounts in DApps
Accessing the centralized apps
Internal currency in DApps
Disadvantages of internal currency in DApps
What are permissioned DApps?
Popular DApps
Bitcoin
What is a ledger?
What is blockchain?
Is Bitcoin legal?
Why would someone use Bitcoin?
Ethereum
The Hyperledger project
IPFS
How does it work?
Filecoin
Namecoin
.bit domains
Dash
Decentralized governance and budgeting
Decentralized service
BigChainDB
OpenBazaar
Ripple
Summary
Understanding How Ethereum Works
Overview of Ethereum
Ethereum accounts
Transactions
Consensus
Timestamp
Nonce
Block time
Forking
Genesis block
Ether denominations
Ethereum virtual machine
Gas
Peer discovery
Whisper and Swarm
Geth
Installing geth
OS X
Ubuntu
Windows
JSON-RPC and JavaScript console
Sub-commands and options
Connecting to the mainnet network
Creating a private network
Creating accounts
Mining
Fast synchronization
Ethereum Wallet
Mist
Weaknesses
Sybil attack
51% attack
Serenity
Payment and state channels
Proof-of-stake and casper
Sharding
Summary
Writing Smart Contracts
Solidity source files
The structure of a smart contract
Data location
What are the different data types?
Arrays
Strings
Structs
Enums
Mappings
The delete operator
Conversion between elementary types
Using var
Control structures
Creating contracts using the new operator
Exceptions
External function calls
Features of contracts
Visibility
Function modifiers
The fallback function
Inheritance
The super keyword
Abstract contracts
Libraries
Using for
Returning multiple values
Importing other Solidity source files
Globally available variables
Block and transaction properties
Address type related
Contract related
Ether units
Proof of existence, integrity, and ownership contract
Compiling and deploying contracts
Summary
Getting Started with web3.js
Introduction to web3.js
Importing web3.js
Connecting to nodes
The API structure
BigNumber.js
Unit conversion
Retrieving gas price, balance, and transaction details
Sending ether
Working with contracts
Retrieving and listening to contract events
Building a client for an ownership contract
The project structure
Building the backend
Building the frontend
Testing the client
Summary
Building a Wallet Service
Difference between online and offline wallets
hooked-web3-provider and ethereumjs-tx libraries
What is a hierarchical deterministic wallet?
Introduction to key derivation functions
Introduction to LightWallet
HD derivation path
Building a wallet service
Prerequisites
Project structure
Building the backend
Building the frontend
Testing
Summary
Building a Smart Contract Deployment Platform
Calculating a transaction's nonce
Introducing solcjs
Installing solcjs
solcjs APIs
Using a different compiler version
Linking libraries
Updating the ABI
Building a contract deployment platform
The project structure
Building the backend
Building the frontend
Testing
Summary
Building a Betting App
Introduction to Oraclize
How does it work?
Data sources
Proof of authenticity
Pricing
Getting started with the Oraclize API
Setting the proof type and storage location
Sending queries
Scheduling queries
Custom gas
Callback functions
Parsing helpers
Getting the query price
Encrypting queries
Decrypting the data source
Oraclize web IDE
Working with strings
Building the betting contract
Building a client for the betting contract
Projecting the structure
Building the backend
Building the frontend
Testing the client
Summary
Building Enterprise Level Smart Contracts
Exploring ethereumjs-testrpc
Installation and usage
The testrpc command-line application
Using ethereumjs-testrpc as a web3 provider or as an HTTP server
Available RPC methods
What are event topics?
Getting started with truffle-contract
Installing and importing truffle-contract
Setting up a testing environment
The truffle-contract API
The contract abstraction API
Creating contract instances
The contract instance API
Introduction to truffle
Installing truffle
Initializing truffle
Compiling contracts
Configuration files
Deploying contracts
Migration files
Writing migrations
Unit testing contracts
Writing tests in JavaScript
Writing tests in Solidity
How to send ether to a test contract
Running tests
Package management
Package management via NPM
Package management via EthPM
Using contracts of packages within your contracts
Using artifacts of packages within your JavaScript code
Accessing a package's contracts deployed addresses in Solidity
Using truffle's console
Running external scripts in truffle's context
Truffle's build pipeline
Running an external command
Running a custom function
Truffle's default builder
Building a client
Truffle's server
Summary
Building a Consortium Blockchain
What is a consortium blockchain?
What is Proof-of-Authority consensus?
Introduction to parity
Understanding how Aura works
Getting parity running
Installing rust
Linux
OS X
Windows
Downloading, installing and running parity
Creating a private network
Creating accounts
Creating a specification file
Launching nodes
Connecting nodes
Permissioning and privacy
Summary
Preface
Blockchain is a decentralized ledger that maintains a continuously growing list of data records secured from tampering and revision. Every user is allowed to connect to the network, send new transactions to it, verify transactions, and create new blocks.
This book will teach you what Blockchain is, how it maintains data integrity, and how to create real-world Blockchain projects using Ethereum. With interesting real-world projects, you will know learn how to write smart contracts which run exactly as programmed without any chance of fraud, censorship or third-party interference, and build end-to-end applications for Blockchain. You will learn concepts such as cryptography in cryptocurrencies, ether security, mining, smart contracts, and solidity.
The blockchain is the main technical innovation of bitcoin, where it serves as the public ledger for bitcoin transactions.
What this book covers
Chapter 1, Understanding Decentralized Applications, will explain what DApps are and provide an an overview of how they work.
Chapter 2, Understanding How Ethereum Works, explains how Ethereum works.
Chapter 3, Writing Smart Contracts, shows how to write smart contracts and use geth's interactive console to deploy and broadcast transactions using web3.js.
Chapter 4, Getting Started with web3.js, introduces web3js and how to import, connect to geth, and explains use it in Node.js or client-side JavaScript.
Chapter 5, Building a Wallet Service, explains how to build a wallet service that users can create and manage Ethereum Wallets easily, even offline. We will specifically use the LightWallet library to achieve this.
Chapter 6, Building a Smart Contract Deployment Platform, shows how to compile smart contracts using web3.js and deploy it using web3.js and EthereumJS.
Chapter 7, Building a Betting App, explains how to use Oraclize to make HTTP requests from Ethereum smart contracts to access data from World Wide Web. We will also learn how to access files stored in IPFS, use the strings library to work with strings, and more.
Chapter 8, Building Enterprise Level Smart Contracts, explains how to use Truffle, which makes it easy to build enterprise-level DApps. We will learn about Truffle by building an alt-coin.
Chapter 9, Building a Consortium Blockchain, we will discuss consortium blockchain.
What you need for this book
You require Windows 7 SP1+, 8, 10 or Mac OS X 10.8+.
Who this book is for
This book is for JavaScript developers who now want to create tamper-proof data (and transaction) applications using Blockchain and Ethereum. Those who are interested in cryptocurrencies and the logic and database empowering it will find this book extremely useful.
Conventions
In this book, you will find a number of text styles that distinguish between different kinds of information. Here are some examples of these styles and an explanation of their meaning.
Code words in text, database table names, folder names, filenames, file extensions, pathnames, dummy URLs, user input, and Twitter handles are shown as follows: Then, run the app using the node app.js command inside the Final directory.
A block of code is set as follows:
Any command-line input or output is written as follows:
New terms and important words are shown in bold. Words that you see on the screen, for example, in menus or dialog boxes, appear in the text like this: Now select the same file again and click on the Get Info button.
Warnings or important notes appear in a box like this.
Tips and tricks appear like this.
Reader feedback
Feedback from our readers is always welcome. Let us know what you think about this book-what you liked or disliked. Reader feedback is important for us as it helps us develop titles that you will really get the most out of.
To send us general feedback, simply e-mail feedback@packtpub.com, and mention the book's title in the subject of your message.
If there is a topic that you have expertise in and you are interested in either writing or contributing to a book, see our author guide at www.packtpub.com/authors.
Customer support
Now that you are the proud owner of a Packt book, we have a number of things to help you to get the most from your purchase.
Downloading the example code
You can download the example code files for this book from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.
You can download the code files by following these steps:
Log in or register to our website using your e-mail address and password.
Hover the mouse pointer on the SUPPORT tab at the top.
Click on Code Downloads & Errata.
Enter the name of the book in the Search box.
Select the book for which you're looking to download the code files.
Choose from the drop-down menu where you purchased this book from.
Click on Code Download.
Once the file is downloaded, please make sure that you unzip or extract the folder using the latest version of:
WinRAR / 7-Zip for Windows
Zipeg / iZip / UnRarX for Mac
7-Zip / PeaZip for Linux
The code bundle for the book is also hosted on GitHub at https://github.com/PacktPublishing/Building-Blockchain-Projects. We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
Downloading the color images of this book
We also provide you with a PDF file that has color images of the screenshots/diagrams used in this book. The color images will help you better understand the changes in the output. You can download this file from https://www.packtpub.com/sites/default/files/downloads/BuildingBlockchainProjects_ColorImages.pdf.
Errata
Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you find a mistake in one of our books-maybe a mistake in the text or the code-we would be grateful if you could report this to us. By doing so, you can save other readers from frustration and help us improve subsequent versions of this book. If you find any errata, please report them by visiting http://www.packtpub.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details of your errata. Once your errata are verified, your submission will be accepted and the errata will be uploaded to our website or added to any list of existing errata under the Errata section of that title.
To view the previously submitted errata, go to https://www.packtpub.com/books/content/support and enter the name of the book in the search field. The required information will appear under the Errata section.
Piracy
Piracy of copyrighted material on the Internet is an ongoing problem across all media. At Packt, we take the protection of our copyright and licenses very seriously. If you come across any illegal copies of our works in any form on the Internet, please provide us with the location address or website name immediately so that we can pursue a remedy.
Please contact us at copyright@packtpub.com with a link to the suspected pirated material.
We appreciate your help in protecting our authors and our ability to bring you valuable content.
Questions
If you have a problem with any aspect of this