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.

121 lines
2.9 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
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. // Types of Signature implementations
  17. const (
  18. SignatureTypeEd25519 = byte(0x01)
  19. SignatureTypeSecp256k1 = byte(0x02)
  20. SignatureNameEd25519 = "ed25519"
  21. SignatureNameSecp256k1 = "secp256k1"
  22. )
  23. var sigMapper data.Mapper
  24. // register both public key types with go-data (and thus go-wire)
  25. func init() {
  26. sigMapper = data.NewMapper(SignatureS{}).
  27. RegisterInterface(SignatureEd25519{}, SignatureNameEd25519, SignatureTypeEd25519).
  28. RegisterInterface(SignatureSecp256k1{}, SignatureNameSecp256k1, SignatureTypeSecp256k1)
  29. }
  30. // SignatureS add json serialization to Signature
  31. type SignatureS struct {
  32. Signature
  33. }
  34. func (p SignatureS) MarshalJSON() ([]byte, error) {
  35. return sigMapper.ToJSON(p.Signature)
  36. }
  37. func (p *SignatureS) UnmarshalJSON(data []byte) (err error) {
  38. parsed, err := sigMapper.FromJSON(data)
  39. if err == nil {
  40. p.Signature = parsed.(Signature)
  41. }
  42. return
  43. }
  44. func (p SignatureS) Empty() bool {
  45. return p.Signature == nil
  46. }
  47. func SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
  48. err = wire.ReadBinaryBytes(sigBytes, &sig)
  49. return
  50. }
  51. //-------------------------------------
  52. // Implements Signature
  53. type SignatureEd25519 [64]byte
  54. func (sig SignatureEd25519) Bytes() []byte {
  55. return wire.BinaryBytes(struct{ Signature }{sig})
  56. }
  57. func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
  58. func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  59. func (sig SignatureEd25519) Equals(other Signature) bool {
  60. if otherEd, ok := other.(SignatureEd25519); ok {
  61. return bytes.Equal(sig[:], otherEd[:])
  62. } else {
  63. return false
  64. }
  65. }
  66. func (p SignatureEd25519) MarshalJSON() ([]byte, error) {
  67. return data.Encoder.Marshal(p[:])
  68. }
  69. func (p *SignatureEd25519) UnmarshalJSON(enc []byte) error {
  70. var ref []byte
  71. err := data.Encoder.Unmarshal(&ref, enc)
  72. copy(p[:], ref)
  73. return err
  74. }
  75. //-------------------------------------
  76. // Implements Signature
  77. type SignatureSecp256k1 []byte
  78. func (sig SignatureSecp256k1) Bytes() []byte {
  79. return wire.BinaryBytes(struct{ Signature }{sig})
  80. }
  81. func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
  82. func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  83. func (sig SignatureSecp256k1) Equals(other Signature) bool {
  84. if otherEd, ok := other.(SignatureSecp256k1); ok {
  85. return bytes.Equal(sig[:], otherEd[:])
  86. } else {
  87. return false
  88. }
  89. }
  90. func (p SignatureSecp256k1) MarshalJSON() ([]byte, error) {
  91. return data.Encoder.Marshal(p)
  92. }
  93. func (p *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
  94. return data.Encoder.Unmarshal((*[]byte)(p), enc)
  95. }