envelope.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 Envelope element. For formal details please see
  17. // the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#envelopes.
  18. package whisperv2
  19. import (
  20. "crypto/ecdsa"
  21. "encoding/binary"
  22. "fmt"
  23. "math/big"
  24. "time"
  25. "github.com/ethereum/go-ethereum/common"
  26. "github.com/ethereum/go-ethereum/common/math"
  27. "github.com/ethereum/go-ethereum/crypto"
  28. "github.com/ethereum/go-ethereum/crypto/ecies"
  29. "github.com/ethereum/go-ethereum/rlp"
  30. )
  31. // Envelope represents a clear-text data packet to transmit through the Whisper
  32. // network. Its contents may or may not be encrypted and signed.
  33. type Envelope struct {
  34. Expiry uint32 // Whisper protocol specifies int32, really should be int64
  35. TTL uint32 // ^^^^^^
  36. Topics []Topic
  37. Data []byte
  38. Nonce uint32
  39. hash common.Hash // Cached hash of the envelope to avoid rehashing every time
  40. }
  41. // NewEnvelope wraps a Whisper message with expiration and destination data
  42. // included into an envelope for network forwarding.
  43. func NewEnvelope(ttl time.Duration, topics []Topic, msg *Message) *Envelope {
  44. return &Envelope{
  45. Expiry: uint32(time.Now().Add(ttl).Unix()),
  46. TTL: uint32(ttl.Seconds()),
  47. Topics: topics,
  48. Data: msg.bytes(),
  49. Nonce: 0,
  50. }
  51. }
  52. // Seal closes the envelope by spending the requested amount of time as a proof
  53. // of work on hashing the data.
  54. func (self *Envelope) Seal(pow time.Duration) {
  55. d := make([]byte, 64)
  56. copy(d[:32], self.rlpWithoutNonce())
  57. finish, bestBit := time.Now().Add(pow).UnixNano(), 0
  58. for nonce := uint32(0); time.Now().UnixNano() < finish; {
  59. for i := 0; i < 1024; i++ {
  60. binary.BigEndian.PutUint32(d[60:], nonce)
  61. d := new(big.Int).SetBytes(crypto.Keccak256(d))
  62. firstBit := math.FirstBitSet(d)
  63. if firstBit > bestBit {
  64. self.Nonce, bestBit = nonce, firstBit
  65. }
  66. nonce++
  67. }
  68. }
  69. }
  70. // rlpWithoutNonce returns the RLP encoded envelope contents, except the nonce.
  71. func (self *Envelope) rlpWithoutNonce() []byte {
  72. enc, _ := rlp.EncodeToBytes([]interface{}{self.Expiry, self.TTL, self.Topics, self.Data})
  73. return enc
  74. }
  75. // Open extracts the message contained within a potentially encrypted envelope.
  76. func (self *Envelope) Open(key *ecdsa.PrivateKey) (msg *Message, err error) {
  77. // Split open the payload into a message construct
  78. data := self.Data
  79. message := &Message{
  80. Flags: data[0],
  81. Sent: time.Unix(int64(self.Expiry-self.TTL), 0),
  82. TTL: time.Duration(self.TTL) * time.Second,
  83. Hash: self.Hash(),
  84. }
  85. data = data[1:]
  86. if message.Flags&signatureFlag == signatureFlag {
  87. if len(data) < signatureLength {
  88. return nil, fmt.Errorf("unable to open envelope. First bit set but len(data) < len(signature)")
  89. }
  90. message.Signature, data = data[:signatureLength], data[signatureLength:]
  91. }
  92. message.Payload = data
  93. // Decrypt the message, if requested
  94. if key == nil {
  95. return message, nil
  96. }
  97. err = message.decrypt(key)
  98. switch err {
  99. case nil:
  100. return message, nil
  101. case ecies.ErrInvalidPublicKey: // Payload isn't encrypted
  102. return message, err
  103. default:
  104. return nil, fmt.Errorf("unable to open envelope, decrypt failed: %v", err)
  105. }
  106. }
  107. // Hash returns the SHA3 hash of the envelope, calculating it if not yet done.
  108. func (self *Envelope) Hash() common.Hash {
  109. if (self.hash == common.Hash{}) {
  110. enc, _ := rlp.EncodeToBytes(self)
  111. self.hash = crypto.Keccak256Hash(enc)
  112. }
  113. return self.hash
  114. }
  115. // DecodeRLP decodes an Envelope from an RLP data stream.
  116. func (self *Envelope) DecodeRLP(s *rlp.Stream) error {
  117. raw, err := s.Raw()
  118. if err != nil {
  119. return err
  120. }
  121. // The decoding of Envelope uses the struct fields but also needs
  122. // to compute the hash of the whole RLP-encoded envelope. This
  123. // type has the same structure as Envelope but is not an
  124. // rlp.Decoder so we can reuse the Envelope struct definition.
  125. type rlpenv Envelope
  126. if err := rlp.DecodeBytes(raw, (*rlpenv)(self)); err != nil {
  127. return err
  128. }
  129. self.hash = crypto.Keccak256Hash(raw)
  130. return nil
  131. }