We've been talking about all these applications that's built on the blockchain, but what exactly is 1
the blockchain and how does the technology work? 2
Now, the easiest way to think about this is the blockchain is just a ledger. 3
Now, what's a ledger? 4
Well, let's say that I went to lunch with my friend Jack and he paid for the bill. 5
Well, I could pull out a notepad and say, well, Jack gave Angela $10 for lunch, and now Angela owes 6
Jack $10. 7
So this is the beginning of my ledger. 8
Now, then I decide to buy Helena a coffee. 9
So Angela gives Helena $5. 10
And as I continue this ledger, it's basically just a record of who owes who, what amount, and just 11
a bunch of data. 12
Now, later on, when I want to figure out, well, how much do I owe Jack, then I can look at this 13
ledger and I could see, Oh, I owe him $10. 14
But there are two problems. 15
First, Jack, Helena and I all need to agree on which transactions should go into the ledger. 16
And second, we have to trust the person who's holding onto the ledger that they aren't just going to 17
modify the transactions that we already recorded. 18
What if we could just simply modify this ledger? 19
I could just go in there and add a zero, and now all of a sudden I'm receiving $100 instead of $10. 20
This kind of data tampering makes these kind of paper ledgers worthless to use. 21
And if we had this on the Internet, it would have the same problems. 22
What we need is to make the history of the transactions in this ledger immutable, basically lock it 23
so it can no longer be changed ever again. 24
How do we do that? 25
Well, we could do that with encryption. 26
And the simplest version is the Caesar cipher. 27
So when Julius Caesar wanted to communicate with his generals, and wanted to make sure that the messages 28
didn't get intercepted by a third party, 29
he turned his message into a cipher. 30
That means if somebody got hold of it, they wouldn't be able to work it out unless they knew how it 31
was encrypted. 32
And this was a very simple form of encryption. 33
All it did was it shifted the letters in the message by a pre-specified number of characters, 34
so maybe A becomes E and B becomes F, etc.. 35
Now, if you wanted to try out the Caesar cipher for yourself, you can take a look at this Caesar cipher 36
demo, which I'll link to in the course resources. 37
Now here you can enter some plaintext and if you click on Encrypt, you can see that there's a shift 38
amount. 39
So what it's going to do is it's going to take each and every letter and shift it up by four. 40
So an E becomes an I, a L becomes a P, etc.. 41
But you can see from looking at the ciphertext that there is a pattern here, right? 42
Because with the double L's, it's two P's and then with the both of these Us in Julius, they both 43
get encoded to Y. 44
And if you knew which cipher was being used, you could just go through all of the shifts, you know, 45
from 1 to 26, and you'll eventually figure out how to decrypt this message. 46
So if you click decrypt, you can see it goes back to Hello, Julius. 47
Now, the only thing that you need to know is what that shift number is in order to figure out how to 48
decrypt a message. 49
So here they've got an example message and if you try decrypt, you can see it's still not working. 50
And you change the shift number from 1 to 2 to 3 until eventually you'll get to the right shift number 51
and you'll figure out what the message is. 52
Try it out yourself. 53
The Caesar cipher is one means of encryption, but there are many, many different types of encryption. 54
But what they all do is they take a message and you encrypt it and you can turn it into a cipher, which 55
will be difficult to read for any normal human being 56
and you disguise the meaning of the message. 57
Now, the key thing about encryption, though, is that you can actually go forwards and backwards, 58
which means that you can take the same encryption 59
as long as you know the components of the encryption, you can decipher it and get the message back 60
out. 61
So this means on the other side, the Roman general will be able to read what Julius Caesar meant to 62
send him by knowing which form of encryption was used. 63
And this makes encryption a little bit less secure, because if there is a method to be able to back 64
out the original data by decrypting the cipher, then your cipher could be broken, such as in the case 65
of the famous encryption of the German enigma in World War II. 66
So if our ledger was secured using encryption, then there's always a way to 67
decrypt it, which means we can still alter the ledger illicitly. 68
So instead of using encryption, the blockchain actually uses something called hashing, which is very 69
similar to encryption 70
with a couple of notable differences. 71
Now, what's hashing? 72
Well, it's basically a mathematical formula that allows you to turn a message into a hash code by passing 73
the message through the hash function. 74
But unlike encryption, a hash is a bunch of mumbo jumbo, 75
but if you use the same hash function, no matter what message you put in, you will always get an output 76
with the same number of characters. 77
So if you hashed 123, you might get this top result. 78
And if you hashed abc, you might get this bottom result. 79
But they both have the same number of characters. 80
So let's try out a hash algorithm ourselves. 81
Now, again, in the course resources, you'll see a link to this demo. 82
And here we've got the SHA-256 hash algorithm, which is one of the most popular hash algorithms out 83
there. 84
And you can see here that you can type any sort of message into this box, and it will formulate a hash, 85
and it changes every single time you type anything. 86
So you can type of space, 87
it'll change it. 88
You can type numbers, it'll change it. 89
You can put the entire War and Peace into this message box, and it will still generate a hash with 90
64 characters. 91
And if you're interested, you can actually see below here for the source code of how the SHA-256 is 92
implemented. 93
But how is it possible that we can turn an input into a hash and be completely unable to work out what 94
the original input is from the hash? 95
Well, a really simplified example of a hash function is working out the factors of a number. 96
For example, 2 times 4 is 8, so 2 and 4 are 97
the factors of 8, because I can divide 8 by 4 and it will cleanly divide and I can divide 98
8 by 2, and it will also cleanly divide with no remainder. 99
So going forwards is very easy. 100
2 times 4 is 8, super easy. 101
Now, if I asked you to work out what are the factors of 8, which would be 1, 2, 4 and 8, that's 102
a little bit more difficult. 103
But what if I asked to work out the factors of 57? 104
Unless you already know what they are, you have to go through all the numbers. 105
So it will be 57 divided by 2, 106
57 divided by 3. 107
Now, here you would find that 57's factors are 3 and 19 because it can be cleanly divided 108
into 3 and into 19. 109
But what if I asked you, are there any other ones? 110
Well, you can't be sure unless you actually continued. 111
So you could say 57 divided by 4. 112
That doesn't cleanly divide because there's a remainder. 113
Then you do 57 divided by 5 and so on and so forth. 114
So essentially when we're multiplying two numbers together, it's almost like creating the hash from 115
the message. 116
And then the final result that we get is the hash. 117
But if you want to go from the hash to go back to the original message, you would have to brute force 118
it. 119
You would have to divide it by every single number from 1 to 57 to find out all of its factors, which 120
for humans takes a lot of time and for computers, takes a lot of energy and computing power, especially 121
when you're dealing with very, very large numbers. 122
You really saw how finding the factors of 57 was a lot more difficult than finding the factors of 8. 123
Now, real world cryptography uses numbers that are hundreds of digits long. 124
For example, in 2019, it took researchers 900 years of computing power to find the factors of a number 125
with 240 digits. 126
So this is kind of the underlying basis of cryptography; going forwards is very quick and easy, very 127
easy to calculate. 128
But going backwards is almost impossible using the current computation power that most people have access 129
to. 130
So how does this apply to blockchain anyways? 131
Remember that our problem with the ledger was that we wanted to make sure that no one can change our history 132
of transactions. 133
Let me show you how the Bitcoin ledger solves this problem. 134
Using what we know about one way functions, we can now take all transaction data and create a hash. 135
Some very clever people have figured out that you can encode a whole bunch of things and hash everything 136
together. 137
So for example, I could write a message and I could write which date it was written on. 138
So the 1st of January of 2023 139
and I could also say I am the person who wrote that my name is Angela. 140
I could put all of these information together and it will create a hash for me. 141
So this is essentially how a block in the blockchain is formed. 142
We have a piece of data, so let's say Angela owes Jack $10. 143
This is our piece of information. 144
This is our piece of data that we want to encode into a block. 145
Now, in order to add that into the block, we're going to have to generate a hash and the hash becomes 146
the identifier for this block. 147
It's kind of like the fingerprint of this block. 148
How can you know that this block is truly this block? 149
Well, you could look at my fingerprint, and it's probably different from yours because we are unique 150
people. 151
And you can look at this block and it'll probably be different from another block because it'll have 152
a different hash. 153
Now, in order for a block to be added to the blockchain, it has to satisfy some predetermined requirement. 154
And this requirement could be, for example, that the hash has to start out with three zeros. 155
Now you can see that as I start typing, the hash keeps on changing 156
and sometimes you might see that we get just purely by chance, a zero to show up. 157
Or in this case, even better two 158
zeroes to show up. 159
But if we wanted a hash that has to begin with three zeros, then we're going to have to keep modifying 160
our data, which is not what we want. 161
We want our data to be clean, or we can modify this thing, which is known as the nonce. 162
So the nonce is basically a number. 163
So we can start from 1 and you can see the hash change because the nonce is being combined with the 164
data to produce the hash. 165
Now 1 produces a hash that starts with 4, 166
so that doesn't work. 167
Well, 2, we can try 3, we can try 4, we can try 5. 168
And we can keep going through these numbers until we find a nonce that creates a hash that starts with 169
a zero. 170
But that's not enough for our block to be added to the chain. 171
We need it to be three zeros to begin with. 172
So you can continue changing this nonce to 7 to 8 to 9 until quite a while. 173
Trust me, I've tried it to find a hash that begins with three zeros. 174
Or you can simply use the computer, undo that for you to brute force the nonce until it finds from 175
1 all the way up, increasing by 1 each time, until it finds the right number 176
that's going to create a hash with three zeros. 177
And you can do that by simply clicking on the mine button in this website. 178
It took 0.1 seconds for my browser to figure it out, and it figured out that 3885 is the nonce that 179
I need in order to encode this data, to have a hash with three zeros to start with. 180
So this makes this block valid. 181
And you can see it turned green because it satisfies the requirements of the blockchain to have three 182
zeros to begin with. 183
Now, if I had a blockchain that had a little bit higher of a difficulty, well, I can change that 184
to four zero to start with or five zeros to start with. 185
So then it's going to have to iterate through all the nonces until it finds a number when combined 186
with this particular piece of data will produce a hash with five zeros to start with. 187
And you can see when I click mine, it's going to take a little bit longer, 0.6 seconds before it finds 188
the correct nonce, which happens to be 53,792. 189
But the important thing is that nonce only works to produce this hash with five zeros for this piece 190
of data. 191
As soon as I alter this piece of data, let's say that Jack decided that I actually owed him a bit more 192
money and he goes in and sneakily alters this data, 193
well, it's now become invalid because you can see the hash no longer starts with five zeros. 194
Well, this tells me that this block has been modified and it is not valid data. 195
So then what we're doing with our ledger is we're taking the data that we want to add into the ledger, 196
and we're putting it into each of these blocks by hashing the data with a nonce. 197
So this is where the block in the blockchain comes from. 198
But what about the chain part? 199
So if we go to the next tab of this demo, you can see we've got a whole bunch of blocks here, five 200
in total and each of the blocks, 201
in order to create the hash, it takes into account the hash of the previous block. 202
This is how they're chained together. 203
So here you'll see that the hash of the first block, which is called the Genesis block, because it 204
doesn't really have any previous blocks attached to it. 205
So that's just 64 zeros in this case. 206
Well, this has a hash, though. 207
And let's say that we decided to put in the data that 208
Angela owes Jack $10. 209
Now, in order for this block to be valid, it needs to have a nonce that combines the data with the 210
previous hash value, which in this case is zero, 211
to create a hash that starts with five zeros so we can click mine and we can get our computer to work 212
it out. There 213
it's found the nonce: 53,792. 214
And with this nonce and our data and the previous hash, all of these things combined creates a hash 215
with five zeros to begin with. 216
So this is now a valid block that's entered the chain. 217
Now, on the next block, let's say that I decide to-- 218
I decide to give Jenny $5, and I want to record this into my blockchain ledger. 219
There's my data, 220
and now you can see this is going to be chained to the previous block because it's now got the previous 221
block's hash in this section. 222
And this plus this plus this is going to be combined to create this hash. When any of these things changes, 223
so, for example if I change the data, if I change the nonce, and I can't but if I did change the 224
previous hash, it will also change the hash here. 225
So these three things together create the hash. 226
Now, if I go ahead and mine it, you can see it'll figure out a nonce in order to satisfy my requirement, 227
which is five zeros to begin with. 228
Now, here's the interesting part. 229
If I decide to go back in time and alter something in the ledger. 230
So let's say that instead of me owing Jack $10, I decide to sneakily change it to Jack actually owes 231
me $10, 232
you can see what it's done is it's invalidated 233
not only this block, it's turned it red because it doesn't start with a zero, 234
it's also invalidated every subsequent block. 235
So because this block depends on this one and the previous hash function has changed, which means this 236
has changed 237
and because we said that these three things together create the final hash, it no longer starts with 238
five zeros. 239
So this means I can't change this data without people knowing about it. 240
And if I change the data back to the original form, you can see everything turns green because that 241
is the original state of the ledger. 242
And this is how the ledger stays immutable. 243
We can't change anything that goes into the blockchain, otherwise it's going to invalidate all of our 244
hashes and it's no longer going to satisfy the requirements. 245
So as you can imagine, if you had more data and you mine each piece of data 246
until you have this entire chain which is all linked to each other 247
because in order to create each block's hash, it takes into account the previous block's hash and 248
whenever the previous block's data changes, 249
it's going to change the hash and thus invalidate every subsequent block. 250
Then this is how we can have security in our blockchain. 251
We now have a ledger where every single time you change anything in a previous block, it's going to 252
invalidate every subsequent block. 253
And in order to make it valid, we have to re-mine every single one of these blocks which, depending 254
on the difficulty level, I haven't even set it that high, is going to take a lot of time because we're 255
going to have to go through each of them. 256
And in reality, in chains like Bitcoin or Ethereum, it would literally be impossible with the current 257
computation power to be able to go through each of the blocks and re-mine them. 258
And you can see, even in this case, which is very a simple one, my Chrome is already telling me that 259
it's hanging and it's not working and I should shut it down. 260
So this is how we ensure the integrity of the blockchain. 261
But blockchain actually goes even one step further in order to assure that the data that's stored is 262
absolutely correct and has not been tampered with. 263
And this is the distributed part of the blockchain. 264
This goes back to the first problem that we talked about. 265
How does Jack, Helena and I all agree on what's in the ledger? 266
So I'm going to make this a little bit easier on my computer and drop the requirement to only need four 267
zeros. 268
So you can see here, I've got a distributed blockchain. 269
So I've got three people with three computers Peer A, Peer B and Peer C. 270
And what happens is every time a new miner joins the blockchain, they're going to get a full copy of 271
the blockchain. 272
So this person's blockchain has the same data as this person's blockchain. 273
And you can see because if you look at the hash, they're all completely identical. 274
So now what this means is that three people have the same blockchain that they hold. 275
Now, if this first person, Peer A, decides, oh, I'm going to be a little bit cheeky and I'm going 276
to change the data in here, I'm going to say that, you know, in this block, actually, something 277
else happened. 278
Then you can see that they've invalidated all of their subsequent blocks. 279
Now, they could re mine each of these blocks in order to make it valid again. 280
Now because I've set the difficulty quite low. 281
That was relatively quick to do. 282
But now they no longer match up with the other blocks. 283
You can see this one is starts with four zeros, but then it's 347e. 284
But for the other two, they've got a88f, a88f. 285
And this is how the network can tell that this Peer A has become a bad player. 286
They're trying to mess with the system because their hashes don't match with anybody else's. 287
So by distributing the blockchain on multiple computers, we now have another way of ensuring the integrity 288
of our data to make sure that our data is absolutely correct and nobody has tampered with it. 289
And this is where the power of blockchain really comes in, because we now have a distributed trustless 290
ledger. 291
We don't have to trust anybody. 292
We don't have to believe that, you know, PayPal is storing my money and they are making sure that 293
all the transactions are correct. 294
Well, actually, we've got blockchain, which encodes all of these transactions. 295
And because of the underlying technology, the distributed nature of it, the blockchain part, it means 296
that we don't have to care who is storing these copies of the blockchain. 297
We can trust in the technology that our ledger is always, always going to be accurate and nobody is 298
going to be able to tamper with it. 299
Now, this is great 300
and this technology up to this point that we've described already allows us to have cryptocurrencies 301
like Bitcoin. 302
But the real cherry on top happened when Ethereum was conceptualized. 303
Now, the real breakthrough here is they realized that not only can we store data inside each of these 304
blocks, who owes who money or who has how much money, 305
we can create what's called smart contracts by storing code into the blocks. 306
So back in our blockchain demo, we could have code, for example, if we were running a Kickstarter 307
or a crowdfunding type of web3 decentralized application, then we could have the code that says, Well, 308
if the total amount was greater than, I don't know, whatever goal they set, so maybe $5,000, 309
well, then in that case, this code is going to execute and we're going to transfer the money. 310
So let's say Angela has an account and is going to get these $5,000. 311
We're going to increase Angela's account by 5000. 312
So this code then gets put into the block, which is going to create a hash, which means that it can 313
never be changed again. 314
And then a user can come along and execute this code 315
and the resulting transaction where Angela gets $5,000 will get saved onto the Ethereum blockchain. 316
So now instead of having a crowdfunding company like Kickstarter, who's going to escrow the funds or 317
who's going to make sure that you know the money once the goal is hit, gets transferred to the project 318
owner, 319
well, we've actually got all of that encapsulated in a few lines of code. 320
So when somebody decides to raise some money and they happen to reach their goal, then our code from 321
the block gets activated automatically and transfers the money to the recipient. 322
And this is where the real mindblowing part of blockchain comes in. 323
And that is exactly what we're going to learn in this module, how to create these decentralized applications 324
and getting our apps to do things automatically based on the code that's stored on the blockchain.