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.

73 lines
1.6 KiB

  1. package account
  2. import (
  3. "errors"
  4. "github.com/tendermint/go-ed25519"
  5. . "github.com/tendermint/tendermint/binary"
  6. )
  7. // PubKey is part of Account and Validator.
  8. type PubKey interface {
  9. Address() []byte
  10. VerifyBytes(msg []byte, sig Signature) bool
  11. TypeByte() byte
  12. }
  13. // Types of PubKey implementations
  14. const (
  15. PubKeyTypeNil = byte(0x00)
  16. PubKeyTypeEd25519 = byte(0x01)
  17. )
  18. // for binary.readReflect
  19. var _ = RegisterInterface(
  20. struct{ PubKey }{},
  21. ConcreteType{PubKeyNil{}},
  22. ConcreteType{PubKeyEd25519{}},
  23. )
  24. //-------------------------------------
  25. // Implements PubKey
  26. type PubKeyNil struct{}
  27. func (key PubKeyNil) TypeByte() byte { return PubKeyTypeNil }
  28. func (key PubKeyNil) Address() []byte {
  29. panic("PubKeyNil has no address")
  30. }
  31. func (key PubKeyNil) VerifyBytes(msg []byte, sig_ Signature) bool {
  32. panic("PubKeyNil cannot verify messages")
  33. }
  34. //-------------------------------------
  35. // Implements PubKey
  36. type PubKeyEd25519 []byte
  37. func (key PubKeyEd25519) TypeByte() byte { return PubKeyTypeEd25519 }
  38. // TODO: Or should this just be BinaryRipemd160(key)? (The difference is the TypeByte.)
  39. func (key PubKeyEd25519) Address() []byte { return BinaryRipemd160([]byte(key)) }
  40. func (key PubKeyEd25519) ValidateBasic() error {
  41. if len(key) != ed25519.PublicKeySize {
  42. return errors.New("Invalid PubKeyEd25519 key size")
  43. }
  44. return nil
  45. }
  46. func (key PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
  47. sig, ok := sig_.(SignatureEd25519)
  48. if !ok {
  49. panic("PubKeyEd25519 expects an SignatureEd25519 signature")
  50. }
  51. v1 := &ed25519.Verify{
  52. Message: msg,
  53. PubKey: key,
  54. Signature: sig,
  55. }
  56. return ed25519.VerifyBatch([]*ed25519.Verify{v1})
  57. }