diff --git a/cmd/gethrpctest/main.go b/cmd/gethrpctest/main.go
index d267dbf585..5e2b9cde3d 100644
--- a/cmd/gethrpctest/main.go
+++ b/cmd/gethrpctest/main.go
@@ -32,7 +32,7 @@ import (
"github.com/ethereum/go-ethereum/node"
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/tests"
- "github.com/ethereum/go-ethereum/whisper"
+ whisper "github.com/ethereum/go-ethereum/whisper/whisper02"
)
const defaultTestKey = "b71c71a67e1177ad4e901695e1b4b9ee17ae16c6668d313eac2f96dbcda3f291"
diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go
index 3ab556a8fc..95d89bb33b 100644
--- a/cmd/utils/flags.go
+++ b/cmd/utils/flags.go
@@ -48,7 +48,7 @@ import (
"github.com/ethereum/go-ethereum/params"
"github.com/ethereum/go-ethereum/pow"
"github.com/ethereum/go-ethereum/rpc"
- "github.com/ethereum/go-ethereum/whisper"
+ whisper "github.com/ethereum/go-ethereum/whisper/whisper02"
"gopkg.in/urfave/cli.v1"
)
diff --git a/whisper/api.go b/whisper/whisper02/api.go
similarity index 99%
rename from whisper/api.go
rename to whisper/whisper02/api.go
index d1f454a1fa..e1b28468d9 100644
--- a/whisper/api.go
+++ b/whisper/whisper02/api.go
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see .
-package whisper
+package whisper02
import (
"encoding/json"
diff --git a/whisper/doc.go b/whisper/whisper02/doc.go
similarity index 98%
rename from whisper/doc.go
rename to whisper/whisper02/doc.go
index cfb0b51175..d2603650c7 100644
--- a/whisper/doc.go
+++ b/whisper/whisper02/doc.go
@@ -29,4 +29,4 @@ Whisper is a pure identity-based messaging system. Whisper provides a low-level
or prejudiced by the low-level hardware attributes and characteristics,
particularly the notion of singular endpoints.
*/
-package whisper
+package whisper02
diff --git a/whisper/envelope.go b/whisper/whisper02/envelope.go
similarity index 99%
rename from whisper/envelope.go
rename to whisper/whisper02/envelope.go
index 97d489b961..b1d74c9d61 100644
--- a/whisper/envelope.go
+++ b/whisper/whisper02/envelope.go
@@ -17,7 +17,7 @@
// 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.
-package whisper
+package whisper02
import (
"crypto/ecdsa"
diff --git a/whisper/envelope_test.go b/whisper/whisper02/envelope_test.go
similarity index 99%
rename from whisper/envelope_test.go
rename to whisper/whisper02/envelope_test.go
index 3bfe527372..c7172024f7 100644
--- a/whisper/envelope_test.go
+++ b/whisper/whisper02/envelope_test.go
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see .
-package whisper
+package whisper02
import (
"bytes"
diff --git a/whisper/filter.go b/whisper/whisper02/filter.go
similarity index 99%
rename from whisper/filter.go
rename to whisper/whisper02/filter.go
index 9f6d6b7817..5a2108fa6d 100644
--- a/whisper/filter.go
+++ b/whisper/whisper02/filter.go
@@ -16,7 +16,7 @@
// Contains the message filter for fine grained subscriptions.
-package whisper
+package whisper02
import (
"crypto/ecdsa"
diff --git a/whisper/filter_test.go b/whisper/whisper02/filter_test.go
similarity index 99%
rename from whisper/filter_test.go
rename to whisper/whisper02/filter_test.go
index b805b2baf1..7bbaa328f6 100644
--- a/whisper/filter_test.go
+++ b/whisper/whisper02/filter_test.go
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see .
-package whisper
+package whisper02
import (
"bytes"
diff --git a/whisper/main.go b/whisper/whisper02/main.go
similarity index 100%
rename from whisper/main.go
rename to whisper/whisper02/main.go
diff --git a/whisper/message.go b/whisper/whisper02/message.go
similarity index 99%
rename from whisper/message.go
rename to whisper/whisper02/message.go
index f05b5d8b54..a3f59a690e 100644
--- a/whisper/message.go
+++ b/whisper/whisper02/message.go
@@ -17,7 +17,7 @@
// 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.
-package whisper
+package whisper02
import (
"crypto/ecdsa"
diff --git a/whisper/message_test.go b/whisper/whisper02/message_test.go
similarity index 99%
rename from whisper/message_test.go
rename to whisper/whisper02/message_test.go
index 921c967a96..e87238d0b7 100644
--- a/whisper/message_test.go
+++ b/whisper/whisper02/message_test.go
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see .
-package whisper
+package whisper02
import (
"bytes"
diff --git a/whisper/peer.go b/whisper/whisper02/peer.go
similarity index 99%
rename from whisper/peer.go
rename to whisper/whisper02/peer.go
index ee10e66e79..3e108816d3 100644
--- a/whisper/peer.go
+++ b/whisper/whisper02/peer.go
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see .
-package whisper
+package whisper02
import (
"fmt"
diff --git a/whisper/peer_test.go b/whisper/whisper02/peer_test.go
similarity index 99%
rename from whisper/peer_test.go
rename to whisper/whisper02/peer_test.go
index 636bd8ca1f..2ec7a51c15 100644
--- a/whisper/peer_test.go
+++ b/whisper/whisper02/peer_test.go
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see .
-package whisper
+package whisper02
import (
"testing"
diff --git a/whisper/topic.go b/whisper/whisper02/topic.go
similarity index 99%
rename from whisper/topic.go
rename to whisper/whisper02/topic.go
index d37eb25ee1..2a80df4884 100644
--- a/whisper/topic.go
+++ b/whisper/whisper02/topic.go
@@ -17,7 +17,7 @@
// Contains the Whisper protocol Topic element. For formal details please see
// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#topics.
-package whisper
+package whisper02
import "github.com/ethereum/go-ethereum/crypto"
diff --git a/whisper/topic_test.go b/whisper/whisper02/topic_test.go
similarity index 99%
rename from whisper/topic_test.go
rename to whisper/whisper02/topic_test.go
index 9c45f67408..21f5e55b01 100644
--- a/whisper/topic_test.go
+++ b/whisper/whisper02/topic_test.go
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see .
-package whisper
+package whisper02
import (
"bytes"
diff --git a/whisper/whisper.go b/whisper/whisper02/whisper.go
similarity index 99%
rename from whisper/whisper.go
rename to whisper/whisper02/whisper.go
index 0a49c1000b..f0f38f29ab 100644
--- a/whisper/whisper.go
+++ b/whisper/whisper02/whisper.go
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see .
-package whisper
+package whisper02
import (
"crypto/ecdsa"
diff --git a/whisper/whisper_test.go b/whisper/whisper02/whisper_test.go
similarity index 99%
rename from whisper/whisper_test.go
rename to whisper/whisper02/whisper_test.go
index 094df373c2..42e2935732 100644
--- a/whisper/whisper_test.go
+++ b/whisper/whisper02/whisper_test.go
@@ -14,7 +14,7 @@
// You should have received a copy of the GNU Lesser General Public License
// along with the go-ethereum library. If not, see .
-package whisper
+package whisper02
import (
"testing"
diff --git a/whisper/whisper05/api.go b/whisper/whisper05/api.go
new file mode 100644
index 0000000000..928cdcb31a
--- /dev/null
+++ b/whisper/whisper05/api.go
@@ -0,0 +1,487 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library 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.
+//
+// The go-ethereum library 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 the go-ethereum library. If not, see .
+
+package whisper05
+
+import (
+ "encoding/json"
+ "errors"
+ "fmt"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/logger/glog"
+ "github.com/ethereum/go-ethereum/rpc"
+
+ mathrand "math/rand"
+)
+
+// PublicWhisperAPI provides the whisper RPC service.
+type PublicWhisperAPI struct {
+ whisper *Whisper
+}
+
+// NewPublicWhisperAPI create a new RPC whisper service.
+func NewPublicWhisperAPI(w *Whisper) *PublicWhisperAPI {
+ return &PublicWhisperAPI{whisper: w}
+}
+
+// Version returns the Whisper version this node offers.
+func (self *PublicWhisperAPI) Version() (*rpc.HexNumber, error) {
+ if self.whisper == nil {
+ return rpc.NewHexNumber(0), whisperOffLineErr
+ }
+ return rpc.NewHexNumber(self.whisper.Version()), nil
+}
+
+// MarkPeerTrusted marks specific peer trusted, which will allow it
+// to send historic (expired) messages.
+func (self *PublicWhisperAPI) MarkPeerTrusted(peerID *rpc.HexBytes) error {
+ if self.whisper == nil {
+ return whisperOffLineErr
+ }
+ return self.whisper.MarkPeerTrusted(peerID)
+}
+
+// RequestHistoricMessages requests the peer to deliver the old (expired) messages.
+// data contains parameters (time frame, payment details, etc.), required
+// by the remote email-like server. Whisper is not aware about the data format,
+// it will just forward the raw data to the server.
+func (self *PublicWhisperAPI) RequestHistoricMessages(peerID *rpc.HexBytes, data *rpc.HexBytes) error {
+ if self.whisper == nil {
+ return whisperOffLineErr
+ }
+ return self.whisper.RequestHistoricMessages(peerID, data)
+}
+
+// HasIdentity checks if the the whisper node is configured with the private key
+// of the specified public pair.
+func (self *PublicWhisperAPI) HasIdentity(identity string) (bool, error) {
+ if self.whisper == nil {
+ return false, whisperOffLineErr
+ }
+ return self.whisper.HasIdentity(crypto.ToECDSAPub(common.FromHex(identity))), nil
+}
+
+// DeleteIdentity deletes the specifies key if it exists.
+func (self *PublicWhisperAPI) DeleteIdentity(identity string) error {
+ if self.whisper == nil {
+ return whisperOffLineErr
+ }
+ self.whisper.DeleteIdentity(identity)
+ return nil
+}
+
+// NewIdentity generates a new cryptographic identity for the client, and injects
+// it into the known identities for message decryption.
+func (self *PublicWhisperAPI) NewIdentity() (string, error) {
+ if self.whisper == nil {
+ return "", whisperOffLineErr
+ }
+ identity := self.whisper.NewIdentity()
+ return common.ToHex(crypto.FromECDSAPub(&identity.PublicKey)), nil
+}
+
+// GenerateTopicKey generates a random key and stores it under the 'name' id.
+// Will be used in the future for session key exchange.
+func (self *PublicWhisperAPI) GenerateTopicKey(name string) error {
+ if self.whisper == nil {
+ return whisperOffLineErr
+ }
+ return self.whisper.GenerateTopicKey(name)
+}
+
+func (self *PublicWhisperAPI) AddTopicKey(name string, key []byte) error {
+ if self.whisper == nil {
+ return whisperOffLineErr
+ }
+ return self.whisper.AddTopicKey(name, key)
+}
+
+func (self *PublicWhisperAPI) HasTopicKey(name string) (bool, error) {
+ if self.whisper == nil {
+ return false, whisperOffLineErr
+ }
+ res := self.whisper.HasTopicKey(name)
+ return res, nil
+}
+
+func (self *PublicWhisperAPI) DeleteTopicKey(name string) error {
+ if self.whisper == nil {
+ return whisperOffLineErr
+ }
+ self.whisper.DeleteTopicKey(name)
+ return nil
+}
+
+// NewWhisperFilter creates and registers a new message filter to watch for inbound whisper messages.
+func (self *PublicWhisperAPI) NewFilter(args WhisperFilterArgs) (*rpc.HexNumber, error) {
+ if self.whisper == nil {
+ return nil, whisperOffLineErr
+ }
+
+ filter := Filter{
+ Src: crypto.ToECDSAPub(args.From),
+ Dst: crypto.ToECDSAPub(args.To),
+ KeySym: self.whisper.GetTopicKey(args.KeyName),
+ PoW: args.PoW,
+ messages: make(map[common.Hash]*ReceivedMessage),
+ acceptP2P: args.AcceptP2P,
+ }
+
+ if len(filter.KeySym) > 0 {
+ filter.TopicKeyHash = crypto.Keccak256Hash(filter.KeySym)
+ }
+
+ for _, t := range args.Topics {
+ filter.Topics = append(filter.Topics, t)
+ }
+
+ if len(args.Topics) == 0 {
+ info := "NewFilter: at least one topic must be specified"
+ glog.V(logger.Error).Infof(info)
+ return nil, errors.New(info)
+ }
+
+ if len(args.KeyName) != 0 && len(filter.KeySym) == 0 {
+ info := "NewFilter: key was not found by name: " + args.KeyName
+ glog.V(logger.Error).Infof(info)
+ return nil, errors.New(info)
+ }
+
+ if len(args.To) == 0 && len(filter.KeySym) == 0 {
+ info := "NewFilter: filter must contain either symmetric or asymmetric key"
+ glog.V(logger.Error).Infof(info)
+ return nil, errors.New(info)
+ }
+
+ if len(args.To) != 0 && len(filter.KeySym) != 0 {
+ info := "NewFilter: filter must not contain both symmetric and asymmetric key"
+ glog.V(logger.Error).Infof(info)
+ return nil, errors.New(info)
+ }
+
+ if len(args.To) > 0 {
+ if !validatePublicKey(filter.Dst) {
+ info := "NewFilter: Invalid 'To' address"
+ glog.V(logger.Error).Infof(info)
+ return nil, errors.New(info)
+ }
+ filter.KeyAsym = self.whisper.GetIdentity(filter.Dst)
+ if filter.KeyAsym == nil {
+ info := "NewFilter: non-existent identity provided"
+ glog.V(logger.Error).Infof(info)
+ return nil, errors.New(info)
+ }
+ }
+
+ if len(args.From) > 0 {
+ if !validatePublicKey(filter.Src) {
+ info := "NewFilter: Invalid 'From' address"
+ glog.V(logger.Error).Infof(info)
+ return nil, errors.New(info)
+ }
+ }
+
+ id := self.whisper.Watch(&filter)
+ return rpc.NewHexNumber(id), nil
+}
+
+// UninstallFilter disables and removes an existing filter.
+func (self *PublicWhisperAPI) UninstallFilter(filterId rpc.HexNumber) {
+ self.whisper.Unwatch(filterId.Int())
+}
+
+// GetFilterChanges retrieves all the new messages matched by a filter since the last retrieval.
+func (self *PublicWhisperAPI) GetFilterChanges(filterId rpc.HexNumber) []WhisperMessage {
+ f := self.whisper.filters.Get(filterId.Int())
+ if f != nil {
+ newMail := f.retrieve()
+ return toWhisperMessages(newMail)
+ }
+ return toWhisperMessages(nil)
+}
+
+// GetMessages retrieves all the known messages that match a specific filter.
+func (self *PublicWhisperAPI) GetMessages(filterId rpc.HexNumber) []WhisperMessage {
+ all := self.whisper.Messages(filterId.Int())
+ return toWhisperMessages(all)
+}
+
+// toWhisperMessages converts a Whisper message to a RPC whisper message.
+func toWhisperMessages(messages []*ReceivedMessage) []WhisperMessage {
+ msgs := make([]WhisperMessage, len(messages))
+ for i, msg := range messages {
+ msgs[i] = NewWhisperMessage(msg)
+ }
+ return msgs
+}
+
+// Post injects a message into the whisper network for distribution.
+func (self *PublicWhisperAPI) Post(args PostArgs) error {
+ if self.whisper == nil {
+ return whisperOffLineErr
+ }
+
+ params := MessageParams{
+ TTL: args.TTL,
+ Dst: crypto.ToECDSAPub(args.To),
+ KeySym: self.whisper.GetTopicKey(args.KeyName),
+ Topic: args.Topic,
+ Payload: args.Payload,
+ Padding: args.Padding,
+ WorkTime: args.WorkTime,
+ PoW: args.PoW,
+ }
+
+ if len(args.From) > 0 {
+ pub := crypto.ToECDSAPub(args.From)
+ if !validatePublicKey(pub) {
+ info := "Post: Invalid 'From' address"
+ glog.V(logger.Error).Infof(info)
+ return errors.New(info)
+ }
+ params.Src = self.whisper.GetIdentity(pub)
+ if params.Src == nil {
+ info := "Post: non-existent identity provided"
+ glog.V(logger.Error).Infof(info)
+ return errors.New(info)
+ }
+ }
+
+ filter := self.whisper.filters.Get(args.FilterID)
+ if filter == nil && args.FilterID > -1 {
+ info := fmt.Sprintf("Post: wrong filter id %d", args.FilterID)
+ glog.V(logger.Error).Infof(info)
+ return errors.New(info)
+ }
+
+ if filter != nil {
+ // get the missing fields from the filter
+ if params.KeySym == nil && filter.KeySym != nil {
+ params.KeySym = filter.KeySym
+ }
+ if params.Dst == nil && filter.Dst != nil {
+ params.Dst = filter.Dst
+ }
+ if params.Src == nil && filter.Src != nil {
+ params.Src = filter.KeyAsym
+ }
+ if (params.Topic == TopicType{}) {
+ sz := len(filter.Topics)
+ if sz < 1 {
+ info := fmt.Sprintf("Post: no topics in filter # %d", args.FilterID)
+ glog.V(logger.Error).Infof(info)
+ return errors.New(info)
+ } else if sz == 1 {
+ params.Topic = filter.Topics[0]
+ } else {
+ // choose randomly
+ rnd := mathrand.Intn(sz)
+ params.Topic = filter.Topics[rnd]
+ }
+ }
+ }
+
+ // validate
+ if len(args.KeyName) != 0 && len(params.KeySym) == 0 {
+ info := "Post: key was not found by name: " + args.KeyName
+ glog.V(logger.Error).Infof(info)
+ return errors.New(info)
+ }
+
+ if len(args.To) == 0 && len(args.KeyName) == 0 {
+ info := "Post: message must be encrypted either symmetrically or asymmetrically"
+ glog.V(logger.Error).Infof(info)
+ return errors.New(info)
+ }
+
+ if len(args.To) != 0 && len(args.KeyName) != 0 {
+ info := "Post: ambigous encryption method requested"
+ glog.V(logger.Error).Infof(info)
+ return errors.New(info)
+ }
+
+ if len(args.To) > 0 {
+ if !validatePublicKey(params.Dst) {
+ info := "Post: Invalid 'To' address"
+ glog.V(logger.Error).Infof(info)
+ return errors.New(info)
+ }
+ }
+
+ // encrypt and send
+ message := NewSentMessage(¶ms)
+ envelope, err := message.Wrap(params)
+ if err != nil {
+ glog.V(logger.Error).Infof(err.Error())
+ return err
+ }
+ if len(envelope.Data) > msgMaxLength {
+ info := "Post: message is too big"
+ glog.V(logger.Error).Infof(info)
+ return errors.New(info)
+ }
+ if (envelope.Topic == TopicType{} && envelope.isSymmetric()) {
+ info := "Post: topic is missing for symmetric encryption"
+ glog.V(logger.Error).Infof(info)
+ return errors.New(info)
+ }
+
+ if args.Peer != nil {
+ return self.whisper.SendP2PMessage(&args.Peer, envelope)
+ }
+
+ return self.whisper.Send(envelope)
+}
+
+type PostArgs struct {
+ TTL uint32 `json:"ttl"`
+ From rpc.HexBytes `json:"from"`
+ To rpc.HexBytes `json:"to"`
+ KeyName string `json:"keyname"`
+ Topic TopicType `json:"topic"`
+ Padding rpc.HexBytes `json:"padding"`
+ Payload rpc.HexBytes `json:"payload"`
+ WorkTime uint32 `json:"worktime"`
+ PoW float64 `json:"pow"`
+ FilterID int `json:"filter"`
+ Peer rpc.HexBytes `json:"directP2P"`
+}
+
+func (args *PostArgs) UnmarshalJSON(data []byte) (err error) {
+ var obj struct {
+ TTL uint32 `json:"ttl"`
+ From rpc.HexBytes `json:"from"`
+ To rpc.HexBytes `json:"to"`
+ KeyName string `json:"keyname"`
+ Topic TopicType `json:"topic"`
+ Payload rpc.HexBytes `json:"payload"`
+ Padding rpc.HexBytes `json:"padding"`
+ WorkTime uint32 `json:"worktime"`
+ PoW float64 `json:"pow"`
+ FilterID rpc.HexBytes `json:"filter"`
+ Peer rpc.HexBytes `json:"directP2P"`
+ }
+
+ if err := json.Unmarshal(data, &obj); err != nil {
+ return err
+ }
+
+ args.TTL = obj.TTL
+ args.From = obj.From
+ args.To = obj.To
+ args.KeyName = obj.KeyName
+ args.Topic = obj.Topic
+ args.Payload = obj.Payload
+ args.Padding = obj.Padding
+ args.WorkTime = obj.WorkTime
+ args.PoW = obj.PoW
+ args.FilterID = -1
+ args.Peer = obj.Peer
+
+ if obj.FilterID != nil {
+ x := bytesToIntBigEndian(obj.FilterID)
+ args.FilterID = int(x)
+ }
+
+ return nil
+}
+
+type WhisperFilterArgs struct {
+ To []byte
+ From []byte
+ KeyName string
+ PoW float64
+ Topics []TopicType
+ AcceptP2P bool
+}
+
+// UnmarshalJSON implements the json.Unmarshaler interface, invoked to convert a
+// JSON message blob into a WhisperFilterArgs structure.
+func (args *WhisperFilterArgs) UnmarshalJSON(b []byte) (err error) {
+ // Unmarshal the JSON message and sanity check
+ var obj struct {
+ To rpc.HexBytes `json:"to"`
+ From rpc.HexBytes `json:"from"`
+ KeyName string `json:"keyname"`
+ PoW float64 `json:"pow"`
+ Topics []interface{} `json:"topics"`
+ AcceptP2P bool `json:"acceptP2P"`
+ }
+ if err := json.Unmarshal(b, &obj); err != nil {
+ return err
+ }
+
+ args.To = obj.To
+ args.From = obj.From
+ args.KeyName = obj.KeyName
+ args.PoW = obj.PoW
+ args.AcceptP2P = obj.AcceptP2P
+
+ // Construct the topic array
+ if obj.Topics != nil {
+ topics := make([]string, len(obj.Topics))
+ for i, field := range obj.Topics {
+ switch value := field.(type) {
+ case string:
+ topics[i] = value
+ case nil:
+ return fmt.Errorf("topic[%d] is empty", i)
+ default:
+ return fmt.Errorf("topic[%d] is not a string", i)
+ }
+ }
+ topicsDecoded := make([]TopicType, len(topics))
+ for j, s := range topics {
+ x := common.FromHex(s)
+ if x == nil || len(x) != topicLength {
+ return fmt.Errorf("topic[%d] is invalid", j)
+ }
+ topicsDecoded[j] = BytesToTopic(x)
+ }
+ args.Topics = topicsDecoded
+ }
+
+ return nil
+}
+
+// WhisperMessage is the RPC representation of a whisper message.
+type WhisperMessage struct {
+ Payload string `json:"payload"`
+ Padding string `json:"padding"`
+ From string `json:"from"`
+ To string `json:"to"`
+ Sent uint32 `json:"sent"`
+ TTL uint32 `json:"ttl"`
+ PoW float64 `json:"pow"`
+ Hash string `json:"hash"`
+}
+
+// NewWhisperMessage converts an internal message into an API version.
+func NewWhisperMessage(message *ReceivedMessage) WhisperMessage {
+ return WhisperMessage{
+ Payload: common.ToHex(message.Payload),
+ Padding: common.ToHex(message.Padding),
+ From: common.ToHex(crypto.FromECDSAPub(message.Recover())),
+ To: common.ToHex(crypto.FromECDSAPub(message.Dst)),
+ Sent: message.Sent,
+ TTL: message.TTL,
+ PoW: message.PoW,
+ Hash: common.ToHex(message.EnvelopeHash.Bytes()),
+ }
+}
diff --git a/whisper/whisper05/doc.go b/whisper/whisper05/doc.go
new file mode 100644
index 0000000000..5100a84024
--- /dev/null
+++ b/whisper/whisper05/doc.go
@@ -0,0 +1,87 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library 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.
+//
+// The go-ethereum library 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 the go-ethereum library. If not, see .
+
+/*
+Package whisper implements the Whisper PoC-1.
+
+(https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec)
+
+Whisper combines aspects of both DHTs and datagram messaging systems (e.g. UDP).
+As such it may be likened and compared to both, not dissimilar to the
+matter/energy duality (apologies to physicists for the blatant abuse of a
+fundamental and beautiful natural principle).
+
+Whisper is a pure identity-based messaging system. Whisper provides a low-level
+(non-application-specific) but easily-accessible API without being based upon
+or prejudiced by the low-level hardware attributes and characteristics,
+particularly the notion of singular endpoints.
+*/
+package whisper05
+
+import (
+ "time"
+)
+
+const (
+ EnvelopeVersion = uint64(0)
+ protocolVersion = uint64(5)
+ protocolVersionStr = "5.0"
+ protocolName = "shh"
+
+ statusCode = 0
+ messagesCode = 1
+ p2pCode = 2
+ mailRequestCode = 3
+
+ paddingMask = byte(3)
+ signatureFlag = byte(4)
+
+ topicLength = 4
+ signatureLength = 65
+ aesKeyLength = 32
+ saltLength = 12
+ msgMaxLength = 0xFFFF
+
+ padSizeLimitLower = 128 // it can not be less - we don't want to reveal the absence of signature
+ padSizeLimitUpper = 256 // just an arbitrary number, could be changed without losing compatibility
+
+ expirationCycle = time.Second
+ transmissionCycle = 300 * time.Millisecond
+
+ DefaultTTL = 50 // seconds
+ SynchAllowance = 10 // seconds
+
+ MinimumPoW = 50.0 // todo: review
+)
+
+type whisperOfflineError struct{}
+
+var whisperOffLineErr = new(whisperOfflineError)
+
+func (e *whisperOfflineError) Error() string {
+ return "whisper is offline"
+}
+
+// MailServer represents a mail server, capable of
+// archiving the old messages for subsequent delivery
+// to the peers. Any implementation must ensure that both
+// functions are thread-safe. Also, they must return ASAP.
+// DeliverMail should use directMessagesCode for delivery,
+// in order to bypass the expiry checks.
+type MailServer interface {
+ Archive(env *Envelope)
+ DeliverMail(whisperPeer *WhisperPeer, data []byte)
+}
diff --git a/whisper/whisper05/envelope.go b/whisper/whisper05/envelope.go
new file mode 100644
index 0000000000..be228adf9b
--- /dev/null
+++ b/whisper/whisper05/envelope.go
@@ -0,0 +1,227 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library 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.
+//
+// The go-ethereum library 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 the go-ethereum library. If not, see .
+
+// 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.
+
+package whisper05
+
+import (
+ "crypto/ecdsa"
+ "encoding/binary"
+ "fmt"
+ "math"
+ "time"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/crypto/ecies"
+ "github.com/ethereum/go-ethereum/rlp"
+)
+
+// Envelope represents a clear-text data packet to transmit through the Whisper
+// network. Its contents may or may not be encrypted and signed.
+type Envelope struct {
+ Expiry uint32
+ TTL uint32
+ Topic TopicType
+ Salt []byte
+ AESNonce []byte
+ Version []byte
+ Data []byte
+ EnvNonce uint64
+
+ hash common.Hash // Cached hash of the envelope to avoid rehashing every time
+ pow float64 // Message-specific PoW as described in the Whisper specification
+}
+
+// NewEnvelope wraps a Whisper message with expiration and destination data
+// included into an envelope for network forwarding.
+func NewEnvelope(ttl uint32, topic TopicType, salt []byte, aesNonce []byte, msg *SentMessage) *Envelope {
+ env := Envelope{
+ Expiry: uint32(time.Now().Add(time.Second * time.Duration(ttl)).Unix()),
+ TTL: ttl,
+ Topic: topic,
+ Salt: salt,
+ AESNonce: aesNonce,
+ Data: msg.Raw,
+ Version: make([]byte, 1),
+ EnvNonce: 0,
+ }
+
+ if EnvelopeVersion > 255 {
+ panic("please fix Envelope.Version size before releasing this version")
+ }
+
+ env.Version[0] = byte(EnvelopeVersion)
+ return &env
+}
+
+func (self *Envelope) isSymmetric() bool {
+ return self.AESNonce != nil
+}
+
+func (self *Envelope) isAsymmetric() bool {
+ return !self.isSymmetric()
+}
+
+func (self *Envelope) Ver() uint64 {
+ return bytesToIntLittleEndian(self.Version)
+}
+
+// Seal closes the envelope by spending the requested amount of time as a proof
+// of work on hashing the data.
+func (self *Envelope) Seal(options MessageParams) {
+ var target int
+ if options.PoW == 0 {
+ // adjust for the duration of Seal() execution only if execution time is predefined unconditionally
+ self.Expiry += options.WorkTime
+ } else {
+ target = self.powToFirstBit(options.PoW)
+ }
+
+ buf := make([]byte, 64)
+ h := crypto.Keccak256(self.rlpWithoutNonce())
+ copy(buf[:32], h)
+
+ finish, bestBit := time.Now().Add(time.Duration(options.WorkTime)*time.Second).UnixNano(), 0
+ for nonce := uint64(0); time.Now().UnixNano() < finish; {
+ for i := 0; i < 1024; i++ {
+ binary.BigEndian.PutUint64(buf[56:], nonce)
+ h = crypto.Keccak256(buf)
+ firstBit := common.FirstBitSet(common.BigD(h))
+ if firstBit > bestBit {
+ self.EnvNonce, bestBit = nonce, firstBit
+ if target > 0 && bestBit >= target {
+ return
+ }
+ }
+ nonce++
+ }
+ }
+}
+
+func (self *Envelope) PoW() float64 {
+ if self.pow == 0 {
+ self.calculatePoW(0)
+ }
+ return self.pow
+}
+
+func (self *Envelope) calculatePoW(diff uint32) {
+ h := self.Hash()
+ firstBit := common.FirstBitSet(common.BigD(h.Bytes()))
+ x := math.Pow(2, float64(firstBit))
+ x /= float64(len(self.Data))
+ x /= float64(self.TTL + diff)
+ self.pow = x
+}
+
+func (self *Envelope) powToFirstBit(pow float64) int {
+ x := pow
+ x *= float64(len(self.Data))
+ x *= float64(self.TTL)
+ bits := math.Log2(x)
+ bits = math.Ceil(bits)
+ return int(bits)
+}
+
+// rlpWithoutNonce returns the RLP encoded envelope contents, except the nonce.
+func (self *Envelope) rlpWithoutNonce() []byte {
+ enc, _ := rlp.EncodeToBytes([]interface{}{self.Expiry, self.TTL, self.Topic, self.Salt, self.AESNonce, self.Data})
+ return enc
+}
+
+// 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.Keccak256Hash(enc)
+ }
+ return self.hash
+}
+
+// DecodeRLP decodes an Envelope from an RLP data stream.
+func (self *Envelope) DecodeRLP(s *rlp.Stream) error {
+ raw, err := s.Raw()
+ if err != nil {
+ return err
+ }
+ // 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 (does not implement DecodeRLP function).
+ // Only public members will be encoded.
+ type rlpenv Envelope
+ if err := rlp.DecodeBytes(raw, (*rlpenv)(self)); err != nil {
+ return err
+ }
+ self.hash = crypto.Keccak256Hash(raw)
+ return nil
+}
+
+// OpenAsymmetric tries to decrypt an envelope, potentially encrypted with a particular key.
+func (self *Envelope) OpenAsymmetric(key *ecdsa.PrivateKey) (*ReceivedMessage, error) {
+ message := &ReceivedMessage{Raw: self.Data}
+ err := message.decryptAsymmetric(key)
+ switch err {
+ case nil:
+ return message, nil
+ case ecies.ErrInvalidPublicKey: // addressed to somebody else
+ return nil, err
+ default:
+ return nil, fmt.Errorf("unable to open envelope, decrypt failed: %v", err)
+ }
+}
+
+// OpenSymmetric tries to decrypt an envelope, potentially encrypted with a particular key.
+func (self *Envelope) OpenSymmetric(key []byte) (msg *ReceivedMessage, err error) {
+ msg = &ReceivedMessage{Raw: self.Data}
+ err = msg.decryptSymmetric(key, self.Salt, self.AESNonce)
+ if err != nil {
+ msg = nil
+ }
+ return
+}
+
+// Open tries to decrypt an envelope, and populates the message fields in case of success.
+func (self *Envelope) Open(watcher *Filter) (msg *ReceivedMessage) {
+ if self.isAsymmetric() {
+ msg, _ = self.OpenAsymmetric(watcher.KeyAsym)
+ if msg != nil {
+ msg.Dst = watcher.Dst
+ }
+ } else if self.isSymmetric() {
+ msg, _ = self.OpenSymmetric(watcher.KeySym)
+ if msg != nil {
+ msg.TopicKeyHash = crypto.Keccak256Hash(watcher.KeySym)
+ }
+ }
+
+ if msg != nil {
+ ok := msg.Validate()
+ if !ok {
+ return nil
+ }
+ msg.Topic = self.Topic
+ msg.PoW = self.PoW()
+ msg.TTL = self.TTL
+ msg.Sent = self.Expiry - self.TTL
+ msg.EnvelopeHash = self.hash
+ msg.EnvelopeVersion = self.Ver()
+ }
+ return msg
+}
diff --git a/whisper/whisper05/filter.go b/whisper/whisper05/filter.go
new file mode 100644
index 0000000000..fbc4f422ea
--- /dev/null
+++ b/whisper/whisper05/filter.go
@@ -0,0 +1,195 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library 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.
+//
+// The go-ethereum library 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 the go-ethereum library. If not, see .
+
+package whisper05
+
+import (
+ "crypto/ecdsa"
+
+ "sync"
+
+ "github.com/ethereum/go-ethereum/common"
+)
+
+type Filter struct {
+ Src *ecdsa.PublicKey // Sender of the message
+ Dst *ecdsa.PublicKey // Recipient of the message
+ KeyAsym *ecdsa.PrivateKey // Private Key of recipient
+ Topics []TopicType // Topics to filter messages with
+ KeySym []byte // Key associated with the Topic
+ TopicKeyHash common.Hash // The Keccak256Hash of the symmetric key
+ PoW float64 // Proof of work as described in the Whisper spec
+ acceptP2P bool // Indicates whether this filter is interested in direct peer-to-peer messages
+
+ messages map[common.Hash]*ReceivedMessage
+ mutex sync.RWMutex
+}
+
+type Filters struct {
+ id int
+ watchers map[int]*Filter
+ whisper *Whisper
+ mutex sync.RWMutex
+}
+
+func NewFilters(w *Whisper) *Filters {
+ return &Filters{
+ watchers: make(map[int]*Filter),
+ whisper: w,
+ }
+}
+
+func (self *Filters) Install(watcher *Filter) int {
+ self.mutex.Lock()
+ defer self.mutex.Unlock()
+
+ self.watchers[self.id] = watcher
+ ret := self.id
+ self.id++
+ return ret
+}
+
+func (self *Filters) Uninstall(id int) {
+ self.mutex.Lock()
+ defer self.mutex.Unlock()
+ delete(self.watchers, id)
+}
+
+func (self *Filters) Get(i int) *Filter {
+ self.mutex.RLock()
+ defer self.mutex.RUnlock()
+ return self.watchers[i]
+}
+
+func (self *Filters) NotifyWatchers(env *Envelope, messageCode uint64) {
+ self.mutex.RLock()
+ var msg *ReceivedMessage
+ for _, watcher := range self.watchers {
+ if messageCode == p2pCode && !watcher.acceptP2P {
+ continue
+ }
+
+ match := false
+ if msg != nil {
+ match = watcher.MatchMessage(msg)
+ } else {
+ match = watcher.MatchEnvelope(env)
+ if match {
+ msg = env.Open(watcher)
+ }
+ }
+
+ if match && msg != nil {
+ watcher.Trigger(msg)
+ }
+ }
+ self.mutex.RUnlock() // we need to unlock before calling addDecryptedMessage
+
+ if msg != nil {
+ self.whisper.addDecryptedMessage(msg)
+ }
+}
+
+func (self *Filter) expectsAsymmetricEncryption() bool {
+ return self.KeyAsym != nil
+}
+
+func (self *Filter) expectsSymmetricEncryption() bool {
+ return self.KeySym != nil
+}
+
+func (self *Filter) Trigger(msg *ReceivedMessage) {
+ self.mutex.Lock()
+ defer self.mutex.Unlock()
+
+ if _, exist := self.messages[msg.EnvelopeHash]; !exist {
+ self.messages[msg.EnvelopeHash] = msg
+ }
+}
+
+func (self *Filter) retrieve() (all []*ReceivedMessage) {
+ self.mutex.Lock()
+ defer self.mutex.Unlock()
+
+ all = make([]*ReceivedMessage, 0, len(self.messages))
+ for _, msg := range self.messages {
+ all = append(all, msg)
+ }
+ self.messages = make(map[common.Hash]*ReceivedMessage) // delete old messages
+ return all
+}
+
+func (self *Filter) MatchMessage(msg *ReceivedMessage) bool {
+ if self.PoW > 0 && msg.PoW < self.PoW {
+ return false
+ }
+
+ if self.Src != nil && !isEqual(msg.Src, self.Src) {
+ return false
+ }
+
+ if self.expectsAsymmetricEncryption() && msg.isAsymmetricEncryption() {
+ // if Dst match, ignore the topic
+ return isEqual(self.Dst, msg.Dst)
+ } else if self.expectsSymmetricEncryption() && msg.isSymmetricEncryption() {
+ // check if that both the key and the topic match
+ if self.TopicKeyHash == msg.TopicKeyHash {
+ for _, t := range self.Topics {
+ if t == msg.Topic {
+ return true
+ }
+ }
+ return false
+ }
+ }
+ return false
+}
+
+func (self *Filter) MatchEnvelope(envelope *Envelope) bool {
+ if self.PoW > 0 && envelope.pow < self.PoW {
+ return false
+ }
+
+ encryptionMethodMatch := false
+ if self.expectsAsymmetricEncryption() && envelope.isAsymmetric() {
+ encryptionMethodMatch = true
+ if self.Topics == nil {
+ return true // wildcard
+ }
+ } else if self.expectsSymmetricEncryption() && envelope.isSymmetric() {
+ encryptionMethodMatch = true
+ }
+
+ if encryptionMethodMatch {
+ for _, t := range self.Topics {
+ if t == envelope.Topic {
+ return true
+ }
+ }
+ }
+
+ return false
+}
+
+func isEqual(a, b *ecdsa.PublicKey) bool {
+ if !validatePublicKey(a) {
+ return false
+ } else if !validatePublicKey(b) {
+ return false
+ }
+ // the Curve is always the same, just compare the points
+ return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
+}
diff --git a/whisper/whisper05/message.go b/whisper/whisper05/message.go
new file mode 100644
index 0000000000..cae3a3b9ed
--- /dev/null
+++ b/whisper/whisper05/message.go
@@ -0,0 +1,371 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library 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.
+//
+// The go-ethereum library 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 the go-ethereum library. If not, see .
+
+// 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.
+// todo: fix the spec link, and move it to doc.go
+
+package whisper05
+
+import (
+ "errors"
+ "fmt"
+
+ "crypto/aes"
+ "crypto/cipher"
+ "crypto/ecdsa"
+ crand "crypto/rand"
+ "crypto/sha256"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/logger/glog"
+
+ "golang.org/x/crypto/pbkdf2"
+)
+
+// Options specifies the exact way a message should be wrapped into an Envelope.
+type MessageParams struct {
+ TTL uint32
+ Src *ecdsa.PrivateKey
+ Dst *ecdsa.PublicKey
+ KeySym []byte
+ Topic TopicType
+ WorkTime uint32
+ PoW float64
+ Payload []byte
+ Padding []byte
+}
+
+// SentMessage represents an end-user data packet to transmit through the
+// Whisper protocol. These are wrapped into Envelopes that need not be
+// understood by intermediate nodes, just forwarded.
+type SentMessage struct {
+ Raw []byte
+}
+
+// ReceivedMessage represents a data packet to be received through the
+// Whisper protocol.
+type ReceivedMessage struct {
+ Raw []byte
+
+ Payload []byte
+ Padding []byte
+ Signature []byte
+
+ PoW float64 // Proof of work as described in the Whisper spec
+ Sent uint32 // Time when the message was posted into the network
+ TTL uint32 // Maximum time to live allowed for the message
+ Src *ecdsa.PublicKey // Message recipient (identity used to decode the message)
+ Dst *ecdsa.PublicKey // Message recipient (identity used to decode the message)
+ Topic TopicType
+
+ TopicKeyHash common.Hash // The Keccak256Hash of the key, associated with the Topic
+ EnvelopeHash common.Hash // Message envelope hash to act as a unique id
+ EnvelopeVersion uint64
+}
+
+func isMessageSigned(flags byte) bool {
+ return (flags & signatureFlag) != 0
+}
+
+func isMessagePadded(flags byte) bool {
+ return (flags & paddingMask) != 0
+}
+
+func (self *ReceivedMessage) isSymmetricEncryption() bool {
+ return self.TopicKeyHash != common.Hash{}
+}
+
+func (self *ReceivedMessage) isAsymmetricEncryption() bool {
+ return self.Dst != nil
+}
+
+func DeriveOneTimeKey(key []byte, salt []byte, version uint64) (derivedKey []byte, err error) {
+ if version == 0 {
+ derivedKey = pbkdf2.Key(key, salt, 16, aesKeyLength, sha256.New)
+ } else {
+ err = fmt.Errorf("DeriveKey: invalid envelope version: %d", version)
+ }
+ return
+}
+
+// NewMessage creates and initializes a non-signed, non-encrypted Whisper message.
+func NewSentMessage(params *MessageParams) *SentMessage {
+ // Construct an initial flag set: no signature, no padding, other bits random
+ buf := make([]byte, 1)
+ crand.Read(buf)
+ flags := buf[0]
+ flags &= ^paddingMask
+ flags &= ^signatureFlag
+
+ msg := SentMessage{}
+ msg.Raw = make([]byte, 1, len(params.Payload)+len(params.Payload)+signatureLength+padSizeLimitUpper)
+ msg.Raw[0] = flags
+ msg.appendPadding(params)
+ msg.Raw = append(msg.Raw, params.Payload...)
+ return &msg
+}
+
+// appendPadding appends the pseudorandom padding bytes and sets the padding flag.
+// The last byte contains the size of padding (thus, its size must not exceed 256).
+func (self *SentMessage) appendPadding(params *MessageParams) {
+ total := len(params.Payload) + 1
+ if params.Src != nil {
+ total += signatureLength
+ }
+ padChunk := padSizeLimitUpper
+ if total <= padSizeLimitLower {
+ padChunk = padSizeLimitLower
+ }
+ odd := total % padChunk
+ if odd > 0 {
+ padSize := padChunk - odd
+ if padSize > 255 {
+ // this algorithm is only valid if padSizeLimitUpper <= 256.
+ // if padSizeLimitUpper will every change, please fix the algorithm
+ // (for more information see ReceivedMessage.extractPadding() function).
+ panic("please fix the padding algorithm before releasing new version")
+ }
+ buf := make([]byte, padSize)
+ crand.Read(buf[1:])
+ buf[0] = byte(padSize)
+ if params.Padding != nil {
+ copy(buf[1:], params.Padding)
+ }
+ self.Raw = append(self.Raw, buf...)
+ self.Raw[0] |= byte(0x1) // number of bytes indicating the padding size
+ }
+}
+
+// sign calculates and sets the cryptographic signature for the message,
+// also setting the sign flag.
+func (self *SentMessage) sign(key *ecdsa.PrivateKey) (err error) {
+ if isMessageSigned(self.Raw[0]) {
+ // this should not happen, but no reason to panic
+ glog.V(logger.Error).Infof("Trying to sign a message which was already signed")
+ return
+ }
+ hash := crypto.Keccak256(self.Raw)
+ signature, err := crypto.Sign(hash, key)
+ if err != nil {
+ self.Raw = append(self.Raw, signature...)
+ self.Raw[0] |= signatureFlag
+ }
+ return
+}
+
+// encryptAsymmetric encrypts a message with a public key.
+func (self *SentMessage) encryptAsymmetric(key *ecdsa.PublicKey) error {
+ if !validatePublicKey(key) {
+ return fmt.Errorf("Invalid public key provided for asymmetric encryption")
+ }
+ encrypted, err := crypto.Encrypt(key, self.Raw)
+ if err == nil {
+ self.Raw = encrypted
+ }
+ return err
+}
+
+// encryptSymmetric encrypts a message with a topic key, using AES-GCM-256.
+// nonce size should be 12 bytes (see cipher.gcmStandardNonceSize).
+func (self *SentMessage) encryptSymmetric(key []byte) (salt []byte, nonce []byte, err error) {
+ if !validateSymmetricKey(key) {
+ err = fmt.Errorf("encryptSymmetric: invalid key provided for symmetric encryption")
+ return
+ }
+
+ salt = make([]byte, saltLength)
+ _, err = crand.Read(salt)
+ if err != nil {
+ return
+ } else if !validateSymmetricKey(salt) {
+ err = fmt.Errorf("encryptSymmetric: failed to generate salt")
+ return
+ }
+
+ derivedKey, err := DeriveOneTimeKey(key, salt, EnvelopeVersion)
+ if err != nil {
+ return
+ }
+ if !validateSymmetricKey(derivedKey) {
+ err = fmt.Errorf("encryptSymmetric: invalid key derived")
+ return
+ }
+ block, err := aes.NewCipher(derivedKey)
+ if err != nil {
+ return
+ }
+ aesgcm, err := cipher.NewGCM(block)
+ if err != nil {
+ return
+ }
+
+ // never use more than 2^32 random nonces with a given key
+ nonce = make([]byte, aesgcm.NonceSize())
+ _, err = crand.Read(nonce)
+ if err != nil {
+ return
+ }
+ self.Raw = aesgcm.Seal(nil, nonce, self.Raw, nil)
+ return
+}
+
+// Wrap bundles the message into an Envelope to transmit over the network.
+//
+// pow (Proof Of Work) controls how much time to spend on hashing the message,
+// 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 *SentMessage) Wrap(options MessageParams) (envelope *Envelope, err error) {
+ if options.TTL == 0 {
+ options.TTL = DefaultTTL
+ }
+ if options.Src != nil {
+ if err = self.sign(options.Src); err != nil {
+ return
+ }
+ }
+ if len(self.Raw) > msgMaxLength {
+ glog.V(logger.Error).Infof("Message size must not exceed %d bytes", msgMaxLength)
+ err = errors.New("Oversized message")
+ return
+ }
+ var salt, nonce []byte
+ if options.Dst != nil {
+ err = self.encryptAsymmetric(options.Dst)
+ } else if options.KeySym != nil {
+ salt, nonce, err = self.encryptSymmetric(options.KeySym)
+ } else {
+ err = errors.New("Unable to encrypt the message: neither Dst nor Key")
+ }
+
+ if err == nil {
+ envelope = NewEnvelope(options.TTL, options.Topic, salt, nonce, self)
+ envelope.Seal(options)
+ }
+ return
+}
+
+// decryptSymmetric decrypts a message with a topic key, using AES-GCM-256.
+// nonce size should be 12 bytes (see cipher.gcmStandardNonceSize).
+func (self *ReceivedMessage) decryptSymmetric(key []byte, salt []byte, nonce []byte) error {
+ derivedKey, err := DeriveOneTimeKey(key, salt, self.EnvelopeVersion)
+ if err != nil {
+ return err
+ }
+
+ block, err := aes.NewCipher(derivedKey)
+ if err != nil {
+ return err
+ }
+ aesgcm, err := cipher.NewGCM(block)
+ if err != nil {
+ return err
+ }
+ if len(nonce) != aesgcm.NonceSize() {
+ glog.V(logger.Error).Infof("AES nonce size must be %d bytes", aesgcm.NonceSize())
+ return errors.New("Wrong AES nonce size")
+ }
+ decrypted, err := aesgcm.Open(nil, nonce, self.Raw, nil)
+ if err != nil {
+ return err
+ }
+ self.Raw = decrypted
+ return nil
+}
+
+// decryptAsymmetric decrypts an encrypted payload with a private key.
+func (self *ReceivedMessage) decryptAsymmetric(key *ecdsa.PrivateKey) error {
+ decrypted, err := crypto.Decrypt(key, self.Raw)
+ if err == nil {
+ self.Raw = decrypted
+ }
+ return err
+}
+
+// Validate checks the validity and extracts the fields in case of success
+func (self *ReceivedMessage) Validate() bool {
+ end := len(self.Raw)
+ if end < 1 {
+ return false
+ }
+
+ if isMessageSigned(self.Raw[0]) {
+ end -= signatureLength
+ if end <= 1 {
+ return false
+ }
+ self.Signature = self.Raw[end:]
+ self.Src = self.Recover()
+ if self.Src == nil {
+ return false
+ }
+ }
+
+ padSize, ok := self.extractPadding(end)
+ if !ok {
+ return false
+ }
+
+ self.Payload = self.Raw[1+padSize : end]
+ return self.isSymmetricEncryption() != self.isAsymmetricEncryption()
+}
+
+// extractPadding extracts the padding from raw message.
+// although we don't support sending messages with padding size
+// exceeding 255 bytes, such messages are perfectly valid, and
+// can be successfully decrypted.
+func (self *ReceivedMessage) extractPadding(end int) (int, bool) {
+ paddingSize := 0
+ sz := int(self.Raw[0] & paddingMask) // number of bytes containing the entire size of padding, could be zero
+ if sz != 0 {
+ paddingSize = int(bytesToIntLittleEndian(self.Raw[1 : 1+sz]))
+ if paddingSize < sz || paddingSize+1 > end {
+ return 0, false
+ }
+ self.Padding = self.Raw[1+sz : 1+paddingSize]
+ }
+ return paddingSize, true
+}
+
+// Recover retrieves the public key of the message signer.
+func (self *ReceivedMessage) Recover() *ecdsa.PublicKey {
+ defer func() { recover() }() // in case of invalid signature
+
+ pub, err := crypto.SigToPub(self.hash(), self.Signature)
+ if err != nil {
+ glog.V(logger.Error).Infof("Could not get public key from signature: %v", err)
+ return nil
+ }
+ return pub
+}
+
+// hash calculates the SHA3 checksum of the message flags, payload and padding.
+func (self *ReceivedMessage) hash() []byte {
+ if isMessageSigned(self.Raw[0]) {
+ sz := len(self.Raw) - signatureLength
+ return crypto.Keccak256(self.Raw[:sz])
+ }
+ return crypto.Keccak256(self.Raw)
+}
diff --git a/whisper/whisper05/peer.go b/whisper/whisper05/peer.go
new file mode 100644
index 0000000000..c8e1c5343f
--- /dev/null
+++ b/whisper/whisper05/peer.go
@@ -0,0 +1,177 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library 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.
+//
+// The go-ethereum library 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 the go-ethereum library. If not, see .
+
+package whisper05
+
+import (
+ "fmt"
+ "time"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/logger/glog"
+ "github.com/ethereum/go-ethereum/p2p"
+ "github.com/ethereum/go-ethereum/rlp"
+ set "gopkg.in/fatih/set.v0"
+)
+
+// peer represents a whisper protocol peer connection.
+type WhisperPeer struct {
+ host *Whisper
+ peer *p2p.Peer
+ ws p2p.MsgReadWriter
+ trusted bool
+
+ known *set.Set // Messages already known by the peer to avoid wasting bandwidth
+
+ quit chan struct{}
+}
+
+// newPeer creates a new whisper peer object, but does not run the handshake itself.
+func newPeer(host *Whisper, remote *p2p.Peer, rw p2p.MsgReadWriter) *WhisperPeer {
+ return &WhisperPeer{
+ host: host,
+ peer: remote,
+ ws: rw,
+ trusted: false,
+ known: set.New(),
+ quit: make(chan struct{}),
+ }
+}
+
+// start initiates the peer updater, periodically broadcasting the whisper packets
+// into the network.
+func (self *WhisperPeer) start() {
+ go self.update()
+ glog.V(logger.Debug).Infof("%v: whisper started", self.peer)
+}
+
+// stop terminates the peer updater, stopping message forwarding to it.
+func (self *WhisperPeer) stop() {
+ close(self.quit)
+ glog.V(logger.Debug).Infof("%v: whisper stopped", self.peer)
+}
+
+// handshake sends the protocol initiation status message to the remote peer and
+// verifies the remote status too.
+func (self *WhisperPeer) handshake() error {
+ // Send the handshake status message asynchronously
+ errc := make(chan error, 1)
+ go func() {
+ errc <- p2p.Send(self.ws, statusCode, protocolVersion)
+ }()
+ // Fetch the remote status packet and verify protocol match
+ packet, err := self.ws.ReadMsg()
+ if err != nil {
+ return err
+ }
+ if packet.Code != statusCode {
+ return fmt.Errorf("peer sent %x before status packet", packet.Code)
+ }
+ s := rlp.NewStream(packet.Payload, uint64(packet.Size))
+ if _, err := s.List(); err != nil {
+ return fmt.Errorf("bad status message: %v", err)
+ }
+ peerVersion, err := s.Uint()
+ if err != nil {
+ return fmt.Errorf("bad status message: %v", err)
+ }
+ if peerVersion != protocolVersion {
+ return fmt.Errorf("protocol version mismatch %d != %d", peerVersion, protocolVersion)
+ }
+ // Wait until out own status is consumed too
+ if err := <-errc; err != nil {
+ return fmt.Errorf("failed to send status packet: %v", err)
+ }
+ return nil
+}
+
+// update executes periodic operations on the peer, including message transmission
+// and expiration.
+func (self *WhisperPeer) update() {
+ // Start the tickers for the updates
+ expire := time.NewTicker(expirationCycle)
+ transmit := time.NewTicker(transmissionCycle)
+
+ // Loop and transmit until termination is requested
+ for {
+ select {
+ case <-expire.C:
+ self.expire()
+
+ case <-transmit.C:
+ if err := self.broadcast(); err != nil {
+ glog.V(logger.Info).Infof("%v: broadcast failed: %v", self.peer, err)
+ return
+ }
+
+ case <-self.quit:
+ return
+ }
+ }
+}
+
+// mark marks an envelope known to the peer so that it won't be sent back.
+func (self *WhisperPeer) mark(envelope *Envelope) {
+ self.known.Add(envelope.Hash())
+}
+
+// marked checks if an envelope is already known to the remote peer.
+func (self *WhisperPeer) marked(envelope *Envelope) bool {
+ return self.known.Has(envelope.Hash())
+}
+
+// expire iterates over all the known envelopes in the host and removes all
+// expired (unknown) ones from the known list.
+func (self *WhisperPeer) expire() {
+ // Assemble the list of available envelopes
+ available := set.NewNonTS()
+ for _, envelope := range self.host.Envelopes() {
+ available.Add(envelope.Hash())
+ }
+ // Cross reference availability with known status
+ unmark := make(map[common.Hash]struct{})
+ self.known.Each(func(v interface{}) bool {
+ if !available.Has(v.(common.Hash)) {
+ unmark[v.(common.Hash)] = struct{}{}
+ }
+ return true
+ })
+ // Dump all known but unavailable
+ for hash, _ := range unmark {
+ self.known.Remove(hash)
+ }
+}
+
+// broadcast iterates over the collection of envelopes and transmits yet unknown
+// ones over the network.
+func (self *WhisperPeer) broadcast() error {
+ // Fetch the envelopes and collect the unknown ones
+ envelopes := self.host.Envelopes()
+ transmit := make([]*Envelope, 0, len(envelopes))
+ for _, envelope := range envelopes {
+ if !self.marked(envelope) {
+ transmit = append(transmit, envelope)
+ self.mark(envelope)
+ }
+ }
+ // Transmit the unknown batch (potentially empty)
+ if err := p2p.Send(self.ws, messagesCode, transmit); err != nil {
+ return err
+ }
+ glog.V(logger.Detail).Infoln(self.peer, "broadcasted", len(transmit), "message(s)")
+ return nil
+}
diff --git a/whisper/whisper05/topic.go b/whisper/whisper05/topic.go
new file mode 100644
index 0000000000..721da6a5a7
--- /dev/null
+++ b/whisper/whisper05/topic.go
@@ -0,0 +1,77 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library 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.
+//
+// The go-ethereum library 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 the go-ethereum library. If not, see .
+
+// Contains the Whisper protocol Topic element. For formal details please see
+// the specs at https://github.com/ethereum/wiki/wiki/Whisper-PoC-1-Protocol-Spec#topics.
+
+package whisper05
+
+import (
+ "fmt"
+ "strings"
+
+ "github.com/ethereum/go-ethereum/common"
+)
+
+// Topic represents a cryptographically secure, probabilistic partial
+// classifications of a message, determined as the first (left) 4 bytes of the
+// SHA3 hash of some arbitrary data given by the original author of the message.
+type TopicType [topicLength]byte
+
+func BytesToTopic(b []byte) (t TopicType) {
+ sz := topicLength
+ if x := len(b); x < topicLength {
+ sz = x
+ }
+ for i := 0; i < sz; i++ {
+ t[i] = b[i]
+ }
+ return t
+}
+
+func HashToTopic(h common.Hash) (t TopicType) {
+ for i := 0; i < topicLength; i++ {
+ t[i] = h[i]
+ }
+ return t
+}
+
+// String converts a topic byte array to a string representation.
+func (self *TopicType) String() string {
+ return string(common.ToHex(self[:]))
+}
+
+// UnmarshalJSON parses a hex representation to a topic.
+func (t *TopicType) UnmarshalJSON(input []byte) error {
+ length := len(input)
+ if length >= 2 && input[0] == '"' && input[length-1] == '"' {
+ input = input[1 : length-1]
+ }
+ // strip "0x" for length check
+ if len(input) > 1 && strings.ToLower(string(input[:2])) == "0x" {
+ input = input[2:]
+ }
+ // validate the length of the input
+ if len(input) != topicLength*2 {
+ return fmt.Errorf("whisper: unmarshalJSON failed: topic must be exactly %d bytes", topicLength)
+ }
+ b := common.FromHex(string(input))
+ if b == nil {
+ return fmt.Errorf("whisper: unmarshalJSON failed: wrong topic format")
+ }
+ *t = BytesToTopic(b)
+ return nil
+}
diff --git a/whisper/whisper05/whisper.go b/whisper/whisper05/whisper.go
new file mode 100644
index 0000000000..7835e9884f
--- /dev/null
+++ b/whisper/whisper05/whisper.go
@@ -0,0 +1,583 @@
+// Copyright 2016 The go-ethereum Authors
+// This file is part of the go-ethereum library.
+//
+// The go-ethereum library 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.
+//
+// The go-ethereum library 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 the go-ethereum library. If not, see .
+
+package whisper05
+
+import (
+ "bytes"
+ "crypto/ecdsa"
+ crand "crypto/rand"
+ "crypto/sha256"
+ "fmt"
+ "sync"
+ "time"
+
+ "github.com/ethereum/go-ethereum/common"
+ "github.com/ethereum/go-ethereum/crypto"
+ "github.com/ethereum/go-ethereum/logger"
+ "github.com/ethereum/go-ethereum/logger/glog"
+ "github.com/ethereum/go-ethereum/p2p"
+ "github.com/ethereum/go-ethereum/rpc"
+
+ "golang.org/x/crypto/pbkdf2"
+
+ "github.com/ethereum/go-ethereum/rlp"
+ set "gopkg.in/fatih/set.v0"
+)
+
+// Whisper represents a dark communication interface through the Ethereum
+// network, using its very own P2P communication layer.
+type Whisper struct {
+ protocol p2p.Protocol
+ filters *Filters
+
+ privateKeys map[string]*ecdsa.PrivateKey
+ topicKeys map[string][]byte
+ keyMu sync.RWMutex
+
+ envelopes map[common.Hash]*Envelope // Pool of messages currently tracked by this node
+ messages map[common.Hash]*ReceivedMessage // Pool of successfully decrypted messages, not expired yet
+ expirations map[uint32]*set.SetNonTS // Message expiration pool
+ poolMu sync.RWMutex // Mutex to sync the message and expiration pools
+
+ peers map[*WhisperPeer]struct{} // Set of currently active peers
+ peerMu sync.RWMutex // Mutex to sync the active peer set
+
+ mailServer *MailServer
+
+ quit chan struct{}
+}
+
+// New creates a Whisper client ready to communicate through the Ethereum P2P network.
+// Param s should be passed if you want to implement mail server, otherwise nil.
+func New(s *MailServer) *Whisper {
+ whisper := &Whisper{
+ privateKeys: make(map[string]*ecdsa.PrivateKey),
+ topicKeys: make(map[string][]byte),
+ envelopes: make(map[common.Hash]*Envelope),
+ messages: make(map[common.Hash]*ReceivedMessage),
+ expirations: make(map[uint32]*set.SetNonTS),
+ peers: make(map[*WhisperPeer]struct{}),
+ mailServer: s,
+ quit: make(chan struct{}),
+ }
+ whisper.filters = NewFilters(whisper)
+
+ // p2p whisper sub protocol handler
+ whisper.protocol = p2p.Protocol{
+ Name: protocolName,
+ Version: uint(protocolVersion),
+ Length: 2,
+ Run: whisper.handlePeer,
+ }
+
+ return whisper
+}
+
+// APIs returns the RPC descriptors the Whisper implementation offers
+func (s *Whisper) APIs() []rpc.API {
+ return []rpc.API{
+ {
+ Namespace: protocolName,
+ Version: protocolVersionStr,
+ Service: NewPublicWhisperAPI(s),
+ Public: true,
+ },
+ }
+}
+
+// Protocols returns the whisper sub-protocols ran by this particular client.
+func (self *Whisper) Protocols() []p2p.Protocol {
+ return []p2p.Protocol{self.protocol}
+}
+
+// Version returns the whisper sub-protocols version number.
+func (self *Whisper) Version() uint {
+ return self.protocol.Version
+}
+
+// MarkPeerTrusted marks specific peer trusted, which will allow it
+// to send historic (expired) messages.
+func (self *Whisper) MarkPeerTrusted(peerID *rpc.HexBytes) error {
+ self.peerMu.Lock()
+ defer self.peerMu.Unlock()
+ for p, _ := range self.peers {
+ id := p.peer.ID()
+ if bytes.Equal(*peerID, id[:]) {
+ p.trusted = true
+ return nil
+ }
+ }
+ return fmt.Errorf("Could not find peer with ID: %x", peerID)
+}
+
+func (self *Whisper) RequestHistoricMessages(peerID *rpc.HexBytes, data *rpc.HexBytes) error {
+ var wp *WhisperPeer
+ self.peerMu.Lock()
+ for p, _ := range self.peers {
+ id := p.peer.ID()
+ if bytes.Equal(*peerID, id[:]) {
+ p.trusted = true
+ wp = p
+ break
+ }
+ }
+ self.peerMu.Unlock()
+
+ if wp == nil {
+ return fmt.Errorf("RequestHistoricMessages: Could not find peer with ID: %x", peerID)
+ }
+
+ return p2p.Send(wp.ws, mailRequestCode, data)
+}
+
+func (self *Whisper) SendP2PMessage(peerID *rpc.HexBytes, envelope *Envelope) error {
+ var wp *WhisperPeer
+ self.peerMu.Lock()
+ for p, _ := range self.peers {
+ id := p.peer.ID()
+ if bytes.Equal(*peerID, id[:]) {
+ p.trusted = true
+ wp = p
+ break
+ }
+ }
+ self.peerMu.Unlock()
+
+ if wp == nil {
+ return fmt.Errorf("SendP2PMessage: Could not find peer with ID: %x", peerID)
+ }
+
+ return p2p.Send(wp.ws, p2pCode, envelope)
+}
+
+// NewIdentity generates a new cryptographic identity for the client, and injects
+// it into the known identities for message decryption.
+func (self *Whisper) NewIdentity() *ecdsa.PrivateKey {
+ key, err := crypto.GenerateKey()
+ if err != nil || !validatePrivateKey(key) {
+ key, err = crypto.GenerateKey() // retry once
+ }
+ if err != nil {
+ panic(err)
+ }
+ if !validatePrivateKey(key) {
+ panic("Failed to generate valid key")
+ }
+ self.keyMu.Lock()
+ defer self.keyMu.Unlock()
+ self.privateKeys[string(crypto.FromECDSAPub(&key.PublicKey))] = key
+ return key
+}
+
+// DeleteIdentity deletes the specifies key if it exists.
+func (self *Whisper) DeleteIdentity(key string) {
+ self.keyMu.Lock()
+ defer self.keyMu.Unlock()
+ delete(self.privateKeys, key)
+}
+
+// HasIdentity checks if the the whisper node is configured with the private key
+// of the specified public pair.
+func (self *Whisper) HasIdentity(key *ecdsa.PublicKey) bool {
+ self.keyMu.RLock()
+ defer self.keyMu.RUnlock()
+ return self.privateKeys[string(crypto.FromECDSAPub(key))] != nil
+}
+
+// GetIdentity retrieves the private key of the specified public identity.
+func (self *Whisper) GetIdentity(key *ecdsa.PublicKey) *ecdsa.PrivateKey {
+ self.keyMu.RLock()
+ defer self.keyMu.RUnlock()
+ return self.privateKeys[string(crypto.FromECDSAPub(key))]
+}
+
+func (self *Whisper) GenerateTopicKey(name string) error {
+ if self.HasTopicKey(name) {
+ return fmt.Errorf("GenerateTopicKey: key with name [%s] already exists", name)
+ }
+
+ key := make([]byte, aesKeyLength)
+ _, err := crand.Read(key) // todo: check how safe is this function
+ if err != nil {
+ return err
+ } else if !validateSymmetricKey(key) {
+ return fmt.Errorf("GenerateTopicKey: failed to generate valid key")
+ }
+
+ self.keyMu.Lock()
+ defer self.keyMu.Unlock()
+ self.topicKeys[name] = key
+ return nil
+}
+
+func (self *Whisper) AddTopicKey(name string, key []byte) error {
+ if self.HasTopicKey(name) {
+ return fmt.Errorf("AddTopicKey: key with name [%s] already exists", name)
+ }
+
+ derived, err := DeriveKeyMaterial(key, EnvelopeVersion)
+ if err != nil {
+ return err
+ }
+
+ self.keyMu.Lock()
+ defer self.keyMu.Unlock()
+ self.topicKeys[name] = derived
+ return nil
+}
+
+func (self *Whisper) HasTopicKey(name string) bool {
+ self.keyMu.RLock()
+ defer self.keyMu.RUnlock()
+ return self.topicKeys[name] != nil
+}
+
+func (self *Whisper) DeleteTopicKey(name string) {
+ self.keyMu.Lock()
+ defer self.keyMu.Unlock()
+ delete(self.topicKeys, name)
+}
+
+func (self *Whisper) GetTopicKey(name string) []byte {
+ self.keyMu.RLock()
+ defer self.keyMu.RUnlock()
+ return self.topicKeys[name]
+}
+
+// Watch installs a new message handler to run in case a matching packet arrives
+// from the whisper network.
+func (self *Whisper) Watch(f *Filter) int {
+ return self.filters.Install(f)
+}
+
+// Unwatch removes an installed message handler.
+func (self *Whisper) Unwatch(id int) {
+ self.filters.Uninstall(id)
+}
+
+// Send injects a message into the whisper send queue, to be distributed in the
+// network in the coming cycles.
+func (self *Whisper) Send(envelope *Envelope) error {
+ return self.add(envelope)
+}
+
+// Start implements node.Service, starting the background data propagation thread
+// of the Whisper protocol.
+func (self *Whisper) Start(*p2p.Server) error {
+ glog.V(logger.Info).Infoln("Whisper started")
+ go self.update()
+ return nil
+}
+
+// Stop implements node.Service, stopping the background data propagation thread
+// of the Whisper protocol.
+func (self *Whisper) Stop() error {
+ close(self.quit)
+ glog.V(logger.Info).Infoln("Whisper stopped")
+ return nil
+}
+
+// handlePeer is called by the underlying P2P layer when the whisper sub-protocol
+// connection is negotiated.
+func (self *Whisper) handlePeer(peer *p2p.Peer, rw p2p.MsgReadWriter) error {
+ // Create the new peer and start tracking it
+ whisperPeer := newPeer(self, peer, rw)
+
+ self.peerMu.Lock()
+ self.peers[whisperPeer] = struct{}{}
+ self.peerMu.Unlock()
+
+ defer func() {
+ self.peerMu.Lock()
+ delete(self.peers, whisperPeer)
+ self.peerMu.Unlock()
+ }()
+
+ // Run the peer handshake and state updates
+ if err := whisperPeer.handshake(); err != nil {
+ return err
+ }
+ whisperPeer.start()
+ defer whisperPeer.stop()
+
+ return self.runMessageLoop(whisperPeer, rw)
+}
+
+// runMessageLoop reads and processes inbound messages directly to merge into client-global state.
+func (self *Whisper) runMessageLoop(p *WhisperPeer, rw p2p.MsgReadWriter) error {
+ for {
+ // fetch the next packet
+ packet, err := rw.ReadMsg()
+ if err != nil {
+ return err
+ }
+
+ switch packet.Code {
+ case statusCode:
+ // this should not happen, but no need to panic; just ignore this message.
+ glog.V(logger.Warn).Infof("%v: unxepected status message received", p.peer)
+ case messagesCode:
+ // decode the contained envelopes
+ var envelopes []*Envelope
+ if err := packet.Decode(&envelopes); err != nil {
+ glog.V(logger.Warn).Infof("%v: failed to decode envelope: [%v], peer will be disconnected", p.peer, err)
+ return fmt.Errorf("garbage received")
+ }
+ // inject all envelopes into the internal pool
+ for _, envelope := range envelopes {
+ if err := self.add(envelope); err != nil {
+ glog.V(logger.Warn).Infof("%v: bad envelope received: [%v], peer will be disconnected", p.peer, err)
+ return fmt.Errorf("invalid envelope")
+ }
+ p.mark(envelope)
+ if self.mailServer != nil {
+ (*self.mailServer).Archive(envelope)
+ }
+ }
+ case p2pCode:
+ // peer-to-peer message, sent directly to peer bypassing PoW checks, etc.
+ // this message is not supposed to be forwarded to other peers, and
+ // therefore might not satisfy the PoW, expiry and other requirements.
+ // these messages are only accepted from the trusted peer.
+ if p.trusted {
+ var envelopes []*Envelope
+ if err := packet.Decode(&envelopes); err != nil {
+ glog.V(logger.Warn).Infof("%v: failed to decode direct message: [%v], peer will be disconnected", p.peer, err)
+ return fmt.Errorf("garbage received (directMessage)")
+ }
+ for _, envelope := range envelopes {
+ self.postEvent(envelope, p2pCode)
+ }
+ }
+ case mailRequestCode:
+ // Must be processed if mail server is implemented. Otherwise ignore.
+ if self.mailServer != nil {
+ s := rlp.NewStream(packet.Payload, uint64(packet.Size))
+ data, err := s.Bytes()
+ if err == nil {
+ (*self.mailServer).DeliverMail(p, data)
+ } else {
+ glog.V(logger.Error).Infof("%v: bad requestHistoricMessages received: [%v]", p.peer, err)
+ }
+ }
+ default:
+ // New message types might be implemented in the future versions of Whisper.
+ // For forward compatibility, just ignore.
+ }
+ }
+}
+
+// add inserts a new envelope into the message pool to be distributed within the
+// whisper network. It also inserts the envelope into the expiration pool at the
+// appropriate time-stamp. In case of error, connection should be dropped.
+func (self *Whisper) add(envelope *Envelope) error {
+ now := uint32(time.Now().Unix())
+ sent := envelope.Expiry - envelope.TTL
+
+ if sent > now {
+ if sent+SynchAllowance > now {
+ return fmt.Errorf("message created in the future")
+ } else {
+ // recalculate PoW, adjusted for the time difference, plus one second for latency
+ envelope.calculatePoW(sent - now + 1)
+ }
+ }
+
+ if envelope.Expiry < now {
+ if envelope.Expiry+SynchAllowance*2 < now {
+ return fmt.Errorf("very old message")
+ } else {
+ return nil // drop envelope without error
+ }
+ }
+
+ if len(envelope.Data) > msgMaxLength {
+ return fmt.Errorf("huge messages are not allowed")
+ }
+
+ if envelope.PoW() < MinimumPoW {
+ glog.V(logger.Debug).Infof("envelope with low PoW dropped: %f", envelope.PoW())
+ return nil // drop envelope without error
+ }
+
+ hash := envelope.Hash()
+
+ self.poolMu.Lock()
+ _, alreadyCached := self.envelopes[hash]
+ if !alreadyCached {
+ self.envelopes[hash] = envelope
+ if self.expirations[envelope.Expiry] == nil {
+ self.expirations[envelope.Expiry] = set.NewNonTS()
+ }
+ if !self.expirations[envelope.Expiry].Has(hash) {
+ self.expirations[envelope.Expiry].Add(hash)
+ }
+ }
+ self.poolMu.Unlock()
+
+ if alreadyCached {
+ glog.V(logger.Detail).Infof("whisper envelope already cached: %x\n", envelope)
+ } else {
+ self.postEvent(envelope, messagesCode) // notify the local node about the new message
+ glog.V(logger.Detail).Infof("cached whisper envelope %x\n", envelope)
+ }
+ return nil
+}
+
+// postEvent delivers the message to the watchers.
+func (self *Whisper) postEvent(envelope *Envelope, messageCode uint64) {
+ // if the version of incoming message is higher than
+ // currently supported version, we can not decrypt it,
+ // and therefore just ignore this message
+ if envelope.Ver() <= EnvelopeVersion {
+ // todo: review if you need an additional thread here
+ go self.filters.NotifyWatchers(envelope, messageCode)
+ }
+}
+
+// update loops until the lifetime of the whisper node, updating its internal
+// state by expiring stale messages from the pool.
+func (self *Whisper) update() {
+ // Start a ticker to check for expirations
+ expire := time.NewTicker(expirationCycle)
+
+ // Repeat updates until termination is requested
+ for {
+ select {
+ case <-expire.C:
+ self.expire()
+
+ case <-self.quit:
+ return
+ }
+ }
+}
+
+// expire iterates over all the expiration timestamps, removing all stale
+// messages from the pools.
+func (self *Whisper) expire() {
+ self.poolMu.Lock()
+ defer self.poolMu.Unlock()
+
+ now := uint32(time.Now().Unix())
+ for then, hashSet := range self.expirations {
+ // Short circuit if a future time
+ if then > now {
+ continue
+ }
+ // Dump all expired messages and remove timestamp
+ hashSet.Each(func(v interface{}) bool {
+ delete(self.envelopes, v.(common.Hash))
+ delete(self.messages, v.(common.Hash))
+ return true
+ })
+ self.expirations[then].Clear()
+ }
+}
+
+// envelopes retrieves all the messages currently pooled by the node.
+func (self *Whisper) Envelopes() []*Envelope {
+ self.poolMu.RLock()
+ defer self.poolMu.RUnlock()
+
+ all := make([]*Envelope, 0, len(self.envelopes))
+ for _, envelope := range self.envelopes {
+ all = append(all, envelope)
+ }
+ return all
+}
+
+// Messages retrieves all the currently pooled messages matching a filter id.
+func (self *Whisper) Messages(id int) []*ReceivedMessage {
+ self.poolMu.RLock()
+ defer self.poolMu.RUnlock()
+
+ result := make([]*ReceivedMessage, 0)
+ if filter := self.filters.Get(id); filter != nil {
+ for _, msg := range self.messages {
+ if filter.MatchMessage(msg) {
+ result = append(result, msg)
+ }
+ }
+ }
+ return result
+}
+
+func (self *Whisper) addDecryptedMessage(msg *ReceivedMessage) {
+ self.poolMu.Lock()
+ defer self.poolMu.Unlock()
+
+ self.messages[msg.EnvelopeHash] = msg
+}
+
+func validatePublicKey(k *ecdsa.PublicKey) bool {
+ return k != nil && k.X != nil && k.Y != nil && k.X.Sign() != 0 && k.Y.Sign() != 0
+}
+
+func validatePrivateKey(k *ecdsa.PrivateKey) bool {
+ if k == nil || k.D == nil || k.D.Sign() == 0 {
+ return false
+ }
+ return validatePublicKey(&k.PublicKey)
+}
+
+// validateSymmetricKey returns false if the key contains all zeros
+func validateSymmetricKey(k []byte) bool {
+ if len(k) == 0 {
+ return false
+ }
+ empty := containsOnlyZeros(k)
+ return !empty
+}
+
+func containsOnlyZeros(data []byte) bool {
+ for _, b := range data {
+ if b != 0 {
+ return true
+ }
+ }
+ return false
+}
+
+func bytesToIntLittleEndian(b []byte) (res uint64) {
+ mul := uint64(1)
+ for i := 0; i < len(b); i++ {
+ res += uint64(b[i]) * mul
+ mul *= 256
+ }
+ return
+}
+
+func bytesToIntBigEndian(b []byte) (res uint64) {
+ for i := 0; i < len(b); i++ {
+ res *= 256
+ res += uint64(b[i])
+ }
+ return
+}
+
+// DeriveSymmetricKey derives symmetric key material from the key or password.
+// pbkdf2 is used for security, in case people use password instead of randomly generated keys.
+func DeriveKeyMaterial(key []byte, version uint64) (derivedKey []byte, err error) {
+ if version == 0 {
+ // todo: review: kdf should run no less than 1 sec, because it's a once in a session experience
+ derivedKey = pbkdf2.Key(key, nil, 65356, aesKeyLength, sha256.New)
+ } else {
+ err = fmt.Errorf("DeriveSymmetricKey: invalid envelope version: %d", version)
+ }
+ return
+}