/
ecdsa.go
211 lines (167 loc) · 4.82 KB
/
ecdsa.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package signer
import (
"crypto/ecdsa"
"fmt"
"github.com/0xPolygon/polygon-edge/crypto"
"github.com/0xPolygon/polygon-edge/secrets"
"github.com/0xPolygon/polygon-edge/types"
"github.com/0xPolygon/polygon-edge/validators"
"github.com/umbracle/fastrlp"
)
// ECDSAKeyManager is a module that holds ECDSA key
// and implements methods of signing by this key
type ECDSAKeyManager struct {
key *ecdsa.PrivateKey
address types.Address
}
// NewECDSAKeyManager initializes ECDSAKeyManager by the ECDSA key loaded from SecretsManager
func NewECDSAKeyManager(manager secrets.SecretsManager) (KeyManager, error) {
key, err := getOrCreateECDSAKey(manager)
if err != nil {
return nil, err
}
return NewECDSAKeyManagerFromKey(key), nil
}
// NewECDSAKeyManagerFromKey initializes ECDSAKeyManager from the given ECDSA key
func NewECDSAKeyManagerFromKey(key *ecdsa.PrivateKey) KeyManager {
return &ECDSAKeyManager{
key: key,
address: crypto.PubKeyToAddress(&key.PublicKey),
}
}
// Type returns the validator type KeyManager supports
func (s *ECDSAKeyManager) Type() validators.ValidatorType {
return validators.ECDSAValidatorType
}
// Address returns the address of KeyManager
func (s *ECDSAKeyManager) Address() types.Address {
return s.address
}
// NewEmptyValidators returns empty validator collection ECDSAKeyManager uses
func (s *ECDSAKeyManager) NewEmptyValidators() validators.Validators {
return validators.NewECDSAValidatorSet()
}
// NewEmptyCommittedSeals returns empty CommittedSeals ECDSAKeyManager uses
func (s *ECDSAKeyManager) NewEmptyCommittedSeals() Seals {
return &SerializedSeal{}
}
// SignProposerSeal signs the given message by ECDSA key the ECDSAKeyManager holds for ProposerSeal
func (s *ECDSAKeyManager) SignProposerSeal(message []byte) ([]byte, error) {
return crypto.Sign(s.key, message)
}
// SignProposerSeal signs the given message by ECDSA key the ECDSAKeyManager holds for committed seal
func (s *ECDSAKeyManager) SignCommittedSeal(message []byte) ([]byte, error) {
return crypto.Sign(s.key, message)
}
// VerifyCommittedSeal verifies a committed seal
func (s *ECDSAKeyManager) VerifyCommittedSeal(
vals validators.Validators,
address types.Address,
signature []byte,
message []byte,
) error {
if vals.Type() != s.Type() {
return ErrInvalidValidators
}
signer, err := s.Ecrecover(signature, message)
if err != nil {
return ErrInvalidSignature
}
if address != signer {
return ErrSignerMismatch
}
if !vals.Includes(address) {
return ErrNonValidatorCommittedSeal
}
return nil
}
func (s *ECDSAKeyManager) GenerateCommittedSeals(
sealMap map[types.Address][]byte,
_ validators.Validators,
) (Seals, error) {
seals := [][]byte{}
for _, seal := range sealMap {
if len(seal) != IstanbulExtraSeal {
return nil, ErrInvalidCommittedSealLength
}
seals = append(seals, seal)
}
serializedSeal := SerializedSeal(seals)
return &serializedSeal, nil
}
func (s *ECDSAKeyManager) VerifyCommittedSeals(
rawCommittedSeal Seals,
digest []byte,
vals validators.Validators,
) (int, error) {
committedSeal, ok := rawCommittedSeal.(*SerializedSeal)
if !ok {
return 0, ErrInvalidCommittedSealType
}
if vals.Type() != s.Type() {
return 0, ErrInvalidValidators
}
return s.verifyCommittedSealsImpl(committedSeal, digest, vals)
}
func (s *ECDSAKeyManager) SignIBFTMessage(msg []byte) ([]byte, error) {
return crypto.Sign(s.key, msg)
}
func (s *ECDSAKeyManager) Ecrecover(sig, digest []byte) (types.Address, error) {
return ecrecover(sig, digest)
}
func (s *ECDSAKeyManager) verifyCommittedSealsImpl(
committedSeal *SerializedSeal,
msg []byte,
validators validators.Validators,
) (int, error) {
numSeals := committedSeal.Num()
if numSeals == 0 {
return 0, ErrEmptyCommittedSeals
}
visited := make(map[types.Address]bool)
for _, seal := range *committedSeal {
addr, err := s.Ecrecover(seal, msg)
if err != nil {
return 0, err
}
if visited[addr] {
return 0, ErrRepeatedCommittedSeal
}
if !validators.Includes(addr) {
return 0, ErrNonValidatorCommittedSeal
}
visited[addr] = true
}
return numSeals, nil
}
type SerializedSeal [][]byte
func (s *SerializedSeal) Num() int {
return len(*s)
}
func (s *SerializedSeal) MarshalRLPWith(ar *fastrlp.Arena) *fastrlp.Value {
if len(*s) == 0 {
return ar.NewNullArray()
}
committed := ar.NewArray()
for _, a := range *s {
if len(a) == 0 {
committed.Set(ar.NewNull())
} else {
committed.Set(ar.NewCopyBytes(a))
}
}
return committed
}
func (s *SerializedSeal) UnmarshalRLPFrom(p *fastrlp.Parser, v *fastrlp.Value) error {
vals, err := v.GetElems()
if err != nil {
return fmt.Errorf("mismatch of RLP type for CommittedSeal, expected list but found %s", v.Type())
}
(*s) = make([][]byte, len(vals))
for indx, val := range vals {
if (*s)[indx], err = val.GetBytes((*s)[indx]); err != nil {
return err
}
}
return nil
}