2015-07-07 03:54:22 +03:00
|
|
|
// Copyright 2014 The go-ethereum Authors
|
|
|
|
// This file is part of go-ethereum.
|
|
|
|
//
|
|
|
|
// go-ethereum is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// go-ethereum is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
|
|
// along with go-ethereum. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
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-04-14 12:12:47 +03: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 // ^^^^^^
|
2015-04-13 12:16:51 +03:00
|
|
|
Topics []Topic
|
2014-12-08 13:43:33 +02:00
|
|
|
Data []byte
|
|
|
|
Nonce uint32
|
|
|
|
|
2015-04-14 13:24:43 +03:00
|
|
|
hash common.Hash // Cached hash of the envelope to avoid rehashing every time
|
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.
|
2015-04-13 12:16:51 +03:00
|
|
|
func NewEnvelope(ttl time.Duration, topics []Topic, 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
|
|
|
// 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],
|
2015-04-21 11:43:11 +03:00
|
|
|
Sent: time.Unix(int64(self.Expiry-self.TTL), 0),
|
|
|
|
TTL: time.Duration(self.TTL) * time.Second,
|
2015-04-20 14:56:38 +03:00
|
|
|
Hash: self.Hash(),
|
2015-04-10 15:53:21 +03:00
|
|
|
}
|
|
|
|
data = data[1:]
|
|
|
|
|
2015-04-13 11:31:51 +03: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 23:23:42 +02:00
|
|
|
}
|
2015-04-13 11:31:51 +03:00
|
|
|
message.Signature, data = data[:signatureLength], data[signatureLength:]
|
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-13 11:31:51 +03:00
|
|
|
// Decrypt the message, if requested
|
2015-04-10 16:52:01 +03:00
|
|
|
if key == nil {
|
|
|
|
return message, nil
|
2014-12-10 15:17:32 +02:00
|
|
|
}
|
2015-04-14 12:12:47 +03:00
|
|
|
err = message.decrypt(key)
|
|
|
|
switch err {
|
2015-04-10 16:52:01 +03:00
|
|
|
case nil:
|
|
|
|
return message, nil
|
2014-12-08 13:43:33 +02:00
|
|
|
|
2015-04-14 12:12:47 +03:00
|
|
|
case ecies.ErrInvalidPublicKey: // Payload isn't encrypted
|
|
|
|
return message, err
|
|
|
|
|
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-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-04-14 13:05:36 +03: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-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
|
|
|
|
}
|