8. Crypto Wallet Setup

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?

What is a Crypto Wallet?

A crypto wallet, or simply wallet, is a mechanism to manage your keys, some are even able to manage your blockchain connections.

There are different types of wallets, so let's get a small overview on the essentials.

1. Wallet App

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.

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.

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.

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.

2. Paper Wallet

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.

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.

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.

3. Hardware Wallets

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.

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.

4. Programmatic Wallet

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.

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.

Ethers.js offers such a programmatic wallet with its concepts of Singers, which are an abstraction over different account types. One of them is Wallet.

Wallet 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!

Recap of the Blockchain Network Connection

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.

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.

Figure 1: Blockchain connection with Ethers.js

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.

Creating a Development Key

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.

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 Wallet every time.

The following example creates a random Wallet every time you run it. Copy one of the private keys and use it as your development key.

const wallet = ethers.Wallet.createRandom() print(wallet.privateKey)
Note: 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.

Using a Development Key

Let's use that key! Try to construct a new Wallet object, pass it your copied private key from above, and print the public key and address it generates.

Also, copy the address, so you have it handy for the later lessons.

// Write your code here!

const wallet = new ethers.Wallet( "0x350690562a6c31051d2c9f085e0b91be0a4ac0a3c206998c89ff3cfedf3524d0" ) print(wallet.publicKey) print(wallet.address)

Connecting a Wallet with a Provider

Now, that you can create wallets from an existing private key, let's try to connect a wallet to a provider.

To establish a connection the wallet object offers a connect method which will create a new wallet that is connected to the provider.

Try to create a connected wallet and use it's getGasPrice method to check if the connection works. Keep in mind that this method is asynchronous, and the price will be a BigNum object.

// Write your code here!

const wallet = new ethers.Wallet( "0x350690562a6c31051d2c9f085e0b91be0a4ac0a3c206998c89ff3cfedf3524d0" ) const connectedWallet = wallet.connect(ethers.getDefaultProvider()) const gasPrice = await connectedWallet.getGasPrice() print(ethers.utils.formatEther(gasPrice) + " ETH")

Summary

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.

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.

In the next lesson, you will learn about test networks and how to request test tokens from token faucets.