2018-04-11 15:41:51 +00:00
|
|
|
package shhext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-04-26 05:56:19 +00:00
|
|
|
"crypto/ecdsa"
|
2018-07-02 07:38:10 +00:00
|
|
|
"encoding/hex"
|
2018-04-26 05:56:19 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2018-10-15 21:15:04 +00:00
|
|
|
"math/big"
|
2018-04-26 05:56:19 +00:00
|
|
|
"time"
|
2018-04-11 15:41:51 +00:00
|
|
|
|
2018-10-19 09:09:13 +00:00
|
|
|
"github.com/ethereum/go-ethereum/rlp"
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2018-04-24 15:50:26 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2018-09-24 18:07:34 +00:00
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2018-04-26 05:56:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2018-11-14 07:03:58 +00:00
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
2018-09-24 18:07:34 +00:00
|
|
|
"github.com/status-im/status-go/services/shhext/chat"
|
2018-09-25 07:05:38 +00:00
|
|
|
whisper "github.com/status-im/whisper/whisperv6"
|
2018-04-11 15:41:51 +00:00
|
|
|
)
|
|
|
|
|
2018-04-26 05:56:19 +00:00
|
|
|
const (
|
|
|
|
// defaultWorkTime is a work time reported in messages sent to MailServer nodes.
|
|
|
|
defaultWorkTime = 5
|
2018-06-15 15:12:31 +00:00
|
|
|
// defaultRequestTimeout is the default request timeout in seconds
|
|
|
|
defaultRequestTimeout = 10
|
2018-04-26 05:56:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrInvalidMailServerPeer is returned when it fails to parse enode from params.
|
|
|
|
ErrInvalidMailServerPeer = errors.New("invalid mailServerPeer value")
|
|
|
|
// ErrInvalidSymKeyID is returned when it fails to get a symmetric key.
|
|
|
|
ErrInvalidSymKeyID = errors.New("invalid symKeyID value")
|
2018-07-04 09:30:57 +00:00
|
|
|
// ErrInvalidPublicKey is returned when public key can't be extracted
|
|
|
|
// from MailServer's nodeID.
|
|
|
|
ErrInvalidPublicKey = errors.New("can't extract public key")
|
2018-09-24 18:07:34 +00:00
|
|
|
// ErrPFSNotEnabled is returned when an endpoint PFS only is called but
|
|
|
|
// PFS is disabled
|
|
|
|
ErrPFSNotEnabled = errors.New("pfs not enabled")
|
2018-04-26 05:56:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// -----
|
|
|
|
// PAYLOADS
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// MessagesRequest is a payload send to a MailServer to get messages.
|
|
|
|
type MessagesRequest struct {
|
|
|
|
// MailServerPeer is MailServer's enode address.
|
|
|
|
MailServerPeer string `json:"mailServerPeer"`
|
|
|
|
|
|
|
|
// From is a lower bound of time range (optional).
|
|
|
|
// Default is 24 hours back from now.
|
|
|
|
From uint32 `json:"from"`
|
|
|
|
|
|
|
|
// To is a upper bound of time range (optional).
|
|
|
|
// Default is now.
|
|
|
|
To uint32 `json:"to"`
|
|
|
|
|
2018-07-02 07:38:10 +00:00
|
|
|
// Limit determines the number of messages sent by the mail server
|
|
|
|
// for the current paginated request
|
|
|
|
Limit uint32 `json:"limit"`
|
|
|
|
|
|
|
|
// Cursor is used as starting point for paginated requests
|
|
|
|
Cursor string `json:"cursor"`
|
|
|
|
|
2018-04-26 05:56:19 +00:00
|
|
|
// Topic is a regular Whisper topic.
|
2018-10-15 21:15:04 +00:00
|
|
|
// DEPRECATED
|
2018-04-26 05:56:19 +00:00
|
|
|
Topic whisper.TopicType `json:"topic"`
|
|
|
|
|
2018-10-15 21:15:04 +00:00
|
|
|
// Topics is a list of Whisper topics.
|
|
|
|
Topics []whisper.TopicType `json:"topics"`
|
|
|
|
|
2018-04-26 05:56:19 +00:00
|
|
|
// SymKeyID is an ID of a symmetric key to authenticate to MailServer.
|
|
|
|
// It's derived from MailServer password.
|
2018-07-04 09:30:57 +00:00
|
|
|
//
|
|
|
|
// It's also possible to authenticate request with MailServerPeer
|
|
|
|
// public key.
|
2018-04-26 05:56:19 +00:00
|
|
|
SymKeyID string `json:"symKeyID"`
|
2018-06-15 15:12:31 +00:00
|
|
|
|
|
|
|
// Timeout is the time to live of the request specified in seconds.
|
|
|
|
// Default is 10 seconds
|
|
|
|
Timeout time.Duration `json:"timeout"`
|
2018-04-26 05:56:19 +00:00
|
|
|
}
|
|
|
|
|
2018-05-04 08:23:38 +00:00
|
|
|
func (r *MessagesRequest) setDefaults(now time.Time) {
|
2018-04-26 05:56:19 +00:00
|
|
|
// set From and To defaults
|
2018-05-10 14:47:54 +00:00
|
|
|
if r.To == 0 {
|
2018-05-04 08:23:38 +00:00
|
|
|
r.To = uint32(now.UTC().Unix())
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
2018-05-10 14:47:54 +00:00
|
|
|
|
|
|
|
if r.From == 0 {
|
|
|
|
oneDay := uint32(86400) // -24 hours
|
|
|
|
if r.To < oneDay {
|
|
|
|
r.From = 0
|
|
|
|
} else {
|
|
|
|
r.From = r.To - oneDay
|
|
|
|
}
|
|
|
|
}
|
2018-06-15 15:12:31 +00:00
|
|
|
|
|
|
|
if r.Timeout == 0 {
|
|
|
|
r.Timeout = defaultRequestTimeout
|
|
|
|
}
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 09:09:13 +00:00
|
|
|
// MessagesRequestPayload is a payload sent to the Mail Server.
|
|
|
|
type MessagesRequestPayload struct {
|
|
|
|
// Lower is a lower bound of time range for which messages are requested.
|
|
|
|
Lower uint32
|
|
|
|
// Upper is a lower bound of time range for which messages are requested.
|
|
|
|
Upper uint32
|
|
|
|
// Bloom is a bloom filter to filter envelopes.
|
|
|
|
Bloom []byte
|
|
|
|
// Limit is the max number of envelopes to return.
|
|
|
|
Limit uint32
|
|
|
|
// Cursor is used for pagination of the results.
|
|
|
|
Cursor []byte
|
|
|
|
// Batch set to true indicates that the client supports batched response.
|
|
|
|
Batch bool
|
|
|
|
}
|
|
|
|
|
2018-04-26 05:56:19 +00:00
|
|
|
// -----
|
|
|
|
// PUBLIC API
|
|
|
|
// -----
|
|
|
|
|
2018-04-11 15:41:51 +00:00
|
|
|
// PublicAPI extends whisper public API.
|
|
|
|
type PublicAPI struct {
|
2018-04-26 05:56:19 +00:00
|
|
|
service *Service
|
2018-04-24 15:50:26 +00:00
|
|
|
publicAPI *whisper.PublicWhisperAPI
|
2018-04-26 05:56:19 +00:00
|
|
|
log log.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPublicAPI returns instance of the public API.
|
|
|
|
func NewPublicAPI(s *Service) *PublicAPI {
|
|
|
|
return &PublicAPI{
|
|
|
|
service: s,
|
|
|
|
publicAPI: whisper.NewPublicWhisperAPI(s.w),
|
|
|
|
log: log.New("package", "status-go/services/sshext.PublicAPI"),
|
|
|
|
}
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Post shamelessly copied from whisper codebase with slight modifications.
|
2018-04-24 15:50:26 +00:00
|
|
|
func (api *PublicAPI) Post(ctx context.Context, req whisper.NewMessage) (hash hexutil.Bytes, err error) {
|
|
|
|
hash, err = api.publicAPI.Post(ctx, req)
|
2018-04-11 15:41:51 +00:00
|
|
|
if err == nil {
|
2018-04-24 15:50:26 +00:00
|
|
|
var envHash common.Hash
|
|
|
|
copy(envHash[:], hash[:]) // slice can't be used as key
|
2018-04-26 05:56:19 +00:00
|
|
|
api.service.tracker.Add(envHash)
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
return hash, err
|
|
|
|
}
|
2018-04-26 05:56:19 +00:00
|
|
|
|
|
|
|
// RequestMessages sends a request for historic messages to a MailServer.
|
2018-06-15 15:12:31 +00:00
|
|
|
func (api *PublicAPI) RequestMessages(_ context.Context, r MessagesRequest) (hexutil.Bytes, error) {
|
2018-04-26 05:56:19 +00:00
|
|
|
api.log.Info("RequestMessages", "request", r)
|
|
|
|
shh := api.service.w
|
2018-05-04 08:23:38 +00:00
|
|
|
now := api.service.w.GetCurrentTime()
|
|
|
|
r.setDefaults(now)
|
2018-06-26 08:41:03 +00:00
|
|
|
|
|
|
|
if r.From > r.To {
|
|
|
|
return nil, fmt.Errorf("Query range is invalid: from > to (%d > %d)", r.From, r.To)
|
|
|
|
}
|
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
var err error
|
|
|
|
|
2018-11-14 07:03:58 +00:00
|
|
|
mailServerNode, err := enode.ParseV4(r.MailServerPeer)
|
2018-04-26 05:56:19 +00:00
|
|
|
if err != nil {
|
2018-06-15 15:12:31 +00:00
|
|
|
return nil, fmt.Errorf("%v: %v", ErrInvalidMailServerPeer, err)
|
2018-04-26 05:56:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
var (
|
|
|
|
symKey []byte
|
|
|
|
publicKey *ecdsa.PublicKey
|
|
|
|
)
|
|
|
|
|
|
|
|
if r.SymKeyID != "" {
|
|
|
|
symKey, err = shh.GetSymKey(r.SymKeyID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("%v: %v", ErrInvalidSymKeyID, err)
|
|
|
|
}
|
|
|
|
} else {
|
2018-11-14 07:03:58 +00:00
|
|
|
publicKey = mailServerNode.Pubkey()
|
2018-04-26 05:56:19 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 09:09:13 +00:00
|
|
|
payload, err := makePayload(r)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-07-04 09:30:57 +00:00
|
|
|
envelope, err := makeEnvelop(
|
2018-10-19 09:09:13 +00:00
|
|
|
payload,
|
2018-07-04 09:30:57 +00:00
|
|
|
symKey,
|
|
|
|
publicKey,
|
|
|
|
api.service.nodeID,
|
|
|
|
shh.MinPow(),
|
|
|
|
now,
|
|
|
|
)
|
2018-04-26 05:56:19 +00:00
|
|
|
if err != nil {
|
2018-06-15 15:12:31 +00:00
|
|
|
return nil, err
|
2018-04-26 05:56:19 +00:00
|
|
|
}
|
2018-06-15 15:12:31 +00:00
|
|
|
|
|
|
|
hash := envelope.Hash()
|
2018-11-14 07:03:58 +00:00
|
|
|
if err := shh.RequestHistoricMessages(mailServerNode.ID().Bytes(), envelope); err != nil {
|
2018-06-15 15:12:31 +00:00
|
|
|
return nil, err
|
2018-04-26 05:56:19 +00:00
|
|
|
}
|
|
|
|
|
2018-06-15 15:12:31 +00:00
|
|
|
api.service.tracker.AddRequest(hash, time.After(r.Timeout*time.Second))
|
|
|
|
|
|
|
|
return hash[:], nil
|
2018-04-26 05:56:19 +00:00
|
|
|
}
|
|
|
|
|
2018-04-20 11:26:54 +00:00
|
|
|
// GetNewFilterMessages is a prototype method with deduplication
|
|
|
|
func (api *PublicAPI) GetNewFilterMessages(filterID string) ([]*whisper.Message, error) {
|
|
|
|
msgs, err := api.publicAPI.GetFilterMessages(filterID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-24 18:07:34 +00:00
|
|
|
|
|
|
|
dedupMessages := api.service.deduplicator.Deduplicate(msgs)
|
|
|
|
|
|
|
|
if api.service.pfsEnabled {
|
|
|
|
// Attempt to decrypt message, otherwise leave unchanged
|
|
|
|
for _, msg := range dedupMessages {
|
|
|
|
|
|
|
|
if err := api.processPFSMessage(msg); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return dedupMessages, nil
|
2018-05-02 12:14:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ConfirmMessagesProcessed is a method to confirm that messages was consumed by
|
|
|
|
// the client side.
|
|
|
|
func (api *PublicAPI) ConfirmMessagesProcessed(messages []*whisper.Message) error {
|
|
|
|
return api.service.deduplicator.AddMessages(messages)
|
2018-04-20 11:26:54 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:07:34 +00:00
|
|
|
// SendPublicMessage sends a public chat message to the underlying transport
|
|
|
|
func (api *PublicAPI) SendPublicMessage(ctx context.Context, msg chat.SendPublicMessageRPC) (hexutil.Bytes, error) {
|
|
|
|
privateKey, err := api.service.w.GetPrivateKey(msg.Sig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is transport layer agnostic
|
|
|
|
protocolMessage, err := api.service.protocol.BuildPublicMessage(privateKey, msg.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
symKeyID, err := api.service.w.AddSymKeyFromPassword(msg.Chat)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enrich with transport layer info
|
2018-10-16 10:31:05 +00:00
|
|
|
whisperMessage := chat.PublicMessageToWhisper(msg, protocolMessage)
|
2018-09-24 18:07:34 +00:00
|
|
|
whisperMessage.SymKeyID = symKeyID
|
|
|
|
|
|
|
|
// And dispatch
|
2018-10-16 10:31:05 +00:00
|
|
|
return api.Post(ctx, whisperMessage)
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendDirectMessage sends a 1:1 chat message to the underlying transport
|
|
|
|
func (api *PublicAPI) SendDirectMessage(ctx context.Context, msg chat.SendDirectMessageRPC) ([]hexutil.Bytes, error) {
|
|
|
|
if !api.service.pfsEnabled {
|
|
|
|
return nil, ErrPFSNotEnabled
|
|
|
|
}
|
|
|
|
// To be completely agnostic from whisper we should not be using whisper to store the key
|
|
|
|
privateKey, err := api.service.w.GetPrivateKey(msg.Sig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKey, err := crypto.UnmarshalPubkey(msg.PubKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is transport layer-agnostic
|
2018-10-16 15:22:28 +00:00
|
|
|
protocolMessages, err := api.service.protocol.BuildDirectMessage(privateKey, msg.Payload, publicKey)
|
2018-09-24 18:07:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response []hexutil.Bytes
|
|
|
|
|
|
|
|
for key, message := range protocolMessages {
|
|
|
|
msg.PubKey = crypto.FromECDSAPub(key)
|
|
|
|
// Enrich with transport layer info
|
2018-10-16 10:31:05 +00:00
|
|
|
whisperMessage := chat.DirectMessageToWhisper(msg, message)
|
2018-09-24 18:07:34 +00:00
|
|
|
|
|
|
|
// And dispatch
|
2018-10-16 10:31:05 +00:00
|
|
|
hash, err := api.Post(ctx, whisperMessage)
|
2018-09-24 18:07:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response = append(response, hash)
|
|
|
|
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:31:05 +00:00
|
|
|
// SendPairingMessage sends a 1:1 chat message to our own devices to initiate a pairing session
|
|
|
|
func (api *PublicAPI) SendPairingMessage(ctx context.Context, msg chat.SendDirectMessageRPC) ([]hexutil.Bytes, error) {
|
|
|
|
if !api.service.pfsEnabled {
|
|
|
|
return nil, ErrPFSNotEnabled
|
|
|
|
}
|
|
|
|
// To be completely agnostic from whisper we should not be using whisper to store the key
|
|
|
|
privateKey, err := api.service.w.GetPrivateKey(msg.Sig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg.PubKey = crypto.FromECDSAPub(&privateKey.PublicKey)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
protocolMessage, err := api.service.protocol.BuildPairingMessage(privateKey, msg.Payload)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response []hexutil.Bytes
|
|
|
|
|
|
|
|
// Enrich with transport layer info
|
|
|
|
whisperMessage := chat.DirectMessageToWhisper(msg, protocolMessage)
|
|
|
|
|
|
|
|
// And dispatch
|
|
|
|
hash, err := api.Post(ctx, whisperMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response = append(response, hash)
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
2018-09-24 18:07:34 +00:00
|
|
|
// SendGroupMessage sends a group messag chat message to the underlying transport
|
|
|
|
func (api *PublicAPI) SendGroupMessage(ctx context.Context, msg chat.SendGroupMessageRPC) ([]hexutil.Bytes, error) {
|
|
|
|
if !api.service.pfsEnabled {
|
|
|
|
return nil, ErrPFSNotEnabled
|
|
|
|
}
|
|
|
|
|
|
|
|
// To be completely agnostic from whisper we should not be using whisper to store the key
|
|
|
|
privateKey, err := api.service.w.GetPrivateKey(msg.Sig)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var keys []*ecdsa.PublicKey
|
|
|
|
|
|
|
|
for _, k := range msg.PubKeys {
|
|
|
|
publicKey, err := crypto.UnmarshalPubkey(k)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
keys = append(keys, publicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// This is transport layer-agnostic
|
2018-10-16 15:22:28 +00:00
|
|
|
protocolMessages, err := api.service.protocol.BuildDirectMessage(privateKey, msg.Payload, keys...)
|
2018-09-24 18:07:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var response []hexutil.Bytes
|
|
|
|
|
|
|
|
for key, message := range protocolMessages {
|
|
|
|
directMessage := chat.SendDirectMessageRPC{
|
|
|
|
PubKey: crypto.FromECDSAPub(key),
|
|
|
|
Payload: msg.Payload,
|
|
|
|
Sig: msg.Sig,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Enrich with transport layer info
|
2018-10-16 10:31:05 +00:00
|
|
|
whisperMessage := chat.DirectMessageToWhisper(directMessage, message)
|
2018-09-24 18:07:34 +00:00
|
|
|
|
|
|
|
// And dispatch
|
2018-10-16 10:31:05 +00:00
|
|
|
hash, err := api.Post(ctx, whisperMessage)
|
2018-09-24 18:07:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
response = append(response, hash)
|
|
|
|
|
|
|
|
}
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (api *PublicAPI) processPFSMessage(msg *whisper.Message) error {
|
|
|
|
var privateKey *ecdsa.PrivateKey
|
|
|
|
var publicKey *ecdsa.PublicKey
|
|
|
|
|
|
|
|
// Msg.Dst is empty is a public message, nothing to do
|
|
|
|
if msg.Dst != nil {
|
|
|
|
// There's probably a better way to do this
|
|
|
|
keyBytes, err := hexutil.Bytes(msg.Dst).MarshalText()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
privateKey, err = api.service.w.GetPrivateKey(string(keyBytes))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This needs to be pushed down in the protocol message
|
|
|
|
publicKey, err = crypto.UnmarshalPubkey(msg.Sig)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:31:05 +00:00
|
|
|
response, err := api.service.protocol.HandleMessage(privateKey, publicKey, msg.Payload)
|
2018-09-24 18:07:34 +00:00
|
|
|
|
2018-10-16 10:31:05 +00:00
|
|
|
handler := EnvelopeSignalHandler{}
|
2018-09-24 18:07:34 +00:00
|
|
|
|
|
|
|
// Notify that someone tried to contact us using an invalid bundle
|
|
|
|
if err == chat.ErrSessionNotFound {
|
|
|
|
api.log.Warn("Session not found, sending signal", "err", err)
|
|
|
|
keyString := fmt.Sprintf("0x%x", crypto.FromECDSAPub(publicKey))
|
|
|
|
handler := EnvelopeSignalHandler{}
|
|
|
|
handler.DecryptMessageFailed(keyString)
|
2018-10-16 10:31:05 +00:00
|
|
|
return nil
|
|
|
|
} else if err != nil {
|
|
|
|
// Ignore errors for now as those might be non-pfs messages
|
|
|
|
api.log.Error("Failed handling message with error", "err", err)
|
|
|
|
return nil
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 10:31:05 +00:00
|
|
|
// Add unencrypted payload
|
|
|
|
msg.Payload = response.Message
|
|
|
|
|
|
|
|
// Notify of added bundles
|
|
|
|
if response.AddedBundles != nil {
|
|
|
|
for _, bundle := range response.AddedBundles {
|
|
|
|
handler.BundleAdded(bundle[0], bundle[1])
|
|
|
|
}
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 10:31:05 +00:00
|
|
|
return nil
|
2018-09-24 18:07:34 +00:00
|
|
|
}
|
|
|
|
|
2018-04-26 05:56:19 +00:00
|
|
|
// -----
|
|
|
|
// HELPER
|
|
|
|
// -----
|
|
|
|
|
|
|
|
// makeEnvelop makes an envelop for a historic messages request.
|
|
|
|
// Symmetric key is used to authenticate to MailServer.
|
|
|
|
// PK is the current node ID.
|
2018-07-04 09:30:57 +00:00
|
|
|
func makeEnvelop(
|
|
|
|
payload []byte,
|
|
|
|
symKey []byte,
|
|
|
|
publicKey *ecdsa.PublicKey,
|
|
|
|
nodeID *ecdsa.PrivateKey,
|
|
|
|
pow float64,
|
|
|
|
now time.Time,
|
|
|
|
) (*whisper.Envelope, error) {
|
2018-04-26 05:56:19 +00:00
|
|
|
params := whisper.MessageParams{
|
|
|
|
PoW: pow,
|
|
|
|
Payload: payload,
|
|
|
|
WorkTime: defaultWorkTime,
|
|
|
|
Src: nodeID,
|
|
|
|
}
|
2018-07-04 09:30:57 +00:00
|
|
|
// Either symKey or public key is required.
|
|
|
|
// This condition is verified in `message.Wrap()` method.
|
|
|
|
if len(symKey) > 0 {
|
|
|
|
params.KeySym = symKey
|
|
|
|
} else if publicKey != nil {
|
|
|
|
params.Dst = publicKey
|
|
|
|
}
|
2018-04-26 05:56:19 +00:00
|
|
|
message, err := whisper.NewSentMessage(¶ms)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-05-04 08:23:38 +00:00
|
|
|
return message.Wrap(¶ms, now)
|
2018-04-26 05:56:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// makePayload makes a specific payload for MailServer to request historic messages.
|
2018-10-19 09:09:13 +00:00
|
|
|
func makePayload(r MessagesRequest) ([]byte, error) {
|
2018-07-02 07:38:10 +00:00
|
|
|
expectedCursorSize := common.HashLength + 4
|
2018-10-19 09:09:13 +00:00
|
|
|
cursor, err := hex.DecodeString(r.Cursor)
|
|
|
|
if err != nil || len(cursor) != expectedCursorSize {
|
|
|
|
cursor = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
payload := MessagesRequestPayload{
|
|
|
|
Lower: r.From,
|
|
|
|
Upper: r.To,
|
|
|
|
Bloom: createBloomFilter(r),
|
|
|
|
Limit: r.Limit,
|
|
|
|
Cursor: cursor,
|
|
|
|
// Client must tell the MailServer if it supports batch responses.
|
|
|
|
// This can be removed in the future.
|
|
|
|
Batch: true,
|
2018-07-02 07:38:10 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 09:09:13 +00:00
|
|
|
return rlp.EncodeToBytes(payload)
|
2018-04-26 05:56:19 +00:00
|
|
|
}
|
2018-10-15 21:15:04 +00:00
|
|
|
|
|
|
|
func createBloomFilter(r MessagesRequest) []byte {
|
|
|
|
if len(r.Topics) > 0 {
|
|
|
|
return topicsToBloom(r.Topics...)
|
|
|
|
}
|
|
|
|
|
|
|
|
return whisper.TopicToBloom(r.Topic)
|
|
|
|
}
|
|
|
|
|
|
|
|
func topicsToBloom(topics ...whisper.TopicType) []byte {
|
|
|
|
i := new(big.Int)
|
|
|
|
for _, topic := range topics {
|
|
|
|
bloom := whisper.TopicToBloom(topic)
|
|
|
|
i.Or(i, new(big.Int).SetBytes(bloom[:]))
|
|
|
|
}
|
|
|
|
|
|
|
|
combined := make([]byte, whisper.BloomFilterSize)
|
|
|
|
data := i.Bytes()
|
|
|
|
copy(combined[whisper.BloomFilterSize-len(data):], data[:])
|
|
|
|
|
|
|
|
return combined
|
|
|
|
}
|