-
Notifications
You must be signed in to change notification settings - Fork 6
/
utils.go
79 lines (69 loc) · 1.96 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
package goether
import (
"crypto/rand"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/ethereum/go-ethereum/crypto/ecies"
"github.com/ethereum/go-ethereum/signer/core/apitypes"
)
func EthToBN(amount float64) (bn *big.Int) {
bf := new(big.Float).Mul(big.NewFloat(amount), big.NewFloat(1000000000000000000))
bn, _ = bf.Int(bn)
return bn
}
func GweiToBN(amount float64) (bn *big.Int) {
bf := new(big.Float).Mul(big.NewFloat(amount), big.NewFloat(1000000000))
bn, _ = bf.Int(bn)
return bn
}
func EIP712Hash(typedData apitypes.TypedData) (hash []byte, err error) {
domainSeparator, err := typedData.HashStruct("EIP712Domain", typedData.Domain.Map())
if err != nil {
return
}
typedDataHash, err := typedData.HashStruct(typedData.PrimaryType, typedData.Message)
if err != nil {
return
}
rawData := []byte(fmt.Sprintf("\x19\x01%s%s", string(domainSeparator), string(typedDataHash)))
hash = crypto.Keccak256(rawData)
return
}
func Ecrecover(hash, signature []byte) (publicBy []byte, address common.Address, err error) {
sig := make([]byte, len(signature))
copy(sig, signature)
if len(sig) != 65 {
err = fmt.Errorf("invalid length of signture: %d", len(sig))
return
}
if sig[64] != 27 && sig[64] != 28 && sig[64] != 1 && sig[64] != 0 {
err = fmt.Errorf("invalid signature type")
return
}
if sig[64] >= 27 {
sig[64] -= 27
}
publicBy, err = crypto.Ecrecover(hash, sig)
if err != nil {
err = fmt.Errorf("can not ecrecover: %v", err)
return
}
address = common.BytesToAddress(crypto.Keccak256(publicBy[1:])[12:])
return
}
// Encrypt encrypt
func Encrypt(publicKey string, message []byte) ([]byte, error) {
pub := common.FromHex(publicKey)
pubKey, err := crypto.UnmarshalPubkey(pub)
if err != nil {
return nil, err
}
eciesPub := ecies.ImportECDSAPublic(pubKey)
result, err := ecies.Encrypt(rand.Reader, eciesPub, message, nil, nil)
if err != nil {
return nil, err
}
return result, nil
}