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.

62 lines
1.5 KiB

  1. package account
  2. import (
  3. "errors"
  4. "github.com/tendermint/ed25519"
  5. "github.com/tendermint/tendermint/binary"
  6. . "github.com/tendermint/tendermint/common"
  7. )
  8. // PubKey is part of Account and Validator.
  9. type PubKey interface {
  10. TypeByte() byte
  11. IsNil() bool
  12. Address() []byte
  13. VerifyBytes(msg []byte, sig Signature) bool
  14. }
  15. // Types of PubKey implementations
  16. const (
  17. PubKeyTypeEd25519 = byte(0x01)
  18. )
  19. // for binary.readReflect
  20. var _ = binary.RegisterInterface(
  21. struct{ PubKey }{},
  22. binary.ConcreteType{PubKeyEd25519{}},
  23. )
  24. //-------------------------------------
  25. // Implements PubKey
  26. type PubKeyEd25519 []byte
  27. func (pubKey PubKeyEd25519) TypeByte() byte { return PubKeyTypeEd25519 }
  28. func (pubKey PubKeyEd25519) IsNil() bool { return false }
  29. // TODO: Or should this just be BinaryRipemd160(key)? (The difference is the TypeByte.)
  30. func (pubKey PubKeyEd25519) Address() []byte { return binary.BinaryRipemd160(pubKey) }
  31. func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
  32. sig, ok := sig_.(SignatureEd25519)
  33. if !ok {
  34. panic("PubKeyEd25519 expects an SignatureEd25519 signature")
  35. }
  36. pubKeyBytes := new([32]byte)
  37. copy(pubKeyBytes[:], pubKey)
  38. sigBytes := new([64]byte)
  39. copy(sigBytes[:], sig)
  40. return ed25519.Verify(pubKeyBytes, msg, sigBytes)
  41. }
  42. func (pubKey PubKeyEd25519) ValidateBasic() error {
  43. if len(pubKey) != ed25519.PublicKeySize {
  44. return errors.New("Invalid PubKeyEd25519 key size")
  45. }
  46. return nil
  47. }
  48. func (pubKey PubKeyEd25519) String() string {
  49. return Fmt("PubKeyEd25519{%X}", []byte(pubKey))
  50. }