-
Notifications
You must be signed in to change notification settings - Fork 13
/
ethereum.go
75 lines (65 loc) · 1.74 KB
/
ethereum.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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package coincodec
import (
"encoding/hex"
"errors"
"fmt"
"strings"
"github.com/wealdtech/go-slip44"
"golang.org/x/crypto/sha3"
)
func init() {
chains := []uint32{
slip44.ETHER, slip44.ETHER_CLASSIC, slip44.GOCHAIN_GO,
slip44.POA, slip44.CALLISTO, slip44.TOMO,
slip44.THUNDERCORE, slip44.THETA, slip44.VECHAIN_TOKEN,
}
for _, c := range chains {
toBytesMap[c] = EtherToBytes
toStringMap[c] = EtherToString
}
}
// EtherToBytes converts the input string to a byte array.
func EtherToBytes(input string) ([]byte, error) {
input = strings.TrimPrefix(input, "0x")
if len(input) != 40 {
return nil, errors.New("Ethereum address must have 40 characters")
}
output, err := hex.DecodeString(input)
if err != nil {
return nil, errors.New("invalid hex string")
}
// Confirm checksum if present
if strings.ToLower(input) != input && strings.ToUpper(input) != input {
checksummed, err := EtherToString(output)
if err != nil {
return nil, errors.New("failed to validate checksum")
}
if checksummed[2:] != input {
return nil, errors.New("invalid checksum")
}
}
return output, nil
}
// EtherToString converts the input byte array to a string representation of the Ethereum address.
func EtherToString(input []byte) (string, error) {
if len(input) != 20 {
return "", errors.New("Ethereum address must have 20 bytes")
}
unchecksummed := hex.EncodeToString(input)
sha := sha3.NewLegacyKeccak256()
_, _ = sha.Write([]byte(unchecksummed))
hash := sha.Sum(nil)
result := []byte(unchecksummed)
for i := 0; i < len(result); i++ {
hashByte := hash[i/2]
if i%2 == 0 {
hashByte = hashByte >> 4
} else {
hashByte &= 0xf
}
if result[i] > '9' && hashByte > 7 {
result[i] -= 32
}
}
return fmt.Sprintf("0x%s", result), nil
}