The Anatomy of MetaMask

Julia Wu
10 min readJun 17, 2022

--

Source: 99images

Since launching in 2016, MetaMask has acquired 30M monthly active users as of March 2022. It has a global user base with elevated participation from the US, Philippines, Brazil, Germany, and Nigeria.

MetaMask is one of the indispensible apps of the Ethereum community, a household name that has initiated and shaped the crypto experiences of many. With NFTs, gaming, and DAOs bringing in large waves of new users to web3, wallets continue to be a fundamental building block in the ecosystem — they are the heart and center of all interactions between users and the blockchain.

This post will focus on the mechanics of blockchain accounts and MetaMask, with a skew towards Ethereum. But if you’re interested in the origins and business aspects of it, The Generalist has a very thorough story on that. The information in this post was gathered from sources such as the Mastering Ethereum book, MetaMask support articles, and Ethereum Homestead documentation. So if you’re interested in diving deeper, these places are full of precious details.

They can also be intimidating to some. But in reality, most wallet applications today are extensions built around a simple, central component: An account consisting of a public/private key pair.

Starting with Principles

Before we talk about MetaMask itself, let’s first dive into some foundational concepts that underpin it.

Source

Accounts

Accounts are a core primitive of the blockchain. In Ethereum, there are 2 types of accounts: Externally Owned Accounts, and Contract Accounts.

Every account is defined by a public key and a private key. The public key is derived from the private key, and all accounts on the blockchain are ordered by their addresses (public keys).

Let’s break down the 2 types of accounts and their characteristics:

Externally Owned Accounts

  1. Have an Ether balance
  2. Can send transactions (ie. transfers or triggering of smart contract code)
  3. Are controlled by private keys
  4. Have no associated code

Contract Accounts

  1. An actual smart contract with an Ether balance
  2. Has associated code
  3. Code execution is triggered by transactions or messages received from other contracts or accounts

Every action on Ethereum is triggered by an Externally Owned Account.

Why are we talking about accounts, if this post is about wallets? Well, at its core, a wallet is nothing but an account manager.

What is a Wallet?

If we strip away all the bells and whistles of the chrome extensions and mobile apps, behind that cute little fox is simply a piece of software that is used to store and manage a user’s keys.

Every wallet starts with a key-management component.

Once the key-management aspect is there, then the wallet becomes a UI that allows a user to interact with the blockchain. It can facilitate money transfers from one account to another, manage keys, track balances of tokens, and help you sign transactions. But none of these things can happen without key management.

Think of it as a keychain. Ethereum wallets have the primary job of containing a user’s keys. They don’t actually hold any real tokens. They merely reflect how many tokens are assigned to your account on the blockchain.

A blockchain is simply a distributed ledger that associates your account with how much you own of some token. Keys held by the wallet are the only requirement to transfer Ether or tokens to others. Using your keys, you make modifications to the ledger by using your private key to sign a transaction. We will cover this in detail in a later section.

Source

Types of Wallets

One way to distinguish wallets is by how their keys are generated.

There are primarily 2 types of key-generation for wallets: deterministic and nondeterministic. They are differentiated by whether the keys are related to each other.

Nondeterministic wallets have keys generated by a random number, and the keys are NOT related to each other.

This is more of the legacy Ethereum wallet. It comes in the form of a wallet file that stores a single randomly generated private key. Deterministic wallets generate a new wallet file for every address and can be much more cumbersome to back up and recover.

Nondeterministic wallets are considered to be inferior to their deterministic counterparts.

Deterministic wallets have keys generated by a single master key, also known as the seed. In deterministic wallets, the keys are related to each other and can always be reproduced with the same seed. This is the type of wallet that we are more familiar with because it is recognized as a standard and used by MetaMask.

Seeds are represented as a list of English words (usually 12) that you can write down, to recover your keys in the event that you need to do so. This is also the “secret recovery phrase” that you see in MetaMask. It is also sometimes referred to as your “mnemonic.” If someone gets hold of them, though, they can immediately access your wallet and do all sorts of things with your assets. This is why the advice is to never share your seed phrase, and not even store it in electronic form.

The seed phrase can be handy for exporting and importing wallets, and easily migrating all the keys across different wallet apps. For example, if you have a wallet on MetaMask, you can still import that wallet into your Rainbow or Argent mobile app by typing in the seed phrase.

The most advanced form of a deterministic wallet is the Hierarchical Deterministic (HD) wallet, based on Bitcoin’s BIP-32 standard. The keys are called “hierarchical” because they represent a tree structure. The parent key can derive a set of child keys, which can then derive yet another series of grandchild keys.

HD wallets are better than other types of deterministic wallets because:

  1. The tree structure can represent an organizational structure. The branches can be treated as sub-teams or subsidiaries in a corporation. This provides the opportunity to organize budgets and payments accordingly
  2. Public keys can be generated without accessing the corresponding private keys

The seed phrase that’s a list of English words is just one way of encoding a private key, though. It is currently widely used because other types of representations (such as a HEX string) can lead to higher error rates when written down on paper. At a high level, the mnemonic words are generated from a process defined by BIP-39 which involves starting with some source of entropy, transforming that data, and then mapping it to a list of words.

Source: Mastering Ethereum

There are a set of industry standards ( such as mnemonic code words and HD wallets) that influence wallet implementations and lead to interoperability in wallets. Thanks to these standards, you can easily export and import wallets from and to different wallet apps.

Form Factors

Wallets can come in a few different forms. Most commonly:

  • Desktop: MyCrypto, Gnosis Safe Desktop
  • Browser: MetaMask, MyEtherWallet, Web3Auth
  • Mobile: Argent, Coinbase Wallet, MetaMask mobile, Rainbow, Trust Wallet
  • Hardware wallets (a secure method for accessing your funds while online as they do not expose the private key when signing transactions): Ledger, Trezor, KeepKey, Bitbox, Lattice1
Argent’s social recovery feature. Source

Smart Contract Wallets

Some wallets, such as Argent and Gnosis Safe are Smart Contract Wallets. These are similar to the “Contract Accounts” described at the very beginning of this article.

These wallets use an alternative to Externally Owned Accounts and are built on smart contracts. Because there is more room for business logic to be implemented, they can offer features such as recovery without seed phrases, automatically blocking certain transfers to untrusted contracts, social recovery, transfer limits, account freezing, etc.

Here’s some sample code of a very simple smart contract wallet, based on a tutorial by Block Explorer:

Enter MetaMask

The protagonist we’ve been waiting for. The little geometric fox that tells us that “the decentralized web awaits” — the one many of us have a love-hate relationship with.

Source

MetaMask is a non-custodial (or self-custodial) cryptocurrency wallet. This means no third-party “custodian” holds on to your private keys. It lets you live the “not you keys, not your tokens” ethos. MetaMask doesn’t store any data about the wallet, and everything you see is in your browser or mobile app at the local level.

MetaMask uses 3 different types of “secrets” to safeguard your wallet:

  1. Secret Recovery Phrase: As noted before, this phrase is the mathematically generated mnemonic based on your master key. These words, when placed in a specific order, can regenerate all the accounts in your wallet. Remember, a single wallet can have many accounts in it. Your wallet is the keychain that holds many public/private keys. Each key pair corresponds to an account in the wallet. When MetaMask regenerates your wallet from the Secret Recovery Phrase, it might only produce the first account at first, but the additional accounts can be re-created in the same order because the wallet is deterministic.
  2. Private Keys: Each account has its own public and private key. Usually, accounts are seen with their address, aka the public key. The individual private keys can be used to import that specific account (and ONLY that one) into any other wallet app.
  3. Password: This is used to secure the wallet app itself. You use it to unlock only the app. FaceID works for mobile.

Coinbase vs. MetaMask

It is worth noting that Coinbase (the core app) is a custodial wallet, so the difference is that instead of you holding on to your own private keys, Coinbase actually stores them for you. So you can’t directly export your Coinbase wallet and import it into another wallet like MetaMask. However, you can do that from a Rainbow wallet to MetaMask (and vice versa) because both are non-custodial.

Note: Coinbase also offers a non-custodial solution with the Coinbase Wallet.

How MetaMask Connects to a Blockchain Network

Wallets like MetaMask can also be your gateway to public blockchain networks. Let’s remind ourselves of the job of a web browser: a software program that allows us to access the World Wide Web. When a user requests a webpage by entering its URL address, the browser fetches the content from a web server to display it on the user’s device.

Blockchain nodes are constantly syncing the state of the network and observing the history of the transactions. Similarly, MetaMask allows you to access the blockchain network because your current browser can’t do that.

MetaMask connects you to the public blockchain networks and allows you to read and write data from/to it. Whereas you point a browser to a URL, you point MetaMask to RPC endpoints to access public blockchain nodes.

To communicate with blockchain nodes, there is a standard called JSON-RPC, which allows for requesting and submitting data from them. These requests through JSON-RPC can be made over HTTP or WebSocket.

How does MetaMask discover these nodes to send requests to? It uses Infura, which provides a highly available, scalable blockchain API that enables access to the blockchain network.

Source

The Life of a Transaction

The blockchain is a distributed ledger, like a mapping of accounts and their assets. Imagine a spreadsheet, where every single row is an account (ethereum address) and every column is the crypto asset that it holds.

Say Alice wants to send 1 Ether to Bob. Alice opens up her MetaMask wallet, enters Bob’s wallet address, and hits send. When she hits send, she is also using her private keys to sign the transaction.

This transaction enters a local memory pool (mempool), which then gets picked up by the closest node in the Ethereum network.

Alice’s transaction will be prioritized based on her gas settings when initiating this transaction. The transaction is propagated across the nodes in the network. Upon processing the transaction, the nodes will also perform the check that Alice has enough Ether to spend before performing the transfer.

Finally, the transfer is simply a modification of the ledger, deducting 1 ETH from Alice’s balance and incrementing 1 ETH in Bob’s account.

Core Features

Let’s wrap up with a list of MetaMask’s main features — notice that many of them are related to or revolve around key management:

  1. Account management (create, import, and export accounts, connect with hardware wallet)
  2. Connect to public blockchain networks
  3. Sign transactions (transfers, smart contract interactions), allowing users to determine the gas price for each transaction
  4. Buy crypto with fiat (using MoonPay, Transak, Wyre)
  5. Swap tokens
  6. Show assets (ERC20 tokens, NFTs)
  7. Show activity (transaction history)
  8. Connect with dApps
Source

Wallet standards are always evolving, and so is MetaMask. We may start to see more attempts at features like social recovery wallets, multi-factor authentication for higher security, and new user experiences made possible by injecting functionality into MetaMask itself when it is connected to a dApp.

There are many possibilities for innovation across wallet UX, dApp interaction, and security fronts. Despite the road paved by MetaMask, the work on wallets is far from done. For example, we haven’t quite figured out how to sign transactions on mobile yet. Thanks to industry standards, wallets can be composable and re-built to suit the needs of users and applications.

One could argue that wallets are similar to web2 concepts like digital identity/passport, bank account, browser, and much more. It could be all of those, or it could be something else. We have just started to scratch the surface — the design space is vast.

Thank you for reading! If you want to continue the conversation and see more stuff like this, you can find me on Twitter.

Sources & Further Reading

--

--

Julia Wu

Building something new. Prev. eng at Brex, Apple, MSFT. More at juliawu.me