So far, we interacted with Ganache and with a private node, either through
Ganache's UI, or through a command-line interface.
Later on, we'll demonstrate how to combine smart contracts and a web
interface into a decentralized application, or Dapp, that people will be
able to use through a special kind of web browser called a Dapp browser. To put
it simply, a Dapp browser is a web browser connected to an Ethereum node, that can
manage a set of keys. There are not a lot of such Dapp browsers on the market yet.
The most common options are Mist, that is developed by the Ethereum Foundation, and
which is more of a Geth node with a web rendering engine on top, and MetaMask,
which works as a browser extension inside of Chrome or Firefox and adds an
Ethereum connection to Chrome through a remote Ethereum node called
Infura. Both Mist and MetaMask are still early beta software and very
unstable and buggy at times. But MetaMask clearly has the most practical
features for Dapp developers for now. So in this course, we'll use MetaMask as
our Dapp browser. Just a word of warning: again, it still has a lot of bugs and we
had to find some workarounds until those bugs are fixed. But whenever necessary,
we'll explain those workarounds. Another note: in this course, we are going to show
you how to install MetaMask on a Mac, but if you are using Chrome or Firefox
on Linux or on Windows, the procedure should be exactly the same, with a small
difference on Linux that I'll show you later. MetaMask exists both as a Firefox
add-on and as a Chrome extension, but in this course we'll use the Chrome
extension, because Chrome is the most popular browser these days. To install
the Chrome extension, we have to go to the MetaMask website on "metamask.io".
The link will be in the resources of this lecture. And then click this "get
chrome extension" button here. You can add the extension to your browser,
"add extension", and that's it. By the way, you can watch this video that introduces
how MetaMask works. And now the MetaMask extension is available here with
this fox icon. So the first thing we need to do is to initialize the vault. MetaMask
uses the vault concept to contain the keys that correspond to your
accounts. So here we need to initialize the vault, and to do that we have to
accept the privacy notice first, "accept". And then accept the Terms of Use as well,
but we can only accept terms of use when we have scrolled down to the bottom. And
we accept. Now again MetaMask uses a software vault to store your private
keys inside the browser extension and keep them safe while handling the
signature of your transactions before they are sent to the node you are
connecting to. It can also connect to public networks, main net, test nets and
so on, as well as private networks running on your own machine through
their RPC API. This vault is protected with a global password that is not
stored anywhere online, so you need to keep your password very safe and also
store a list of 12 words called a seed phrase, that you can use to recover your
vault if you forgot your password. Now to initialize the MetaMask vault
there are basically two possibilities: you can create a new MetaMask vault
from scratch and it will create a random seed phrase, or you can restore a vault
from an existing seed phrase. Whatever the option you choose, you can always
import accounts created somewhere else using their private key, on top of those
created inside MetaMask. We are going to use Ganache a lot for development in
this course, and Ganache generates 10 accounts by default. And those accounts
will always be identical if you keep the default settings. Those accounts are
generated based on the following seed phrase: "candy maple cake sugar pudding
cream honey rich smooth crumble sweet treat".
Now what is very important is that you do not, I repeat, you DO NOT, under any
circumstance, use this seed phrase in the "import existing DEN" functionality in
MetaMask. It will work, but then the private keys of all the accounts you
create in this vault are exposed, because anyone can regenerate the exact same
accounts and private keys based on this seed phrase that anybody can find.
It's the default one in Ganache. Now unfortunately, MetaMask doesn't keep
isolated vaults for each network, so the best option here is to use the "create"
button in this screen in order to create a random vault with a random seed, and
then we will import the Ganache accounts based on their private keys. Also once we
have them imported, we will clearly mark them not to use them on the main net. Now
if you already have generated a MetaMask vault before, you can keep it, you can
skip the vault creation step, and import Ganache accounts in it afterwards. But
here, we'll show you how to create your vault from scratch. So here, what you can
do is type your very secure password twice, and then hit "create". Then
it shows you the seed phrase that it generated randomly, and this seed phrase
is very important because that's the one that you can use then to regenerate the
accounts that have been created inside of MetaMask. And if you use that vault
to store some real Ether, you have to keep this phrase very secure. Now of
course, I'm not showing this phrase here, because then you would be able to
recreate the same accounts as me and empty them of their Ether afterwards. So
make sure you copy that phrase somewhere safe, and then you can click the "I've copied
it somewhere safe" button. Just a tip: what I generally do is store this seed phrase
inside my password manager. I'm using 1Password, but maybe you are using
LastPass or something like that. These are tools where you can store
information in an encrypted fashion, so that's a good
way to do it. So at this stage, MetaMask will create a first account in
my vault, and then we can import the 10 accounts that are generated by Ganache.
To do that, we need the private key of each account, and Ganache makes it
available through the "key" button at the end of each account's line, in the
"accounts" tab. So now let's start Ganache, and here we can see the 10 accounts
that have been generated. So let's click the button at the end of the first line,
for first account, and then we copy this value. This is the private key. Then
we go back to Chrome, we open the MetaMask extension, and then we will switch
network. So here you have a button where you can switch network, and here you have
a button where you can switch accounts. So here we switch to the network, and if
you remember, our Ganache network is running on port 7545, which is not a
network option here yet. We can use the "Custom RPC", type
"http://localhost:7545" and hit "save". Then I go back with the left arrow here, and now
I'm connected to my private network. This will be useful in a moment. Now that I'm
on the private network, I can use the "import" command that is in the account
menu here, with this little guy here. So I click this, and I use the "Import account"
menu item. There I can choose between 2 types of
imports: either "Private key" or "JSON file". "JSON file" will be useful later when we
import accounts from the private node. Here we'll use the "Private key" one, and
there we can paste the value that we copied from Ganache, and we hit "Import".
And as you can see, the balance of this account is 100 Ether. What we can do here
is rename the key account right away into "Ganache 1", because it's the first
account on Ganache, and this will also serve as a warning that this is an
account generated in Ganache, and we should not use this account on the main
net. And we save. And then we will repeat this process for the other 9 accounts.
So going back to Ganache, account number 2, we copy the private key, go back to
Chrome, "import account", we paste the private key, we import, and we rename the
account to "Ganache 2".
And that's it: at this point, we imported all the 10 accounts generated by Ganache,
and we can see their balance in MetaMask. If at any point during this
process, you have a problem seeing your network, seeing the right balance, or if
you have a spinning wheel that doesn't stop, know that the MetaMask extension
is very fragile when it comes to synchronization and switching between
networks. One thing that can sometimes solve a lot of issues is to try and
switch to the main net, and then back to your own private network, and this will
put everything back in sync, and you should see the right thing. If that
doesn't work, another trick is to go to
"chrome://extensions", and to disable, not remove,
disable, and re-enable the extension. Then you have to unlock the vault again, and
normally it should have everything in sync. So if I switch to account "Ganache 1"
for example, I can see now the right balance. If after all that, you still
can't see your accounts, your best bet is to contact the MetaMask team directly
via their Slack channel. It's the best option we found so far. So you can
register to their Slack by going to this URL, and then you can ask the
question directly. But first do all the steps that we just described, make sure
that Ganache is properly running, that it's on the right port, that you are
connecting to the right network, and that you tried the "switching to the main
network" and "disabling the extension" tricks before. So as we showed, you can
rename accounts by hovering over the name, and then clicking "edit".
Unfortunately you cannot remove an account for now in MetaMask, so you will
have long lists of accounts in the "switch account" list. But for each account,
you can also use this contextual menu to
view the account on Etherscan. By the way this is only available for
accounts on the main net or on public test chains. You can also show a QR code
that contains... that encodes the address of the account. You can also copy the
address of the account to clipboard and export the private key.
Now that we've imported all the accounts from Ganache, let's do the same with the
accounts that we created in the private node. So the first thing we'll do is
start our private node. So we can start it at the same time as Ganache, it
doesn't matter, because they run on different ports. But still we'll just
quit Ganache, and then start a Terminal, then you go to the
"ChainSkills/private" directory in your user home directory, whatever the system
you're using, and you can run "startnode.sh". And of course on Windows it's not
".sh", it's "startnode.cmd". So again, always check for the information
where the node is started, and where it opens the various endpoints. So here we can
see that the HTTP endpoint is successfully opened on port 8545, and
mining has started. So now we can go back to Chrome, and we can switch to another
network. It will be another private network, and this will be the
"localhost 8545" one that's already in the menu here. So you can click that and here you see
that it's showing us the balance of this address on the private node, which is
0. And here, what we'll need to do is to import the private keys for these
accounts. But we need to import them in the right order. So to do that, we will go
back to the Terminal again, open a new Terminal tab, and here, assuming that our
IPC endpoint was opened in the right place, which
is... "IPC endpoint opened in "Library/Ethereum/geth.ipc", so that's the default
location for MacOS. Of course it's different from for Linux, and for Windows
you don't care. And there you should be able to run "geth attach", and it will
attach to the IPC endpoint, and that is correct. It's connecting to the
"ChainSkills/private". If you left Ganache running, you might have a different
version here, so just get quit Ganache and run only your private node
to do that. So here as we did before, we can list the accounts in order and we
can see that this is the index #0, index #1, index #2. That will be useful information
now. So let's import the coinbase account, the
first one into MetaMask. So that's the account that starts with "0x1cf0", so we go
back to the browser, and we open the extension. Here we will use the "Import
account" functionality again, and instead of "private key" will select the "JSON file".
Then we can click the "Choose file" button to go and look for this file on our
machine. On MacOS, it will be under my user directory, inside of
"ChainSkills/private/keystore", and here we have the 3 files, one for each key. And if you
remember, the coinbase account is "0x1cf0", so that's this one here. This corresponds
to the first account that we want to import. So you just double click that to
select that file, and you enter the password which is "pass1234". And
you import, and it takes a few seconds, you just have to wait for it to do the
import. By the way, if you're on Linux, you might
have an issue here, just a second, I will explain. Here I can rename this to
"Private 1" to indicate it is the first account on the private network.
So let's import the other 2 files now.
So "import account", "JSON file". Sorry, I forgot to look at... so that's the "0x46f8"
account. So again, I import the second account, "JSON file", "choose file" and
that's the "0x46f8", that's the second one. Type the password for this account...
"pass1234"... and then "import". Again, it takes a
few seconds and here we can see the 10 ethers that we transferred from the
coinbase account in the previous lecture. So here I will rename this
account to "private 2", and do the same for account number 3. And I rename it
to "private 3". So when you try to import those accounts on Linux for
example, you might have an issue because the dialog here to import the accounts
will just disappear on you when you select the file. If that happens,
that is a known issue with MetaMask on Linux, and actually on Firefox also on
some other systems, and it's documented in the MetaMask FAQ. So if you go to
"github.com/metamask/faq" - the link will be in the resources of this lecture -
you can see at the bottom of the page here, the
solution to that problem. So for Google Chrome, you need to type this entire
address here "chrome-extension://" and then this very weird URL.
You can type that in the address bar, hit [Enter], and then what it does is open the
Chrome dialog but in a full tab, so that it doesn't disappear when you use
this "import account" feature. And then it should work okay.
And you can do the same trick with the other URL here,
with "moz-extensions", and that should solve the issue you might have on linux
or on Firefox. Now let's just check balances. If I'm still on the private
network... ok... I check "private 1", I can see the balance corresponding to the
coinbase account always increasing. "private 2" has 10 Ether, and "private 3"
has 10 Ether as well. So now we should have the 10 accounts that we
imported from Ganache, and the 3 accounts that we imported from our
private Geth node. And also we should have the "Account 1" here, that was
created with the vault in MetaMask directly. And now of course, if you switch
to another network, let's take for example our coinbase account, our
"private 1" account, and with that same account selected, we switch to the main
Ethereum network, our balance becomes 0. Sorry you cannot use your fake Ether
on the real network, and that's a good thing. In the same way, you cannot use
your real Ether on a private network either. Don't forget that if you want to
export let's say the account that was generated by MetaMask, you can always
use this contextual menu and export the private key, and keep that in a safe
place, so that you can reimport it later on.
Again if you need some help with MetaMask, we can certainly help in the Q&A
section of this course. But generally the best option is to go to the MetaMask
Slack. And again the link to the MetaMask Slack will be in the resources of
this lecture, and it appears on screen now. So don't forget
to always back up the private key of your accounts on the main net, especially
after you start storing real Ether in them, because your private key is all you
have and all you need to access your cryptocurrency wallet. Now it's time to
learn about smart contracts and how to create them. So in the next lecture we
will have an introduction to Solidity and to the tools that you can use to
compile it.
Không có nhận xét nào:
Đăng nhận xét