Hands-On Blockchain for Python Developers
eBook - ePub

Hands-On Blockchain for Python Developers

Gain blockchain programming skills to build decentralized applications using Python

Arjuna Sky Kok

Share book
  1. 450 pages
  2. English
  3. ePUB (mobile friendly)
  4. Available on iOS & Android
eBook - ePub

Hands-On Blockchain for Python Developers

Gain blockchain programming skills to build decentralized applications using Python

Arjuna Sky Kok

Book details
Book preview
Table of contents
Citations

About This Book

Implement real-world decentralized applications using Python, Vyper, Populus, and Ethereum

Key Features

  • Stay up-to-date with everything you need to know about the blockchain ecosystem
  • Implement smart contracts, wallets, and decentralized applications(DApps) using Python libraries
  • Get deeper insights into storing content in a distributed storage platform

Book Description

Blockchain is seen as the main technological solution that works as a public ledger for all cryptocurrency transactions. This book serves as a practical guide to developing a full-fledged decentralized application with Python to interact with the various building blocks of blockchain applications.

Hands-On Blockchain for Python Developers starts by demonstrating how blockchain technology and cryptocurrency hashing works. You will understand the fundamentals and benefits of smart contracts such as censorship resistance and transaction accuracy. As you steadily progress, you'll go on to build smart contracts using Vyper, which has a similar syntax to Python. This experience will further help you unravel the other benefits of smart contracts, including reliable storage and backup, and efficiency. You'll also use web3.py to interact with smart contracts and leverage the power of both the web3.py and Populus framework to build decentralized applications that offer security and seamless integration with cryptocurrencies. As you explore later chapters, you'll learn how to create your own token on top of Ethereum and build a cryptocurrency wallet graphical user interface (GUI) that can handle Ethereum and Ethereum Request for Comments (ERC-20) tokens using the PySide2 library. This will enable users to seamlessly store, send, and receive digital money. Toward the end, you'll implement InterPlanetary File System (IPFS) technology in your decentralized application to provide a peer-to-peer filesystem that can store and expose media.

By the end of this book, you'll be well-versed in blockchain programming and be able to build end-to-end decentralized applications on a range of domains using Python.

What you will learn

  • Understand blockchain technology and what makes it an immutable database
  • Use the features of web3.py API to interact with the smart contract
  • Create your own cryptocurrency and token in Ethereum using Vyper
  • Use IPFS features to store content on the decentralized storage platform
  • Implement a Twitter-like decentralized application with a desktop frontend
  • Build decentralized applications in the shape of console, web, and desktop applications

Who this book is for

If you are a Python developer who wants to enter the world of blockchain, Hands-On Blockchain for Python Developers is for you. The book will be your go-to guide to becoming well-versed with the blockchain ecosystem and building your own decentralized applications using Python and library support.

Frequently asked questions

How do I cancel my subscription?
Simply head over to the account section in settings and click on “Cancel Subscription” - it’s as simple as that. After you cancel, your membership will stay active for the remainder of the time you’ve paid for. Learn more here.
Can/how do I download books?
At the moment all of our mobile-responsive ePub books are available to download via the app. Most of our PDFs are also available to download and we're working on making the final remaining ones downloadable now. Learn more here.
What is the difference between the pricing plans?
Both plans give you full access to the library and all of Perlego’s features. The only differences are the price and subscription period: With the annual plan you’ll save around 30% compared to 12 months on the monthly plan.
What is Perlego?
We are an online textbook subscription service, where you can get access to an entire online library for less than the price of a single book per month. With over 1 million books across 1000+ topics, we’ve got you covered! Learn more here.
Do you support text-to-speech?
Look out for the read-aloud symbol on your next book to see if you can listen to it. The read-aloud tool reads text aloud for you, highlighting the text as it is being read. You can pause it, speed it up and slow it down. Learn more here.
Is Hands-On Blockchain for Python Developers an online PDF/ePUB?
Yes, you can access Hands-On Blockchain for Python Developers by Arjuna Sky Kok in PDF and/or ePUB format, as well as other popular books in Ciencia de la computación & Criptografía. We have over one million books available in our catalogue for you to explore.

Information

Year
2019
ISBN
9781788624640

Section 1: Blockchain and Smart Contracts

Introduction to blockchain technology, smart contracts, and decentralized applications.
The following chapters will be covered in this section:
  • Chapter 1, Introduction to Blockchain Programming
  • Chapter 2, Smart Contract Fundamentals
  • Chapter 3, Implementing Smart Contracts Using Vyper

Introduction to Blockchain Programming

In this book, we'll learn blockchain programming so that you can become a force to be reckoned with when finding blockchain opportunities. To achieve this, you need to begin by understanding blockchain technology and what it entails. In this chapter, we will learn what blockchain technology is. How does blockchain empower Bitcoin and Ethereum? We will get an intuitive understanding of blockchain technology. We will also replicate some basic functions behind blockchain.
The following topics will be covered in this chapter:
  • The rise of cryptocurrency and blockchain
  • Blockchain technology
  • Cryptography
  • The hashing function
  • Consensus
  • Coding on the blockchain

The rise of cryptocurrency and blockchain

Assuming that you didn't live a secluded life as a hermit on a mountain in 2017, you would have heard all about cryptocurrency, especially Bitcoin. You didn't have to look far to hear about the soaring popularity of this topic, its terminology, and its growth in value. At this point, other cryptocurrencies also began to grow, making way for headlines such as Ethereum reaches $1,000! During this craze, people discussed everything about cryptocurrency, from the swinging price to the technology behind it, which is blockchain.
Blockchain was regarded as the technology that would bring the dawn of a new era of justice and prosperity for mankind. It would democratize wealth. It would take the power away from the oligarchy and give it back to the people. It would protect the data of the people. Then came 2018, and cryptocurrency went down. The party was over. Bitcoin now sits at $6,000, while Ethereum sits at less than $400.
However, despite the fact that the hype surrounding cryptocurrency had died down, it still continues to be a regular point of discussion. Blockchain conferences and meetups are cropping up in many places, while investments keep pouring into blockchain startups. Andreessen Horowitz, a giant name in Silicon Valley, secured as much as $300 million from its limited partner in a dedicated blockchain fund. [1] In this case, the opportunities lie where the money flows into. Katheryn Griffith Hill, a lead recruiter at Blockchain Developers, claims that [2] there are currently fourteen blockchain developer positions available for every blockchain developer. In addition, a friend of mine who attended a local blockchain event in Jakarta commented on this, stating that I could see around one hundred audience members, but there were only around four or five developers. 50% of the audience were investors. There are people who want to put money into blockchain, but there are fewer people who are capable of developing the product.
Blockchain started to be used as a payment solution without the middleman, namely Bitcoin. Then, people found out that blockchain has some other properties that are interesting. First, it is transparent, meaning people can audit it to check whether there is money laundering going on or not. Second, it gives to some extent privacy for users, which can be used to avoid profiling.
Then, after Ethereum was released, people suddenly became creative with how to apply blockchain in real life. From creating a token to represent ownership of something, such as an autonomous organization or payment with full privacy, to digital assets that cannot be duplicated (unlike MP3 files).

Blockchain technology

Most people know Bitcoin exists because of blockchain. But what is blockchain? It is an append-only database that consists of blocks that are linked by hashing. Here, each block contains many transactions of transferring value (but could be other things) between participants secured by cryptography; a consensus between many nodes that hold an identical database decides on which new block is to be appended next.
You don't have to understand the definition at this point; those are a lot of words to chew on! First, I'll explain blockchain to you so that you can adjust to this new knowledge as we move through this book.
Going back to the definition of blockchain, we can summarize the definition as an append-only database. Once you put something into the database, it cannot be changed; there is no Undo. We'll talk about the ramifications of this feature in Chapter 2, Smart Contract Fundamentals. This definition entails many things and opens up a whole new world.
So, what can you put into this append-only database? It depends on the cryptocurrency. For Bitcoin, you can store the transactions of transferring value. For example, Nelson sends one Bitcoin to Dian. However, we accumulate many transactions into one block before appending them to the database. For Ethereum, the things that you can put into the append-only database are richer. This not only includes the transaction of transferring value—it could also be a change of state. What I mean by state here is really general. For example, a queue for buying a ticket for a show can have a state. This state can be empty or full. Similarly to Bitcoin, in Ethereum, you gather all the transactions before appending them together in this append-only database.
To make it clearer, we put all these transactions into the block before appending them to the append-only database. Aside from the list of transactions, we store other things in this block, such as the time when we append the block into the append-only database, the target's difficulty (don't worry if you don't know about this), and the parent's hash (I'll explain this shortly), among many other things.
Now that you understand the block element of the blockchain, let's look at the chain element. As previously explained, aside from the list of transactions, we also put the parent's hash in the block. But for now, let's just use a simple ID to indicate the parent instead of using a hash. Parent id is just the previous block id. Here, think of the stack. In the beginning, there is no block. Instead, we put Block A, which has three transactions: Transaction 1, Transaction 2, and Transaction 3. Since Block A is the first block, it has no parent. We then apply Block B to Block A, which consists of two transactions: Transaction 4 and Transaction 5. Block B is not the first one in this blockchain. Consequently, we set the parent section in Block B as the Block A id because Block A is the parent of Block B. Then, we put Block C in the blockchain, which has two transactions: Transaction 6 and Transaction 7.
The parent section in Block C would be the Block B id, and so on. To simplify things, we increment the id from 0 by 1 for every new block:
Let's implement a database to record the history of what people like and hate. This means that when you said you like cats at one point in history, you won't be able to change that history. You may add new history when you change your mind (for example, if you then hate cats), but that won't change the fact that you liked them in the past. So, we can see that in the past you liked cats, but now you hate them. We want to make this database full of integrity and secure against cheating. Take a look at the following code block:
class Block:
id = None
history = None
parent_id = None

block_A = Block()
block_A.id = 1
block_A.history = 'Nelson likes cat'

block_B = Block()
block_B.id = 2
block_B.history = 'Marie likes dog'
block_B.parent_id = block_A.id

block_C = Block()
block_C.id = 3
block_C.history = 'Sky hates dog'
block_C.parent_id = block_B.id
If you studied computer science, you will recognize this data structure, which is called a linked list. Now, there is a problem. Say Marie hates Nelson and wants to paint Nelson in a negative light. Marie can do this by changing the history of block A:
block_A.history = 'Nelson hates cat'
This is unfair to Nelson, who is a big fan of cats. So, we need to add a way in which only Nelson can write the history of his own preferences. The way to do this is by using a private key and a public key.

Signing data in blockchain

In blockchain, we use two keys to sign data, to authenticate a message and protect it from being altered by unauthorized users. The two keys are as follows:
  • Pri...

Table of contents