Understanding Ethereum Addresses: Types and Code Examples

ยท

An Ethereum address is a fundamental identifier within the Ethereum blockchain ecosystem. It serves as a unique destination for transactions, representing either an externally owned user account or a smart contract account. This article explores the nature of Ethereum addresses, their different types, and practical implementation through Solidity code examples.

What Is an Ethereum Address?

An Ethereum address is a hexadecimal identifier derived from the last 20 bytes of the Keccak-256 hash of a public key, generated using Elliptic Curve Digital Signature Algorithm (ECDSA) cryptography. It functions as both a source and destination for transactions on the blockchain, enabling the sending and receiving of assets or interactions with smart contracts.

Types of Ethereum Accounts

The Ethereum network supports two primary types of accounts, each serving distinct purposes.

Externally Owned Accounts (EOA)

Externally Owned Accounts (EOA) are user-controlled accounts managed through private keys, often generated from seed phrases or mnemonic recovery phrases. These accounts lack associated code and are typically accessed and managed through cryptocurrency wallets. Users utilize EOAs to initiate transactions, store assets, and interact with decentralized applications.

Contract Accounts

Contract Accounts are smart contract instances deployed on the blockchain. Unlike EOAs, they contain executable code that triggers automatically when receiving transactions from other accounts. These accounts facilitate decentralized logic, enabling automated agreements, token operations, and complex blockchain-based functionalities.

Working with Addresses in Solidity

Solidity, Ethereum's primary smart contract language, provides built-in functionalities to handle addresses. Below are practical examples demonstrating common address-related operations.

Retrieving Contract Address

Every deployed smart contract possesses its own unique address. You can access this address within the contract using the address(this) property.

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract AddressExample {
    function getContractAddress() external view returns(address) {
        return address(this);
    }
}
Important: Directly returning this without the address() conversion will result in a compilation error, as the contract instance itself is not implicitly convertible to an address type.

Accessing Message Sender

The msg.sender global variable returns the address of the entity that initiated the current function call. This could be an EOA or another contract.

function getSenderAddress() external view returns(address) {
    return msg.sender;
}

Many developers initially assume msg.sender permanently refers to the original contract deployer. However, it dynamically represents whichever address triggers the function. To preserve the original deployer's address, you must store it during contract construction:

address ownerAddress;

constructor() {
    ownerAddress = msg.sender;
}

This pattern enables access control mechanisms by comparing subsequent msg.sender values against the stored ownerAddress.

Checking Account Balances

You can retrieve the native currency balance (ETH) of any address using the .balance property.

function getBalance() external view returns(uint, uint, uint) {
    uint contractBalance = address(this).balance;
    uint senderBalance = msg.sender.balance;
    uint ownerBalance = ownerAddress.balance;
    return (contractBalance, senderBalance, ownerBalance);
}

This function returns three values: the balance of the contract itself, the balance of the function caller, and the balance of the contract's owner.

๐Ÿ‘‰ Explore more strategies for smart contract development

Frequently Asked Questions

What is the difference between an EOA and a contract account?
EOAs are controlled by private keys and held by users, while contract accounts contain code that executes automatically upon receiving transactions. EOAs initiate transactions, whereas contract accounts react to them.

Can a contract account become a message sender?
Yes, when a smart contract calls another contract function, its address becomes the msg.sender for that call. This enables complex interactions between multiple contracts on the blockchain.

How are Ethereum addresses generated?
Ethereum addresses are generated from public keys, which are derived from private keys. The address represents the last 20 bytes of the Keccak-256 hash of the public key, prefixed with "0x".

Are Ethereum addresses case-sensitive?
Ethereum addresses are not case-sensitive in practice, as the ecosystem generally uses checksum-enabled addresses that validate themselves through capitalization patterns, improving security against typographical errors.

Can I identify an account type from its address?
No, Ethereum addresses themselves don't encode whether they represent EOAs or contract accounts. You can determine account type by checking whether code exists at that address using blockchain explorers or specific contract calls.

What happens if I send assets to an invalid address?
Transactions sent to improperly formatted or non-existent addresses may result in permanent loss of assets. Always verify address validity before initiating transactions.