<?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: Mohsen</title>
    <description>The latest articles on DEV Community by Mohsen (@mohsenm4).</description>
    <link>https://dev.to/mohsenm4</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.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4035029%2F4633c7d3-0f5e-4535-abad-1456779430d9.jpg</url>
      <title>DEV Community: Mohsen</title>
      <link>https://dev.to/mohsenm4</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mohsenm4"/>
    <language>en</language>
    <item>
      <title>Ethereum Transaction Lifecycle in Go: From Signing to "It's Really Money"</title>
      <dc:creator>Mohsen</dc:creator>
      <pubDate>Fri, 06 Nov 2026 11:59:24 +0000</pubDate>
      <link>https://dev.to/mohsenm4/ethereum-transaction-lifecycle-in-go-from-signing-to-its-really-money-2f68</link>
      <guid>https://dev.to/mohsenm4/ethereum-transaction-lifecycle-in-go-from-signing-to-its-really-money-2f68</guid>
      <description>&lt;h1&gt;
  
  
  Ethereum Transaction Lifecycle in Go: From Signing to "It's Really Money"
&lt;/h1&gt;

&lt;p&gt;An Ethereum transaction is not finished just because we signed it or sent it to an RPC node.&lt;/p&gt;

&lt;p&gt;There are several steps between creating a transaction and being confident that money has actually moved. Some of these steps are handled by Ethereum, but many others are our responsibility as backend developers.&lt;/p&gt;

&lt;p&gt;In this post, I'll walk through the transaction lifecycle I worked on while upgrading my Go-based Ethereum explorer, &lt;a href="https://github.com/mohsenm4/blockchain-insight" rel="noopener noreferrer"&gt;&lt;code&gt;blockchain-insight&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;I'll focus on the practical problems: signing, gas estimation, nonce management, broadcasting, receipts, confirmations, and handling reorgs.&lt;/p&gt;

&lt;p&gt;This is Part 2 of my Ethereum wallet and transaction series.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 1:&lt;/strong&gt; &lt;a href="https://dev.to/mohsenm4/ethereum-cryptography-from-a-go-devs-view-47h8"&gt;Ethereum cryptography from a Go dev's view&lt;/a&gt;&lt;/p&gt;




&lt;h2&gt;
  
  
  1. Why: An Explorer Is Not an Exchange
&lt;/h2&gt;

&lt;p&gt;Before this upgrade, &lt;code&gt;blockchain-insight&lt;/code&gt; was mainly an Ethereum blockchain explorer.&lt;/p&gt;

&lt;p&gt;It could read blockchain data, inspect blocks and transactions, and provide information about the network.&lt;/p&gt;

&lt;p&gt;But reading blockchain data and handling real money are two different problems.&lt;/p&gt;

&lt;p&gt;An explorer answers questions like:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;What happened in this block?&lt;/li&gt;
&lt;li&gt;Who sent this transaction?&lt;/li&gt;
&lt;li&gt;What was the transaction value?&lt;/li&gt;
&lt;li&gt;Which events were emitted by a smart contract?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;An exchange needs to answer different questions:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Did we actually send the withdrawal?&lt;/li&gt;
&lt;li&gt;Can we safely retry it?&lt;/li&gt;
&lt;li&gt;Are we using the correct nonce?&lt;/li&gt;
&lt;li&gt;Was the transaction included in a block?&lt;/li&gt;
&lt;li&gt;Did the transaction execution succeed?&lt;/li&gt;
&lt;li&gt;Is a deposit safe to credit to a user's balance?&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, imagine a user requests a withdrawal of 100 USDC.&lt;/p&gt;

&lt;p&gt;Sending a transaction to Ethereum is only one part of the process. We also need to know whether the transaction was accepted, whether it was included, whether the contract execution succeeded, and whether the result is sufficiently confirmed.&lt;/p&gt;

&lt;p&gt;A transaction hash alone does not prove that the withdrawal was successful.&lt;/p&gt;

&lt;p&gt;That's the difference between building an explorer and building a system that handles money.&lt;/p&gt;

&lt;p&gt;With &lt;a href="https://github.com/mohsenm4/blockchain-insight/releases/tag/v2.0.0" rel="noopener noreferrer"&gt;&lt;code&gt;blockchain-insight v2.0.0&lt;/code&gt;&lt;/a&gt;, I wanted to move beyond reading blockchain data and understand the complete transaction lifecycle.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Build and Sign a Transaction by Hand (EIP-1559)
&lt;/h2&gt;

&lt;p&gt;Before sending a transaction, we need to construct it.&lt;/p&gt;

&lt;p&gt;For this project, I used EIP-1559 transactions (type 2).&lt;/p&gt;

&lt;p&gt;A transaction contains several important fields:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Nonce:&lt;/strong&gt; The sender's transaction sequence number.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MaxPriorityFeePerGas:&lt;/strong&gt; The maximum tip offered to the validator.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;MaxFeePerGas:&lt;/strong&gt; The maximum total fee per unit of gas.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;GasLimit:&lt;/strong&gt; The maximum amount of gas the transaction can consume.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;To:&lt;/strong&gt; The recipient address.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Value:&lt;/strong&gt; The amount of ETH being transferred.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Data:&lt;/strong&gt; Additional input data, usually used when calling a smart contract.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;ChainID:&lt;/strong&gt; Identifies the Ethereum network.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Where do these values come from?&lt;/p&gt;

&lt;p&gt;The nonce comes from the sender's account and our local nonce management.&lt;/p&gt;

&lt;p&gt;The fee values come from network fee information and our fee policy.&lt;/p&gt;

&lt;p&gt;The gas limit is estimated using the transaction's execution requirements.&lt;/p&gt;

&lt;p&gt;The recipient, value, and data come from the withdrawal request.&lt;/p&gt;

&lt;p&gt;For a normal ETH transfer, &lt;code&gt;Data&lt;/code&gt; is usually empty.&lt;/p&gt;

&lt;p&gt;For an ERC-20 transfer, however, &lt;code&gt;Value&lt;/code&gt; is usually zero and &lt;code&gt;Data&lt;/code&gt; contains the encoded contract function call.&lt;/p&gt;

&lt;h3&gt;
  
  
  Understanding the fee cap
&lt;/h3&gt;

&lt;p&gt;One thing I learned while working on &lt;a href="https://github.com/mohsenm4/blockchain-insight/blob/v2.0.0/cmd/tools/rawtx/main.go" rel="noopener noreferrer"&gt;&lt;code&gt;cmd/tools/rawtx&lt;/code&gt;&lt;/a&gt; was that the transaction's fee fields are not the same as the final gas price.&lt;/p&gt;

&lt;p&gt;EIP-1559 separates the fee into two parts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Base fee: The network fee, which is burned.&lt;/li&gt;
&lt;li&gt;Priority fee: The tip offered to the validator.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A common fee estimation strategy is:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;maxFeePerGas = 2 * baseFee + priorityFee&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Why multiply the base fee by two?&lt;/p&gt;

&lt;p&gt;Because the base fee can increase when blocks are congested. Using twice the current base fee provides some room for future increases while the transaction waits to be included.&lt;/p&gt;

&lt;p&gt;This is a fee estimation strategy, not a mandatory Ethereum rule.&lt;/p&gt;

&lt;p&gt;The actual amount paid is calculated using the base fee of the block containing the transaction and the available priority fee, subject to the transaction's fee cap.&lt;/p&gt;

&lt;p&gt;In other words, &lt;code&gt;maxFeePerGas&lt;/code&gt; is a maximum, not necessarily what we will pay.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why build a raw transaction?
&lt;/h3&gt;

&lt;p&gt;Using a high-level library or an &lt;code&gt;abigen&lt;/code&gt; binding makes interacting with contracts easier.&lt;/p&gt;

&lt;p&gt;For example, an ERC-20 binding can generate the calldata for a &lt;code&gt;transfer&lt;/code&gt; function.&lt;/p&gt;

&lt;p&gt;But it can also hide some of the lower-level details.&lt;/p&gt;

&lt;p&gt;Building a transaction manually helped me understand:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How the nonce is assigned.&lt;/li&gt;
&lt;li&gt;How calldata is constructed.&lt;/li&gt;
&lt;li&gt;How gas and fee fields are selected.&lt;/li&gt;
&lt;li&gt;How the unsigned transaction is signed.&lt;/li&gt;
&lt;li&gt;How the signed transaction becomes raw bytes.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A useful lesson here is that knowing how to use a binding is not the same as understanding the transaction being sent.&lt;/p&gt;




&lt;h2&gt;
  
  
  3. Nonce: Two Withdrawals, One Nonce
&lt;/h2&gt;

&lt;p&gt;Nonce management is one of the most important parts of a withdrawal system.&lt;/p&gt;

&lt;p&gt;Ethereum requires transactions from the same account to use sequential nonces.&lt;/p&gt;

&lt;p&gt;For example:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transaction A uses nonce 10.&lt;/li&gt;
&lt;li&gt;Transaction B uses nonce 11.&lt;/li&gt;
&lt;li&gt;Transaction C uses nonce 12.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now imagine two withdrawal requests arrive almost simultaneously.&lt;/p&gt;

&lt;p&gt;Both workers ask Ethereum for the account's next nonce.&lt;/p&gt;

&lt;p&gt;If both receive nonce 10, they may create two different transactions with the same nonce.&lt;/p&gt;

&lt;p&gt;This is a problem.&lt;/p&gt;

&lt;p&gt;The network cannot include both transactions as separate sequential transactions from the same account. One may replace the other if its fee is sufficient, or one may remain pending.&lt;/p&gt;

&lt;p&gt;I broke this on purpose and saw it happen on Sepolia: one withdrawal went through, the other failed with &lt;code&gt;replacement transaction underpriced&lt;/code&gt;. The full story is in &lt;a href="https://x.com/mohsenqv/status/2118671836762263571" rel="noopener noreferrer"&gt;this thread&lt;/a&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  The problem with reading the nonce directly
&lt;/h3&gt;

&lt;p&gt;Calling:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;eth_getTransactionCount(address, "pending")&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;is useful because it includes transactions known to the node's pending state.&lt;/p&gt;

&lt;p&gt;However, it is not a complete solution for concurrent workers.&lt;/p&gt;

&lt;p&gt;Two workers can still read the same nonce before either transaction is broadcast.&lt;/p&gt;

&lt;p&gt;That's why a withdrawal service needs its own nonce manager.&lt;/p&gt;

&lt;p&gt;Mine is small (&lt;a href="https://github.com/mohsenm4/blockchain-insight/blob/v2.0.0/internal/nonce/nonce.go" rel="noopener noreferrer"&gt;&lt;code&gt;internal/nonce&lt;/code&gt;&lt;/a&gt;): it asks the node once, then gives out numbers from memory, under a mutex:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;nm&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;NonceManager&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;NextNonce&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt; &lt;span class="n"&gt;context&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Context&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;uint64&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kt"&gt;error&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;nm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;nm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unlock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;nm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initialized&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;nm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;provider&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PendingNonceAt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ctx&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;nm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;addr&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
        &lt;span class="n"&gt;nm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nextNonce&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;
        &lt;span class="n"&gt;nm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;initialized&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;true&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;nonce&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;nm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nextNonce&lt;/span&gt;
    &lt;span class="n"&gt;nm&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;nextNonce&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;nonce&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The important point is that reserving a nonce must be atomic.&lt;/p&gt;

&lt;p&gt;If two workers request a nonce simultaneously, they must receive different numbers. After the fix, the same two requests got nonces 7 and 8, and both were mined in the same block.&lt;/p&gt;

&lt;h3&gt;
  
  
  What if a transaction is signed but never sent?
&lt;/h3&gt;

&lt;p&gt;This is an interesting case.&lt;/p&gt;

&lt;p&gt;Signing a transaction does not change the Ethereum account's on-chain nonce.&lt;/p&gt;

&lt;p&gt;Ethereum does not know about a transaction that was only signed locally.&lt;/p&gt;

&lt;p&gt;Suppose we reserve nonce 10 and sign a transaction, but never broadcast it.&lt;/p&gt;

&lt;p&gt;The network still considers nonce 10 available.&lt;/p&gt;

&lt;p&gt;However, our application may have already reserved nonce 10.&lt;/p&gt;

&lt;p&gt;If we then send a different transaction using nonce 11, it cannot be processed before nonce 10.&lt;/p&gt;

&lt;p&gt;This can create a nonce gap and block later transactions.&lt;/p&gt;

&lt;p&gt;That is why my nonce manager has a &lt;code&gt;Reset&lt;/code&gt;: when a tx that took a nonce may never reach the network, the next call reads the nonce from the node again. A bigger system would track every reservation and its transaction state, not just read a number from an RPC node.&lt;/p&gt;




&lt;h2&gt;
  
  
  4. Broadcast Is Not Done
&lt;/h2&gt;

&lt;p&gt;Once a transaction is signed, we can broadcast it to an Ethereum node.&lt;/p&gt;

&lt;p&gt;But broadcasting and completing a transaction are different events.&lt;/p&gt;

&lt;p&gt;A common mistake is to treat a successful RPC response as proof that the money has moved.&lt;/p&gt;

&lt;p&gt;It isn't.&lt;/p&gt;

&lt;p&gt;A successful broadcast usually means the node accepted the transaction for propagation or pending processing.&lt;/p&gt;

&lt;p&gt;It does not mean the transaction has been included in a block.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why can a broadcast error be misleading?
&lt;/h3&gt;

&lt;p&gt;Imagine our application sends a signed transaction.&lt;/p&gt;

&lt;p&gt;The Ethereum node receives it, but the network connection times out before our application receives the response.&lt;/p&gt;

&lt;p&gt;Our application sees an error.&lt;/p&gt;

&lt;p&gt;But did the node receive the transaction?&lt;/p&gt;

&lt;p&gt;Possibly.&lt;/p&gt;

&lt;p&gt;The transaction may already be in the node's mempool, even though our application never received the transaction hash response.&lt;/p&gt;

&lt;p&gt;This means:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A broadcast error does not necessarily mean the transaction was not broadcast.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Blindly creating and signing another transaction with a new nonce could result in duplicate withdrawals.&lt;/p&gt;

&lt;h3&gt;
  
  
  Sign → Save → Broadcast
&lt;/h3&gt;

&lt;p&gt;A safer approach is:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Build the transaction.&lt;/li&gt;
&lt;li&gt;Sign it.&lt;/li&gt;
&lt;li&gt;Save the signed transaction and its hash.&lt;/li&gt;
&lt;li&gt;Broadcast the saved transaction.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Why save it first?&lt;/p&gt;

&lt;p&gt;Because if the application crashes after signing or broadcasting, we still have the exact transaction bytes.&lt;/p&gt;

&lt;p&gt;We can retry broadcasting the same signed transaction instead of creating a completely new transaction.&lt;/p&gt;

&lt;p&gt;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;Withdrawal request
       |
       v
Build transaction
       |
       v
Sign transaction
       |
       v
Save signed transaction
       |
       v
Broadcast transaction
       |
       v
Track transaction hash
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  What about idempotency?
&lt;/h3&gt;

&lt;p&gt;Imagine a client sends a withdrawal request with an idempotency key.&lt;/p&gt;

&lt;p&gt;The request reaches our server, but the client times out and retries.&lt;/p&gt;

&lt;p&gt;If we process the second request as a completely new withdrawal, we might send the money twice.&lt;/p&gt;

&lt;p&gt;Instead, we should associate the idempotency key with the original withdrawal.&lt;/p&gt;

&lt;p&gt;When the same request arrives again, we return the existing withdrawal result rather than creating another withdrawal.&lt;/p&gt;

&lt;p&gt;The idempotency key protects the application-level operation.&lt;/p&gt;

&lt;p&gt;The saved signed transaction protects the blockchain-level broadcast retry.&lt;/p&gt;

&lt;p&gt;These are related, but they solve different problems. (In my code both live in &lt;a href="https://github.com/mohsenm4/blockchain-insight/blob/v2.0.0/internal/api/send.go" rel="noopener noreferrer"&gt;&lt;code&gt;internal/api/send.go&lt;/code&gt;&lt;/a&gt;.)&lt;/p&gt;




&lt;h2&gt;
  
  
  5. The Receipt
&lt;/h2&gt;

&lt;p&gt;After broadcasting, we need to track the transaction until it is included in a block.&lt;/p&gt;

&lt;p&gt;This is where &lt;code&gt;eth_getTransactionReceipt&lt;/code&gt; becomes important.&lt;/p&gt;

&lt;p&gt;A receipt contains information about the execution of a transaction.&lt;/p&gt;

&lt;p&gt;It includes fields such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Transaction hash&lt;/li&gt;
&lt;li&gt;Block number&lt;/li&gt;
&lt;li&gt;Block hash&lt;/li&gt;
&lt;li&gt;Gas used&lt;/li&gt;
&lt;li&gt;Effective gas price&lt;/li&gt;
&lt;li&gt;Execution status&lt;/li&gt;
&lt;li&gt;Logs&lt;/li&gt;
&lt;/ul&gt;

&lt;h3&gt;
  
  
  What does NotFound mean?
&lt;/h3&gt;

&lt;p&gt;If we query for a receipt and get &lt;code&gt;NotFound&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;, it means the node currently has no receipt for that transaction.&lt;/p&gt;

&lt;p&gt;It does not necessarily mean the transaction failed.&lt;/p&gt;

&lt;p&gt;Possible explanations include:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The transaction is still pending.&lt;/li&gt;
&lt;li&gt;The transaction has not reached this node.&lt;/li&gt;
&lt;li&gt;The transaction was dropped.&lt;/li&gt;
&lt;li&gt;The transaction was replaced.&lt;/li&gt;
&lt;li&gt;The transaction was not successfully broadcast.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A pending transaction does not have a receipt yet.&lt;/p&gt;

&lt;h3&gt;
  
  
  Status 1 vs Status 0
&lt;/h3&gt;

&lt;p&gt;For modern Ethereum receipts:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;Status = 1&lt;/code&gt;: The transaction execution succeeded.&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;Status = 0&lt;/code&gt;: The transaction execution failed.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;But what does failure mean?&lt;/p&gt;

&lt;p&gt;Consider an ERC-20 transfer.&lt;/p&gt;

&lt;p&gt;The transaction might be included in a block, but the smart contract could revert during execution.&lt;/p&gt;

&lt;p&gt;In that case, the transaction has a receipt with &lt;code&gt;Status = 0&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;It failed, but it was still included in the blockchain.&lt;/p&gt;

&lt;h3&gt;
  
  
  Does a failed transaction cost gas?
&lt;/h3&gt;

&lt;p&gt;Yes.&lt;/p&gt;

&lt;p&gt;The sender still pays for the gas consumed during execution, even if the transaction reverts.&lt;/p&gt;

&lt;p&gt;The transaction also consumes its nonce.&lt;/p&gt;

&lt;p&gt;This is an important distinction:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;A failed transaction is not the same as a transaction that was never included.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;For an exchange, this means we must not mark a withdrawal as successful just because it has a transaction hash or a receipt.&lt;/p&gt;

&lt;p&gt;We must check the receipt status.&lt;/p&gt;




&lt;h2&gt;
  
  
  6. The Other Side: When Is a Deposit Really Money?
&lt;/h2&gt;

&lt;p&gt;Withdrawals are only one side of an exchange.&lt;/p&gt;

&lt;p&gt;Deposits introduce another important problem: when can we safely credit a user's balance?&lt;/p&gt;

&lt;p&gt;Imagine someone sends 100 USDC to one of our deposit addresses.&lt;/p&gt;

&lt;p&gt;Our blockchain watcher detects the ERC-20 transfer event.&lt;/p&gt;

&lt;p&gt;Should we immediately add 100 USDC to the user's exchange balance?&lt;/p&gt;

&lt;p&gt;Not necessarily.&lt;/p&gt;

&lt;p&gt;The block containing that event could be removed from the canonical chain during a reorganization.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why wait for 12 confirmations?
&lt;/h3&gt;

&lt;p&gt;A confirmation means that a block containing our transaction has additional blocks built on top of it.&lt;/p&gt;

&lt;p&gt;In my code, a deposit in block 100 is checked when the chain head reaches block 112: &lt;code&gt;head − block_number &amp;gt;= 12&lt;/code&gt;, so 12 blocks are on top of it.&lt;/p&gt;

&lt;p&gt;Waiting for more confirmations makes a reorganization affecting the deposit less likely.&lt;/p&gt;

&lt;p&gt;In this project, 12 confirmations are the chosen policy (&lt;code&gt;CONFIRMATIONS&lt;/code&gt;, default 12).&lt;/p&gt;

&lt;p&gt;This is a risk-management decision, not a universal Ethereum rule or a guarantee of absolute finality.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why check the same block hash?
&lt;/h3&gt;

&lt;p&gt;Counting confirmations is not enough.&lt;/p&gt;

&lt;p&gt;Suppose our watcher detects a deposit in block 100.&lt;/p&gt;

&lt;p&gt;It stores:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Block number: 100&lt;/li&gt;
&lt;li&gt;Block hash: &lt;code&gt;0xABC...&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Later, a reorganization replaces block 100 with another block at the same height.&lt;/p&gt;

&lt;p&gt;The new block might have a different hash.&lt;/p&gt;

&lt;p&gt;If we only check the block number, we could incorrectly assume the original deposit is still valid.&lt;/p&gt;

&lt;p&gt;That's why we also check the block hash.&lt;/p&gt;

&lt;p&gt;The block number tells us the position in the chain.&lt;/p&gt;

&lt;p&gt;The block hash tells us which exact block we are talking about.&lt;/p&gt;

&lt;p&gt;We should verify that the block at the original height still has the expected hash.&lt;/p&gt;

&lt;h3&gt;
  
  
  What happens during a reorg?
&lt;/h3&gt;

&lt;p&gt;A reorganization happens when the canonical chain changes.&lt;/p&gt;

&lt;p&gt;Imagine this sequence:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Deposit detected
       |
       v
Pending deposit
       |
       v
12 confirmations
       |
       v
Block hash verification
       |
       v
Confirmed deposit
       |
       v
Credited to user
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now imagine the block containing the deposit is removed before the deposit is credited.&lt;/p&gt;

&lt;p&gt;The deposit should no longer be treated as a valid pending confirmation.&lt;/p&gt;

&lt;p&gt;My tracker (&lt;a href="https://github.com/mohsenm4/blockchain-insight/blob/v2.0.0/internal/confirm/confirm.go" rel="noopener noreferrer"&gt;&lt;code&gt;internal/confirm&lt;/code&gt;&lt;/a&gt;) asks the node for the transaction receipt. If the transaction is now in another block, the deposit moves there and counts its confirmations again. If it is in no block, or it failed, the deposit becomes &lt;code&gt;reverted&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;If the deposit was already credited, the system needs a compensating accounting operation according to its ledger and risk policy. (I haven't built this part yet: it is next on my roadmap.)&lt;/p&gt;

&lt;p&gt;We should not simply delete the original database row.&lt;/p&gt;

&lt;p&gt;Why?&lt;/p&gt;

&lt;p&gt;Because financial systems need an audit trail.&lt;/p&gt;

&lt;p&gt;Deleting the row would remove evidence that the deposit was previously detected and processed.&lt;/p&gt;

&lt;p&gt;Instead, we should preserve the original record and update its state or create a compensating ledger entry.&lt;/p&gt;

&lt;h3&gt;
  
  
  Confirmed vs Credited
&lt;/h3&gt;

&lt;p&gt;These two statuses represent different events.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Confirmed&lt;/strong&gt; means the deposit has passed the required blockchain checks.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Credited&lt;/strong&gt; means the exchange has actually added the corresponding amount to the user's internal balance.&lt;/p&gt;

&lt;p&gt;Why separate them?&lt;/p&gt;

&lt;p&gt;Because blockchain confirmation and internal accounting are different responsibilities.&lt;/p&gt;

&lt;p&gt;A deposit can be confirmed while the crediting operation is still waiting to be processed.&lt;/p&gt;

&lt;p&gt;Separating these states also makes recovery easier if a worker crashes.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why use one database transaction?
&lt;/h3&gt;

&lt;p&gt;Imagine our application does two operations:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Marks a deposit as credited.&lt;/li&gt;
&lt;li&gt;Adds the deposit amount to the user's balance.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;What happens if the first operation succeeds but the second fails?&lt;/p&gt;

&lt;p&gt;We would have a deposit marked as credited without the user receiving the money.&lt;/p&gt;

&lt;p&gt;The opposite problem is also possible: the balance increases, but the deposit still appears uncredited.&lt;/p&gt;

&lt;p&gt;Both are dangerous.&lt;/p&gt;

&lt;p&gt;These database changes should happen atomically.&lt;/p&gt;

&lt;p&gt;Either both operations succeed, or neither does.&lt;/p&gt;

&lt;p&gt;In my ledger it looks like this (inside one transaction):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight sql"&gt;&lt;code&gt;&lt;span class="c1"&gt;-- 1. only a confirmed deposit can become credited: this must change exactly 1 row&lt;/span&gt;
&lt;span class="k"&gt;UPDATE&lt;/span&gt; &lt;span class="n"&gt;deposits&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'credited'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;credited_at_block&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;
&lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt; &lt;span class="k"&gt;AND&lt;/span&gt; &lt;span class="n"&gt;status&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="s1"&gt;'confirmed'&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

&lt;span class="c1"&gt;-- 2. add the amount to the balance (create the row if it is the first deposit)&lt;/span&gt;
&lt;span class="k"&gt;INSERT&lt;/span&gt; &lt;span class="k"&gt;INTO&lt;/span&gt; &lt;span class="n"&gt;balances&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;SELECT&lt;/span&gt; &lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="k"&gt;FROM&lt;/span&gt; &lt;span class="n"&gt;deposits&lt;/span&gt; &lt;span class="k"&gt;WHERE&lt;/span&gt; &lt;span class="n"&gt;id&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="err"&gt;$&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
&lt;span class="k"&gt;ON&lt;/span&gt; &lt;span class="n"&gt;CONFLICT&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user_id&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;token&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;DO&lt;/span&gt; &lt;span class="k"&gt;UPDATE&lt;/span&gt;
&lt;span class="k"&gt;SET&lt;/span&gt; &lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;balances&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="n"&gt;EXCLUDED&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;amount&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;WHERE status = 'confirmed'&lt;/code&gt; is what makes it safe: if two workers try to credit the same deposit, the second one waits for the row lock, then changes 0 rows, and the code stops.&lt;/p&gt;

&lt;p&gt;The exact implementation can vary, but the accounting invariant must remain the same.&lt;/p&gt;




&lt;h2&gt;
  
  
  7. Three Things I'd Tell Myself at the Start
&lt;/h2&gt;

&lt;p&gt;If I were starting this project again, these are three things I would remind myself of:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;1. A transaction hash is not proof of success.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Signing, broadcasting, inclusion, execution success, and confirmation are different stages. Always track them separately.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. Nonce management is an application-level responsibility.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Reading the nonce from Ethereum is not enough when multiple workers can send transactions from the same account.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. Financial operations need to be recoverable.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Applications crash. RPC requests time out. Transactions get replaced. Blocks get reorganized.&lt;/p&gt;

&lt;p&gt;A good backend should be able to recover without sending money twice or losing track of an operation.&lt;/p&gt;

&lt;p&gt;Building an Ethereum transaction service taught me that interacting with a blockchain is not just about calling RPC methods.&lt;/p&gt;

&lt;p&gt;It is about designing a reliable system around an unreliable environment.&lt;/p&gt;

&lt;p&gt;And that is what makes the difference between sending a transaction and building software that can safely handle money.&lt;/p&gt;




&lt;p&gt;&lt;strong&gt;Source code:&lt;/strong&gt; &lt;a href="https://github.com/mohsenm4/blockchain-insight" rel="noopener noreferrer"&gt;blockchain-insight on GitHub&lt;/a&gt; · &lt;a href="https://github.com/mohsenm4/blockchain-insight/releases/tag/v2.0.0" rel="noopener noreferrer"&gt;v2.0.0&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Part 1:&lt;/strong&gt; &lt;a href="https://dev.to/mohsenm4/ethereum-cryptography-from-a-go-devs-view-47h8"&gt;Ethereum cryptography from a Go dev's view&lt;/a&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>ethereum</category>
      <category>blockchain</category>
      <category>backend</category>
    </item>
    <item>
      <title>What I learned from reading Go's chan.go</title>
      <dc:creator>Mohsen</dc:creator>
      <pubDate>Thu, 24 Sep 2026 07:11:04 +0000</pubDate>
      <link>https://dev.to/mohsenm4/what-i-learned-from-reading-gos-chango-4jbj</link>
      <guid>https://dev.to/mohsenm4/what-i-learned-from-reading-gos-chango-4jbj</guid>
      <description>&lt;p&gt;Before reading &lt;code&gt;chan.go&lt;/code&gt;, my mental model of a channel was one sentence: &lt;em&gt;a safe way for goroutines to send values to each other.&lt;/em&gt; That sentence was true, but it was not enough. I could not answer simple follow-up questions: where does a blocked goroutine actually wait? Is an unbuffered channel secretly a buffer of size one? Why does &lt;code&gt;select&lt;/code&gt; pick a random case?&lt;/p&gt;

&lt;p&gt;So I spent a week reading &lt;code&gt;src/runtime/chan.go&lt;/code&gt; (and later &lt;code&gt;select.go&lt;/code&gt;) from Go 1.26. This post is what I found, written as the questions I had.&lt;/p&gt;

&lt;h2&gt;
  
  
  1. What is a channel, really?
&lt;/h2&gt;

&lt;p&gt;Every &lt;code&gt;make(chan T, n)&lt;/code&gt; gives you a pointer to an &lt;code&gt;hchan&lt;/code&gt;. Here it is, lightly trimmed:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;type&lt;/span&gt; &lt;span class="n"&gt;hchan&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;qcount&lt;/span&gt;   &lt;span class="kt"&gt;uint&lt;/span&gt;           &lt;span class="c"&gt;// values currently in the buffer&lt;/span&gt;
    &lt;span class="n"&gt;dataqsiz&lt;/span&gt; &lt;span class="kt"&gt;uint&lt;/span&gt;           &lt;span class="c"&gt;// buffer capacity (the n in make)&lt;/span&gt;
    &lt;span class="n"&gt;buf&lt;/span&gt;      &lt;span class="n"&gt;unsafe&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Pointer&lt;/span&gt; &lt;span class="c"&gt;// the ring buffer itself&lt;/span&gt;
    &lt;span class="n"&gt;elemsize&lt;/span&gt; &lt;span class="kt"&gt;uint16&lt;/span&gt;
    &lt;span class="n"&gt;closed&lt;/span&gt;   &lt;span class="kt"&gt;uint32&lt;/span&gt;
    &lt;span class="n"&gt;elemtype&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;_type&lt;/span&gt;
    &lt;span class="n"&gt;sendx&lt;/span&gt;    &lt;span class="kt"&gt;uint&lt;/span&gt;  &lt;span class="c"&gt;// next slot to write&lt;/span&gt;
    &lt;span class="n"&gt;recvx&lt;/span&gt;    &lt;span class="kt"&gt;uint&lt;/span&gt;  &lt;span class="c"&gt;// next slot to read&lt;/span&gt;
    &lt;span class="n"&gt;recvq&lt;/span&gt;    &lt;span class="n"&gt;waitq&lt;/span&gt; &lt;span class="c"&gt;// goroutines blocked on receive&lt;/span&gt;
    &lt;span class="n"&gt;sendq&lt;/span&gt;    &lt;span class="n"&gt;waitq&lt;/span&gt; &lt;span class="c"&gt;// goroutines blocked on send&lt;/span&gt;
    &lt;span class="n"&gt;lock&lt;/span&gt;     &lt;span class="n"&gt;mutex&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Three groups of fields: a &lt;strong&gt;ring buffer&lt;/strong&gt; (&lt;code&gt;buf&lt;/code&gt;, &lt;code&gt;sendx&lt;/code&gt;, &lt;code&gt;recvx&lt;/code&gt;, &lt;code&gt;qcount&lt;/code&gt;), &lt;strong&gt;two wait queues&lt;/strong&gt; (&lt;code&gt;recvq&lt;/code&gt;, &lt;code&gt;sendq&lt;/code&gt;), and &lt;strong&gt;one lock&lt;/strong&gt; that protects all of it.&lt;/p&gt;

&lt;p&gt;The lock is &lt;code&gt;runtime.mutex&lt;/code&gt;, not &lt;code&gt;sync.Mutex&lt;/code&gt;. At first I thought that was just "the runtime likes its own tools". The real reason is circularity: &lt;code&gt;sync.Mutex&lt;/code&gt; parks goroutines through the scheduler, and the scheduler is exactly the code that uses &lt;code&gt;hchan&lt;/code&gt;. The runtime cannot build its foundation on top of something that is built on top of it.&lt;/p&gt;

&lt;h2&gt;
  
  
  2. Is an unbuffered channel a buffer of size one?
&lt;/h2&gt;

&lt;p&gt;No. When &lt;code&gt;dataqsiz == 0&lt;/code&gt;, &lt;code&gt;buf&lt;/code&gt; holds nothing. A send and a receive have to &lt;em&gt;meet&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If a receiver is already waiting, the sender copies its value &lt;strong&gt;directly into the receiver's variable&lt;/strong&gt;, which lives on the receiver's stack. The source has a comment about this that surprised me:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Sends and receives on unbuffered or empty-buffered channels are the only operations where one running goroutine writes to the stack of another running goroutine.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;That is why an unbuffered channel is a synchronization point and not just a pipe: there is no place for the value to sit, so both sides must be there.&lt;/p&gt;

&lt;h2&gt;
  
  
  3. How many ways can a send end?
&lt;/h2&gt;

&lt;p&gt;Reading &lt;code&gt;chansend&lt;/code&gt; top to bottom, I counted these paths (for a normal, blocking send):&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;nil channel&lt;/strong&gt;: the goroutine parks forever. There is no one to wake it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;closed channel&lt;/strong&gt;: &lt;code&gt;panic("send on closed channel")&lt;/code&gt;. It does not return quietly.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;a receiver is waiting&lt;/strong&gt; in &lt;code&gt;recvq&lt;/code&gt;: dequeue it and hand the value over directly, even if the channel is buffered. The buffer is skipped.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;buffer has space&lt;/strong&gt;: copy into &lt;code&gt;buf[sendx]&lt;/code&gt;, advance &lt;code&gt;sendx&lt;/code&gt;, done.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;otherwise&lt;/strong&gt;: wrap this goroutine in a &lt;code&gt;sudog&lt;/code&gt;, put it on &lt;code&gt;sendq&lt;/code&gt;, and &lt;code&gt;gopark&lt;/code&gt;.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Path 3 was the most surprising one for me. If a receiver is already waiting, the value never touches the buffer. The source even writes this as an invariant: in a buffered channel, &lt;code&gt;qcount &amp;gt; 0&lt;/code&gt; implies &lt;code&gt;recvq&lt;/code&gt; is empty. You never have data in the buffer &lt;em&gt;and&lt;/em&gt; a waiting receiver at the same time.&lt;/p&gt;

&lt;h2&gt;
  
  
  4. Full buffer and a waiting sender: who gets what?
&lt;/h2&gt;

&lt;p&gt;Say the buffer is full and a sender is parked on &lt;code&gt;sendq&lt;/code&gt;. Now a receiver arrives. The naive idea is "give the receiver the sender's value". That would break FIFO order, because older values are still in the buffer.&lt;/p&gt;

&lt;p&gt;What &lt;code&gt;recv&lt;/code&gt; actually does:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="c"&gt;// Queue is full. Take the item at the head of the queue.&lt;/span&gt;
&lt;span class="c"&gt;// Make the sender enqueue its item at the tail of the queue.&lt;/span&gt;
&lt;span class="c"&gt;// Since the queue is full, those are both the same slot.&lt;/span&gt;
&lt;span class="n"&gt;qp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;chanbuf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recvx&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;typedmemmove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;elemtype&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ep&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qp&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;                &lt;span class="c"&gt;// oldest value -&amp;gt; receiver&lt;/span&gt;
&lt;span class="n"&gt;typedmemmove&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;elemtype&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;qp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sg&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;elem&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;get&lt;/span&gt;&lt;span class="p"&gt;())&lt;/span&gt;     &lt;span class="c"&gt;// sender's value -&amp;gt; freed slot&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recvx&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recvx&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;dataqsiz&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recvx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;recvx&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The receiver takes the oldest value, and the waiting sender's value drops into the slot that just became free. Because the buffer is full, the head and the tail are the same slot. One read, one write, order preserved. I think this is the nicest piece of code in the file.&lt;/p&gt;

&lt;h2&gt;
  
  
  5. What is a &lt;code&gt;sudog&lt;/code&gt;, and why not just queue the goroutine?
&lt;/h2&gt;

&lt;p&gt;A &lt;code&gt;sudog&lt;/code&gt; is "a goroutine waiting on one thing". It holds a pointer to the goroutine (&lt;code&gt;g&lt;/code&gt;), a pointer to the value being sent or received (&lt;code&gt;elem&lt;/code&gt;), and links for the wait queue.&lt;/p&gt;

&lt;p&gt;Why a separate struct instead of putting the goroutine itself in &lt;code&gt;recvq&lt;/code&gt;? Because &lt;strong&gt;one goroutine can wait on many channels at once&lt;/strong&gt;. That is exactly what &lt;code&gt;select&lt;/code&gt; does. Each case gets its own &lt;code&gt;sudog&lt;/code&gt;, all pointing to the same goroutine. When one of them fires, the runtime has to remove the others. So the relation is one goroutine to many &lt;code&gt;sudog&lt;/code&gt;s, and the queue holds &lt;code&gt;sudog&lt;/code&gt;s.&lt;/p&gt;

&lt;h2&gt;
  
  
  6. How does &lt;code&gt;select&lt;/code&gt; use all of this?
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;select.go&lt;/code&gt; builds on the same pieces. &lt;code&gt;selectgo&lt;/code&gt; works in three passes:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Poll&lt;/strong&gt;: look at every case in a &lt;em&gt;random&lt;/em&gt; order (&lt;code&gt;pollorder&lt;/code&gt;). If one is ready, do it and return. Random order is for fairness: a busy channel written first in your code should not starve the others.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enqueue and park&lt;/strong&gt;: nothing ready and no &lt;code&gt;default&lt;/code&gt;? Put a &lt;code&gt;sudog&lt;/code&gt; on the queue of &lt;strong&gt;every&lt;/strong&gt; channel in the select, then &lt;code&gt;gopark&lt;/code&gt; once.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Wake up and clean up&lt;/strong&gt;: whoever woke us set &lt;code&gt;gp.param&lt;/code&gt; to the winning &lt;code&gt;sudog&lt;/code&gt;. Dequeue all the other &lt;code&gt;sudog&lt;/code&gt;s from their channels.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Before touching the channels, &lt;code&gt;select&lt;/code&gt; locks them all. The lock order is not the order in your code. It is sorted by the address of each &lt;code&gt;hchan&lt;/code&gt; (&lt;code&gt;lockorder&lt;/code&gt;). If two goroutines run selects over the same channels in opposite order, a code-order lock could deadlock. A single global order (addresses) makes that impossible.&lt;/p&gt;

&lt;p&gt;One practical trick falls out of this: a &lt;strong&gt;nil channel case is never ready&lt;/strong&gt;. The runtime just leaves it out. So inside a loop you can set &lt;code&gt;ch = nil&lt;/code&gt; to turn a case off without restructuring the select.&lt;/p&gt;

&lt;p&gt;Honest note: I read &lt;code&gt;selectgo&lt;/code&gt; carefully, wrote notes, and five days later I could only explain half of it (I scored myself 5/10). Things I only &lt;em&gt;read&lt;/em&gt; faded. Things I &lt;em&gt;built&lt;/em&gt; stayed. That is the reason for the next section.&lt;/p&gt;

&lt;h2&gt;
  
  
  7. I built one myself
&lt;/h2&gt;

&lt;p&gt;To check whether I understood &lt;code&gt;hchan&lt;/code&gt;, I wrote a bounded channel with a &lt;code&gt;sync.Mutex&lt;/code&gt; and two &lt;code&gt;sync.Cond&lt;/code&gt;s (&lt;code&gt;notFull&lt;/code&gt;, &lt;code&gt;notEmpty&lt;/code&gt;).&lt;/p&gt;

&lt;p&gt;My first version stored items in a slice and received with &lt;code&gt;buf = buf[1:]&lt;/code&gt;. It worked in small tests. The problem: re-slicing moves the start forward, and the space at the beginning is never reused. After enough sends and receives, the buffer looked full even when it had free space. The fix was the same idea as &lt;code&gt;hchan&lt;/code&gt;: a fixed array with a read index and a write index that wrap around.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;BoundedChannelTest&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="n"&gt;Send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="n"&gt;T&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Lock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
    &lt;span class="k"&gt;defer&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;mu&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Unlock&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;

    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;closed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"send on closed channel"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;notFull&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Wait&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="c"&gt;// releases mu while waiting&lt;/span&gt;
        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;closed&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="nb"&gt;panic&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"send on closed channel"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;

    &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendx&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendx&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;sendx&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buf&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;count&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt;
    &lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;notEmpty&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Signal&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I even named the indexes &lt;code&gt;sendx&lt;/code&gt; and &lt;code&gt;recvx&lt;/code&gt;, like &lt;code&gt;hchan&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;A week later I closed it and rewrote it from memory, without notes or AI. It passed with &lt;code&gt;go test -race&lt;/code&gt; (5 producers, 5 consumers, 1000 items each). That rebuild is what convinced me I understood it.&lt;/p&gt;

&lt;p&gt;What my version does &lt;em&gt;not&lt;/em&gt; have, compared to the real one: direct handoff to a waiting receiver (path 3), &lt;code&gt;sudog&lt;/code&gt;s, or any way to take part in a &lt;code&gt;select&lt;/code&gt;. &lt;code&gt;sync.Cond&lt;/code&gt; wakes someone up. &lt;code&gt;hchan&lt;/code&gt; knows exactly who is waiting and gives them the value.&lt;/p&gt;

&lt;h2&gt;
  
  
  8. What I take away
&lt;/h2&gt;

&lt;p&gt;A Go channel is not just a queue. It is a ring buffer, two wait queues, and a lock, plus a scheduler that can park and wake goroutines and copy values straight between their stacks.&lt;/p&gt;

&lt;p&gt;After &lt;code&gt;chan.go&lt;/code&gt;, the rest of the &lt;code&gt;sync&lt;/code&gt; package read differently. &lt;code&gt;sync.Once&lt;/code&gt; is a mutex plus an atomic flag, and the detail I missed when I rebuilt it was that the standard library marks it done with a &lt;code&gt;defer&lt;/code&gt;, so even a panicking &lt;code&gt;f&lt;/code&gt; counts as "done". &lt;code&gt;sync.Pool&lt;/code&gt; mostly avoids locks by giving each P (the scheduler's processor) its own local pool. Same questions every time: where does the data live, who waits, and who wakes them?&lt;/p&gt;

&lt;p&gt;If you are learning Go concurrency, my advice is: read &lt;code&gt;chan.go&lt;/code&gt; once, then close it and build a small version yourself. The reading gives you the words. The building makes them stay.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Source: Go 1.26, &lt;code&gt;src/runtime/chan.go&lt;/code&gt; and &lt;code&gt;src/runtime/select.go&lt;/code&gt;. Exercises: &lt;a href="https://github.com/mohsenm4/go-fundamentals" rel="noopener noreferrer"&gt;github.com/mohsenm4/go-fundamentals&lt;/a&gt;.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Ethereum cryptography from a Go dev's view</title>
      <dc:creator>Mohsen</dc:creator>
      <pubDate>Sun, 13 Sep 2026 06:20:02 +0000</pubDate>
      <link>https://dev.to/mohsenm4/ethereum-cryptography-from-a-go-devs-view-47h8</link>
      <guid>https://dev.to/mohsenm4/ethereum-cryptography-from-a-go-devs-view-47h8</guid>
      <description>&lt;p&gt;I built an Ethereum wallet CLI in Go because I wanted to understand what a wallet actually does. Not just:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;generate a key
create an address
sign a transaction
done
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That was my mental model at the beginning. After implementing the pieces myself, I realized a wallet is less like a key container and more like a collection of cryptographic decisions, where small implementation details can break compatibility, correctness, or security.&lt;/p&gt;

&lt;p&gt;This post is about what I learned while building &lt;a href="https://github.com/mohsenm4/mini-wallet" rel="noopener noreferrer"&gt;mini-wallet&lt;/a&gt;, and the cryptography details that surprised me.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The project is built for learning, not production key management.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Why I built a wallet CLI instead of just reading about crypto
&lt;/h2&gt;

&lt;p&gt;Reading about cryptography is useful. Implementing it is different.&lt;/p&gt;

&lt;p&gt;Before this project, my view of wallets was much simpler: I thought they were mostly responsible for transferring assets. Once I started implementing the pieces myself, I started seeing the hidden complexity:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;How random data becomes a human-readable recovery phrase.&lt;/li&gt;
&lt;li&gt;How one seed generates thousands of keys.&lt;/li&gt;
&lt;li&gt;How signatures can recover public keys.&lt;/li&gt;
&lt;li&gt;Why different tools sometimes represent the same value differently.&lt;/li&gt;
&lt;li&gt;Why compatibility tests matter as much as the algorithms themselves.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;I chose to build a CLI wallet because you understand a system differently when you are responsible for making every part work. A library can hide complexity; building the library exposes it.&lt;/p&gt;

&lt;h2&gt;
  
  
  Entropy → mnemonic (BIP39)
&lt;/h2&gt;

&lt;p&gt;Most wallets start with entropy. For a 12-word mnemonic, BIP39 starts with 128 bits of random entropy, then calculates SHA-256 of that entropy and takes the first 4 bits as the checksum:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;128 bits entropy + 4 bits checksum = 132 bits
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Those 132 bits are split into groups of 11 bits. Each 11-bit number is an index between 0 and 2047, and BIP39 has a word list with exactly 2048 words, so each index maps to one word. The result is the 12-word mnemonic.&lt;/p&gt;

&lt;p&gt;The interesting part for me was the checksum. The wallet does not store an extra "checksum field" — the checksum is embedded in the words themselves. In my implementation it is calculated directly from the entropy:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;checksumBits&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;bits&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="m"&gt;32&lt;/span&gt;

&lt;span class="n"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sha256&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sum256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;entropy&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;firstByteBits&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%08b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;hash&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt;
&lt;span class="n"&gt;checksum&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;firstByteBits&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="n"&gt;checksumBits&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;

&lt;span class="n"&gt;bitstring&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="s"&gt;""&lt;/span&gt;
&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;entropy&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;bitstring&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"%08b"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;b&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;bitstring&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;checksum&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The resulting bit string is then split into 11-bit chunks and mapped to the BIP39 word list:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;entropy
   |
SHA-256
   |
checksum bits
   |
entropy + checksum
   |
11-bit groups
   |
BIP39 words
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When recovering the wallet, the process runs in reverse: the words are converted back into bits, the entropy is separated from the claimed checksum, SHA-256 is calculated again, and the two checksums are compared. A handful of bits is enough to catch most human input mistakes.&lt;/p&gt;

&lt;h2&gt;
  
  
  Seed → keys (BIP32/BIP44)
&lt;/h2&gt;

&lt;p&gt;The mnemonic is not the private key. It is used to generate a seed, and that seed becomes the root of a hierarchical deterministic (HD) wallet:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;mnemonic
   |
   v
seed
   |
   v
master key
   |
   +---- account
   |       |
   |       +---- address
   |       +---- address
   |
   +---- account
           |
           +---- address
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;BIP32 lets us derive a whole tree of keys from one master key. The most important detail here is the difference between hardened and non-hardened derivation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Hardened derivation
&lt;/h3&gt;

&lt;p&gt;With hardened derivation, creating a child key requires the parent private key — an xpub alone is not enough. In the path parser, I mark hardened indices by adding &lt;code&gt;HardenedOffset&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;hardened&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;strings&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;HasSuffix&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;"'"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;hardened&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;seg&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;seg&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seg&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;strconv&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ParseUint&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;seg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;10&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;32&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Errorf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"invalid path segment %q: %w"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;seg&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="kt"&gt;uint32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;hardened&lt;/span&gt; &lt;span class="o"&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;=&lt;/span&gt; &lt;span class="n"&gt;HardenedOffset&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Errorf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"non-hardened index %d exceeds 2^31"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;index&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;hardened&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;+=&lt;/span&gt; &lt;span class="n"&gt;HardenedOffset&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;44'  -&amp;gt; 44 + 0x80000000
60'  -&amp;gt; 60 + 0x80000000
0    -&amp;gt; 0
0    -&amp;gt; 0
5    -&amp;gt; 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  Non-hardened derivation
&lt;/h3&gt;

&lt;p&gt;With non-hardened derivation, someone with only an xpub can derive child public keys. That is useful for watch-only wallets, but it has a dangerous edge case. If an attacker has:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;the parent xpub&lt;/li&gt;
&lt;li&gt;one child private key&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;they can compute the parent private key — and once that is compromised, the whole subtree below it is compromised too. This is the well-known xpub + child-private-key attack, and it is why the first three levels of the path are hardened.&lt;/p&gt;

&lt;p&gt;Ethereum uses the BIP44 path:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;m/44'/60'/0'/0/n
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Meaning:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;code&gt;m&lt;/code&gt; → master key&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;44'&lt;/code&gt; → BIP44 purpose&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;60'&lt;/code&gt; → Ethereum&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0'&lt;/code&gt; → account index&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;0&lt;/code&gt; → change&lt;/li&gt;
&lt;li&gt;
&lt;code&gt;n&lt;/code&gt; → address index&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The default path is also what my CLI uses when deriving Ethereum addresses.&lt;/p&gt;

&lt;h2&gt;
  
  
  secp256k1 signing and recovery
&lt;/h2&gt;

&lt;p&gt;Ethereum uses the secp256k1 elliptic curve. A signature is usually written as:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;(r, s, v)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The interesting part is &lt;code&gt;v&lt;/code&gt;. Many cryptographic systems send the public key together with the signature. Ethereum does something different: the signature carries enough information to &lt;em&gt;recover&lt;/em&gt; the public key.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;signature
    |
    v
recover public key
    |
    v
derive Ethereum address
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;This matters because an Ethereum transaction has no &lt;code&gt;from&lt;/code&gt; field. The sender is recovered from the signature, which saves 64 bytes of public key per transaction.&lt;/p&gt;

&lt;p&gt;It is also where compatibility problems start showing up, because different tools represent &lt;code&gt;v&lt;/code&gt; differently:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;go-ethereum's &lt;code&gt;SigToPub&lt;/code&gt; expects a recovery ID of &lt;code&gt;0/1&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;wallet tooling can provide &lt;code&gt;27/28&lt;/code&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;They carry the same recovery information, but they are not interchangeable byte-for-byte. I ran into exactly this problem. Instead of teaching the CLI about every wallet's signature format, I moved the compatibility logic into the signer layer:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;64&lt;/span&gt;&lt;span class="p"&gt;];&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;27&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="m"&gt;28&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;normalised&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;65&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="nb"&gt;copy&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;normalised&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;sig&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;normalised&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;64&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;v&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="m"&gt;27&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;hash&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;hashPersonalMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;pubKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;SigToPub&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;hash&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Bytes&lt;/span&gt;&lt;span class="p"&gt;(),&lt;/span&gt; &lt;span class="n"&gt;normalised&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;common&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Address&lt;/span&gt;&lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;PubkeyToAddress&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;pubKey&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now &lt;code&gt;RecoverPersonal&lt;/code&gt; accepts either representation. A small detail — but exactly the kind that makes two otherwise-correct implementations incompatible.&lt;/p&gt;

&lt;p&gt;There is a third use of &lt;code&gt;v&lt;/code&gt; that is easy to confuse with these. For transactions, EIP-155 uses &lt;code&gt;v&lt;/code&gt; for replay protection:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;v = chainId * 2 + 35/36
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That is different from the &lt;code&gt;0/1&lt;/code&gt; recovery ID of the signing primitive and from the &lt;code&gt;27/28&lt;/code&gt; convention of personal-message signing. Same field, three conventions.&lt;/p&gt;

&lt;h2&gt;
  
  
  Keystore V3: encryption is more than encrypt/decrypt
&lt;/h2&gt;

&lt;p&gt;Ethereum keystore files protect private keys with three pieces:&lt;/p&gt;

&lt;h3&gt;
  
  
  scrypt
&lt;/h3&gt;

&lt;p&gt;scrypt is the key derivation function: it turns the user's password into a 32-byte derived key. That key is split in two:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;derivedKey&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;deriveKey&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;password&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;salt&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;KeystoreV3&lt;/span&gt;&lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;encKey&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;derivedKey&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="m"&gt;16&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;span class="n"&gt;macKey&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;derivedKey&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="m"&gt;16&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="m"&gt;32&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The first 16 bytes become the AES key; the remaining 16 bytes are used for the MAC.&lt;/p&gt;

&lt;h3&gt;
  
  
  AES-128-CTR
&lt;/h3&gt;

&lt;p&gt;The private key is encrypted using AES-128 in CTR mode:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;aes&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewCipher&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;encKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;KeystoreV3&lt;/span&gt;&lt;span class="p"&gt;{},&lt;/span&gt; &lt;span class="n"&gt;err&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;

&lt;span class="n"&gt;stream&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;cipher&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewCTR&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;block&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;iv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;ciphertext&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;make&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;
&lt;span class="n"&gt;stream&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;XORKeyStream&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;secret&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;CTR turns AES into a stream cipher, so the ciphertext is exactly as long as the private key and no padding is involved.&lt;/p&gt;

&lt;h3&gt;
  
  
  MAC
&lt;/h3&gt;

&lt;p&gt;The MAC protects the ciphertext against modification:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;keccak256(derivedKey[16:32] ‖ ciphertext)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In code:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;sha3&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;NewLegacyKeccak256&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;macKey&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;ciphertext&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;mac&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;h&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sum&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;So the overall flow looks like:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;password
   |
   v
  scrypt
   |
   v
derived key
   |
   +---- first 16 bytes ----&amp;gt; AES-128-CTR key
   |
   +---- last 16 bytes -----&amp;gt; MAC key
                                |
ciphertext &amp;lt;--------------------+
   |
   v
keccak256(macKey || ciphertext)
   |
   v
MAC
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At first, a simple round-trip test looks like enough:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;encrypt
   |
decrypt
   |
same private key
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But this can hide bugs: if encryption and decryption share the same mistake, the test still passes. That is why reference vectors matter — they test compatibility with the actual format, not with your own implementation.&lt;/p&gt;

&lt;p&gt;One of the bugs I hit was hard-coded scrypt parameters. The reference vector intentionally used:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;p = 8
r = 1
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;while my decryption path had the parameters hard-coded instead of reading them from the keystore JSON. My own round-trip tests passed, because both sides shared the same assumption. The reference vector did not. That was a much more useful failure.&lt;/p&gt;

&lt;h2&gt;
  
  
  Messages, not hashes
&lt;/h2&gt;

&lt;p&gt;A signature alone does not explain what was signed — a hash is just 32 bytes. The same signing primitive could be used for:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;a transaction&lt;/li&gt;
&lt;li&gt;a login message&lt;/li&gt;
&lt;li&gt;a smart contract interaction&lt;/li&gt;
&lt;li&gt;structured application data&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;This is why Ethereum has standards such as EIP-191 and EIP-712.&lt;/p&gt;

&lt;h3&gt;
  
  
  EIP-191
&lt;/h3&gt;

&lt;p&gt;For personal messages, the message is not hashed directly. Ethereum adds a prefix that includes the message length:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;"\x19Ethereum Signed Message:\n" + len(message) + message
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;My implementation does exactly that:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="n"&gt;hashPersonalMessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;common&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Hash&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;prefix&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sprintf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\x19&lt;/span&gt;&lt;span class="s"&gt;Ethereum Signed Message:&lt;/span&gt;&lt;span class="se"&gt;\n&lt;/span&gt;&lt;span class="s"&gt;%d"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;

    &lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;prefix&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;message&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Keccak256Hash&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;code&gt;\x19&lt;/code&gt; byte is not a valid start of an RLP-encoded transaction, so a signed message can never be replayed as a transaction. The signature is tied to the personal-message format instead of being a raw signature over arbitrary bytes.&lt;/p&gt;

&lt;h3&gt;
  
  
  EIP-712
&lt;/h3&gt;

&lt;p&gt;For structured data, EIP-712 goes further. Instead of an opaque blob, it defines typed data with a schema, a domain separator, and a structured hashing process. The final digest is:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;keccak256(
    "\x19\x01" ||
    domainSeparator ||
    hashStruct(message)
)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The last step of my implementation:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;([]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="se"&gt;\x19\x01&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;domainHash&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="n"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;append&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;messageHash&lt;/span&gt;&lt;span class="o"&gt;...&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;

&lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Sign&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;crypto&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Keccak256&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;priv&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The domain separator binds the signature to context such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;application&lt;/li&gt;
&lt;li&gt;version&lt;/li&gt;
&lt;li&gt;chain ID&lt;/li&gt;
&lt;li&gt;verifying contract&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In my tests, a signature made with &lt;code&gt;chainId = 1&lt;/code&gt; recovers to a different address when checked against &lt;code&gt;chainId = 999&lt;/code&gt; — that is the domain separator doing its job.&lt;/p&gt;

&lt;h2&gt;
  
  
  Three bugs my tests caught
&lt;/h2&gt;

&lt;p&gt;This was the most valuable part of the project. The biggest lessons did not come from implementing algorithms; they came from failing tests.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bug 1: &lt;code&gt;v&lt;/code&gt; format mismatch
&lt;/h3&gt;

&lt;p&gt;I took signatures with &lt;code&gt;v = 27/28&lt;/code&gt; from wallet tooling and passed them straight into &lt;code&gt;SigToPub&lt;/code&gt;, which expects &lt;code&gt;0/1&lt;/code&gt;. Recovery returned the wrong address.&lt;/p&gt;

&lt;p&gt;My first fix was inside the CLI command. Then I realized the conversion belonged in the signer layer: &lt;code&gt;RecoverPersonal&lt;/code&gt; should accept both formats and normalize before recovery.&lt;/p&gt;

&lt;p&gt;The lesson:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Compatibility logic should live where the concept belongs.&lt;/strong&gt;&lt;/p&gt;

&lt;h3&gt;
  
  
  Bug 2: hard-coded scrypt parameters
&lt;/h3&gt;

&lt;p&gt;My keystore tests passed — because encryption and decryption made the same assumption. The reference vector intentionally used &lt;code&gt;p=8, r=1&lt;/code&gt;, while my decryption path ignored the parameters in the keystore JSON. The implementation was consistently wrong, and only the external vector exposed it.&lt;/p&gt;

&lt;p&gt;The lesson:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Passing your own tests does not always mean you implemented the standard correctly.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Reference vectors are not optional polish for cryptographic code. They are part of the implementation.&lt;/p&gt;

&lt;h3&gt;
  
  
  Bug 3: unsafe type assertion in &lt;code&gt;HashStruct&lt;/code&gt;
&lt;/h3&gt;

&lt;p&gt;The third bug was in EIP-712 &lt;code&gt;HashStruct&lt;/code&gt;. When processing a nested typed-data field, I assumed the value was always a &lt;code&gt;map[string]any&lt;/code&gt;:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;nested&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;A bare type assertion panics when the input has the wrong shape. The fix:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="n"&gt;nested&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;map&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="n"&gt;any&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="no"&gt;nil&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;fmt&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Errorf&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
        &lt;span class="s"&gt;"field %s must be a nested struct (map[string]any), got %T"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;field&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Name&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
        &lt;span class="n"&gt;value&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now malformed typed data produces an error instead of crashing the process.&lt;/p&gt;

&lt;p&gt;The lesson:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Crypto security is not only about cryptographic algorithms.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Parsing, validation, and handling unexpected input are part of security too.&lt;/p&gt;

&lt;h2&gt;
  
  
  What's next
&lt;/h2&gt;

&lt;p&gt;The next milestone is smaller than I originally thought. &lt;code&gt;v0.1.0&lt;/code&gt; is the MVP tag:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;EIP-712 in the CLI&lt;/li&gt;
&lt;li&gt;tag &lt;code&gt;v0.1.0&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;call the MVP done&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;After that, transaction lifecycle work moves to a separate project, &lt;code&gt;blockchain-insight&lt;/code&gt;:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;transaction creation&lt;/li&gt;
&lt;li&gt;signing&lt;/li&gt;
&lt;li&gt;broadcasting&lt;/li&gt;
&lt;li&gt;tracking transaction status&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Separating those concerns is another thing this project taught me: a wallet does not need to become an entire blockchain application just because it can sign messages.&lt;/p&gt;

&lt;h2&gt;
  
  
  Final thoughts
&lt;/h2&gt;

&lt;p&gt;The biggest thing I learned is that cryptography is not just mathematics — it is also engineering. The algorithms are one part of the problem. The other part is everything around them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;standards&lt;/li&gt;
&lt;li&gt;byte formats&lt;/li&gt;
&lt;li&gt;compatibility&lt;/li&gt;
&lt;li&gt;serialization&lt;/li&gt;
&lt;li&gt;validation&lt;/li&gt;
&lt;li&gt;reference vectors&lt;/li&gt;
&lt;li&gt;error handling&lt;/li&gt;
&lt;li&gt;API boundaries&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A &lt;code&gt;27&lt;/code&gt; instead of a &lt;code&gt;0&lt;/code&gt;. A hard-coded KDF parameter. A type assertion without &lt;code&gt;ok&lt;/code&gt;. Each one looks like a tiny detail, and each one is the difference between "works on my machine" and an implementation that actually interoperates with the Ethereum ecosystem.&lt;/p&gt;

&lt;p&gt;Building this wallet changed how I look at Ethereum. I no longer see a wallet as a key container; I see it as a system where every small decision matters. That is probably the most useful thing I got from building it myself.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Code: &lt;a href="https://github.com/mohsenm4/mini-wallet" rel="noopener noreferrer"&gt;github.com/mohsenm4/mini-wallet&lt;/a&gt;. Built for learning — not for production key management.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>go</category>
      <category>ethereum</category>
      <category>cryptography</category>
      <category>blockchain</category>
    </item>
  </channel>
</rss>
