neoneo-go/pkg/consensus/crypto.go

51 lines
1.2 KiB
Go
Raw Normal View History

2019-11-15 10:32:40 +00:00
package consensus
import (
"crypto/sha256"
"errors"
"github.com/nspcc-dev/dbft"
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
2019-11-15 10:32:40 +00:00
)
// privateKey is a wrapper around keys.PrivateKey
// which implements the crypto.PrivateKey interface.
2019-11-15 10:32:40 +00:00
type privateKey struct {
*keys.PrivateKey
}
var _ dbft.PrivateKey = &privateKey{}
// Sign implements the 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 the crypto.PublicKey interface.
2019-11-15 10:32:40 +00:00
type publicKey struct {
*keys.PublicKey
}
var _ dbft.PublicKey = &publicKey{}
// 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
}
// 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)
}
// 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")
}