2015-04-10 15:53:21 +03:00
|
|
|
// Contains the Whisper protocol Message element. For formal details please see
|
|
|
|
// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#messages.
|
|
|
|
|
2014-12-08 13:43:33 +02:00
|
|
|
package whisper
|
|
|
|
|
2014-12-10 01:03:50 +02:00
|
|
|
import (
|
2014-12-10 15:17:32 +02:00
|
|
|
"crypto/ecdsa"
|
2015-04-10 15:53:21 +03:00
|
|
|
"math/rand"
|
2014-12-10 01:03:50 +02:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2015-04-07 13:48:13 +03:00
|
|
|
"github.com/ethereum/go-ethereum/logger"
|
2015-04-05 20:31:18 +03:00
|
|
|
"github.com/ethereum/go-ethereum/logger/glog"
|
2014-12-10 01:03:50 +02:00
|
|
|
)
|
|
|
|
|
2015-04-10 15:53:21 +03:00
|
|
|
// Message represents an end-user data packet to trasmit through the Whisper
|
|
|
|
// protocol. These are wrapped into Envelopes that need not be understood by
|
|
|
|
// intermediate nodes, just forwarded.
|
2014-12-08 13:43:33 +02:00
|
|
|
type Message struct {
|
2015-04-10 16:52:01 +03:00
|
|
|
Flags byte // First bit is signature presence, rest reserved and should be random
|
2014-12-08 13:43:33 +02:00
|
|
|
Signature []byte
|
|
|
|
Payload []byte
|
2015-02-03 17:16:05 +02:00
|
|
|
Sent int64
|
2015-02-10 14:20:06 +02:00
|
|
|
|
|
|
|
To *ecdsa.PublicKey
|
2014-12-08 13:43:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 15:53:21 +03:00
|
|
|
// Options specifies the exact way a message should be wrapped into an Envelope.
|
|
|
|
type Options struct {
|
|
|
|
From *ecdsa.PrivateKey
|
|
|
|
To *ecdsa.PublicKey
|
|
|
|
TTL time.Duration
|
2015-04-14 11:12:09 +03:00
|
|
|
Topics []Topic
|
2015-04-10 15:53:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
|
2014-12-08 13:43:33 +02:00
|
|
|
func NewMessage(payload []byte) *Message {
|
2015-04-13 11:31:51 +03:00
|
|
|
// Construct an initial flag set: no signature, rest random
|
|
|
|
flags := byte(rand.Intn(256))
|
|
|
|
flags &= ^signatureFlag
|
2015-04-10 15:53:21 +03:00
|
|
|
|
|
|
|
// Assemble and return the message
|
|
|
|
return &Message{
|
|
|
|
Flags: flags,
|
|
|
|
Payload: payload,
|
|
|
|
Sent: time.Now().Unix(),
|
|
|
|
}
|
2014-12-08 13:43:33 +02:00
|
|
|
}
|
|
|
|
|
2015-04-10 15:53:21 +03:00
|
|
|
// Wrap bundles the message into an Envelope to transmit over the network.
|
|
|
|
//
|
2015-04-12 14:34:53 +03:00
|
|
|
// pow (Proof Of Work) controls how much time to spend on hashing the message,
|
2015-04-10 15:53:21 +03:00
|
|
|
// inherently controlling its priority through the network (smaller hash, bigger
|
|
|
|
// priority).
|
|
|
|
//
|
|
|
|
// The user can control the amount of identity, privacy and encryption through
|
|
|
|
// the options parameter as follows:
|
|
|
|
// - options.From == nil && options.To == nil: anonymous broadcast
|
|
|
|
// - options.From != nil && options.To == nil: signed broadcast (known sender)
|
|
|
|
// - options.From == nil && options.To != nil: encrypted anonymous message
|
|
|
|
// - options.From != nil && options.To != nil: encrypted signed message
|
|
|
|
func (self *Message) Wrap(pow time.Duration, options Options) (*Envelope, error) {
|
|
|
|
// Use the default TTL if non was specified
|
|
|
|
if options.TTL == 0 {
|
|
|
|
options.TTL = DefaultTimeToLive
|
|
|
|
}
|
|
|
|
// Sign and encrypt the message if requested
|
|
|
|
if options.From != nil {
|
|
|
|
if err := self.sign(options.From); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if options.To != nil {
|
|
|
|
if err := self.encrypt(options.To); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Wrap the processed message, seal it and return
|
2015-04-14 11:12:09 +03:00
|
|
|
envelope := NewEnvelope(options.TTL, options.Topics, self)
|
2015-04-10 15:53:21 +03:00
|
|
|
envelope.Seal(pow)
|
|
|
|
|
|
|
|
return envelope, nil
|
2014-12-10 01:03:50 +02:00
|
|
|
}
|
|
|
|
|
2015-04-12 14:34:53 +03:00
|
|
|
// sign calculates and sets the cryptographic signature for the message , also
|
2015-04-10 15:53:21 +03:00
|
|
|
// setting the sign flag.
|
2014-12-10 15:17:32 +02:00
|
|
|
func (self *Message) sign(key *ecdsa.PrivateKey) (err error) {
|
2015-04-13 11:31:51 +03:00
|
|
|
self.Flags |= signatureFlag
|
2014-12-10 01:03:50 +02:00
|
|
|
self.Signature, err = crypto.Sign(self.hash(), key)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2015-04-10 15:53:21 +03:00
|
|
|
// Recover retrieves the public key of the message signer.
|
2014-12-10 15:17:32 +02:00
|
|
|
func (self *Message) Recover() *ecdsa.PublicKey {
|
2015-04-10 15:53:21 +03:00
|
|
|
defer func() { recover() }() // in case of invalid signature
|
|
|
|
|
2015-04-05 20:31:18 +03:00
|
|
|
pub, err := crypto.SigToPub(self.hash(), self.Signature)
|
|
|
|
if err != nil {
|
2015-04-10 15:53:21 +03:00
|
|
|
glog.V(logger.Error).Infof("Could not get public key from signature: %v", err)
|
2015-04-05 20:31:18 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return pub
|
2014-12-10 15:17:32 +02:00
|
|
|
}
|
|
|
|
|
2015-04-12 14:34:53 +03:00
|
|
|
// encrypt encrypts a message payload with a public key.
|
2015-04-13 11:31:51 +03:00
|
|
|
func (self *Message) encrypt(key *ecdsa.PublicKey) (err error) {
|
|
|
|
self.Payload, err = crypto.Encrypt(key, self.Payload)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// decrypt decrypts an encrypted payload with a private key.
|
|
|
|
func (self *Message) decrypt(key *ecdsa.PrivateKey) (err error) {
|
|
|
|
self.Payload, err = crypto.Decrypt(key, self.Payload)
|
2015-04-10 15:53:21 +03:00
|
|
|
return
|
2014-12-08 13:43:33 +02:00
|
|
|
}
|
2014-12-10 01:03:50 +02:00
|
|
|
|
2015-04-12 14:34:53 +03:00
|
|
|
// hash calculates the SHA3 checksum of the message flags and payload.
|
2015-04-10 15:53:21 +03:00
|
|
|
func (self *Message) hash() []byte {
|
|
|
|
return crypto.Sha3(append([]byte{self.Flags}, self.Payload...))
|
2014-12-10 01:03:50 +02:00
|
|
|
}
|
|
|
|
|
2015-04-12 14:34:53 +03:00
|
|
|
// bytes flattens the message contents (flags, signature and payload) into a
|
2015-04-10 15:53:21 +03:00
|
|
|
// single binary blob.
|
|
|
|
func (self *Message) bytes() []byte {
|
|
|
|
return append([]byte{self.Flags}, append(self.Signature, self.Payload...)...)
|
2014-12-10 01:03:50 +02:00
|
|
|
}
|