<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: George</title>
    <description>The latest articles on DEV Community by George (@dekadent85).</description>
    <link>https://dev.to/dekadent85</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3097554%2F1d63c5b1-e3a7-4ba7-baae-2b2cb8f36a0f.png</url>
      <title>DEV Community: George</title>
      <link>https://dev.to/dekadent85</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/dekadent85"/>
    <language>en</language>
    <item>
      <title>Dissecting BIP-39 Under the Hood: How Mnemonic Phrases and Private Keys Work (With Python Code)</title>
      <dc:creator>George</dc:creator>
      <pubDate>Sun, 27 Apr 2025 21:41:36 +0000</pubDate>
      <link>https://dev.to/dekadent85/dissecting-bip-39-under-the-hood-how-mnemonic-phrases-and-private-keys-work-with-python-code-5cgl</link>
      <guid>https://dev.to/dekadent85/dissecting-bip-39-under-the-hood-how-mnemonic-phrases-and-private-keys-work-with-python-code-5cgl</guid>
      <description>&lt;h2&gt;
  
  
  Introduction
&lt;/h2&gt;

&lt;p&gt;BIP-39 (Bitcoin Improvement Proposal 39) is a standard that converts random entropy bits into human-readable mnemonic phrases. These 12–24-word phrases are used to recover cryptocurrency wallets. In this article, we will explore how BIP-39 works at the code level and write a Python example to generate a mnemonic phrase and derive a private key.&lt;/p&gt;

&lt;h2&gt;
  
  
  How Does a Mnemonic Phrase Work
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Generating Entropy&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;BIP-39 starts with creating random entropy (128–256 bits). For example, 128 bits of entropy produce 12 words, while 256 bits produce 24 words. More entropy means higher security.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Checksum Addition&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;A checksum is appended to the entropy. Its length depends on the entropy size:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;128 bits → 4-bit checksum
&lt;/li&gt;
&lt;li&gt;256 bits → 8-bit checksum
This allows verifying the phrase’s correctness during wallet recovery.
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;3. Mapping to a Wordlist&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The combined bits are split into 11-bit groups, each corresponding to a word from BIP-39’s predefined list (2048 words). For example, &lt;em&gt;01011010101&lt;/em&gt; might map to &lt;em&gt;apple&lt;/em&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Practice: Generating a Mnemonic in Python
&lt;/h2&gt;

&lt;p&gt;Here’s a Python code snippet to generate a mnemonic phrase. We will use the &lt;em&gt;hashlib&lt;/em&gt; and &lt;em&gt;bitstring&lt;/em&gt; libraries.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import hashlib
import secrets
from bitstring import BitArray

# Generate entropy (128 bits)
entropy = secrets.token_bytes(16)  # 16 bytes = 128 bits

# Add checksum
entropy_bits = BitArray(entropy).bin
checksum_length = len(entropy_bits) // 32
hash_entropy = hashlib.sha256(entropy).digest()
checksum = BitArray(hash_entropy).bin[:checksum_length]

full_bits = entropy_bits + checksum

# Split into 11-bit chunks
indices = []
for i in range(0, len(full_bits), 11):
    chunk = full_bits[i:i+11]
    indices.append(int(chunk, 2))

# Load BIP-39 wordlist (example)
bip39_words = [
    "abandon", "ability", "able", ..., "zoo"
]  # Full list: https://bip39-phrase.com/

mnemonic = " ".join([bip39_words[index] for index in indices])
print("Mnemonic phrase:", mnemonic)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Converting a Mnemonic to a Private Key
&lt;/h2&gt;

&lt;p&gt;According to BIP-39, the mnemonic and an optional passphrase are converted into a 512-bit seed using &lt;strong&gt;PBKDF2-HMAC-SHA512&lt;/strong&gt;. This seed is used to derive private keys via BIP-32/BIP-44.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import hashlib
import hmac

# Password (optional)
passphrase = ""
salt = "mnemonic" + passphrase

# PBKDF2
seed = hashlib.pbkdf2_hmac(
    "sha512",
    mnemonic.encode("utf-8"),
    salt.encode("utf-8"),
    2048,  # Recommended iterations
    64     # Seed length (64 bytes = 512 bits)
)

print("Seed (hex):", seed.hex())
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  How Do BIP-32 and BIP-44 Work?
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;BIP-32 (HD Wallets)&lt;/strong&gt; enables hierarchical key derivation from a single seed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;BIP-44&lt;/strong&gt; defines a structure for multi-currency wallets (e.g., &lt;code&gt;m/44'/0'/0'&lt;/code&gt; for Bitcoin).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;To derive private keys, use derivation paths. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;from bip32utils import BIP32Key

# Example for Bitcoin (BIP-44)
bip32_key = BIP32Key.fromEntropy(seed)
child_key = bip32_key.ChildKey(0x80000000)  # Hardened path
private_key = child_key.WalletImportFormat()
print("Private key:", private_key)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Tools for Working with BIP-39
&lt;/h2&gt;

&lt;p&gt;To test mnemonics or convert phrases to private keys, use these tools:  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;a href="https://bip39-phrase.com/seed-phrase-to-private-key-converter/" rel="noopener noreferrer"&gt;https://bip39-phrase.com/seed-phrase-to-private-key-converter/&lt;/a&gt; — derive private keys and addresses for different blockchains using BIP-32/BIP-44 paths.
&lt;/li&gt;
&lt;li&gt;Ian Coleman’s BIP39 Tool (&lt;a href="https://iancoleman.io/bip39/" rel="noopener noreferrer"&gt;https://iancoleman.io/bip39/&lt;/a&gt;) — a popular open-source offline tool.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Critical Warnings
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. Entropy Security&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Always use cryptographically secure generators (e.g., Python’s secrets). Avoid homemade solutions.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Offline Generation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Tools like Ian Coleman’s BIP39 should run offline to eliminate leakage risks.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Never Share Your Seed Phrase&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Your seed phrase equals your private key. Even open-source tools should be audited for backdoors.  &lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;BIP-39 bridges human-readable phrases and cryptographic keys. Understanding its mechanics helps build secure wallets and avoid critical mistakes. For testing, online tools can be used, but always store real assets in trusted hardware wallets.  &lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Additional Resources:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Official BIP-39 Repository &lt;a href="https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki" rel="noopener noreferrer"&gt;https://github.com/bitcoin/bips/blob/master/bip-0039.mediawiki&lt;/a&gt; &lt;/li&gt;
&lt;li&gt;BIP-32 and BIP-44: How HD Wallets Work (&lt;a href="https://vault12.com/learn/crypto-security-basics/what-is-bip39/" rel="noopener noreferrer"&gt;https://vault12.com/learn/crypto-security-basics/what-is-bip39/&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>python</category>
      <category>bip39</category>
      <category>cryptocurrency</category>
    </item>
  </channel>
</rss>
