When I took up IT as a career, the most appealing thing to me was that I would never get bored in this field. I mean, which other field has fun names, like “bugs” for defects? And every couple of years, everything changes. After many years in this field, this still holds true. I keep seeing groundbreaking changes that promise to change our society. Think about the change the Internet has had on our lives. Think about the change storage capacity has had on our lives. Now that so much can be recorded and searched, computers know more about us than we know about us. Scary. Think about the amount of change that phone in your pocket has brought forth. Think about how different the COVID crisis would have been if we couldn't work from home, or didn't have information at our fingertips? Or how wars are fought and broadcast on social media.

One such technology that has flown under the radar for many years, and has become very important recently, is blockchain. I thought we should chat about it.

What Is Blockchain

Believe it or not, this concept, although all the rage today, was first discussed all the way back in 1982. In a dissertation entitled “Computer Systems Established, Maintained, and Trusted by Mutually Suspicious Groups”, David Chaum first proposed the idea of a blockchain-like protocol.

The easiest way to think of blockchain is as a distributed database that's shared among the nodes of a computer network. Like any database, it stores information in a digital format. The only difference is that this database doesn't reside on one computer, or even a computer in Azure or AWS; this database is distributed among all the nodes of the computer network that participate in that blockchain. These computers can be your laptop, a server somewhere - really anything.

You've probably heard of Bitcoin. Bitcoin is an example of a decentralized digital currency. And the Bitcoin system is a collection of nodes that run Bitcoin code and that stores data on its blockchain. If you and I exchange some Bitcoin, this transaction gets written to this distributed database. And because it's decentralized, there's no single party controlling it. This means, for all practical purposes, that given its size, it cannot be censored, tampered with, or taken over by a single party. This is quite in contrast with, say, a database sitting in the cloud. Some hacker could tamper with that database or a central authority could censor it. Or some disgruntled employee or party could take control of it and shut it down or tamper with it. Bitcoin chooses to use blockchain to safeguard its decentralized currency, but there are many other uses of blockchain, and many uses that we're still figuring out.

What would blockchain mean for proving your identity? This's exactly the spirit behind decentralized identities in the Microsoft Identity Platform. Imagine the ramifications where you, the consumer, hold your identity and choose to share it with whomever you desire to.

What would it mean for social media? Information could be shared in a P2P form, and, for better or worse, would be effectively uncensorable.

This sure makes a lot of governments and people in positions of power quite nervous. Like I said, just as the Internet has had a profound impact on our society, so will blockchain.

Why the name “blockchain?” A typical database has data in rows and columns. In-memory databases can store objects, similar to JSON. But blockchain collects information together in groups called blocks. These blocks have a defined storage capacity, and when filled, are closed, given a timestamp, and are linked to a previously filled block. This creates a chain of blocks, and therefore we call it a blockchain. This is illustrated in Figure 1.

Figure 1: How blockchain stores data
Figure 1: How blockchain stores data

This also contributes to the irreversible nature of blockchain data. These chains are replicated across various nodes, and changing the history of all these nodes is practically impossible.

Think of blockchain as an immutable ledger that cannot be edited, deleted, or destroyed. An alternate name for blockchain is distributed ledger technology or DLT.

An alternate name for blockchain is distributed ledger technology or DLT.

How Does Blockchain Work?

Blockchain is designed for digital information to be recorded and distributed, but not edited. When a new transaction is entered on the blockchain, the transaction is transmitted to a peer-to-peer network of computers. This network of computers then solves equations to confirm validity of the transaction. When the transaction is confirmed as a valid transaction, it's entered into blocks. Each block contains data, hash of the block, and hash of the previous block it's linked to. As blocks fill, they're chained together, creating a long history of transactions that are permanent.

The details of the data depend on the kind of blockchain. For instance, a payment network, such as Bitcoin, could store the sender, receiver, and the amount of the transaction.

The hash acts as a fingerprint for your block. It verifies the uniqueness and validity of the block. Changing the block causes the hash to change, which means that changing the block data effectively removes it from the chain, making spurious data easy to identify.

Similarly, the hash of the previous block creates a chain of blocks. This means that, like a journaling database, or a ledger system, you can't simply edit one link in the chain and expect the chain to not detect the problem.

The first block in a blockchain doesn't point to any previous block and it's called the genesis block.

Now, you may be thinking that new computers, such as that fancy M1 Ultra Mac, are pretty fast. And Bitcoin is about 324GB of data. So why can't I just completely recalculate the entire blockchain's hashes and make my invalid blockchain valid? You could. But there's an additional layer of protection called “proof of work.” This effectively slows down the creation of a new block. For instance, in Bitcoin, every block takes 10 minutes to calculate proof of work for each block and add it to your blockchain. This means that editing the entire bitcoin blockchain is impractical. To put numbers to it, Bitcoin block size is around 1MB and Bitcoin blockchain is 324GB, which translates to more than six years to tamper with the entire Bitcoin blockchain in its current size.

Yet another mechanism to keep things secure is the fact that a blockchain is distributed. This means that anyone who joins the blockchain gets a full copy of the entire blockchain. So, if someone creates a new block, that block is sent to everyone on the network, and everyone verifies it as a legit block belonging to the existing blockchain. This is then added to the existing blockchain on everyone's network, thereby creating a consensus among everyone.

To tamper with a blockchain, you'd have to recalculate the entire blockchain, get over the proof of work hurdle, and tamper with this blockchain on at least 50% of the P2P network. As you can imagine, this is nearly impossible to do on a mature network such as Bitcoin. Again, to give it some numbers, the Bitcoin network has close to 15000 nodes, but you can see an up-to-date number at https://bitnodes.io/.

Applications for Blockchain

At its heart, blockchain is a ledger-distributed database, designed to record, distribute, but not tamper. This means that any application needing wide dissemination of data that can be trusted is a great application for it. Here are some examples of blockchain being used today.

Cryptocurrencies

Imagine that you're in a group of four friends and you frequently exchange money. Maybe you go out for dinner and one of you foots the bill, for instance. The other three owe that person some money. How does this work?

Well, you maintain a record, or a ledger of who owes who how much. Then, there is mutual trust. You have trust, and records, and this is how you can continue to loan each other money.

You already know that blockchain can give you an untamperable ledger database. But really, anyone can write anything to it. What prevents me from writing, randomly, that Alice owes you money? This is where trust comes in. For that, you add cryptography to it and you get trust. This is the foundation of crypto currencies. Anyone can write to the ledger around who owes who how much, but it isn't until the data is signed with a private key that the transaction is trustable. And the public key allows anyone to verify that the transaction is legit. The signature, in fact, requires your private key, the entry ID, and the contents of the message, so the signature is distinct for each transaction.

There are many cryptocurrencies, such as Bitcoin, Ethereum, Ripple, Litecoin, and more. In fact, there are more than 1600 crypto currencies, each with their unique characteristics. They're all implementations of blockchain.

On top of that, this virtual currency is tied to a real currency to start with. So, let's say, I buy $100 worth of SahilCoin. I know this sounds funny, but I could set up a new coin by that name and we could start using it between friends (wink wink). Now, let's say that I can't transact > $100 worth of SahilCoin. This means that I can't keep generating signed transactions on the ledger once I exceed my allotted value of $100. In order to transact money on this SahilCoin network, I can continue to settle transactions in SahilCoin. I don't need banks or wire transfer fees or intermediaries. Even so, the money has real value. In fact, currencies such as Bitcoin can be converted back and forth to real cash, with the convenience of an ATM. Yes, there are physical ATMs where you can buy and sell Bitcoin for cash. Of course, you can do so virtually as well.

This is where things get interesting and what differentiates one coin from another. When an entry is made to the blockchain, theoretically speaking, everyone should be able to verify and agree to that entry. But in reality, there are transmission delays on a P2P network. Different crypto currencies use different protocols to balance between speed and reliability. Additionally, crypto currencies, such as Ethereum, allow the deployment of smart contracts and decentralized applications, which are bits of code that can execute and release cryptocurrency when certain conditions are met. Both Bitcoin and Ethereum currently use proof of work as consensus protocol, but Ethereum will soon move to proof of stake, which will allow it to be much more scalable, secure, and much more energy efficient.

In fact, this is one of the major criticisms of Bitcoin. When any transaction is done on Bitcoin, and it spreads through the P2P network and is verified and recorded, all those computers are consuming lots and lots of energy. It's estimated that Bitcoin today consumes 131TWh of energy annually, enough to power Ukraine and Egypt. Much of this energy is created from non-renewable sources, which produces a terrible amount of greenhouse gases. On top of that, the massive computation requirements mean that you need to have powerful hardware, which gets outdated very quickly, so it produces tons of e-waste. In fact, Tesla started allowing customers to buy cars using Bitcoin, and they reversed their stance due to environmental concerns.

Even though the name says “coin” there is no need for a physical coin involved here. It's not backed or issued by central banks or governments, it's backed by blockchain, and an equivalent cash transaction value that fluctuates based on demand. The currency amount on Bitcoin tokens is kept using public and private keys. The public key is sort of your bank account number, and can serve as an address that you use to send or receive Bitcoin. The private key is something you must never share. It's what you use to authorize Bitcoin transmissions. A Bitcoin wallet, on the other hand, is a secure digital store of information that keeps track of the ownership of your coins.

Smart Contracts

Smart contracts are tiny computer programs that live on a blockchain and can disburse a payment once the conditions of the contract are met. For instance, if there's a crowdfunding effort involved, money can be collected from many people and be disbursed when minimums are met. Or you can have a collective auction. Or perhaps an inheritance. Think of it as a digital vending machine.

Smart contracts on the Ethereum blockchain require you to write the contract in “smart contract language,” which has its own very simple syntax, and you need to have enough ETH to deploy your contract. Then you have to pay “Gas,” which is a unit of computational effort to execute operations on the Ethereum network, to deploy your smart contract. You pay these gas fees also in ETH.

So as long as you can back your smart contract with ETH and gas money, and write the logic in a simple language and stick it on the Ethereum blockchain, you're in business.

Just read that sentence again. Now you see why I picked this field for work?

NFTs

For such a long time, we've been used to physical objects that are hard to replicate. There is the Mona Lisa, for instance. I'm sure copies exist, but there's only one original and there's a way to protect it and detect counterfeits. This is so much harder in the digital world.

Being digital creates so many issues. It serves as a disincentive to produce original work, because people just copy it. Just think of all the memes floating around. There's no payment mechanism to the original creator as their creation goes viral. And most of all, the recipient has a hard time trusting what they see and if they can trust it to be original. For instance, a doctored video of the president saying silly stuff on social media could have serious ramifications. Wouldn't it be nice if the White House could somehow stamp something as original and untampered with, and, just like you verify a site using SSL, you could verify the originality of content? This is exactly the problem NFTs solve.

NFTs stand for non-fungible tokens. In our digital world, people copy everything. All it takes is a bunch of keystrokes, and your digital artwork is now mine with a screenshot. An NFT is simply any binary data put on the blockchain. Given the characteristics of a blockchain - the fact that it's immutable and independently verifiable - now, you can prove the originality of any work. This has pretty significant ramifications for ownership rights.

NFTs don't have to be just images. They can be anything. They can be, for instance, an audio file, a video, or any other kind of digital artwork. They can be domain names, concert tickets, objects in the metaverse, really, anything that is digital or can have a digital representation. Do you remember a game called FarmVille, where you could buy/sell stuff? McDonalds just bought some real estate in the metaverse. Strange times we live in.

McDonald's just bought real estate in FarmVille.

You can even buy real-estate - yes, a house you can live in - using NFTs. Remember, you're proving ownership. What's your house's current ownership? Who has the deed? Well, how is that ownership proof not digital? Of course, there are still issues to be worked out, such as governments recognizing Bitcoin as official currency or NFTs as a valid equivalent of a deed. But you can link Bitcoin to dollars and NFTs to deeds.

Really, anything can be sold as an NFT. Jack Dorsey, the founder of Twitter, sold his first tweet as an NFT for 2.9 million USD (https://v.cent.co/tweet/20). You can see this in Figure 2. Honestly, I'd be willing to sell mine for a lot less.

Figure       2      : The $2.9 million tweet
Figure 2 : The $2.9 million tweet

This NFT's ownership is now sinaEstavi's and sinaEstavi can, in the future, choose to sell it to someone else. The transaction to sell the NFT itself can be stored on the blockchain, which proves ownership transfer, again, in a verifiable, non-tamperable manner. Of course, if sinaEstavi can't find any buyers, this NFT becomes worthless, much like objects in the real world. After all, what's so special about the Mona Lisa?

You may be thinking that typical blocks in blockchain are small. For instance, bitcoin block size is just 1MB. In a world where your phone takes 108MP images, how do you efficiently store them on blockchain? The answer is that the entry on the blockchain contains a unique fingerprint (hash), token name, and symbol, a pointer to the previous block, and a link to the file on IPFS. IPFS stands for interplanetary file system. And it looks like ipfs://somelongweirdstring. Usually when you try to get a file, it looks like https://location. That's called location-based addressing.

Here in IPFS, you're using ipfs://hash and providing a hash of the content you're interested in. This is content based addressing. This link points you to both the file and the metadata of the file at ipfs://somelongweirdstring/metadata.json. There are python and node packages that let you decipher this metadata easily, although you can also just visit https://ipfs.io. When you request an IPFS file, anyone on the blockchain network who has a copy of that file will return the file to you, and given that it's built on blockchain, you can use the hash to ensure that the file hasn't been tampered with.

Also, the metadata can point to a location that is a simple HTTPS URL or any other mechanism that stores data off the blockchain. This is called off-chain storage, which allows you to have a hybrid of blockchain and old-style technologies and gain the best of both worlds.

One interesting thing about IPFS is that the actual file doesn't need to be stored by everyone. In fact, the creators of IPFS have created a blockchain called Filecoin, and if you have extra space on your hard disk, and some Internet bandwidth to share, you can become an IPFS node and start serving files to the world, and yes, earn Filecoin in return. Filecoin can then be traded for other crypto currencies, or to fiat currencies, which you can then use at your local fast-food restaurant. Isn't this amazing? You setup an IPFS node and start making passive income. You can track the price of Filecoin here https://www.coindesk.com/price/filecoin/.

This is perhaps why it's also called the “interplanetary” file system. Imagine if humans moved to Mars and wanted to load https://somesite.com. A roundtrip to earth would take forever. But the Martian humans could effectively replicate the file on the IPFS locally. Finally, we can surf for memes on Mars.

NFTs only represent ownership. Think of it as digital bragging rights. But anyone can download the NFT and even copy it. Except you can always differentiate the original from the copy. NFTs, therefore, also allow concepts such as the buyer owning the original, but the creator owning the copyright or reproduction rights.

In a sense, NFTs are nothing but just a smart contract. The contract stores unique properties of the item and keeps track of current and previous owners. You can program this contract to pay the owner if it changes hands. Or for that matter, pay all previous owners, or just the creator, every time it changes hands, much like a royalty.

Here you are, reading this article. Imagine if this article was an NFT. And every reader who read it sends a royalty of 25 satoshi (the smallest unit of Bitcoin) back to the author. One bitcoin has 100 million satoshis, and 25 satoshi roughly translates to one cent. This transaction could be done via a smart contract, with no middlemen, transaction fees, or even considerable delays. But this kind of microtransaction has historically been nearly impossible to replicate using the conventional banking system. Can I write a blogpost where very reader pays me 0.0001 cent? Not in the traditional way. With NFTs or crypto this is possible. And you do see examples of crypto tip jars already on so many sites. What prevents us from taking the next step and unlocking smart contracts on content? I am sure it will happen, if it isn't already.

I have no misgivings about the capability and risks of this technology. This transaction could cross borders, avoid the banking system altogether, even circumvent governments. It makes a lot of people in power very nervous. Sure, this has positives and negatives, and for sure, like any technology, has potential for misuse or great use.

This technology, I am sure, is being used to transport illicit money, and governments are trying hard to catch up to it, but there are some really good uses for it too. Imagine someone trying to flee their country because their country is at war. Carrying money as crypto is memorizing a phrase. Or for that matter information. For instance, in 2017, the Turkish government blocked access to Wikipedia as anti-government. People just put Turkish Wikipedia on IPFS, and good luck blocking that. Another example is https://d.tube/, which is just like YouTube, but built on blockchain. This means no ads, no censorship, and you can't delete videos.

Decentralized Identities

Identity is an interesting problem. Many years ago, it was a username/password. We then started coming up with better mechanisms for proving that you had the right password. Over time, we realized that it was not only hard to keep this secure, but it was also inflexible. So we created standards like OpenID Connect, where we delegate or federate the process of proving an identity to a well-known identity provider, like Twitter, Facebook, Google, Microsoft, etc.

It's still not ideal. Why should Facebook or Google or Microsoft hold all my data? Remember, an identity is tied to your profile. Let's say you go to a doctor and have to prove who you are. Today, you show a driver's license or similar form of identification. I've always feared what would happen if I lost my identification when travelling and TSA wouldn't let me on the plane. Could I log into my Azure AD account to prove who I am? It's certainly enough for my employer, so why isn't it enough for TSA or my doctor?

Well, it may be enough, but that requires both of you trusting Microsoft to hold your profile, and the government and its regulations allowing Microsoft to hold every citizen's personal information.

Yeah, not gonna happen.

This is where decentralized identities come in. Put simply, they are identity information that you control, backed by an attestation authority. And you control what bit of information you share with whom. For instance, when visiting a doctor, they don't need to know your salary. Or while going through TSA, they don't need to know about that wart that itches weird. This is where you can hold your distributed identity in a wallet, and you share what you deem worth sharing, yet no central authority holds all your profile information. You do.

Other uses

The blockchain technology is nascent, but a number of future uses and implementations will occur. How about voting? What's more important that being able to trust our voting system? How about notaries? How about tracking goods and shippable items that aren't tied to a single vendor's tracking system? Perhaps medical records that you, the patient, control and share as and when necessary in whatever manner necessary with your doctor? Vaccination records, taxation records, so much more. Like many other technologies, the technology runs far ahead and then the regulators catch up. We're entering an interesting time when governments are launching cryptocurrency equivalents of their fiat currencies. What does mean for the traditional banking system?

Ethereum Blockchain in JavaScript

Sometimes it's important to understand the mechanics and reasoning behind any new technology before we roll up our sleeves and see it in action. The good news is that, like almost anything else, a lot of the hard work has been done for you in reusable libraries.

Let's see a quick example of listening to transactions happening on the Ethereum blockchain. I'm going to do this in JavaScript, although equivalent libraries exist for other languages as well.

For JavaScript, there exists a well published JavaScript API called web3.js at https://web3js.readthedocs.io/. This API can be used in both HTML or NodeJS. I'll use it in HTML. Go ahead and set up a simple HTML page, and reference this script. This can be seen in Listing 1.

Listing 1: Our starter page with web3.js

<!doctype html>

<html lang="en">
<head>    
    <meta charset="utf-8">
    <title>Ethereum demo</title>
    <script
        src="https://cdn.jsdelivr.net/npm/
                web3@latest</a>/dist/<a href="http://web3.min.js">web3.min.js">
    </script>
</head>
<body>
</body>
</html>

In order to use web.js, the first thing you need to do is initialize an instance of Web3. Ethereum libraries communicate through RPC calls. You could host one on your computer if you were running a NodeJS process on localhost at some port, or you could use a free, hosted version on cloudflare. Because I'm running this on an HTML page, I'll use cloudflare.

const web3 = new Web3("https://cloudflare-eth.com");

I want to initialize two variables: one that holds the latest BlockNumber I receive from Ethereum, and the other that acts as a time interval where I listen for new blocks mined on the blockchain every five seconds.

let latestBlockNumber = -1;
let timeInterval = 5000;

Now I will create a basic definition of a function that will get called every five seconds, and it will receive a block from Ethereum.

async function checkCurrentBlock() {
    const currentBlockNumber = await web3.eth.getBlockNumber();
    setTimeout(checkCurrentBlock, timeInterval);
}

Let's finish up this logic by writing up some processing logic. I'm simply writing out the block I receive from Ethereum. This can be seen in Listing 2.

Listing 2: Logic to process ethereum blocks

async function checkCurrentBlock() {
    const currentBlockNumber = await web3.eth.getBlockNumber();
    console.log(
        "Current blockchain top: " + currentBlockNumber,
        " latest Block Number: " + latestBlockNumber)
    while (latestBlockNumber == -1 ||
        currentBlockNumber > latestBlockNumber) {
        await processBlock(latestBlockNumber == -1 ?
            currentBlockNumber : latestBlockNumber + 1);
        }
        setTimeout(checkCurrentBlock, timeInterval);
}

async function processBlock(blockNumber) {
    console.log("Processing block: " + blockNumber)
    latestBlockNumber = blockNumber;
}

Go ahead and run this HTML page by double clicking on it. If you've loaded this up in a reasonably modern browser, it should show you a blank page. Open developer tools, and you should see Ethereum blocks scrolling. This can be seen in Figure 3.

Figure 3: The latest blocks on Ethereum
Figure 3: The latest blocks on Ethereum

Summary

We're gradually but surely entering a new phase of computing. Web 3.0 technologies such as blockchain are going to have a profound impact on everything we do. Think how life was before the Internet. We had cold-war USSR, complete with an iron curtain, and a whole country with millions of people living in an alternate reality. Then came the Internet, information was free, and people saw things that were hard to believe but they were real.

I remember, as a kid, when I first saw photographs of the USA. I had a very hard time believing a place like that existed on this planet. The only reason I trusted that information shown to me is because I trusted that a glossy photograph I was holding in my hand could not be faked. At least, using the technology around me at that time, it was not possible to create a real-world replication on a photograph so perfect, on glossy paper, that I could hold in my hand. I trusted that information, even though I didn't believe it. I was five years old, and you must keep in mind that India was quite socialist back then. Information was very controlled, forget the Internet - we had no phones, and everything on TV and radio was extremely censored and controlled. Those memories were etched in my mind even without reinforcement for many years. Sometimes, rarely, I'd catch a glimpse of it on TV. But it wasn't until many years later I got to see all this and touch it, first hand. Honestly, I still have a hard time believing a lot of it. Maybe this is a dream and I'm in a metaverse. Who knows?

Governments are trying hard to nail down information, as are corporations. But information wants to be free. The more you control it, the more people seek it. And it isn't just about information. Can you imagine surviving today without the Internet? As romantic as being “off the grid” sounds, how many of us actually do it, or even could do it?

With incredible connectivity, ample storage, and cheap computing power, this revolution is just starting. I can't wait to see how it will change our world, in some ways better, in some ways worse.

Like any tool - a hammer, a car, or nuclear energy - mastering it and using it willfully will differentiate us from riding the car, or the car riding us.

Where will you be on this drive? I hope in the driver's seat.