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.

63 lines
1.4 KiB

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