Reader small image

You're reading from  Hands-On Blockchain for Python Developers

Product typeBook
Published inFeb 2019
Reading LevelExpert
PublisherPackt
ISBN-139781788627856
Edition1st Edition
Languages
Concepts
Right arrow
Author (1)
Arjuna Sky Kok
Arjuna Sky Kok
author image
Arjuna Sky Kok

Arjuna Sky Kok has experience more than 10 years in expressing himself as a software engineer. He has developed web applications using Symfony, Laravel, Ruby on Rails, and Django. He also has built mobile applications on top of Android and iOS platforms. Currently, he is researching Ethereum technology. Other than that, he teaches Android and iOS programming to students. He graduated from Bina Nusantara University with majors in Computer Science and Applied Mathematics. He always strives to become a holistic person by enjoying leisure activities, such as dancing Salsa, learning French, and playing StarCraft 2. He lives quietly in the bustling city of Jakarta. In loving memory of my late brother, Hengdra Santoso (1979-2011).
Read more about Arjuna Sky Kok

Right arrow

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...

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...

Cryptography

The most popular use of blockchain is to create a cryptocurrency. As the word crypto is in cryptocurrency, you would expect that you need to master cryptography in order to become a blockchain programmer. That is not true. You only need to know two things about cryptography:

  • Private key and public key (asymmetric cryptography)
  • Hashing

These two have been explained in the previous part of this chapter. You don't need to know how to design a hashing algorithm or private key and public key algorithm. You only need to get an intuitive understanding of how they work and the implications of these technologies.

The implication of private keys and public keys is that it enables decentralized accounts. In a normal application, you have a username and password. These two fields enable someone to access their account. But having a private key and public key enables someone...

The hashing function

Hashing is a function that takes an input of any length and turns it into a fixed length output. So, to make this clearer, we can look at the following code example:

>>> import hashlib
>>> hashlib.sha256(b"hello").hexdigest()
'2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824'
>>> hashlib.sha256(b"a").hexdigest()
'ca978112ca1bbdcafac231b39a23dc4da786eff8147c4e72b9807785afee48bb'
>>> hashlib.sha256(b"hellohellohellohello").hexdigest()
'25b0b104a66b6a2ad14f899d190b043e45442d29a3c4ce71da2547e37adc68a9'

As you can see, the length of the input can be 1, 5, or even 20 characters, but the output will always be the length of 64 hexadecimal numeric characters. The output looks scrambled and it appears that there is no apparent link between the input and the output...

Consensus

As we can see, the hashing function makes history tampering hard, but not too hard. Even if we have a blockchain that consists of 1000 blocks, it would be trivial to alter the content of the first block and change the 999 parent hashes on the other blocks with recent computers. So, to ensure that bad people cannot alter the history (or at least make it very hard), we distribute this append-only database to everyone who wants to keep it (let's call them miners). Say there are ten miners. In this case, you cannot just alter the blockchain in your copy because the other nine miners who would scold, saying something like hey, our records say history A but your record says B. In this case, the majority wins.

However, consensus is not just a case of choosing which blockchain has been chosen by the majority. The problem starts when we want to add a new block to the blockchain...

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...

Summary

In this chapter, we looked into the technology behind cryptocurrencies such as Bitcoin and Ethereum. This technology enables the decentralization of storing values or code. We also covered cryptography by using private and public keys to secure the integrity of any data. Further on, we learned about hash functions, proof of work, consensus, and the basic concepts of blockchain programming.

In the next chapter, we will learn about a smart contract, a kind of program that lives in Ethereum. A smart contract is different than a kind of program that lives in a server, such as an application written with Ruby on Rails, Laravel, or Django. The differences are more than just the syntax; the concept is radically different than a normal web application.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Blockchain for Python Developers
Published in: Feb 2019Publisher: PacktISBN-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.
undefined
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 $15.99/month. Cancel anytime

Author (1)

author image
Arjuna Sky Kok

Arjuna Sky Kok has experience more than 10 years in expressing himself as a software engineer. He has developed web applications using Symfony, Laravel, Ruby on Rails, and Django. He also has built mobile applications on top of Android and iOS platforms. Currently, he is researching Ethereum technology. Other than that, he teaches Android and iOS programming to students. He graduated from Bina Nusantara University with majors in Computer Science and Applied Mathematics. He always strives to become a holistic person by enjoying leisure activities, such as dancing Salsa, learning French, and playing StarCraft 2. He lives quietly in the bustling city of Jakarta. In loving memory of my late brother, Hengdra Santoso (1979-2011).
Read more about Arjuna Sky Kok