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.

98 lines
2.4 KiB

9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
9 years ago
  1. package crypto
  2. import (
  3. "bytes"
  4. "fmt"
  5. "github.com/tendermint/go-wire"
  6. data "github.com/tendermint/go-wire/data"
  7. . "github.com/tendermint/tmlibs/common"
  8. )
  9. func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
  10. err = wire.ReadBinaryBytes(sigBytes, &sig)
  11. return
  12. }
  13. //----------------------------------------
  14. // DO NOT USE THIS INTERFACE.
  15. // You probably want to use Signature.
  16. // +gen wrapper:"Signature,Impl[SignatureEd25519,SignatureSecp256k1],ed25519,secp256k1"
  17. type SignatureInner interface {
  18. AssertIsSignatureInner()
  19. Bytes() []byte
  20. IsZero() bool
  21. String() string
  22. Equals(Signature) bool
  23. Wrap() Signature
  24. }
  25. //-------------------------------------
  26. var _ SignatureInner = SignatureEd25519{}
  27. // Implements Signature
  28. type SignatureEd25519 [64]byte
  29. func (sig SignatureEd25519) AssertIsSignatureInner() {}
  30. func (sig SignatureEd25519) Bytes() []byte {
  31. return wire.BinaryBytes(Signature{sig})
  32. }
  33. func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
  34. func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  35. func (sig SignatureEd25519) Equals(other Signature) bool {
  36. if otherEd, ok := other.Unwrap().(SignatureEd25519); ok {
  37. return bytes.Equal(sig[:], otherEd[:])
  38. } else {
  39. return false
  40. }
  41. }
  42. func (sig SignatureEd25519) MarshalJSON() ([]byte, error) {
  43. return data.Encoder.Marshal(sig[:])
  44. }
  45. func (sig *SignatureEd25519) UnmarshalJSON(enc []byte) error {
  46. var ref []byte
  47. err := data.Encoder.Unmarshal(&ref, enc)
  48. copy(sig[:], ref)
  49. return err
  50. }
  51. //-------------------------------------
  52. var _ SignatureInner = SignatureSecp256k1{}
  53. // Implements Signature
  54. type SignatureSecp256k1 []byte
  55. func (sig SignatureSecp256k1) AssertIsSignatureInner() {}
  56. func (sig SignatureSecp256k1) Bytes() []byte {
  57. return wire.BinaryBytes(Signature{sig})
  58. }
  59. func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
  60. func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  61. func (sig SignatureSecp256k1) Equals(other Signature) bool {
  62. if otherEd, ok := other.Unwrap().(SignatureSecp256k1); ok {
  63. return bytes.Equal(sig[:], otherEd[:])
  64. } else {
  65. return false
  66. }
  67. }
  68. func (sig SignatureSecp256k1) MarshalJSON() ([]byte, error) {
  69. return data.Encoder.Marshal(sig)
  70. }
  71. func (sig *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
  72. return data.Encoder.Unmarshal((*[]byte)(sig), enc)
  73. }