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.

90 lines
2.5 KiB

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