<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en"><generator uri="https://jekyllrb.com/" version="3.5.2">Jekyll</generator><link href="https://www.learningblockchains.com/feed.xml" rel="self" type="application/atom+xml" /><link href="https://www.learningblockchains.com/" rel="alternate" type="text/html" hreflang="en" /><updated>2018-09-20T13:58:08+10:00</updated><id>https://www.learningblockchains.com/</id><title type="html">LearningBlockchains.com</title><subtitle>Understand bitcoin, cryptocurrencies, blockchains and the future of money </subtitle><author><name>dataPipeline</name></author><entry><title type="html">Blockchain verification and validation</title><link href="https://www.learningblockchains.com/lessons/blockchain-verification-validation/" rel="alternate" type="text/html" title="Blockchain verification and validation" /><published>2018-01-25T00:00:00+11:00</published><updated>2018-01-25T00:00:00+11:00</updated><id>https://www.learningblockchains.com/lessons/blockchain-verification-validation</id><content type="html" xml:base="https://www.learningblockchains.com/lessons/blockchain-verification-validation/">&lt;h1 id=&quot;heading-blockchain-validation&quot;&gt;Blockchain Validation&lt;/h1&gt;

&lt;p&gt;&lt;img style=&quot;width: 400px;&quot; src=&quot;/assets/images/verify.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Blockchain networks can be categorised by the way that new coins are distributed within the network. These different categories are commonly known as proofs. There are many types of proofs and in this lesson, I will go over each one and explain their differences.&lt;/p&gt;

&lt;h1 id=&quot;heading-proof-of-work&quot;&gt;Proof Of Work&lt;/h1&gt;

&lt;p&gt;Anyone on a blockchain that uses proof of work can become what is called a node. Or more commonly known as a miner. These people all keep a copy of the blockchain on their computers. By doing this they help secure the blockchain by having a copy of the ledger. This means that there is hundreds or thousands of people with this ledger, meaning that not one person has it and is in full control of it.&lt;/p&gt;

&lt;p&gt;&lt;img style=&quot;width: 400px;&quot; src=&quot;/assets/images/mining.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;b&gt;accreditation:&lt;/b&gt; &lt;a href=&quot;https://www.flickr.com/photos/159526894@N02/with/24357432498/&quot;&gt;Descryptive.com &lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Proof of work is where these miners are given a hard mathematical problem for their computer to solve. This problem is generating a specific SHA-252 hash.
 Each block in the blockchain has a block number with a hash associated with it. No one knows what this hash is, So all of the miners are in a sort of race to solve the number,
  every time a new hash is created it is compared to the block and is either rejected or verified as the correct hash.
  When a miner verifies the correct has the are rewarded with a certain amount of coins from solving that blocks math problem.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Anyone can be a node/miner&lt;/li&gt;
  &lt;li&gt;This stops the ability to perform DDOS attacks as it requires some sort of work for any information to be pushed to the blockchain&lt;/li&gt;
  &lt;li&gt;Everyone who is a node/miner contains a ledger of the blockahin, meaning there is no way to shut it down.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Solving these mathematical problems require an insane amount of power. (Currently Bitcoin mining is using more or close to the amount of power used to run New Zealand)&lt;/li&gt;
  &lt;li&gt;Proof of work in it's current state is unsustainable&lt;/li&gt;
  &lt;li&gt;Proof of work requires Specific ASCII computers to get any real result, Which consume lots of power and are quite expensive to buy.&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;heading-proof-of-stake&quot;&gt;Proof Of Stake&lt;/h1&gt;

&lt;p&gt;Proof of stake is a bit different to proof of work, instead of having miners solving a mathematical problem, people who want to be a node will have to stake a portion of their own money. Then over a period of time blocks are mined as part of the blockchain code, releasing a number of tokens into the system. The people who have staked their coins are then rewarded a portion of the blocks tokens. These nodes still work in the same way as a miner,
 as in they still approve or disapprove a transaction. However, if they do something within the network that a majority of people do not like, They can lose their stake. Which can be a lot of money.
&lt;img style=&quot;width: 300px; float: left; margin: 20px;&quot; src=&quot;/assets/images/stake.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Proof of stake doesnt require any computer mining meaning no huge power cost.&lt;/li&gt;
  &lt;li&gt;Nodes have to use their own money, which insures they will do right by the network&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Disadvantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Whilst this is an advantage as well, It is also a bit nerve racking to stake all of your money into the blockchain.&lt;/li&gt;
  &lt;li&gt;Anyone can be a node meaning the reward pool is smaller.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;&lt;/p&gt;

&lt;h1 id=&quot;heading-delegated-proof-of-stake&quot;&gt;Delegated Proof Of Stake&lt;/h1&gt;

&lt;p&gt;Delegated proof of stake is similar to proof of stake, the difference here is only 21 people can run the network. If you want to become a delegate there are websites in which you need to write up what is essentially a resume.
 Explaining what you will do for the network and how you will help it to grow. Then the community vote for the delegates they want to see run the network.
 In turn. if someone wants to change the network it must be put forward to the other delegates and then depending on the blockchain,
 a % of delegates must agree in a consensus for the change to go ahead. For example in Steem the delegates are called witness's,
 It requires 15 out of the 21 witness to agree to make a change to the network.&lt;/p&gt;

&lt;p&gt;&lt;img style=&quot;width: 200px;&quot; src=&quot;/assets/images/delegate.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;If a delegate starts doing things people do not like or want to change the way things work on their own, people will simply vote them out and then vote in some new people to run the network.
This means it is still completely decentralised and fully in the people's control.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Advantages:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Full voting system meaning that the people put who they want in charge.&lt;/li&gt;
  &lt;li&gt;Doesnt require computer calculations, so there is no power costs&lt;/li&gt;
&lt;/ul&gt;

&lt;h1 id=&quot;heading-proof-of-brain&quot;&gt;Proof Of Brain&lt;/h1&gt;

&lt;p&gt;&lt;img style=&quot;width: 400px;&quot; src=&quot;/assets/images/brain.jpg&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Proof of brain is a new idea in this space. Currently steem is the only user of proof of brain.&lt;/p&gt;

&lt;p&gt;The idea of proof of brain is you get rewarded for content you create. Steem is a social network, that allows people to blog, create videos or do podcasts and get paid for it.
The better the content the better the reward.&lt;/p&gt;

&lt;p&gt;Proof of brain is a very interesting idea indeed. You can now imagine places like Wikipedia using steem so that they can be paid for there work, as they should be!&lt;/p&gt;</content><author><name>dataPipeline</name></author><category term="exchanges" /><category term="binance" /><category term="using" /><category term="cryptocurrencies" /><category term="bitcoin" /><category term="altcoin" /><summary type="html">Blockchain Validation</summary></entry><entry><title type="html">Hard and soft forks</title><link href="https://www.learningblockchains.com/lessons/hard-and-soft-forks/" rel="alternate" type="text/html" title="Hard and soft forks" /><published>2018-01-23T00:00:00+11:00</published><updated>2018-01-23T00:00:00+11:00</updated><id>https://www.learningblockchains.com/lessons/hard-and-soft-forks</id><content type="html" xml:base="https://www.learningblockchains.com/lessons/hard-and-soft-forks/">&lt;h1 id=&quot;heading-what-are-hard-and-soft-forks&quot;&gt;What are hard and soft forks?&lt;/h1&gt;

&lt;p&gt;In the blockchain world we often hear of these things called forks, and how they are linked to updating or upgrading a blockchain, or even is some cases forming
a new blockchain. The question is then what is a Fork and what is it's impact?&lt;/p&gt;

&lt;p&gt;In this lesson I will discuss the two types of forks which are, Hard forks and Soft forks.&lt;/p&gt;

&lt;h1 id=&quot;heading-soft-forks&quot;&gt;Soft Forks&lt;/h1&gt;

&lt;p&gt;A soft fork is when the network decides they want a new feature in the blockchain. This would mean that the new version of the blockchain is fully backwards compatible.
What I mean by that is, If someone opted to not update their version of the blockchain, they would still be able to use it as they were before the soft fork, and they would
still be able to interact and communicate with the people on the new version. The only downside for them is they wont have the new features that have been updated
to the new venison of that blockchain.&lt;/p&gt;

&lt;p&gt;Soft forks are for simple small changes that the community decides they would like to implement.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/soft_fork.png&quot; /&gt;&lt;/p&gt;

&lt;h1 id=&quot;heading-hard-forks&quot;&gt;Hard Forks&lt;/h1&gt;

&lt;p&gt;A hard fork is much more complicated. A hark for is the result of the community splitting up and starting their own thing. We have seen this with Bitcoin and Bitcoin cash,
Where a lot of the users in the Bitcoin blockchain wanted to change the way Bitcoin works completely, but a lot of people who were wanting to keep to the original white
paper written out by Satoshi Nakamoto and disagreed with the new changes. So Bitcoin Cash was created.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/hard-fork.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;A hard fork is not backwards compatible, once it is upgraded if you havent updated to the new version you will no longer be able to interact or communicate with the people
on the new fork, since a hark fork is in-fact creating a new blockchain.&lt;/p&gt;

&lt;p&gt;Hard forks are risky because if not enough people move to the new updated version it will become useless and unusable. For this reason a lot of preparation goes into a hard fork
set up. Generally people will be given a time frame until the new fork is updated. This is usually set by a block number, Saying the hard fork will happen at block number
50000 for example. At this point, anyone is able to download the new version of the blockchain, however until the hard fork time actually takes place the blockchain
will look and function exactly the same as the original fork. The reason for this is to give people time to download the new blockchain and get ready for the hard fork.&lt;/p&gt;

&lt;p&gt;Because the new fork is based on the original, all previous transactions are the same on both blocks, in the example of Bitcoin and Bitcoin cash, this means that, if
you had 500 Bitcoin when Bitcoin Cash was created you would receive 500 Bitcoin cash. Because in the ledger it says you have that amount seeing that the blockchains
 run off of the same history. However from that point on you can no longer send or receive coins from either side of the fork.&lt;/p&gt;

&lt;h1 id=&quot;heading-summary&quot;&gt;Summary&lt;/h1&gt;

&lt;p&gt;Soft forks are for minor updates that the community agree upon, A hard fork is creating a new blockchain from the bases of the original blockchain, because there were
enough people who wanted to change the way that particular blockchain worked. Because it is risky to do so and not to mention a lot of work, Hard forks arent very
popular in this space. However they do happen and when they do it is always interesting to see what will come out of the fork.&lt;/p&gt;</content><author><name>dataPipeline</name></author><category term="exchanges" /><category term="binance" /><category term="using" /><category term="cryptocurrencies" /><category term="bitcoin" /><category term="altcoin" /><summary type="html">What are hard and soft forks?</summary></entry><entry><title type="html">Merkle Trees</title><link href="https://www.learningblockchains.com/lessons/merkle-trees/" rel="alternate" type="text/html" title="Merkle Trees" /><published>2018-01-02T00:00:00+11:00</published><updated>2018-01-02T00:00:00+11:00</updated><id>https://www.learningblockchains.com/lessons/merkle-trees</id><content type="html" xml:base="https://www.learningblockchains.com/lessons/merkle-trees/">&lt;h2 id=&quot;heading-ensuring-blocks-havent-been-tampered-with&quot;&gt;Ensuring blocks haven't been tampered with&lt;/h2&gt;

&lt;p&gt;In a bitcoin block, there may be many thousands of transactions.
If they were not stored in any particularly clever way, the block itself would always need to store all the information about these transactions.
&quot;Full nodes&quot; do this - they store &lt;em&gt;everything&lt;/em&gt;.
However, for most people this is not sufficient.&lt;/p&gt;

&lt;p&gt;For most use cases, you do not need to download the full list of transactions.
However, you want to be able to verify it later, if needed.
&lt;a href=&quot;/lessons/verifying_blocks/&quot;&gt;Have a look at our lesson on verifying blocks&lt;/a&gt; for more information.
So instead of storing everything, you have the option of just downloading the header information.
In this header information, all of the transactions are represented by a single hash value.
This is the &lt;strong&gt;merkle root&lt;/strong&gt; and is derived from a &lt;strong&gt;merkle tree&lt;/strong&gt; that stores all of the transactions in a bitcoin block.&lt;/p&gt;

&lt;p&gt;Let's have a look at one in action.
First, navigate to &lt;a href=&quot;https://blockchain.info/block/0000000000000000006319e891e4c98886dabe420652b1a58729bb50101ebc2d&quot;&gt;this block's information&lt;/a&gt;.
The merkle root is 7252b7…781be.
The block is made from a whole bunch of transactions, &lt;a href=&quot;https://blockchain.info/tx/03e4c6ce251459a89ade1fe38a275752ed02829773aaa0e6a514bf3d8f17c56e&quot;&gt;such as this one&lt;/a&gt;.
If you look at the Input Scripts section of that page, you can see the raw transaction information (this is what is hashed).
This is a &lt;a href=&quot;/lessons/hashing_functions/&quot;&gt;SHA256 hash&lt;/a&gt;, but how is it derived?&lt;/p&gt;

&lt;p&gt;Let's consider a simple method of storing the transactions, as a simple list.
We take each of the transactions, put them in a list, hash the list, and this gives a merkle root.
This alone provides a lot of benefits.
If someone changes &lt;em&gt;just one&lt;/em&gt; of the values in this list (including simply reording the transactions), the overall hash would change.
The resulting hash wouldn't match the one published on the blockchain, and everyone will know something has changed.
People can then check each transaction independently to see which transaction has been modified.
However, this means downloading all of the transactions to do a verification.&lt;/p&gt;

&lt;p&gt;To get around this issue, a merkle tree is used. The procedure is as follows:&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;The transactions are ordered. The order is up to the miner, who will usually just sort by the highest fees and include those transactions.&lt;/li&gt;
  &lt;li&gt;Every transaction is hashed using the SHA256 function&lt;/li&gt;
  &lt;li&gt;The transactions are paired, with transaction 1 paired with 2. Tx 3 with tx 4. And so on…&lt;/li&gt;
  &lt;li&gt;A pair is then hashed, by concatenating the hashes and then hashing the result. The order of the hashed pairs is maintained.&lt;/li&gt;
  &lt;li&gt;Each of &lt;em&gt;those&lt;/em&gt; hashes is then paired, concatenated and hashed.&lt;/li&gt;
  &lt;li&gt;Step 5 is repeated until only one hash remains, and this is the merkle root.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The full set of transactions, hashed pairs and so on is the merkle tree.&lt;/p&gt;

&lt;h2 id=&quot;heading-benefits&quot;&gt;Benefits&lt;/h2&gt;

&lt;p&gt;So why bother doing this? We already have looked at a list, which provides the benefit of knowing when things are tampered with.
If any transaction is altered, the merkle root changes. Therefore a merkle tree provides the same protection as a simple list-of-transactions.
It is &lt;em&gt;at least as good&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Next, if the block has been tampered with, the list-of-hashes approach requires that we download every transaction to verify it.
With a merkle tree, we can just download the first level down (i.e. the two hashes that combined to create the merkle root).
If the merkle root is different to the published one, then one of these hashes will also be different.
We then only need to verify that &lt;em&gt;half&lt;/em&gt; of the merkle tree.
Further, we can download that hashes &quot;parents&quot; to find out which of those is invalid, further reducing our search space.
In effect, we keep looking down the tree, only looking at branches that have invalid values.&lt;/p&gt;

&lt;p&gt;With computers, it normally takes longer to download a hash, then it takes to compute a hash locally.
This means that we can reduce the amount of network traffic required to find an altered transaction by using a merkle tree.&lt;/p&gt;

&lt;p&gt;In summary, merkle trees provide a way for us to verify that the transactions included in a block have not been modified.
Those with lower-powered computers can simply download the headers for a block, rather than the full list of transactions.
They then &lt;em&gt;know&lt;/em&gt; that a block's hasn't been tampered with later on, because they have a record of the merkle root and can verify that.
Merkle trees are also faster to use to find any modified data.&lt;/p&gt;</content><author><name>dataPipeline</name></author><category term="algorithms" /><category term="blockchains" /><category term="merkle_trees" /><category term="implementing" /><summary type="html">Ensuring blocks haven't been tampered with</summary></entry><entry><title type="html">Verifying a Block</title><link href="https://www.learningblockchains.com/lessons/verifying_blocks/" rel="alternate" type="text/html" title="Verifying a Block" /><published>2017-11-30T00:00:00+11:00</published><updated>2017-11-30T00:00:00+11:00</updated><id>https://www.learningblockchains.com/lessons/verifying_blocks</id><content type="html" xml:base="https://www.learningblockchains.com/lessons/verifying_blocks/">&lt;p&gt;In this lesson, we are going to deep dive into one component of the blockchain, and discover how you can verify, for yourself, a mined block.
This gives you assurance that the blocks on the blockchain are legitimate, and that the transactions are appropriately mined.
If you do this for all blocks in the blockchain, you can be sure that all transactions in the chain are fair.
Open auditing is a great feature!&lt;/p&gt;

&lt;p&gt;You'll need to first know what a blockchain is, and what a hash function is.&lt;/p&gt;

&lt;p&gt;There are two steps we will cover, over two lessons.
In the first lesson (this one), we will cover how to verify the block from the top-level information, specifically the information discoverable in the headers of the blockchain.
The main critical part that will be missing from our analysis is verifying the transaction's merkle tree, which we will deal with in the second lesson (coming soon!).
For now, we will assume the merkle tree root value is correct.
In short, the merkle tree root is a hash of the list of transactions, allowing us to verify the transactions, their order, and their values.
If a single transaction in a merkle tree is changed, the root will change, allowing us to see something has been tampered with.
(By the way, this has the flow on affect of meaning the nonce doesn't work for the block, and therefore is it not correctly mined.)&lt;/p&gt;

&lt;h2 id=&quot;heading-useful-python-functions&quot;&gt;Useful Python functions&lt;/h2&gt;

&lt;p&gt;We will be using Python 3 in this lesson. If you are more familiar with another programming language, the general concepts will transfer over, but I will be talking about some specific python features as well.
If you use Python 2, I recommend using Python 3, as it makes the data types more explicit and its more obvious what exactly is happening at each step (but it is a bit more verbose).&lt;/p&gt;

&lt;p&gt;First, a slight detour. Python 3 makes a distinction between a &lt;code class=&quot;highlighter-rouge&quot;&gt;bytes&lt;/code&gt; object and a &lt;code class=&quot;highlighter-rouge&quot;&gt;string&lt;/code&gt; object.
A string is a list of character, such as the text you are reading now.
A bytes object is a sequence of 1s and 0s.
We can &lt;em&gt;choose&lt;/em&gt; to represent a bytes object as a string object, which we do with a mapping called an encoding.
You may be familiar with ascii, which maps the numbers 0 to 255 to the English characters, punctuation and some other control characters (such as space and the newline character).
With this encoding, we can take a singe byte, map it to its character, and show that character in a process known as decoding.&lt;/p&gt;

&lt;p&gt;Another encoding is hexadecimal.
It takes a four bit sequence and maps it to the characters 0123456789ABCDE, in that order.
The bit sequence 0010 maps to the character 2.
The sequence 0111 maps to 7, while 1011 maps to B (it's the decimal number 11 and B is the eleventh character in that list).&lt;/p&gt;

&lt;p&gt;That's all a little theoretical, let's have a look at it in practice.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;s&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;I am a string&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;s&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ascii&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;([&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;bin&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bb&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The first line creates a simple string. The second decodes it into a bytes object. If you &lt;code class=&quot;highlighter-rouge&quot;&gt;print(b)&lt;/code&gt;, the result will &lt;em&gt;look&lt;/em&gt; like the string, except for a b in front of the opening quote.
Instead, its just a sequence of 1s and 0s in memory, and when we print it, it uses ascii to represent it.
The third line shows a bit clearer what the data structure is like, a sequence of numbers on disk, which are just ones and zeros as shown on by the fourth line of code.&lt;/p&gt;

&lt;p&gt;Representing the same integers as hexadecimal in Python 3 requires the &lt;code class=&quot;highlighter-rouge&quot;&gt;binascii&lt;/code&gt; library:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;binascii&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;binascii&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexlify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;b&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;16&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The output from this shows the hexademical representation of the same numbers.
Note they are in &quot;pairs&quot;, which is why there are twice as many hexadecimal numbers as letters in the string.
The first pair, &quot;49&quot; is the hexadecimal representation of the number 73, which is converted in the last line of the above code block.&lt;/p&gt;

&lt;p&gt;Bitcoin, and most blockchains, share their data either in a raw binary format or in hexadecimal format, which you'll be familiar with already, of very shortly after starting to use blockchains.
Let's now have a look at a block in detail.&lt;/p&gt;

&lt;h2 id=&quot;heading-block-123&quot;&gt;Block 123&lt;/h2&gt;

&lt;p&gt;You can view information about &lt;a href=&quot;https://blockexplorer.com/block/00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d&quot;&gt;this block on blockchain.info&lt;/a&gt;, but I'll copy the relevant information to this page.
The hash of this block is &lt;code class=&quot;highlighter-rouge&quot;&gt;00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d&lt;/code&gt;, which matches the general criteria of &quot;starting with lots of zeros&quot;.
One small issue is that bitcoin puts its hashes &quot;backwards&quot; by pairs, compared to Python (and many other implementations).
This is known as the endianess of the output, and is a topic for another day.
The same hash in a &quot;python friendly format&quot; is &lt;code class=&quot;highlighter-rouge&quot;&gt;1dbd981fe6985776b644b173a4d0385ddc1aa2a829688d1e0000000000000000&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;To convert between two strings with hexademical formats, here is a useful function.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;swap_endianess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;0x&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:]&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# If the hash starts with &quot;0x&quot;, remove it&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;join&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;i&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;+&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;i&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;range&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)])&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If you aren't familiar with python, this code splits the string into chunks of length two, reverses the order, and joins them back together. You can call it like this:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;new_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap_endianess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Finally, remember what we said about bytes objects? If you actually want to use this in our later code, i.e. to compute the hash-of-the-hash, you need to convert to a bytes object.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;new_hash&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ascii&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The type of g is a bytes object (try &lt;code class=&quot;highlighter-rouge&quot;&gt;print(type(g))&lt;/code&gt;), and can be put into a hashing function (strings cannot), like this:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hashlib&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashlib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;g&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;h&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexdigest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h2 id=&quot;heading-verifying-a-block&quot;&gt;Verifying a Block&lt;/h2&gt;

&lt;p&gt;Now that the necessary Python is out of the way, let's have a look at how we can use those functions and concepts to verify a block from top-level information.
You can see information about the schema on &lt;a href=&quot;https://bitcoin.org/en/developer-reference#block-headers&quot;&gt;bitcoin.org&lt;/a&gt;, but note that it may change in the future.
Bitcoin is consensus, not formality, and if the general consensus decides to change, then the format changes.&lt;/p&gt;

&lt;p&gt;The linked site specifies the data needed, and the format its in. The main pieces of the puzzle are these:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Version number, as a 32 bit (4 byte) value&lt;/li&gt;
  &lt;li&gt;Previous block hash, as a 32 byte value&lt;/li&gt;
  &lt;li&gt;Merkle root hash, as a 32 byte value&lt;/li&gt;
  &lt;li&gt;Time, as a 4 byte number&lt;/li&gt;
  &lt;li&gt;nBits, the target threshold for the block as a 4 byte number&lt;/li&gt;
  &lt;li&gt;nonce, the value that needs to be mined as a 4 byte number&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Let's grab the relevant information from the block and then work on changing the format, one-by-one, to the format needed to verify this block's hash.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;previous_block_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;00000000000008a3a41b85b8b29ad444def299fee21793cd8b9e567eab02cd81&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;merkle_root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;2b12fcf1b09288fcaff797d71e950e71ae42b91e8bdb2304758dfcffc2b620e3&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;2011-05-21 17:26:31&quot;&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;bits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;440711666&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;nonce&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;2504433986&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;First, we convert the version number to a four byte hexadecimal number.
If we just use the &lt;code class=&quot;highlighter-rouge&quot;&gt;hex&lt;/code&gt; function, it will not pad it out to four bytes:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nb&quot;&gt;hex&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# prints &quot;0x01&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Instead, we can represent it ourselves like this:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;{0:#0{1}x}&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Then, we use our function to change the endianess:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;c&quot;&gt;# Before: 0x000001&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap_endianess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;c&quot;&gt;# After: 010000&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Next, we convert the two hash values to the required bitcoin format, by swapping the endianess:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;previous_block_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap_endianess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;previous_block_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;merkle_root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap_endianess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;merkle_root&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The next value is the time, which is a little tricky.
The string &lt;strong&gt;v&lt;/strong&gt; is just a representation of the time, but we need to find out the &lt;em&gt;timestamp&lt;/em&gt;, which is the number of seconds since January 1st, 1970.
Timestamps are useful, as they let us represent dates and times as a single integer.
This code parses the timestamp:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;dateutil&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;datetime_object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;parser&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;parse&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;


&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;This code convert it into a timestamp, we work out how many seconds since the 1st of January, 1970 (GMT/UTC).&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;datetime&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;timestamp_as_int&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;((&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;datetime_object&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;datetime&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1970&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;total_seconds&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We then convert to hexadecimal and swap the endianess, as we did with the version number:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap_endianess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{0:#0{1}x}&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;timestamp_as_int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;The last two values are the required data are just two numbers, and we now know how to convert those:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;bits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap_endianess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{0:#0{1}x}&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;bits&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;nonce&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap_endianess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;{0:#0{1}x}&quot;&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;format&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;nonce&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;8&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;))&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Each of these now needs to be encoded as bytes objects, and then concatenated together (joined) to form a long bytes object.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ascii&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;previous_block_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous_block_hash&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ascii&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;merkle_root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;merkle_root&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ascii&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ascii&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;bits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bits&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ascii&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;nonce&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nonce&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;ascii&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;header_as_bytes&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;version&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous_block_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;merkle_root&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;bits&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;nonce&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Finally, we take all this data, convert to bits, and then compute the SAH256 hash &lt;em&gt;twice&lt;/em&gt;, which gives us:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hashlib&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;block&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;binascii&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;unhexlify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;header_as_bytes&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;n&quot;&gt;round1&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashlib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;digest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;round2&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashlib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;round1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexdigest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;verify&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;swap_endianess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;round2&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We can then verify that the result is the same:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;assert&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;verify&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;00000000000000001e8d6829a8a21adc5d38d0a473b144b6765798e61f98bd1d&quot;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We are done! If you got an &lt;code class=&quot;highlighter-rouge&quot;&gt;AssertionError&lt;/code&gt;, check your code and try again. If nothing bad happened, then the hashest match, and you have verified a block!&lt;/p&gt;</content><author><name>dataPipeline</name></author><category term="implementing" /><category term="python" /><category term="hash" /><category term="coding" /><summary type="html">In this lesson, we are going to deep dive into one component of the blockchain, and discover how you can verify, for yourself, a mined block. This gives you assurance that the blocks on the blockchain are legitimate, and that the transactions are appropriately mined. If you do this for all blocks in the blockchain, you can be sure that all transactions in the chain are fair. Open auditing is a great feature!</summary></entry><entry><title type="html">Exchanging your coins</title><link href="https://www.learningblockchains.com/lessons/exchanging_coins/" rel="alternate" type="text/html" title="Exchanging your coins" /><published>2017-11-02T00:00:00+11:00</published><updated>2017-11-02T00:00:00+11:00</updated><id>https://www.learningblockchains.com/lessons/exchanging_coins</id><content type="html" xml:base="https://www.learningblockchains.com/lessons/exchanging_coins/">&lt;p&gt;There are a lot of different cryptocurrencies around at the moment.
Of these, there are a few top-tier coins, such as Bitcoin and Etherium.
There are many, many others too.&lt;/p&gt;

&lt;p&gt;Some of these other coins aim to be the dominant coin.
For instance, Bitcoin Cash has features that some claim make it a more useful coin than Bitcoin, and they suggest that everyone move over to Bitcoin Cash instead of using Bitcoin.
(It's a heated argument, and not one I'm making a specific recommendation on!)
Other coins are specialised, like Golem, designed to be for people to sell their computing power to others.&lt;/p&gt;

&lt;p&gt;Regardless of the exact coins, you may want to convert your coins from one coin to another, or even just convert a little at a time.
Doing this requires a way to exchange your coins.
Most of the time, you do this by converting from your coin to Bitcoin, and then from Bitcoin to the other coin.
Bitcoin is chosen due to its dominance in the crypto-world, and its easier to just have one conversion to bitcoin, than between all possible coin-pairs.
That said, some websites do provide other direct coin-exchanges.&lt;/p&gt;

&lt;iframe id=&quot;widget-ticker-preview&quot; src=&quot;//www.coingecko.com/en/widget_component/ticker/bitcoin/usd?id=bitcoin&quot; style=&quot;border:none; height:125px; width: 275px;&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; allowtransparency=&quot;true&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;One such website is the exchange &lt;a href=&quot;https://www.binance.com/register.html?ref=10323397&quot;&gt;Binance&lt;/a&gt;, which allows you to convert between coins on an open exchange.
One thing to note is that Binance is an exchange, where you make an offer of your coin and the price you want to sell or buy at.
At that point, someone else needs to come along and fulfil that order.
As an exchange, Binance records that &quot;I want to sell 3 Etherium for 0.125 Bitcoin&quot;.
Other people come through, accept that offer and perform the trade. If you are selling for too much, your trade may not find a buyer!&lt;/p&gt;

&lt;p&gt;There are other services, like Shapeshift, that simply convert your coins, but they simply just set the price to whatever is needed to do the conversion (i.e. market price, which we will use in a minute), or those services keep a cache of coins for conversion and determine their own price.
This makes it nice and easy to use, but it may not be the best price for you!&lt;/p&gt;

&lt;p&gt;In the rest of this lesson, I'll go through the process of using Binance to convert coins from one type (Bitcoin - BTC) into another type (EOS).
Every exchange will be slightly different, and ensure you are aware of the consequences of your steps before you take them.
Remember, you are dealing with coins of value, and mistakes are often very hard or impossible to undo. Take it slowly and carefully!&lt;/p&gt;

&lt;h1 id=&quot;heading-step-1-register&quot;&gt;Step 1: Register&lt;/h1&gt;

&lt;p&gt;Sign up on &lt;a href=&quot;https://www.binance.com/register.html?ref=10323397&quot;&gt;Binance&lt;/a&gt; with an account.
If you want to exchange large amounts, you will need to formally identify yourself, which you can do from your account settings, found on the top right of the screen.
After you have registered, sign into Binance.&lt;/p&gt;

&lt;h1 id=&quot;heading-step-2-deposit-funds&quot;&gt;Step 2: Deposit funds&lt;/h1&gt;

&lt;p&gt;To exchange, you'll need some initial coin. I recommend Bitcoin as the initial coin as there are more places to buy bitcoin.
That said, Binance accepts many different coins.&lt;/p&gt;

&lt;p&gt;Click the &lt;code class=&quot;highlighter-rouge&quot;&gt;Funds&lt;/code&gt; option on the top navigation bar, and click &lt;code class=&quot;highlighter-rouge&quot;&gt;Deposits Withdrawals&lt;/code&gt;.
The menu bar is shown below.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.learningblockchains.com/assets/images/exchange_lesson/binance-menu-1.png&quot; alt=&quot;Screenshot of Binance menu&quot; /&gt;&lt;/p&gt;

&lt;p&gt;This next screen shows all the coins that Binance can accept. Choose your deposit coin (i.e. the coin you already have) and click &lt;code class=&quot;highlighter-rouge&quot;&gt;Deposit&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.learningblockchains.com/assets/images/exchange_lesson/binance-coins-2.png&quot; alt=&quot;Screenshot of Binance deposit and withdrawal screen&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In the screen below, I had some spare Litecoin and used that coin to deposit.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.learningblockchains.com/assets/images/exchange_lesson/binance-deposit-4.png&quot; alt=&quot;Screenshot of LTC deposit&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Copy your address and send the coin from your other wallet.
I'm going to use Bitcoin, but be aware that it has high transaction fees compared to other coins.
Check Binance's list of coins, compare with what you have or can get, and find a coin that suits you.&lt;/p&gt;

&lt;p&gt;Double check the hash before sending, and also confirm you are sending the right coin.
As an example, Bitcoin Cash addresses look exactly like normal Bitcoin addresses, and no error will be identified, but your money will not turn up!
Check with a local expert if you are unsure - there are many coins that share similar address structures.&lt;/p&gt;

&lt;p&gt;After your coins have arrived, you'll have a listed balance in Binance.&lt;/p&gt;

&lt;h1 id=&quot;heading-step-3-trade-at-market-prices&quot;&gt;Step 3: Trade at Market Prices&lt;/h1&gt;
&lt;p&gt;Click the &lt;code class=&quot;highlighter-rouge&quot;&gt;Exchange&lt;/code&gt; button in the top menu, then choose &lt;code class=&quot;highlighter-rouge&quot;&gt;Basic&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.learningblockchains.com/assets/images/exchange_lesson/binance-exchange-5.png&quot; alt=&quot;Choosing Basic Exchange&quot; /&gt;&lt;/p&gt;

&lt;p&gt;On the right of the screen is a search bar.
In this bar, enter the non-bitcoin coin you want to convert to or from.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.learningblockchains.com/assets/images/exchange_lesson/binance-find-6.png&quot; alt=&quot;Searching for conversions&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I'll do two trades in this lesson. First, I'll sell my LTC (Litecoin) for some BTC (Bitcoin), and then I'll also trade Bitcoin for EOS.
First, I enter LTC, and LTC/BTC comes up.
Click that entry, and it shows the trades for this conversion.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.learningblockchains.com/assets/images/exchange_lesson/binance-trade-7.png&quot; alt=&quot;Trading box at Binance&quot; /&gt;&lt;/p&gt;

&lt;iframe id=&quot;widget-ticker-preview&quot; src=&quot;//www.coingecko.com/en/widget_component/ticker/litecoin/usd?id=litecoin&quot; style=&quot;border:none; height:125px; width: 275px;&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; allowtransparency=&quot;true&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;At the bottom of the screen is a tabbed section with the options &quot;Trade&quot; or &quot;Market&quot;.
If you want to set your price, select Trade.
If you want to let the market decide, choose Market.&lt;/p&gt;

&lt;p&gt;For this first trade (LTC to BTC), I'll choose Market, which uses the current price of the market.
This means the price is set by finding an order that can fulfil your request to buy or sell, and then selecting that price.
Sometimes this doesn't happen (i.e. if the price drastically drops just as you put your trade in, or if you are trading a coin with no buyers at the moment).&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.learningblockchains.com/assets/images/exchange_lesson/binance-ltc100market-8.png&quot; alt=&quot;Selling all your coins&quot; /&gt;&lt;/p&gt;

&lt;p&gt;I want to trade all of my LTC for BTC, and therefore select the 100% option.
If you have a specific amount to trade, you can enter that manually, or select as a percentage.&lt;/p&gt;

&lt;p&gt;After making your order, if it was a Market order, it is usually performed pretty quickly.
You can check by clicking &lt;code class=&quot;highlighter-rouge&quot;&gt;Orders&lt;/code&gt; in the top menu, and then &lt;code class=&quot;highlighter-rouge&quot;&gt;Open Orders&lt;/code&gt; to seen any incomplete orders.
If your order is not there, click &lt;code class=&quot;highlighter-rouge&quot;&gt;Orders&lt;/code&gt; again and then &lt;code class=&quot;highlighter-rouge&quot;&gt;Order History&lt;/code&gt; to see your order.&lt;/p&gt;

&lt;h1 id=&quot;heading-step-4-trade-at-your-price&quot;&gt;Step 4: Trade at your price&lt;/h1&gt;
&lt;p&gt;If you want to set your price, you need to use the &lt;code class=&quot;highlighter-rouge&quot;&gt;Trade&lt;/code&gt; tab we saw earlier.
Click &lt;code class=&quot;highlighter-rouge&quot;&gt;Exchange&lt;/code&gt; and &lt;code class=&quot;highlighter-rouge&quot;&gt;Basic&lt;/code&gt; again to go to the basic exchange screen.
Enter the non-bitcoin coin in the search menu (I'm converting my bitcoin to EOS) and select the conversion.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.learningblockchains.com/assets/images/exchange_lesson/binance-eossearch-9.png&quot; alt=&quot;Searching for EOS&quot; /&gt;&lt;/p&gt;

&lt;iframe id=&quot;widget-ticker-preview&quot; src=&quot;//www.coingecko.com/en/widget_component/ticker/eos/usd?id=eos&quot; style=&quot;border:none; height:125px; width: 275px;&quot; scrolling=&quot;no&quot; frameborder=&quot;0&quot; allowtransparency=&quot;true&quot;&gt;&lt;/iframe&gt;

&lt;p&gt;The ticker will show the current price. At the time of writing, the conversion was 1 EOS for 0.00015200 BTC.
I'd like to buy if the price dips a little, so I set my price at 0.000145BTC.
The order will not process straight away.
Instead, it will likely sit in the queue until people sell at higher prices.
If the price drops to my offer, someone may choose to sell their EOS for my price.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.learningblockchains.com/assets/images/exchange_lesson/binance-eosoverview-10.png&quot; alt=&quot;EOS Pricing screen&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You need to enter the current price you are offering, as well as how many coins you are buying or selling at that price.
In my example, I want to buy 10 EOS and am willing to pay 0.000145 BTC per EOS.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.learningblockchains.com/assets/images/exchange_lesson/binance-eosbuy-11.png&quot; alt=&quot;Buying EOS for a set price&quot; /&gt;&lt;/p&gt;

&lt;p&gt;You can see how hard it will be to sell at a given price by checking the depth chart.
On the top right of the plot in the middle of the screen, select &lt;code class=&quot;highlighter-rouge&quot;&gt;Depth&lt;/code&gt;.
The depth chart highlights the cumulative orders at different prices.
It has pointed to my price, and it shows all the buy orders that need to be fulfilled that are at higher prices in green and to the right of my order.
That's a lot of orders!
Learning to read depth charts can give you a sense of how easy or hard it will be to sell or buy at a given price.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;https://www.learningblockchains.com/assets/images/exchange_lesson/binance-eosdepth-12.png&quot; alt=&quot;EOS Depth Chart&quot; /&gt;&lt;/p&gt;

&lt;p&gt;That's it on exchanging coins.
If you use our referral links to Binance above, half of your transaction fees to to the site, helping us stay up and with new content coming regularly.
On top of that, it doesn't cost you any more than usual to do your transactions!&lt;/p&gt;</content><author><name>dataPipeline</name></author><category term="exchanges" /><category term="binance" /><category term="using" /><category term="cryptocurrencies" /><category term="bitcoin" /><category term="altcoin" /><summary type="html">There are a lot of different cryptocurrencies around at the moment. Of these, there are a few top-tier coins, such as Bitcoin and Etherium. There are many, many others too.</summary></entry><entry><title type="html">Buying Bitcoin</title><link href="https://www.learningblockchains.com/lessons/buying-bitcoin/" rel="alternate" type="text/html" title="Buying Bitcoin" /><published>2017-11-01T00:00:00+11:00</published><updated>2017-11-01T00:00:00+11:00</updated><id>https://www.learningblockchains.com/lessons/buying-bitcoin</id><content type="html" xml:base="https://www.learningblockchains.com/lessons/buying-bitcoin/">&lt;p&gt;In the world of Cryptocurrencies, Bitcoin (BTC) is the biggest and most known of the coins, So as it stands most other alternate coins (ALTS) are bought using BTC.&lt;/p&gt;

&lt;p&gt;As the market grows for BTC so does its value, We have seen just over the last few months BTC go from $5300 USD to $7718 USD. This means people who bought in at around the $5300 mark
made a substantial amount of money, this means you can potentially be spending money and still making it!&lt;/p&gt;

&lt;p&gt;So how do you buy BTC?&lt;/p&gt;

&lt;p&gt;The first thing you need to know is that you do not need to buy a full bitcoin, at the time of writing 1 BTC is worth $6694.02 USD.
You can buy as little or as much bitcoin as you want, Putting a few hundred in it is still going to see you make returns and you will still be able to buy anything with the same dollar value of your
own countries currency, as long as the place you are buying from accepts bitcoin or B pay.&lt;/p&gt;

&lt;p&gt;To get started head to &lt;a href=&quot;https://www.btcmarkets.net/&quot;&gt; https://www.btcmarkets.net/ &lt;/a&gt;  and create an account, by clicking on the create account button.&lt;/p&gt;

&lt;p&gt;&lt;img style=&quot;height: 500px; &quot; src=&quot;/assets/images/btcmarkets/btcmarkets_create_account.png&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this process you will need to provide your ID and other personal details to verify your account.&lt;/p&gt;

&lt;p&gt;Once your account is created click on account and head to deposit. Here you will be given options to deposit your Fiat which will be used to buy bitcoin.
There are many options to send your money through to BTC markets, I personally use POLI.&lt;/p&gt;

&lt;p&gt;Once you have found a suitable option for yourself head to the Buy/Sell at the top right hand side of the page. You will come to a screen like this.&lt;/p&gt;

&lt;div class=&quot;centered&quot;&gt;
&lt;img style=&quot;height: 500px; &quot; src=&quot;/assets/images/btcmarkets/btcmarkets_Buy_sell.png&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;Here is where we will buy some BTC!&lt;/p&gt;

&lt;p&gt;In the screenshot below we need to provide the Volume and price of what we want to buy.&lt;/p&gt;

&lt;div class=&quot;centered&quot;&gt;
&lt;img src=&quot;/assets/images/btcmarkets/btcmarkets_buy.png&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;It is important you understand exactly what these are.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;b&gt;Volume:&lt;/b&gt; Is how many coins or % of a coin you want to buy.&lt;/li&gt;
  &lt;li&gt;&lt;b&gt;Price:&lt;/b&gt; The amount you want to buy at.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I'll now show you an example of buying $100 worth of BTC.&lt;/p&gt;

&lt;pre&gt;&lt;code class=&quot;language-code&quot;&gt;1 BTC = $6677.94
Volume:  0.01498 BTC
Price: $6500
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;What this above example is showing, is the current value of BTC being at $6677.94 , we are wanting to buy $100 worth of BTC. The volume is 0.01498 which Is the equivalent of $100.04 of USD currency.
The Price is $6500 which is the price we want to buy the coin at. This means when the price of BTC goes down to $6500 BTC markets will buy our $100 worth of bitcoin at that point.&lt;/p&gt;

&lt;p&gt;In this image below I have filled out the information. I am In Australia so the price is slightly different and with the constant change of bitcoin it has, in-fact moved below $100 at this point.&lt;/p&gt;

&lt;div class=&quot;centered&quot;&gt;
&lt;img src=&quot;/assets/images/btcmarkets/btcmarkets_buy_numbers.PNG&quot; /&gt;
&lt;/div&gt;

&lt;p&gt;So you can now see you want to buy 0.01498 of BTC at the market price of $3500, and that will cost you $98.19.&lt;/p&gt;

&lt;p&gt;When this is completed you will now be able to go to your account and you will see Account summary, Here you should see your BTC listed!
Now all you need to do is click withdraw and send your new BTC to your wallet!&lt;/p&gt;

&lt;p&gt;Congratulations you have bought your first BTC.&lt;/p&gt;</content><author><name>dataPipeline</name></author><category term="exchanges" /><category term="binance" /><category term="using" /><category term="cryptocurrencies" /><category term="bitcoin" /><category term="altcoin" /><summary type="html">In the world of Cryptocurrencies, Bitcoin (BTC) is the biggest and most known of the coins, So as it stands most other alternate coins (ALTS) are bought using BTC.</summary></entry><entry><title type="html">Interacting with the blockchain</title><link href="https://www.learningblockchains.com/lessons/interacting-with-the-blockchain/" rel="alternate" type="text/html" title="Interacting with the blockchain" /><published>2017-10-29T00:00:00+11:00</published><updated>2017-10-29T00:00:00+11:00</updated><id>https://www.learningblockchains.com/lessons/interacting-with-the-blockchain</id><content type="html" xml:base="https://www.learningblockchains.com/lessons/interacting-with-the-blockchain/">&lt;p&gt;In this lesson, we are going to take the code we used in &lt;a href=&quot;/lessons/Building_blockchain/&quot;&gt;building a blockchain&lt;/a&gt;, and turn it into an interactive API endpoint using Flask.&lt;/p&gt;

&lt;p&gt;&lt;i&gt; If you haven't done the lesson on building a blockchain, it is recommended you go back and do that before doing this lesson. &lt;/i&gt;&lt;/p&gt;

&lt;p&gt;In this lesson, we will cover these 4 steps.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Implementing a basic proof of work&lt;/li&gt;
  &lt;li&gt;Making our blockchain an API endpoint&lt;/li&gt;
  &lt;li&gt;Creating a miner for your blockchain&lt;/li&gt;
  &lt;li&gt;Interacting with the blockchain&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;&lt;strong&gt;Things we need:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You will need to have Python 3.6 or higher along with pip installed.
To install Python click &lt;a href=&quot;https://www.python.org/downloads/&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once Python is installed, we then need to install Flask and the requests library.&lt;/p&gt;

&lt;p&gt;To install these open up your terminal and type the following:&lt;/p&gt;

&lt;p&gt;&lt;code class=&quot;highlighter-rouge&quot;&gt; pip install Flask==0.12.2 requests==2.18.4 &lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Open up your &lt;code class=&quot;highlighter-rouge&quot;&gt;blockchain.py&lt;/code&gt; file from the previous lesson.&lt;/p&gt;

&lt;p&gt;At the top of your file import all of these&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hashlib&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;json&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;time&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;typing&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;List&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;urllib.parse&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;urlparse&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;uuid&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;uuid4&lt;/span&gt;

&lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;requests&lt;/span&gt;
&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;flask&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Flask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jsonify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We will also require a HTTP client, I'm going to recommend &lt;a href=&quot;https://www.getpostman.com/&quot;&gt;postman&lt;/a&gt;, as it is an easy to use interface.&lt;/p&gt;

&lt;h1 id=&quot;heading-step-1---implementing-a-basic-proof-of-work&quot;&gt;Step 1 - Implementing a basic proof of work&lt;/h1&gt;

&lt;p&gt;&lt;strong&gt;Understanding Proof of Work&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A Proof of Work algorithm (PoW) is how new Blocks are created or mined on the blockchain.&lt;/p&gt;

&lt;p&gt;The goal of PoW is to discover a number which solves a problem.
The number must be difficult to find but easy to verify for anyone on the network. The number is made of cryptographic signatures, meaning that if the wrong number is sent somewhere it will be
denied access. Thus the PoW algorithm means you can now send money without having to trust anybody or any organisation, as the blockchain only cares about the cryptographic signatures.
This is the core idea behind Proof of Work.&lt;/p&gt;

&lt;p&gt;We’ll look at a very simple example:&lt;/p&gt;

&lt;p&gt;Let’s decide that the hash of some integer x multiplied by another y must end in 0. So, hash(x * y) = ac23d….0.
And for this simplified example, let’s fix x = 10.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Implementing this in Python:&lt;/strong&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;kn&quot;&gt;from&lt;/span&gt; &lt;span class=&quot;nn&quot;&gt;hashlib&lt;/span&gt; &lt;span class=&quot;kn&quot;&gt;import&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;x&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;10&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;  &lt;span class=&quot;c&quot;&gt;# We don't know what y should be yet...&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'{x*y}'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexdigest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;!=&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;y&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;print&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'The solution is y = {y}'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;The solution here is y = 24. Since, the produced hash ends in 0:
hash(10 * 24) = 1253e9373e…5e3600155e860&lt;/p&gt;

&lt;p&gt;Just before we add our PoW code, We will need to add some code that will doing the hashing of the block for us.&lt;/p&gt;

&lt;p&gt;Here is the code:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;nd&quot;&gt;@staticmethod&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        Creates a SHA-256 hash of a Block
        :param block: Block
        &quot;&quot;&quot;&lt;/span&gt;

        &lt;span class=&quot;c&quot;&gt;# We must make sure that the Dictionary is Ordered, or we'll have inconsistent hashes&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;block_string&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;json&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;dumps&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sort_keys&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;True&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashlib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block_string&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexdigest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;If you want to learn more about hashing, check out our lesson on &lt;a href=&quot;/lessons/hashing_functions/&quot;&gt;Hashing Functions&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The PoW algorithm is what miners race to solve in order to create a new block.
The difficulty is determined by the number of characters searched for in a string.
The miners are then rewarded for their solution by receiving a coin via a transaction.&lt;/p&gt;

&lt;p&gt;Below we will implement our own basic proof of work for our blockchain:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;proof_of_work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_proof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        Simple Proof of Work Algorithm:
         - Find a number 0' such that hash(00') contains leading 4 zeroes, where 0 is the previous 0'
         - 0 is the previous proof, and 0' is the new proof
        &quot;&quot;&quot;&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;proof&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;while&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;valid_proof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_proof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;is&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;False&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
            &lt;span class=&quot;n&quot;&gt;proof&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+=&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proof&lt;/span&gt;

    &lt;span class=&quot;nd&quot;&gt;@staticmethod&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;valid_proof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_proof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;bool&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        Validates the Proof
        :param last_proof: Previous Proof
        :param proof: Current Proof
        :return: True if correct, False if not.
        &quot;&quot;&quot;&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;guess&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'{last_proof}{proof}'&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;encode&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;guess_hash&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;hashlib&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;sha256&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;guess&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;hexdigest&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;guess_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[:&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;0000&quot;&lt;/span&gt;

&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;h1 id=&quot;heading-step-2---making-our-blockchain-an-api-endpoint&quot;&gt;Step 2 - Making our blockchain an API endpoint&lt;/h1&gt;

&lt;p&gt;Add the below code to the bottom of your Python file. This code turns your file into an API end point.
This will allow us to use postman to send/receive requests in our blockchain.&lt;/p&gt;

&lt;p&gt;&lt;i&gt; More on Flask API endpoints &lt;a href=&quot;https://flask-restless.readthedocs.io/en/stable/basicusage.html&quot;&gt; here &lt;/a&gt;&lt;/i&gt;&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Blockchain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Instantiate our Node&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;app&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Flask&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Generate a globally unique address for this node&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;node_identifier&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;uuid4&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;())&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;replace&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'-'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;''&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

&lt;span class=&quot;c&quot;&gt;# Instantiate the Blockchain&lt;/span&gt;
&lt;span class=&quot;n&quot;&gt;blockchain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Blockchain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;


&lt;span class=&quot;nd&quot;&gt;@app.route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'/mine'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'GET'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;We'll mine a new Block&quot;&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@app.route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'/transactions/new'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'POST'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new_transaction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;We'll add a new transaction&quot;&lt;/span&gt;

&lt;span class=&quot;nd&quot;&gt;@app.route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'/chain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'GET'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;full_chain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'chain'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockchain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'length'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;blockchain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jsonify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;

&lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;__name__&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;==&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'__main__'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;app&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;run&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;host&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'0.0.0.0'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;port&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5000&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;Now if you open a terminal in the location of your file, and type &lt;code class=&quot;highlighter-rouge&quot;&gt;python blockchain.py&lt;/code&gt; it will start the server.&lt;/p&gt;

&lt;p&gt;Now we need to add this below code under your new flask server code. This code will allow you to POST a new transaction over the web to your blockchain.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@app.route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'/transactions/new'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'POST'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new_transaction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;request&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;get_json&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;()&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;# Check that the required fields are in the POST'ed data&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;required&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'sender'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'recipient'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'amount'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;if&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;not&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;all&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt; &lt;span class=&quot;k&quot;&gt;for&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;k&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;in&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;required&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;'Missing values'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;400&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;# Create a new Transaction&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;index&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockchain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_transaction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'sender'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'recipient'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;values&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'amount'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'message'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;f&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'Transaction will be added to Block {index}'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jsonify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;201&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;This is what a Transaction will look like, the user will send this over the server.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;&quot;sender&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;my address&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;&quot;recipient&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;someone else's address&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
 &lt;span class=&quot;s&quot;&gt;&quot;amount&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;we are almost ready to start interacting, all we need now is a way to mine a transaction.&lt;/p&gt;

&lt;h1 id=&quot;heading-step-3---creating-a-miner-for-your-blockchain&quot;&gt;Step 3 - Creating a miner for your blockchain&lt;/h1&gt;

&lt;p&gt;The below code will create a miner for your server, This will mine and create a new transaction to be placed on a block in your blockchain.&lt;/p&gt;

&lt;p&gt;Add this code, below your new transaction code.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;nd&quot;&gt;@app.route&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'/mine'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;methods&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'GET'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt;
&lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;mine&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# We run the proof of work algorithm to get the next proof...&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;last_block&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockchain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_block&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;last_proof&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;last_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'proof'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;proof&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockchain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proof_of_work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_proof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;# We must receive a reward for finding the proof.&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# The sender is &quot;0&quot; to signify that this node has mined a new coin.&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;blockchain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_transaction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;&quot;0&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;recipient&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;node_identifier&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;c&quot;&gt;# Forge the new Block by adding it to the chain&lt;/span&gt;
    &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;blockchain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;new_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proof&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;proof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous_hash&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;0&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;

    &lt;span class=&quot;n&quot;&gt;response&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'message'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;s&quot;&gt;&quot;New Block Forged&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'index'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'index'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'transactions'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'transactions'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'proof'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'proof'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;'previous_hash'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'previous_hash'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;],&lt;/span&gt;
    &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;jsonify&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;response&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;),&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;200&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We will be able to use this code to interact with our blockchain, See it be mined, and then check the chain to see that the transaction has taken place.&lt;/p&gt;

&lt;h1 id=&quot;heading-step-4---interacting-with-the-blockchain&quot;&gt;Step 4 - Interacting with the blockchain&lt;/h1&gt;

&lt;p&gt;Start up your server.&lt;/p&gt;

&lt;div class=&quot;highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;python blockchain.py

Running on http://127.0.0.1:5000/ (Press CTRL+C to quit)
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;Now open up postman, at the top of postman you will see a search bar. Make sure the button to the left of it is set to GET.&lt;/p&gt;

&lt;p&gt;Then type into the bar &lt;code class=&quot;highlighter-rouge&quot;&gt;http://localhost:5000/mine&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;This is what it should look like.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/postman_mine.png&quot; style=&quot;width: 1000px;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;In this image, we have now mined our blockchain, Which has created our first block.
We can see the block number index is 1 and its hash is also 1&lt;/p&gt;

&lt;p&gt;Now we can make a new transaction by changing the GET option to POST and typing in &lt;code class=&quot;highlighter-rouge&quot;&gt;http://localhost:5000/transactions/new&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;&lt;i&gt;(not that this specific blockchain will only do 1 transaction per block at a time)&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/postman_transaction.png&quot; style=&quot;width: 1000px;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;Finally, we can view the blockchain to now see the new transaction.&lt;/p&gt;

&lt;p&gt;Turn the POST command back to a GET and type in &lt;code class=&quot;highlighter-rouge&quot;&gt;http://localhost:5000/chain&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;you will be displayed with this screen.&lt;/p&gt;

&lt;p&gt;&lt;img src=&quot;/assets/images/postman_chain.png&quot; style=&quot;width: 1000px;&quot; /&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we now have a new block, It's id'd by It's index &quot;2&quot; It has now hashed the previous blocks hash and has its own proof.&lt;/p&gt;

&lt;p&gt;If you look further down, you can see our transaction, we sent an amount of 1 then there is the recipient's address and finally the sender is &quot;0&quot; to signify that this node has mined a new coin.&lt;/p&gt;

&lt;p&gt;Success!! You have now built and interacted with your very own blockchain!&lt;/p&gt;

&lt;p&gt;This lesson is based on material from &lt;a href=&quot;https://github.com/dvf/blockchain&quot;&gt;https://github.com/dvf/blockchain&lt;/a&gt; which has this licence:
&lt;a href=&quot;https://github.com/dvf/blockchain/blob/master/LICENSE&quot;&gt;https://github.com/dvf/blockchain/blob/master/LICENSE&lt;/a&gt;&lt;/p&gt;</content><author><name>dataPipeline</name></author><category term="wallets" /><category term="using" /><category term="cryptocurrencies" /><category term="bitcoin" /><category term="altcoin" /><summary type="html">In this lesson, we are going to take the code we used in building a blockchain, and turn it into an interactive API endpoint using Flask.</summary></entry><entry><title type="html">Basics of Security</title><link href="https://www.learningblockchains.com/lessons/basics_of_security/" rel="alternate" type="text/html" title="Basics of Security" /><published>2017-10-27T00:00:00+11:00</published><updated>2017-10-27T00:00:00+11:00</updated><id>https://www.learningblockchains.com/lessons/basics_of_security</id><content type="html" xml:base="https://www.learningblockchains.com/lessons/basics_of_security/">&lt;p&gt;If you are getting into bitcoin and other crypto, you'll need to get at least a basic understanding of security of your system.
As the holder of your money, it is up to you, and you alone, to properly secure your private information.
As an analogy, consider if you had a bar of gold to store your money.
You'll need to secure it, ensuring others cannot access it.&lt;/p&gt;

&lt;p&gt;At a minimum, cryptocoins have the following properties you need to consider:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;If you lose your crypto, there is no central authority to argue for your money back (as opposed to a bank, where you can often make a complaint).&lt;/li&gt;
  &lt;li&gt;If you lose your private key, then &lt;strong&gt;nobody&lt;/strong&gt; can access your crypto ever again. Thousands of bitcoins are already lost forever due to this.&lt;/li&gt;
  &lt;li&gt;If anyone has your private key, they can move your money, acting as if you did. You won't be able to prove they moved the money either.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Due to these issues, you need to take security seriously.&lt;/p&gt;

&lt;p&gt;Here is rule #1: Your &lt;strong&gt;private key&lt;/strong&gt; is &lt;strong&gt;private&lt;/strong&gt;.
In retrospect, this seems obvious, but it is worth keeping in mind.
If you save your private key to your online email, then its possible the owner of the online email can access your crypto (including any hackers who break into that email).
If you give it to a friend to keep safe, they have access to your coin.
If you use a software wallet, and there is a security issue with the software, your private key may be stolen.&lt;/p&gt;

&lt;p&gt;What can you do? Store it in a password encrypted file, remember the password, and make it a secure password.
If your password is a 24 word key, you may choose to keep the words, and their order, in different locations.&lt;/p&gt;

&lt;p&gt;Rule #2 is to keep your device protected.
If you run a mobile wallet, and get malware on your mobile, attackers may be able to access your crypto.
If you run a software wallet and you get malware, the same thing can happen.
Due to this, be very careful when you are accessing the internet or your email on a device that includes your wallet.
The best strategy is to use a different computer or device for storing your bitcoin (preferably one that never touches the internet), than you use for general internet browsing or email.
If you do download cracked software or pirated movies, ensure that you never use the same computer for storing your crypto.&lt;/p&gt;

&lt;p&gt;Rule #3 is to use encryption whereever it is possible.
Every major operating system has a method of encrypting the hard drive of your computer.
Depending on the method, you may need to enter a password when your computer starts to access your coin.
Sell or trash the hard drive without encryting it is like putting your bitcoin in a bin and hoping nobody goes through the trash.
Many people buy old hard drives from trading sites (like Craigslist or eBay) and go through them, looking for private data.&lt;/p&gt;

&lt;p&gt;Rule #4 is to follow security tips from software, and seek assistance if you do not understand a step.
If a wallet advises you to perform a specific action to secure your wallet, you should (generally) do it.
Take as many precautions as possible.
That said, if you don't understand the step, check with a local expert first.
Most cities around the world are starting to have bitcoin/crypto meetups and you can probably find someone with technical skills to help.
(Just don't pass out your private key.)&lt;/p&gt;

&lt;p&gt;Rule #5 is to take notes.
Some security measures are hard, and your setup is likely to be unique to you.
If you have a certain procedure for accessing your coin, make sure to take a note of how to do it.
Write down everything except for your private key in a procedure.
Keep your private key separate though!
The last thing you want is to not be able to access your coin because you forgot how to do it.&lt;/p&gt;

&lt;p&gt;Remember, if you get hacked or your private key is leaked, there is no method to claim back your lost crypto, there are no central authorities to complain to, and it would also be quite hard to prove who actually stole your coin.
It's lost.&lt;/p&gt;

&lt;p&gt;Take your security seriously&lt;/p&gt;</content><author><name>dataPipeline</name></author><category term="security" /><category term="wallets" /><category term="using" /><category term="cryptocurrencies" /><summary type="html">If you are getting into bitcoin and other crypto, you'll need to get at least a basic understanding of security of your system. As the holder of your money, it is up to you, and you alone, to properly secure your private information. As an analogy, consider if you had a bar of gold to store your money. You'll need to secure it, ensuring others cannot access it.</summary></entry><entry><title type="html">Building a blockchain</title><link href="https://www.learningblockchains.com/lessons/Building_blockchain/" rel="alternate" type="text/html" title="Building a blockchain" /><published>2017-10-26T00:00:00+11:00</published><updated>2017-10-26T00:00:00+11:00</updated><id>https://www.learningblockchains.com/lessons/Building_blockchain</id><content type="html" xml:base="https://www.learningblockchains.com/lessons/Building_blockchain/">&lt;p&gt;In this tutorial, I am going to show you how to create your own basic blockchain.&lt;/p&gt;

&lt;p&gt;&lt;i&gt;If you haven't already checked out our lesson on &lt;a href=&quot;/lessons/blockchain_basics/&quot;&gt;blockchain basics&lt;/a&gt;, I recommend you do that before this tutorial&lt;/i&gt;&lt;/p&gt;

&lt;p&gt;This tutorial is aimed at people with at least some knowledge in Python.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Things we need:&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;You will need to have Python 3.6 or higher along with pip installed.
To install Python click &lt;a href=&quot;https://www.python.org/downloads/&quot;&gt;here&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We are going to create this blockchain in 3 steps.&lt;/p&gt;

&lt;ol&gt;
  &lt;li&gt;Building a blueprint for the blockchain&lt;/li&gt;
  &lt;li&gt;Adding a new transaction to the blockchain&lt;/li&gt;
  &lt;li&gt;Creating new blocks&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Let's get building!&lt;/p&gt;

&lt;h1 id=&quot;heading-step-1---building-a-blueprint-for-the-blockchain&quot;&gt;Step 1 - Building a blueprint for the blockchain&lt;/h1&gt;

&lt;p&gt;Open your favorite text editor or IDE, I personally use Pycharm but any editor will do.&lt;/p&gt;

&lt;p&gt;Create a new Python file and save it as &lt;code class=&quot;highlighter-rouge&quot;&gt;blockchain.py&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Below we will create our blockchain blueprint.&lt;/p&gt;

&lt;p&gt;The first step is to create a blockchain class, this class will be an empty list in which we will store our blockchain in.&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;k&quot;&gt;class&lt;/span&gt; &lt;span class=&quot;nc&quot;&gt;Blockchain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;object&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;__init__&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chain&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_transactions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new_transaction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;# Adds a new transaction to the list of transactions&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;c&quot;&gt;# Creates a new Block and adds it to the chain&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;

    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;proof_of_work&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;():&lt;/span&gt;
       &lt;span class=&quot;c&quot;&gt;#  Simple Proof of Work Algorithm&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;


     &lt;span class=&quot;nd&quot;&gt;@property&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;last_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt;


    &lt;span class=&quot;nd&quot;&gt;@staticmethod&lt;/span&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;):&lt;/span&gt;
    &lt;span class=&quot;c&quot;&gt;# creates a SHA-256 hash of a block&lt;/span&gt;
       &lt;span class=&quot;k&quot;&gt;pass&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;As you see above  most of the defs have a pass in them, Everything we do in this Tutorial will be based on this blueprint,
so as we move through the tutorial you will just have to take the new code and replace the PASS section within the corresponding def.&lt;/p&gt;

&lt;h1 id=&quot;heading-step-2---adding-a-new-transaction-to-the-blockchain&quot;&gt;Step 2 - Adding a new transaction to the blockchain&lt;/h1&gt;

&lt;p&gt;We need a way of adding transactions to a Block. Our &lt;code class=&quot;highlighter-rouge&quot;&gt;new_transaction()&lt;/code&gt; method is responsible for this:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;
    &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new_transaction&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recipient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        Creates a new transaction to go into the next mined Block
        :param sender: Address of the Sender
        :param recipient: Address of the Recipient
        :param amount: Amount
        :return: The index of the Block that will hold this transaction
        &quot;&quot;&quot;&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_transactions&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;({&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'sender'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;sender&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'recipient'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;recipient&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'amount'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;amount&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;})&lt;/span&gt;

        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;last_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;s&quot;&gt;'index'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;
&lt;p&gt;After &lt;code class=&quot;highlighter-rouge&quot;&gt;new_transaction()&lt;/code&gt; adds a transaction to the list,
it returns the index of the block which then sends the new transaction to the block, and will be the next one to be mined.
This will be useful later on, to the user submitting the transaction.&lt;/p&gt;

&lt;h1 id=&quot;heading-step-3---creating-new-blocks&quot;&gt;Step 3 - Creating new blocks&lt;/h1&gt;

&lt;p&gt;When our Blockchain is instantiated we’ll need to seed it with a genesis block with no predecessors.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A genesis block is the first block of a block chain&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;We’ll also need to add a “proof” to our genesis block which is the result of mining (or proof of work). We’ll talk more about mining and proof
of work shortly.&lt;/p&gt;

&lt;p&gt;Below we will create the new genesis block:&lt;/p&gt;

&lt;div class=&quot;language-python highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt; &lt;span class=&quot;k&quot;&gt;def&lt;/span&gt; &lt;span class=&quot;nf&quot;&gt;new_block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;int&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous_hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Optional&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;])&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;-&amp;gt;&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Dict&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;str&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;Any&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]:&lt;/span&gt;
        &lt;span class=&quot;s&quot;&gt;&quot;&quot;&quot;
        Create a new Block in the Blockchain
        :param proof: The proof given by the Proof of Work algorithm
        :param previous_hash: Hash of previous Block
        :return: New Block
        &quot;&quot;&quot;&lt;/span&gt;

        &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'index'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;nb&quot;&gt;len&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;+&lt;/span&gt; &lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'timestamp'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;time&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(),&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'transactions'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_transactions&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'proof'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;proof&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;
            &lt;span class=&quot;s&quot;&gt;'previous_hash'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;previous_hash&lt;/span&gt; &lt;span class=&quot;ow&quot;&gt;or&lt;/span&gt; &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;nb&quot;&gt;hash&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chain&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;-&lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;1&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;]),&lt;/span&gt;
        &lt;span class=&quot;p&quot;&gt;}&lt;/span&gt;

        &lt;span class=&quot;c&quot;&gt;# Reset the current list of transactions&lt;/span&gt;
        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;current_transactions&lt;/span&gt; &lt;span class=&quot;o&quot;&gt;=&lt;/span&gt; &lt;span class=&quot;p&quot;&gt;[]&lt;/span&gt;

        &lt;span class=&quot;bp&quot;&gt;self&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;chain&lt;/span&gt;&lt;span class=&quot;o&quot;&gt;.&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;append&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;(&lt;/span&gt;&lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;)&lt;/span&gt;
        &lt;span class=&quot;k&quot;&gt;return&lt;/span&gt; &lt;span class=&quot;n&quot;&gt;block&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;&lt;strong&gt;What does a block look like?&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Each Block has an&lt;/p&gt;
&lt;ul&gt;
  &lt;li&gt;Index&lt;/li&gt;
  &lt;li&gt;Timestamp (in Unix time)&lt;/li&gt;
  &lt;li&gt;List of transactions&lt;/li&gt;
  &lt;li&gt;Proof&lt;/li&gt;
  &lt;li&gt;A hash of the previous Block&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here’s an example of what a single Block looks like:&lt;/p&gt;

&lt;div class=&quot;language-json highlighter-rouge&quot;&gt;&lt;pre class=&quot;highlight&quot;&gt;&lt;code&gt;&lt;span class=&quot;err&quot;&gt;block&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;=&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;p&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'index':&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;1,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'timestamp':&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;1506057125.900785,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'transactions':&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;[&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;{&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'sender':&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;nt&quot;&gt;&quot;8527147fe1f5426f9dd545de4b27ee00&quot;&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'recipient'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;a77f5cdfa2934df3954a5c7c7da5df1f&quot;&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
            &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'amount'&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;:&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;5&lt;/span&gt;&lt;span class=&quot;p&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
        &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;],&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'proof':&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;mi&quot;&gt;324984774000&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;,&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
    &lt;/span&gt;&lt;span class=&quot;err&quot;&gt;'previous_hash':&lt;/span&gt;&lt;span class=&quot;w&quot;&gt; &lt;/span&gt;&lt;span class=&quot;s2&quot;&gt;&quot;2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824&quot;&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;span class=&quot;err&quot;&gt;}&lt;/span&gt;&lt;span class=&quot;w&quot;&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;
&lt;/div&gt;

&lt;p&gt;We have now created our very own basic blockchain! In the next lesson you will learn how to mine the blockchain and have hands on interactions with your blockchain.&lt;/p&gt;

&lt;p&gt;Click  &lt;a href=&quot;/lessons/interacting-with-the-blockchain/&quot;&gt;here for the next lesson.&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;This lesson is based on material from &lt;a href=&quot;https://github.com/dvf/blockchain&quot;&gt;https://github.com/dvf/blockchain&lt;/a&gt; which has this licence:
&lt;a href=&quot;https://github.com/dvf/blockchain/blob/master/LICENSE&quot;&gt;https://github.com/dvf/blockchain/blob/master/LICENSE&lt;/a&gt;&lt;/p&gt;</content><author><name>dataPipeline</name></author><category term="wallets" /><category term="using" /><category term="cryptocurrencies" /><category term="bitcoin" /><category term="altcoin" /><summary type="html">In this tutorial, I am going to show you how to create your own basic blockchain.</summary></entry><entry><title type="html">Wallets</title><link href="https://www.learningblockchains.com/lessons/wallets/" rel="alternate" type="text/html" title="Wallets" /><published>2017-10-18T00:00:00+11:00</published><updated>2017-10-18T00:00:00+11:00</updated><id>https://www.learningblockchains.com/lessons/wallets</id><content type="html" xml:base="https://www.learningblockchains.com/lessons/wallets/">&lt;p&gt;To use cryptocurrencies, you'll need a wallet to store them in and transact with the blockchain.
In our algorithm track, we will go through the algorithms for interacting with cryptocurrencies, but most of the time you want to just be able to spend and receive coins in an easy-to-use manner.
This is where wallets come in.&lt;/p&gt;

&lt;p&gt;Wallets are programs that contain information on using and transacting for different cryptocurrencies.
Some wallets support more than one crypto, while others are specific to just one coin.
Additionally, some crypto will be supported by multiple wallets, while others will be supported only on a single wallet.&lt;/p&gt;

&lt;p&gt;Further, there are many types of wallets:&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;Software wallets, that you run on your computer&lt;/li&gt;
  &lt;li&gt;Mobile wallets, like software wallets but are run on your mobile device&lt;/li&gt;
  &lt;li&gt;Hardware wallets, with USB-like sticks that store your coins on them&lt;/li&gt;
  &lt;li&gt;Paper wallets, that print out the relevant information onto a piece of paper&lt;/li&gt;
  &lt;li&gt;Online wallets, cloud-based software wallets&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;There are additional options, but that's the standard list most people would use.&lt;/p&gt;

&lt;p&gt;Going through the above list, you might be unsure about what it means for a wallet to &quot;store&quot; your coins.
In essence, all the wallet really stores is your private key.
With that key, however, the wallet is able to make transactions against &quot;your&quot; coins on the blockchain.
Think of it like a key to a deposit box at a bank - the bank actually has your things in a box on-site, but you hold the key to get into that box.
The blockchain stores your coins, but you hold the key to actually use them.&lt;/p&gt;

&lt;p&gt;As an example, when you use a paper wallet, you are really just printing out your private key.
We will cover paper wallets in a later lesson, including how to spend from a paper wallet.&lt;/p&gt;

&lt;p&gt;We like the following wallets, although we should point out that this doesn't mean they do, or might, have issues.&lt;/p&gt;

&lt;ul&gt;
  &lt;li&gt;&lt;a href=&quot;https://coinomi.com/&quot;&gt;Coinomi&lt;/a&gt;, a mobile-wallet for Android (and iOS coming soon)&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.exodus.io/&quot;&gt;Exodus&lt;/a&gt;, a software wallet for your computers desktop&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://ark.io/&quot;&gt;ARK wallet&lt;/a&gt;, a software wallet for your computers desktop specifically for ARK&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://www.myetherwallet.com/&quot;&gt;MyEtherWallet&lt;/a&gt;, a web browser based wallet for Ethereum&lt;/li&gt;
  &lt;li&gt;&lt;a href=&quot;https://trezor.io/&quot;&gt; Trezor&lt;/a&gt;, a hardware wallet that plugs into your pc via USB&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;When using a wallet, you need to be quite careful to follow the instructions on how to use and secure the wallet.
If you lose your private key or password to the wallet, &lt;em&gt;nobody&lt;/em&gt;, not even the wallet software's creators, can recover your password or your coins.
Follow the instructions closely, and keep an eye out for a new lesson from us on best-practice security.&lt;/p&gt;</content><author><name>dataPipeline</name></author><category term="wallets" /><category term="using" /><category term="cryptocurrencies" /><category term="bitcoin" /><category term="altcoin" /><summary type="html">To use cryptocurrencies, you'll need a wallet to store them in and transact with the blockchain. In our algorithm track, we will go through the algorithms for interacting with cryptocurrencies, but most of the time you want to just be able to spend and receive coins in an easy-to-use manner. This is where wallets come in.</summary></entry></feed>