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