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
1.9 KiB

  1. package crypto
  2. import (
  3. "fmt"
  4. "crypto/subtle"
  5. . "github.com/tendermint/tmlibs/common"
  6. )
  7. func SignatureFromBytes(pubKeyBytes []byte) (pubKey Signature, err error) {
  8. err = cdc.UnmarshalBinaryBare(pubKeyBytes, &pubKey)
  9. return
  10. }
  11. //----------------------------------------
  12. type Signature interface {
  13. Bytes() []byte
  14. IsZero() bool
  15. Equals(Signature) bool
  16. }
  17. //-------------------------------------
  18. var _ Signature = SignatureEd25519{}
  19. const SignatureEd25519Size = 64
  20. // Implements Signature
  21. type SignatureEd25519 [SignatureEd25519Size]byte
  22. func (sig SignatureEd25519) Bytes() []byte {
  23. bz, err := cdc.MarshalBinaryBare(sig)
  24. if err != nil {
  25. panic(err)
  26. }
  27. return bz
  28. }
  29. func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
  30. func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  31. func (sig SignatureEd25519) Equals(other Signature) bool {
  32. if otherEd, ok := other.(SignatureEd25519); ok {
  33. return subtle.ConstantTimeCompare(sig[:], otherEd[:]) == 1
  34. } else {
  35. return false
  36. }
  37. }
  38. func SignatureEd25519FromBytes(data []byte) Signature {
  39. var sig SignatureEd25519
  40. copy(sig[:], data)
  41. return sig
  42. }
  43. //-------------------------------------
  44. var _ Signature = SignatureSecp256k1{}
  45. // Implements Signature
  46. type SignatureSecp256k1 []byte
  47. func (sig SignatureSecp256k1) Bytes() []byte {
  48. bz, err := cdc.MarshalBinaryBare(sig)
  49. if err != nil {
  50. panic(err)
  51. }
  52. return bz
  53. }
  54. func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
  55. func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  56. func (sig SignatureSecp256k1) Equals(other Signature) bool {
  57. if otherSecp, ok := other.(SignatureSecp256k1); ok {
  58. return subtle.ConstantTimeCompare(sig[:], otherSecp[:]) == 1
  59. } else {
  60. return false
  61. }
  62. }
  63. func SignatureSecp256k1FromBytes(data []byte) Signature {
  64. sig := make(SignatureSecp256k1, len(data))
  65. copy(sig[:], data)
  66. return sig
  67. }