Learn the basics of Bitcoin ₿ in 100 seconds, then build your own blockchain from scratch with Node.js and TypeScript https://github.com/fireship-io/node-blockchain

Support the Cause

Send me some BTC: 13pEjpLf3taeezGkLpn2CMi2YNxaBF7rVZ   
Sponsor me on Github for $1 https://github.com/codediodeio

References

How Bitcoin Works https://bitcoin.org/en/how-it-works
Original White Paper https://bitcoin.org/bitcoin.pdf

Source code https://github.com/fireship-io/node-blockchain

#bitcoin #blockchain #js

Install the quiz app 🤓

iOS https://itunes.apple.com/us/app/fireship/id1462592372?mt=8
Android https://play.google.com/store/apps/details?id=io.fireship.quizapp

Upgrade to Fireship PRO at https://fireship.io/pro
Use code lORhwXd2 for 25% off your first payment. 

My VS Code Theme

- Atom One Dark 
- vscode-icons
- Fira Code Font
2024-02-04T14:15:56.667Z
nova-2
2-general-nova
2024-01-09.29447
30089e05-99d1-4376-b32e-c263170674af
16da1610-f5f5-4a9c-9f07-f6c19a76c836
299769675a50ad0364956f44d6e0e76cb38eafff88c78097fc665521fa59e60b
67875a7f-c9c4-48a0-aa55-5bdb8a91c34a
deprecated
Bitcoin, a peer to peer electronic cash system first described in a 2008 white paper authored by the mysterious Satoshi Nakamoto.
The modern financial system relies on our trust of big centralized banks to hold our fiat currencies and execute transactions, but trust is a weakness that eventually spire's intervention by lawyers and government.
Bitcoin allows 2 parties to make reliable transactions based on cryptographic proof, eliminating the need for a trusty middleman.
The optimists call it digital gold, the pessimists call it fool's gold, but in reality, it's just software.
And like all software, its purpose is to arrange change ones and zeros in a meaningful way.
The protocol that makes it meaningful is Blockchain, which allows 2 parties to engage in transactions denominated and Bitcoins or Satoshis, and just like dollars and cents, they have value because we believe they do.
From a financial perspective, the Blockchain scene is like a shared public ledger that contains all transactions from all Bitcoin users and is distributed and synchronized around the world, which eliminates the need for a central authority to maintain and validate it.
From a technical perspective, think of the Blockchain as a database structured as a linked list, where each record or block represents a group of transactions that have been permanently committed to the database.
It works kind of like a Git repo that can never be rebased.
The important thing is that each new block is linked to the previous one in the Blockchain, and its creation goes through a very strict set of cryptographic rules.
Each user or wallet has a unique public key for receiving money, kind of like username and a unique private key for spending money, kind of like a password.
But before you can spend money, you'll need to prove that you're the owner of a public key that money has been sent to in the past.
Each transaction contains a hash or encrypted representation of the previous transaction and the new owner's public key.
The hash is then signed with the previous owner's private key.
This makes it possible to validate the chain of ownership without have the need to expose the private key, and the signature makes it virtually impossible to alter the transaction after it's been issued.
But what if somebody tries you pay 2 different people with Bitcoin at the exact same time or double spend their money.
That's where mining comes in, which is a system that allows multiple computers around the world to agree on the appropriate state of the entire system or ledger.
Each new transaction is broadcast to all nodes in the network.
The transactions are packaged into a block, then miners will expend computing power to validate proof of work.
They compute a proof for a random problem that is very difficult to solve, but very easy to verify.
The first miner to solve the proof, which happens via dumb luck, gets a portion of the Bitcoin as a reward.
The block is then broadcast back to all other nodes where it's permanently confirmed on the blockchain.
This has been Bitcoin in 100 seconds.
If you wanna more short videos like this, make sure to like and subscribe.
And if you really wanna learn how blockchain technology works, stay tuned, because today we're going beyond 100 seconds 2 build a blockchain from scratch with Node.
Js and TypeScript, and in the process, you'll learn all kinds of useful information about cryptography.
Whether you're into cryptocurrency is not.
There's many concepts in cryptography that you should know about as a developer.
Things like encryption and signing, hashing functions, and algorithms like SHA, RSA, and MD 5 that make all this stuff possible.
Over the next few minutes, you'll learn about these concepts by implementing your own Blockchain from scratch using nothing but Node.
Js and TypeScript.
Our blockchain will contain wallets where users can send money back and forth.
Each transaction is added into block, then that block is mined with a proof of work system, at which point it can be added and confirmed on the blockchain.
And throughout this process, you'll learn about all kinds of important concepts and how they apply to different areas of development.
To get started, you'll need Node.
Js installed on your system then will open up Versus Code to an empty directory, run npm init y to create a new Node.
Js project.
I'll be using TypeScript in this code because we'll be using object oriented programming principles to implement the blockchain.
And having types will just make our code a bit more readable.
School, you can install it with npm, then create a tsconfig file, and copy and paste the values from the main source code.
From there, we can go into our package JSON and create a script called dev that runs the TypeScript compiler with the watch flag to constantly compile our code in the background to plain JavaScript.
From there, you should be able to run and NPM run dev from the command line to keep TypeScript running in the background.
Now we'll write all of our source code in the index CS file.
At the top of that file, we will import the crypto library, which is a built in in Node.
Js.
As the name implies, it's a module at Node that handles a bunch of different functionality for cryptography.
Our simple Blockchain implementation here has 4 classes, a transaction, a block, a chain, and a wallet.
Let's start by implementing the transaction, which is the fundamental purpose of any cryptocurrency, transfer funds from one user to another user in a transaction.
A transaction object has 3 properties, the amount of the transaction denominated and Bitcoins or Fire Coins or whatever you wanna call your coin, along with the person paying the money and the person receiving the money.
Eventually, we'll set up public keys for these users, which which you can think of as a username that will eventually set as these values in the transaction.
One final thing we'll do here is add a method to the class to convert the object to a string.
As we go through the tutorial, we're going to serialize everything as strings, just to make the cryptographic objects easier to work with.
And now we're ready to move on to the block implementation.
A block is like a container for multiple transactions, or in are case just a single transaction to keep things simple.
You can think of a block like an element in an array or more accurately, a linked list, because each block has a reference or link to the previous block in the chain with the previous hash property.
And that brings up a good cryptographic question, what is a hash?
A hashing function allows you to take a value of an arbitrary size, like, say, a transaction, then map it see a value with a fixed length like a hexadecimal string.
The value returned from the hashing function is often called a hash or a hash digest.
When you create a hash, it cannot be reversed to reconstruct the contents of the original value, but what you can do is validate that two values are identical by comparing their hashes.
And that's important for a blockchain, because it ensures that 2 blocks can be linked together without being manipulated.
Now to create a hash of a block, we're going to implement a getter that will first stringify the object itself.
Then we'll use the create hash function from nodecrypto, which specifies a specific hashing algorithm.
In this case, SHA 256.
It stands for secure hash algorithm with a length of 256 bits.
It was developed by the NSA back in 2001, and it's what's known as a one way cryptographic function, which means that it can encrypt data, but it cannot decrypt data back to its original form.
We can use the function to hash the string version of the block, then return the hash value or digest as a hex sub decimal string.
And that's all it takes to build a block.
In this case, it's a transaction that has a link to the previous transaction in the form of a hash, and there also contains a time stamp because all blocks will be placed in chronological order.
Now we can move on to the chain, which again is like a linked list of blocks.
There should only be 1 blockchain, so we'll go ahead and make it a singleton instance by setting up a static instance property that is equal will to a new Chain instance.
That'll just ensure that we have one Chain instance instantiated before anything else.
Now we can declare a property for the chain itself, which is just an array of blocks, and the constructor will define the first block in the chain, which is called the Genesis block.
The previous hash is null because there's nothing for it to link to, then it instantiates a new transaction that transfers a 100 coins to Satoshi.
Notice how we're creating money out of thin air here.
That's no different than when the central bankers or Federal Reserve turn on the money printer.
Now in the Blockchain class, we'll often need to grab the last block in the chain, so we'll go ahead and create a getter to help us out with that.
Then will define a new method named addblock that takes a transaction, the sender's public key, and a signature that we can verify before adding a new block to the chain.
A naive and simple implementation might look like this.
We instantiate a new block, taking the last block's hash and this new transaction, then we push that new block onto the chain, but the problem is that there's no way to know that this is a legitimate transaction.
Anybody could send arbitrary transaction data to transfer coin to someone else.
We can allow people to securely send coin back and forth by implementing a wallet, which is essentially just a wrapper for a public key key and a private key.
The public key is for receiving money, the private key is for spending money.
To generate a public and private key, we're going to use a different algorithm called RSA, which stands for the names of the guys that created it.
And unlike SHA, this is a full encryption algorithm that can encrypt data and then decrypt it if you have the proper key to do so.
To encrypt a value, you would use the public key to convert it to ciphertext, which is an unreadable version of the original value.
Then you would use the private key to decrypt it back to its original form.
But what we're actually more interested in is using the key pair to create a digital signature.
With signing, we don't need to encrypt the message, but instead create a hash of it.
We then sign the hash with our private key, then the message can be verified later using the public key.
If anybody tried to change the message, it would produce a different hash, in which case the verification would fail.
And that's really important for a coin, Because if we didn't have a signature, then someone could intercept the transaction message and change the amount or change the payee with no way to detect that anything was out of the ordinary.
Now Now when generating the key pair with Nodecrypto, I'm going to format them as strings.
And to do that, I'm going to add some extra options here for encoding.
Scheme, the important one to know is the format PEM, which you would normally save to a file on the user's computer system where it could be reused in the future.
Now that we have a public key and private key, we can use it to send money to another user.
The way we do that is by specifying an amount and the public key of the user being paid.
We can then use Nodecrypto to create a signature, again, an SHA 256 format, using the transaction data as the action data adds the value.
We can then create a signature by signing it with the private key.
This is kind of like creating a one time password.
It allows us to verify our identity with the private key without actually having to expose the private key.
The signature depends on both the transaction data and the private key, but it can be verified as authentic using the public key.
We can now attempt to add that block to the blockchain by passing the transaction, the public key, and the signature.
In real life, those values will be transferred over the Internet, then the signature could be verified.
Let's go ahead and refactor the add block method in the chain class.
It'll use Nodecrypto to create a signature verification, then pass the same transaction data to the verifier.
We can then validate it by verifying that the transaction data has not tampered with using the sender's public key and the signature itself, and we're now able to securely verify that that user is actually are trying to spend that amount of money to the other user.
But there's still one more big issue with our Blockchain, and that is the double spend issue.
Imagine the spender tried to send money to 2 different users at the same time.
They could potentially spend more money than they actually own before their transaction is confirmed on the blockchain.
The way Bitcoin addresses that issue is with a proof of work system, which requires each new block to go through a process called mining, where a difficult computational problem is solved in order to confirm the block, but it's very easy to verify that work by multiple other nodes on the system.
When mining is distributed around the world.
It means you have multiple nodes competing to confirm a block on the blockchain and works like a big lottery.
The winner of the lottery earns a portion of the coin as has incentive, which motivates people to invest in the computing resources to mine a coin, and also to pump the price of the coin higher and higher because the higher the price, the more money you make.
You can think of it like converting cloud computing resources into money.
Now to implement a basic proof of work system, we'll go back into our block class and add a nonce value, which is a one time use random number.
Then, in the chain class, we'll add a method called mine that takes that nonce as an argument.
What this method will do is attempt to find a number that when added to the nonce will produce a hash that starts with 4 zeros.
The only real way to figure out that value is with brute force by creating a while loop that goes digit by digit until we find the requested value.
To handle the brute force computation, we'll will create a while loop that creates a hash with the MD 5 algorithm.
It's very similar to SHA 256, but is only a 128 bits and is faster to compute.
We'll continue to create new hashes inside the while loop until we find one that starts with 4 zeros.
When we find it, we can return the solution and then send it off to other nodes where it can be verified, and the block can finally be confirmed on the blockchain.
And and that takes care of our blockchain implementation.
If we go down to the example usage here, you can see we instantiate a few wallets for different users, then allow allow those users to send money to each other using their various public keys.
If we go ahead and log out the chain itself, you can see we have a bunch of blocks or transactions link to each other based on a hash of the previous block.
Now this blockchain is obviously not perfect and something that I just hacked together to teach you the gist of how a blockchain works.
Feel free to grab the full source code on GitHub.
If you learned something in the process, please make sure to like and subscribe and consider sending me some Bitcoin or sponsoring me on hub for just $1 of fiat per month.
I use all that income to sponsor other developers myself.
Thanks for watching, and I will see you in the next one.

Speaker 0: Bitcoin, a peer to peer electronic cash system first described in a 2008 white paper authored by the mysterious Satoshi Nakamoto. The modern financial system relies on our trust of big centralized banks to hold our fiat currencies and execute transactions, but trust is a weakness that eventually spire's intervention by lawyers and government. Bitcoin allows 2 parties to make reliable transactions based on cryptographic proof, eliminating the need for a trusty middleman. The optimists call it digital gold, the pessimists call it fool's gold, but in reality, it's just software. And like all software, its purpose is to arrange change ones and zeros in a meaningful way.

The protocol that makes it meaningful is Blockchain, which allows 2 parties to engage in transactions denominated and Bitcoins or Satoshis, and just like dollars and cents, they have value because we believe they do. From a financial perspective, the Blockchain scene is like a shared public ledger that contains all transactions from all Bitcoin users and is distributed and synchronized around the world, which eliminates the need for a central authority to maintain and validate it. From a technical perspective, think of the Blockchain as a database structured as a linked list, where each record or block represents a group of transactions that have been permanently committed to the database. It works kind of like a Git repo that can never be rebased. The important thing is that each new block is linked to the previous one in the Blockchain, and its creation goes through a very strict set of cryptographic rules.

Each user or wallet has a unique public key for receiving money, kind of like username and a unique private key for spending money, kind of like a password. But before you can spend money, you'll need to prove that you're the owner of a public key that money has been sent to in the past. Each transaction contains a hash or encrypted representation of the previous transaction and the new owner's public key. The hash is then signed with the previous owner's private key. This makes it possible to validate the chain of ownership without have the need to expose the private key, and the signature makes it virtually impossible to alter the transaction after it's been issued.

But what if somebody tries you pay 2 different people with Bitcoin at the exact same time or double spend their money. That's where mining comes in, which is a system that allows multiple computers around the world to agree on the appropriate state of the entire system or ledger. Each new transaction is broadcast to all nodes in the network. The transactions are packaged into a block, then miners will expend computing power to validate proof of work. They compute a proof for a random problem that is very difficult to solve, but very easy to verify.

The first miner to solve the proof, which happens via dumb luck, gets a portion of the Bitcoin as a reward. The block is then broadcast back to all other nodes where it's permanently confirmed on the blockchain. This has been Bitcoin in 100 seconds. If you wanna more short videos like this, make sure to like and subscribe. And if you really wanna learn how blockchain technology works, stay tuned, because today we're going beyond 100 seconds 2 build a blockchain from scratch with Node.

Js and TypeScript, and in the process, you'll learn all kinds of useful information about cryptography. Whether you're into cryptocurrency is not. There's many concepts in cryptography that you should know about as a developer. Things like encryption and signing, hashing functions, and algorithms like SHA, RSA, and MD 5 that make all this stuff possible. Over the next few minutes, you'll learn about these concepts by implementing your own Blockchain from scratch using nothing but Node.

Js and TypeScript. Our blockchain will contain wallets where users can send money back and forth. Each transaction is added into block, then that block is mined with a proof of work system, at which point it can be added and confirmed on the blockchain. And throughout this process, you'll learn about all kinds of important concepts and how they apply to different areas of development. To get started, you'll need Node.

Js installed on your system then will open up Versus Code to an empty directory, run npm init y to create a new Node. Js project. I'll be using TypeScript in this code because we'll be using object oriented programming principles to implement the blockchain. And having types will just make our code a bit more readable. School, you can install it with npm, then create a tsconfig file, and copy and paste the values from the main source code.

From there, we can go into our package JSON and create a script called dev that runs the TypeScript compiler with the watch flag to constantly compile our code in the background to plain JavaScript. From there, you should be able to run and NPM run dev from the command line to keep TypeScript running in the background. Now we'll write all of our source code in the index CS file. At the top of that file, we will import the crypto library, which is a built in in Node. Js.

As the name implies, it's a module at Node that handles a bunch of different functionality for cryptography. Our simple Blockchain implementation here has 4 classes, a transaction, a block, a chain, and a wallet. Let's start by implementing the transaction, which is the fundamental purpose of any cryptocurrency, transfer funds from one user to another user in a transaction. A transaction object has 3 properties, the amount of the transaction denominated and Bitcoins or Fire Coins or whatever you wanna call your coin, along with the person paying the money and the person receiving the money. Eventually, we'll set up public keys for these users, which which you can think of as a username that will eventually set as these values in the transaction.

One final thing we'll do here is add a method to the class to convert the object to a string. As we go through the tutorial, we're going to serialize everything as strings, just to make the cryptographic objects easier to work with. And now we're ready to move on to the block implementation. A block is like a container for multiple transactions, or in are case just a single transaction to keep things simple. You can think of a block like an element in an array or more accurately, a linked list, because each block has a reference or link to the previous block in the chain with the previous hash property.

And that brings up a good cryptographic question, what is a hash? A hashing function allows you to take a value of an arbitrary size, like, say, a transaction, then map it see a value with a fixed length like a hexadecimal string. The value returned from the hashing function is often called a hash or a hash digest. When you create a hash, it cannot be reversed to reconstruct the contents of the original value, but what you can do is validate that two values are identical by comparing their hashes. And that's important for a blockchain, because it ensures that 2 blocks can be linked together without being manipulated.

Now to create a hash of a block, we're going to implement a getter that will first stringify the object itself. Then we'll use the create hash function from nodecrypto, which specifies a specific hashing algorithm. In this case, SHA 256. It stands for secure hash algorithm with a length of 256 bits. It was developed by the NSA back in 2001, and it's what's known as a one way cryptographic function, which means that it can encrypt data, but it cannot decrypt data back to its original form.

We can use the function to hash the string version of the block, then return the hash value or digest as a hex sub decimal string. And that's all it takes to build a block. In this case, it's a transaction that has a link to the previous transaction in the form of a hash, and there also contains a time stamp because all blocks will be placed in chronological order. Now we can move on to the chain, which again is like a linked list of blocks. There should only be 1 blockchain, so we'll go ahead and make it a singleton instance by setting up a static instance property that is equal will to a new Chain instance.

That'll just ensure that we have one Chain instance instantiated before anything else. Now we can declare a property for the chain itself, which is just an array of blocks, and the constructor will define the first block in the chain, which is called the Genesis block. The previous hash is null because there's nothing for it to link to, then it instantiates a new transaction that transfers a 100 coins to Satoshi. Notice how we're creating money out of thin air here. That's no different than when the central bankers or Federal Reserve turn on the money printer.

Now in the Blockchain class, we'll often need to grab the last block in the chain, so we'll go ahead and create a getter to help us out with that. Then will define a new method named addblock that takes a transaction, the sender's public key, and a signature that we can verify before adding a new block to the chain. A naive and simple implementation might look like this. We instantiate a new block, taking the last block's hash and this new transaction, then we push that new block onto the chain, but the problem is that there's no way to know that this is a legitimate transaction. Anybody could send arbitrary transaction data to transfer coin to someone else.

We can allow people to securely send coin back and forth by implementing a wallet, which is essentially just a wrapper for a public key key and a private key. The public key is for receiving money, the private key is for spending money. To generate a public and private key, we're going to use a different algorithm called RSA, which stands for the names of the guys that created it. And unlike SHA, this is a full encryption algorithm that can encrypt data and then decrypt it if you have the proper key to do so. To encrypt a value, you would use the public key to convert it to ciphertext, which is an unreadable version of the original value.

Then you would use the private key to decrypt it back to its original form. But what we're actually more interested in is using the key pair to create a digital signature. With signing, we don't need to encrypt the message, but instead create a hash of it. We then sign the hash with our private key, then the message can be verified later using the public key. If anybody tried to change the message, it would produce a different hash, in which case the verification would fail.

And that's really important for a coin, Because if we didn't have a signature, then someone could intercept the transaction message and change the amount or change the payee with no way to detect that anything was out of the ordinary. Now Now when generating the key pair with Nodecrypto, I'm going to format them as strings. And to do that, I'm going to add some extra options here for encoding. Scheme, the important one to know is the format PEM, which you would normally save to a file on the user's computer system where it could be reused in the future. Now that we have a public key and private key, we can use it to send money to another user.

The way we do that is by specifying an amount and the public key of the user being paid. We can then use Nodecrypto to create a signature, again, an SHA 256 format, using the transaction data as the action data adds the value. We can then create a signature by signing it with the private key. This is kind of like creating a one time password. It allows us to verify our identity with the private key without actually having to expose the private key.

The signature depends on both the transaction data and the private key, but it can be verified as authentic using the public key. We can now attempt to add that block to the blockchain by passing the transaction, the public key, and the signature. In real life, those values will be transferred over the Internet, then the signature could be verified. Let's go ahead and refactor the add block method in the chain class. It'll use Nodecrypto to create a signature verification, then pass the same transaction data to the verifier.

We can then validate it by verifying that the transaction data has not tampered with using the sender's public key and the signature itself, and we're now able to securely verify that that user is actually are trying to spend that amount of money to the other user. But there's still one more big issue with our Blockchain, and that is the double spend issue. Imagine the spender tried to send money to 2 different users at the same time. They could potentially spend more money than they actually own before their transaction is confirmed on the blockchain. The way Bitcoin addresses that issue is with a proof of work system, which requires each new block to go through a process called mining, where a difficult computational problem is solved in order to confirm the block, but it's very easy to verify that work by multiple other nodes on the system.

When mining is distributed around the world. It means you have multiple nodes competing to confirm a block on the blockchain and works like a big lottery. The winner of the lottery earns a portion of the coin as has incentive, which motivates people to invest in the computing resources to mine a coin, and also to pump the price of the coin higher and higher because the higher the price, the more money you make. You can think of it like converting cloud computing resources into money. Now to implement a basic proof of work system, we'll go back into our block class and add a nonce value, which is a one time use random number.

Then, in the chain class, we'll add a method called mine that takes that nonce as an argument. What this method will do is attempt to find a number that when added to the nonce will produce a hash that starts with 4 zeros. The only real way to figure out that value is with brute force by creating a while loop that goes digit by digit until we find the requested value. To handle the brute force computation, we'll will create a while loop that creates a hash with the MD 5 algorithm. It's very similar to SHA 256, but is only a 128 bits and is faster to compute.

We'll continue to create new hashes inside the while loop until we find one that starts with 4 zeros. When we find it, we can return the solution and then send it off to other nodes where it can be verified, and the block can finally be confirmed on the blockchain. And and that takes care of our blockchain implementation. If we go down to the example usage here, you can see we instantiate a few wallets for different users, then allow allow those users to send money to each other using their various public keys. If we go ahead and log out the chain itself, you can see we have a bunch of blocks or transactions link to each other based on a hash of the previous block.

Now this blockchain is obviously not perfect and something that I just hacked together to teach you the gist of how a blockchain works. Feel free to grab the full source code on GitHub. If you learned something in the process, please make sure to like and subscribe and consider sending me some Bitcoin or sponsoring me on hub for just $1 of fiat per month. I use all that income to sponsor other developers myself. Thanks for watching, and I will see you in the next one.
Bitcoin, a peer to peer electronic cash system first described in a 2008 white paper authored by the mysterious Satoshi Nakamoto. The modern financial system relies on our trust of big centralized banks to hold our fiat currencies and execute transactions, but trust is a weakness that eventually spire's intervention by lawyers and government. Bitcoin allows 2 parties to make reliable transactions based on cryptographic proof, eliminating the need for a trusty middleman. The optimists call it digital gold, the pessimists call it fool's gold, but in reality, it's just software. And like all software, its purpose is to arrange change ones and zeros in a meaningful way. The protocol that makes it meaningful is Blockchain, which allows 2 parties to engage in transactions denominated and Bitcoins or Satoshis, and just like dollars and cents, they have value because we believe they do. From a financial perspective, the Blockchain scene is like a shared public ledger that contains all transactions from all Bitcoin users and is distributed and synchronized around the world, which eliminates the need for a central authority to maintain and validate it. From a technical perspective, think of the Blockchain as a database structured as a linked list, where each record or block represents a group of transactions that have been permanently committed to the database. It works kind of like a Git repo that can never be rebased. The important thing is that each new block is linked to the previous one in the Blockchain, and its creation goes through a very strict set of cryptographic rules. Each user or wallet has a unique public key for receiving money, kind of like username and a unique private key for spending money, kind of like a password. But before you can spend money, you'll need to prove that you're the owner of a public key that money has been sent to in the past. Each transaction contains a hash or encrypted representation of the previous transaction and the new owner's public key. The hash is then signed with the previous owner's private key. This makes it possible to validate the chain of ownership without have the need to expose the private key, and the signature makes it virtually impossible to alter the transaction after it's been issued. But what if somebody tries you pay 2 different people with Bitcoin at the exact same time or double spend their money.
banking
databases
cryptocurrency
finance
intellectual property
security
That's where mining comes in, which is a system that allows multiple computers around the world to agree on the appropriate state of the entire system or ledger.
Each new transaction is broadcast to all nodes in the network. The transactions are packaged into a block, then miners will expend computing power to validate proof of work. They compute a proof for a random problem that is very difficult to solve, but very easy to verify. The first miner to solve the proof, which happens via dumb luck, gets a portion of the Bitcoin as a reward. The block is then broadcast back to all other nodes where it's permanently confirmed on the blockchain. This has been Bitcoin in 100 seconds. If you wanna more short videos like this, make sure to like and subscribe. And if you really wanna learn how blockchain technology works, stay tuned, because today we're going beyond 100 seconds 2 build a blockchain from scratch with Node. Js and TypeScript, and in the process, you'll learn all kinds of useful information about cryptography. Whether you're into cryptocurrency is not. There's many concepts in cryptography that you should know about as a developer. Things like encryption and signing, hashing functions, and algorithms like SHA, RSA, and MD 5 that make all this stuff possible. Over the next few minutes, you'll learn about these concepts by implementing your own Blockchain from scratch using nothing but Node. Js and TypeScript. Our blockchain will contain wallets where users can send money back and forth. Each transaction is added into block, then that block is mined with a proof of work system, at which point it can be added and confirmed on the blockchain. And throughout this process, you'll learn about all kinds of important concepts and how they apply to different areas of development.
gambling
cryptocurrency
mining
probability
To get started, you'll need Node. Js installed on your system then will open up Versus Code to an empty directory, run npm init y to create a new Node. Js project.
I'll be using TypeScript in this code because we'll be using object oriented programming principles to implement the blockchain. And having types will just make our code a bit more readable. School, you can install it with npm, then create a tsconfig file, and copy and paste the values from the main source code.
programming
software development
From there, we can go into our package JSON and create a script called dev that runs the TypeScript compiler with the watch flag to constantly compile our code in the background to plain JavaScript. From there, you should be able to run and NPM run dev from the command line to keep TypeScript running in the background. Now we'll write all of our source code in the index CS file.
At the top of that file, we will import the crypto library, which is a built in in Node. Js. As the name implies, it's a module at Node that handles a bunch of different functionality for cryptography. Our simple Blockchain implementation here has 4 classes, a transaction, a block, a chain, and a wallet. Let's start by implementing the transaction, which is the fundamental purpose of any cryptocurrency, transfer funds from one user to another user in a transaction. A transaction object has 3 properties, the amount of the transaction denominated and Bitcoins or Fire Coins or whatever you wanna call your coin, along with the person paying the money and the person receiving the money. Eventually, we'll set up public keys for these users, which which you can think of as a username that will eventually set as these values in the transaction.
cryptocurrency
banking
gambling
intellectual property
One final thing we'll do here is add a method to the class to convert the object to a string. As we go through the tutorial, we're going to serialize everything as strings, just to make the cryptographic objects easier to work with. And now we're ready to move on to the block implementation. A block is like a container for multiple transactions, or in are case just a single transaction to keep things simple. You can think of a block like an element in an array or more accurately, a linked list, because each block has a reference or link to the previous block in the chain with the previous hash property. And that brings up a good cryptographic question, what is a hash? A hashing function allows you to take a value of an arbitrary size, like, say, a transaction, then map it see a value with a fixed length like a hexadecimal string. The value returned from the hashing function is often called a hash or a hash digest. When you create a hash, it cannot be reversed to reconstruct the contents of the original value, but what you can do is validate that two values are identical by comparing their hashes. And that's important for a blockchain, because it ensures that 2 blocks can be linked together without being manipulated. Now to create a hash of a block, we're going to implement a getter that will first stringify the object itself.
Then we'll use the create hash function from nodecrypto, which specifies a specific hashing algorithm. In this case, SHA 256. It stands for secure hash algorithm with a length of 256 bits. It was developed by the NSA back in 2001, and it's what's known as a one way cryptographic function, which means that it can encrypt data, but it cannot decrypt data back to its original form. We can use the function to hash the string version of the block, then return the hash value or digest as a hex sub decimal string.
cryptocurrency
And that's all it takes to build a block. In this case, it's a transaction that has a link to the previous transaction in the form of a hash, and there also contains a time stamp because all blocks will be placed in chronological order. Now we can move on to the chain, which again is like a linked list of blocks. There should only be 1 blockchain, so we'll go ahead and make it a singleton instance by setting up a static instance property that is equal will to a new Chain instance. That'll just ensure that we have one Chain instance instantiated before anything else. Now we can declare a property for the chain itself, which is just an array of blocks, and the constructor will define the first block in the chain, which is called the Genesis block.
The previous hash is null because there's nothing for it to link to, then it instantiates a new transaction that transfers a 100 coins to Satoshi. Notice how we're creating money out of thin air here. That's no different than when the central bankers or Federal Reserve turn on the money printer. Now in the Blockchain class, we'll often need to grab the last block in the chain, so we'll go ahead and create a getter to help us out with that.
banking
finance
cryptocurrency
gambling
Then will define a new method named addblock that takes a transaction, the sender's public key, and a signature that we can verify before adding a new block to the chain. A naive and simple implementation might look like this. We instantiate a new block, taking the last block's hash and this new transaction, then we push that new block onto the chain, but the problem is that there's no way to know that this is a legitimate transaction. Anybody could send arbitrary transaction data to transfer coin to someone else. We can allow people to securely send coin back and forth by implementing a wallet, which is essentially just a wrapper for a public key key and a private key. The public key is for receiving money, the private key is for spending money. To generate a public and private key, we're going to use a different algorithm called RSA, which stands for the names of the guys that created it. And unlike SHA, this is a full encryption algorithm that can encrypt data and then decrypt it if you have the proper key to do so.
security
cryptocurrency
gambling
logic
To encrypt a value, you would use the public key to convert it to ciphertext, which is an unreadable version of the original value. Then you would use the private key to decrypt it back to its original form. But what we're actually more interested in is using the key pair to create a digital signature.
cryptocurrency
With signing, we don't need to encrypt the message, but instead create a hash of it. We then sign the hash with our private key, then the message can be verified later using the public key. If anybody tried to change the message, it would produce a different hash, in which case the verification would fail.
And that's really important for a coin, Because if we didn't have a signature, then someone could intercept the transaction message and change the amount or change the payee with no way to detect that anything was out of the ordinary. Now Now when generating the key pair with Nodecrypto, I'm going to format them as strings.
cryptocurrency
And to do that, I'm going to add some extra options here for encoding. Scheme, the important one to know is the format PEM, which you would normally save to a file on the user's computer system where it could be reused in the future. Now that we have a public key and private key, we can use it to send money to another user. The way we do that is by specifying an amount and the public key of the user being paid. We can then use Nodecrypto to create a signature, again, an SHA 256 format, using the transaction data as the action data adds the value. We can then create a signature by signing it with the private key. This is kind of like creating a one time password. It allows us to verify our identity with the private key without actually having to expose the private key.
The signature depends on both the transaction data and the private key, but it can be verified as authentic using the public key. We can now attempt to add that block to the blockchain by passing the transaction, the public key, and the signature. In real life, those values will be transferred over the Internet, then the signature could be verified. Let's go ahead and refactor the add block method in the chain class. It'll use Nodecrypto to create a signature verification, then pass the same transaction data to the verifier. We can then validate it by verifying that the transaction data has not tampered with using the sender's public key and the signature itself, and we're now able to securely verify that that user is actually are trying to spend that amount of money to the other user. But there's still one more big issue with our Blockchain, and that is the double spend issue. Imagine the spender tried to send money to 2 different users at the same time. They could potentially spend more money than they actually own before their transaction is confirmed on the blockchain. The way Bitcoin addresses that issue is with a proof of work system, which requires each new block to go through a process called mining, where a difficult computational problem is solved in order to confirm the block, but it's very easy to verify that work by multiple other nodes on the system. When mining is distributed around the world. It means you have multiple nodes competing to confirm a block on the blockchain and works like a big lottery. The winner of the lottery earns a portion of the coin as has incentive, which motivates people to invest in the computing resources to mine a coin, and also to pump the price of the coin higher and higher because the higher the price, the more money you make. You can think of it like converting cloud computing resources into money. Now to implement a basic proof of work system, we'll go back into our block class and add a nonce value, which is a one time use random number. Then, in the chain class, we'll add a method called mine that takes that nonce as an argument.
mining
gambling
cryptocurrency
revenue
logic
intellectual property
What this method will do is attempt to find a number that when added to the nonce will produce a hash that starts with 4 zeros. The only real way to figure out that value is with brute force by creating a while loop that goes digit by digit until we find the requested value. To handle the brute force computation, we'll will create a while loop that creates a hash with the MD 5 algorithm. It's very similar to SHA 256, but is only a 128 bits and is faster to compute. We'll continue to create new hashes inside the while loop until we find one that starts with 4 zeros. When we find it, we can return the solution and then send it off to other nodes where it can be verified, and the block can finally be confirmed on the blockchain.
And and that takes care of our blockchain implementation. If we go down to the example usage here, you can see we instantiate a few wallets for different users, then allow allow those users to send money to each other using their various public keys. If we go ahead and log out the chain itself, you can see we have a bunch of blocks or transactions link to each other based on a hash of the previous block.
cryptocurrency
Now this blockchain is obviously not perfect and something that I just hacked together to teach you the gist of how a blockchain works. Feel free to grab the full source code on GitHub. If you learned something in the process, please make sure to like and subscribe and consider sending me some Bitcoin or sponsoring me on hub for just $1 of fiat per month.
cryptocurrency
I use all that income to sponsor other developers myself. Thanks for watching, and I will see you in the next one.
Bitcoin, a peer to peer electronic cash system first described in a 2008 white paper authored by the mysterious Satoshi Nakamoto. The modern financial system relies on our trust of big centralized banks to hold our fiat currencies and execute transactions, but trust is a weakness that eventually spire's intervention by lawyers and government. Bitcoin allows 2 parties to make reliable transactions based on cryptographic proof, eliminating the need for a trusty middleman. The optimists call it digital gold, the pessimists call it fool's gold, but in reality, it's just software. And like all software, its purpose is to arrange change ones and zeros in a meaningful way. The protocol that makes it meaningful is Blockchain, which allows 2 parties to engage in transactions denominated and Bitcoins or Satoshis, and just like dollars and cents, they have value because we believe they do. From a financial perspective, the Blockchain scene is like a shared public ledger that contains all transactions from all Bitcoin users and is distributed and synchronized around the world, which eliminates the need for a central authority to maintain and validate it. From a technical perspective, think of the Blockchain as a database structured as a linked list, where each record or block represents a group of transactions that have been permanently committed to the database. It works kind of like a Git repo that can never be rebased. The important thing is that each new block is linked to the previous one in the Blockchain, and its creation goes through a very strict set of cryptographic rules. Each user or wallet has a unique public key for receiving money, kind of like username and a unique private key for spending money, kind of like a password. But before you can spend money, you'll need to prove that you're the owner of a public key that money has been sent to in the past. Each transaction contains a hash or encrypted representation of the previous transaction and the new owner's public key. The hash is then signed with the previous owner's private key. This makes it possible to validate the chain of ownership without have the need to expose the private key, and the signature makes it virtually impossible to alter the transaction after it's been issued. But what if somebody tries you pay 2 different people with Bitcoin at the exact same time or double spend their money. That's where mining comes in, which is a system that allows multiple computers around the world to agree on the appropriate state of the entire system or ledger. Each new transaction is broadcast to all nodes in the network. The transactions are packaged into a block, then miners will expend computing power to validate proof of work. They compute a proof for a random problem that is very difficult to solve, but very easy to verify. The first miner to solve the proof, which happens via dumb luck, gets a portion of the Bitcoin as a reward. The block is then broadcast back to all other nodes where it's permanently confirmed on the blockchain. This has been Bitcoin in 100 seconds. If you wanna more short videos like this, make sure to like and subscribe. And if you really wanna learn how blockchain technology works, stay tuned, because today we're going beyond 100 seconds 2 build a blockchain from scratch with Node. Js and TypeScript, and in the process, you'll learn all kinds of useful information about cryptography. Whether you're into cryptocurrency is not. There's many concepts in cryptography that you should know about as a developer. Things like encryption and signing, hashing functions, and algorithms like SHA, RSA, and MD 5 that make all this stuff possible. Over the next few minutes, you'll learn about these concepts by implementing your own Blockchain from scratch using nothing but Node. Js and TypeScript. Our blockchain will contain wallets where users can send money back and forth. Each transaction is added into block, then that block is mined with a proof of work system, at which point it can be added and confirmed on the blockchain. And throughout this process, you'll learn about all kinds of important concepts and how they apply to different areas of development. To get started, you'll need Node. Js installed on your system then will open up Versus Code to an empty directory, run npm init y to create a new Node. Js project. I'll be using TypeScript in this code because we'll be using object oriented programming principles to implement the blockchain. And having types will just make our code a bit more readable. School, you can install it with npm, then create a tsconfig file, and copy and paste the values from the main source code. From there, we can go into our package JSON and create a script called dev that runs the TypeScript compiler with the watch flag to constantly compile our code in the background to plain JavaScript. From there, you should be able to run and NPM run dev from the command line to keep TypeScript running in the background. Now we'll write all of our source code in the index CS file. At the top of that file, we will import the crypto library, which is a built in in Node. Js. As the name implies, it's a module at Node that handles a bunch of different functionality for cryptography. Our simple Blockchain implementation here has 4 classes, a transaction, a block, a chain, and a wallet. Let's start by implementing the transaction, which is the fundamental purpose of any cryptocurrency, transfer funds from one user to another user in a transaction. A transaction object has 3 properties, the amount of the transaction denominated and Bitcoins or Fire Coins or whatever you wanna call your coin, along with the person paying the money and the person receiving the money. Eventually, we'll set up public keys for these users, which which you can think of as a username that will eventually set as these values in the transaction. One final thing we'll do here is add a method to the class to convert the object to a string. As we go through the tutorial, we're going to serialize everything as strings, just to make the cryptographic objects easier to work with. And now we're ready to move on to the block implementation. A block is like a container for multiple transactions, or in are case just a single transaction to keep things simple. You can think of a block like an element in an array or more accurately, a linked list, because each block has a reference or link to the previous block in the chain with the previous hash property. And that brings up a good cryptographic question, what is a hash? A hashing function allows you to take a value of an arbitrary size, like, say, a transaction, then map it see a value with a fixed length like a hexadecimal string. The value returned from the hashing function is often called a hash or a hash digest. When you create a hash, it cannot be reversed to reconstruct the contents of the original value, but what you can do is validate that two values are identical by comparing their hashes. And that's important for a blockchain, because it ensures that 2 blocks can be linked together without being manipulated. Now to create a hash of a block, we're going to implement a getter that will first stringify the object itself. Then we'll use the create hash function from nodecrypto, which specifies a specific hashing algorithm. In this case, SHA 256. It stands for secure hash algorithm with a length of 256 bits. It was developed by the NSA back in 2001, and it's what's known as a one way cryptographic function, which means that it can encrypt data, but it cannot decrypt data back to its original form. We can use the function to hash the string version of the block, then return the hash value or digest as a hex sub decimal string. And that's all it takes to build a block. In this case, it's a transaction that has a link to the previous transaction in the form of a hash, and there also contains a time stamp because all blocks will be placed in chronological order. Now we can move on to the chain, which again is like a linked list of blocks. There should only be 1 blockchain, so we'll go ahead and make it a singleton instance by setting up a static instance property that is equal will to a new Chain instance. That'll just ensure that we have one Chain instance instantiated before anything else. Now we can declare a property for the chain itself, which is just an array of blocks, and the constructor will define the first block in the chain, which is called the Genesis block. The previous hash is null because there's nothing for it to link to, then it instantiates a new transaction that transfers a 100 coins to Satoshi. Notice how we're creating money out of thin air here. That's no different than when the central bankers or Federal Reserve turn on the money printer. Now in the Blockchain class, we'll often need to grab the last block in the chain, so we'll go ahead and create a getter to help us out with that. Then will define a new method named addblock that takes a transaction, the sender's public key, and a signature that we can verify before adding a new block to the chain. A naive and simple implementation might look like this. We instantiate a new block, taking the last block's hash and this new transaction, then we push that new block onto the chain, but the problem is that there's no way to know that this is a legitimate transaction. Anybody could send arbitrary transaction data to transfer coin to someone else. We can allow people to securely send coin back and forth by implementing a wallet, which is essentially just a wrapper for a public key key and a private key. The public key is for receiving money, the private key is for spending money. To generate a public and private key, we're going to use a different algorithm called RSA, which stands for the names of the guys that created it. And unlike SHA, this is a full encryption algorithm that can encrypt data and then decrypt it if you have the proper key to do so. To encrypt a value, you would use the public key to convert it to ciphertext, which is an unreadable version of the original value. Then you would use the private key to decrypt it back to its original form. But what we're actually more interested in is using the key pair to create a digital signature. With signing, we don't need to encrypt the message, but instead create a hash of it. We then sign the hash with our private key, then the message can be verified later using the public key. If anybody tried to change the message, it would produce a different hash, in which case the verification would fail. And that's really important for a coin, Because if we didn't have a signature, then someone could intercept the transaction message and change the amount or change the payee with no way to detect that anything was out of the ordinary. Now Now when generating the key pair with Nodecrypto, I'm going to format them as strings. And to do that, I'm going to add some extra options here for encoding. Scheme, the important one to know is the format PEM, which you would normally save to a file on the user's computer system where it could be reused in the future. Now that we have a public key and private key, we can use it to send money to another user. The way we do that is by specifying an amount and the public key of the user being paid. We can then use Nodecrypto to create a signature, again, an SHA 256 format, using the transaction data as the action data adds the value. We can then create a signature by signing it with the private key. This is kind of like creating a one time password. It allows us to verify our identity with the private key without actually having to expose the private key. The signature depends on both the transaction data and the private key, but it can be verified as authentic using the public key. We can now attempt to add that block to the blockchain by passing the transaction, the public key, and the signature. In real life, those values will be transferred over the Internet, then the signature could be verified. Let's go ahead and refactor the add block method in the chain class. It'll use Nodecrypto to create a signature verification, then pass the same transaction data to the verifier. We can then validate it by verifying that the transaction data has not tampered with using the sender's public key and the signature itself, and we're now able to securely verify that that user is actually are trying to spend that amount of money to the other user. But there's still one more big issue with our Blockchain, and that is the double spend issue. Imagine the spender tried to send money to 2 different users at the same time. They could potentially spend more money than they actually own before their transaction is confirmed on the blockchain. The way Bitcoin addresses that issue is with a proof of work system, which requires each new block to go through a process called mining, where a difficult computational problem is solved in order to confirm the block, but it's very easy to verify that work by multiple other nodes on the system. When mining is distributed around the world. It means you have multiple nodes competing to confirm a block on the blockchain and works like a big lottery. The winner of the lottery earns a portion of the coin as has incentive, which motivates people to invest in the computing resources to mine a coin, and also to pump the price of the coin higher and higher because the higher the price, the more money you make. You can think of it like converting cloud computing resources into money. Now to implement a basic proof of work system, we'll go back into our block class and add a nonce value, which is a one time use random number. Then, in the chain class, we'll add a method called mine that takes that nonce as an argument. What this method will do is attempt to find a number that when added to the nonce will produce a hash that starts with 4 zeros. The only real way to figure out that value is with brute force by creating a while loop that goes digit by digit until we find the requested value. To handle the brute force computation, we'll will create a while loop that creates a hash with the MD 5 algorithm. It's very similar to SHA 256, but is only a 128 bits and is faster to compute. We'll continue to create new hashes inside the while loop until we find one that starts with 4 zeros. When we find it, we can return the solution and then send it off to other nodes where it can be verified, and the block can finally be confirmed on the blockchain. And and that takes care of our blockchain implementation. If we go down to the example usage here, you can see we instantiate a few wallets for different users, then allow allow those users to send money to each other using their various public keys. If we go ahead and log out the chain itself, you can see we have a bunch of blocks or transactions link to each other based on a hash of the previous block. Now this blockchain is obviously not perfect and something that I just hacked together to teach you the gist of how a blockchain works. Feel free to grab the full source code on GitHub. If you learned something in the process, please make sure to like and subscribe and consider sending me some Bitcoin or sponsoring me on hub for just $1 of fiat per month. I use all that income to sponsor other developers myself. Thanks for watching, and I will see you in the next one.
Bitcoin,
bitcoin
a
a
peer
peer
to
to
peer
peer
electronic
electronic
cash
cash
system
system
first
first
described
described
in
in
a
a
2008
2008
white
white
paper
paper
authored
authored
by
by
the
the
mysterious
mysterious
Satoshi
satoshi
Nakamoto.
nakamoto
The
the
modern
modern
financial
financial
system
system
relies
relies
on
on
our
our
trust
trust
of
of
big
big
centralized
centralized
banks
banks
to
to
hold
hold
our
our
fiat
fiat
currencies
currencies
and
and
execute
execute
transactions,
transactions
but
but
trust
trust
is
is
a
a
weakness
weakness
that
that
eventually
eventually
spire's
spire's
intervention
intervention
by
by
lawyers
lawyers
and
and
government.
government
Bitcoin
bitcoin
allows
allows
2
2
parties
parties
to
to
make
make
reliable
reliable
transactions
transactions
based
based
on
on
cryptographic
cryptographic
proof,
proof
eliminating
eliminating
the
the
need
need
for
for
a
a
trusty
trusty
middleman.
middleman
The
the
optimists
optimists
call
call
it
it
digital
digital
gold,
gold
the
the
pessimists
pessimists
call
call
it
it
fool's
fool's
gold,
gold
but
but
in
in
reality,
reality
it's
it's
just
just
software.
software
And
and
like
like
all
all
software,
software
its
its
purpose
purpose
is
is
to
to
arrange
arrange
change
change
ones
ones
and
and
zeros
zeros
in
in
a
a
meaningful
meaningful
way.
way
The
the
protocol
protocol
that
that
makes
makes
it
it
meaningful
meaningful
is
is
Blockchain,
blockchain
which
which
allows
allows
2
2
parties
parties
to
to
engage
engage
in
in
transactions
transactions
denominated
denominated
and
and
Bitcoins
bitcoins
or
or
Satoshis,
satoshis
and
and
just
just
like
like
dollars
dollars
and
and
cents,
cents
they
they
have
have
value
value
because
because
we
we
believe
believe
they
they
do.
do
From
from
a
a
financial
financial
perspective,
perspective
the
the
Blockchain
blockchain
scene
scene
is
is
like
like
a
a
shared
shared
public
public
ledger
ledger
that
that
contains
contains
all
all
transactions
transactions
from
from
all
all
Bitcoin
bitcoin
users
users
and
and
is
is
distributed
distributed
and
and
synchronized
synchronized
around
around
the
the
world,
world
which
which
eliminates
eliminates
the
the
need
need
for
for
a
a
central
central
authority
authority
to
to
maintain
maintain
and
and
validate
validate
it.
it
From
from
a
a
technical
technical
perspective,
perspective
think
think
of
of
the
the
Blockchain
blockchain
as
as
a
a
database
database
structured
structured
as
as
a
a
linked
linked
list,
list
where
where
each
each
record
record
or
or
block
block
represents
represents
a
a
group
group
of
of
transactions
transactions
that
that
have
have
been
been
permanently
permanently
committed
committed
to
to
the
the
database.
database
It
it
works
works
kind
kind
of
of
like
like
a
a
Git
git
repo
repo
that
that
can
can
never
never
be
be
rebased.
rebased
The
the
important
important
thing
thing
is
is
that
that
each
each
new
new
block
block
is
is
linked
linked
to
to
the
the
previous
previous
one
one
in
in
the
the
Blockchain,
blockchain
and
and
its
its
creation
creation
goes
goes
through
through
a
a
very
very
strict
strict
set
set
of
of
cryptographic
cryptographic
rules.
rules
Each
each
user
user
or
or
wallet
wallet
has
has
a
a
unique
unique
public
public
key
key
for
for
receiving
receiving
money,
money
kind
kind
of
of
like
like
username
username
and
and
a
a
unique
unique
private
private
key
key
for
for
spending
spending
money,
money
kind
kind
of
of
like
like
a
a
password.
password
But
but
before
before
you
you
can
can
spend
spend
money,
money
you'll
you'll
need
need
to
to
prove
prove
that
that
you're
you're
the
the
owner
owner
of
of
a
a
public
public
key
key
that
that
money
money
has
has
been
been
sent
sent
to
to
in
in
the
the
past.
past
Each
each
transaction
transaction
contains
contains
a
a
hash
hash
or
or
encrypted
encrypted
representation
representation
of
of
the
the
previous
previous
transaction
transaction
and
and
the
the
new
new
owner's
owner's
public
public
key.
key
The
the
hash
hash
is
is
then
then
signed
signed
with
with
the
the
previous
previous
owner's
owner's
private
private
key.
key
This
this
makes
makes
it
it
possible
possible
to
to
validate
validate
the
the
chain
chain
of
of
ownership
ownership
without
without
have
have
the
the
need
need
to
to
expose
expose
the
the
private
private
key,
key
and
and
the
the
signature
signature
makes
makes
it
it
virtually
virtually
impossible
impossible
to
to
alter
alter
the
the
transaction
transaction
after
after
it's
it's
been
been
issued.
issued
But
but
what
what
if
if
somebody
somebody
tries
tries
you
you
pay
pay
2
2
different
different
people
people
with
with
Bitcoin
bitcoin
at
at
the
the
exact
exact
same
same
time
time
or
or
double
double
spend
spend
their
their
money.
money
That's
that's
where
where
mining
mining
comes
comes
in,
in
which
which
is
is
a
a
system
system
that
that
allows
allows
multiple
multiple
computers
computers
around
around
the
the
world
world
to
to
agree
agree
on
on
the
the
appropriate
appropriate
state
state
of
of
the
the
entire
entire
system
system
or
or
ledger.
ledger
Each
each
new
new
transaction
transaction
is
is
broadcast
broadcast
to
to
all
all
nodes
nodes
in
in
the
the
network.
network
The
the
transactions
transactions
are
are
packaged
packaged
into
into
a
a
block,
block
then
then
miners
miners
will
will
expend
expend
computing
computing
power
power
to
to
validate
validate
proof
proof
of
of
work.
work
They
they
compute
compute
a
a
proof
proof
for
for
a
a
random
random
problem
problem
that
that
is
is
very
very
difficult
difficult
to
to
solve,
solve
but
but
very
very
easy
easy
to
to
verify.
verify
The
the
first
first
miner
miner
to
to
solve
solve
the
the
proof,
proof
which
which
happens
happens
via
via
dumb
dumb
luck,
luck
gets
gets
a
a
portion
portion
of
of
the
the
Bitcoin
bitcoin
as
as
a
a
reward.
reward
The
the
block
block
is
is
then
then
broadcast
broadcast
back
back
to
to
all
all
other
other
nodes
nodes
where
where
it's
it's
permanently
permanently
confirmed
confirmed
on
on
the
the
blockchain.
blockchain
This
this
has
has
been
been
Bitcoin
bitcoin
in
in
100
100
seconds.
seconds
If
if
you
you
wanna
wanna
more
more
short
short
videos
videos
like
like
this,
this
make
make
sure
sure
to
to
like
like
and
and
subscribe.
subscribe
And
and
if
if
you
you
really
really
wanna
wanna
learn
learn
how
how
blockchain
blockchain
technology
technology
works,
works
stay
stay
tuned,
tuned
because
because
today
today
we're
we're
going
going
beyond
beyond
100
100
seconds
seconds
2
2
build
build
a
a
blockchain
blockchain
from
from
scratch
scratch
with
with
Node.
node
Js
js
and
and
TypeScript,
typescript
and
and
in
in
the
the
process,
process
you'll
you'll
learn
learn
all
all
kinds
kinds
of
of
useful
useful
information
information
about
about
cryptography.
cryptography
Whether
whether
you're
you're
into
into
cryptocurrency
cryptocurrency
is
is
not.
not
There's
there's
many
many
concepts
concepts
in
in
cryptography
cryptography
that
that
you
you
should
should
know
know
about
about
as
as
a
a
developer.
developer
Things
things
like
like
encryption
encryption
and
and
signing,
signing
hashing
hashing
functions,
functions
and
and
algorithms
algorithms
like
like
SHA,
sha
RSA,
rsa
and
and
MD
md
5
5
that
that
make
make
all
all
this
this
stuff
stuff
possible.
possible
Over
over
the
the
next
next
few
few
minutes,
minutes
you'll
you'll
learn
learn
about
about
these
these
concepts
concepts
by
by
implementing
implementing
your
your
own
own
Blockchain
blockchain
from
from
scratch
scratch
using
using
nothing
nothing
but
but
Node.
node
Js
js
and
and
TypeScript.
typescript
Our
our
blockchain
blockchain
will
will
contain
contain
wallets
wallets
where
where
users
users
can
can
send
send
money
money
back
back
and
and
forth.
forth
Each
each
transaction
transaction
is
is
added
added
into
into
block,
block
then
then
that
that
block
block
is
is
mined
mined
with
with
a
a
proof
proof
of
of
work
work
system,
system
at
at
which
which
point
point
it
it
can
can
be
be
added
added
and
and
confirmed
confirmed
on
on
the
the
blockchain.
blockchain
And
and
throughout
throughout
this
this
process,
process
you'll
you'll
learn
learn
about
about
all
all
kinds
kinds
of
of
important
important
concepts
concepts
and
and
how
how
they
they
apply
apply
to
to
different
different
areas
areas
of
of
development.
development
To
to
get
get
started,
started
you'll
you'll
need
need
Node.
node
Js
js
installed
installed
on
on
your
your
system
system
then
then
will
will
open
open
up
up
Versus
versus
Code
code
to
to
an
an
empty
empty
directory,
directory
run
run
npm
npm
init
init
y
y
to
to
create
create
a
a
new
new
Node.
node
Js
js
project.
project
I'll
i'll
be
be
using
using
TypeScript
typescript
in
in
this
this
code
code
because
because
we'll
we'll
be
be
using
using
object
object
oriented
oriented
programming
programming
principles
principles
to
to
implement
implement
the
the
blockchain.
blockchain
And
and
having
having
types
types
will
will
just
just
make
make
our
our
code
code
a
a
bit
bit
more
more
readable.
readable
School,
school
you
you
can
can
install
install
it
it
with
with
npm,
npm
then
then
create
create
a
a
tsconfig
tsconfig
file,
file
and
and
copy
copy
and
and
paste
paste
the
the
values
values
from
from
the
the
main
main
source
source
code.
code
From
from
there,
there
we
we
can
can
go
go
into
into
our
our
package
package
JSON
json
and
and
create
create
a
a
script
script
called
called
dev
dev
that
that
runs
runs
the
the
TypeScript
typescript
compiler
compiler
with
with
the
the
watch
watch
flag
flag
to
to
constantly
constantly
compile
compile
our
our
code
code
in
in
the
the
background
background
to
to
plain
plain
JavaScript.
javascript
From
from
there,
there
you
you
should
should
be
be
able
able
to
to
run
run
and
and
NPM
npm
run
run
dev
dev
from
from
the
the
command
command
line
line
to
to
keep
keep
TypeScript
typescript
running
running
in
in
the
the
background.
background
Now
now
we'll
we'll
write
write
all
all
of
of
our
our
source
source
code
code
in
in
the
the
index
index
CS
cs
file.
file
At
at
the
the
top
top
of
of
that
that
file,
file
we
we
will
will
import
import
the
the
crypto
crypto
library,
library
which
which
is
is
a
a
built
built
in
in
in
in
Node.
node
Js.
js
As
as
the
the
name
name
implies,
implies
it's
it's
a
a
module
module
at
at
Node
node
that
that
handles
handles
a
a
bunch
bunch
of
of
different
different
functionality
functionality
for
for
cryptography.
cryptography
Our
our
simple
simple
Blockchain
blockchain
implementation
implementation
here
here
has
has
4
4
classes,
classes
a
a
transaction,
transaction
a
a
block,
block
a
a
chain,
chain
and
and
a
a
wallet.
wallet
Let's
let's
start
start
by
by
implementing
implementing
the
the
transaction,
transaction
which
which
is
is
the
the
fundamental
fundamental
purpose
purpose
of
of
any
any
cryptocurrency,
cryptocurrency
transfer
transfer
funds
funds
from
from
one
one
user
user
to
to
another
another
user
user
in
in
a
a
transaction.
transaction
A
a
transaction
transaction
object
object
has
has
3
3
properties,
properties
the
the
amount
amount
of
of
the
the
transaction
transaction
denominated
denominated
and
and
Bitcoins
bitcoins
or
or
Fire
fire
Coins
coins
or
or
whatever
whatever
you
you
wanna
wanna
call
call
your
your
coin,
coin
along
along
with
with
the
the
person
person
paying
paying
the
the
money
money
and
and
the
the
person
person
receiving
receiving
the
the
money.
money
Eventually,
eventually
we'll
we'll
set
set
up
up
public
public
keys
keys
for
for
these
these
users,
users
which
which
which
which
you
you
can
can
think
think
of
of
as
as
a
a
username
username
that
that
will
will
eventually
eventually
set
set
as
as
these
these
values
values
in
in
the
the
transaction.
transaction
One
one
final
final
thing
thing
we'll
we'll
do
do
here
here
is
is
add
add
a
a
method
method
to
to
the
the
class
class
to
to
convert
convert
the
the
object
object
to
to
a
a
string.
string
As
as
we
we
go
go
through
through
the
the
tutorial,
tutorial
we're
we're
going
going
to
to
serialize
serialize
everything
everything
as
as
strings,
strings
just
just
to
to
make
make
the
the
cryptographic
cryptographic
objects
objects
easier
easier
to
to
work
work
with.
with
And
and
now
now
we're
we're
ready
ready
to
to
move
move
on
on
to
to
the
the
block
block
implementation.
implementation
A
a
block
block
is
is
like
like
a
a
container
container
for
for
multiple
multiple
transactions,
transactions
or
or
in
in
are
are
case
case
just
just
a
a
single
single
transaction
transaction
to
to
keep
keep
things
things
simple.
simple
You
you
can
can
think
think
of
of
a
a
block
block
like
like
an
an
element
element
in
in
an
an
array
array
or
or
more
more
accurately,
accurately
a
a
linked
linked
list,
list
because
because
each
each
block
block
has
has
a
a
reference
reference
or
or
link
link
to
to
the
the
previous
previous
block
block
in
in
the
the
chain
chain
with
with
the
the
previous
previous
hash
hash
property.
property
And
and
that
that
brings
brings
up
up
a
a
good
good
cryptographic
cryptographic
question,
question
what
what
is
is
a
a
hash?
hash
A
a
hashing
hashing
function
function
allows
allows
you
you
to
to
take
take
a
a
value
value
of
of
an
an
arbitrary
arbitrary
size,
size
like,
like
say,
say
a
a
transaction,
transaction
then
then
map
map
it
it
see
see
a
a
value
value
with
with
a
a
fixed
fixed
length
length
like
like
a
a
hexadecimal
hexadecimal
string.
string
The
the
value
value
returned
returned
from
from
the
the
hashing
hashing
function
function
is
is
often
often
called
called
a
a
hash
hash
or
or
a
a
hash
hash
digest.
digest
When
when
you
you
create
create
a
a
hash,
hash
it
it
cannot
cannot
be
be
reversed
reversed
to
to
reconstruct
reconstruct
the
the
contents
contents
of
of
the
the
original
original
value,
value
but
but
what
what
you
you
can
can
do
do
is
is
validate
validate
that
that
two
two
values
values
are
are
identical
identical
by
by
comparing
comparing
their
their
hashes.
hashes
And
and
that's
that's
important
important
for
for
a
a
blockchain,
blockchain
because
because
it
it
ensures
ensures
that
that
2
2
blocks
blocks
can
can
be
be
linked
linked
together
together
without
without
being
being
manipulated.
manipulated
Now
now
to
to
create
create
a
a
hash
hash
of
of
a
a
block,
block
we're
we're
going
going
to
to
implement
implement
a
a
getter
getter
that
that
will
will
first
first
stringify
stringify
the
the
object
object
itself.
itself
Then
then
we'll
we'll
use
use
the
the
create
create
hash
hash
function
function
from
from
nodecrypto,
nodecrypto
which
which
specifies
specifies
a
a
specific
specific
hashing
hashing
algorithm.
algorithm
In
in
this
this
case,
case
SHA
sha
256.
256
It
it
stands
stands
for
for
secure
secure
hash
hash
algorithm
algorithm
with
with
a
a
length
length
of
of
256
256
bits.
bits
It
it
was
was
developed
developed
by
by
the
the
NSA
nsa
back
back
in
in
2001,
2001
and
and
it's
it's
what's
what's
known
known
as
as
a
a
one
one
way
way
cryptographic
cryptographic
function,
function
which
which
means
means
that
that
it
it
can
can
encrypt
encrypt
data,
data
but
but
it
it
cannot
cannot
decrypt
decrypt
data
data
back
back
to
to
its
its
original
original
form.
form
We
we
can
can
use
use
the
the
function
function
to
to
hash
hash
the
the
string
string
version
version
of
of
the
the
block,
block
then
then
return
return
the
the
hash
hash
value
value
or
or
digest
digest
as
as
a
a
hex
hex
sub
sub
decimal
decimal
string.
string
And
and
that's
that's
all
all
it
it
takes
takes
to
to
build
build
a
a
block.
block
In
in
this
this
case,
case
it's
it's
a
a
transaction
transaction
that
that
has
has
a
a
link
link
to
to
the
the
previous
previous
transaction
transaction
in
in
the
the
form
form
of
of
a
a
hash,
hash
and
and
there
there
also
also
contains
contains
a
a
time
time
stamp
stamp
because
because
all
all
blocks
blocks
will
will
be
be
placed
placed
in
in
chronological
chronological
order.
order
Now
now
we
we
can
can
move
move
on
on
to
to
the
the
chain,
chain
which
which
again
again
is
is
like
like
a
a
linked
linked
list
list
of
of
blocks.
blocks
There
there
should
should
only
only
be
be
1
1
blockchain,
blockchain
so
so
we'll
we'll
go
go
ahead
ahead
and
and
make
make
it
it
a
a
singleton
singleton
instance
instance
by
by
setting
setting
up
up
a
a
static
static
instance
instance
property
property
that
that
is
is
equal
equal
will
will
to
to
a
a
new
new
Chain
chain
instance.
instance
That'll
that'll
just
just
ensure
ensure
that
that
we
we
have
have
one
one
Chain
chain
instance
instance
instantiated
instantiated
before
before
anything
anything
else.
else
Now
now
we
we
can
can
declare
declare
a
a
property
property
for
for
the
the
chain
chain
itself,
itself
which
which
is
is
just
just
an
an
array
array
of
of
blocks,
blocks
and
and
the
the
constructor
constructor
will
will
define
define
the
the
first
first
block
block
in
in
the
the
chain,
chain
which
which
is
is
called
called
the
the
Genesis
genesis
block.
block
The
the
previous
previous
hash
hash
is
is
null
null
because
because
there's
there's
nothing
nothing
for
for
it
it
to
to
link
link
to,
to
then
then
it
it
instantiates
instantiates
a
a
new
new
transaction
transaction
that
that
transfers
transfers
a
a
100
100
coins
coins
to
to
Satoshi.
satoshi
Notice
notice
how
how
we're
we're
creating
creating
money
money
out
out
of
of
thin
thin
air
air
here.
here
That's
that's
no
no
different
different
than
than
when
when
the
the
central
central
bankers
bankers
or
or
Federal
federal
Reserve
reserve
turn
turn
on
on
the
the
money
money
printer.
printer
Now
now
in
in
the
the
Blockchain
blockchain
class,
class
we'll
we'll
often
often
need
need
to
to
grab
grab
the
the
last
last
block
block
in
in
the
the
chain,
chain
so
so
we'll
we'll
go
go
ahead
ahead
and
and
create
create
a
a
getter
getter
to
to
help
help
us
us
out
out
with
with
that.
that
Then
then
will
will
define
define
a
a
new
new
method
method
named
named
addblock
addblock
that
that
takes
takes
a
a
transaction,
transaction
the
the
sender's
sender's
public
public
key,
key
and
and
a
a
signature
signature
that
that
we
we
can
can
verify
verify
before
before
adding
adding
a
a
new
new
block
block
to
to
the
the
chain.
chain
A
a
naive
naive
and
and
simple
simple
implementation
implementation
might
might
look
look
like
like
this.
this
We
we
instantiate
instantiate
a
a
new
new
block,
block
taking
taking
the
the
last
last
block's
block's
hash
hash
and
and
this
this
new
new
transaction,
transaction
then
then
we
we
push
push
that
that
new
new
block
block
onto
onto
the
the
chain,
chain
but
but
the
the
problem
problem
is
is
that
that
there's
there's
no
no
way
way
to
to
know
know
that
that
this
this
is
is
a
a
legitimate
legitimate
transaction.
transaction
Anybody
anybody
could
could
send
send
arbitrary
arbitrary
transaction
transaction
data
data
to
to
transfer
transfer
coin
coin
to
to
someone
someone
else.
else
We
we
can
can
allow
allow
people
people
to
to
securely
securely
send
send
coin
coin
back
back
and
and
forth
forth
by
by
implementing
implementing
a
a
wallet,
wallet
which
which
is
is
essentially
essentially
just
just
a
a
wrapper
wrapper
for
for
a
a
public
public
key
key
key
key
and
and
a
a
private
private
key.
key
The
the
public
public
key
key
is
is
for
for
receiving
receiving
money,
money
the
the
private
private
key
key
is
is
for
for
spending
spending
money.
money
To
to
generate
generate
a
a
public
public
and
and
private
private
key,
key
we're
we're
going
going
to
to
use
use
a
a
different
different
algorithm
algorithm
called
called
RSA,
rsa
which
which
stands
stands
for
for
the
the
names
names
of
of
the
the
guys
guys
that
that
created
created
it.
it
And
and
unlike
unlike
SHA,
sha
this
this
is
is
a
a
full
full
encryption
encryption
algorithm
algorithm
that
that
can
can
encrypt
encrypt
data
data
and
and
then
then
decrypt
decrypt
it
it
if
if
you
you
have
have
the
the
proper
proper
key
key
to
to
do
do
so.
so
To
to
encrypt
encrypt
a
a
value,
value
you
you
would
would
use
use
the
the
public
public
key
key
to
to
convert
convert
it
it
to
to
ciphertext,
ciphertext
which
which
is
is
an
an
unreadable
unreadable
version
version
of
of
the
the
original
original
value.
value
Then
then
you
you
would
would
use
use
the
the
private
private
key
key
to
to
decrypt
decrypt
it
it
back
back
to
to
its
its
original
original
form.
form
But
but
what
what
we're
we're
actually
actually
more
more
interested
interested
in
in
is
is
using
using
the
the
key
key
pair
pair
to
to
create
create
a
a
digital
digital
signature.
signature
With
with
signing,
signing
we
we
don't
don't
need
need
to
to
encrypt
encrypt
the
the
message,
message
but
but
instead
instead
create
create
a
a
hash
hash
of
of
it.
it
We
we
then
then
sign
sign
the
the
hash
hash
with
with
our
our
private
private
key,
key
then
then
the
the
message
message
can
can
be
be
verified
verified
later
later
using
using
the
the
public
public
key.
key
If
if
anybody
anybody
tried
tried
to
to
change
change
the
the
message,
message
it
it
would
would
produce
produce
a
a
different
different
hash,
hash
in
in
which
which
case
case
the
the
verification
verification
would
would
fail.
fail
And
and
that's
that's
really
really
important
important
for
for
a
a
coin,
coin
Because
because
if
if
we
we
didn't
didn't
have
have
a
a
signature,
signature
then
then
someone
someone
could
could
intercept
intercept
the
the
transaction
transaction
message
message
and
and
change
change
the
the
amount
amount
or
or
change
change
the
the
payee
payee
with
with
no
no
way
way
to
to
detect
detect
that
that
anything
anything
was
was
out
out
of
of
the
the
ordinary.
ordinary
Now
now
Now
now
when
when
generating
generating
the
the
key
key
pair
pair
with
with
Nodecrypto,
nodecrypto
I'm
i'm
going
going
to
to
format
format
them
them
as
as
strings.
strings
And
and
to
to
do
do
that,
that
I'm
i'm
going
going
to
to
add
add
some
some
extra
extra
options
options
here
here
for
for
encoding.
encoding
Scheme,
scheme
the
the
important
important
one
one
to
to
know
know
is
is
the
the
format
format
PEM,
pem
which
which
you
you
would
would
normally
normally
save
save
to
to
a
a
file
file
on
on
the
the
user's
user's
computer
computer
system
system
where
where
it
it
could
could
be
be
reused
reused
in
in
the
the
future.
future
Now
now
that
that
we
we
have
have
a
a
public
public
key
key
and
and
private
private
key,
key
we
we
can
can
use
use
it
it
to
to
send
send
money
money
to
to
another
another
user.
user
The
the
way
way
we
we
do
do
that
that
is
is
by
by
specifying
specifying
an
an
amount
amount
and
and
the
the
public
public
key
key
of
of
the
the
user
user
being
being
paid.
paid
We
we
can
can
then
then
use
use
Nodecrypto
nodecrypto
to
to
create
create
a
a
signature,
signature
again,
again
an
an
SHA
sha
256
256
format,
format
using
using
the
the
transaction
transaction
data
data
as
as
the
the
action
action
data
data
adds
adds
the
the
value.
value
We
we
can
can
then
then
create
create
a
a
signature
signature
by
by
signing
signing
it
it
with
with
the
the
private
private
key.
key
This
this
is
is
kind
kind
of
of
like
like
creating
creating
a
a
one
one
time
time
password.
password
It
it
allows
allows
us
us
to
to
verify
verify
our
our
identity
identity
with
with
the
the
private
private
key
key
without
without
actually
actually
having
having
to
to
expose
expose
the
the
private
private
key.
key
The
the
signature
signature
depends
depends
on
on
both
both
the
the
transaction
transaction
data
data
and
and
the
the
private
private
key,
key
but
but
it
it
can
can
be
be
verified
verified
as
as
authentic
authentic
using
using
the
the
public
public
key.
key
We
we
can
can
now
now
attempt
attempt
to
to
add
add
that
that
block
block
to
to
the
the
blockchain
blockchain
by
by
passing
passing
the
the
transaction,
transaction
the
the
public
public
key,
key
and
and
the
the
signature.
signature
In
in
real
real
life,
life
those
those
values
values
will
will
be
be
transferred
transferred
over
over
the
the
Internet,
internet
then
then
the
the
signature
signature
could
could
be
be
verified.
verified
Let's
let's
go
go
ahead
ahead
and
and
refactor
refactor
the
the
add
add
block
block
method
method
in
in
the
the
chain
chain
class.
class
It'll
it'll
use
use
Nodecrypto
nodecrypto
to
to
create
create
a
a
signature
signature
verification,
verification
then
then
pass
pass
the
the
same
same
transaction
transaction
data
data
to
to
the
the
verifier.
verifier
We
we
can
can
then
then
validate
validate
it
it
by
by
verifying
verifying
that
that
the
the
transaction
transaction
data
data
has
has
not
not
tampered
tampered
with
with
using
using
the
the
sender's
sender's
public
public
key
key
and
and
the
the
signature
signature
itself,
itself
and
and
we're
we're
now
now
able
able
to
to
securely
securely
verify
verify
that
that
that
that
user
user
is
is
actually
actually
are
are
trying
trying
to
to
spend
spend
that
that
amount
amount
of
of
money
money
to
to
the
the
other
other
user.
user
But
but
there's
there's
still
still
one
one
more
more
big
big
issue
issue
with
with
our
our
Blockchain,
blockchain
and
and
that
that
is
is
the
the
double
double
spend
spend
issue.
issue
Imagine
imagine
the
the
spender
spender
tried
tried
to
to
send
send
money
money
to
to
2
2
different
different
users
users
at
at
the
the
same
same
time.
time
They
they
could
could
potentially
potentially
spend
spend
more
more
money
money
than
than
they
they
actually
actually
own
own
before
before
their
their
transaction
transaction
is
is
confirmed
confirmed
on
on
the
the
blockchain.
blockchain
The
the
way
way
Bitcoin
bitcoin
addresses
addresses
that
that
issue
issue
is
is
with
with
a
a
proof
proof
of
of
work
work
system,
system
which
which
requires
requires
each
each
new
new
block
block
to
to
go
go
through
through
a
a
process
process
called
called
mining,
mining
where
where
a
a
difficult
difficult
computational
computational
problem
problem
is
is
solved
solved
in
in
order
order
to
to
confirm
confirm
the
the
block,
block
but
but
it's
it's
very
very
easy
easy
to
to
verify
verify
that
that
work
work
by
by
multiple
multiple
other
other
nodes
nodes
on
on
the
the
system.
system
When
when
mining
mining
is
is
distributed
distributed
around
around
the
the
world.
world
It
it
means
means
you
you
have
have
multiple
multiple
nodes
nodes
competing
competing
to
to
confirm
confirm
a
a
block
block
on
on
the
the
blockchain
blockchain
and
and
works
works
like
like
a
a
big
big
lottery.
lottery
The
the
winner
winner
of
of
the
the
lottery
lottery
earns
earns
a
a
portion
portion
of
of
the
the
coin
coin
as
as
has
has
incentive,
incentive
which
which
motivates
motivates
people
people
to
to
invest
invest
in
in
the
the
computing
computing
resources
resources
to
to
mine
mine
a
a
coin,
coin
and
and
also
also
to
to
pump
pump
the
the
price
price
of
of
the
the
coin
coin
higher
higher
and
and
higher
higher
because
because
the
the
higher
higher
the
the
price,
price
the
the
more
more
money
money
you
you
make.
make
You
you
can
can
think
think
of
of
it
it
like
like
converting
converting
cloud
cloud
computing
computing
resources
resources
into
into
money.
money
Now
now
to
to
implement
implement
a
a
basic
basic
proof
proof
of
of
work
work
system,
system
we'll
we'll
go
go
back
back
into
into
our
our
block
block
class
class
and
and
add
add
a
a
nonce
nonce
value,
value
which
which
is
is
a
a
one
one
time
time
use
use
random
random
number.
number
Then,
then
in
in
the
the
chain
chain
class,
class
we'll
we'll
add
add
a
a
method
method
called
called
mine
mine
that
that
takes
takes
that
that
nonce
nonce
as
as
an
an
argument.
argument
What
what
this
this
method
method
will
will
do
do
is
is
attempt
attempt
to
to
find
find
a
a
number
number
that
that
when
when
added
added
to
to
the
the
nonce
nonce
will
will
produce
produce
a
a
hash
hash
that
that
starts
starts
with
with
4
4
zeros.
zeros
The
the
only
only
real
real
way
way
to
to
figure
figure
out
out
that
that
value
value
is
is
with
with
brute
brute
force
force
by
by
creating
creating
a
a
while
while
loop
loop
that
that
goes
goes
digit
digit
by
by
digit
digit
until
until
we
we
find
find
the
the
requested
requested
value.
value
To
to
handle
handle
the
the
brute
brute
force
force
computation,
computation
we'll
we'll
will
will
create
create
a
a
while
while
loop
loop
that
that
creates
creates
a
a
hash
hash
with
with
the
the
MD
md
5
5
algorithm.
algorithm
It's
it's
very
very
similar
similar
to
to
SHA
sha
256,
256
but
but
is
is
only
only
a
a
128
128
bits
bits
and
and
is
is
faster
faster
to
to
compute.
compute
We'll
we'll
continue
continue
to
to
create
create
new
new
hashes
hashes
inside
inside
the
the
while
while
loop
loop
until
until
we
we
find
find
one
one
that
that
starts
starts
with
with
4
4
zeros.
zeros
When
when
we
we
find
find
it,
it
we
we
can
can
return
return
the
the
solution
solution
and
and
then
then
send
send
it
it
off
off
to
to
other
other
nodes
nodes
where
where
it
it
can
can
be
be
verified,
verified
and
and
the
the
block
block
can
can
finally
finally
be
be
confirmed
confirmed
on
on
the
the
blockchain.
blockchain
And
and
and
and
that
that
takes
takes
care
care
of
of
our
our
blockchain
blockchain
implementation.
implementation
If
if
we
we
go
go
down
down
to
to
the
the
example
example
usage
usage
here,
here
you
you
can
can
see
see
we
we
instantiate
instantiate
a
a
few
few
wallets
wallets
for
for
different
different
users,
users
then
then
allow
allow
allow
allow
those
those
users
users
to
to
send
send
money
money
to
to
each
each
other
other
using
using
their
their
various
various
public
public
keys.
keys
If
if
we
we
go
go
ahead
ahead
and
and
log
log
out
out
the
the
chain
chain
itself,
itself
you
you
can
can
see
see
we
we
have
have
a
a
bunch
bunch
of
of
blocks
blocks
or
or
transactions
transactions
link
link
to
to
each
each
other
other
based
based
on
on
a
a
hash
hash
of
of
the
the
previous
previous
block.
block
Now
now
this
this
blockchain
blockchain
is
is
obviously
obviously
not
not
perfect
perfect
and
and
something
something
that
that
I
i
just
just
hacked
hacked
together
together
to
to
teach
teach
you
you
the
the
gist
gist
of
of
how
how
a
a
blockchain
blockchain
works.
works
Feel
feel
free
free
to
to
grab
grab
the
the
full
full
source
source
code
code
on
on
GitHub.
github
If
if
you
you
learned
learned
something
something
in
in
the
the
process,
process
please
please
make
make
sure
sure
to
to
like
like
and
and
subscribe
subscribe
and
and
consider
consider
sending
sending
me
me
some
some
Bitcoin
bitcoin
or
or
sponsoring
sponsoring
me
me
on
on
hub
hub
for
for
just
just
$1
$1
of
of
fiat
fiat
per
per
month.
month
I
i
use
use
all
all
that
that
income
income
to
to
sponsor
sponsor
other
other
developers
developers
myself.
myself
Thanks
thanks
for
for
watching,
watching
and
and
I
i
will
will
see
see
you
you
in
in
the
the
next
next
one.
one
success
The blockchain is designed to create a chain with a public and private key to ensure proper transfer of money and is designed to handle multiple transactions. It is created using nothing but liquor and is designed to handle multiple transactions and create a blockchain from scratch using nothing but liquor. The speaker explains the process of creating a blockchain and setting up a static instance to ensure there is only one blockchain. They also discuss the implementation of a wallet and the use of a proof of work system to ensure the blockchain is working properly. The blockchain is created using a static instance to ensure the blockchain is working properly and the nonce value is used to ensure the blockchain is working properly.
Bitcoin ₿ in 100 Seconds // Build your Own Blockchain
Bitcoin, a peer to peer electronic cash system first described in a 2008 white paper authored by the mysterious Satoshi Nakamoto. The modern financial system relies on our trust of big centralized banks to hold our fiat currencies and execute transactions, but trust is a weakness that eventually spire's intervention by lawyers and government. Bitcoin allows 2 parties to make reliable transactions based on cryptographic proof, eliminating the need for a trusty middleman. The optimists call it digital gold, the pessimists call it fool's gold, but in reality, it's just software. And like all software, its purpose is to arrange change ones and zeros in a meaningful way. The protocol that makes it meaningful is Blockchain, which allows 2 parties to engage in transactions denominated and Bitcoins or Satoshis, and just like dollars and cents, they have value because we believe they do. From a financial perspective, the Blockchain scene is like a shared public ledger that contains all transactions from all Bitcoin users and is distributed and synchronized around the world, which eliminates the need for a central authority to maintain and validate it. From a technical perspective, think of the Blockchain as a database structured as a linked list, where each record or block represents a group of transactions that have been permanently committed to the database. It works kind of like a Git repo that can never be rebased. The important thing is that each new block is linked to the previous one in the Blockchain, and its creation goes through a very strict set of cryptographic rules. Each user or wallet has a unique public key for receiving money, kind of like username and a unique private key for spending money, kind of like a password. But before you can spend money, you'll need to prove that you're the owner of a public key that money has been sent to in the past. Each transaction contains a hash or encrypted representation of the previous transaction and the new owner's public key. The hash is then signed with the previous owner's private key. This makes it possible to validate the chain of ownership without have the need to expose the private key, and the signature makes it virtually impossible to alter the transaction after it's been issued. But what if somebody tries you pay 2 different people with Bitcoin at the exact same time or double spend their money. That's where mining comes in, which is a system that allows multiple computers around the world to agree on the appropriate state of the entire system or ledger. Each new transaction is broadcast to all nodes in the network. The transactions are packaged into a block, then miners will expend computing power to validate proof of work. They compute a proof for a random problem that is very difficult to solve, but very easy to verify. The first miner to solve the proof, which happens via dumb luck, gets a portion of the Bitcoin as a reward. The block is then broadcast back to all other nodes where it's permanently confirmed on the blockchain. This has been Bitcoin in 100 seconds. If you wanna more short videos like this, make sure to like and subscribe. And if you really wanna learn how blockchain technology works, stay tuned, because today we're going beyond 100 seconds 2 build a blockchain from scratch with Node. Js and TypeScript, and in the process, you'll learn all kinds of useful information about cryptography. Whether you're into cryptocurrency is not. There's many concepts in cryptography that you should know about as a developer. Things like encryption and signing, hashing functions, and algorithms like SHA, RSA, and MD 5 that make all this stuff possible. Over the next few minutes, you'll learn about these concepts by implementing your own Blockchain from scratch using nothing but Node. Js and TypeScript. Our blockchain will contain wallets where users can send money back and forth. Each transaction is added into block, then that block is mined with a proof of work system, at which point it can be added and confirmed on the blockchain. And throughout this process, you'll learn about all kinds of important concepts and how they apply to different areas of development. To get started, you'll need Node. Js installed on your system then will open up Versus Code to an empty directory, run npm init y to create a new Node. Js project. I'll be using TypeScript in this code because we'll be using object oriented programming principles to implement the blockchain. And having types will just make our code a bit more readable. School, you can install it with npm, then create a tsconfig file, and copy and paste the values from the main source code. From there, we can go into our package JSON and create a script called dev that runs the TypeScript compiler with the watch flag to constantly compile our code in the background to plain JavaScript. From there, you should be able to run and NPM run dev from the command line to keep TypeScript running in the background. Now we'll write all of our source code in the index CS file. At the top of that file, we will import the crypto library, which is a built in in Node. Js. As the name implies, it's a module at Node that handles a bunch of different functionality for cryptography. Our simple Blockchain implementation here has 4 classes, a transaction, a block, a chain, and a wallet. Let's start by implementing the transaction, which is the fundamental purpose of any cryptocurrency, transfer funds from one user to another user in a transaction. A transaction object has 3 properties, the amount of the transaction denominated and Bitcoins or Fire Coins or whatever you wanna call your coin, along with the person paying the money and the person receiving the money. Eventually, we'll set up public keys for these users, which which you can think of as a username that will eventually set as these values in the transaction. One final thing we'll do here is add a method to the class to convert the object to a string. As we go through the tutorial, we're going to serialize everything as strings, just to make the cryptographic objects easier to work with. And now we're ready to move on to the block implementation. A block is like a container for multiple transactions, or in are case just a single transaction to keep things simple. You can think of a block like an element in an array or more accurately, a linked list, because each block has a reference or link to the previous block in the chain with the previous hash property. And that brings up a good cryptographic question, what is a hash? A hashing function allows you to take a value of an arbitrary size, like, say, a transaction, then map it see a value with a fixed length like a hexadecimal string. The value returned from the hashing function is often called a hash or a hash digest. When you create a hash, it cannot be reversed to reconstruct the contents of the original value, but what you can do is validate that two values are identical by comparing their hashes. And that's important for a blockchain, because it ensures that 2 blocks can be linked together without being manipulated. Now to create a hash of a block, we're going to implement a getter that will first stringify the object itself. Then we'll use the create hash function from nodecrypto, which specifies a specific hashing algorithm. In this case, SHA 256. It stands for secure hash algorithm with a length of 256 bits. It was developed by the NSA back in 2001, and it's what's known as a one way cryptographic function, which means that it can encrypt data, but it cannot decrypt data back to its original form. We can use the function to hash the string version of the block, then return the hash value or digest as a hex sub decimal string. And that's all it takes to build a block. In this case, it's a transaction that has a link to the previous transaction in the form of a hash, and there also contains a time stamp because all blocks will be placed in chronological order. Now we can move on to the chain, which again is like a linked list of blocks. There should only be 1 blockchain, so we'll go ahead and make it a singleton instance by setting up a static instance property that is equal will to a new Chain instance. That'll just ensure that we have one Chain instance instantiated before anything else. Now we can declare a property for the chain itself, which is just an array of blocks, and the constructor will define the first block in the chain, which is called the Genesis block. The previous hash is null because there's nothing for it to link to, then it instantiates a new transaction that transfers a 100 coins to Satoshi. Notice how we're creating money out of thin air here. That's no different than when the central bankers or Federal Reserve turn on the money printer. Now in the Blockchain class, we'll often need to grab the last block in the chain, so we'll go ahead and create a getter to help us out with that. Then will define a new method named addblock that takes a transaction, the sender's public key, and a signature that we can verify before adding a new block to the chain. A naive and simple implementation might look like this. We instantiate a new block, taking the last block's hash and this new transaction, then we push that new block onto the chain, but the problem is that there's no way to know that this is a legitimate transaction. Anybody could send arbitrary transaction data to transfer coin to someone else. We can allow people to securely send coin back and forth by implementing a wallet, which is essentially just a wrapper for a public key key and a private key. The public key is for receiving money, the private key is for spending money. To generate a public and private key, we're going to use a different algorithm called RSA, which stands for the names of the guys that created it. And unlike SHA, this is a full encryption algorithm that can encrypt data and then decrypt it if you have the proper key to do so. To encrypt a value, you would use the public key to convert it to ciphertext, which is an unreadable version of the original value. Then you would use the private key to decrypt it back to its original form. But what we're actually more interested in is using the key pair to create a digital signature. With signing, we don't need to encrypt the message, but instead create a hash of it. We then sign the hash with our private key, then the message can be verified later using the public key. If anybody tried to change the message, it would produce a different hash, in which case the verification would fail. And that's really important for a coin, Because if we didn't have a signature, then someone could intercept the transaction message and change the amount or change the payee with no way to detect that anything was out of the ordinary. Now Now when generating the key pair with Nodecrypto, I'm going to format them as strings. And to do that, I'm going to add some extra options here for encoding. Scheme, the important one to know is the format PEM, which you would normally save to a file on the user's computer system where it could be reused in the future. Now that we have a public key and private key, we can use it to send money to another user. The way we do that is by specifying an amount and the public key of the user being paid. We can then use Nodecrypto to create a signature, again, an SHA 256 format, using the transaction data as the action data adds the value. We can then create a signature by signing it with the private key. This is kind of like creating a one time password. It allows us to verify our identity with the private key without actually having to expose the private key. The signature depends on both the transaction data and the private key, but it can be verified as authentic using the public key. We can now attempt to add that block to the blockchain by passing the transaction, the public key, and the signature. In real life, those values will be transferred over the Internet, then the signature could be verified. Let's go ahead and refactor the add block method in the chain class. It'll use Nodecrypto to create a signature verification, then pass the same transaction data to the verifier. We can then validate it by verifying that the transaction data has not tampered with using the sender's public key and the signature itself, and we're now able to securely verify that that user is actually are trying to spend that amount of money to the other user. But there's still one more big issue with our Blockchain, and that is the double spend issue. Imagine the spender tried to send money to 2 different users at the same time. They could potentially spend more money than they actually own before their transaction is confirmed on the blockchain. The way Bitcoin addresses that issue is with a proof of work system, which requires each new block to go through a process called mining, where a difficult computational problem is solved in order to confirm the block, but it's very easy to verify that work by multiple other nodes on the system. When mining is distributed around the world. It means you have multiple nodes competing to confirm a block on the blockchain and works like a big lottery. The winner of the lottery earns a portion of the coin as has incentive, which motivates people to invest in the computing resources to mine a coin, and also to pump the price of the coin higher and higher because the higher the price, the more money you make. You can think of it like converting cloud computing resources into money. Now to implement a basic proof of work system, we'll go back into our block class and add a nonce value, which is a one time use random number. Then, in the chain class, we'll add a method called mine that takes that nonce as an argument. What this method will do is attempt to find a number that when added to the nonce will produce a hash that starts with 4 zeros. The only real way to figure out that value is with brute force by creating a while loop that goes digit by digit until we find the requested value. To handle the brute force computation, we'll will create a while loop that creates a hash with the MD 5 algorithm. It's very similar to SHA 256, but is only a 128 bits and is faster to compute. We'll continue to create new hashes inside the while loop until we find one that starts with 4 zeros. When we find it, we can return the solution and then send it off to other nodes where it can be verified, and the block can finally be confirmed on the blockchain. And and that takes care of our blockchain implementation. If we go down to the example usage here, you can see we instantiate a few wallets for different users, then allow allow those users to send money to each other using their various public keys. If we go ahead and log out the chain itself, you can see we have a bunch of blocks or transactions link to each other based on a hash of the previous block. Now this blockchain is obviously not perfect and something that I just hacked together to teach you the gist of how a blockchain works. Feel free to grab the full source code on GitHub. If you learned something in the process, please make sure to like and subscribe and consider sending me some Bitcoin or sponsoring me on hub for just $1 of fiat per month. I use all that income to sponsor other developers myself. Thanks for watching, and I will see you in the next one.
The blockchain is designed to create a chain with a public and private key to ensure proper transfer of money and is designed to handle multiple transactions. It is created using nothing but liquor and is designed to handle multiple transactions and create a blockchain from scratch using nothing but liquor. The speaker explains the process of creating a blockchain and setting up a static instance to ensure there is only one blockchain. They also discuss the implementation of a wallet and the use of a proof of work system to ensure the blockchain is working properly. The blockchain is created using a static instance to ensure the blockchain is working properly and the nonce value is used to ensure the blockchain is working properly.

        [INST]
        You are youtuber called Fireship you make engaging high-intensity and entertaining coding tutorials and tech news. 
        you covers a wide range of topics relevant to programmers, aiming to help them learn and improve their skills quickly.
        
        Given the title of the video : Bitcoin ₿ in 100 Seconds // Build your Own Blockchain 
        and a small summary : The blockchain is designed to create a chain with a public and private key to ensure proper transfer of money and is designed to handle multiple transactions. It is created using nothing but liquor and is designed to handle multiple transactions and create a blockchain from scratch using nothing but liquor. The speaker explains the process of creating a blockchain and setting up a static instance to ensure there is only one blockchain. They also discuss the implementation of a wallet and the use of a proof of work system to ensure the blockchain is working properly. The blockchain is created using a static instance to ensure the blockchain is working properly and the nonce value is used to ensure the blockchain is working properly.
        [/INST]
        
        Generate the video : Bitcoin, a peer to peer electronic cash system first described in a 2008 white paper authored by the mysterious Satoshi Nakamoto. The modern financial system relies on our trust of big centralized banks to hold our fiat currencies and execute transactions, but trust is a weakness that eventually spire's intervention by lawyers and government. Bitcoin allows 2 parties to make reliable transactions based on cryptographic proof, eliminating the need for a trusty middleman. The optimists call it digital gold, the pessimists call it fool's gold, but in reality, it's just software. And like all software, its purpose is to arrange change ones and zeros in a meaningful way. The protocol that makes it meaningful is Blockchain, which allows 2 parties to engage in transactions denominated and Bitcoins or Satoshis, and just like dollars and cents, they have value because we believe they do. From a financial perspective, the Blockchain scene is like a shared public ledger that contains all transactions from all Bitcoin users and is distributed and synchronized around the world, which eliminates the need for a central authority to maintain and validate it. From a technical perspective, think of the Blockchain as a database structured as a linked list, where each record or block represents a group of transactions that have been permanently committed to the database. It works kind of like a Git repo that can never be rebased. The important thing is that each new block is linked to the previous one in the Blockchain, and its creation goes through a very strict set of cryptographic rules. Each user or wallet has a unique public key for receiving money, kind of like username and a unique private key for spending money, kind of like a password. But before you can spend money, you'll need to prove that you're the owner of a public key that money has been sent to in the past. Each transaction contains a hash or encrypted representation of the previous transaction and the new owner's public key. The hash is then signed with the previous owner's private key. This makes it possible to validate the chain of ownership without have the need to expose the private key, and the signature makes it virtually impossible to alter the transaction after it's been issued. But what if somebody tries you pay 2 different people with Bitcoin at the exact same time or double spend their money. That's where mining comes in, which is a system that allows multiple computers around the world to agree on the appropriate state of the entire system or ledger. Each new transaction is broadcast to all nodes in the network. The transactions are packaged into a block, then miners will expend computing power to validate proof of work. They compute a proof for a random problem that is very difficult to solve, but very easy to verify. The first miner to solve the proof, which happens via dumb luck, gets a portion of the Bitcoin as a reward. The block is then broadcast back to all other nodes where it's permanently confirmed on the blockchain. This has been Bitcoin in 100 seconds. If you wanna more short videos like this, make sure to like and subscribe. And if you really wanna learn how blockchain technology works, stay tuned, because today we're going beyond 100 seconds 2 build a blockchain from scratch with Node. Js and TypeScript, and in the process, you'll learn all kinds of useful information about cryptography. Whether you're into cryptocurrency is not. There's many concepts in cryptography that you should know about as a developer. Things like encryption and signing, hashing functions, and algorithms like SHA, RSA, and MD 5 that make all this stuff possible. Over the next few minutes, you'll learn about these concepts by implementing your own Blockchain from scratch using nothing but Node. Js and TypeScript. Our blockchain will contain wallets where users can send money back and forth. Each transaction is added into block, then that block is mined with a proof of work system, at which point it can be added and confirmed on the blockchain. And throughout this process, you'll learn about all kinds of important concepts and how they apply to different areas of development. To get started, you'll need Node. Js installed on your system then will open up Versus Code to an empty directory, run npm init y to create a new Node. Js project. I'll be using TypeScript in this code because we'll be using object oriented programming principles to implement the blockchain. And having types will just make our code a bit more readable. School, you can install it with npm, then create a tsconfig file, and copy and paste the values from the main source code. From there, we can go into our package JSON and create a script called dev that runs the TypeScript compiler with the watch flag to constantly compile our code in the background to plain JavaScript. From there, you should be able to run and NPM run dev from the command line to keep TypeScript running in the background. Now we'll write all of our source code in the index CS file. At the top of that file, we will import the crypto library, which is a built in in Node. Js. As the name implies, it's a module at Node that handles a bunch of different functionality for cryptography. Our simple Blockchain implementation here has 4 classes, a transaction, a block, a chain, and a wallet. Let's start by implementing the transaction, which is the fundamental purpose of any cryptocurrency, transfer funds from one user to another user in a transaction. A transaction object has 3 properties, the amount of the transaction denominated and Bitcoins or Fire Coins or whatever you wanna call your coin, along with the person paying the money and the person receiving the money. Eventually, we'll set up public keys for these users, which which you can think of as a username that will eventually set as these values in the transaction. One final thing we'll do here is add a method to the class to convert the object to a string. As we go through the tutorial, we're going to serialize everything as strings, just to make the cryptographic objects easier to work with. And now we're ready to move on to the block implementation. A block is like a container for multiple transactions, or in are case just a single transaction to keep things simple. You can think of a block like an element in an array or more accurately, a linked list, because each block has a reference or link to the previous block in the chain with the previous hash property. And that brings up a good cryptographic question, what is a hash? A hashing function allows you to take a value of an arbitrary size, like, say, a transaction, then map it see a value with a fixed length like a hexadecimal string. The value returned from the hashing function is often called a hash or a hash digest. When you create a hash, it cannot be reversed to reconstruct the contents of the original value, but what you can do is validate that two values are identical by comparing their hashes. And that's important for a blockchain, because it ensures that 2 blocks can be linked together without being manipulated. Now to create a hash of a block, we're going to implement a getter that will first stringify the object itself. Then we'll use the create hash function from nodecrypto, which specifies a specific hashing algorithm. In this case, SHA 256. It stands for secure hash algorithm with a length of 256 bits. It was developed by the NSA back in 2001, and it's what's known as a one way cryptographic function, which means that it can encrypt data, but it cannot decrypt data back to its original form. We can use the function to hash the string version of the block, then return the hash value or digest as a hex sub decimal string. And that's all it takes to build a block. In this case, it's a transaction that has a link to the previous transaction in the form of a hash, and there also contains a time stamp because all blocks will be placed in chronological order. Now we can move on to the chain, which again is like a linked list of blocks. There should only be 1 blockchain, so we'll go ahead and make it a singleton instance by setting up a static instance property that is equal will to a new Chain instance. That'll just ensure that we have one Chain instance instantiated before anything else. Now we can declare a property for the chain itself, which is just an array of blocks, and the constructor will define the first block in the chain, which is called the Genesis block. The previous hash is null because there's nothing for it to link to, then it instantiates a new transaction that transfers a 100 coins to Satoshi. Notice how we're creating money out of thin air here. That's no different than when the central bankers or Federal Reserve turn on the money printer. Now in the Blockchain class, we'll often need to grab the last block in the chain, so we'll go ahead and create a getter to help us out with that. Then will define a new method named addblock that takes a transaction, the sender's public key, and a signature that we can verify before adding a new block to the chain. A naive and simple implementation might look like this. We instantiate a new block, taking the last block's hash and this new transaction, then we push that new block onto the chain, but the problem is that there's no way to know that this is a legitimate transaction. Anybody could send arbitrary transaction data to transfer coin to someone else. We can allow people to securely send coin back and forth by implementing a wallet, which is essentially just a wrapper for a public key key and a private key. The public key is for receiving money, the private key is for spending money. To generate a public and private key, we're going to use a different algorithm called RSA, which stands for the names of the guys that created it. And unlike SHA, this is a full encryption algorithm that can encrypt data and then decrypt it if you have the proper key to do so. To encrypt a value, you would use the public key to convert it to ciphertext, which is an unreadable version of the original value. Then you would use the private key to decrypt it back to its original form. But what we're actually more interested in is using the key pair to create a digital signature. With signing, we don't need to encrypt the message, but instead create a hash of it. We then sign the hash with our private key, then the message can be verified later using the public key. If anybody tried to change the message, it would produce a different hash, in which case the verification would fail. And that's really important for a coin, Because if we didn't have a signature, then someone could intercept the transaction message and change the amount or change the payee with no way to detect that anything was out of the ordinary. Now Now when generating the key pair with Nodecrypto, I'm going to format them as strings. And to do that, I'm going to add some extra options here for encoding. Scheme, the important one to know is the format PEM, which you would normally save to a file on the user's computer system where it could be reused in the future. Now that we have a public key and private key, we can use it to send money to another user. The way we do that is by specifying an amount and the public key of the user being paid. We can then use Nodecrypto to create a signature, again, an SHA 256 format, using the transaction data as the action data adds the value. We can then create a signature by signing it with the private key. This is kind of like creating a one time password. It allows us to verify our identity with the private key without actually having to expose the private key. The signature depends on both the transaction data and the private key, but it can be verified as authentic using the public key. We can now attempt to add that block to the blockchain by passing the transaction, the public key, and the signature. In real life, those values will be transferred over the Internet, then the signature could be verified. Let's go ahead and refactor the add block method in the chain class. It'll use Nodecrypto to create a signature verification, then pass the same transaction data to the verifier. We can then validate it by verifying that the transaction data has not tampered with using the sender's public key and the signature itself, and we're now able to securely verify that that user is actually are trying to spend that amount of money to the other user. But there's still one more big issue with our Blockchain, and that is the double spend issue. Imagine the spender tried to send money to 2 different users at the same time. They could potentially spend more money than they actually own before their transaction is confirmed on the blockchain. The way Bitcoin addresses that issue is with a proof of work system, which requires each new block to go through a process called mining, where a difficult computational problem is solved in order to confirm the block, but it's very easy to verify that work by multiple other nodes on the system. When mining is distributed around the world. It means you have multiple nodes competing to confirm a block on the blockchain and works like a big lottery. The winner of the lottery earns a portion of the coin as has incentive, which motivates people to invest in the computing resources to mine a coin, and also to pump the price of the coin higher and higher because the higher the price, the more money you make. You can think of it like converting cloud computing resources into money. Now to implement a basic proof of work system, we'll go back into our block class and add a nonce value, which is a one time use random number. Then, in the chain class, we'll add a method called mine that takes that nonce as an argument. What this method will do is attempt to find a number that when added to the nonce will produce a hash that starts with 4 zeros. The only real way to figure out that value is with brute force by creating a while loop that goes digit by digit until we find the requested value. To handle the brute force computation, we'll will create a while loop that creates a hash with the MD 5 algorithm. It's very similar to SHA 256, but is only a 128 bits and is faster to compute. We'll continue to create new hashes inside the while loop until we find one that starts with 4 zeros. When we find it, we can return the solution and then send it off to other nodes where it can be verified, and the block can finally be confirmed on the blockchain. And and that takes care of our blockchain implementation. If we go down to the example usage here, you can see we instantiate a few wallets for different users, then allow allow those users to send money to each other using their various public keys. If we go ahead and log out the chain itself, you can see we have a bunch of blocks or transactions link to each other based on a hash of the previous block. Now this blockchain is obviously not perfect and something that I just hacked together to teach you the gist of how a blockchain works. Feel free to grab the full source code on GitHub. If you learned something in the process, please make sure to like and subscribe and consider sending me some Bitcoin or sponsoring me on hub for just $1 of fiat per month. I use all that income to sponsor other developers myself. Thanks for watching, and I will see you in the next one.
        