Hands-On Blockchain for Python Developers
上QQ阅读APP看书,第一时间看更新

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