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.

48 lines
1.2 KiB

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