tendermint/account/privkey.go

49 lines
1.1 KiB
Go
Raw Normal View History

package account
import (
"github.com/tendermint/ed25519"
2015-04-01 17:30:16 -07:00
"github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/common"
)
// PrivKey is part of PrivAccount and state.PrivValidator.
type PrivKey interface {
Sign(msg []byte) Signature
2015-01-13 21:03:01 -08:00
PubKey() PubKey
}
// Types of PrivKey implementations
const (
PrivKeyTypeEd25519 = byte(0x01)
)
// for binary.readReflect
var _ = binary.RegisterInterface(
struct{ PrivKey }{},
2015-04-14 15:57:16 -07:00
binary.ConcreteType{PrivKeyEd25519{}, PrivKeyTypeEd25519},
)
//-------------------------------------
// Implements PrivKey
2015-01-13 21:03:01 -08:00
type PrivKeyEd25519 []byte
func (privKey PrivKeyEd25519) Sign(msg []byte) Signature {
pubKey := privKey.PubKey().(PubKeyEd25519)
privKeyBytes := new([64]byte)
copy(privKeyBytes[:32], privKey[:])
copy(privKeyBytes[32:], pubKey[:])
signatureBytes := ed25519.Sign(privKeyBytes, msg)
return SignatureEd25519(signatureBytes[:])
}
2015-01-13 21:03:01 -08:00
func (key PrivKeyEd25519) PubKey() PubKey {
keyBytes := new([64]byte)
copy(keyBytes[:], key[:])
return PubKeyEd25519(ed25519.MakePublicKey(keyBytes)[:])
2015-01-13 21:03:01 -08:00
}
2015-01-16 00:31:34 -08:00
func (key PrivKeyEd25519) String() string {
return Fmt("PrivKeyEd25519{*****}")
}