2015-04-10 08:52:01 -05:00
|
|
|
// Contains the Whisper protocol Envelope element. For formal details please see
|
|
|
|
// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#envelopes.
|
|
|
|
|
2014-12-08 05:43:33 -06:00
|
|
|
package whisper
|
|
|
|
|
|
|
|
import (
|
2014-12-10 07:17:32 -06:00
|
|
|
"crypto/ecdsa"
|
2014-12-08 05:43:33 -06:00
|
|
|
"encoding/binary"
|
2014-12-10 07:17:32 -06:00
|
|
|
"fmt"
|
2014-12-08 05:43:33 -06:00
|
|
|
"time"
|
|
|
|
|
2015-03-19 09:18:31 -05:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2014-12-08 05:43:33 -06:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-04-14 04:12:47 -05:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/ecies"
|
2014-12-08 05:43:33 -06:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2015-04-10 08:52:01 -05:00
|
|
|
// Envelope represents a clear-text data packet to transmit through the Whisper
|
|
|
|
// network. Its contents may or may not be encrypted and signed.
|
2014-12-08 05:43:33 -06:00
|
|
|
type Envelope struct {
|
2014-12-09 17:03:50 -06:00
|
|
|
Expiry uint32 // Whisper protocol specifies int32, really should be int64
|
2015-03-20 18:57:18 -05:00
|
|
|
TTL uint32 // ^^^^^^
|
2015-04-13 04:16:51 -05:00
|
|
|
Topics []Topic
|
2014-12-08 05:43:33 -06:00
|
|
|
Data []byte
|
|
|
|
Nonce uint32
|
|
|
|
|
2015-04-14 05:24:43 -05:00
|
|
|
hash common.Hash // Cached hash of the envelope to avoid rehashing every time
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
|
2015-04-10 08:52:01 -05:00
|
|
|
// NewEnvelope wraps a Whisper message with expiration and destination data
|
|
|
|
// included into an envelope for network forwarding.
|
2015-04-13 04:16:51 -05:00
|
|
|
func NewEnvelope(ttl time.Duration, topics []Topic, msg *Message) *Envelope {
|
2015-03-20 18:57:18 -05:00
|
|
|
return &Envelope{
|
2015-04-10 08:52:01 -05:00
|
|
|
Expiry: uint32(time.Now().Add(ttl).Unix()),
|
2015-03-20 18:57:18 -05:00
|
|
|
TTL: uint32(ttl.Seconds()),
|
|
|
|
Topics: topics,
|
2015-04-10 08:52:01 -05:00
|
|
|
Data: msg.bytes(),
|
2015-03-20 18:57:18 -05:00
|
|
|
Nonce: 0,
|
|
|
|
}
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
|
2015-04-10 08:52:01 -05:00
|
|
|
// Seal closes the envelope by spending the requested amount of time as a proof
|
|
|
|
// of work on hashing the data.
|
2014-12-09 17:03:50 -06:00
|
|
|
func (self *Envelope) Seal(pow time.Duration) {
|
2015-04-10 08:52:01 -05:00
|
|
|
d := make([]byte, 64)
|
|
|
|
copy(d[:32], self.rlpWithoutNonce())
|
|
|
|
|
|
|
|
finish, bestBit := time.Now().Add(pow).UnixNano(), 0
|
|
|
|
for nonce := uint32(0); time.Now().UnixNano() < finish; {
|
|
|
|
for i := 0; i < 1024; i++ {
|
|
|
|
binary.BigEndian.PutUint32(d[60:], nonce)
|
|
|
|
|
|
|
|
firstBit := common.FirstBitSet(common.BigD(crypto.Sha3(d)))
|
|
|
|
if firstBit > bestBit {
|
|
|
|
self.Nonce, bestBit = nonce, firstBit
|
|
|
|
}
|
|
|
|
nonce++
|
|
|
|
}
|
|
|
|
}
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
|
2015-04-12 06:34:53 -05:00
|
|
|
// rlpWithoutNonce returns the RLP encoded envelope contents, except the nonce.
|
2015-04-10 08:52:01 -05:00
|
|
|
func (self *Envelope) rlpWithoutNonce() []byte {
|
|
|
|
enc, _ := rlp.EncodeToBytes([]interface{}{self.Expiry, self.TTL, self.Topics, self.Data})
|
|
|
|
return enc
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open extracts the message contained within a potentially encrypted envelope.
|
2015-04-10 07:53:21 -05:00
|
|
|
func (self *Envelope) Open(key *ecdsa.PrivateKey) (msg *Message, err error) {
|
2015-04-10 08:52:01 -05:00
|
|
|
// Split open the payload into a message construct
|
2014-12-10 07:17:32 -06:00
|
|
|
data := self.Data
|
2015-04-10 07:53:21 -05:00
|
|
|
|
2015-04-10 08:52:01 -05:00
|
|
|
message := &Message{
|
2015-04-10 07:53:21 -05:00
|
|
|
Flags: data[0],
|
|
|
|
}
|
|
|
|
data = data[1:]
|
|
|
|
|
2015-04-13 03:31:51 -05:00
|
|
|
if message.Flags&signatureFlag == signatureFlag {
|
|
|
|
if len(data) < signatureLength {
|
|
|
|
return nil, fmt.Errorf("unable to open envelope. First bit set but len(data) < len(signature)")
|
2014-12-12 15:23:42 -06:00
|
|
|
}
|
2015-04-13 03:31:51 -05:00
|
|
|
message.Signature, data = data[:signatureLength], data[signatureLength:]
|
2014-12-12 15:23:42 -06:00
|
|
|
}
|
2015-04-10 07:53:21 -05:00
|
|
|
message.Payload = data
|
2014-12-16 12:55:57 -06:00
|
|
|
|
2015-04-13 03:31:51 -05:00
|
|
|
// Decrypt the message, if requested
|
2015-04-10 08:52:01 -05:00
|
|
|
if key == nil {
|
|
|
|
return message, nil
|
2014-12-10 07:17:32 -06:00
|
|
|
}
|
2015-04-14 04:12:47 -05:00
|
|
|
err = message.decrypt(key)
|
|
|
|
switch err {
|
2015-04-10 08:52:01 -05:00
|
|
|
case nil:
|
|
|
|
return message, nil
|
2014-12-08 05:43:33 -06:00
|
|
|
|
2015-04-14 04:12:47 -05:00
|
|
|
case ecies.ErrInvalidPublicKey: // Payload isn't encrypted
|
|
|
|
return message, err
|
|
|
|
|
2015-04-10 08:52:01 -05:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unable to open envelope, decrypt failed: %v", err)
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 08:52:01 -05:00
|
|
|
// Hash returns the SHA3 hash of the envelope, calculating it if not yet done.
|
|
|
|
func (self *Envelope) Hash() common.Hash {
|
|
|
|
if (self.hash == common.Hash{}) {
|
|
|
|
enc, _ := rlp.EncodeToBytes(self)
|
|
|
|
self.hash = crypto.Sha3Hash(enc)
|
|
|
|
}
|
|
|
|
return self.hash
|
2014-12-08 05:43:33 -06:00
|
|
|
}
|
|
|
|
|
2015-04-10 08:52:01 -05:00
|
|
|
// DecodeRLP decodes an Envelope from an RLP data stream.
|
2015-01-13 06:36:44 -06:00
|
|
|
func (self *Envelope) DecodeRLP(s *rlp.Stream) error {
|
2015-03-20 18:49:58 -05:00
|
|
|
raw, err := s.Raw()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-13 06:36:44 -06:00
|
|
|
}
|
2015-04-14 05:05:36 -05:00
|
|
|
// The decoding of Envelope uses the struct fields but also needs
|
|
|
|
// to compute the hash of the whole RLP-encoded envelope. This
|
|
|
|
// type has the same structure as Envelope but is not an
|
|
|
|
// rlp.Decoder so we can reuse the Envelope struct definition.
|
|
|
|
type rlpenv Envelope
|
2015-03-20 18:49:58 -05:00
|
|
|
if err := rlp.DecodeBytes(raw, (*rlpenv)(self)); err != nil {
|
2015-01-13 06:36:44 -06:00
|
|
|
return err
|
|
|
|
}
|
2015-03-20 18:57:18 -05:00
|
|
|
self.hash = crypto.Sha3Hash(raw)
|
2015-01-13 06:36:44 -06:00
|
|
|
return nil
|
|
|
|
}
|