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.

64 lines
1.4 KiB

  1. package account
  2. import (
  3. "errors"
  4. "io"
  5. "reflect"
  6. "github.com/tendermint/go-ed25519"
  7. . "github.com/tendermint/tendermint/binary"
  8. . "github.com/tendermint/tendermint/common"
  9. )
  10. // PrivKey is part of PrivAccount and state.PrivValidator.
  11. type PrivKey interface {
  12. Sign(msg []byte) Signature
  13. }
  14. // Types of PrivKey implementations
  15. const (
  16. PrivKeyTypeEd25519 = byte(0x01)
  17. )
  18. //-------------------------------------
  19. // For binary.readReflect
  20. func PrivKeyDecoder(r io.Reader, n *int64, err *error) interface{} {
  21. switch t := ReadByte(r, n, err); t {
  22. case PrivKeyTypeEd25519:
  23. return ReadBinary(&PrivKeyEd25519{}, r, n, err)
  24. default:
  25. *err = Errorf("Unknown PrivKey type %X", t)
  26. return nil
  27. }
  28. }
  29. var _ = RegisterType(&TypeInfo{
  30. Type: reflect.TypeOf((*PrivKey)(nil)).Elem(),
  31. Decoder: PrivKeyDecoder,
  32. })
  33. //-------------------------------------
  34. // Implements PrivKey
  35. type PrivKeyEd25519 struct {
  36. PubKey []byte
  37. PrivKey []byte
  38. }
  39. func (key PrivKeyEd25519) TypeByte() byte { return PrivKeyTypeEd25519 }
  40. func (key PrivKeyEd25519) ValidateBasic() error {
  41. if len(key.PubKey) != ed25519.PublicKeySize {
  42. return errors.New("Invalid PrivKeyEd25519 pubkey size")
  43. }
  44. if len(key.PrivKey) != ed25519.PrivateKeySize {
  45. return errors.New("Invalid PrivKeyEd25519 privkey size")
  46. }
  47. return nil
  48. }
  49. func (key PrivKeyEd25519) Sign(msg []byte) Signature {
  50. signature := ed25519.SignMessage(msg, key.PrivKey, key.PubKey)
  51. return SignatureEd25519{signature}
  52. }