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.

103 lines
2.5 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. func SignatureEd25519FromBytes(data []byte) Signature {
  51. var sig SignatureEd25519
  52. copy(sig[:], data)
  53. return sig.Wrap()
  54. }
  55. //-------------------------------------
  56. var _ SignatureInner = SignatureSecp256k1{}
  57. // Implements Signature
  58. type SignatureSecp256k1 []byte
  59. func (sig SignatureSecp256k1) AssertIsSignatureInner() {}
  60. func (sig SignatureSecp256k1) Bytes() []byte {
  61. return wire.BinaryBytes(Signature{sig})
  62. }
  63. func (sig SignatureSecp256k1) IsZero() bool { return len(sig) == 0 }
  64. func (sig SignatureSecp256k1) String() string { return fmt.Sprintf("/%X.../", Fingerprint(sig[:])) }
  65. func (sig SignatureSecp256k1) Equals(other Signature) bool {
  66. if otherEd, ok := other.Unwrap().(SignatureSecp256k1); ok {
  67. return bytes.Equal(sig[:], otherEd[:])
  68. } else {
  69. return false
  70. }
  71. }
  72. func (sig SignatureSecp256k1) MarshalJSON() ([]byte, error) {
  73. return data.Encoder.Marshal(sig)
  74. }
  75. func (sig *SignatureSecp256k1) UnmarshalJSON(enc []byte) error {
  76. return data.Encoder.Unmarshal((*[]byte)(sig), enc)
  77. }