Address Utilities
Utilities for validating, formatting, and working with Ethereum addresses
Utilities for validating, formatting, and working with Ethereum addresses.
isAddress
Check if a string is a valid Ethereum address:
1import "github.com/ChefBingbong/viem-go/utils/address"2
3valid := address.IsAddress("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")4fmt.Println(valid) // true5
6valid = address.IsAddress("0xinvalid")7fmt.Println(valid) // false8
9valid = address.IsAddress("not an address")10fmt.Println(valid) // falsegetAddress
Convert an address to its checksummed format (EIP-55):
1checksummed, err := address.GetAddress("0xd8da6bf26964af9d7eed9e03e53415d37aa96045")2if err != nil {3 log.Fatal(err)4}5fmt.Println(checksummed)6// 0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045isAddressEqual
Compare two addresses (case-insensitive):
1equal := address.IsAddressEqual(2 "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",3 "0xD8DA6BF26964AF9D7EED9E03E53415D37AA96045",4)5fmt.Println(equal) // truegetContractAddress
Compute the address of a contract deployed with CREATE:
1contractAddr := address.GetContractAddress(address.GetContractAddressOptions{2 From: common.HexToAddress("0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"),3 Nonce: 0,4})5fmt.Println(contractAddr.Hex())getCreate2Address
Compute the address of a contract deployed with CREATE2:
1contractAddr := address.GetCreate2Address(address.GetCreate2AddressOptions{2 From: factoryAddress,3 Salt: salt, // [32]byte4 InitCodeHash: initCodeHash, // keccak256 of init code5})Working with go-ethereum
viem-go uses go-ethereum's common.Address type:
import "github.com/ethereum/go-ethereum/common"
// Parse string to Address
addr := common.HexToAddress("0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045")
// Get hex string
hex := addr.Hex() // "0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045"
// Get bytes
bytes := addr.Bytes() // [20]byte
// Compare addresses
addr1 := common.HexToAddress("0x...")
addr2 := common.HexToAddress("0x...")
if addr1 == addr2 {
fmt.Println("Addresses match")
}
// Check for zero address
if addr == (common.Address{}) {
fmt.Println("Zero address")
}
Common Patterns
Validate User Input
func parseUserAddress(input string) (common.Address, error) {
// Validate format
if !address.IsAddress(input) {
return common.Address{}, errors.New("invalid address format")
}
// Convert to checksummed
checksummed, err := address.GetAddress(input)
if err != nil {
return common.Address{}, err
}
return common.HexToAddress(checksummed), nil
}
Predict Contract Address
// Predict where a contract will be deployed
nonce, _ := client.GetTransactionCount(ctx, deployer)
predictedAddr := address.GetContractAddress(address.GetContractAddressOptions{
From: deployer,
Nonce: nonce,
})
fmt.Printf("Contract will deploy to: %s\n", predictedAddr.Hex())
Create2 Deterministic Deployment
// Compute CREATE2 address before deployment
salt := common.HexToHash("0x1234...")
initCode := contractBytecode
initCodeHash := crypto.Keccak256Hash(initCode)
predictedAddr := address.GetCreate2Address(address.GetCreate2AddressOptions{
From: factoryAddress,
Salt: salt,
InitCodeHash: initCodeHash,
})
// Deploy will result in this exact address regardless of nonce
fmt.Printf("Deterministic address: %s\n", predictedAddr.Hex())
Zero Address
The zero address (0x0000000000000000000000000000000000000000) is commonly used to represent:
- Contract creation transactions (no
toaddress) - Burn addresses
- Uninitialized address fields
// Check for zero address
zeroAddr := common.Address{}
if addr == zeroAddr {
fmt.Println("Address is zero")
}
// Or use bytes comparison
if addr == common.HexToAddress("0x0000000000000000000000000000000000000000") {
fmt.Println("Address is zero")
}