Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Hands-On Blockchain for Python Developers

You're reading from   Hands-On Blockchain for Python Developers Gain blockchain programming skills to build decentralized applications using Python

Arrow left icon
Product type Paperback
Published in Feb 2019
Publisher Packt
ISBN-13 9781788627856
Length 450 pages
Edition 1st Edition
Languages
Concepts
Arrow right icon
Author (1):
Arrow left icon
Arjuna Sky Kok Arjuna Sky Kok
Author Profile Icon Arjuna Sky Kok
Arjuna Sky Kok
Arrow right icon
View More author details
Toc

Table of Contents (17) Chapters Close

Preface
Who this book is for
What this book covers
To get the most out of this book
Get in touch
1. Introduction to Blockchain Programming FREE CHAPTER 2. Smart Contract Fundamentals 3. Implementing Smart Contracts Using Vyper 4. Interacting with Smart Contracts Using Web3 5. Populus Development Framework 6. Building a Practical Decentralized Application 7. Frontend Decentralized Application 8. Creating Token in Ethereum 9. Cryptocurrency Wallet 10. InterPlanetary - A Brave New File System 11. Using ipfsapi to Interact with IPFS 12. Implementing a Decentralized Application Using IPFS

Coding on the blockchain

As this book is being written, the two most popular cryptocurrencies are Bitcoin and Ethereum (once in a while, Ripple will take second place). If you ask a simple question to someone who knows a lot about cryptocurrencies, you may get an answer: Bitcoin is just for sending money, but you can create a program on Ethereum. The program can be tokens, auction, or escrow, among many other things. But that is a half-truth. You can also create a program on Bitcoin. Usually, people call this program a script. In fact, it is a must to provide a script in a Bitcoin transaction. A transaction in Bitcoin can be mundane, so if I want to send you 1 BTC (a unit of currency in Bitcoin) and your Bitcoin address is Z, I need to upload a script like this into Bitcoin blockchain:

What's your public key? If the public key is hashed, does it equal Z? If yes, could you provide your private key to prove that you own this public key?

But it could be a little bit fancier. Let's say you want to require at least two signatures from four authorized signatures to unlock this account; you can do that with Bitcoin script. Think creative and you can come up with something like this:

This transaction is frozen until 5 years from now. Then business will be as usual, that the spender must provide public key and private key.

But a Bitcoin script is created with a simple programming language, incapable of even looping. It is stack-based. So, you put instructions: hash the public key, check a signature, and check the current time. Then, it will be executed on the Bitcoin node from left to right.

This means that you cannot create a fancy program, such as an auction, on Bitcoin. Bitcoin is designed just to store and transfer value (money). So it is purposely designed to avoid a complex program. In a Bitcoin node, every script is executed. Without a loop, a Bitcoin script will be so simple and you know when it will stop. But if you have a loop in a Bitcoin script, you don't know when it will stop. It could stop in the fourth iteration, or the millionth iteration, or in a far away future.

Some people were not satisfied with this limitation, so Ethereum was created. The programming language that you are equipped with on the Ethereum blockchain is much more sophisticated than the programming language in Bitcoin (there is a while or for construct). Technically speaking, you could create a program that runs forever in the Ethereum blockchain.

You can do what you can do in Bitcoin, which is store and transfer values. But there is so much more that you can do in Ethereum. You could create a voting program, an escrow service, an online auction, and even another cryptocurrency on top of it. So, people like to differentiate the currencies of Bitcoin (BTC) and Ethereum (ETH).  BTC is like digital gold. ETH is like oil and gas. Both are valuable, if we take that analogy. But, you can use oil and gas to create a whole new world, such as by creating plastics, fuel, and so on. On the other hand, what you can do with gold is quite limited, other than creating jewelry.

Creating a cryptocurrency on top of Ethereum is very easy. All you need is a weekend if you are a skilled programmer. You just inherit a class, and set your token's name and supply limit. Then, you compile it and launch to the Ethereum production blockchain, and you would have your own cryptocurrency. Prior to this, creating another cryptocurrency meant forking Bitcoin. The skill level required to do that is quite deep (C++, CMake, and replacing many parts of files in the Bitcoin core).

Other types of blockchain programmers

This chapter intended to give you an intuitive understanding of how blockchain works. However, it's not a complete scope of how it works. My explanation differs quite a lot from how Bitcoin works (and even Ethereum). Ethereum does not use SHA-256 for hashing; it commonly uses the Keccak-256 algorithm. In our case, we only put one history/transaction/payload in one block, but Bitcoin can save more than 1,000 transactions in one block. Then, we generate a private key and public key by using RSA cryptography, while Bitcoin and Ethereum use elliptic curve cryptography. In our case, the payload is history (who likes/loves/hates an animal), but in Bitcoin it's a transaction that has a dependency on the previous payload. In Ethereum itself, it's a state of programs. So, if you have variable a as equal to integer 5 in the payload, it could be something like change variable a to integer 7. In the Bitcoin consensus, we choose the blockchain that has the most hashing rate power, not the one that has the longest chain. For example, blockchain A has two blocks, but each block has the answer to solve the puzzle with 12 leading zeros, while blockchain B has ten blocks but each block has the answer to solving the puzzle with only five leading zeros. In this situation, blockchain A has the most hash rate power.

Now, we go back to the following questions: what does it mean to be a blockchain programmer? How many types of Blockchain programmers are there? What is the scope of this book?

Blockchain programming could mean that you are working on improving the state of Bitcoin or creating a fork of Bitcoin, such as Bitcoin Cash. You need C++ and Python. If you are creating a Bitcoin fork, such as Bitcoin Gold, you need to dig deeper into cryptography. In Bitcoin Gold, the developers changed the proof of work hashing function from SHA-256 to Equihash because Equihash is ASIC resistant. ASIC resistance means you cannot create a specific machine to do the hashing. You need a computer with a GPU to do the Equihash hashing function, but this book will not discuss that.

Furthermore, Blockchain programming could mean that you are working on improving the Ethereum Virtual Machine. You need Go, C++, or Python. You need to understand how to interact with low-level cryptographic library functions. An intuitive understanding of how basic cryptography works is not enough, but this book will not discuss that either.

Blockchain programming could mean that you are writing the program on top of Ethereum. You need Solidity or Vyper for this, which this book will discuss. You only need an intuitive understanding of how basic cryptography works. You have been abstracted away from low-level cryptography. Once in a while, you might use a hashing function in a program you write, but nothing fancy.

Blockchain programming could mean that you are writing a program to interact with the program on top of Ethereum, which sounds meta. But what you will need for this depends on the platform. If it is a mobile app, you need Kotlin, Java, Swift, Obj-C, or even C++. If it is a web frontend, you will most likely need JavaScript. Only an intuitive understanding of how basic cryptography works is needed. This book will discuss some of this.

This is the same as if I asked you, what does it entail when someone wants to become a web developer? The answer is quite diverse. Should I learn Ruby, Java, PHP, or Python? Should I learn Ruby on Rails, Laravel, or Django?

This book is going to teach you how to build a program on top of Ethereum (not to be confused with building Ethereum itself). Comparing this with web development, this is like saying that this book is going to teach you how to build a web application using Ruby on Rails, but the book does not teach you how to dissect the Ruby on Rails framework itself. This does not mean that the internals of Ruby on Rails are not important, it just means that most of the time, you don't need them.

This book will teach you to use the Python programming language, assuming that you have basic knowledge of Python already. But why Python? The answer is a cliché: Python is one of the easiest and most popular programming languages. It lowers the barrier to entry for someone who wants to jump into blockchain.

You have been reading a chapter from
Hands-On Blockchain for Python Developers
Published in: Feb 2019
Publisher: Packt
ISBN-13: 9781788627856
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at €14.99/month. Cancel anytime
Visually different images