-
Notifications
You must be signed in to change notification settings - Fork 29
/
08-crypto-wallet-setup.html
235 lines (183 loc) · 7.45 KB
/
08-crypto-wallet-setup.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
<!DOCTYPE html>
<link rel="stylesheet" href="boilerplate/style.css" />
<script type="module" src="boilerplate/editor.js"></script>
<title>Web3 From Zero - Lesson 8</title>
<h1>8. Crypto Wallet Setup</h1>
<p>
We know how public key cryptography works, even if just in essence. We know that a EOA is a
blockchain address that is controlled by the holder of a related public/private key pair. But how
come crypto wallets into play?
</p>
<h2>What is a Crypto Wallet?</h2>
<p>
A crypto wallet, or simply wallet, is a mechanism to manage your keys, some are even able to
manage your blockchain connections.
</p>
<p>
There are different types of wallets, so let's get a small overview on the essentials.
</p>
<h3>1. Wallet App</h3>
<p>
A wallet app is a dedicated application that manages your keys and blockchain connections. Some
can connect to different types of blockchains, some only understand one specific blockchain
network.
</p>
<p>
Since a wallet app is software, it comes with the danger of being hacked if it's installed on a
device that has an internet connection.
</p>
<p>
MetaMask is probably the most popular wallet app for the Ethereum network. You can enter your keys
into it, or even let it generate keys for your with a seed phrase. It allows you to add and remove
RPCs for different chains, and comes preconfigured with Ethereum mainnet and it's test networks.
</p>
<p>
To connect a wallet app with a Web3 app, you either need a browser extension that links the wallet
you installed on your PC or Mac with the browser or, if you're using the mobile version, you need
to scan a QR code displayed on the Web3 app.
</p>
<h3>2. Paper Wallet</h3>
<p>
A paper wallet is simply a piece of paper with all your keys, addresses, or your seed phrase
written down. It can't do much, since it's not software, but at least you don't have to remember
all this information on your own.
</p>
<p>
With a paper wallet you can't send any tokens, but if you tell people one of the addresses you
written on it, you can at least receive them.
</p>
<p>
Also, it can't get hacked online, because it can't be connected to the internet. This makes
paper wallet quite safe. Depending on the material of the wallet and the risk of burglaries in
your location, it can be susceptible to other problems. Also, like everything else, you can simply
lose it.
</p>
<h3>3. Hardware Wallets</h3>
<p>
Hardware wallets are devices that store your keys, addresses, and seed phrases. They keep your
private keys always on the device and never make them public. If you need to sign a message with a
private key, you have to send the message to the wallet and it will respond with the signature.
</p>
<p>
Hardware wallets have standardized interfaces and work in tandem with wallet apps. So, you can see
them as an extension of a wallet app that makes it more secure.
</p>
<h3>4. Programmatic Wallet</h3>
<p>
Since all these digital wallets talk to each other and DApps via standardized interfaces, it might
not come as a surprise that there is an API for this.
</p>
<p>
What a software library is to an app, is a programmatic wallet to an wallet app. It's just a piece
of software that is controlled with code; no UI, no CLI, just code.
</p>
<p>
Ethers.js offers such a programmatic wallet with its concepts of Singers, which are an abstraction
over different account types. One of them is <code>Wallet</code>.
</p>
<p>
<code>Wallet</code> is a class you can create an object from that works directly with an RPC. This
way you can actually write something on the blockchain without installing additional software!
</p>
<h2>Recap of the Blockchain Network Connection</h2>
<p>
In Part 1 of this course, we talked about how crypto wallets form a layer between your browser and
the blockchain network of your choice. Figure 1 shows where a wallet is usually located in the
stack.
</p>
<p>
The blockchain connection with wallet part of the image shows that the wallet handles the
connection to the RPC, and the browser doesn't know anything about it.
</p>
<figure>
<img src="images/blockchain-connection-ethersjs.png" />
<figcaption>
Figure 1: Blockchain connection with Ethers.js
</figcaption>
</figure>
<p>
As you just learned, Ethers.js can work as a wallet too! So, we can connect to a RPC with
Ethers.js and also use it to sign transactions, which are the mechanism to write on the
blockchain.
</p>
<h2>Creating a Development Key</h2>
<p>
If you're developing with a blockchain, never use private keys that control addresses with your
personal assets on it! Create one or more development keys, via a seed phrase or the random key
generator of Ethers.js.
</p>
<p>
If you have to do transactions in development that require tokens, be it for deployments or simply
to test your code, you need to save the keys somewhere for later use. If you don't need any tokens
for an action, you can just take a new random <code>Wallet</code> every time.
</p>
<p>
The following example creates a random <code>Wallet</code> every time you run it. Copy one of the
private keys and use it as your development key.
</p>
<code-editor>
const wallet = ethers.Wallet.createRandom()
print(wallet.privateKey)
</code-editor>
<blockquote>
<b>Note:</b> You shouldn't copy your private keys around and save them in an insecure way, but
since a development key doesn't have any tokens, it's not a problem if it gets stolen. Just create
a new one and delete the old one if that happens.
</blockquote>
<h2>Using a Development Key</h2>
<p>
Let's use that key! Try to construct a new <code>Wallet</code> object, pass it your copied private
key from above, and print the public key and address it generates.
</p>
<p>
Also, copy the address, so you have it handy for the later lessons.
</p>
<code-editor>
// Write your code here!
<p>
const wallet = new ethers.Wallet(
"0x350690562a6c31051d2c9f085e0b91be0a4ac0a3c206998c89ff3cfedf3524d0"
)
print(wallet.publicKey)
print(wallet.address)
</p>
</code-editor>
<h2>Connecting a Wallet with a Provider</h2>
<p>
Now, that you can create wallets from an existing private key, let's try to connect a wallet to a
provider.
</p>
<p>
To establish a connection the wallet object offers a <code>connect</code> method which will create
a new wallet that is connected to the provider.
</p>
<p>
Try to create a connected wallet and use it's <code>getGasPrice</code> method to check if the
connection works. Keep in mind that this method is asynchronous, and the price will be a
<code>BigNum</code> object.
</p>
<code-editor>
// Write your code here!
<p>
const wallet = new ethers.Wallet(
"0x350690562a6c31051d2c9f085e0b91be0a4ac0a3c206998c89ff3cfedf3524d0"
)
const connectedWallet = wallet.connect(ethers.getDefaultProvider())
const gasPrice = await connectedWallet.getGasPrice()
print(ethers.utils.formatEther(gasPrice) + " ETH")
</p>
</code-editor>
<h2>Summary</h2>
<p>
In this lesson, you learned about different types of wallets, and that you can use a programmatic
wallet for development. This way, you don't have to install any additional software.
</p>
<p>
You also learned how to connect a wallet with a provider, which will be important for the
following lessons. As I said before, you need a wallet to send transactions to a blockchain
network.
</p>
<p>
In <a href="09-test-networks.html">the next lesson</a>, you will learn about test networks and how
to request test tokens from token faucets.
</p>