/
secp256k1.go
53 lines (44 loc) · 1.15 KB
/
secp256k1.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
package signature
import (
"errors"
"github.com/btcsuite/btcd/btcec"
"github.com/TesraSupernet/tesracrypto/ec"
"golang.org/x/crypto/sha3"
)
func hashKeccak256(data []byte) []byte {
hasher := sha3.NewLegacyKeccak256()
hasher.Write(data)
return hasher.Sum(nil)
}
func Secp256k1Sign(pri *ec.PrivateKey, msg []byte) ([]byte, error) {
digest := hashKeccak256(msg)
return btcec.SignCompact(btcec.S256(), (*btcec.PrivateKey)(pri.PrivateKey), digest, false)
}
func Secp256k1Verify(pub *ec.PublicKey, msg []byte, sig []byte) bool {
digest := hashKeccak256(msg)
recKey, _, err := btcec.RecoverCompact(btcec.S256(), sig, digest)
if err != nil {
return false
}
return recKey.IsEqual((*btcec.PublicKey)(pub.PublicKey))
}
func ConvertToEthCompatible(sig []byte) ([]byte, error) {
s, err := Deserialize(sig)
if err != nil {
return nil, err
}
if s.Scheme != SHA3_256withECDSA {
return nil, errors.New("invalid signature scheme")
}
t, ok := s.Value.([]byte)
if !ok {
return nil, errors.New("invalid signature type")
}
if len(t) != 65 {
return nil, errors.New("invalid signature length")
}
v := t[0] - 27
copy(t, t[1:])
t[64] = v
return t, nil
}