You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

51 lines
1.2 KiB

10 years ago
  1. package account
  2. import (
  3. "github.com/tendermint/ed25519"
  4. "github.com/tendermint/tendermint2/binary"
  5. . "github.com/tendermint/tendermint2/common"
  6. )
  7. // PrivKey is part of PrivAccount and state.PrivValidator.
  8. type PrivKey interface {
  9. TypeByte() byte
  10. Sign(msg []byte) Signature
  11. PubKey() PubKey
  12. }
  13. // Types of PrivKey implementations
  14. const (
  15. PrivKeyTypeEd25519 = byte(0x01)
  16. )
  17. // for binary.readReflect
  18. var _ = binary.RegisterInterface(
  19. struct{ PrivKey }{},
  20. binary.ConcreteType{PrivKeyEd25519{}},
  21. )
  22. //-------------------------------------
  23. // Implements PrivKey
  24. type PrivKeyEd25519 []byte
  25. func (privKey PrivKeyEd25519) TypeByte() byte { return PrivKeyTypeEd25519 }
  26. func (privKey PrivKeyEd25519) Sign(msg []byte) Signature {
  27. pubKey := privKey.PubKey().(PubKeyEd25519)
  28. privKeyBytes := new([64]byte)
  29. copy(privKeyBytes[:32], privKey[:])
  30. copy(privKeyBytes[32:], pubKey[:])
  31. signatureBytes := ed25519.Sign(privKeyBytes, msg)
  32. return SignatureEd25519(signatureBytes[:])
  33. }
  34. func (key PrivKeyEd25519) PubKey() PubKey {
  35. keyBytes := new([64]byte)
  36. copy(keyBytes[:], key[:])
  37. return PubKeyEd25519(ed25519.MakePublicKey(keyBytes)[:])
  38. }
  39. func (key PrivKeyEd25519) String() string {
  40. return Fmt("PrivKeyEd25519{*****}")
  41. }