© The Author(s) 2018
Sarah Swammy, Richard Thompson and Marvin LohCrypto Uncoveredhttps://doi.org/10.1007/978-3-030-00135-3_7

7. Creation of a Distributed Ledger

Sarah Swammy1  , Richard Thompson2   and Marvin Loh3  
(1)
State Street Global Market, LLC, New York, NY, USA
(2)
Digital Air Technologies, New York, NY, USA
(3)
Bank of New York Mellon, New York, NY, USA
 
 
Sarah Swammy (Corresponding author)
 
Richard Thompson
 
Marvin Loh

Keywords

BlockchainBitcoinMiningHashingCrypto currency walletSecurity

Blockchain

Over the last few years the words blockchain, Bitcoin, and mining have been buzzing around all over the internet. Before we jump right into the details of crypto currency, I want to explain these buzzwords, which will help us absorb the later sections with much more clarity. Blockchain is a continuously growing list of records called blocks. Every block in the blockchain typically contains a cryptographic hash of the previous block, a timestamp, and transaction data. Blockchain was designed so that the transactions are immutable, meddle-proof, and distributed. Figure 7.1 demonstrates a very simple visualization of a blockchain.
../images/465002_1_En_7_Chapter/465002_1_En_7_Fig1_HTML.png
Fig. 7.1

Blockchain

Let’s take a little deeper insight into the blockchain and analyze it from bottom up starting from a Hash.

Hash

In modest terms, hashing is a software concept of taking an input data of any length and running it through a cryptographic algorithm and producing an output of a fixed length. A Hash is a bunch of random alphanumeric characters. It is like a fingerprint of some digital data. Let’s explore how the hashing process works. We are going to put in certain inputs. Following is a set of Hash being generated for different set of data.

Data

Hash

Hi

3639EFCD08ABB273B1619E82E78C29A7DF02C1051B1820E99FC395DCAA3326B8

Blockchain

625DA44E4EAF58D61CF048D168AA6F5E492DEA166D8BB54EC06C30DE07DB57E1

How are you?

DF287DFC1406ED2B692E1C2C783BB5CEC97EAC53151EE1D9810397AA0AFA0D89

Crypto currency

6EC60FE39028887E7FE9C4B025545748953C27515073BF9AD17CEB5417A407D7

In the above examples, the input string is of variable lengths, but the output always has a fixed 256-bit length. This turns out to be even more complex when you are dealing with a huge amount of data and transactions. So basically, a hash is unique for every input, which makes it more convenient to store the hash instead of the original string wherever appropriate. The best example is password storage where the actual password string does not get stored in the database; instead, you keep just the hash of the password which makes your system more secure. Let’s now analyze the internals of the hashing procedure and see how we can use it in a blockchain.

A cryptographic hash function is a complex mathematical algorithm which has certain properties which are considered to be secure.
  1. 1.

    Deterministic—Processing the same input multiple times always produces the same result. This is very important because if you get different hashes for multiple iterations of the same input then it will be impossible to keep track of the input.

     
  2. 2.

    Quick Computation—The hash function should be capable of returning the hash of an input quickly. If the process isn’t fast enough then the system simply won’t be efficient.

     
  3. 3.

    Pre-Image Resistance—For any given hash value H(A) it is infeasible to determine A, where A is the input and H(A) is the output hash.

     
  4. 4.

    Pseudorandomness—For any small change in the input, the changes that will be reflected in the hash will be huge. Let’s check it out using SHA-256. The words Single and Mingle have just a one alphabet different but their hashes are completely different.

     

Single

8888A029AAF60B70574640EFD1655343D1C46C692918C113C16F44F606477253

Mingle

5FB188B33D53EAD28780E22822A34CE1C624740A6F5C85C7AF0D12607EAF5D51

  1. 5.

    Collision Resistant—Given two different inputs X and Y where H(X) and H (Y) are their respective hashes, it is infeasible for H (X) to be equal to H(Y). So, for the most part, each input will have its own unique hash.

     
  2. 6.

    Puzzle-Friendly—For every single output “Y,” if k is selected from a distribution with high min-entropy it is infeasible to determine an input x such that H(k|x) = Y. Assume you have an output value “Y.” If you select a random value “k” from a wide distribution, it is infeasible to determine a value X to the extent that the hash of the concatenation of k and x will produce the output Y. Please note the word “infeasible,” it is not impossible. In fact, the mining process works upon this.

     
Following are a few examples of cryptographic hash functions:
  • MD 5: Produces a 128-bit hash. Collision resistance was broken after ~2^21 hashes.

  • SHA 1: Produces a 160-bit hash. Collision resistance broke after ~2^61 hashes.

  • SHA 256: Produces a 256-bit hash. This is currently being used by Bitcoin.

  • Keccak-256: Produces a 256-bit hash and is currently used by Ethereum.

Block

A block is the most recent and current part of a blockchain, which registers some or all of the latest transactions. Once finalized, the block goes into the blockchain as a permanent catalog. Each time a block gets finalized, a new one is generated. There is an endless number of such blocks in the blockchain, linked to each other (like links in a chain) in proper linear, chronological order. Every block holds a hash of the previous block. The blockchain has comprehensive information about different user addresses and their balances right from the genesis block to the most recently finalized block. Below is a sample of a block that consists of the following properties. This is a very simplified block and is not a replica of Bitcoin.
  • Block Identifier, which is a sequential number.

  • Nonce, which is a random number.

  • Tx, which is the actual transaction information.

  • Prev, which is the hash of the previous block.

  • Hash, which is the computed hash value of the data and Nonce.

As discussed in the earlier sections, every string will have its own unique hash value. Any small change in the string will have a substantial change in the value of the Hash. If you take a much closer look at the hash value in Fig. 7.2, the Hash value starts with a particular format starting with four zeros “0000,” which is very uncommon and more difficult to arrive at. This pattern of the hash value in a block is achieved in the process of mining. Miners have to determine the value of the random number called the Nonce which when combined with the transaction data will result in the hash that has the patter on four leading zeros. When this hash is produced the block is considered signed. When a miner successfully signs the block before any other miner does, he or she receives the block reward and the block is added to the blockchain.
../images/465002_1_En_7_Chapter/465002_1_En_7_Fig2_HTML.png
Fig. 7.2

Block structure

Mining

Miners enter the Bitcoin network by configuring the Bitcoin core software and building a node environment on a computer that meets minimum resource requirements. After completing the environment build, a potential miner performs the initial block download that synchronizes nodes within the network downloads block and points the node to the tip of the best blockchain.

A node (computer) performing a transaction on the network has to broadcast the transaction to all the other nodes in the network. Each node in the network has to solve a mathematical problem to verify that the transaction is valid. Once verified, the node has to vote YES. At least 51% of the nodes on the network have to vote YES for the transaction to be successful. This process of verifying the transaction using distributed processing is called mining and the nodes that are involved in this verification process are called miners. The first node that verifies the transaction gets a small reward for a job well done. Figure 7.3 illustrates the mining process.
../images/465002_1_En_7_Chapter/465002_1_En_7_Fig3_HTML.png
Fig. 7.3

Mining process

Once a Bitcoin transaction is propagated on the Bitcoin network, it does not become part of the shared ledger (the blockchain) until it is verified and included in a block in a process called mining. The Bitcoin mining process serves two purposes in Bitcoin:
  • Mining generates new Bitcoins in each block, practically like a central bank printing new money. The amount of Bitcoin generated per block is fixed and diminishes with time.

  • Mining generates trust by ensuring that transactions are only confirmed if enough computational power was devoted to the block that contains them. More blocks require more computation, which eventually means more trust.

The Bitcoin system of trust is fundamentally based on computation. Transactions are bundled into blocks, which requires a huge amount of computation to prove, but only a little amount of computation to verify as proven, in a process called mining. Bitcoin mining is purposely designed to be extremely resource-intensive and difficult so that the number of blocks found each day by miners remains fixed. So, a good way to describe mining is like a puzzle that resets every time somebody finds a solution and its difficulty automatically adjusts so that it takes about ten minutes to find a solution. The puzzle used in Bitcoin is developed based on a cryptographic hash, which is asymmetrically difficult to solve, but at the same time easy to verify and its difficulty can be adjusted. Finding such a solution, in blockchain terms “Proof of Work,” requires millions of hashing operations per second, across the entire Bitcoin network. The algorithm for Proof of Work involves hashing the header of the block and a random number with the SHA256 cryptographic algorithm, until a solution matching a predetermined pattern emerges. The first miner who finds such a solution wins the round of competition and publishes that block into the blockchain. Every ten minutes, miners produce a new block, which comprises all the transactions since the last block. New transactions are continuously flowing into the blockchain network from user wallets and various other sources. As these transactions enter into the Bitcoin network nodes, they get collected into a temporary unverified transaction pool of each Bitcoin node. When miners build a new block of transactions, they pick the unverified transactions from this pool and then try to solve a very difficult problem (Proof of Work) to prove the validity of that new block.

Taking from where we left the previous chapter, our dinner guy, who also happens to be a miner is now sitting before his ASIC computers mining for Bitcoins. So, his computers are now trying to find the Proof of Work like thousands of others at the same time. Let’s say for the sake of luck, Harry finds the solution now before others on the network. As soon as the solution is found, it validates all transactions within the block and the block is not pushed publicly to the network. All nodes now receive the new block and continue to find the Proof of Work for the next block (with the new transactions) (Fig. 7.4).
../images/465002_1_En_7_Chapter/465002_1_En_7_Fig4_HTML.png
Fig. 7.4

Mining rate management

The Bitcoin mining network difficulty is the measure of how difficult it is to find a new block. It is recalculated every 2016 blocks to a value such that the previous 2016 blocks would have been generated in exactly two weeks had everyone been mining at this difficulty. This will yield, on average, one block every ten minutes.

Transactions get added to the new block, prioritized by the highest-paying transactions first and a few other inclusion criteria. When a miner receives a block confirmation from the network, it means they have lost the competition and must start a new block. Each miner starts constructing a new block of transactions as soon as they receive the previous block from the network and start calculating the Proof of Work for the new block. Each miner includes a special transaction in their block, one that pays their own Bitcoin address a reward of newly created Bitcoins. Currently the Bitcoin network pays about 12.5 Bitcoins per block which used to be 50 Bitcoins a couple of years back. If a miner finds a solution that makes that block valid, he or she wins this reward for the successful block that is added to the global blockchain. Bitcoin mining difficulty is a measure of how difficult it is to find a hash below the target value (Fig. 7.5).
../images/465002_1_En_7_Chapter/465002_1_En_7_Fig5_HTML.png
Fig. 7.5

Mining process flow

As every new block added to the blockchain is based on the previous block added, it adds even more computation on top of that block, thereby increasing the trust in those transactions. The blocks that are mined after the one that contains your transaction act as additional assurance; as more blocks get piled up more computation is required in a longer and longer chain. Each block that is mined on top of the one containing your transaction is called one confirmation for that transaction. As more blocks get added on top of each other, it becomes exponentially difficult to reverse the transaction, thereby making it increasingly trusted by the network. By convention, any block with more than six confirmations is considered irrevocable, as it would require an enormous amount of computation to invalidate and recalculate six blocks.

Bitcoin

A strange person by the name Satoshi Nakamoto published a white paper in 2008 and explained what a blockchain would look like and explained how to run a value system on it. Subsequently he created Bitcoin as a distributed network that maintains a ledger of balances in chronological order. Anyone who has a record in this network is said to possess Bitcoins. A record in the Bitcoin ledger has an address and transactions. The address determines the identity and the transaction determines the value. As the popularity of Bitcoin started to grow more people wanted to acquire Bitcoins, and many merchants started to accept Bitcoins in place of dollars and euros. Slowly Bitcoin evolved as a currency system which runs on the internet powered by blockchain and cryptography. Satoshi Nakamoto’s original paper is still recommended reading for anyone studying how Bitcoins work. Below is the link to download a copy of the Bitcoin white paper published in 2008 by Satoshi Nakamoto.
A transaction is said to be an interaction between a user and the system. To understand it, let’s take a few examples here.
  • Facebook: Like, Share, and Comment are a few of the social transactions between the user and the system.

  • Twitter: Tweet and Retweet are the basic transactions that a user makes on the system.

  • Github: The developer user interacts with the system via Push, Pull, Merge, and so on.

  • Crypto currency/Bitcoin: Send money is a transaction.

The example below illustrates how the ledger is maintained in a blockchain.
  • Transaction 1: Dave sends $25 to Emily at 1/1/2018 5:00 PM UTC

  • Transaction 2: Dave sends $15 to Joan at 1/1/2018 5:20 PM UTC

  • Transaction 3: Joan sends $56 to Mike at 1/1/2018 5:45 PM UTC

  • Transaction 4: Mike sends $35 to Emily at 1/1/2018 6:00 PM UTC

As you can notice, the transactions are added to the block in the chronological order by the time of the transaction, and all the transactions in a blockchain are immutable. This ensures that there is no way somebody can trick the system to make a double spend. Also, blockchain is a decentralized system. The banks that we have in the world are mostly running a centralized system. If the bank gets hacked or encounters financial trouble, everybody’s money gets lost. Blockchain is a decentralized system, where all the nodes in the system contain the complete database of all transactions. So, in order to break down the system, at least 51% of the nodes need to be hacked, which is nearly impossible. Figure 7.6 illustrates a centralized and a decentralized system.
../images/465002_1_En_7_Chapter/465002_1_En_7_Fig6_HTML.png
Fig. 7.6

Centralized versus decentralized system

Bitcoin crossed 500,000 blocks during the time of writing this book. We will deal with blockchain and Bitcoin in greater depth in the next chapter. I am just scratching the surface in this chapter so that we can understand the basics of crypto currency. Bitcoin is the first crypto currency that is sustainable and publicly accepted. All other crypto currencies are called alternative coins or altcoins for short. There are more than 1000 altcoins at the time of this writing. As more and more people started buying Bitcoins, it became more expensive and difficult for people to buy one whole Bitcoin at the price of $9000 at the time of this writing. But you can also buy a satoshi which is a fraction of the Bitcoin.
../images/465002_1_En_7_Chapter/465002_1_En_7_Figc_HTML.png
In future satoshi may also become expensive, but there is a lot of time for that to happen. Let’s look at the price pattern of Bitcoin during the last one year which explains when the demand started and how the value is sustained (Fig. 7.7).
../images/465002_1_En_7_Chapter/465002_1_En_7_Fig7_HTML.png
Fig. 7.7

Bitcoin price chart. (From CoinMarketCap.​com)

Crypto Currency Wallet

Crypto currency wallets are software applications that store your public and private keys and interface with various blockchains and provide functionalities to check the balance, send money, and conduct other operations. When a wallet user sends Bitcoins or any other type of digital currency to you, they are basically signing off ownership of the coins to the sender’s wallet address. To be able to realize the funds, the private key stored in your wallet software must match the public address the currency is attached to. If public and private keys of the sender and receiver match, the balance in your wallet software will increase, and consequently the sender’s account balance will decrease. There is no actual exchange of physical coins. The transaction is implied merely by a transaction record on the blockchain and a change in balance in your crypto currency wallet (Fig. 7.8).
../images/465002_1_En_7_Chapter/465002_1_En_7_Fig8_HTML.png
Fig. 7.8

Crypto currency wallets

Let us now understand this process with an example. Let Mark be a customer who has a Bitcoin wallet and is now ready to receive funds.
  • Once Mark signs up for a Bitcoin account, his wallet application will randomly generate a private key together with its corresponding Bitcoin address.

  • His Bitcoin address is merely a number that corresponds to a key that he can use to control access to the funds. There is no association between that address and an account.

  • Until the moment when this address is participating in a Bitcoin transaction as a sender or a receiver (recipient) of value posted on the Bitcoin ledger (the blockchain), it is simply part of the massive number of possible addresses that are valid in a Bitcoin.

  • Once the address has been associated with a transaction, it becomes part of the identified addresses in the network and Mark can check his balance on the public ledger (explained later).

Let’s say Mark owns a coffee shop and a customer, say, Harry, chooses to pay for dinner using Bitcoins. Mark has displayed his address or the QR code of his Bitcoin address at the top of the menu board in his shop for his customers to be able to pay with their Bitcoins. This is the public key of this wallet which is used for receiving the funds. Let’s consider the cost for dinner to be 0.10 Bitcoin, also known as 100 millibits. To transfer the Bitcoins to Mark’s account, if Harry is using the blockchain mobile wallet on an Android phone, he would see a screen requesting two inputs:
  1. 1.

    Receiver: The destination Bitcoin address for the transaction

     
  2. 2.

    Value: The amount of Bitcoin to send

     

Most of the crypto currency wallets provide a feature called send by QR code. Around the input field for the Bitcoin address, there is a small icon that looks like a QR code. This allows Harry to scan the barcode with his smartphone camera so that he doesn’t have to type in Mark’s Bitcoin address (e.g., 1Cdid9KFAaatwczBwBttQcwXYCpvK8h7FK), which is quite long and difficult to type. The mobile wallet application fills in the Bitcoin address scanning the QR code and Harry can check that it scanned correctly by comparing a few digits from the address with the address displayed by Mark. Harry then enters the Bitcoin value for the transaction, 0.10 Bitcoin. He then presses Send to transmit the transaction. Harry’s mobile Bitcoin wallet constructs a transaction that assigns 0.10 Bitcoin to the address provided by Mark, sourcing the funds from Harry’s wallet and signing the transaction with Harry’s private keys. This tells the Bitcoin network that Harry has authorized a transfer of value from one of his addresses to Mark’s new address. As the transaction is transferred via the peer-to-peer protocol, it quickly broadcasts across the Bitcoin network. In a split second, most of the nodes in the network receive the transaction and see Mark’s address for the first time.

If Mark has a system with him, he will also be able to see the transaction online. The Bitcoin ledger is a constantly growing file that records every Bitcoin transaction that has ever occurred; it is public. That means that all Mark must do is look up his own address in the blockchain network and see if any funds have been sent to it. He can do this quite easily at the blockchain.info website by entering his address in the search box. The website will show him a page listing all the transactions to and from that address. If Mark is watching that page, it will update to show a new transaction transferring 0.10 Bitcoin to his balance soon after Harry hits Send. Figure 7.9 illustrates this transaction.
../images/465002_1_En_7_Chapter/465002_1_En_7_Fig9_HTML.png
Fig. 7.9

Bitcoin transaction—real-world scenario

At first, Mark’s address will show the transaction from Harry as “Unconfirmed” since the transaction has been propagated to the network but has not yet been added in the Bitcoin transaction ledger (blockchain). To be included in the blockchain, the transaction must be picked up by a miner and included in a block of transactions. Once a new block is created—in the current situation it takes about ten minutes (Proof of Work and validation process)—the transactions within the block will be accepted as “confirmed” by the network and available to be spent. Now the transaction is seen by everyone immediately after it is included in a newly mined block. Mark is now the proud owner of 0.10 Bitcoin that he can spend. Mark can buy something using the Bitcoins he has now.

Types of Wallets

There are five different types of wallets available for digital currency that provide different ways to store and access your coins securely. Wallets can be classified into three distinct categories—software, hardware, and paper.
  1. 1.

    Desktop wallets are designed for PCs and Mac, which has to be downloaded and installed on the computer. Once installed, they are only accessible from the same computer in which they are installed. Desktop wallets are more secure; however, if your computer is hacked or infected by a virus then there is the possibility of you losing all your funds.

     
  2. 2.

    Online wallets are cloud-based software accessible from any computer from any location. They are very easy to access, but online wallets store your private keys in a centralized online storage and are managed by a third party, which makes them vulnerable to hacking attacks and theft.

     
  3. 3.

    Mobile wallets are software applications available on your favorite mobile app store for download and installation on your smartphones. These are the most convenient of all the wallets that can be used from anywhere including retail stores. Mobile wallets are simpler than desktop wallets due to resource limitations on the mobile, but they provide most of the essential features required for most of the transactions.

     
  4. 4.

    Hardware wallets store a user’s private key in a hardware device such as USB. Hardware wallets provide options to make online transactions in a highly secured fashion by storing the user’s private keys in an offline storage. Hardware wallets are compatible to be used over different protocols and support multiple crypto currencies. Users connect the wallet to their internet-enabled computer via the USB, enter their PIN or password to login to the wallet, and start making transactions. Hardware wallets come with a higher price point, but they are the best wallet for serious crypto currency holdings where spending a small sum toward securing the private keys is extremely critical to safeguard the value of the assets you own on the blockchain.

     
  5. 5.

    Paper wallets are a completely disconnected non-electronic way of storing the private keys. They are basically a physical copy of your public and private keys on paper. It is an old school traditional approach of maintaining your secret yourself. Transferring Bitcoin or any other type of crypto currency to your paper wallet is relatively straightforward and involves transfer of coins from an electronic wallet to the public address displayed on the printout of your paper wallet. To withdraw or send coins to another wallet, you will need to transfer funds from the paper wallet to a software wallet. This process is referred to as sweeping which can be done either manually entering your private keys or scanning a QR code that normally prints on the paper wallet.

     

Wallet Security

As we discussed in the earlier section, wallets are secure to varying degrees and subject to the type of wallet and the software provider. Online wallets are inherently riskier due to the centralized storage of the private keys, thereby exposing users to the vulnerabilities in the electronic platform which in rare cases can be exploited by hackers to steal your assets on a blockchain. On the other hand, hardware wallets are highly secure as they store user’s private keys offline but come with a price tag and difficulty in accessing your keys for making the transactions. Although online wallets are prone to cyberattacks, it is highly advisable to take diligent security precautions when using any type of wallet.

Always remember, losing your private keys is equivalent to losing your money. Whichever wallet you use, you must take extreme precautions and be very careful!

If your wallet gets hacked or you send money to an incorrect address or to a scammer, there is absolutely no way to recover lost currency or reverse the transaction. Let’s now look at some precautions that will prevent you from great loss.
  1. 1.

    Backup the Keys—Backing up your wallet keys can save you a lot of turmoil and trouble. Anything can happen at any time; your phone might get lost or your computer might crash. Backing up your crypto currency wallets can come to your rescue and make a critical crypto-catastrophe into a minor problem that can be fixed without any loss of your crypto currency.

     
  2. 2.

    Cold Storage—Cold storage is achieved when crypto currency private keys are created and stored in a secure offline environment. Cold storage is important for anyone with high-value crypto holdings. Online computers are vulnerable to hackers and should not be used to store a significant amount of Bitcoins.

     
  3. 3.

    Hardware Wallet—These are the physical devices created to keep your crypto currency safe. When you request a payment, the hardware wallet’s API creates and signs the transaction and provides a public key which is directed to the network by the API. This ensures that the signing keys never leave the hardware wallet. Hardware wallets come with support for advanced features such as multi-signature transactions.

     
  4. 4.

    Multi-Signature—Wallets are advanced security configuration available with most of the crypto currency platforms and supported by most of the popular wallets. It involves multiple stakeholders to be involved in a transaction. When you configure your Bitcoin address to be multi-signature then it requires another user(s) to sign the transaction along with you before the transaction can be broadcast to the blockchain network. The first multi-signature wallet was launched in the market by BitGo in 2013.