2014-12-09 18:49:04 -08:00
|
|
|
package account
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
|
|
|
|
"github.com/tendermint/go-ed25519"
|
2015-01-14 20:34:53 -08:00
|
|
|
"github.com/tendermint/tendermint/binary"
|
2015-01-16 00:31:34 -08:00
|
|
|
. "github.com/tendermint/tendermint/common"
|
2014-12-09 18:49:04 -08:00
|
|
|
)
|
|
|
|
|
2014-12-22 17:38:16 -08:00
|
|
|
// PrivKey is part of PrivAccount and state.PrivValidator.
|
2014-12-09 18:49:04 -08:00
|
|
|
type PrivKey interface {
|
|
|
|
Sign(msg []byte) Signature
|
2015-01-13 21:03:01 -08:00
|
|
|
PubKey() PubKey
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
|
|
|
|
2014-12-22 17:38:16 -08:00
|
|
|
// Types of PrivKey implementations
|
2014-12-09 18:49:04 -08:00
|
|
|
const (
|
|
|
|
PrivKeyTypeEd25519 = byte(0x01)
|
|
|
|
)
|
|
|
|
|
2015-01-04 17:33:18 -08:00
|
|
|
// for binary.readReflect
|
2015-01-14 20:34:53 -08:00
|
|
|
var _ = binary.RegisterInterface(
|
2015-01-04 17:33:18 -08:00
|
|
|
struct{ PrivKey }{},
|
2015-01-14 20:34:53 -08:00
|
|
|
binary.ConcreteType{PrivKeyEd25519{}},
|
2015-01-04 17:33:18 -08:00
|
|
|
)
|
2014-12-09 18:49:04 -08:00
|
|
|
|
|
|
|
//-------------------------------------
|
|
|
|
|
2014-12-22 17:38:16 -08:00
|
|
|
// Implements PrivKey
|
2015-01-13 21:03:01 -08:00
|
|
|
type PrivKeyEd25519 []byte
|
2014-12-09 18:49:04 -08:00
|
|
|
|
|
|
|
func (key PrivKeyEd25519) TypeByte() byte { return PrivKeyTypeEd25519 }
|
|
|
|
|
|
|
|
func (key PrivKeyEd25519) ValidateBasic() error {
|
2015-01-13 21:03:01 -08:00
|
|
|
if len(key) != ed25519.PrivateKeySize {
|
2014-12-09 18:49:04 -08:00
|
|
|
return errors.New("Invalid PrivKeyEd25519 privkey size")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (key PrivKeyEd25519) Sign(msg []byte) Signature {
|
2015-01-13 21:03:01 -08:00
|
|
|
signature := ed25519.SignMessage(msg, key, ed25519.MakePubKey(key))
|
2015-01-03 20:24:02 -08:00
|
|
|
return SignatureEd25519(signature)
|
2014-12-09 18:49:04 -08:00
|
|
|
}
|
2015-01-13 21:03:01 -08:00
|
|
|
|
|
|
|
func (key PrivKeyEd25519) PubKey() PubKey {
|
|
|
|
return PubKeyEd25519(ed25519.MakePubKey(key))
|
|
|
|
}
|
2015-01-16 00:31:34 -08:00
|
|
|
|
|
|
|
func (key PrivKeyEd25519) String() string {
|
|
|
|
return Fmt("PrivKeyEd25519{*****}")
|
|
|
|
}
|