2019-07-17 22:25:42 +00:00
|
|
|
package whisper
|
|
|
|
|
|
|
|
import (
|
2019-11-06 16:23:11 +00:00
|
|
|
"bytes"
|
2019-07-17 22:25:42 +00:00
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
2019-07-30 06:14:13 +00:00
|
|
|
"database/sql"
|
2019-07-17 22:25:42 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"go.uber.org/zap"
|
2019-10-09 14:22:53 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2019-07-17 22:25:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrNoMailservers returned if there is no configured mailservers that can be used.
|
|
|
|
ErrNoMailservers = errors.New("no configured mailservers")
|
|
|
|
)
|
|
|
|
|
|
|
|
type whisperServiceKeysManager struct {
|
2019-11-23 17:57:05 +00:00
|
|
|
shh types.Whisper
|
2019-07-17 22:25:42 +00:00
|
|
|
|
|
|
|
// Identity of the current user.
|
|
|
|
privateKey *ecdsa.PrivateKey
|
|
|
|
|
|
|
|
passToSymKeyMutex sync.RWMutex
|
|
|
|
passToSymKeyCache map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *whisperServiceKeysManager) AddOrGetKeyPair(priv *ecdsa.PrivateKey) (string, error) {
|
|
|
|
// caching is handled in Whisper
|
|
|
|
return m.shh.AddKeyPair(priv)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *whisperServiceKeysManager) AddOrGetSymKeyFromPassword(password string) (string, error) {
|
|
|
|
m.passToSymKeyMutex.Lock()
|
|
|
|
defer m.passToSymKeyMutex.Unlock()
|
|
|
|
|
|
|
|
if val, ok := m.passToSymKeyCache[password]; ok {
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
id, err := m.shh.AddSymKeyFromPassword(password)
|
|
|
|
if err != nil {
|
|
|
|
return id, err
|
|
|
|
}
|
|
|
|
|
|
|
|
m.passToSymKeyCache[password] = id
|
|
|
|
|
|
|
|
return id, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *whisperServiceKeysManager) RawSymKey(id string) ([]byte, error) {
|
|
|
|
return m.shh.GetSymKey(id)
|
|
|
|
}
|
|
|
|
|
2019-08-29 06:33:46 +00:00
|
|
|
type Option func(*WhisperServiceTransport) error
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
// WhisperServiceTransport is a transport based on Whisper service.
|
|
|
|
type WhisperServiceTransport struct {
|
2019-11-23 17:57:05 +00:00
|
|
|
shh types.Whisper
|
|
|
|
shhAPI types.PublicWhisperAPI // only PublicWhisperAPI implements logic to send messages
|
2019-07-17 22:25:42 +00:00
|
|
|
keysManager *whisperServiceKeysManager
|
2019-08-29 06:33:46 +00:00
|
|
|
filters *filtersManager
|
2019-07-17 22:25:42 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
|
2019-08-09 07:03:10 +00:00
|
|
|
mailservers []string
|
|
|
|
envelopesMonitor *EnvelopesMonitor
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
// NewWhisperServiceTransport returns a new WhisperServiceTransport.
|
2019-10-14 14:10:48 +00:00
|
|
|
// TODO: leaving a chat should verify that for a given public key
|
|
|
|
// there are no other chats. It may happen that we leave a private chat
|
|
|
|
// but still have a public chat for a given public key.
|
2019-07-17 22:25:42 +00:00
|
|
|
func NewWhisperServiceTransport(
|
2019-11-23 17:57:05 +00:00
|
|
|
shh types.Whisper,
|
2019-07-17 22:25:42 +00:00
|
|
|
privateKey *ecdsa.PrivateKey,
|
2019-07-30 06:14:13 +00:00
|
|
|
db *sql.DB,
|
2019-07-17 22:25:42 +00:00
|
|
|
mailservers []string,
|
2019-08-09 07:03:10 +00:00
|
|
|
envelopesMonitorConfig *EnvelopesMonitorConfig,
|
2019-07-17 22:25:42 +00:00
|
|
|
logger *zap.Logger,
|
2019-08-29 06:33:46 +00:00
|
|
|
opts ...Option,
|
2019-07-17 22:25:42 +00:00
|
|
|
) (*WhisperServiceTransport, error) {
|
2019-08-29 06:33:46 +00:00
|
|
|
filtersManager, err := newFiltersManager(db, shh, privateKey, logger)
|
2019-07-17 22:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-09 07:03:10 +00:00
|
|
|
var envelopesMonitor *EnvelopesMonitor
|
|
|
|
if envelopesMonitorConfig != nil {
|
2019-10-09 14:22:53 +00:00
|
|
|
envelopesMonitor = NewEnvelopesMonitor(shh, *envelopesMonitorConfig)
|
2019-08-09 07:03:10 +00:00
|
|
|
envelopesMonitor.Start()
|
|
|
|
}
|
2019-08-29 06:33:46 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
var shhAPI types.PublicWhisperAPI
|
2019-10-09 14:22:53 +00:00
|
|
|
if shh != nil {
|
|
|
|
shhAPI = shh.PublicWhisperAPI()
|
|
|
|
}
|
2019-08-29 06:33:46 +00:00
|
|
|
t := &WhisperServiceTransport{
|
2019-08-09 07:03:10 +00:00
|
|
|
shh: shh,
|
2019-10-09 14:22:53 +00:00
|
|
|
shhAPI: shhAPI,
|
2019-08-09 07:03:10 +00:00
|
|
|
envelopesMonitor: envelopesMonitor,
|
2019-07-17 22:25:42 +00:00
|
|
|
keysManager: &whisperServiceKeysManager{
|
|
|
|
shh: shh,
|
|
|
|
privateKey: privateKey,
|
|
|
|
passToSymKeyCache: make(map[string]string),
|
|
|
|
},
|
2019-08-29 06:33:46 +00:00
|
|
|
filters: filtersManager,
|
2019-07-17 22:25:42 +00:00
|
|
|
mailservers: mailservers,
|
|
|
|
logger: logger.With(zap.Namespace("WhisperServiceTransport")),
|
2019-08-29 06:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(t); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WhisperServiceTransport) InitFilters(chatIDs []string, publicKeys []*ecdsa.PublicKey) ([]*Filter, error) {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
return a.filters.Init(chatIDs, publicKeys)
|
2019-08-29 06:33:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WhisperServiceTransport) Filters() []*Filter {
|
|
|
|
return a.filters.Filters()
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED
|
2019-08-29 06:33:46 +00:00
|
|
|
func (a *WhisperServiceTransport) LoadFilters(filters []*Filter) ([]*Filter, error) {
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
return a.filters.InitWithFilters(filters)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// DEPRECATED
|
2019-08-29 06:33:46 +00:00
|
|
|
func (a *WhisperServiceTransport) RemoveFilters(filters []*Filter) error {
|
|
|
|
return a.filters.Remove(filters...)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WhisperServiceTransport) Reset() error {
|
2019-08-29 06:33:46 +00:00
|
|
|
return a.filters.Reset()
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (a *WhisperServiceTransport) ProcessNegotiatedSecret(secret types.NegotiatedSecret) (*Filter, error) {
|
2019-08-29 06:33:46 +00:00
|
|
|
filter, err := a.filters.LoadNegotiated(secret)
|
2019-08-27 12:04:15 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return filter, nil
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WhisperServiceTransport) JoinPublic(chatID string) error {
|
2019-08-29 06:33:46 +00:00
|
|
|
_, err := a.filters.LoadPublic(chatID)
|
2019-07-17 22:25:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WhisperServiceTransport) LeavePublic(chatID string) error {
|
2019-08-29 06:33:46 +00:00
|
|
|
chat := a.filters.Filter(chatID)
|
2019-07-17 22:25:42 +00:00
|
|
|
if chat != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-08-29 06:33:46 +00:00
|
|
|
return a.filters.Remove(chat)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WhisperServiceTransport) JoinPrivate(publicKey *ecdsa.PublicKey) error {
|
2019-09-02 09:29:06 +00:00
|
|
|
_, err := a.filters.LoadDiscovery()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = a.filters.LoadContactCode(publicKey)
|
2019-07-17 22:25:42 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WhisperServiceTransport) LeavePrivate(publicKey *ecdsa.PublicKey) error {
|
2019-08-29 06:33:46 +00:00
|
|
|
filters := a.filters.FiltersByPublicKey(publicKey)
|
|
|
|
return a.filters.Remove(filters...)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
func (a *WhisperServiceTransport) JoinGroup(publicKeys []*ecdsa.PublicKey) error {
|
|
|
|
_, err := a.filters.LoadDiscovery()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, pk := range publicKeys {
|
|
|
|
_, err = a.filters.LoadContactCode(pk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *WhisperServiceTransport) LeaveGroup(publicKeys []*ecdsa.PublicKey) error {
|
|
|
|
for _, publicKey := range publicKeys {
|
|
|
|
filters := a.filters.FiltersByPublicKey(publicKey)
|
|
|
|
if err := a.filters.Remove(filters...); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
type Message struct {
|
2019-11-23 17:57:05 +00:00
|
|
|
Message *types.Message
|
2019-09-26 07:01:17 +00:00
|
|
|
Public bool
|
2019-07-26 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
func (a *WhisperServiceTransport) RetrieveAllMessages() ([]Message, error) {
|
|
|
|
var messages []Message
|
2019-07-26 07:17:29 +00:00
|
|
|
|
2019-08-29 06:33:46 +00:00
|
|
|
for _, filter := range a.filters.Filters() {
|
2019-10-09 14:22:53 +00:00
|
|
|
filterMsgs, err := a.shhAPI.GetFilterMessages(filter.FilterID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-07-26 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
for _, m := range filterMsgs {
|
2019-09-26 07:01:17 +00:00
|
|
|
messages = append(messages, Message{
|
|
|
|
Message: m,
|
|
|
|
Public: filter.IsPublic(),
|
|
|
|
})
|
|
|
|
}
|
2019-07-26 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
return messages, nil
|
2019-07-26 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (a *WhisperServiceTransport) RetrievePublicMessages(chatID string) ([]*types.Message, error) {
|
2019-08-29 06:33:46 +00:00
|
|
|
filter, err := a.filters.LoadPublic(chatID)
|
2019-07-17 22:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
return a.shhAPI.GetFilterMessages(filter.FilterID)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (a *WhisperServiceTransport) RetrievePrivateMessages(publicKey *ecdsa.PublicKey) ([]*types.Message, error) {
|
2019-08-29 06:33:46 +00:00
|
|
|
chats := a.filters.FiltersByPublicKey(publicKey)
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
discoveryChats, err := a.filters.Init(nil, nil)
|
2019-07-17 22:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
var result []*types.Message
|
2019-07-17 22:25:42 +00:00
|
|
|
|
|
|
|
for _, chat := range append(chats, discoveryChats...) {
|
2019-10-09 14:22:53 +00:00
|
|
|
filterMsgs, err := a.shhAPI.GetFilterMessages(chat.FilterID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
2019-07-26 07:17:29 +00:00
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
result = append(result, filterMsgs...)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (a *WhisperServiceTransport) RetrieveRawAll() (map[Filter][]*types.Message, error) {
|
|
|
|
result := make(map[Filter][]*types.Message)
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-11-04 10:08:22 +00:00
|
|
|
allFilters := a.filters.Filters()
|
|
|
|
for _, filter := range allFilters {
|
|
|
|
msgs, err := a.shhAPI.GetFilterMessages(filter.FilterID)
|
|
|
|
if err != nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
result[*filter] = append(result[*filter], msgs...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendPublic sends a new message using the Whisper service.
|
2019-08-09 07:03:10 +00:00
|
|
|
// For public filters, chat name is used as an ID as well as
|
2019-07-17 22:25:42 +00:00
|
|
|
// a topic.
|
2019-11-23 17:57:05 +00:00
|
|
|
func (a *WhisperServiceTransport) SendPublic(ctx context.Context, newMessage *types.NewMessage, chatName string) ([]byte, error) {
|
2019-08-09 07:03:10 +00:00
|
|
|
if err := a.addSig(newMessage); err != nil {
|
2019-07-17 22:25:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-29 06:33:46 +00:00
|
|
|
filter, err := a.filters.LoadPublic(chatName)
|
2019-07-17 22:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-29 06:33:46 +00:00
|
|
|
newMessage.SymKeyID = filter.SymKeyID
|
2019-11-06 16:23:11 +00:00
|
|
|
newMessage.Topic = filter.Topic
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-08-09 07:03:10 +00:00
|
|
|
return a.shhAPI.Post(ctx, *newMessage)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (a *WhisperServiceTransport) SendPrivateWithSharedSecret(ctx context.Context, newMessage *types.NewMessage, publicKey *ecdsa.PublicKey, secret []byte) ([]byte, error) {
|
2019-08-09 07:03:10 +00:00
|
|
|
if err := a.addSig(newMessage); err != nil {
|
2019-07-17 22:25:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
filter, err := a.filters.LoadNegotiated(types.NegotiatedSecret{
|
2019-07-17 22:25:42 +00:00
|
|
|
PublicKey: publicKey,
|
|
|
|
Key: secret,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-29 06:33:46 +00:00
|
|
|
newMessage.SymKeyID = filter.SymKeyID
|
2019-11-23 17:57:05 +00:00
|
|
|
newMessage.Topic = filter.Topic
|
2019-07-17 22:25:42 +00:00
|
|
|
newMessage.PublicKey = nil
|
|
|
|
|
2019-08-09 07:03:10 +00:00
|
|
|
return a.shhAPI.Post(ctx, *newMessage)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (a *WhisperServiceTransport) SendPrivateWithPartitioned(ctx context.Context, newMessage *types.NewMessage, publicKey *ecdsa.PublicKey) ([]byte, error) {
|
2019-08-09 07:03:10 +00:00
|
|
|
if err := a.addSig(newMessage); err != nil {
|
2019-07-17 22:25:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-08-29 06:33:46 +00:00
|
|
|
filter, err := a.filters.LoadPartitioned(publicKey)
|
2019-07-17 22:25:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-06 16:23:11 +00:00
|
|
|
newMessage.Topic = filter.Topic
|
2019-07-17 22:25:42 +00:00
|
|
|
newMessage.PublicKey = crypto.FromECDSAPub(publicKey)
|
|
|
|
|
2019-08-09 07:03:10 +00:00
|
|
|
return a.shhAPI.Post(ctx, *newMessage)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (a *WhisperServiceTransport) SendPrivateOnDiscovery(ctx context.Context, newMessage *types.NewMessage, publicKey *ecdsa.PublicKey) ([]byte, error) {
|
2019-08-09 07:03:10 +00:00
|
|
|
if err := a.addSig(newMessage); err != nil {
|
2019-07-17 22:25:42 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// There is no need to load any chat
|
|
|
|
// because listening on the discovery topic
|
|
|
|
// is done automatically.
|
|
|
|
// TODO: change this anyway, it should be explicit
|
|
|
|
// and idempotent.
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
newMessage.Topic = types.BytesToTopic(
|
2019-08-09 07:03:10 +00:00
|
|
|
ToTopic(discoveryTopic),
|
2019-07-17 22:25:42 +00:00
|
|
|
)
|
|
|
|
newMessage.PublicKey = crypto.FromECDSAPub(publicKey)
|
|
|
|
|
2019-08-09 07:03:10 +00:00
|
|
|
return a.shhAPI.Post(ctx, *newMessage)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (a *WhisperServiceTransport) addSig(newMessage *types.NewMessage) error {
|
2019-07-17 22:25:42 +00:00
|
|
|
sigID, err := a.keysManager.AddOrGetKeyPair(a.keysManager.privateKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-10-28 13:50:33 +00:00
|
|
|
newMessage.SigID = sigID
|
2019-07-17 22:25:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (a *WhisperServiceTransport) Track(identifiers [][]byte, hash []byte, newMessage *types.NewMessage) {
|
2019-08-09 07:03:10 +00:00
|
|
|
if a.envelopesMonitor != nil {
|
2019-11-23 17:57:05 +00:00
|
|
|
a.envelopesMonitor.Add(identifiers, types.BytesToHash(hash), *newMessage)
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-09-02 09:29:06 +00:00
|
|
|
func (a *WhisperServiceTransport) Stop() error {
|
2019-08-09 07:03:10 +00:00
|
|
|
if a.envelopesMonitor != nil {
|
|
|
|
a.envelopesMonitor.Stop()
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
2019-09-02 09:29:06 +00:00
|
|
|
return nil
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-06 16:23:11 +00:00
|
|
|
// RequestHistoricMessages requests historic messages for all registered filters.
|
|
|
|
func (a *WhisperServiceTransport) SendMessagesRequest(
|
|
|
|
ctx context.Context,
|
|
|
|
peerID []byte,
|
|
|
|
from, to uint32,
|
|
|
|
previousCursor []byte,
|
|
|
|
) (cursor []byte, err error) {
|
2019-11-23 17:57:05 +00:00
|
|
|
topics := make([]types.TopicType, len(a.Filters()))
|
2019-11-06 16:23:11 +00:00
|
|
|
for _, f := range a.Filters() {
|
|
|
|
topics = append(topics, f.Topic)
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-11-06 16:23:11 +00:00
|
|
|
r := createMessagesRequest(from, to, previousCursor, topics)
|
|
|
|
r.SetDefaults(a.shh.GetCurrentTime())
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
events := make(chan types.EnvelopeEvent, 10)
|
2019-11-06 16:23:11 +00:00
|
|
|
sub := a.shh.SubscribeEnvelopeEvents(events)
|
|
|
|
defer sub.Unsubscribe()
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-11-06 16:23:11 +00:00
|
|
|
err = a.shh.SendMessagesRequest(peerID, r)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-11-06 16:23:11 +00:00
|
|
|
resp, err := a.waitForRequestCompleted(ctx, r.ID, events)
|
|
|
|
if err == nil && resp != nil && resp.Error != nil {
|
|
|
|
err = resp.Error
|
|
|
|
} else if err == nil && resp != nil {
|
|
|
|
cursor = resp.Cursor
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (a *WhisperServiceTransport) waitForRequestCompleted(ctx context.Context, requestID []byte, events chan types.EnvelopeEvent) (*types.MailServerResponse, error) {
|
2019-11-06 16:23:11 +00:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case ev := <-events:
|
|
|
|
a.logger.Debug(
|
|
|
|
"waiting for request completed and received an event",
|
|
|
|
zap.Binary("requestID", requestID),
|
|
|
|
zap.Any("event", ev),
|
|
|
|
)
|
|
|
|
if !bytes.Equal(ev.Hash.Bytes(), requestID) {
|
|
|
|
continue
|
|
|
|
}
|
2019-11-23 17:57:05 +00:00
|
|
|
if ev.Event != types.EventMailServerRequestCompleted {
|
2019-11-06 16:23:11 +00:00
|
|
|
continue
|
|
|
|
}
|
2019-11-23 17:57:05 +00:00
|
|
|
data, ok := ev.Data.(*types.MailServerResponse)
|
2019-11-06 16:23:11 +00:00
|
|
|
if ok {
|
|
|
|
return data, nil
|
|
|
|
}
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil, ctx.Err()
|
|
|
|
}
|
|
|
|
}
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|