message.go 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. // Copyright 2014 The go-ethereum Authors
  2. // This file is part of the go-ethereum library.
  3. //
  4. // The go-ethereum library is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Lesser General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // The go-ethereum library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
  16. // Contains the Whisper protocol Message element. For formal details please see
  17. // the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#messages.
  18. package whisper
  19. import (
  20. "crypto/ecdsa"
  21. "math/rand"
  22. "time"
  23. "github.com/ethereum/go-ethereum/common"
  24. "github.com/ethereum/go-ethereum/crypto"
  25. "github.com/ethereum/go-ethereum/logger"
  26. "github.com/ethereum/go-ethereum/logger/glog"
  27. )
  28. // Message represents an end-user data packet to transmit through the Whisper
  29. // protocol. These are wrapped into Envelopes that need not be understood by
  30. // intermediate nodes, just forwarded.
  31. type Message struct {
  32. Flags byte // First bit is signature presence, rest reserved and should be random
  33. Signature []byte
  34. Payload []byte
  35. Sent time.Time // Time when the message was posted into the network
  36. TTL time.Duration // Maximum time to live allowed for the message
  37. To *ecdsa.PublicKey // Message recipient (identity used to decode the message)
  38. Hash common.Hash // Message envelope hash to act as a unique id
  39. }
  40. // Options specifies the exact way a message should be wrapped into an Envelope.
  41. type Options struct {
  42. From *ecdsa.PrivateKey
  43. To *ecdsa.PublicKey
  44. TTL time.Duration
  45. Topics []Topic
  46. }
  47. // NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
  48. func NewMessage(payload []byte) *Message {
  49. // Construct an initial flag set: no signature, rest random
  50. flags := byte(rand.Intn(256))
  51. flags &= ^signatureFlag
  52. // Assemble and return the message
  53. return &Message{
  54. Flags: flags,
  55. Payload: payload,
  56. Sent: time.Now(),
  57. }
  58. }
  59. // Wrap bundles the message into an Envelope to transmit over the network.
  60. //
  61. // pow (Proof Of Work) controls how much time to spend on hashing the message,
  62. // inherently controlling its priority through the network (smaller hash, bigger
  63. // priority).
  64. //
  65. // The user can control the amount of identity, privacy and encryption through
  66. // the options parameter as follows:
  67. // - options.From == nil && options.To == nil: anonymous broadcast
  68. // - options.From != nil && options.To == nil: signed broadcast (known sender)
  69. // - options.From == nil && options.To != nil: encrypted anonymous message
  70. // - options.From != nil && options.To != nil: encrypted signed message
  71. func (self *Message) Wrap(pow time.Duration, options Options) (*Envelope, error) {
  72. // Use the default TTL if non was specified
  73. if options.TTL == 0 {
  74. options.TTL = DefaultTTL
  75. }
  76. self.TTL = options.TTL
  77. // Sign and encrypt the message if requested
  78. if options.From != nil {
  79. if err := self.sign(options.From); err != nil {
  80. return nil, err
  81. }
  82. }
  83. if options.To != nil {
  84. if err := self.encrypt(options.To); err != nil {
  85. return nil, err
  86. }
  87. }
  88. // Wrap the processed message, seal it and return
  89. envelope := NewEnvelope(options.TTL, options.Topics, self)
  90. envelope.Seal(pow)
  91. return envelope, nil
  92. }
  93. // sign calculates and sets the cryptographic signature for the message , also
  94. // setting the sign flag.
  95. func (self *Message) sign(key *ecdsa.PrivateKey) (err error) {
  96. self.Flags |= signatureFlag
  97. self.Signature, err = crypto.Sign(self.hash(), key)
  98. return
  99. }
  100. // Recover retrieves the public key of the message signer.
  101. func (self *Message) Recover() *ecdsa.PublicKey {
  102. defer func() { recover() }() // in case of invalid signature
  103. // Short circuit if no signature is present
  104. if self.Signature == nil {
  105. return nil
  106. }
  107. // Otherwise try and recover the signature
  108. pub, err := crypto.SigToPub(self.hash(), self.Signature)
  109. if err != nil {
  110. glog.V(logger.Error).Infof("Could not get public key from signature: %v", err)
  111. return nil
  112. }
  113. return pub
  114. }
  115. // encrypt encrypts a message payload with a public key.
  116. func (self *Message) encrypt(key *ecdsa.PublicKey) (err error) {
  117. self.Payload, err = crypto.Encrypt(key, self.Payload)
  118. return
  119. }
  120. // decrypt decrypts an encrypted payload with a private key.
  121. func (self *Message) decrypt(key *ecdsa.PrivateKey) error {
  122. cleartext, err := crypto.Decrypt(key, self.Payload)
  123. if err == nil {
  124. self.Payload = cleartext
  125. }
  126. return err
  127. }
  128. // hash calculates the SHA3 checksum of the message flags and payload.
  129. func (self *Message) hash() []byte {
  130. return crypto.Keccak256(append([]byte{self.Flags}, self.Payload...))
  131. }
  132. // bytes flattens the message contents (flags, signature and payload) into a
  133. // single binary blob.
  134. func (self *Message) bytes() []byte {
  135. return append([]byte{self.Flags}, append(self.Signature, self.Payload...)...)
  136. }