image
Hosted Blockchain Nodes (MetaMask)
Course Introduction
Difficulty
Intermediate
Duration
1h 12m
Students
22
Ratings
3.6/5
Description

This course explores MetaMask which allows you to access the Ethereum blockchain. We will be looking at what MetaMask is, how it works with Truffle, incorporating the HTML and Javascript code from a previous course, and then finally you will see all the components of the game coming together as we actually play it.

Transcript

In this lecture, before we actually start installing MetaMask, I want to talk about where MetaMask exactly fits into this whole ecosystem, where we talk about storage and access methods of the data that is stored on the blockchain. Now, when we are talking about a blockchain, then usually we talk about something like this structure, where we have a couple of nodes and the nodes talk to each other, and they basically save the same information on every node. They all have a copy of the exact same blockchain, but somehow we need to get access to this. And we saw this before with our Ganache, where we said we are talking about an HDB provider or WebSockets provider and get access to an RPC interface.

Now what we also saw in Ganache is that it stores or gives us 10 accounts, and so it has these 10 private keys to these 10 accounts saved directly in Ganache, and we can send-off transactions, and they internally get signed and Ganache is adding this to its own blockchain. Now where does Ganache fit in here? That is something that you will learn in a second. Before we talk about that, I want to talk about these five pillars here, where we have storage versus access methods to the data that is stored on the blockchain. So, on the one hand, we have a different storage location. We have tons of different blockchains. We have one main net, which is the blockchain.

The Ethereum blockchain, where the ethos cost real money. Then we have test networks like Ropsten, Rinkeby, Kovan, and so on. There you have the exact same way of accessing the method, accessing the blockchain. The same method but the data is different. So, it's like having a different database. Let's say, you take your bank database, you clone it, but obviously, the bank will only recognize its own database in its own basement. But if you have the same database system, then you have the same way of accessing the data. You also have private networks and in addition to that, you have a special kind of private network, which is the developer network which we used with Ganache. Now Ganache is only one of these tools. This blockchain nodes which can access the data. In addition to Ganache, there is a plethora of tools and the most famous ones that I know are Geth, Pantheon, and Parity. So, if you hear of those, then you hear of the tool that can do the exact same thing. It can access blockchain data; it's just a different implementation. So, Geth would be an implementation in Go. Pantheon would be the exact same tool  doing the exact same thing in Java.

Parity would be written in Rust, and why is that? Because they wanted to have a redundance of the tools. Ethereum, the developers or the researchers are actually just publishing the Ethereum yellow paper. So, they want to have a way of redundant developers programming tools based on that Ethereum yellow paper. Now in addition to that, some of these tools, they talk to each other and some not. So, for example, with Geth, you can connect  to other blockchain nodes. With Ganache, you cannot. Ganache is just a developer blockchain that is running locally on the developer machine, and it doesn't store anything persistently, which is also one of the things that you want to keep in mind. Ganache is not a real blockchain node. It is simulating a real blockchain node, but it's not a real blockchain node. So, if you are working with Ganache, then you maybe want to try, in between deploying something like a private network or deploying your depth on a test network like Ropsten and see if it's still working. And we are going to deploy our smart contract on the Ropsten test network and see if it's still working or what we have to modify in order to make it work again.

And then there is the important part for our MetaMask. Where are the keys stored? The keys are the most important thing in order to gain access to the blockchain, and write something to the blockchain. So, if you have the private keys, then you have access to the underlying accounts. And this is where MetaMask really comes in, and what makes it really easy to access the blockchain from a user perspective using your smart contracts. The first thing is there are different networks. I've said before, there is the main-net; there the ethos cost real money, but it's not the same as any of the test-networks, and the test-networks are separate databases, basically. So, be aware of that when you connect to Ropsten  and you deploy something there and then you connect to another network, then you are having a different database, basically. But then there are different ways of connecting to those networks, and this is where it gets interesting. So, the first one and this is something I rarely do anymore is a local node. If you are downloading Geth, Go Ethereum, and you would start it, it would start downloading all the blocks from the main-net except you provided with the right parameters. If you just start Geth, and it will start downloading the blocks, and it would start something called block synchronization. So, it would have to download every data from the beginning on basically in order to know the current state of the blockchain  because  this is how the blockchain works.

You send-off a transaction. The transaction gets mined; it gets baked into a block, the block is sealed, the block is sent off to the network and then distributed across the nodes. So, if you start with a new node, you cannot just download the last block. You need to have all the blocks in order to get all the information and fill up your local database. And that is very tedious. First of all, it takes time. It takes a lot of processing power. It takes a lot of storage, and it takes a lot of bandwidth. And it's also very tedious to keep the blockchain up and running because we have protocol updates like recently Constantinople was published; it's a protocol update, and that means if you are not updating your local node, then your node will not be compatible to the rest of the network.

So, you have to keep your node updated. You have to keep your node synchronized. And every time you shut down your computer and you restart it a week later, you have to start synchronizing again from the point where you left off and every around 10-20 seconds, probably 14-15 seconds on average, there is a new product generated. So, it's kind of not the thing that I want if I just want to develop taps, and there is a better way to do that. That is Hosted Nodes. And this is where services like Infura come in. Infura is a company which makes it their mission to host these nodes. And all you really need to do is you need to access one of those nodes in order to gain access to the rest of the network because those nodes have a copy of the blockchain. And if you want to send-off something, if you want to do a writing transaction, then that transaction has to be signed. Now the question is where are the keys to sign a transaction? So, this is the most important thing that one single question you have to ask is always who has the keys?  And obviously, you cannot save those keys; those very precious keys on the Infura servers. You have to keep them locally on your computer, and this is where MetaMask really comes in. MetaMask stores the keys on your computer and makes it easy to manage those keys, but more than that in a second. The last method or the last storage method is developer network. And the developer network, just to highlight this again, is nothing to do with any of the blockchains.

It's really just a sealed network running on your computer. It's like having a local my sequel database running or you just open a local chase and fell and write something inside there and don't share it with anyone. So, this is really has nothing to do with outside or distributed data at all. It's just the same way of accessing the data. You have an RPC or WebSocket interface, and you can access the data stored on your machine. Back again, question is who can access?  And it's usually about writing access and then the question is who has the key? The one who has the private key can write transaction in the name of the account that is generated by the private key. And MetaMask comes in as a keystore. If you send-off a transaction inside your browser and there is MetaMask installed, then MetaMask will intercept the sentTransaction, RPC call, and it will have a little pop-up, and then it will ask you to confirm. And then if you hit confirm, then it will sign the transaction with a private key that is stored in MetaMask. So, the difference to this is in something like Geth, Go Ethereum, or Ganache, we have the keystore already integrated. And if we're working with Ganache  and we sent-off transaction, then Ganache will take this transaction, will sign the transaction, and then itself send it off to its own blockchain.

The same with Geth. Geth has a keystore integrated and it will sign the transaction when you want it. On the other hand, there are also Hardware Wallets, and they want to highlight this over here as well. Hardware wallets have the very benefit of never giving access anyone to the private keys. So, all they do is they sign transactions with the private key that is stored on the hardware element, but there's no way you can read out the private key. So, you send inside the hardware wallet and unsigned transaction, the hardware wallet will give you a little prompt to press a button and when you press the button, the hardware wallet will send back the signed transaction, but you will never gain access to the private key and that way, it is extremely secure and extremely convenient to carry around to have a hardware wallet. But that's another story. Back to MetaMask. If we were to interact with a smart contract then internally, Truffle contract or web3, if you do anything from set a stone, create a new game, join the game internally, it will use this web3.eth.sendTransaction to send-off a transaction with the right data field. And this will be intercepted from MetaMask. MetaMask will show you this pop-up and forward the transaction to Infura or to a local custom blockchain node like Ganache, and this is what we are going to use. Let me highlight this with hardware wallets. Hardware wallets will also intercept this sentTransaction call, and it will sent to the hardware wallet, and then you have to press a physical button and the hardware element signs the transaction without revealing the private key.

Very important difference and very good to keep in mind. A MetaMask has an implementation to connect to a hardware wallet. So, if you want to have it really secure, you might want to look into hardware wallets. Now because we have talked of clients, let me give you a little comparison of the clients now that we're talking about them. We were working mostly with Ganache and the Remix Virtual Machine. Those two are having this proof of authority network usually. So, in Remix, if you send-off a transaction, it's immediately mined; you don't have to wait. In Ganache, you can set the block mining time to 3, 5, 15, 20 seconds in order to simulate proof of work. The thing is what you can do in Ganache is you can run a single node, and you can connect to the single node from outside, both with HDB, RPC and WebSockets. And with remix, you're really just encapsulated in your little browser and that's it. But both are not really persistently storing anything, and you cannot connect to other nodes. So, this is why we are going to sign up with Infura. We are getting us an hosted with Geth or Parity node, and then we can also send transactions to a real network, and we can persistently store them there, and we don't have to worry about setting up our own blockchain nodes instead we're just using them.

And the summary: data access and storage methods vary by clients so it really matters which client are you using and how are you configuring your client. The two questions that are the most important are who has the keys?  Where are they stored? And how are you signing the transactions? And the second one is where is the data stored when you send-off the transaction? To which network are you connected to? Are you connected to your private network on your machine? Are you connected to the main network? Are you connected to Ropsten or connected to Kovan? And so on. So, these are the two questions that you have to always pay special attention to. MetaMask makes it easy to store these keys and to manage those keys. So, if you use MetaMask, it will attach itself to the Window object of the browser. It's really just a browser plug in and it will intercept the sentTransaction calls and sign it with its own private keys. It is that at the time it was released, there was nothing yet like MetaMask, and it's an extremely large user experience gain if MetaMask is in use. So, for us and for any user you want to onboard, probably the way forward is with something like MetaMask or MetaMask itself. And this is why we are going to use it, and this is why we are going to install it right in the next lecture.

 

About the Author
Students
90
Courses
7

Tom is a CTO, senior back-end developer, and systems architect with over twenty years of hands-on development experience in a variety of languages and systems. He has a CS master's degree and has been working with Ethereum and blockchain technologies since 2016.