2019-11-15 10:32:40 +00:00
|
|
|
package consensus
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha256"
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/CityOfZion/neo-go/pkg/crypto/keys"
|
|
|
|
)
|
|
|
|
|
|
|
|
// privateKey is a wrapper around keys.PrivateKey
|
|
|
|
// which implements crypto.PrivateKey interface.
|
|
|
|
type privateKey struct {
|
|
|
|
*keys.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalBinary implements encoding.BinaryMarshaler interface.
|
|
|
|
func (p privateKey) MarshalBinary() (data []byte, err error) {
|
|
|
|
return p.PrivateKey.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalBinary implements encoding.BinaryUnmarshaler interface.
|
|
|
|
func (p *privateKey) UnmarshalBinary(data []byte) (err error) {
|
|
|
|
p.PrivateKey, err = keys.NewPrivateKeyFromBytes(data)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-16 15:30:16 +00:00
|
|
|
// Sign implements dbft's crypto.PrivateKey interface.
|
|
|
|
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
|
|
|
|
// which implements crypto.PublicKey interface.
|
|
|
|
type publicKey struct {
|
|
|
|
*keys.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalBinary implements encoding.BinaryMarshaler interface.
|
|
|
|
func (p publicKey) MarshalBinary() (data []byte, err error) {
|
|
|
|
return p.PublicKey.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnmarshalBinary implements encoding.BinaryUnmarshaler interface.
|
|
|
|
func (p *publicKey) UnmarshalBinary(data []byte) error {
|
|
|
|
return p.PublicKey.DecodeBytes(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify implements crypto.PublicKey interface.
|
|
|
|
func (p publicKey) Verify(msg, sig []byte) error {
|
|
|
|
hash := sha256.Sum256(msg)
|
|
|
|
if p.PublicKey.Verify(sig, hash[:]) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return errors.New("error")
|
|
|
|
}
|