DEV Community

Cover image for The blockchain bootcamp · 2. Wallets, keys and addresses
Amit chakraborty
Amit chakraborty

Posted on Originally published at amitchakraborty.dev

The blockchain bootcamp · 2. Wallets, keys and addresses

The blockchain bootcamp · Chapter 2 of 18 · Blockchain · new chapter every Saturday evening

By the end of this chapter: Understand custody, signing and the mistakes that lose funds permanently.

The problem

Say you are Emma, a front-end engineer tasked with adding a "view latest block" widget to your company's dashboard. You have a working React codebase and a CI pipeline that already ships daily, but you have never written blockchain-related code. Your design mock shows the block number, its hash, the timestamp, and a badge that says "finalised". As you start researching, you realize that to display this information, you need to understand how blockchain wallets, keys, and addresses work. You try to generate a wallet, but you're not sure what type of wallet to use, how to manage your private keys, and how to derive an address from them. You're concerned about the security implications of handling private keys and the potential consequences of losing funds.

As you delve deeper, you discover that there are different types of wallets, such as hardware wallets, software wallets, and paper wallets, each with its own trade-offs. You're unsure which one to choose and how to integrate it with your React application. You're also unsure about the differences between private keys, public keys, and addresses, and how they relate to each other. You've heard stories about people losing their private keys and losing access to their funds, and you want to make sure you understand the concepts of custody, signing, and the mistakes that can lead to permanent fund loss.

Before you start

To follow along with this chapter, you should have a basic understanding of blockchain concepts, such as blocks, chains, hashes, nonces, consensus, and finality, which were covered in the previous chapter. You should also have a working React codebase and a CI pipeline set up. You'll need to install the following dependencies: web3 and ethers.js. You can verify that your setup is working by running the command npm install web3 ethers.js and then npx web3 --version to check the version of web3 installed.

Understanding wallet types

There are several types of wallets, each with its own strengths and weaknesses. Hardware wallets, such as Ledger or Trezor, are considered to be the most secure, as they store private keys offline and require physical access to sign transactions. Software wallets, such as MetaMask or Trust Wallet, are more convenient, but may be vulnerable to hacking or malware. Paper wallets are a type of software wallet that generates a private key and address, which can be printed out and stored securely.

Generating a wallet

To generate a wallet, you can use a library such as ethers.js. You'll need to create a new instance of the Wallet class, passing in a random mnemonic phrase or a private key. You can then use the wallet.address property to get the Ethereum address associated with the wallet.

Deriving an address from a private key

To derive an address from a private key, you can use the ethers.js library. You'll need to create a new instance of the Wallet class, passing in the private key. You can then use the wallet.address property to get the Ethereum address associated with the wallet.

Build it

Here are the steps to generate a wallet and derive an address from a private key:

  1. Install the required dependencies: npm install web3 ethers.js
  2. Create a new instance of the Wallet class, passing in a random mnemonic phrase or a private key: const wallet = new ethers.Wallet('0x...' + 'private key')
  3. Use the wallet.address property to get the Ethereum address associated with the wallet: const address = wallet.address
  4. Verify that the address is correct by checking it against a blockchain explorer: console.log(address)

When this breaks

Here are some common issues that can occur when working with wallets and addresses:

  • Private key leakage: If your private key is compromised, an attacker can use it to sign transactions and steal your funds. To prevent this, make sure to store your private key securely, such as in a hardware wallet or a secure software wallet.
  • Address derivation errors: If you derive an address incorrectly, you may end up with an invalid address that cannot be used to receive funds. To prevent this, make sure to use a reliable library such as ethers.js to derive the address.
  • Wallet software bugs: If the wallet software you are using has a bug, it may not function correctly, leading to lost funds or other issues. To prevent this, make sure to use a reputable wallet software and keep it up to date.

What it costs

Using a hardware wallet can be more expensive than using a software wallet, as you need to purchase the hardware device. However, the added security benefits may be worth the cost. Additionally, using a wallet that is not compatible with your blockchain platform of choice may lead to issues with transaction signing and verification.

In the interview

In an interview, you may be asked questions such as:

  • "What is the difference between a private key and an address?"
  • "How do you derive an address from a private key?"
  • "What are the trade-offs between using a hardware wallet and a software wallet?"

A weak answer might be: "I'm not really sure, but I think a private key is used to sign transactions and an address is used to receive funds."

A strong answer would be: "A private key is a secret key used to sign transactions, while an address is a public key used to receive funds. To derive an address from a private key, you can use a library such as ethers.js. The trade-offs between using a hardware wallet and a software wallet include security, convenience, and cost. Hardware wallets are more secure, but may be less convenient and more expensive, while software wallets are more convenient, but may be less secure and more vulnerable to hacking or malware."

Your tasks

  1. Generate a new wallet using ethers.js and derive an address from the private key.
  2. Use a blockchain explorer to verify that the address is correct.
  3. Research the different types of wallets and their trade-offs, and write a short report on your findings.
  4. Implement a simple wallet software using ethers.js that allows users to generate a new wallet and derive an address from the private key.
  5. Test the wallet software and identify any potential security vulnerabilities or issues.

Your tasks this week

Do the exercises above before the next chapter. Reading a tutorial and doing
one are different activities and only one of them changes what you can build.

Stuck on any of them? Say so — describe what you tried and what happened:
tell me where you got stuck. I read every one, and the questions
that come back more than twice get answered in the next chapter.

The blockchain bootcamp

Chapter 2 of 18. New chapter every Saturday evening.
Next: A chain on your own machine.

· The full syllabus and every chapter so far
· Subscribers also get the condensed notes for this chapter, the running
recap of everything the series has covered, and the extended guidance:
subscribe


Written by Amit Chakraborty — founding engineer and senior architect: React Native, AI and RAG systems, production architecture. Portfolio · LinkedIn · GitHub.

Need this built, reviewed or taught to your team? Get in touch or email amit@devamit.co.in. Available for senior and founding engineering roles, consulting and training, remote worldwide.

Top comments (0)