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.

76 lines
1.6 KiB

  1. package tx
  2. import (
  3. crypto "github.com/tendermint/go-crypto"
  4. data "github.com/tendermint/go-data"
  5. keys "github.com/tendermint/go-crypto/keys"
  6. )
  7. const (
  8. typeOneSig = byte(0x01)
  9. typeMultiSig = byte(0x02)
  10. nameOneSig = "sig"
  11. nameMultiSig = "multi"
  12. )
  13. var _ keys.Signable = Sig{}
  14. var TxMapper data.Mapper
  15. func init() {
  16. TxMapper = data.NewMapper(Sig{}).
  17. RegisterInterface(&OneSig{}, nameOneSig, typeOneSig).
  18. RegisterInterface(&MultiSig{}, nameMultiSig, typeMultiSig)
  19. }
  20. /*
  21. DO NOT USE this interface.
  22. It is public by necessity but should never be used directly
  23. outside of this package.
  24. Only use Sig, never SigInner
  25. */
  26. type SigInner interface {
  27. SignBytes() []byte
  28. Sign(pubkey crypto.PubKey, sig crypto.Signature) error
  29. Signers() ([]crypto.PubKey, error)
  30. }
  31. // Sig is what is exported, and handles serialization
  32. type Sig struct {
  33. SigInner
  34. }
  35. // TxBytes
  36. func (s Sig) TxBytes() ([]byte, error) {
  37. return data.ToWire(s)
  38. }
  39. // WrapSig goes from concrete implementation to "interface" struct
  40. func WrapSig(pk SigInner) Sig {
  41. if wrap, ok := pk.(Sig); ok {
  42. pk = wrap.Unwrap()
  43. }
  44. return Sig{pk}
  45. }
  46. // Unwrap recovers the concrete interface safely (regardless of levels of embeds)
  47. func (p Sig) Unwrap() SigInner {
  48. pk := p.SigInner
  49. for wrap, ok := pk.(Sig); ok; wrap, ok = pk.(Sig) {
  50. pk = wrap.SigInner
  51. }
  52. return pk
  53. }
  54. func (p Sig) MarshalJSON() ([]byte, error) {
  55. return TxMapper.ToJSON(p.Unwrap())
  56. }
  57. func (p *Sig) UnmarshalJSON(data []byte) (err error) {
  58. parsed, err := TxMapper.FromJSON(data)
  59. if err == nil && parsed != nil {
  60. p.SigInner = parsed.(SigInner)
  61. }
  62. return
  63. }