-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
evm_keyring.go
77 lines (63 loc) · 2.19 KB
/
evm_keyring.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
package ocr2key
import (
"bytes"
"crypto/ecdsa"
"io"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/crypto"
"github.com/smartcontractkit/libocr/offchainreporting2plus/chains/evmutil"
ocrtypes "github.com/smartcontractkit/libocr/offchainreporting2plus/types"
)
var _ ocrtypes.OnchainKeyring = &evmKeyring{}
type evmKeyring struct {
privateKey ecdsa.PrivateKey
}
func newEVMKeyring(material io.Reader) (*evmKeyring, error) {
ecdsaKey, err := ecdsa.GenerateKey(curve, material)
if err != nil {
return nil, err
}
return &evmKeyring{privateKey: *ecdsaKey}, nil
}
// XXX: PublicKey returns the address of the public key not the public key itself
func (ok *evmKeyring) PublicKey() ocrtypes.OnchainPublicKey {
address := ok.signingAddress()
return address[:]
}
func (ok *evmKeyring) reportToSigData(reportCtx ocrtypes.ReportContext, report ocrtypes.Report) []byte {
rawReportContext := evmutil.RawReportContext(reportCtx)
sigData := crypto.Keccak256(report)
sigData = append(sigData, rawReportContext[0][:]...)
sigData = append(sigData, rawReportContext[1][:]...)
sigData = append(sigData, rawReportContext[2][:]...)
return crypto.Keccak256(sigData)
}
func (ok *evmKeyring) Sign(reportCtx ocrtypes.ReportContext, report ocrtypes.Report) ([]byte, error) {
return crypto.Sign(ok.reportToSigData(reportCtx, report), &ok.privateKey)
}
func (ok *evmKeyring) Verify(publicKey ocrtypes.OnchainPublicKey, reportCtx ocrtypes.ReportContext, report ocrtypes.Report, signature []byte) bool {
hash := ok.reportToSigData(reportCtx, report)
authorPubkey, err := crypto.SigToPub(hash, signature)
if err != nil {
return false
}
authorAddress := crypto.PubkeyToAddress(*authorPubkey)
return bytes.Equal(publicKey[:], authorAddress[:])
}
func (ok *evmKeyring) MaxSignatureLength() int {
return 65
}
func (ok *evmKeyring) signingAddress() common.Address {
return crypto.PubkeyToAddress(*(&ok.privateKey).Public().(*ecdsa.PublicKey))
}
func (ok *evmKeyring) Marshal() ([]byte, error) {
return crypto.FromECDSA(&ok.privateKey), nil
}
func (ok *evmKeyring) Unmarshal(in []byte) error {
privateKey, err := crypto.ToECDSA(in)
if err != nil {
return err
}
ok.privateKey = *privateKey
return nil
}