ebook img

Mastering the Lightning Network: A Second Layer Blockchain Protocol for Instant Bitcoin Payments PDF

393 Pages·2021·21.675 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview Mastering the Lightning Network: A Second Layer Blockchain Protocol for Instant Bitcoin Payments

Mastering Lightning 1 Mastering the Lightning Network A Second Layer Blockchain Protocol for Instant Bitcoin Payments by Andreas M. Antonopoulos, Olaoluwa Osuntokun, and René Pickhardt Copyright © 2022 aantonop Books LLC, René Pickhardt, and uuddlrlrbas 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 • Development Editor: Michele Cronin • Production Editor: Kristen Brown • Copyeditor: Tom Sullivan • Proofreader: Sonia Saruba • Indexer: WordCo Indexing Services, Inc. • Interior Designer: David Futato • Cover Designer: Karen Montgomery • Illustrator: Kate Dullea • December 2021: First Edition Revision History for the First Edition • 2021-11-22: First Release See http://oreilly.com/catalog/errata.csp?isbn=9781492054863 for release details. The O’Reilly logo is a registered trademark of O’Reilly Media, Inc. Mastering the Lightning Network, 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, 2 it is your responsibility to ensure that your use thereof complies with such licenses and/or rights. This book is not intended as legal or financial advice; use the appropriate advice. Please consult a qualified professional if you require legal/financial advice. Mastering the Lightning Network is offered under the Creative Commons Attribution- Noncommercial-No Derivative Works 4.0 International License (CC BY-NC-ND 4.0). ISBN-13: 978-1492054863 ISBN-10: 1492054860 3 About the Authors Andreas M. Antonopoulos is a best-selling author, speaker, educator, and highly sought after expert in Bitcoin and open blockchain technologies. He is known for making complex subjects easy to understand and highlighting both the positive and negative impacts these technologies can have on our global societies. Andreas has written two more best-selling technical books for programmers with O’Reilly Media, Mastering Bitcoin and Mastering Ethereum. He has also published The Internet of Money series of books, which focus on the social, political, and economic importance and implications of these technologies. Andreas produces free educational content on his YouTube channel weekly and teaches virtual workshops on his website. Learn more at aantonop.com. Olaoluwa Osuntokun is the cofounder and CTO of Lightning Labs, and also the lead developer of lnd, one of the main implementations of Lightning. He received his BS and MS in Computer Science from UCSB and was a member of the Forbes 30 Under 30 class of 2019. During his graduate studies he focused on the field of applied cryptography, specifically encrypted search. He has been an active Bitcoin developer for over five years, and is an author of several Bitcoin Improvement Proposals (BIP-157 and 158). These days, his primary focus lies in building, designing, and evolving private, scalable off-chain blockchain protocols, such as Lightning. René Pickhardt is a trained mathematician and data science consultant who uses his statistical knowledge to do research with NTNU about pathfinding, privacy, reliability of payments, and service-level agreements of the Lightning Network. René maintains a technical and developer- oriented YouTube channel about the Lightning Network and has answered roughly half of the questions about the Lightning Network on the Bitcoin Stack Exchange, making him the go-to point for almost all new developers who want to join the space. René has held numerous workshops about the Lightning Network in public and private, including teaching students of the 2019 Chaincode Labs residency together with other core Lightning developers. 4 Preface The Lightning Network (LN) is a second layer peer-to-peer network that allows us to make Bitcoin payments "off-chain," meaning without committing them as transactions to the Bitcoin blockchain. The Lightning Network gives us Bitcoin payments that are secure, cheap, fast, and much more private, even for very small payments. Building on the idea of payment channels, first proposed by Bitcoin’s inventor Satoshi Nakamoto, the Lightning Network is a routed network of payment channels where payments "hop" across a path of payment channels from the sender to the recipient. The initial idea of the Lightning Network was proposed in 2015 in the groundbreaking paper "The Bitcoin Lightning Network: Scalable Off-Chain Instant Payments," by Joseph Poon and Thaddeus Dryja. By 2017, there was a "test" Lightning Network running on the internet, as different groups built compatible implementations and coordinated to set some interoperability standards. In 2018, the Lightning Network went "live" and payments started flowing. In 2019, Andreas M. Antonopoulos, Olaoluwa Osuntokun, and René Pickhardt agreed to collaborate to write this book. It appears we have been successful! How to Use This Book Intended Audience This book is mostly intended for technical readers with an understanding of the fundamentals of Bitcoin and other open blockchains. Conventions Used in This Book The following typographical conventions are used in this book: Italic Indicates new terms, URLs, email addresses, filenames, and file extensions. Constant width Used for program listings, as well as within paragraphs to refer to program elements such as variable or function names, databases, data types, environment variables, statements, and keywords. Constant width bold Shows commands or other text that should be typed literally by the user. Constant width italic Shows text that should be replaced with user-supplied values or by values determined by context. 5 TIP This element signifies a tip or suggestion. NOTE This element signifies a general note. WARNING This element indicates a warning or caution. Code Examples The examples are illustrated in Go, C++, Python, and using the command line of a Unix-like operating system. All code snippets are available in the GitHub repository under the code subdirectory. Fork the book code, try the code examples, or submit corrections via GitHub. All the code snippets can be replicated on most operating systems with a minimal installation of compilers, interpreters, and libraries for the corresponding languages. Where necessary, we provide basic installation instructions and step-by-step examples of the output of those instructions. Some of the code snippets and code output have been reformatted for print. In all such cases, the lines have been split by a backslash (\) character, followed by a newline character. When transcribing the examples, remove those two characters and join the lines again and you should see identical results to those shown in the example. All the code snippets use real values and calculations where possible, so that you can build from example to example and see the same results in any code you write to calculate the same values. For example, the private keys and corresponding public keys and addresses are all real. Using Code Examples If you have a technical question or a problem using the code examples, please send email to [email protected]. This book is here to help you get your job done. In general, if example code is offered with this book, you may use it in your programs and documentation. You do not need to contact us for permission unless you’re reproducing a significant portion of the code. For example, writing a program that uses several chunks of code from this book does not require permission. Selling or distributing examples from O’Reilly books does require permission. Answering a question by citing this book and quoting example code does not require permission. Incorporating a significant amount of example code from this book into your product’s documentation does require permission. We appreciate, but do not require, attribution. An attribution usually includes the title, author, publisher, ISBN, and copyright. For example: “Mastering the Lightning Network by Andreas M. Antonopoulos, Olaoluwa Osuntokun, and René Pickhardt (O’Reilly). Copyright 2022 aantonop Books LLC, René Pickhardt, and uuddlrlrbas LLC, ISBN 978-1-492-05486-3." Mastering the Lightning Network is offered under the Creative Commons Attribution- Noncommercial-No Derivative Works 4.0 International License (CC BY-NC-ND 4.0). If you feel your use of code examples falls outside fair use or the permission given previously, feel 6 free to contact us at [email protected]. References to Companies and Products All references to companies and products are intended for educational, demonstration, and reference purposes. The authors do not endorse any of the companies or products mentioned. We have not tested the operation or security of any of the products, projects, or code segments shown in this book. Use them at your own risk! Addresses and Transactions in This Book The Bitcoin addresses, transactions, keys, QR codes, and blockchain data used in this book are, for the most part, real. That means you can browse the blockchain, look at the transactions offered as examples, retrieve them with your own scripts or programs, etc. However, note that the private keys used to construct the addresses printed in this book have been "burned." This means that if you send money to any of these addresses, the money will either be lost forever or (more likely) appropriated, since anyone who reads the book can take it using the private keys printed herein. DO NOT SEND MONEY TO ANY OF THE ADDRESSES IN THIS BOOK. Your WARNING money will be taken by another reader, or lost forever. O’Reilly Online Learning For more than 40 years, O’Reilly Media has provided technology and business NOTE training, knowledge, and insight to help companies succeed. Our unique network of experts and innovators share their knowledge and expertise through books, articles, and our online learning platform. O’Reilly’s online learning platform gives you on-demand access to live training courses, in-depth learning paths, interactive coding environments, and a vast collection of text and video from O’Reilly and 200+ other publishers. For more information, visit http://oreilly.com. How to Contact Us Information about Mastering the Lightning Network as well as the Open Edition and translations are available at https://lnbook.info/. Please address comments and questions concerning this book to the publisher: 7 O Reilly Media, Inc. 1005 Gravenstein Highway North Sebastopol, CA 95472 800-998-9938 (in the United States or Canada) 707-829-0515 (international or local) 707-829-0104 (fax) Email [email protected] to comment or ask technical questions about this book. For news and information about our books and courses, visit http://oreilly.com. Find us on Facebook: http://facebook.com/oreilly Follow us on Twitter: http://twitter.com/oreillymedia Watch us on YouTube: http://www.youtube.com/oreillymedia Contacting Andreas You can contact Andreas M. Antonopoulos on his personal site: https://aantonop.com/ Subscribe to Andreas’s channel on YouTube: https://www.youtube.com/aantonop Like Andreas’s page on Facebook: https://www.facebook.com/AndreasMAntonopoulos Follow Andreas on Twitter: https://twitter.com/aantonop Connect with Andreas on LinkedIn: https://linkedin.com/company/aantonop Andreas would also like to thank all of the patrons who support his work through monthly donations. You can support Andreas on Patreon at https://patreon.com/aantonop. Contacting René You can contact René Pickhardt on his personal site: https://ln.rene-pickhardt.de/ Subscribe to René’s channel on YouTube: https://www.youtube.com/user/RenePickhardt Follow René on Twitter: https://twitter.com/renepickhardt Connect with René on LinkedIn: https://www.linkedin.com/in/rene-pickhardt-80313744/ René would also like to thank all of the patrons who support his work through monthly donations. You can support René on Patreon at https://patreon.com/renepickhardt. Or you can support his work directly with Bitcoin (also via the Lightning Network) at https://donate.ln.rene-pickhardt.de for which René is equally thankful as for his patreons. 8 Contacting Olaoluwa Osuntokun You can contact Olaoluwa Osuntokun at his professional email address: [email protected] Follow Olaoluwa on Twitter: https://twitter.com/roasbeef Acknowledgments By Andreas I owe my love of words and books to my mother, Theresa, who raised me in a house with books lining every wall. My mother also bought me my first computer in 1982, despite being a self- described technophobe. My father, Menelaos, a civil engineer who published his first book at 80 years old, was the one who taught me logical and analytical thinking and a love of science and engineering. Thank you all for supporting me throughout this journey. Acknowledgments By René I want to thank the German education system through which I acquired the knowledge upon which my work builds. It is one of the greatest gifts I was given. Similarly I want to thank the German public healthcare system and every person devoting their time into working within that industry. Your effort and endurance make you my personal heroes and I will never forget the help, patience, and support I received when I was in need. Thanks goes to all the students I was allowed to teach and who engaged in interesting discussions and questions. From you I learned the most. I am also grateful to the Bitcoin and Lightning Network community that warmly welcomed me and to the enthusiasts and private persons who financially supported and continue to support my work. In particular I am grateful to all the open source developers (not only Bitcoin and Lightning Network) and to the people who fund them to make that technology possible. A special thanks goes to my coauthors for riding with me through the storm. Last but not least, I am thankful to my loved ones. Acknowledgments by Olaoluwa Osuntokun I’d like to thank the amazing team at Lightning Labs, as without you all, there would be no LND. I’d also like to thank the original set of authors of the BOLT specification: Rusty Russell, Fabrice Drouin, Conner Fromnkchet, Pierre-Marie Padiou, Lisa Neigut, and Christian Decker. Last but not least, I’d like to thank Joseph Poon and Tadge Dryja, the authors of the original Lightning Network paper, as without them, there would be no Lightning Network to write a book about. Contributions Many contributors offered comments, corrections, and additions to the book as it was collaboratively written on GitHub. Following is an alphabetically sorted list of all the GitHub contributors, including their GitHub IDs in parentheses: • 8go (@8go) 9 • Aaqil Aziz (@batmanscode) • Alexander Gnip (@quantumcthulhu) • Alpha Q. Smith (@alpha_github_id) • Ben Skee (@benskee) • Brian L. McMichael (@brianmcmichael) • CandleHater (@CandleHater) • Daniel Gockel (@dancodery) • Dapeng Li (@luislee818) • Darius E. Parvin (@DariusParvin) • Doru Muntean (@chriton) • Eduardo Lima III (@elima-iii) • Emilio Norrmann (@enorrmann) • Francisco Calderón (@grunch) • Francisco Requena (@FrankyFFV) • François Degros (@fdegros) • Giovanni Zotta (@GiovanniZotta) • Gustavo Silva (@GustavoRSSilva) • Guy Thayakorn (@saguywalker) • Haoyu Lin (@HAOYUatHZ) • Hatim Boufnichel (@boufni95) • Imran Lorgat (@ImranLorgat) • Jeffrey McLarty (@jnmclarty) • John Davies (@tigeryant) • Julien Wendling (@trigger67) • Jussi Tiira (@juhi24) • Kory Newton (@korynewton) • Lawrence Webber (@lwebbz) • Luigi (@gin) • Maximilian Karasz (@mknoszlig) • Omega X. Last (@omega_github_id) • Owen Gunden (@ogunden) • Patrick Lemke (@PatrickLemke) • Paul Wackerow (@wackerow) • Randy McMillan (@RandyMcMillan) • René Köhnke (@rene78) 10

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.