2019-11-15 10:32:40 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
|
|
|
|
2024-03-21 20:38:24 +00:00
|
|
|
"github.com/nspcc-dev/dbft"
|
2020-03-03 14:21:42 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2019-11-15 10:32:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// privateKey is a wrapper around keys.PrivateKey
|
2022-04-20 18:30:09 +00:00
|
|
|
// which implements the crypto.PrivateKey interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
type privateKey struct {
|
|
|
|
*keys.PrivateKey
|
|
|
|
}
|
|
|
|
|
2024-03-21 20:38:24 +00:00
|
|
|
var _ dbft.PrivateKey = &privateKey{}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Sign implements the dbft's crypto.PrivateKey interface.
|
2020-01-16 15:30:16 +00:00
|
|
|
func (p *privateKey) Sign(data []byte) ([]byte, error) {
|
|
|
|
return p.PrivateKey.Sign(data), nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 10:32:40 +00:00
|
|
|
// publicKey is a wrapper around keys.PublicKey
|
2022-04-20 18:30:09 +00:00
|
|
|
// which implements the crypto.PublicKey interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
type publicKey struct {
|
|
|
|
*keys.PublicKey
|
|
|
|
}
|
|
|
|
|
2024-03-21 20:38:24 +00:00
|
|
|
var _ dbft.PublicKey = &publicKey{}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// MarshalBinary implements the encoding.BinaryMarshaler interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (p publicKey) MarshalBinary() (data []byte, err error) {
|
|
|
|
return p.PublicKey.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (p *publicKey) UnmarshalBinary(data []byte) error {
|
|
|
|
return p.PublicKey.DecodeBytes(data)
|
|
|
|
}
|
|
|
|
|
2022-04-20 18:30:09 +00:00
|
|
|
// Verify implements the crypto.PublicKey interface.
|
2019-11-15 10:32:40 +00:00
|
|
|
func (p publicKey) Verify(msg, sig []byte) error {
|
|
|
|
hash := sha256.Sum256(msg)
|
|
|
|
if p.PublicKey.Verify(sig, hash[:]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("error")
|
|
|
|
}
|