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.

88 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. // Implements Signature
  20. type SignatureEd25519 [64]byte
  21. func (sig SignatureEd25519) Bytes() []byte {
  22. bz, err := cdc.MarshalBinaryBare(sig)
  23. if err != nil {
  24. panic(err)
  25. }
  26. return bz
  27. }
  28. func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
  29. func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  30. func (sig SignatureEd25519) Equals(other Signature) bool {
  31. if otherEd, ok := other.(SignatureEd25519); ok {
  32. return subtle.ConstantTimeCompare(sig[:], otherEd[:]) == 1
  33. } else {
  34. return false
  35. }
  36. }
  37. func SignatureEd25519FromBytes(data []byte) Signature {
  38. var sig SignatureEd25519
  39. copy(sig[:], data)
  40. return sig
  41. }
  42. //-------------------------------------
  43. var _ Signature = SignatureSecp256k1{}
  44. // Implements Signature
  45. type SignatureSecp256k1 []byte
  46. func (sig SignatureSecp256k1) Bytes() []byte {
  47. bz, err := cdc.MarshalBinaryBare(sig)
  48. if err != nil {
  49. panic(err)
  50. }
  51. return bz
  52. }
  53. func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
  54. func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  55. func (sig SignatureSecp256k1) Equals(other Signature) bool {
  56. if otherSecp, ok := other.(SignatureSecp256k1); ok {
  57. return subtle.ConstantTimeCompare(sig[:], otherSecp[:]) == 1
  58. } else {
  59. return false
  60. }
  61. }
  62. func SignatureSecp256k1FromBytes(data []byte) Signature {
  63. sig := make(SignatureSecp256k1, len(data))
  64. copy(sig[:], data)
  65. return sig
  66. }