What is an Ethereum Address?
An Ethereum address is a unique identifier representing an externally owned account (EOA) or smart contract on the blockchain. Technically, it's a 20-byte hexadecimal value derived from the last 20 bytes of the Keccak-256 hash of a public key generated using ECDSA cryptography.
Types of Ethereum Accounts
1. Externally Owned Accounts (EOA)
- Controlled by private keys (typically generated from seed phrases)
- No associated code โ used for sending transactions and holding ETH/tokens
- Managed through wallets like MetaMask
2. Contract Accounts
- Contains executable smart contract code
- Activated when receiving transactions
- Cannot initiate transactions autonomously
Key Address Operations with Solidity Examples
1. Getting Contract Address
// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;
contract AddressDemo {
function getContractAddress() external view returns(address) {
return address(this); // Proper address conversion
}
}Important: Always useaddress()for explicit type conversion. Returningthisdirectly causes compilation errors.
2. Retrieving msg.sender
function getSender() external view returns(address) {
return msg.sender; // Returns current caller's address
}msg.sender represents the immediate caller, which could be:
- An EOA initiating a transaction
- Another contract calling the function
- Changes dynamically based on the calling context
๐ Learn more about Ethereum address security
3. Checking Account Balances
address owner;
constructor() {
owner = msg.sender; // Sets deployer as permanent owner
}
function getBalances() external view returns(uint, uint, uint) {
uint contractBalance = address(this).balance;
uint callerBalance = msg.sender.balance;
uint ownerBalance = owner.balance;
return (contractBalance, callerBalance, ownerBalance);
}Practical Considerations
- Permission Control: Store
msg.senderin constructors for permanent owner records - Balance Checks: Use
.balanceproperty to query ETH amounts - Type Safety: Always verify address types in function returns
Frequently Asked Questions
Q: Can contract addresses initiate transactions?
A: No, only EOAs can initiate transactions. Contracts execute code in response to incoming transactions.
Q: Why does msg.sender change?
A: It's context-dependent โ always reflects the immediate caller, whether it's a user or another contract.
Q: How are addresses generated for EOAs?
A: From private keys โ public keys โ Keccak-256 hash โ last 20 bytes.
Q: Are Ethereum addresses case-sensitive?
A: Yes, but checksum addresses (with mixed case) help prevent input errors.
๐ Discover advanced Ethereum development techniques
Security Best Practices
- Always validate incoming addresses in smart contracts
- Use OpenZeppelin's Address utility for safety checks
- Never store unencrypted private keys
Remember: Addresses are fundamental to all Ethereum interactions, whether you're sending ETH, calling contracts, or implementing access control systems.