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.

92 lines
2.3 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package crypto
  2. import (
  3. "bytes"
  4. "github.com/tendermint/ed25519"
  5. "github.com/tendermint/ed25519/extra25519"
  6. . "github.com/tendermint/go-common"
  7. "github.com/tendermint/go-wire"
  8. "golang.org/x/crypto/ripemd160"
  9. )
  10. // PubKey is part of Account and Validator.
  11. type PubKey interface {
  12. Address() []byte
  13. KeyString() string
  14. VerifyBytes(msg []byte, sig Signature) bool
  15. Equals(PubKey) bool
  16. }
  17. // Types of PubKey implementations
  18. const (
  19. PubKeyTypeEd25519 = byte(0x01)
  20. )
  21. // for wire.readReflect
  22. var _ = wire.RegisterInterface(
  23. struct{ PubKey }{},
  24. wire.ConcreteType{PubKeyEd25519{}, PubKeyTypeEd25519},
  25. )
  26. //-------------------------------------
  27. // Implements PubKey
  28. type PubKeyEd25519 [32]byte
  29. // TODO: Slicing the array gives us length prefixing but loses the type byte.
  30. // Revisit if we add more pubkey types.
  31. // For now, we artificially append the type byte in front to give us backwards
  32. // compatibility for when the pubkey wasn't fixed length array
  33. func (pubKey PubKeyEd25519) Address() []byte {
  34. w, n, err := new(bytes.Buffer), new(int), new(error)
  35. wire.WriteBinary(pubKey[:], w, n, err)
  36. if *err != nil {
  37. PanicCrisis(*err)
  38. }
  39. // append type byte
  40. encodedPubkey := append([]byte{1}, w.Bytes()...)
  41. hasher := ripemd160.New()
  42. hasher.Write(encodedPubkey) // does not error
  43. return hasher.Sum(nil)
  44. }
  45. // TODO: Consider returning a reason for failure, or logging a runtime type mismatch.
  46. func (pubKey PubKeyEd25519) VerifyBytes(msg []byte, sig_ Signature) bool {
  47. sig, ok := sig_.(SignatureEd25519)
  48. if !ok {
  49. return false
  50. }
  51. pubKeyBytes := [32]byte(pubKey)
  52. sigBytes := [64]byte(sig)
  53. return ed25519.Verify(&pubKeyBytes, msg, &sigBytes)
  54. }
  55. // For use with golang/crypto/nacl/box
  56. // If error, returns nil.
  57. func (pubKey PubKeyEd25519) ToCurve25519() *[32]byte {
  58. keyCurve25519, pubKeyBytes := new([32]byte), [32]byte(pubKey)
  59. ok := extra25519.PublicKeyToCurve25519(keyCurve25519, &pubKeyBytes)
  60. if !ok {
  61. return nil
  62. }
  63. return keyCurve25519
  64. }
  65. func (pubKey PubKeyEd25519) String() string {
  66. return Fmt("PubKeyEd25519{%X}", pubKey[:])
  67. }
  68. // Must return the full bytes in hex.
  69. // Used for map keying, etc.
  70. func (pubKey PubKeyEd25519) KeyString() string {
  71. return Fmt("%X", pubKey[:])
  72. }
  73. func (pubKey PubKeyEd25519) Equals(other PubKey) bool {
  74. if otherEd, ok := other.(PubKeyEd25519); ok {
  75. return bytes.Equal(pubKey[:], otherEd[:])
  76. } else {
  77. return false
  78. }
  79. }