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.

60 lines
1.5 KiB

10 years ago
10 years ago
  1. package account
  2. import (
  3. "errors"
  4. "github.com/tendermint/tendermint/Godeps/_workspace/src/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. // TODO: Consider returning a reason for failure, or logging a runtime type mismatch.
  30. func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
  31. sig, ok := sig_.(SignatureEd25519)
  32. if !ok {
  33. return false
  34. }
  35. pubKeyBytes := new([32]byte)
  36. copy(pubKeyBytes[:], pubKey)
  37. sigBytes := new([64]byte)
  38. copy(sigBytes[:], sig)
  39. return ed25519.Verify(pubKeyBytes, msg, sigBytes)
  40. }
  41. func (pubKey PubKeyEd25519) ValidateBasic() error {
  42. if len(pubKey) != ed25519.PublicKeySize {
  43. return errors.New("Invalid PubKeyEd25519 key size")
  44. }
  45. return nil
  46. }
  47. func (pubKey PubKeyEd25519) String() string {
  48. return Fmt("PubKeyEd25519{%X}", []byte(pubKey))
  49. }