2015-04-10 16:52:01 +03: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 13:43:33 +02:00
|
|
|
package whisper
|
|
|
|
|
|
|
|
import (
|
2014-12-10 15:17:32 +02:00
|
|
|
"crypto/ecdsa"
|
2014-12-08 13:43:33 +02:00
|
|
|
"encoding/binary"
|
2014-12-10 15:17:32 +02:00
|
|
|
"fmt"
|
2014-12-08 13:43:33 +02:00
|
|
|
"time"
|
|
|
|
|
2015-03-19 16:18:31 +02:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2014-12-08 13:43:33 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-02-14 01:25:47 +02:00
|
|
|
"github.com/ethereum/go-ethereum/crypto/ecies"
|
2014-12-08 13:43:33 +02:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
)
|
|
|
|
|
2015-04-10 16:52:01 +03: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 13:43:33 +02:00
|
|
|
type Envelope struct {
|
2014-12-10 01:03:50 +02:00
|
|
|
Expiry uint32 // Whisper protocol specifies int32, really should be int64
|
2015-03-21 01:57:18 +02:00
|
|
|
TTL uint32 // ^^^^^^
|
2014-12-08 13:43:33 +02:00
|
|
|
Topics [][]byte
|
|
|
|
Data []byte
|
|
|
|
Nonce uint32
|
|
|
|
|
2015-03-21 01:57:18 +02:00
|
|
|
hash common.Hash
|
2014-12-08 13:43:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 16:52:01 +03:00
|
|
|
// NewEnvelope wraps a Whisper message with expiration and destination data
|
|
|
|
// included into an envelope for network forwarding.
|
|
|
|
func NewEnvelope(ttl time.Duration, topics [][]byte, msg *Message) *Envelope {
|
2015-03-21 01:57:18 +02:00
|
|
|
return &Envelope{
|
2015-04-10 16:52:01 +03:00
|
|
|
Expiry: uint32(time.Now().Add(ttl).Unix()),
|
2015-03-21 01:57:18 +02:00
|
|
|
TTL: uint32(ttl.Seconds()),
|
|
|
|
Topics: topics,
|
2015-04-10 16:52:01 +03:00
|
|
|
Data: msg.bytes(),
|
2015-03-21 01:57:18 +02:00
|
|
|
Nonce: 0,
|
|
|
|
}
|
2014-12-08 13:43:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 16:52:01 +03:00
|
|
|
// Seal closes the envelope by spending the requested amount of time as a proof
|
|
|
|
// of work on hashing the data.
|
2014-12-10 01:03:50 +02:00
|
|
|
func (self *Envelope) Seal(pow time.Duration) {
|
2015-04-10 16:52:01 +03: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 13:43:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-12 14:34:53 +03:00
|
|
|
// valid checks whether the claimed proof of work was indeed executed.
|
2015-04-10 16:52:01 +03:00
|
|
|
// TODO: Is this really useful? Isn't this always true?
|
|
|
|
func (self *Envelope) valid() bool {
|
|
|
|
d := make([]byte, 64)
|
|
|
|
copy(d[:32], self.rlpWithoutNonce())
|
|
|
|
binary.BigEndian.PutUint32(d[60:], self.Nonce)
|
|
|
|
|
|
|
|
return common.FirstBitSet(common.BigD(crypto.Sha3(d))) > 0
|
|
|
|
}
|
|
|
|
|
2015-04-12 14:34:53 +03:00
|
|
|
// rlpWithoutNonce returns the RLP encoded envelope contents, except the nonce.
|
2015-04-10 16:52:01 +03: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 15:53:21 +03:00
|
|
|
func (self *Envelope) Open(key *ecdsa.PrivateKey) (msg *Message, err error) {
|
2015-04-10 16:52:01 +03:00
|
|
|
// Split open the payload into a message construct
|
2014-12-10 15:17:32 +02:00
|
|
|
data := self.Data
|
2015-04-10 15:53:21 +03:00
|
|
|
|
2015-04-10 16:52:01 +03:00
|
|
|
message := &Message{
|
2015-04-10 15:53:21 +03:00
|
|
|
Flags: data[0],
|
|
|
|
}
|
|
|
|
data = data[1:]
|
|
|
|
|
|
|
|
if message.Flags&128 == 128 {
|
|
|
|
if len(data) < 65 {
|
|
|
|
return nil, fmt.Errorf("unable to open envelope. First bit set but len(data) < 65")
|
2014-12-12 23:23:42 +02:00
|
|
|
}
|
2015-04-10 15:53:21 +03:00
|
|
|
message.Signature, data = data[:65], data[65:]
|
2014-12-12 23:23:42 +02:00
|
|
|
}
|
2015-04-10 15:53:21 +03:00
|
|
|
message.Payload = data
|
2014-12-16 20:55:57 +02:00
|
|
|
|
2015-04-10 16:52:01 +03:00
|
|
|
// Short circuit if the encryption was requested
|
|
|
|
if key == nil {
|
|
|
|
return message, nil
|
2014-12-10 15:17:32 +02:00
|
|
|
}
|
2015-04-10 16:52:01 +03:00
|
|
|
// Otherwise try to decrypt the message
|
|
|
|
message.Payload, err = crypto.Decrypt(key, message.Payload)
|
|
|
|
switch err {
|
|
|
|
case nil:
|
|
|
|
return message, nil
|
2014-12-08 13:43:33 +02:00
|
|
|
|
2015-04-10 16:52:01 +03:00
|
|
|
case ecies.ErrInvalidPublicKey: // Payload isn't encrypted
|
|
|
|
return message, err
|
2014-12-08 13:43:33 +02:00
|
|
|
|
2015-04-10 16:52:01 +03:00
|
|
|
default:
|
|
|
|
return nil, fmt.Errorf("unable to open envelope, decrypt failed: %v", err)
|
2014-12-08 13:43:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-10 16:52:01 +03: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 13:43:33 +02:00
|
|
|
}
|
|
|
|
|
2015-03-21 01:49:58 +02:00
|
|
|
// rlpenv is an Envelope but is not an rlp.Decoder.
|
|
|
|
// It is used for decoding because we need to
|
|
|
|
type rlpenv Envelope
|
2015-01-13 14:36:44 +02:00
|
|
|
|
2015-04-10 16:52:01 +03:00
|
|
|
// DecodeRLP decodes an Envelope from an RLP data stream.
|
2015-01-13 14:36:44 +02:00
|
|
|
func (self *Envelope) DecodeRLP(s *rlp.Stream) error {
|
2015-03-21 01:49:58 +02:00
|
|
|
raw, err := s.Raw()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2015-01-13 14:36:44 +02:00
|
|
|
}
|
2015-03-21 01:49:58 +02:00
|
|
|
if err := rlp.DecodeBytes(raw, (*rlpenv)(self)); err != nil {
|
2015-01-13 14:36:44 +02:00
|
|
|
return err
|
|
|
|
}
|
2015-03-21 01:57:18 +02:00
|
|
|
self.hash = crypto.Sha3Hash(raw)
|
2015-01-13 14:36:44 +02:00
|
|
|
return nil
|
|
|
|
}
|