Wednesday 10 January 2018

Bitcoin Transaction 101: PART 2

In Part 1 we learned that a Bitcoin transaction records a transfer of ownership on the Bitcoin Blockchain. In this portion we will further breakdown the Bitcoin transaction properties and see how Bitcoin theft (or general fakery) happens by looking at a few high profile cases throughout the past years.


Side Note:
While writing this, I just read a post on LinkedIn about someone saying
"Bitcoin is such a scam and we don't even know who's controlling the software code"
Well obviously this person has not read a lot about Bitcoin and the Blockchain. However, if you have read our blog (and many others writing about the Blockchain) you will know why this statement is obtuse... I'm not a huge proponent of the Bitcoin 'gold-rush' that we are experiencing these days because the risk of that bubble bursting is real; but the Blockchain technology is a different ball game all together and is definitely something worth studying in more detail... rant over.

Before we proceed, let's recap a few key properties of a Bitcoin transaction.

1. Private Key
The private key is the first key generated when you register for a Bitcoin account. It must be kept secret. Think of the private key as your banking PIN number. Similar to your PIN number, if somebody knows your PIN, they can steal your money, but in cryptocurrency if you loose your private key you loose access to your coins. If you forgot how the key was generated, it is next to impossible to regenerate your private keys to reclaim your coins, especially if you are using a third party wallet. It may be unclaimed in the Blockchain forever.

2. Public Key
The public key is derived from your private key. Think of it as your bank account number. You can share your bank account number with another party for them to send you money. Same concept works in the Blockchain. But as we saw in Part 1, the public key is used to digitally sign a transaction and mark the transaction as proof of ownership.

3. Address
The address is just another representation of your public key. It has no meaning in the Blockchain and is just hashed to a more readable format and is represented as a QR Code in most Bitcoin wallets. You share this representation of your public key when requesting a coin transfer. 

4. Wallet
A wallet is where you can store all your addresses. Most cryptocurrency systems including trading platforms and exchanges will provide you with a wallet to track your addresses and transactions within the Blockchain. These are software based wallets, but there are multiple types of wallets from hardware based to even paper based wallets.


Now lets revisit our transaction from Part 1, with transaction id:
"1e25a20effbbcb121ea6900ec086f24256234144792ff9bf362c99e710b314a8"

Below is how it looks like in the Blockchain:

The header section:


The 'Hash' value is our transaction id. This is where your transaction is located within the block. The 'vin_sz' and 'vout_sz' can be literally translated as value in and value out size, which corresponds to the number of transactions within our Input List and Output List (as described in Part 1).

'Size' denotes the actual size of the transaction in bytes. Our transaction has a size of 140kb (kilobytes). A standard Bitcoin transaction ranges from 140kb to 255kb in size. The 'size' field is important because in Bitcoin, mining fees are charged by the size of the transaction, and not necessarily the amount transacted. 

The size of a transaction is actually how much information the transaction carries. Sizes can vary when there are more information carried within the code; such as more items within the Input or Output lists and multi signature transactions.

The "In" section


Within this portion of the code, we have two values, 'prev_out' and 'scriptSig'.

The 'prev_out' hash is the id of the origin transaction. The transaction that contains the total (unspent) Bitcoins that makes up the Input List. Remember a Bitcoin transaction 'collects' the total unspent coins from as many previous transactions as required in order to complete a transfer request. 

The total unspent coins collected from previous transactions must exceed the amount of the requested transfer plus mining fees.

The 'scriptSig' is the signature script. Data that is generated (by the sender) as proof of ownership of the transactions within the Input List and enables the network to generate the Output List and re-allocate the value ownership within this transaction. 

The scriptSig can be made extremely complicated because a transaction in Bitcoin can be signed by more than one sender (a concept known as multi-signature transaction... we will talk about multi-signature process in a later post). 

The "Out" Section


This portion of the code has two similar components with a 'value' and a 'scriptPubKey'.

The 'value' is simply the amount of BTC involved in the transfer and the 'scriptPubKey' is the actual hash of the address of the recipient(s) of the transaction

In the first portion, the 'value' of 2.2 BTC is clearly the amount that was sent to us from the faucet in Part 1 while it's 'scriptPubKey' is a hash of our Bitcoin Address within the Blockchain.

The second portion 'value' of 1972.16814575 BTC was the amount attributed back to the sender, minus mining fees of 0.00100000 BTC, while its 'scriptPubKey' is the hash of the Bitcoin Address of the faucet owner. He now becomes the recipient of the balance amount from the original transaction.

So from that small portion of code, the Blockchain did so many things, and with the help of the Bitcoin Blockchain network we managed to confirm that the sender actually owns and has enough unspent coins to complete the 2.2 BTC transfer to us and we are able to verify the origin transactions that made up his unspent coins.

Confirmations

Another concept that you need to know about are 'confirmations'. In the Bitcoin Blockchain there is a confirmation field that continues to increment as the transaction gets propagated into the network.

Lets take a look at our transaction at the time of me writing this article:

Notice that our transaction from Part 1 at the time of writing this article, has 2,907 confirmations. You can click here to see how many confirmations it has at the time of you are reading this article: View Transaction

The official Bitcoin Wiki defines Confirmations as: 

'After a transaction is broadcast to the Bitcoin network, it may be included in a block that is published to the network. When that happens it is said that the transaction has been mined at a depth of 1 block. With each subsequent block that is found, the number of blocks deep is increased by one. To be secure against double spending, a transaction should not be considered as confirmed until it is a certain number of blocks deep.'

Remember that a block in the Bitcoin Blockchain is committed to the network every 10 minutes? Confirmations are incremented every time a new transaction is recorded and verified by the block. So  to simplify the concept, a new transaction is confirming that an old transaction exists in the block.

Most exchanges or wallets today require a transaction to be confirmed at least 6 times (which roughly equals to 1 hour after being committed to the block) before it  can be spent to perform another transaction. Some have an incremental spending amount based on the number of confirmations per transaction.

Side story:
In 2015, an Australian software expert Craig Wright, claimed that he was Satoshi Nakamoto. Craig had successfully convinced many people that we was Satoshi through social media and email correspondences. 

If his claim was true, then he would be the person in possession of the famed 1,148,800 unspent Bitcoins and at today's rates would have made him a bona fide Bitcoin bagajillionaire (You can read more about Satoshi's unspent Bitcoin calculation in Sergio Demian's blog entry titled The Well Deserved Fortune of Satoshi Nakamoto).

However, with how Bitcoin transactions are structured, it was quite easy to verify his claim. In the book 'Programming The Blokchain in C#', Nicholas Dorier demonstrated how easy it was to verify transaction ownership in Bitcoin by using a mere 5 lines of code. However the sender (or Craig in this case) needed to participate in the verification process, but it merely required the sender generate a message in the Blockchain using his public address and verifying that message with his public signature. An interesting article about Craig Wright can be read here: The Craig Wright Story

So if the Blockchain cryptography and architecture is so secure:

Why do we keep on hearing about Bitcoins being hacked and stolen?

Let's try to apply what we've learned from the first section of this article...

Remember the key properties of a cryptocurrency transaction? The private key, public key, address and wallets... Bitcoins can be 'stolen' by manipulating these items. It is not easy to do, but let's study a few concepts and high profile cases that occurred over the past few years to understand why and how it happened.

1. Transaction Malleability

Sorry big word... 'malleability' is defined as a substance's ability to deform under pressure. 

In Bitcoin it means that a transaction can still be changed before it is confirmed in the Bitcoin network. An example of this happening was a hack on Mt. Gox Bitcoin exchange in 2014.

In this instance the hackers manipulated the sender's 'scriptSig' within the 'in' section of the transaction code to make it as if the transaction was sent from another address which in turn changes the transaction id

For the actual sender then, it would have been like the transaction didn't happen at all since the transaction id recorded on his side did not actually exist anymore! But as you know, the blockchain is still able to locate ownership of that transferred value, because the 'out' section still attributes the value of transfer to the receiver (possibly the hacker) and the balance to the sender (also the hacker).

The original sender, thinking that his transaction didn't go thru, performs the transaction again, sending the coins a second time, but in fact the receiver now actually receives double the value. This is also called 'double spending' but the hackers have effectively manipulated the transaction structure to their advantage.

This hack resulted in USD $473 million worth of Bitcoin (7% of the world's supply at the time) to be stolen and laundered which in turn bankrupted the whole Mt. Gox exchange.

But still, due to the traceability of Bitcoin transactions and a good dose of detective work, it was found a year later that the stolen Bitcoins were laundered through another exchange called BTC-e, the owner of that exchange was arrested in Greece and accused of laundering stolen Bitcoins. He is currently facing a 55 year jail term if extradited to the US.

The Mt. Gox exchange hacks are attributed to a lack of proper management procedures and processes which enabled hackers to take advantage of flaws in the exchange design and abuse the system. For example, Mt. Gox allows a transaction to be cancelled and resubmitted, and a miner on Mt.Gox exchange can be accredited with a transaction even before it is confirmed by the network. 

Andreas Antonopoulos, a respected technologist and author on Bitcoin technology commented:

“Mt. Gox has built an exchange based on a hodgepodge of technologies that are really not suitable for running an exchange. And it’s being run by people who don’t really have experience building and operating scalable systems.”

2. DDOS Attacks

DDOS (Distributed Denial of Service) attacks are launched by a network of compromised computers (sometimes literally number in the millions), floods a server with multiple types of transaction requests (logins, signups, transactions etc) which cripples it from processing any actual transactions. 

In cryptocurrency, DDOS attacks does not necessarily steal any coins, but by crippling the cryptocurrency network, it is able to lock users out from their accounts for up to days or weeks at a time because the owners of an exchange or ICO developers have to shut down their servers in order to restructure their networks.

By locking users out of their accounts, the cryptocurrency is slowly devalued due to a loss of confidence in the its network, existing investors are not able perform any trades and no new investors are able to participate in the network. The currency's drop in value is the main reason of the total loss in its user's initial investments.

Most recent example of DDOS attacks which have successfully destabilized cryptocurrency networks are the attacks on the launch of Bitcoin Gold and the new cryptocurrency Electroneum both in mid 2017.

3. The Private Key

As we have established, the private key is the most important aspect of cryptocurrency as it allows a value transfer to be initiated. 

Any type of wallet (be it software based, hardware based or even paper based) which stores their user's private keys are ripe for attacks from hackers.

There used to be cryptocurrency wallets that generate your private key from your email address, first or last name, date of birth, mother's maiden name etc. These 'brain-wallet' address have private keys which are generated by simple or normal phrases which are too closely linked to the individual

These wallets were easily hacked by hackers who (most of the time) have already hacked the person's identity through their email or social media. 

Worse still some of these wallets actually stored their user's private keys in unsecured and un-encrypted databases or allow users to backup their private keys to cloud storage such as Dropbox. 

These practices are counterproductive because there were no protocols which govern how private keys should be stored outside of the network.

In 2013, an anonymous marketplace called Sheep Marketplace offered services to store private keys for a large number of users. Sure enough one of the site's vendors exploited a vulnerability in the marketplace to steal 5400 BTC worth (at the time) USD $6 million.

The BIP

Because Bitcoin (and cryptocurrency in general) does not have a formal structure, people working on them started developing Bitcoin Improvement Proposal documents called BIPs. The first document BIP 001 was submitted by Amir Taaki on 19th August 2011, which simply describes what is a BIP and how to use it. 

Today we have over a hundred BIPs proposing new standards and protocol improvements to the Bitcoin network. 

These proposals cover everything from improving hashing methods to generate private keys to better ways to create and store wallet addresses.

You can take a look at the current BIPs here: GitHub BIP

In conclusion

So we have learned today that over the course of the past years, the Bitcoin network has had its share of flaws and exploits. One can make a case that these incidents are a result of human error, oversight and maybe just pure inexperience running such a (relatively) new protocol. 

One can also say that nothing man made is fool proof. Such as the case of the recent Meltdown and Spectre vulnerabilities found on our Intel x86 and ARM microprocessors, which apparently effects microprocessors built dating back since 1995.... ouch.

The incidents that affected Mt. Gox exchange for example, effectively put back public acceptance and adoption of Bitcoin and cryptocurrency by at least a few years. 

Today, Bitcoin has bounced back strong with a market cap of over USD $200 billion. We have established portals and wallets such as coinbase.com, blockchain.info, ledgerwallet.com and trezor.io which have successfully established themselves as leading players in the game. 

We also have seen the rise of other cryptocurrencies that are utilizing the Blockchain to execute smart contracts and decentralized applications and are opening up more possibilities of future innovation in the technology.

Will there ever be another Mt. Gox incident? Let's hope not! Bitcoin and blockchain acceptance has increased by leaps and bounds over this year alone as more and more people begin to have a deeper understanding of how it works. 

As hackers and application developers continue to play this cyber security cat and mouse game, technology will continue to advance forward and its stability and robustness is the key to general acceptance by the public.

No comments:

Post a Comment

Popular Posts