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.

120 lines
2.8 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-common"
  6. data "github.com/tendermint/go-data"
  7. "github.com/tendermint/go-wire"
  8. )
  9. // Signature is a part of Txs and consensus Votes.
  10. type Signature interface {
  11. Bytes() []byte
  12. IsZero() bool
  13. String() string
  14. Equals(Signature) bool
  15. }
  16. var sigMapper data.Mapper
  17. // register both public key types with go-data (and thus go-wire)
  18. func init() {
  19. sigMapper = data.NewMapper(SignatureS{}).
  20. RegisterInterface(SignatureEd25519{}, NameEd25519, TypeEd25519).
  21. RegisterInterface(SignatureSecp256k1{}, NameSecp256k1, TypeSecp256k1)
  22. }
  23. // SignatureS add json serialization to Signature
  24. type SignatureS struct {
  25. Signature
  26. }
  27. func WrapSignature(sig Signature) SignatureS {
  28. for ssig, ok := sig.(SignatureS); ok; ssig, ok = sig.(SignatureS) {
  29. sig = ssig.Signature
  30. }
  31. return SignatureS{sig}
  32. }
  33. func (p SignatureS) MarshalJSON() ([]byte, error) {
  34. return sigMapper.ToJSON(p.Signature)
  35. }
  36. func (p *SignatureS) UnmarshalJSON(data []byte) (err error) {
  37. parsed, err := sigMapper.FromJSON(data)
  38. if err == nil && parsed != nil {
  39. p.Signature = parsed.(Signature)
  40. }
  41. return
  42. }
  43. func (p SignatureS) Empty() bool {
  44. return p.Signature == nil
  45. }
  46. func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
  47. err = wire.ReadBinaryBytes(sigBytes, &sig)
  48. return
  49. }
  50. //-------------------------------------
  51. // Implements Signature
  52. type SignatureEd25519 [64]byte
  53. func (sig SignatureEd25519) Bytes() []byte {
  54. return wire.BinaryBytes(struct{ Signature }{sig})
  55. }
  56. func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
  57. func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  58. func (sig SignatureEd25519) Equals(other Signature) bool {
  59. if otherEd, ok := other.(SignatureEd25519); ok {
  60. return bytes.Equal(sig[:], otherEd[:])
  61. } else {
  62. return false
  63. }
  64. }
  65. func (p SignatureEd25519) MarshalJSON() ([]byte, error) {
  66. return data.Encoder.Marshal(p[:])
  67. }
  68. func (p *SignatureEd25519) UnmarshalJSON(enc []byte) error {
  69. var ref []byte
  70. err := data.Encoder.Unmarshal(&ref, enc)
  71. copy(p[:], ref)
  72. return err
  73. }
  74. //-------------------------------------
  75. // Implements Signature
  76. type SignatureSecp256k1 []byte
  77. func (sig SignatureSecp256k1) Bytes() []byte {
  78. return wire.BinaryBytes(struct{ Signature }{sig})
  79. }
  80. func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
  81. func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  82. func (sig SignatureSecp256k1) Equals(other Signature) bool {
  83. if otherEd, ok := other.(SignatureSecp256k1); ok {
  84. return bytes.Equal(sig[:], otherEd[:])
  85. } else {
  86. return false
  87. }
  88. }
  89. func (p SignatureSecp256k1) MarshalJSON() ([]byte, error) {
  90. return data.Encoder.Marshal(p)
  91. }
  92. func (p *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
  93. return data.Encoder.Unmarshal((*[]byte)(p), enc)
  94. }