40,81 €
Implement decentralized blockchain applications to build scalable Dapps
Key Features
Book Description
The Blockchain is a revolution promising a new world without middlemen. Technically, it is an immutable and tamper-proof distributed ledger of all transactions across a peer-to-peer network. With this book, you will get to grips with the blockchain ecosystem to build real-world projects.
This book will walk you through the process of building multiple blockchain projects with different complexity levels and hurdles. Each project will teach you just enough about the field's leading technologies, Bitcoin, Ethereum, Quorum, and Hyperledger in order to be productive from the outset. As you make your way through the chapters, you will cover the major challenges that are associated with blockchain ecosystems such as scalability, integration, and distributed file management. In the concluding chapters, you'll learn to build blockchain projects for business, run your ICO, and even create your own cryptocurrency. Blockchain by Example also covers a range of projects such as Bitcoin payment systems, supply chains on Hyperledger, and developing a Tontine Bank Every is using Ethereum.
By the end of this book, you will not only be able to tackle common issues in the blockchain ecosystem, but also design and build reliable and scalable distributed systems.
What you will learn
Who this book is for
If you are keen on learning how to build your own blockchain decentralized applications from scratch, then this book is for you. It explains all the basic concepts required to develop intermediate projects and will teach you to implement the building blocks of a blockchain ecosystem.
Das E-Book können Sie in Legimi-Apps oder einer beliebigen App lesen, die das folgende Format unterstützen:
Seitenzahl: 504
Veröffentlichungsjahr: 2018
Copyright © 2018 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 authors, nor Packt Publishing or its dealers and distributors, will be held liable for any damages caused or alleged to have been 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.
Commissioning Editor: Sunith ShettyAcquisition Editor: Porous GodhaaContent Development Editor: Karan ThakkarTechnical Editor: Dinesh PawarCopy Editor:Safis EditingProject Coordinator:Namrata SwettaProofreader: Safis EditingIndexer: Priyanka DhadkeGraphics: Jisha ChirayilProduction Coordinator: Shantanu Zagade
First published: November 2018
Production reference: 1301118
Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK.
ISBN 978-1-78847-568-6
www.packtpub.com
Mapt is an online digital library that gives you full access to over 5,000 books and videos, as well as industry leading tools to help you plan your personal development and advance your career. For more information, please visit our website.
Spend less time learning and more time coding with practical eBooks and Videos from over 4,000 industry professionals
Improve your learning with Skill Plans built especially for you
Get a free eBook or video every month
Mapt is fully searchable
Copy and paste, print, and bookmark content
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.packt.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.packt.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.
From the time Badr Bellaj first told me about his intention to write a book about blockchain, I was highly supportive of his endeavor. I even managed to suggest topics that I, as a veteran developer, still needed to understand. A year later, after reading this book, my expectations have been surpassed and I could not be happier. Therefore, it's my pleasure to provide this foreword and to recommendBellaj's book, Blockchain by Example.
It is generally agreed that blockchain is a disruptive technology that has shaken the IT scene. Consequently, it has increasingly drawn the interest of a lot of developers. However, according to my experience, it's hard for newcomers to find a helpful guide that explainsblockchain in the easiest possible terms, and teaches with concrete examples how to use major blockchain solutions to build projects.
This book is intended to change this situation and acts as an introduction to blockchain technology from a developer viewpoint. It's an undertaking of great potential value, and Bellaj and his coauthors have done it justice.
In fact, this book will help you, step by step, to build realistic projects from scratch using different concepts and technologies, namely Bitcoin, Ethereum, Quorum, and Hyperledger. You'll find, among other topics, how a developer can create a Bitcoin-like coin, run an ICO, and implement privacy-preserving solution in business.
This book isn't just for absolute beginners. It's also a good book for introducing experienced programmers to blockchain technologies or even bringing up to speed developers already familiar with these technologies. It helps if you know a bit about blockchain when you step in, but if you don't, hang on and you should pick up the basic ideas as you go along.
Bellaj is an educator, and a darn good one. He wants us to acquire practical skills instead understanding only the superficial concepts! He knows that if you and I are ready to learn, we have to practice: we have to do the work. In his chapters, he will continually challenge you to propose new features for the presented project. We are well advised to try to implement what he suggests and to create pull requests.
I have enjoyed the book and found it valuable. I think you will, too. Enjoy!
Sam Hauer
Cofounder of NSApps
Bellaj Badr is an experienced security and software engineer who loves blockchain with a passion. Currently, he is the CTO at Mchain, a blockchain start-up that develops blockchain solutions for companies.
Alongside his role as CTO, he acts as technical consultant, offering strategic and technical consulting to many companies worldwide. Aside from this, he is involved in many blockchain projects involving the establishment of new blockchain business-oriented protocols. Badr is a frequent speaker at developer conferences and is father to two angels.
Richard Horrocks is a freelance Ethereum and full-stack developer based in the UK, and holds a BA and MSc in natural sciences from the University of Cambridge. He worked for many years as a technical lead for Cisco Systems, where he worked on the operating systems of carrier-grade routing hardware, before leaving the world of IT to work as an English teacher.
The advent of cryptocurrency piqued his interest sufficiently to lead him back to IT, and, since 2015, he has been working with Ethereum and other cryptocurrencies. His specialist interests are cryptoeconomics and incentive layers, with a particular focus on mechanism design and token engineering.
When not in front of a computer, he enjoys yoga and falling off motorbikes.
Xun (Brian) Wu is the founder and CEO of SmartChart. He has 16+ years of extensive, hands-on, design and development experience with blockchain, big data, cloud, UI, and system infrastructure. He has coauthored a number of books, including Seven NoSQL Databases in a Week, Hyperledger Cookbook, and Blockchain Quick Start Guide. He has been a technical reviewer on more than 50 technical books for Packt. He serves as a board adviser for several blockchain start-ups and owns several patents on blockchain. Brian also holds an NJIT computer science masters degree. He lives in New Jersey with his two beautiful daughters, Bridget and Charlotte.
Karthikeyan Sukumaran has been involved in blockchain research and development for the past three years and has over a decade of industry experience in connection with mobile and web platforms. He has been a CEO for his own blockchain start-up, where he architected multiple blockchain projects for various consulting, automobile, supply chain, logistics, and financial companies. He is also a renowned speaker within the Indian blockchain community. Currently, Karthikeyan is an associate director (DLT Labs—Blockchain R&D) for The Depository Trust and Clearing Corporation (DTCC), India.
Aafaf Ouaddah is a veteran security engineer, currently pursuing a PhD involving blockchain. She has wide-ranging experience in distributed systems. Currently, she is a lead researcher investigating security and privacy in distributed systems, blockchain, IoT and fog computing. She has presented more than 10 research papers at various conferences and workshops and has published in reputed international journals with IEEE, Springer, and Elsevier.
If you're interested in becoming an author for Packt, please visit authors.packtpub.com and apply today. We have worked with thousands of developers and tech professionals, just like you, to help them share their insight with the global tech community. You can make a general application, apply for a specific hot topic that we are recruiting an author for, or submit your own idea.
Title Page
Copyright and Credits
Blockchain By Example
About Packt
Why subscribe?
Packt.com
Foreword
Contributors
About the authors
About the reviewers
Packt is searching for authors like you
Preface
Who this book is for
What this book covers
To get the most out of this book
Download the example code files
Download the color images
Conventions used
Get in touch
Reviews
Say Hello to Blockchain
The emergence of blockchain and cryptocurrency
From virtual currencies to cryptocurrency
The invention of bitcoin
What is blockchain?
Blocks
Transactions
Smart contracts
Interact with the blockchain
Getting started
Running a bitcoin client for the first time 
Synchronizing the blockchain
Running Bitcoin Core
Running Electrum
Method 1 – Building a raw transaction using Bitcoin Core
Funding our address
Unspent transaction output
Creating the transaction
Transaction structure
Signing the transaction 
Sending the transaction
Retrieving your message online from the blockchain
Using the local blockchain
Method 2 – build a raw bitcoin transaction in JavaScript
Preparation
Let's code
Types of blockchains
Classification of blockchains
Summary
Building a Bitcoin Payment System
What is Bitcoin?
Why choose Bitcoin as a platform?
Getting started with Bitcoin
Setting up a Regtest environment
First commands
Transactions in bitcoin 
Mining 
Bitcoin scripting
Building a P2PKH script using JavaScript
Building a custom script
Building a payment gateway
Project description
Bitcoin payment protocol overview
Prerequisites
Project setup
Certificate management
Merchant side
Building a payment request URI
Routing
Checkout view
Proceeding with payment
Requesting payment details
Receiving and acknowledging payment
Invoicing
Client side
Previewing your application
Payment protocol using BitcoinJ
Prerequisites
BitcoinJ installation
BitcoinJ client
Bitcoin wallet configuration
Requesting payment
Sending a payment
Testing the code
Bitcoin smart contracts
What is Rootstock?
Rootstock setup
Interactions with RSK
Account settings
Writing your first bitcoin smart contract
Deploying the contract
Interacting  with the contract
Summary
Building Your Own Cryptocurrency
Compiling Bitcoin from source
Preparing your build system
Installing dependencies
Bitcoin-qt - Qt5 GUI for Bitcoin
Cloning the Bitcoin source code
Building Bitcoin Core
Checking your build worked
New cryptocurrency – Readercoin
Cloning Bitcoin
Readercoin rebranding
Changing the ports
Changing pchMessageStart
Genesis block
New pszTimestamp
New nonce, epoch time, and nbits
New genesis hash
Editing the Merkle root
Removing the seed nodes
Checkpoints
ChainTxData
Rewarding
Halving
Total supply
POW parameters – target
New block time
Difficulty adjustment time interval
Difficulty adjustment rate
Time maturity
Block size
BIPs: Bitcoin Improvement Proposals
Compiling and testing
Design and graphics (Qt wallet)
Redesign of the Bitcoin-Qt wallet
Graphical interface addition
Building a Readercoin network
Readercoin mining
Sending transactions
Building a transaction and block explorer
Iquidus setup requirements
Creating Iquidus's database
Installing Iquidus
Iquidus configuration
Syncing databases with the blockchain
Wallet preparation
Starting the explorer
Building an exchange market
Summary
Peer-to-Peer Auctions in Ethereum
Introduction to Ethereum
Ethereum virtual machine and smart contracts
Ether and gas
Your first smart contract
What's a DApp?
DApp architecture
Building an auction DApp 
Auction description
Auction contract in Solidity
Contract code analysis
State variables
Variable state types
Visibility and access modifiers
Enumerations
Arrays
Mappings
Structures
Functions
Modifiers
Condition and error verification
Events
Inheritance
Constructors
Time in Solidity
Special variables and functions
The fallback function
Function overloading
The bidding function
Canceling an auction
Withdrawing bids
Contract destruction
Remix IDE
Bidding form in web3.js
Introduction to the web3.js API
Step 1 – talking to the blockchain
Step 2 – interaction with the smart contract
The ABI
Call versus send transactions
Invoking contract methods via a call
Invoking contract methods via a transaction
Callbacks
Reading state variables
Watching events
Indexing events and filtering
Numbers in Ethereum and floating point
Transaction status receipt
Deployment environment – multiple ways to enjoy!
Option 1 – Ganache
Option 2 – Testnet
Connecting MetaMask to Testnet
Option 3 – private network
Geth installation
 Creating new accounts
Genesis file
Node initialization
Connecting Ethereum nodes
RPC tests
Mining process
Contract deployment
Compiling and deploying contracts using solc
Proof of authority (POA) and difficulty adjustment
Option 1 – editing the Ethereum client code
Option 2 – POW
Running the auction DApp
Contract and transaction cost
How cost is calculated
Deployment cost
Function execution cost
Contract destruction cost
Potential optimization
 Solidity tips and tricks
Summary
Tontine Game with Truffle and Drizzle
Background
Prerequisites
Truffle quick start
Installing Truffle
Saying hello to Truffle
Running Truffle for the first time
Preparing the migration
Configuring Truffle
Compiling the contract
Migrating the contract
Setting up Ganache
The Tontine contract
General structure
UML model
Preparing the contracts
Cplayer as a CRUD contract
Smart contract data location
The CRUD data repository
CRUD – Create
CRUD – Read
Mapping iterations
CRUD – Update
Function overloading
CRUD – Delete
Tontine interfaces – Itontine
Interface implementation – Ctontine contract
Smart contract interaction
Contract instantiation
Contract creation
Reading the storage of a remote contract
Editing the storage of a remote contract
Joining the game
Calling remote functions
Option one – using the ABI
Option two – using low-level calls
Using call to send ether
The ping() method
The eliminate() method
The share_pension() method
Standard integer division
The claimReward method
The this keyword
Truffle unit tests
Preparing the testing environment
Migrating the contracts
Running Ganache
Preparing the tests
Testing addPlayer()
Testing findPlayer()
Testing removePlayer()
Testing Ctontine
Testing a payable function
Testing events
Testing claimReward()
Time manipulation
Running the tests
Testing with Solidity
Debugging with Truffle and Remix
Debugging with Truffle
Spotting the error
Breakpoints and watching values
Debugging with Remix
Attaching the debugger to the code
Watching values
Setting breakpoints
Frontend with Drizzle
Prerequisites
What is the Drizzle box?
Getting started with the Drizzle box
Running the demo
Starting the web UI
Hacking the Drizzle box
The game’s homepage
Trying the DApp
Connecting Ganache to MetaMask
Running the Tontine DApp
Showtime – ready to play?
Getting help
Summary
Blockchain-Based Futures System
Project presentation
Futures smart contract
Blockchain oracles
Web3j
Prerequisites
Setting up the Web3J Maven project
Installing web3j
Wallet creation
Java client 
The wrapper generator
Initializing web3j
Setting up Ethereum accounts
Deploying the contract
Interacting with smart contracts
Calling a contract function
 Calling view methods
Web3j events
Enhancement
Summary
Blockchains in Business
Public versus private and permissioned versus permissionless blockchains
Privacy and anonymity in Ethereum
Why are privacy and anonymity important?
The Ethereum Enterprise Alliance
Ethereum's licensing
Blockchain-as-a-Service
Quorum
Privacy
Higher performance and throughput
Permission and governance
The Quorum client
Quorum Node
Constellation
Our project
Prerequisites
Bringing up the network
Interacting with the network
Testing the network
Deploying the public contract
Deploying the private contract
Permissioning the network
Summary
Creating an ICO
What is an ICO?
Project setup
Token contracts
ERC-20 token standard
Name and symbol (optional)
totalSupply (required)
decimals (optional)
Transfer event (required)
balanceOf (required)
transfer() (required)
The constructor
Delegated transfer
allowance (required)
approve() (required)
Approval event (required)
transferFrom() (required)
The complete token contract
ERC-223 token standard
ERC-721 non-fungible token standard
Token sale contracts
Hard cap
Soft cap
Uncapped
Dutch auction
Reverse dutch auction
Considerations
Implementing our token sale contract
Constructor
Token supply
Buying tokens
Ending the sale
The complete token sale contract
Contract security
Known attack vectors
Integer overflow/underflow
Reentrancy
OpenZeppelin
Testing the code
The public test networks
Ropsten
Rinkeby
Kovan
Migrating the code
Testing our token contract
Testing our token sale contract
Deploying to a test network
Running Geth on Rinkeby
Configuring Truffle to work with Geth
The Truffle console
Provisioning the token sale contract
Verifying our contract code on Etherscan
Creating a frontend website
Setting up the frontend development
Frontend directory structure
index.html
app.js
Running the frontend code
Interacting with the frontend
Summary
Suggestions for further work
Distributed Storage IPFS and Swarm
Background
Swarm and IPFS
Installing IPFS
Installing Swarm
Hosting our frontend
Serving your frontend using IFPS
Using IPNS
Serving your frontend using Swarm
ENS 
IPFS file uploader project
Project setup
The web page
index.html
main.js
Summary
Supply Chain on Hyperledger
Food industry supply chain
Quick Hyperledger overview
Hyperledger Fabric 
End-to-end transaction flow
Hyperledger Composer
Setting up the development environment
Prerequisites 
Installing Hyperledger Fabric
Fabric's Chaincode
Writing Chaincode
The Init function
The Invoke function
Hyperledger data storage
The Query function
Error handling
Building a business network
Privacy in Hyperledger 
Define services in a compose file
Resources and profiles
Fabric channel and Genesis block 
Generate peers and orderer certificates
Start the Docker containers
Join the channel and install Chaincode
Chaincode compilation and deployment
Interacting with Chaincode
Running the project
Interacting over REST API/JSON-RPC
Setting up the development environment
Register users
Querying the Chaincode  from UI
Hyperledger Composer
Get Hyperledger Composer and Playground
Composer Playground
Summary
Letter of Credit (LC) Hyperledger
LC concepts and design
Development environment
Setting up the IDE
Getting Hyperledger Fabric running
Creating a composer Fabric application
Creating our first business network using Hyperledger Composer
Models definition
JavaScript transaction logic
Access control definition
LC business network
Initial LC model
Participant onboarding
Initial agreement
LC request
LC approval
LC advising
Goods shipping
Present document
Document delivery
Debit payment
Payment transfer
Pay the seller
LC closure
Deploying the LC
Deploying business network
Generating a REST server
Testing LC project
Participant onboarding
Initial agreement
LC request
LC approval
LC advising
Goods shipping
Present document
Deliver document
Debit payment
Payment transfer
Pay the seller
LC closure
Hyperledger Fabric on IBM Cloud
Summary
Other Books You May Enjoy
Leave a review - let other readers know what you think
Blockchain is a disruptive technology that promises to disrupt many sectors of the global economy. This innovative technology aims to revolutionize those industries that rely on intermediation and trust by shifting the paradigm away from the currently dominant centralized architectures and toward decentralization.
The past couple of years have seen the exponential growth of blockchain, which has evolved into multiple forms and currently comprises many different technologies and tools, some mature, others relatively new, all of which makes understanding and mastering the key ideas and concepts a difficult task.
This is where this book enters the picture. There are many books out there aiming to capitalize on blockchain's current popularity, but, in our opinion, a vast majority of them concentrate on blockchain's theoretical or speculative aspects: in other words, what blockchain could be used for in future, without providing any concrete details on how these things can be achieved in practice. This book is different: it's more pragmatic. From the outset, this book details what can be done with blockchain technology now, and how to do it, by guiding the reader through a series of in-depth, hands-on implementations.
The book is organized into several main parts. It starts by providing an introduction and high-level overview of blockchain's concepts, before moving on to present different use cases and practical implementations based on the Ethereum, Bitcoin, and Hyperledger blockchains.
This book is aimed at the blockchain novice, and aims to provide an easy way to learn how to conduct a blockchain-based project. It provides comprehensive coverage of the technical details associated with different blockchain solutions, and step-by-step guidance on implementing typical blockchain projects. By the end of the book, the reader will be able to build and maintain reliable and scalable distributed systems based on blockchain.
Chapter 1, Say Hello to Blockchain, serves as a general introduction, and explains the general concepts on which blockchain technology is based.
Chapter 2, Building a Bitcoin Payment System, introduces the specifics of Bitcoin by first building a customer-friendly payment system, before moving on to looking in more detail at the use of smart contracts on the Bitcoin blockchain.
Chapter 3, Building Your Own Cryptocurrency, builds on the general understanding of Bitcoin learned in the previous chapter to then create a new currency based on the Bitcoin code base.
Chapter 4, Peer-to-Peer Auction in Ethereum, introduces the basic features, concepts, and tools of Ethereum that are required to build a decentralized application. This chapter introduces the Solidity smart contract language that is used extensively in later chapters.
Chapter 5, Tontine Game with Truffle and Drizzle, builds on the previous Ethereum chapter to create a more complex decentralized application, leveraging more advanced features of Solidity, together with the Truffle development environment.
Chapter 6, Blockchain-Based Futures System, continues to build on the previous two Ethereum chapters, this time focusing on how smart contracts can interact with the outside world using oracles and third-party APIs.
Chapter 7, Blockchains in Business, introduces the idea of private enterprise blockchains and their use cases, before detailing how to implement a private network using an enterprise-focused fork of Ethereum called Quorum.
Chapter 8, Creating an ICO, continues the theme of using the Ethereum blockchain for business by describing, in detail, how to create and run an initial coin offering, also known as a token sale.
Chapter 9, Distributed Storage – IPFS and Swarm, builds on the previous Ethereum chapters to explore how to incorporate decentralized file storage in a decentralized application.
Chapter 10, Supply Chain on Hyperledger, introduces the third blockchain network of the book: Hyperledger. This chapter introduces the main concepts and basic features of Hyperledger, and how it differs from Bitcoin and Ethereum, before describing a practical implementation of how Hyperledger can be used to run a supply chain.
Chapter 11, Letter of Credit (LC) Hyperledger, builds on the basics from the previous chapter to implement a letter of credit issued between two banks and two transacting customers.
The book assumes that you are comfortable using a command-line interface, though doesn't require any formal shell scripting skills. Basic language-agnostic programming knowledge is also assumed, and, in some cases, a familiarity with a particular language would be beneficial, though isn't a requirement.
As the book covers many languages and technologies, it is unlikely the reader will be familiar with all of them. As such, a willingness to learn is certainly recommended.
You can download the example code files for this book from your account at www.packt.com. If you purchased this book elsewhere, you can visit www.packt.com/support and register to have the files emailed directly to you.
You can download the code files by following these steps:
Log in or register at
www.packt.com
.
Select the
SUPPORT
tab.
Click on
Code Downloads & Errata
.
Enter the name of the book in the
Search
box and follow the onscreen instructions.
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/Blockchain-By-Example. In case there's an update to the code, it will be updated on the existing GitHub repository.
We also have other code bundles from our rich catalog of books and videos available at https://github.com/PacktPublishing/. Check them out!
We also provide a PDF file that has color images of the screenshots/diagrams used in this book. You can download it here: https://www.packtpub.com/sites/default/files/downloads/9781788475686_ColorImages.pdf.
Feedback from our readers is always welcome.
General feedback: If you have questions about any aspect of this book, mention the book title in the subject of your message and email us at [email protected].
Errata: Although we have taken every care to ensure the accuracy of our content, mistakes do happen. If you have found a mistake in this book, we would be grateful if you would report this to us. Please visit www.packt.com/submit-errata, selecting your book, clicking on the Errata Submission Form link, and entering the details.
Piracy: If you come across any illegal copies of our works in any form on the internet, we would be grateful if you would provide us with the location address or website name. Please contact us at [email protected] with a link to the material.
If you are interested in becoming an author: If there is a topic that you have expertise in, and you are interested in either writing or contributing to a book, please visit authors.packtpub.com.
Please leave a review. Once you have read and used this book, why not leave a review on the site that you purchased it from? Potential readers can then see and use your unbiased opinion to make purchase decisions, we at Packt can understand what you think about our products, and our authors can see your feedback on their book. Thank you!
For more information about Packt, please visit packt.com.
What is blockchain? Certainly, with the huge hype around it, you must have heard or come across this question—it might be even the reason why you are reading this book. Let's discover, in this first chapter from a developer's standpoint, what's behind the hype.
As you might know, blockchain is an emerging technology that has the potential to dramatically revolutionize many different fields. This potential is primarily based on its ability to offer people a trustworthy channel to transfer value or real assets (tokenization) over the internet.
Blockchain has the capacity to move us from the internet of information to the internet of value, potentially breaking our existing financial systems.
Blockchain is in many ways a revolution, similar to the internet when it was conceived—certainly not a passing trend. The reason for this is that it presents a solution to a previously unsolved financial dilemma. For the first time in history, we are able to establish trust within trustless environments (such as the internet), without relying on an authority. As a result, some refer to blockchain as a trust machine.
The potential impact of blockchain is huge—it goes far beyond the mere decentralization of the financial sector. In fact, its ability to circumvent intermediaries opens the door to redefine almost every field revolving around technology—even the internet—pushing us toward a peer-to-peer world.
Through this short introduction, I am trying to give you a foretaste of the importance of our topic, and to confirm that your choice to learn about the technology is timely. As the book's name suggests, the approach we will be following throughout this book is to build concrete blockchain projects, instead of laying out abstract concepts.
Nonetheless, in spite of its less technical nature, the prime objective of this introductory chapter is to provide you with the necessary background to build the various projects presented in this book.
In this chapter, we will cover the following topics:
What cryptocurrency is
What blockchain is
How to send and receive bitcoins
How to store data into a bitcoin blockchain using JavaScript
An overview of blockchain types
However, this chapter doesn't intend to cover:
The underlying cryptography
Cryptocurrency trading
In this chapter, the first part will introduce basic concepts. The second part will be practical, and we will discover how to interact with the blockchain, using the famous Hello World example to get you started.
Many find it hard to understand the logic and the concepts behind blockchain, and why they would need it. This is primarily because we don't have a clear idea what problems it solves, or what advantages it promises.
Therefore, I believe it is necessary to clarify from the start which problems are solved by blockchain. We will start by learning about the concept and history of cryptocurrencies.
Blockchain didn't appear out of the blue. It was the product of the evolution of fintech and virtual currencies over the last few decades.
At the end of the last century, the widespread use of the internet favored the emergence of digital currencies as an extension of electronic cash systems. Many projects were developed to create new digital currencies: E-cash, E-gold, WebMoney, and Liberty Reserve, to name just a few
Despite huge success in the 1990s, these projects had ceased to exist by the beginning of the new century, either through bankruptcy or being halted by authorities. A currency which is capable of disappearing overnight is a real financial nightmare, but this situation was inevitable due to the centralized nature of such digital currency systems.
There was also always a need for a central authority to be involved, to fight fraud and manage trust within the system.
Because of this fatal weakness, the opposite, decentralized model was presented as a solution. However, it was hard to establish trust in such environments without any central authority. This contrast made creating a reliable digital currency a disentangled Gordian Knot.
Thankfully, the progress of cryptography and the emergence of some clever solutions such as proof of work (for example, the hashcash Project—see http://hashcash.org) brought hope of breaking the deadlock.
In 2008, Satoshi Nakamoto rose to the challenge and unveiled a digital currency called bitcoin. This new currency effectively harnessed cryptography techniques to manage ownership and to secure the system—hence the name cryptocurrency.
Satoshi solved the aforementioned problems by introducing what he called initially a chain of blocks. In his published whitepaper (see https://bitcoin.org/bitcoin.pdf), he presented his vision for a new peer-to-peer electronic cash system—bitcoin—and described in detail its underlying machinery—blockchain.
Bitcoin was the first reliable and distributed electronic cash system that's fully peer-to-peer, underpinned by the following basic concepts:
Encryption to ensure ownership and identity
A proof-of-work consensus mechanism
for validating transactions and securing the network against double transactions
A transparent and shared ledger (a blockchain)
Pseudonymity
With the assumption that the network majority (>51%) is honest, the bitcoin system operates autonomously following the rules defined by the protocol (consensus rules) to validate a given transaction. By using a shared blockchain, each player has the ability to check the transaction's log history and the sender's solvency, then vote on whether the proceeded transaction is valid or not.
The voting depends on the overall hash-power the player puts into service to secure the network (initially, one CPU is one vote).
To use a cryptocurrency, users need to install a specific client which creates a wallet, generates cryptographic key pairs (private/public keys), and syncs the blockchain with the network. The public key is used by the client (software) to generate valid addresses, and the funds sent to a given address are controlled by the private key from which the address was calculated. In this way, we rely on secure cryptographic principles to manage ownership.
The following diagram depicts how transactions are processed in bitcoin's peer-to-peer network and added into a blockchain:
In a bitcoin network where users don't know one another, the blockchain is considered the single source of truth to which they refer to learn about previous consensus outcomes. The blockchain with the consensus protocol allows the network to manage transactions without a single point of failure.
Often confused with bitcoin, blockchain is the underlying technology used by bitcoin to operate. Concretely, it's an append-only and chronologically (timestamped) growing database, which harnesses basic cryptographic measures to protect stored transactions from being tampered with (in other words, data can't be deleted or altered).
This database, or ledger, collects and records monetary transactions validated by the network in elementary units called blocks. Once validated by the network consensus mechanism, these blocks are added to an existing sequential chain of cryptographic hash-linked blocks, to ensure the integrity of the data—hence the name blockchain.
If a single bit changes in one of the linked blocks, the hash-link collapses, the chain is broken, and it will be rejected by the network.
The following diagram shows how the blockchain is replicated and processed by the members of the network to ensure that everyone has a consistent view of the transaction log. When a new block is validated, all nodes synchronize the same copy:
The diagram also shows us that blockchain implements a special data structure, consisting of linked blocks storing transactions and smart contracts. Let us take a closer look at these key elements in detail.
If we consider blockchain to be a ledger or a book, a block can be compared to a page or a table in which we record a collection of confirmed transactions. Each block stored in the blockchain is uniquely identified by a hash, and composed of a header and a body.
The header encloses information about its creation (timestamp, Merkle root, Nonce, difficulty target, and version), and a reference to a previous block, whereas the body is a collection of the accepted transactions.
When a block is successfully validated (mined), it becomes part of the official blockchain. New bitcoins are generated in the block (a coinbase transaction) and paid to the validators (or miners).
Transactions are the most fundamental building blocks of the blockchain system. They represent the transfer of value (cryptocurrency) within the blockchain network between two addresses.
More tangibly, they are represented by small data structures, defined by the blockchain protocol (such as bitcoin or Ethereum), which specifies their attributes (metadata, inputs, outputs, and so on), and model.
Before broadcasting the transaction, the user sending the funds signs it using their private key (managed by their wallet), and specifies the destination address. Digital signatures and public keys are used to enable network users to validate the transaction, and to check whether the sender has the right to spend the bitcoins held by a specific address.
Smart contracts are one of the most exciting concepts in blockchain, representing self-executing scripts stored on the blockchain itself. The smart contract takes the blockchain concept to the next stage, enabling it to translate business logic into inviolable contract terms, which will be autonomously executed without relying on a broker, lawyer, or other intermediary.
The earliest form of a smart contract was defined in bitcoin using basic locking and unlocking scripts, but the concept evolved with the emergence of other blockchains.
Smart contracts are one of the more powerful, disruptive forces within blockchain, and are garnering more and more business attention, as described in the Gartner report Why Blockchain’s Smart Contracts Aren’t Ready for the Business World (see https://www.gartner.com/smarterwithgartner/why-blockchains-smart-contracts-arent-ready-for-the-business-world/). Gartner estimates that by 2022, smart contracts will be used by more than 25% of global organizations.
Owing to their importance, we will return later in this book to introduce you to smart contracts in leading blockchain platforms—bitcoin, Ethereum, and Hyperledger.
We have now finished describing the concepts—let's practice a little bit to understand what has been presented so far.
Blockchain as a technology has evolved rapidly, as new techniques derivingfrom the proliferation of blockchain projects have emerged. Hence the attempts to understand the present day blockchain machinery more closely led to the discovery of bitcoin.
Therefore, in this chapter we will adopt bitcoin as our main example. This choice is due to the fact that bitcoin is the original blockchain implementation, and almost all other projects mimic its design and mechanics.
In the following sections, we will connect to the bitcoin network and store the classic Hello World message into a blockchain. Bitcoin transactions can be used to store small amounts of data in a blockchain—allowing developers to build distributed systems on top of bitcoin, such as Colored Coins, Counterparty, Tierion, and more.
You would be surprised by the number of hidden messages stored in the bitcoin blockchain.
A bitcoin client is the end-user software that allows us to perform bitcoin operations (sending transactions, receiving payments, and so on). When you run one, you become part of the bitcoin network.
We have chosen two common clients: Bitcoin Core and Electrum. In our example, the sender will use Electrum and the receiver will use Bitcoin Core (the most popular bitcoin client).
For the purposes of this demonstration, I will install them on a single machine using Ubuntu 16.04.
You can install Bitcoin Core (version 15.04) using the following commands:
wget https://bitcoincore.org/bin/bitcoin-core-0.15.2/bitcoin-0.15.2-x86_64-linux-gnu.tar.gzsudo install -m 0755 -o root -g root -t /usr/local/bin bitcoin-0.15.2/bin/*
Electrum is a lightweight wallet, which means it doesn't require you to download the entire blockchain, as we will see in the next section. Download and install the latest version of Electrum as follows:
wget https://download.electrum.org/3.2.2/Electrum-3.2.2.tar.gz
sudo apt-get install python3-setuptools python3-pyqt5 python3-pip
sudo pip3 install Electrum-3.2.2.tar.gz
Once both clients are installed, we need to synchronize them with the network.
We learned earlier that a blockchain is a transaction database duplicated by all computers on the network. We need to sync a huge amount of data (>200 GB) to enable the sending or receiving of bitcoins.
However, there are two workarounds to this:
Enabling pruning for a full-node client such as Bitcoin Core
Using a thin (SPV) client such as Electrum, which fetches blockchain information from Electrum servers instead of having a local copy
We will look at both solutions. Nonetheless, it's always advisable to use a bitcoin full-node client to benefit from the power of blockchain.
Depending on your OS, you need to create the bitcoin.conf configuration file in the default data directory located under the following paths:
Windows
:
%APPDATA%\Bitcoin\
Mac
:
$HOME/Library/Application Support/Bitcoin/
Linux
:
$HOME/.bitcoin/
In Linux, create a .bitcoin directory using mkdir ~/.bitcoin, then create the bitcoin.conf file using nano ~/.bitcoin/bitcoin.conf.
Add the following lines to bitcoin.conf to define your client configuration (the comments after each # sign explain the parameters):
rpcuser=user_name #Username for JSON-RPC connectionsrpcpassword=your_password #Password Username for JSON-RPC connectionsserver=1 #Tells Bitcoin-Qt and bitcoind to accept JSON-RPC commandstestnet=1 #Run on the test network instead of the real bitcoin network.prune=550 #Enables pruning mode
Once copied, press Ctrl + X, then Y, and then Enter to save the file.
Now our first client is ready to run on the testnet, which is a bitcoin network created for testing purposesthat follows the same rules as a main network. It's a public network using worthless bitcoins. You can use this network to send free transactions and test your applications.
It's now time to run Bitcoin Core. Open a new command line interface (CLI) window, and run the following command:
bitcoin-qt
Bitcoin Core will start running with its standard GUI interface connected to the testnet.
For the first run, it will ask you to set the data directory, which we will set to the default. It will then automatically create a wallet for you, start syncing with the testnet, and download the blockchain:
Alternatively, you could run the bitcoin daemon in CLI mode with the following command:
bitcoind
It's up to you to choose which mode to continue using (bitcoind or bitcoin-qt); the available RPC commands are the same. For my part, I'll continue this guide using btcoin-qt.
As Bitcoin Core starts up, it creates many subdirectories and files in the default data directory (.bitcoin), as shown in the following screenshot:
The main subdirectories are:
blocks
:
Stores actual bitcoin blocks
chainstate
: Holds a
LevelDB database for available
UTXOs
(short for
Unspent Transaction Outputs
)—in other words, a database storing how much money everyone has
wallet
:
Contains an encrypted
wallet.dat
file, which stores the private keys
Even if the network sync is not yet finished, you can open the blocks/ subdirectory to see the blockchain's blocks stored in raw format. Each blk00*.dat file is a collection of several raw blocks:
We will read the content of one of these files later.
While the server (bitcoind or bitcoin-qt) is running, open another Terminal. Let's generate a new address for our wallet by executing bitcoin-cli getnewaddress, as in the following screenshot:
Basically, bitcoin-cli is a tool that enables us to issue RPC commands to bitcoind or bitcoin-qt from the command line (bitcoin-qt users can also access the bitcoin RPC interface by using the Debug console, under the Help menu).
Now we have finished with Bitcoin Core, let's leave it to sync with the blockchain and move on to configuring Electrum.
After you have downloaded and installed Electrum, open Electrum's testnet mode by running electrum --testnet. When you run Electrum for the first time, it will display the new wallet creation wizard. Follow these steps:
Select
Auto Connect
in the
first dialog box
and click
Next
.
Select
Standard wallet
and click
Next
.
Keep selecting
Next
for each dialog box that appears, until you are asked to save your seed words. Copy them, then reconfirm that you've saved them correctly, as follows:
In the last step, it will ask you for a password, which you can leave empty.
Once finished, Electrum will generate a new wallet with plenty of new addresses. Quit the Electrum GUI, and let's continue in CLI mode. We run Electrum as a
daemon process, whereby we execute the JSON/RPC commands as following:
electrum --testnet daemon
electrum --testnet daemon load_wallet
In a new Terminal window, run
electrum --testnet listaddresses
:
Great, now we have the necessary environment to start transacting with the public bitcoin network. That said, let's discover how a bitcoin transaction is created, exchanged and stored in the blockchain by constructing a bitcoin raw transaction, signing it, and broadcasting it to the network.
For the sake of brevity, we'll focus herein on the instructions needed to create and send raw transactions in Bitcoin Core, without lengthy explanations.
Don't worry if you don't understand all of what you read right away. In Chapter 2, Building a Bitcoin Payment System, we will explain the new concepts introduced in this section (inputs, outputs, scripts, and so on).
First off, we need to fund our previously created address with some bitcoins in order to make the first transaction. Thankfully, in the testnet we can use a free funding source called a bitcoin faucet, which provides worthless bitcoins for testing applications.
For this example, browse to the online faucet website at http://bitcoinfaucet.uo1.net/ or any other bitcoin's faucet websites, and get a few by providing the first address generated by Electrum and the address created by Bitcoin Core, as shown in the following screenshot:
Now that we've sent the bitcoins from the faucet, let's check whether Bitcoin Core can see the transaction. To do that, we'll need to list the available UTXOs in both clients, using the listunspentRPC command.
With Bitcoin Core running, run the following command in your Terminal window:
bitcoin-cli listunspent
This will return the following result:
[{ }]
Initially, listunpsnet returns an empty result, because Bitcoin Core hasn't yet finished syncing the blockchain, which takes time (a few hours). For this reason, we will use Electrum instead of Bitcoin Core for the remainder of this guide, asit avoids us waiting for hours to see the received bitcoins.
However, we will go back using Bitcoin Core from time to time, as it has a powerful command line to deal with raw transactions.
Now run the same command for Electrum, as follows:
electrum --testnet listunspent
We will get a list of available entries, such as the following:
The previous command's output shows that we have a single available transaction received from the faucet, uniquely identified by its hash (prevout_hash field), with 1.1 Bitcoins.
More precisely, we have an available unspent transaction output from a previous transaction, which can be used as an input for the transaction we are willing to build, as follows:
In Bitcoin, transactions spend outputs from prior transactions, and generate new outputs that can be spent by transactions in the future. In fact, users move funds solely by spending UTXOs.
The previous diagram shows that the transaction (Transaction C) we received from the faucet consumes as inputs an existing output(output 1) created earlier by an old transaction. The same transaction creates two outputs: one for us (output 1), and the other returns back the change (output 0). The reason for this is that transaction outputs must be fully spent.
Unlike what you might have expected, in bitcoin, transactions don't update a global user balance (the account/balance model). Instead, they move bitcoins between one or more inputs and outputs (the UTXO model). The total balance is calculated by the bitcoin client as the sum of the values transferred by the received unspent transactions.
At this level, it's time to create a transaction that spends the received transaction. From the listunspent output, we have the necessary ingredients (prevout_hash and prevout_n) to construct our raw transaction. Let's see how.
First, you need to convert the hello worldmessage into hexadecimal, using an online converter (such as https://codebeautify.org/string-hex-converter). The hexadecimal encoded form will be 68656c6c6f20776f726c64.
Then we have to use the createrawtransaction command, which creates a transaction spending the given inputs and creating new outputs. We have to pass as an argument (from the previous output) an object with the following parameters:
The
txid
of one of the available outputs
The
vout
index
(
prevout_n
for Electrum)
of the selected output
The hexadecimal form of the message
The destination address (created earlier)
The total number of satoshis (
the smallest unit of the
bitcoin
currency
) to send
Here we are sending one bitcoin, although you can set it to 0:
bitcoin-cli createrawtransaction "[{\"txid\":\"0791521362528725683caedf998006cf68b1cd817be1694ef0daca265d9b4252\", \"vout\": 1}]" "{\"data\":\"68656c6c6f20776f726c64\",\"2MsHsi4CHXsaNZSq5krnrpP4WShNgtuRa9U\":1.0000000}"
You'll get the following serialized long hex-encoded string, representing our raw transaction:
020000000152429b5d26cadaf04e69e17b81cdb168cf068099dfae3c6825875262135291070100000000ffffffff0200000000000000000d6a0b68656c6c6f20776f726c6400e1f5050000000017a914008051b4d96aa26269dfd36af0eb9c2b2fa894568700000000
At first sight, the previous resultant hexadecimal string seems ambiguous and meaningless. The following table breaks down and examines indepth our transaction, byte by byte:
As you can see, our transaction has one input (the only unspent transaction received from the faucet), with the 0791...252 transaction id, and two outputs:
An
OP_RETURN
output with an
OP_RETURN
script
An output sending one bitcoin to the specified address
The transaction structure can be visualized by decoding back the raw transaction using the deserialize command. If you run electrum --testnet deserialize <Raw transactions>, it will output a meaningful JSON representation of our constructed transaction:
To get the same result, you can decode the raw transaction using bitcoin-cli decoderawtransaction, or by using an online decoder such as the one at https://live.blockcypher.com/btc-testnet/decodetx/.
At this point, the transaction is created, but not yet transmitted to the network. To send our transaction, we need to sign it using the bitcoin-cli signrawtransaction command. We sign the transaction using our private key (related to the receiving address) to prove to the network our ownership of the output, and therefore our authority to spend the held bitcoins.
The first step will be to extract the private key associated with the first address used to receive the bitcoins from the faucet:
electrum --testnet listaddresses | electrum --testnet getprivatekeys -
Notice the presence of a dash at the end of the command. It will be replaced by the values returned from the pipe. As a result, you'll get a list of private keys. Copy the first one without the p2pkh prefix, as follows:
Next, we need to get scriptPubKey from the output we are willing to spend. For that, firstly, we have to retrieve the transaction from the blockchain, using electrum gettransaction --testnet "0791521362528725683caedf998006cf68b1cd817be1694ef0daca265d9b4252".
Secondly, we use the resultant raw form to get scriptPubKey, as follows:
electrum deserialize --testnet 0200000001915bf222c2e4e6ff36760168904ae102a0e968d83b3c575077d5475aa94dd9bf010000006b483045022100b129bc0fb5631aa668c48bb7a8fef0c81fec131d2f68ba430cd7cd9de0bd971b02203dabbf054790e31b4fd1b9a333881cd480c19b38a229e70f886dbb88ee4673f1012103bcf53d63d2fa14ee04d9ebb9170dfa7987298689c7e6ceb765c1d3ccd7f9ad01feffffff02d618b24a000000001976a914b9172e192d2805ea52fa975847eea0657e38fef888ac80778e06000000001976a914edcce89f510bf95606ec6a79cb28a745c039e22088ac63b31400
Unlike before, here we are loading and deserializing the received transaction from the faucet. We will get the outputs created in this transaction, as follows:
The part surrounded in red is scriptPubKey of the unspent transaction output.
A scriptPubKey can be seen in the outputs; it represents the conditions that are set for spending the outputs. The new owner can sign using the private key associated with the address receiving the output to fulfil the conditions of scriptPubKey.
The network checks whether the digital signature is valid, and if so makes it an input for the new transaction. The cryptographic parts—scriptSig and scriptPubKey—are particularly complex, and will be discussed in the next chapter.
Copy scriptPubKey from the output, and pass it along the other options to the signrawtransaction command, as follows:
signrawtransaction "Raw hexstring" ( [{"txid":"id","vout":n,"scriptPubKey":"hex","redeemScript":"hex"},..] ["privatekey",..])
The second argument is a JSON array of the previous transaction outputs we are consuming, and the third argument is the private key belonging to the address that received the output. The result will be similar to the following output:
After succeeding in signing the raw transaction, it is time to send the signed transaction to the testnet.
To send the transaction into a blockchain, we submit the signed signature using the broadcast command provided by Electrum, as shown in the following screenshot:
You'll get back the hex-encoded transaction hash ID:
d3e300c2f2eedf673ab544f4c2b09063353e618ab8a0c9444e931d0145e43ded
If everything goes as planned, you should have successfully stored the hello world message into bitcoin's testnet blockchain.
The following screenshot illustrates what we have done so far. We consumed an input (from a previous transaction), then created a transaction with two outputs; the first being an OP_RETURN transaction carrying our message along, the other one transferring one bitcoin (BTC):
Isn't it just fascinating? You can use a block explorer such as https://live.blockcypher.com/btc-testnet/tx/<txid> to inspect the transaction with the printed transaction hash (txid), and to retrieve your stored message.
It would be more exciting to retry the same operation using the mainnet (the original and main network for bitcoin), but then you would be dealing with real, expensive bitcoins.
If Bitcoin Core has finished syncing the blockchain, you can locally parse the blocks to locate our transaction and read the stored message.
To open and parse the blockchain blocks, we need to install a graphical hex editor such as bless, by running sudo apt-get install bless.
Once installed, you can run it and open one of the .blk files present in the blocks directory:
As shown in the screenshot, bless will display a pane divided into three parts:
The left column is the offset column
The center column displays the blocks' hexadecimal content
The right column is the same line of data as in the center, with recognized text characters displayed as text and binary values represented by period characters
To locate our transaction, you can search for it by pasting the unsigned raw transaction string into the Search field. You may go through a few blk**.dat files before you find your transaction. In my case, I found it in the blk00100.dat file.
At first glance, it may not be very meaningful, but once you locate your transaction you can easily locate the message you’ve stored in the blockchain. The hello world message will be visible in the ASCII section on the right.
You can also locate the block that encompasses the transaction by searching for the previous block delimiter, called magic bytes,represented by 0b110907. Then you can, by following the structure of the block, determine the meaning of these long hexadecimal strings.
In the previous screenshot, I delimited the block with a yellow border and highlighted the blocks header field with multiple colors. I delimited our transaction and the coinbase transaction in blue and gray, respectively.
To help you visualize the block content, the following table explainsthe meaning of the previously highlighted bytes:
And that's it! You can now send transactions with extra messages into the blockchain, and retrieve the data online or locally. Although this is not usually required, it may prove useful in the future.
Let's go ahead and send another raw transaction with an OP_RETURN output using a different method.
At this point, I would guess that you want to write some code. Your wish is my command.
In this section, we will build a simple Node.Js script to perform what we have performed manually before: to send a raw transaction over the testnet. You can stop running Electrum and Bitcoin Core, as we will use an online REST API (chain.so/api) as a middle tier to interact with bitcoin's network.
