Note: This article clarifies the exact steps to derive an Ethereum address from a public key, addressing common misconceptions and providing a verified implementation.
Understanding Ethereum Address Generation
Many resources explain how ECDSA (Elliptic Curve Digital Signature Algorithm) generates Ethereum key pairs, but few detail the precise hashing process involved in converting a public key to an address. This guide demystifies the workflow and corrects prevalent inaccuracies.
Why Accuracy Matters
A popular Zhihu article claimed to demonstrate the process but yielded incorrect results—highlighting the need for precision. Here’s the verified method.
Step-by-Step Address Generation
1. Public Key Preparation
- Format: Ethereum public keys start with
04(uncompressed format). - Action: Remove the
04prefix to isolate the X and Y coordinates.
Example:0450863ad64a87ae8a2fe83c1af1a8403cb53f53e486d8511dad8a04887e5b23522cd470243453a299fa9e77237716103abc11a1df38855ed6f2ee187e9c582ba6→5086...82ba6.
2. Keccak-256 Hashing
- Convert to Bytes: Transform the hex string into a byte array.
👉 Why byte conversion matters. - Hash: Apply Keccak-256 to the byte array to produce a 32-byte digest.
Misstep Alert: Hashing the hex string directly (without byte conversion) leads to errors.
3. Extract the Address
- Final 40 Digits: Take the last 40 characters of the hash (20 bytes).
Example:...3E9003153d9A39D3f57B126b0c38513D5e289c3E. - Checksum (Optional): Use
ethers.utils.getAddress()to convert to a mixed-case EIP-55 checksum address.
Verified Implementation (Node.js)
const { ethers, utils } = require("ethers");
const sha3 = require("js-sha3");
// Sample Private Key
const private_key = "18e14a7b6a307f426a94f8114701e7c8e774e7f9a47e2c2035db29a206321725";
const wallet = new ethers.Wallet(private_key);
// Step 1: Remove '04' prefix
let publicKey = wallet.publicKey.substring(4);
// Step 2: Convert to byte array
let bytes = utils.arrayify("0x" + publicKey);
// Step 3: Keccak-256 hash
let hash = sha3.keccak_256(bytes);
// Step 4: Last 40 chars = address
let address = "0x" + hash.substring(24);
address = utils.getAddress(address); // Checksum
console.log(address === wallet.address); // trueKey Takeaways
- Prefix Removal: Ignoring the
04prefix before hashing is critical. - Byte Conversion: Hashing requires a byte array, not the raw hex string.
- Testing: Always verify against trusted tools like
ethers.js.
FAQ
Q1: Why does the address start with ‘0x’?
A: 0x denotes hexadecimal format, standard for Ethereum addresses.
Q2: What’s the purpose of the checksum?
A: EIP-55 checksums prevent typos by mixing letter cases based on hash bits.
Q3: Can I use compressed public keys?
A: Yes, but uncompressed keys (04-prefixed) are more common in Ethereum.
👉 Explore Ethereum key formats.
Conclusion
Generating an Ethereum address correctly hinges on precise hashing steps. This guide eliminates ambiguities—ensuring your implementations match blockchain standards. For further reading, consult Ethereum’s official docs.