6 "github.com/tendermint/ed25519"
7 "github.com/tendermint/go-wire"
8 "github.com/tendermint/go-wire/data"
11 func PrivKeyFromBytes(privKeyBytes []byte) (privKey PrivKey, err error) {
12 err = wire.ReadBinaryBytes(privKeyBytes, &privKey)
14 // add support for a ValidateKey method on PrivKeys
15 // to make sure they load correctly
16 val, ok := privKey.Unwrap().(validatable)
18 err = val.ValidateKey()
24 // validatable is an optional interface for keys that want to
26 type validatable interface {
30 //----------------------------------------
32 // DO NOT USE THIS INTERFACE.
33 // You probably want to use PrivKey
34 // +gen wrapper:"PrivKey,Impl[PrivKeyEd25519,PrivKeySecp256k1],ed25519,secp256k1"
35 type PrivKeyInner interface {
36 Sign(msg []byte) Signature
41 //-------------------------------------
43 var _ PrivKeyInner = PrivKeyEd25519{}
46 type PrivKeyEd25519 [64]byte
48 func (privKey PrivKeyEd25519) Sign(msg []byte) Signature {
49 privKeyBytes := [64]byte(privKey)
50 signatureBytes := ed25519.Sign(&privKeyBytes, msg)
51 return SignatureEd25519(*signatureBytes).Wrap()
54 func (privKey PrivKeyEd25519) PubKey() PubKey {
55 privKeyBytes := [64]byte(privKey)
56 pubBytes := *ed25519.MakePublicKey(&privKeyBytes)
57 return PubKeyEd25519(pubBytes).Wrap()
60 func (p PrivKeyEd25519) MarshalJSON() ([]byte, error) {
61 return data.Encoder.Marshal(p[:])
64 func (p *PrivKeyEd25519) UnmarshalJSON(enc []byte) error {
66 err := data.Encoder.Unmarshal(&ref, enc)
71 func (privKey PrivKeyEd25519) String() string {
72 return hex.EncodeToString(privKey[:])
75 func GenPrivKeyEd25519() PrivKeyEd25519 {
76 privKeyBytes := new([64]byte)
77 copy(privKeyBytes[:32], CRandBytes(32))
78 ed25519.MakePublicKey(privKeyBytes)
79 return PrivKeyEd25519(*privKeyBytes)