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.

59 lines
1.4 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. IsNil() bool
  11. Address() []byte
  12. VerifyBytes(msg []byte, sig Signature) bool
  13. }
  14. // Types of PubKey implementations
  15. const (
  16. PubKeyTypeEd25519 = byte(0x01)
  17. )
  18. // for binary.readReflect
  19. var _ = binary.RegisterInterface(
  20. struct{ PubKey }{},
  21. binary.ConcreteType{PubKeyEd25519{}, PubKeyTypeEd25519},
  22. )
  23. //-------------------------------------
  24. // Implements PubKey
  25. type PubKeyEd25519 []byte
  26. func (pubKey PubKeyEd25519) IsNil() bool { return false }
  27. // TODO: Or should this just be BinaryRipemd160(key)? (The difference is the TypeByte.)
  28. func (pubKey PubKeyEd25519) Address() []byte { return binary.BinaryRipemd160(pubKey) }
  29. func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
  30. sig, ok := sig_.(SignatureEd25519)
  31. if !ok {
  32. panic("PubKeyEd25519 expects an SignatureEd25519 signature")
  33. }
  34. pubKeyBytes := new([32]byte)
  35. copy(pubKeyBytes[:], pubKey)
  36. sigBytes := new([64]byte)
  37. copy(sigBytes[:], sig)
  38. return ed25519.Verify(pubKeyBytes, msg, sigBytes)
  39. }
  40. func (pubKey PubKeyEd25519) ValidateBasic() error {
  41. if len(pubKey) != ed25519.PublicKeySize {
  42. return errors.New("Invalid PubKeyEd25519 key size")
  43. }
  44. return nil
  45. }
  46. func (pubKey PubKeyEd25519) String() string {
  47. return Fmt("PubKeyEd25519{%X}", []byte(pubKey))
  48. }