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.

89 lines
2.0 KiB

  1. package account
  2. import (
  3. "errors"
  4. "reflect"
  5. "github.com/tendermint/go-ed25519"
  6. . "github.com/tendermint/tendermint/binary"
  7. . "github.com/tendermint/tendermint/common"
  8. )
  9. // PubKey is part of Account and Validator.
  10. type PubKey interface {
  11. Address() []byte
  12. VerifyBytes(msg []byte, sig Signature) bool
  13. }
  14. // Types of PubKey implementations
  15. const (
  16. PubKeyTypeNil = byte(0x00)
  17. PubKeyTypeUnknown = byte(0x01) // For pay-to-pubkey-hash txs.
  18. PubKeyTypeEd25519 = byte(0x02)
  19. )
  20. //-------------------------------------
  21. // for binary.readReflect
  22. func PubKeyDecoder(r Unreader, n *int64, err *error) interface{} {
  23. switch t := PeekByte(r, n, err); t {
  24. case PubKeyTypeNil:
  25. return PubKeyNil{}
  26. case PubKeyTypeEd25519:
  27. return ReadBinary(PubKeyEd25519{}, r, n, err)
  28. default:
  29. *err = Errorf("Unknown PubKey type %X", t)
  30. return nil
  31. }
  32. }
  33. var _ = RegisterType(&TypeInfo{
  34. Type: reflect.TypeOf((*PubKey)(nil)).Elem(),
  35. Decoder: PubKeyDecoder,
  36. })
  37. //-------------------------------------
  38. // Implements PubKey
  39. type PubKeyNil struct{}
  40. func (key PubKeyNil) TypeByte() byte { return PubKeyTypeNil }
  41. func (key PubKeyNil) Address() []byte {
  42. panic("PubKeyNil has no address")
  43. }
  44. func (key PubKeyNil) VerifyBytes(msg []byte, sig_ Signature) bool {
  45. panic("PubKeyNil cannot verify messages")
  46. }
  47. //-------------------------------------
  48. // Implements PubKey
  49. type PubKeyEd25519 struct {
  50. PubKey []byte
  51. }
  52. func (key PubKeyEd25519) TypeByte() byte { return PubKeyTypeEd25519 }
  53. func (key PubKeyEd25519) Address() []byte { return BinaryRipemd160(key.PubKey) }
  54. func (key PubKeyEd25519) ValidateBasic() error {
  55. if len(key.PubKey) != ed25519.PublicKeySize {
  56. return errors.New("Invalid PubKeyEd25519 key size")
  57. }
  58. return nil
  59. }
  60. func (key PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
  61. sig, ok := sig_.(SignatureEd25519)
  62. if !ok {
  63. panic("PubKeyEd25519 expects an SignatureEd25519 signature")
  64. }
  65. v1 := &ed25519.Verify{
  66. Message: msg,
  67. PubKey: key.PubKey,
  68. Signature: []byte(sig.Bytes),
  69. }
  70. return ed25519.VerifyBatch([]*ed25519.Verify{v1})
  71. }