Building Blockchain Projects Develop real-time practical DApps using Ethereum and JavaScript Narayan Prusty BIRMINGHAM - MUMBAI 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 Author Copy Editor Narayan Prusty Stuti Srivastava Reviewers Project Coordinator Imran Bahsir Nidhi Joshi Daniel Kraft Gaurang Torvekar Commissioning Editor Proofreader Veena Pagare Safis Editing Acquisition Editor Indexer Vinay Argekar Pratik Shirodkar Content Development Editor Graphics Mayur Pawanikar Tania Dutta Technical Editor Production Coordinator Prasad Ramesh Melwyn Dsa 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 [email protected] 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 [email protected]. 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 1 Chapter 1: Understanding Decentralized Applications 6 What is a DApp? 7 Advantages of decentralized applications 8 Disadvantages of decentralized applications 8 Decentralized autonomous organization 9 User identity in DApps 9 User accounts in DApps 11 Accessing the centralized apps 11 Internal currency in DApps 12 Disadvantages of internal currency in DApps 13 What are permissioned DApps? 13 Popular DApps 13 Bitcoin 14 What is a ledger? 14 What is blockchain? 14 Is Bitcoin legal? 15 Why would someone use Bitcoin? 15 Ethereum 15 The Hyperledger project 16 IPFS 16 How does it work? 17 Filecoin 17 Namecoin 18 .bit domains 18 Dash 19 Decentralized governance and budgeting 20 Decentralized service 20 BigChainDB 21 OpenBazaar 21 Ripple 22 Summary 23 Chapter 2: Understanding How Ethereum Works 24 Overview of Ethereum 25 Ethereum accounts 25 Transactions 26 Consensus 26 Timestamp 28 Nonce 28 Block time 29 Forking 32 Genesis block 32 Ether denominations 33 Ethereum virtual machine 33 Gas 34 Peer discovery 35 Whisper and Swarm 35 Geth 36 Installing geth 36 OS X 37 Ubuntu 37 Windows 37 JSON-RPC and JavaScript console 37 Sub-commands and options 38 Connecting to the mainnet network 38 Creating a private network 38 Creating accounts 38 Mining 39 Fast synchronization 39 Ethereum Wallet 40 Mist 41 Weaknesses 43 Sybil attack 43 51% attack 43 Serenity 43 Payment and state channels 44 Proof-of-stake and casper 44 Sharding 45 Summary 45 Chapter 3: Writing Smart Contracts 46 Solidity source files 46 The structure of a smart contract 47 Data location 48 What are the different data types? 49 Arrays 50 [ ii ]