2018-04-11 15:41:51 +00:00
|
|
|
package shhext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-04-26 05:56:19 +00:00
|
|
|
"crypto/ecdsa"
|
|
|
|
"encoding/binary"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"time"
|
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-04-26 05:56:19 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/discover"
|
2018-04-11 15:41:51 +00:00
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv6"
|
|
|
|
)
|
|
|
|
|
2018-04-26 05:56:19 +00:00
|
|
|
const (
|
|
|
|
// defaultWorkTime is a work time reported in messages sent to MailServer nodes.
|
|
|
|
defaultWorkTime = 5
|
|
|
|
)
|
|
|
|
|
|
|
|
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")
|
|
|
|
)
|
|
|
|
|
|
|
|
// -----
|
|
|
|
// 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"`
|
|
|
|
|
|
|
|
// Topic is a regular Whisper topic.
|
|
|
|
Topic whisper.TopicType `json:"topic"`
|
|
|
|
|
|
|
|
// SymKeyID is an ID of a symmetric key to authenticate to MailServer.
|
|
|
|
// It's derived from MailServer password.
|
|
|
|
SymKeyID string `json:"symKeyID"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
if r.From == 0 && r.To == 0 {
|
2018-05-04 08:23:38 +00:00
|
|
|
r.From = uint32(now.UTC().Add(-24 * time.Hour).Unix())
|
|
|
|
r.To = uint32(now.UTC().Unix())
|
2018-04-11 15:41:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (api *PublicAPI) RequestMessages(_ context.Context, r MessagesRequest) (bool, error) {
|
|
|
|
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-04-26 05:56:19 +00:00
|
|
|
mailServerNode, err := discover.ParseNode(r.MailServerPeer)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("%v: %v", ErrInvalidMailServerPeer, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
symKey, err := shh.GetSymKey(r.SymKeyID)
|
|
|
|
if err != nil {
|
|
|
|
return false, fmt.Errorf("%v: %v", ErrInvalidSymKeyID, err)
|
|
|
|
}
|
|
|
|
|
2018-05-04 08:23:38 +00:00
|
|
|
envelope, err := makeEnvelop(makePayload(r), symKey, api.service.nodeID, shh.MinPow(), now)
|
2018-04-26 05:56:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if err := shh.RequestHistoricMessages(mailServerNode.ID[:], envelope); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
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-05-02 12:14:08 +00:00
|
|
|
return api.service.deduplicator.Deduplicate(msgs), err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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-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-05-04 08:23:38 +00:00
|
|
|
func makeEnvelop(payload []byte, symKey []byte, 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,
|
|
|
|
KeySym: symKey,
|
|
|
|
WorkTime: defaultWorkTime,
|
|
|
|
Src: nodeID,
|
|
|
|
}
|
|
|
|
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.
|
|
|
|
func makePayload(r MessagesRequest) []byte {
|
|
|
|
// first 8 bytes are lowed and upper bounds as uint32
|
|
|
|
data := make([]byte, 8+whisper.BloomFilterSize)
|
|
|
|
binary.BigEndian.PutUint32(data, r.From)
|
|
|
|
binary.BigEndian.PutUint32(data[4:], r.To)
|
|
|
|
copy(data[8:], whisper.TopicToBloom(r.Topic))
|
|
|
|
return data
|
|
|
|
}
|