-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.go
63 lines (52 loc) · 1.33 KB
/
utils.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
package ethcli
import (
"encoding/hex"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"math/big"
"strings"
)
// GenKey 本地生成mondo链格式的账户
// 返回:公钥,地址,私钥,mondo采用压缩公钥
func GenKey() (string, string, string, error) {
privkey, err := crypto.GenerateKey()
if err != nil {
return "", "", "", err
}
buff := make([]byte, 32)
copy(buff[32-len(privkey.D.Bytes()):], privkey.D.Bytes())
return common.Bytes2Hex(crypto.CompressPubkey(&privkey.PublicKey)),
crypto.PubkeyToAddress(privkey.PublicKey).String(),
common.Bytes2Hex(buff),
nil
}
func HexToBytes(str string) []byte {
str = strings.TrimPrefix(str, "0x")
b, _ := hex.DecodeString(str)
return b
}
func BytesToHex(bz []byte) string {
return hex.EncodeToString(bz)
}
func BytesToHexWith0x(bz []byte) string {
return "0x" + hex.EncodeToString(bz)
}
func ValidAddress(address string) bool {
return common.IsHexAddress(address)
}
func ToWei(v *big.Int) *big.Int {
return new(big.Int).Mul(v, big.NewInt(1e10))
}
func ToEther(v *big.Int) *big.Int {
return new(big.Int).Div(v, big.NewInt(1e10))
}
func HashToAddress(hx common.Hash) common.Address {
a := common.Address{}
a.SetBytes(hx.Bytes())
return a
}
func HashToBigInt(hx common.Hash) *big.Int {
v := new(big.Int)
v.SetBytes(hx.Bytes())
return v
}