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.

117 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
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 SignatureFromBytes(sigBytes []byte) (sig Signature, err error) {
  45. err = wire.ReadBinaryBytes(sigBytes, &sig)
  46. return
  47. }
  48. //-------------------------------------
  49. // Implements Signature
  50. type SignatureEd25519 [64]byte
  51. func (sig SignatureEd25519) Bytes() []byte {
  52. return wire.BinaryBytes(struct{ Signature }{sig})
  53. }
  54. func (sig SignatureEd25519) IsZero() bool { return len(sig) == 0 }
  55. func (sig SignatureEd25519) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  56. func (sig SignatureEd25519) Equals(other Signature) bool {
  57. if otherEd, ok := other.(SignatureEd25519); ok {
  58. return bytes.Equal(sig[:], otherEd[:])
  59. } else {
  60. return false
  61. }
  62. }
  63. func (p SignatureEd25519) MarshalJSON() ([]byte, error) {
  64. return data.Encoder.Marshal(p[:])
  65. }
  66. func (p *SignatureEd25519) UnmarshalJSON(enc []byte) error {
  67. var ref []byte
  68. err := data.Encoder.Unmarshal(&ref, enc)
  69. copy(p[:], ref)
  70. return err
  71. }
  72. //-------------------------------------
  73. // Implements Signature
  74. type SignatureSecp256k1 []byte
  75. func (sig SignatureSecp256k1) Bytes() []byte {
  76. return wire.BinaryBytes(struct{ Signature }{sig})
  77. }
  78. func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
  79. func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  80. func (sig SignatureSecp256k1) Equals(other Signature) bool {
  81. if otherEd, ok := other.(SignatureSecp256k1); ok {
  82. return bytes.Equal(sig[:], otherEd[:])
  83. } else {
  84. return false
  85. }
  86. }
  87. func (p SignatureSecp256k1) MarshalJSON() ([]byte, error) {
  88. return data.Encoder.Marshal(p)
  89. }
  90. func (p *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
  91. return data.Encoder.Unmarshal((*[]byte)(p), enc)
  92. }