forked from sonm-io/core
/
util.go
176 lines (152 loc) · 3.31 KB
/
util.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
package util
import (
"crypto/ecdsa"
"errors"
"fmt"
"io/ioutil"
"math/big"
"net"
"os"
"os/user"
"runtime"
"strconv"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"gopkg.in/yaml.v2"
)
// GetLocalIP find local non-loopback ip addr
func GetLocalIP() string {
addrs, err := net.InterfaceAddrs()
if err != nil {
return ""
}
for _, address := range addrs {
// check the address type and if it is not a loopback the display it
if ipnet, ok := address.(*net.IPNet); ok && !ipnet.IP.IsLoopback() {
if ipnet.IP.To4() != nil {
return ipnet.IP.String()
}
}
}
return ""
}
func GetUserHomeDir() (homeDir string, err error) {
usr, err := user.Current()
if err != nil {
return "", err
}
return usr.HomeDir, nil
}
func ParseEndpointPort(s string) (string, error) {
_, port, err := net.SplitHostPort(s)
if err != nil {
return "", err
}
intPort, err := strconv.Atoi(port)
if err != nil {
return "", err
}
if intPort < 1 || intPort > 65535 {
return "", errors.New("invalid port value")
}
return port, nil
}
func GetPlatformName() string {
return fmt.Sprintf("%s/%s/%s", runtime.GOOS, runtime.GOARCH, runtime.Version())
}
func PubKeyToString(key ecdsa.PublicKey) string {
return fmt.Sprintf("%x", crypto.FromECDSAPub(&key))
}
func PubKeyToAddr(key ecdsa.PublicKey) common.Address {
return crypto.PubkeyToAddress(key)
}
func LoadYamlFile(from string, to interface{}) error {
buf, err := ioutil.ReadFile(from)
if err != nil {
return err
}
err = yaml.Unmarshal(buf, to)
if err != nil {
return err
}
return nil
}
// DirectoryExists returns true if the given directory exists
func DirectoryExists(p string) bool {
if _, err := os.Stat(p); err != nil {
return !os.IsNotExist(err)
}
return true
}
// ParseBigInt parses the given string and converts it to *big.Int
func ParseBigInt(s string) (*big.Int, error) {
n := new(big.Int)
n, ok := n.SetString(s, 10)
if !ok {
return nil, fmt.Errorf("cannot convert %s to big.Int", s)
}
return n, nil
}
// CmpPrice transforms the given prices in string format and compares them,
// returning:
// -1 if x < y
// 0 if x == y
// +1 if x > y
func CmpPrice(x, y string) (int, error) {
xBig, err := ParseBigInt(x)
if err != nil {
return 0, err
}
yBig, err := ParseBigInt(y)
if err != nil {
return 0, err
}
return xBig.Cmp(yBig), nil
}
func VerifyEqualPrice(x, y string) error {
cmp, err := CmpPrice(x, y)
if err != nil {
return err
}
if cmp != 0 {
return fmt.Errorf("prices are not equal")
}
return nil
}
func GetAvailableIPs() (availableIPs []net.IP, err error) {
ifaces, err := net.Interfaces()
if err != nil {
return nil, err
}
for _, i := range ifaces {
addrs, err := i.Addrs()
if err != nil {
return nil, err
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
case *net.IPAddr:
ip = v.IP
}
if ip != nil && ip.IsGlobalUnicast() {
availableIPs = append(availableIPs, ip)
}
}
}
if len(availableIPs) == 0 {
return nil, errors.New("could not determine a single unicast addr, check networking")
}
return availableIPs, nil
}
// EqualAddresses checks if provided Addresses are equal
func EqualAddresses(a, b common.Address) bool {
for i := range a {
if a[i] != b[i] {
return false
}
}
return true
}