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.

86 lines
2.0 KiB

10 years ago
  1. package account
  2. import (
  3. "errors"
  4. "github.com/tendermint/ed25519"
  5. "github.com/tendermint/tendermint2/binary"
  6. . "github.com/tendermint/tendermint2/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. PubKeyTypeNil = byte(0x00)
  18. PubKeyTypeEd25519 = byte(0x01)
  19. )
  20. // for binary.readReflect
  21. var _ = binary.RegisterInterface(
  22. struct{ PubKey }{},
  23. binary.ConcreteType{PubKeyNil{}},
  24. binary.ConcreteType{PubKeyEd25519{}},
  25. )
  26. //-------------------------------------
  27. // Implements PubKey
  28. type PubKeyNil struct{}
  29. func (key PubKeyNil) TypeByte() byte { return PubKeyTypeNil }
  30. func (key PubKeyNil) IsNil() bool { return true }
  31. func (key PubKeyNil) Address() []byte {
  32. panic("PubKeyNil has no address")
  33. }
  34. func (key PubKeyNil) VerifyBytes(msg []byte, sig_ Signature) bool {
  35. panic("PubKeyNil cannot verify messages")
  36. }
  37. func (key PubKeyEd25519) ValidateBasic() error {
  38. if len(key) != ed25519.PublicKeySize {
  39. return errors.New("Invalid PubKeyEd25519 key size")
  40. }
  41. return nil
  42. }
  43. func (key PubKeyNil) String() string {
  44. return "PubKeyNil{}"
  45. }
  46. //-------------------------------------
  47. // Implements PubKey
  48. type PubKeyEd25519 []byte
  49. func (pubKey PubKeyEd25519) TypeByte() byte { return PubKeyTypeEd25519 }
  50. func (pubKey PubKeyEd25519) IsNil() bool { return false }
  51. // TODO: Or should this just be BinaryRipemd160(key)? (The difference is the TypeByte.)
  52. func (pubKey PubKeyEd25519) Address() []byte { return binary.BinaryRipemd160(pubKey) }
  53. func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
  54. sig, ok := sig_.(SignatureEd25519)
  55. if !ok {
  56. panic("PubKeyEd25519 expects an SignatureEd25519 signature")
  57. }
  58. pubKeyBytes := new([32]byte)
  59. copy(pubKeyBytes[:], pubKey)
  60. sigBytes := new([64]byte)
  61. copy(sigBytes[:], sig)
  62. return ed25519.Verify(pubKeyBytes, msg, sigBytes)
  63. }
  64. func (pubKey PubKeyEd25519) String() string {
  65. return Fmt("PubKeyEd25519{%X}", []byte(pubKey))
  66. }