Hands-On Smart Contract Development with Hyperledger Fabric V2 Building Enterprise Blockchain Applications Matt Zand, Xun Wu & Mark Anthony Morris Praise for Hands-On Smart Contract Development with Hyperledger Fabric V2 Check out this book if you want to get a deeper understanding of Hyperledger Fabric and gain expertise with creating permissioned blockchain networks. —Brian Behlendorf, Executive Director for Hyperledger One of the best books covering Hyperledger Fabric to the degree of breadth and clarity that this piece captures. From component overview to practical examples within a relatable context, it uniquely explicates Hyperledger’s inner workings in a way that is hard to match. —Jim Murphy and Jason Jaan Financial Transformation Partner, KPMG; Transformation Delivery Manager, KPMG A well-balanced comprehensive literature. This book provides that balance with an approach essential in blockchain architect skill-building. While many of its concepts may focus on Hyperledger Fabric, they are portable to other blockchain frameworks and platforms. A must-read for all aspiring blockchain architects and developers. —Nitin Gaur Director, Financial Sciences and Digital Assets, IBM A great practical hands-on guide to developing smart contracts using Hyperledger Fabric on popular platforms. This book provides detailed step-by-step instructions for developing, testing, deploying, and maintaining smart contracts on popular Blockchain as a Service platforms. —Todd Little, Oracle Blockchain Platform Architect Clean, clear, and clinical, this thoughtful, deep dive into Smart Contracts is a boon to blockchain development and a must-read for Hyperledger. —Joseph Raczynski, Technologist & Futurist, Thomson Reuters This book makes understanding Hyperledger concepts extremely simple and easy by serving technical and nontechnical audiences to understand Hyperledger from the basics and then build on top of it. The authors allow anyone to understand the intricacies of building an enterprise blockchain application using Hyperledger. —Andile Ngcaba, Founding Partner & Chairman of Convergence Partners This book is the gold standard in smart contract development. This comprehensive examination understands the challenges facing enterprises interested in implementing blockchain solutions and offers concise direction. Zand, Wu, and Morris supply readers with valuable insights for smart contract development through the hands-on techniques discussed in the text. —Bobbi Muscara, Hyperledger TSC Member; Chair of the Hyperledger Learning Materials Working Group; Director of Education, Ledger Academy I have been building enterprise blockchain solutions for five years, and Hands-On Smart Contract Development with Hyperledger Fabric is my go-to resource for creating new blockchain use cases and improving existing solutions. This book should be in every HLF developer’s toolkit. —David Havera, Chief Blockchain Engineer, GE Anyone interested in smart contract development on Hyperledger Fabric should get this book both for a cover-to-cover read as well as an ongoing reference. Well written, well organized, and eminently understandable. Zand leverages his years of development training experience to deliver highly consumable foundational, strategic, and tactical knowledge for builders in the Hyperledger ecosystem. —Eric Hess, Host of The Encrypted Economy podcast and Managing Partner of Hess Legal Counsel Comprehensive, easy to read, and state of the art: the first book that intuitively supports both software developers and product managers in developing real-world enterprise blockchain applications. —Robin Pilling, CEO & Founder, Dis3bute Hands-On Smart Contract Development with Hyperledger Fabric V2 Building Enterprise Blockchain Applications Matt Zand, Xun Wu, and Mark Anthony Morris BBeeiijjiinngg BBoossttoonn FFaarrnnhhaamm SSeebbaassttooppooll TTookkyyoo Hands-On Smart Contract Development with Hyperledger Fabric V2 by Matt Zand, Xun Wu, and Mark Anthony Morris Copyright © 2021 WEG2G LLC. All rights reserved. Printed in the United States of America. Published by O’Reilly Media, Inc., 1005 Gravenstein Highway North, Sebastopol, CA 95472. O’Reilly books may be purchased for educational, business, or sales promotional use. Online editions are also available for most titles (http://oreilly.com). For more information, contact our corporate/institutional sales department: 800-998-9938 or [email protected]. Acquisitions Editor: Michelle Smith Indexer: nSight, Inc. Development Editor: Gary O’Brien Interior Designer: David Futato Production Editor: Christopher Faucher Cover Designer: Karen Montgomery Copyeditor: Sharon Wilkey Illustrator: Kate Dullea Proofreader: Charles Roumeliotis September 2021: First Edition Revision History for the First Edition 2021-09-08: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781492086123 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Hands-On Smart Contract Development with Hyperledger Fabric V2, the cover image, and related trade dress are trademarks of O’Reilly Media, Inc. The views expressed in this work are those of the authors, and do not represent the publisher’s views. While the publisher and the authors have used good faith efforts to ensure that the information and instructions contained in this work are accurate, the publisher and the authors disclaim all responsibility for errors or omissions, including without limitation responsibility for damages resulting from the use of or reliance on this work. Use of the information and instructions contained in this work is at your own risk. If any code samples or other technology this work contains or describes is subject to open source licenses or the intellectual property rights of others, it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. 978-1-492-08612-3 [LSI] Table of Contents Preface. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xiii Part I. Introduction to Blockchain 1. Fundamental Concepts of Blockchain. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3 What Is Blockchain? 4 Origin of Blockchain 6 The Blockchain Revolution 7 Blockchain 2.0 7 Core Components of Blockchain 7 Decentralized Network 8 Cryptography 8 Wallet 10 Shared Ledger 10 Consensus 11 Smart Contracts 12 Blockchain Architecture 13 Nodes 14 Blocks 14 Chains 15 Channels 15 Supporting Technology and Concepts 15 DLT 15 Decentralization 15 Peer-to-Peer 16 Immutability 16 v Identity 16 Accounts 16 SDK 17 API 17 Transactions 17 Incentives 18 Privacy 18 State 18 Turing Complete 19 Gas 19 Tokens 20 On-Chain 21 Off-Chain 21 Scalability 21 Cryptocurrency 22 Enclaves 22 Oracles 23 DApps 23 Virtual Machine 23 Fork 23 Governance 24 Genesis 24 Clients 24 Summary 24 Part II. Introduction to Hyperledger Projects 2. Overview of Hyperledger Projects and Tools. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27 Evolution and Phases of Blockchain Technology 28 Blockchain 1.0 28 Blockchain 2.0 29 Blockchain 3.0 29 Blockchain 4.0 29 Hyperledger Family, Architecture, and Ecosystem 30 The Hyperledger Design Philosophy 31 Overview of Hyperledger 32 Overview of Hyperledger Projects 34 Distributed Ledger Frameworks 34 Domain-Specific 36 Overview of Hyperledger Tools and Libraries 37 vi | Table of Contents Tools 37 Libraries 38 Summary 39 3. Hyperledger Fabric Architecture and Components. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41 Hyperledger Fabric Overview 42 Hyperledger Fabric Model 42 Blockchain Network 44 Identity 45 Membership Service Provider 46 Policies 47 Nodes 48 Chaincode 49 Ledger 50 The Ordering Service 51 Private Collections of Data 51 Summary 52 Part III. Developing Smart Contracts with Hyperledger Fabric 4. Smart Contract Development. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 55 Installing Prerequisites and Setting Up Hyperledger Fabric 56 Git 56 cURL 56 Node.js and npm 56 Docker and Docker Compose 57 Fabric Installation Script 57 Fundamental Requirements of a Smart Contract 58 SDK 60 Create and Execute Queries 66 Defining a Smart Contract 67 Define Assets by Using Key-Value Pairs 72 Collect Private Data 73 Set Attribute-Based Access Control 74 Initialize the Ledger State 75 Installing and Instantiating a Smart Contract 77 Package the Chaincode 77 Install the Chaincode 78 Query the Installation 78 Approve the Package 78 Table of Contents | vii Check Commit Readiness 79 Commit the Chaincode Definition 79 Query Whether the Chaincode Is Committed 80 Initialize the Contract 80 Execute a Query 81 Summary 82 5. Smart Contract Invocation. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 83 Overview of fabric-samples, Fabcar, and the Test Network 83 fabric-samples 84 Fabcar 84 Fabric Test Network 88 Deploying a Smart Contract 90 Understand the startFabric.sh Anatomy 90 Execute startFabric.sh 91 Generate Fabric Certificate Authorities 92 Create Org1 Identities 92 Invoke the Peer Chaincode 93 Invoking Smart Contract Transactions 95 Peer Command 96 Fabric SDK for Node.js Command-Line Application 101 Summary 110 6. Testing and Maintenance. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 111 Creating a Fabcar UI Client 112 Error Response Handling 113 Fabcar UI Web Pages 113 Fabcar UI Code 116 Fabcar UI Execution 120 Performing Rapid Smart Contract Testing 124 Setting Up 124 Preparing the Fabcar Smart Contract for Testing and Debugging 129 Performing Fabcar Testing and Debugging 131 Identifying and Reviewing Logs 134 Creating Unit Test Contracts 136 Summary 138 viii | Table of Contents