/
util.go
106 lines (96 loc) · 2.99 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
package metaresolver
import (
"bytes"
"crypto/ecdsa"
"fmt"
"math/big"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/common/hexutil"
ethCrypto "github.com/ethereum/go-ethereum/crypto"
"github.com/metadium/go-delegator/log"
)
func verifySignature(reqID uint64, hash []byte, v uint8, r hexutil.Bytes, s hexutil.Bytes, address common.Address) Error {
isValid, err := isValidSignature(reqID, hash, v, r, s, address)
log.Debugfd(reqID, "isValid : %v\n", isValid)
if err != nil {
return err
}
isValidWithPrefix, err := isValidSignatureWithPrefix(reqID, hash, v, r, s, address)
log.Debugfd(reqID, "isValidWithPrefix : %v\n", isValidWithPrefix)
if err != nil {
return err
}
if isValid || isValidWithPrefix {
return nil
}
return &invalidSignatureError{"Failed to verify signature"}
}
func isValidSignatureWithPrefix(reqID uint64, hash []byte, v uint8, r hexutil.Bytes, s hexutil.Bytes, address common.Address) (bool, Error) {
phash := signHash(hash)
return isValidSignature(reqID, phash, v, r, s, address)
}
func isValidSignature(reqID uint64, hash []byte, v uint8, r hexutil.Bytes, s hexutil.Bytes, address common.Address) (bool, Error) {
var sig hexutil.Bytes
sig = append(sig, r...)
sig = append(sig, s...)
sig = append(sig, v-27)
log.Debugfd(reqID, "signature : '%v' \n", sig)
var signedAddress common.Address
pKeyBytes, err := ethCrypto.Ecrecover(hash, sig)
if err != nil {
log.Errorfd(reqID, "EcRecover Error: %v", err)
return false, &internalError{"Failed to EcRecover"}
}
pKey, err := ethCrypto.UnmarshalPubkey(pKeyBytes)
if err != nil {
log.Errorfd(reqID, "EcRecover Error: %v", err)
return false, &internalError{"Failed to EcRecover"}
}
signedAddress = ethCrypto.PubkeyToAddress(*pKey)
log.Debugfd(reqID, "signedAddress : %v\n", signedAddress.String())
if !bytes.Equal(signedAddress.Bytes(), address.Bytes()) {
log.Debugfd(reqID, "not same address %v / %v ", address.String(), signedAddress.String())
return false, nil
}
return true, nil
}
func addressArrayToBytes(addresses []common.Address) []byte {
var ret []byte
if len(addresses) == 0 {
return ret
}
for _, addr := range addresses {
addrBytes := addressToByte32(addr)
ret = append(ret, addrBytes[:]...)
}
return ret
}
func addressToByte32(address common.Address) [32]byte {
var result [32]byte
copy(result[12:], address.Bytes())
return result
}
func bigIntToByte32(_val *big.Int) [32]byte {
result := [32]byte{}
if _val != nil {
_bytes := _val.Bytes()
copy(result[32-len(_bytes):], _bytes)
}
return result
}
func signBytes(bmsg []byte, privKey *ecdsa.PrivateKey) ([]byte, error) {
bMsg := ethCrypto.Keccak256(bmsg)
return ethCrypto.Sign(signHash(bMsg), privKey)
}
func signHash(data []byte) []byte {
msg := fmt.Sprintf("\x19Ethereum Signed Message:\n%d%s", len(data), data)
return ethCrypto.Keccak256([]byte(msg))
}
func intToByte32(_val *big.Int) [32]byte {
result := [32]byte{}
if _val != nil {
_bytes := _val.Bytes()
copy(result[32-len(_bytes):], _bytes)
}
return result
}