-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaddress.go
38 lines (32 loc) · 1.05 KB
/
address.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
package ethutils
import (
"encoding/hex"
"strconv"
"strings"
"github.com/ethereum/go-ethereum/common"
"golang.org/x/crypto/sha3"
)
// HexToAddress is similar to [common.HexToAddress] but converts to a checksumed address first.
// It is necessary to perform an address validation first before using HexToAddress e.g. with go-playground/validator.
func HexToAddress(hexAddress string) common.Address {
return common.HexToAddress(ChecksumAddress(hexAddress))
}
// ChecksumAddress converts a mixed case hex address string to a checksumed one.
func ChecksumAddress(address string) string {
address = strings.ToLower(address)
address = strings.Replace(address, "0x", "", 1)
sha := sha3.NewLegacyKeccak256()
sha.Write([]byte(address))
hash := sha.Sum(nil)
hashstr := hex.EncodeToString(hash)
result := []string{"0x"}
for i, v := range address {
res, _ := strconv.ParseInt(string(hashstr[i]), 16, 64)
if res > 7 {
result = append(result, strings.ToUpper(string(v)))
continue
}
result = append(result, string(v))
}
return strings.Join(result, "")
}