forked from livepeer/go-livepeer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
verify.go
56 lines (45 loc) · 1.41 KB
/
verify.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
package crypto
import (
"errors"
"math/big"
"github.com/ethereum/go-ethereum/accounts"
ethcommon "github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
)
var (
secp256k1N, _ = new(big.Int).SetString("fffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141", 16)
secp256k1halfN = new(big.Int).Div(secp256k1N, big.NewInt(2))
)
// Verify verifies that a ETH ECDSA signature over a given message
// is produced by a given ETH address
func VerifySig(addr ethcommon.Address, msg, sig []byte) bool {
recovered, err := ecrecover(msg, sig)
if err != nil {
return false
}
return recovered == addr
}
func ecrecover(msg, sig []byte) (ethcommon.Address, error) {
if len(sig) != 65 {
return ethcommon.Address{}, errors.New("invalid signature length")
}
s := new(big.Int).SetBytes(sig[32:64])
if s.Cmp(secp256k1halfN) > 0 {
return ethcommon.Address{}, errors.New("signature s value too high")
}
v := sig[64]
if v != byte(27) && v != byte(28) {
return ethcommon.Address{}, errors.New("signature v value must be 27 or 28")
}
// crypto.SigToPub() expects signature v value = 0/1
// Copy the signature and convert its value to 0/1
ethSig := make([]byte, 65)
copy(ethSig[:], sig[:])
ethSig[64] -= 27
ethMsg := accounts.TextHash(msg)
pubkey, err := crypto.SigToPub(ethMsg, ethSig)
if err != nil {
return ethcommon.Address{}, err
}
return crypto.PubkeyToAddress(*pubkey), nil
}