description: Tutorial on how to check if an address is a smart contract or an account with Go.

Address Check

This section will describe how to validate an address and determine if it’s a smart contract address.

Check if Address is Valid

We can use a simple regular expression to check if the ethereum address is valid:

  1. re := regexp.MustCompile("^0x[0-9a-fA-F]{40}$")
  2. fmt.Printf("is valid: %v\n", re.MatchString("0x323b5d4c32345ced77393b3530b1eed0f346429d")) // is valid: true
  3. fmt.Printf("is valid: %v\n", re.MatchString("0xZYXb5d4c32345ced77393b3530b1eed0f346429d")) // is valid: false

Check if Address is an Account or a Smart Contract

We can determine if an address is a smart contract if there’s bytecode stored at that address. Here’s an example where we fetch the code for a token smart contract and check the length to verify that it’s a smart contract:

  1. // 0x Protocol Token (ZRX) smart contract address
  2. address := common.HexToAddress("0xe41d2489571d322189246dafa5ebde1f4699f498")
  3. bytecode, err := client.CodeAt(context.Background(), address, nil) // nil is latest block
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. isContract := len(bytecode) > 0
  8. fmt.Printf("is contract: %v\n", isContract) // is contract: true

When there’s no bytecode at the address then we know that it’s not a smart contract and it’s a standard ethereum account:

  1. // a random user account address
  2. address := common.HexToAddress("0x8e215d06ea7ec1fdb4fc5fd21768f4b34ee92ef4")
  3. bytecode, err := client.CodeAt(context.Background(), address, nil) // nil is latest block
  4. if err != nil {
  5. log.Fatal(err)
  6. }
  7. isContract = len(bytecode) > 0
  8. fmt.Printf("is contract: %v\n", isContract) // is contract: false

Full code

address_check.go

  1. package main
  2. import (
  3. "context"
  4. "fmt"
  5. "log"
  6. "regexp"
  7. "github.com/ethereum/go-ethereum/common"
  8. "github.com/ethereum/go-ethereum/ethclient"
  9. )
  10. func main() {
  11. re := regexp.MustCompile("^0x[0-9a-fA-F]{40}$")
  12. fmt.Printf("is valid: %v\n", re.MatchString("0x323b5d4c32345ced77393b3530b1eed0f346429d")) // is valid: true
  13. fmt.Printf("is valid: %v\n", re.MatchString("0xZYXb5d4c32345ced77393b3530b1eed0f346429d")) // is valid: false
  14. client, err := ethclient.Dial("https://mainnet.infura.io")
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. // 0x Protocol Token (ZRX) smart contract address
  19. address := common.HexToAddress("0xe41d2489571d322189246dafa5ebde1f4699f498")
  20. bytecode, err := client.CodeAt(context.Background(), address, nil) // nil is latest block
  21. if err != nil {
  22. log.Fatal(err)
  23. }
  24. isContract := len(bytecode) > 0
  25. fmt.Printf("is contract: %v\n", isContract) // is contract: true
  26. // a random user account address
  27. address = common.HexToAddress("0x8e215d06ea7ec1fdb4fc5fd21768f4b34ee92ef4")
  28. bytecode, err = client.CodeAt(context.Background(), address, nil) // nil is latest block
  29. if err != nil {
  30. log.Fatal(err)
  31. }
  32. isContract = len(bytecode) > 0
  33. fmt.Printf("is contract: %v\n", isContract) // is contract: false
  34. }