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.

137 lines
3.3 KiB

9 years ago
9 years ago
8 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
9 years ago
  1. package crypto
  2. import (
  3. "bytes"
  4. "fmt"
  5. . "github.com/tendermint/tmlibs/common"
  6. data "github.com/tendermint/go-wire/data"
  7. "github.com/tendermint/go-wire"
  8. )
  9. func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
  10. err = wire.ReadBinaryBytes(sigBytes, &sig)
  11. return
  12. }
  13. //----------------------------------------
  14. type Signature struct {
  15. SignatureInner `json:"unwrap"`
  16. }
  17. // DO NOT USE THIS INTERFACE.
  18. // You probably want to use Signature.
  19. type SignatureInner interface {
  20. AssertIsSignatureInner()
  21. Bytes() []byte
  22. IsZero() bool
  23. String() string
  24. Equals(Signature) bool
  25. Wrap() Signature
  26. }
  27. func (sig Signature) MarshalJSON() ([]byte, error) {
  28. return sigMapper.ToJSON(sig.SignatureInner)
  29. }
  30. func (sig *Signature) UnmarshalJSON(data []byte) (err error) {
  31. parsed, err := sigMapper.FromJSON(data)
  32. if err == nil && parsed != nil {
  33. sig.SignatureInner = parsed.(SignatureInner)
  34. }
  35. return
  36. }
  37. // Unwrap recovers the concrete interface safely (regardless of levels of embeds)
  38. func (sig Signature) Unwrap() SignatureInner {
  39. pk := sig.SignatureInner
  40. for wrap, ok := pk.(Signature); ok; wrap, ok = pk.(Signature) {
  41. pk = wrap.SignatureInner
  42. }
  43. return pk
  44. }
  45. func (sig Signature) Empty() bool {
  46. return sig.SignatureInner == nil
  47. }
  48. var sigMapper = data.NewMapper(Signature{}).
  49. RegisterImplementation(SignatureEd25519{}, NameEd25519, TypeEd25519).
  50. RegisterImplementation(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1)
  51. //-------------------------------------
  52. var _ SignatureInner = SignatureEd25519{}
  53. // Implements Signature
  54. type SignatureEd25519 [64]byte
  55. func (sig SignatureEd25519) AssertIsSignatureInner() {}
  56. func (sig SignatureEd25519) Bytes() []byte {
  57. return wire.BinaryBytes(Signature{sig})
  58. }
  59. func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
  60. func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  61. func (sig SignatureEd25519) Equals(other Signature) bool {
  62. if otherEd, ok := other.Unwrap().(SignatureEd25519); ok {
  63. return bytes.Equal(sig[:], otherEd[:])
  64. } else {
  65. return false
  66. }
  67. }
  68. func (sig SignatureEd25519) MarshalJSON() ([]byte, error) {
  69. return data.Encoder.Marshal(sig[:])
  70. }
  71. func (sig *SignatureEd25519) UnmarshalJSON(enc []byte) error {
  72. var ref []byte
  73. err := data.Encoder.Unmarshal(&ref, enc)
  74. copy(sig[:], ref)
  75. return err
  76. }
  77. func (sig SignatureEd25519) Wrap() Signature {
  78. return Signature{sig}
  79. }
  80. //-------------------------------------
  81. var _ SignatureInner = SignatureSecp256k1{}
  82. // Implements Signature
  83. type SignatureSecp256k1 []byte
  84. func (sig SignatureSecp256k1) AssertIsSignatureInner() {}
  85. func (sig SignatureSecp256k1) Bytes() []byte {
  86. return wire.BinaryBytes(Signature{sig})
  87. }
  88. func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
  89. func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  90. func (sig SignatureSecp256k1) Equals(other Signature) bool {
  91. if otherEd, ok := other.Unwrap().(SignatureSecp256k1); ok {
  92. return bytes.Equal(sig[:], otherEd[:])
  93. } else {
  94. return false
  95. }
  96. }
  97. func (sig SignatureSecp256k1) MarshalJSON() ([]byte, error) {
  98. return data.Encoder.Marshal(sig)
  99. }
  100. func (sig *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
  101. return data.Encoder.Unmarshal((*[]byte)(sig), enc)
  102. }
  103. func (sig SignatureSecp256k1) Wrap() Signature {
  104. return Signature{sig}
  105. }