/
signer.go
43 lines (34 loc) · 878 Bytes
/
signer.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
package valkeystore
import (
"crypto/ecdsa"
"github.com/ethereum/go-ethereum/crypto"
"github.com/skyhighblockchain/skyhigh/inter/validatorpk"
"github.com/skyhighblockchain/skyhigh/valkeystore/encryption"
)
type SignerI interface {
Sign(pubkey validatorpk.PubKey, digest []byte) ([]byte, error)
}
type Signer struct {
backend KeystoreI
}
func NewSigner(backend KeystoreI) *Signer {
return &Signer{
backend: backend,
}
}
func (s *Signer) Sign(pubkey validatorpk.PubKey, digest []byte) ([]byte, error) {
if pubkey.Type != validatorpk.Types.Secp256k1 {
return nil, encryption.ErrNotSupportedType
}
key, err := s.backend.GetUnlocked(pubkey)
if err != nil {
return nil, err
}
secp256k1Key := key.Decoded.(*ecdsa.PrivateKey)
sigRSV, err := crypto.Sign(digest, secp256k1Key)
if err != nil {
return nil, err
}
sigRS := sigRSV[:64]
return sigRS, err
}