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.

97 lines
2.3 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. Equals(Signature) bool
  22. Wrap() Signature
  23. }
  24. //-------------------------------------
  25. var _ SignatureInner = SignatureEd25519{}
  26. // Implements Signature
  27. type SignatureEd25519 [64]byte
  28. func (sig SignatureEd25519) AssertIsSignatureInner() {}
  29. func (sig SignatureEd25519) Bytes() []byte {
  30. return wire.BinaryBytes(Signature{sig})
  31. }
  32. func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
  33. func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  34. func (sig SignatureEd25519) Equals(other Signature) bool {
  35. if otherEd, ok := other.Unwrap().(SignatureEd25519); ok {
  36. return bytes.Equal(sig[:], otherEd[:])
  37. } else {
  38. return false
  39. }
  40. }
  41. func (sig SignatureEd25519) MarshalJSON() ([]byte, error) {
  42. return data.Encoder.Marshal(sig[:])
  43. }
  44. func (sig *SignatureEd25519) UnmarshalJSON(enc []byte) error {
  45. var ref []byte
  46. err := data.Encoder.Unmarshal(&ref, enc)
  47. copy(sig[:], ref)
  48. return err
  49. }
  50. //-------------------------------------
  51. var _ SignatureInner = SignatureSecp256k1{}
  52. // Implements Signature
  53. type SignatureSecp256k1 []byte
  54. func (sig SignatureSecp256k1) AssertIsSignatureInner() {}
  55. func (sig SignatureSecp256k1) Bytes() []byte {
  56. return wire.BinaryBytes(Signature{sig})
  57. }
  58. func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
  59. func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  60. func (sig SignatureSecp256k1) Equals(other Signature) bool {
  61. if otherEd, ok := other.Unwrap().(SignatureSecp256k1); ok {
  62. return bytes.Equal(sig[:], otherEd[:])
  63. } else {
  64. return false
  65. }
  66. }
  67. func (sig SignatureSecp256k1) MarshalJSON() ([]byte, error) {
  68. return data.Encoder.Marshal(sig)
  69. }
  70. func (sig *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
  71. return data.Encoder.Unmarshal((*[]byte)(sig), enc)
  72. }