Santiago Palladino

Ethereum for Web Developers

Learn to Build Web Applications on top of the Ethereum Blockchain

Santiago Palladino
Ciudad Autónoma de Buenos Aires, Argentina
ISBN 978-1-4842-5277-2e-ISBN 978-1-4842-5278-9
© Santiago Palladino 2019
This work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer software, or by similar or dissimilar methodology now known or hereafter developed.
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefit of the trademark owner, with no intention of infringement of the trademark. The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identified as such, is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. The publisher makes no warranty, express or implied, with respect to the material contained herein.
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor, New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mail [email protected], or visit www.springeronline.com. Apress Media, LLC is a California LLC and the sole member (owner) is Springer Science + Business Media Finance Inc (SSBM Finance Inc). SSBM Finance Inc is a Delaware corporation.

To Ale, my soon-to-be-wife, who supported me through the many days that went on while writing this book.

To my parents, both computer scientists, who passed down their passion on to me.

Introduction

It was not easy to write a book on Ethereum. Ethereum is one of the fastest evolving technologies I have ever seen. It builds on an entirely new computing model that did not even exist five years ago, and has undergone countless transformations since its conception. Writing something as static as a book feels an almost futile attempt to try to capture it. Combining this with web development, another area known for its rapid-changing environment, seemed like a daunting task.

Nevertheless, there are concepts that have become fundamental to developing blockchain-based applications. Even if the space is new, we are seeing many new chains being launched 1 after Bitcoin and Ethereum, and all share many of its basic building blocks. Those are the concepts that this book attempts to capture, seen from the perspective of a web developer. I believe they will be as helpful in several years’ time as they are today, even if the tools and practices we rely on change completely.

Throughout the book, you may read many disclaimers in the form of at the time of this writing , to emphasize things that are bound to change in the near future – some of them even did in the time it took me to write and review each chapter. However, it is important for you to keep in mind that the disclaimer applies to the entire book, as the Ethereum ecosystem is in constant evolution.

Who this book is for

This book is for me a few years ago, before entering the blockchain space. I had worked as a full stack developer for several years, and moving into Ethereum rocked the foundations of how I had been building and thinking applications.

At that moment, it was surprisingly difficult to find comprehensive material to understand all the aspects of Ethereum needed to build a decentralized application. And it still is today, for information is mostly fragmented, and often tailored for a specific toolset.

So, this book is for developers experienced in web applications, who want to apply their skillset to this new decentralized platform that is Ethereum. What we are seeing is much like the mobile revolution, which completely changed how we interacted with the web over a decade ago. It required developers to relearn and adjust to a new paradigm. The same is happening now.

With that in mind, this book presents, on top of traditional web development, the new concepts that come from having an Ethereum network at your disposal.

What you should know

Being written for web developers, this book assumes that you already know and master what a web application is, are comfortable with javascript as a development language, and understand concepts such as client-server architecture, relational databases, the HTTP request/response cycle, and DNS.

In particular, we will make use of React as a front-end library to simplify the development of many examples throughout the book. Considering that React has existed for longer than Ethereum itself, and given its popularity today, it seems like an adequate choice. We will not rely on any framework or state-management solution, as we will keep our examples simple and focused on the Ethereum side of the picture.

Even though we will be working with a cryptocurrency, it is not necessary to have prior knowledge of cryptography or currencies to approach this book. We will be covering the basics of hashing and public key cryptography in the first chapter, and briefly go through financial incentives in the last ones. Of course, if either of those areas are of interest to you, blockchain is a fantastic place to exercise them.

The working environment

Much of this book is composed of code samples. Even if the value of this book relies on the concepts it attempts to pass on to you, each chapter includes several code snippets or full applications to help illustrate them. With that in mind, you may want to have an environment ready to reproduce the experiments listed.

All code samples are written in Javascript ES6, were developed and tested on an Ubuntu Linux system, and run on nodejs 2 10.16 from a bash shell. With Javascript being multiplatform, the samples should seamlessly run on OSX or Windows environments, though your mileage may vary. That said, having npm 3 working and being able to run a create-react-app 4 locally should be enough for most code samples. Certain chapters may also require you to install and run an Ethereum node, such as Geth 5 or Parity Ethereum. 6 Refer to their websites for platform-specific instructions for installing them.

We will make very limited usage of Ethereum-specific tools and libraries throughout the book. We will limit ourselves to a single Javascript library for interacting with the Ethereum network, 7 plus a tool for simplifying the compilation of smart contracts, 8 and a library of standard contracts to avoid reimplementing them from scratch. 9 The tooling and framework space in Ethereum changes fast, and I wanted to avoid tying the book to one of them. Nevertheless, when you start your new Ethereum application, relying on an existing framework such as OpenZeppelin, Buidler, Truffle, Embark, or Etherlime may help you speed things up.

A walkthrough of the chapters

The first chapter will be the only one free of code. It will introduce what a blockchain is, with a bit on history going from Bitcoin to Ethereum, plus the fundamental concepts of accounts, transactions, and blocks, along with the basic bits of cryptography that will be needed throughout the book. It will also briefly cover blockchain use cases, and introduce the concept of decentralized applications.

Chapter two is fully hands-on. It compensates for the lack of code on the previous one by going heads-on to develop a complete decentralized application from scratch. Many concepts will be quickly glossed over, but this chapter should help you understand in a practical way how all the components fit together, so you have a clear idea of their role when they are explored in depth later.

Chapter three is the only one unrelated to web development. It provides a crash-course on smart contracts, a key construct in Ethereum. Having a good understanding of smart contracts and knowing what they can and cannot do will help you design the architecture of your applications. Most of the chapter builds on Solidity, the most popular high-level language for writing contracts.

Chapters four and five go back to web development, going in-depth with very basic tasks: reading and writing data from the blockchain. Gathering blockchain data is not like sending a SQL query to your average relational database, and sending a transaction requires managing concepts like gas and signatures. In blockchain development, long confirmation times and reorganizations may dwarf the challenges you know from NoSQL databases’ eventual consistency. These two chapters will present those issues, and several techniques for dealing with them.

Chapter six will challenge decentralization itself. Up to this point, all samples were built as a static single-page application using the blockchain as its only back end. This chapter will introduce centralized components to the architecture of a decentralized application, such as indexing and storage solutions.

Finally, chapters seven and eight deal with two of the most pressing challenges in Ethereum development as of today: user onboarding and scalability. Getting started in the Ethereum space for a non-technical user can be challenging, as they get bombarded with concepts like private keys or mnemonics, in an unforgiving space where they could lose all their funds if they make a mistake. Furthermore, having a platform with a global throughput of a dozen transactions per second severely limits the applications it can run – think of a cloud provider that offers no more than twelve database writes per second shared across all its clients. These two chapters explore these problems in depth, and include a survey of the current solutions available. These are also the two scenarios most rapidly changing, but the chapters will give you the basics to help you navigate this space, so you can then build an outstanding Ethereum application.

The mandatory disclaimer

Security in software development is hard. Security in blockchain development is even harder. Smart contract applications potentially manage large amounts of funds, and are like sitting ducks to attackers in a public executable environment.

I want you to write a program that has to run in a concurrent environment under Byzantine circumstances where any adversary can invoke your program with any arguments of their choosing. The environment in which your program executes (and hence any direct or indirect environmental dependencies) is also under adversary control. If you make a single exploitable mistake or oversight in the implementation, or even in the logical design of the program, then either you personally or perhaps the users of your program could lose a substantial amount of money. Where your program will run, there is no legal recourse if things go wrong. Oh, and once you release the first version of your program, you can never change it. It has be right first time.

—Adrian Colyer, “Zeus: Analyzing safety of smart contracts” 10

Even though all code samples in this book have been reviewed, they have not been formally audited. And even if they were, there could still be an overseen security bug. The goal of the code in this book is to teach you, not to be copy-pasted into your application – we already have StackOverflow for that. Neither the author nor the publisher of this book can offer any warranties on the security of the code you will find here.

Bottom line is that you should not blindly trust any code snippet from this book – or from any other source, for that matter. Always make sure you have a good understanding of what you are doing, and have your code reviewed and audited by third parties before going to production in the Ethereum network.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Without further ado, let’s get started.

Acknowledgments

I want to thank, first and foremost, the awesome team at OpenZeppelin. OpenZeppelin was my first contact with the mind-blowing world of Ethereum, and where I learned everything that you will read throughout this book.

I also want to thank all the colleagues with whom I shared my professional life as a developer, in particular those at manas.tech, where I worked and learned for almost a decade. Those years got me to be the developer I am today.

A big thank you also goes to the entire Ethereum community. Many of the buidlers 11 and maintainers of the infrastructure that keeps everything running often go unnoticed or unappreciated. It is through their great efforts that this ecosystem thrives and grows.

I also have eternal gratitude to the teachers of the public and free University of Buenos Aires, where I got an invaluable education in computer science. Even if blockchain did not even exist during my time as a student, the foundations I learned there still help me today when approaching any topic.

Last but not least, a special thanks to the team at Apress who helped me in putting this book together, and in getting it to your hands.

And of course, to my cat, who unrelentingly slept by my side every single hour I spent writing this book.

Table of Contents

Index 321

About the Author and About the Technical Reviewer

About the Author

Santiago Palladino
../images/476252_1_En_BookFrontmatter_Figb_HTML.jpg

has been working as a professional software developer since 2005, working extensively in web applications for over 10 years. He entered the Ethereum space in 2017, when he joined the OpenZeppelin team as one of the first employees. There he worked as security researcher in several audits, and led the development of open source tools and services for building Ethereum applications. He has an MSc in Computer Science from University of Buenos Aires, where he was a TA in Algorithms and Numerical Methods for a total of 7 years. He has written several online articles on Ethereum development and Blockchain in general. You can find him on Twitter @smpalladino, or Github @spalladino.

 

About the Technical Reviewer

Alexander Chinedu Nnakwue
../images/476252_1_En_BookFrontmatter_Figc_HTML.jpg

has a background in Mechanical Engineering from the University of Ibadan, Nigeria and has been a front-end developer for over 3 years working on both web and mobile technologies. He also has experience as a technical author, writer, and reviewer. He enjoys programming for the web, and occasionally, you can also find him playing soccer. He was born in Benin City and is currently based in Lagos, Nigeria.

 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.218.91.239