Understanding Derivation Paths in MetaMask: A Comparison Between Ledger Live and BIP44
As a user of the popular cryptocurrency wallet MetaMask, you’ve probably noticed that it generates the same address for both Ledger Live and Bitcoin Integration (BIP44) derivation paths. But why is that? In this article, we’ll explore the Metamask codebase to understand what’s happening behind the scenes.
Redirection Paths
In cryptocurrency wallets, derivation paths are used to encode a user’s private key into multiple addresses. The most common derivation path is BIP44 (Bitcoin Improvement Proposal 44), which splits private keys into six copies: « m/44’/60’/0/ », « m/44’/61’/1/ », etc. These secondary addresses can then be combined to generate a single address.
The Ledger Live Wallet and MetaMask have different derivation paths as shown in the following code snippet:
const LEDGER_LIVE_PATH = m/44'/60'/0'/0/0
;
const BIP44_PATH = m/44'/…
Note that “BIP44_PATH” uses a different prefix (“m/”) and a slightly different structure for secondary addresses.
MetaMask Code
In the Metamask codebase, we can see how the derivation paths are encoded in an address:
${address}/${getChildren(address)}`;
const generateAddress = (path) => {
const [address] = path.split('/');
returns
};
const getChildren = (address) => {
// For simplicity, let's assume this function extracts child addresses from a BIP44 path
// In reality, this would mean parsing the BIP44 output and extracting the individual components of the address
const children = [];
for (let i = 1; i < address.length; i += 8) {
children.push(address.substring(i, i + 8));
}
returning children;
};
This "getChildren" function takes a BIP44 path as input and returns an array of child addresses. These secondary addresses are then encoded into the final address.
Why the same derivation path in both wallets
Now that we understand how derivation paths work, let's see why MetaMask generates the same address for Ledger Live and BIP44 derivation paths:
In the generateAddress function, we simply take a BIP44 path as input and split it into individual addresses using the "/" character. We then concatenate these child addresses to form the final address.
The key takeaway here is that if we encode a BIP44 path into an address, we can treat each child address independently. In other words, if we have a BIP44 path "m/44'/60'/0'/0/0", it is equivalent to a single four-part address: "m/44'/60'/0/".
In the MetaMask codebase, we do not explicitly encode each child address into a single address. Instead, we treat the entire derivation path as a whole and concatenate the resulting addresses.
This results in both the Ledger Live and BIP44 derivation paths ultimately generating the same address in MetaMask. This may seem counterintuitive at first, but it is actually a consequence of the way Metamask handles the encoding of BIP44 paths into addresses.
Conclusion
Understanding the Metamask codebase will help you better understand how the wallet generates addresses for Ledger Live and BIP44 derivation paths. By recognizing the differences between “BIP44_PATH” and “LEDDER_LIVE_PATH,” we can see that the MetaMask implementation is actually equivalent, even though it uses a different prefix and structure.
When you work with Metamask or other wallets in the future, you will appreciate the underlying mechanisms that ensure the smooth operation of these wallets.