2019-11-21 16:19:22 +00:00
|
|
|
package protocol
|
2019-09-02 09:29:06 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"database/sql"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/pkg/errors"
|
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-11-21 16:19:22 +00:00
|
|
|
"github.com/status-im/status-go/protocol/datasync"
|
|
|
|
datasyncpeer "github.com/status-im/status-go/protocol/datasync/peer"
|
|
|
|
"github.com/status-im/status-go/protocol/encryption"
|
|
|
|
"github.com/status-im/status-go/protocol/encryption/multidevice"
|
|
|
|
transport "github.com/status-im/status-go/protocol/transport/whisper"
|
|
|
|
v1protocol "github.com/status-im/status-go/protocol/v1"
|
2019-09-02 09:29:06 +00:00
|
|
|
datasyncnode "github.com/vacp2p/mvds/node"
|
|
|
|
datasyncproto "github.com/vacp2p/mvds/protobuf"
|
2019-09-26 07:01:17 +00:00
|
|
|
"go.uber.org/zap"
|
2019-09-02 09:29:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Whisper message properties.
|
|
|
|
const (
|
|
|
|
whisperTTL = 15
|
|
|
|
whisperPoW = 0.002
|
|
|
|
whisperPoWTime = 5
|
|
|
|
)
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
type messageHandler interface {
|
2019-11-21 16:19:22 +00:00
|
|
|
HandleMembershipUpdate(m v1protocol.MembershipUpdateMessage) error
|
2019-10-14 14:10:48 +00:00
|
|
|
}
|
|
|
|
|
2019-09-02 09:29:06 +00:00
|
|
|
type messageProcessor struct {
|
|
|
|
identity *ecdsa.PrivateKey
|
|
|
|
datasync *datasync.DataSync
|
|
|
|
protocol *encryption.Protocol
|
|
|
|
transport *transport.WhisperServiceTransport
|
2019-10-14 14:10:48 +00:00
|
|
|
handler messageHandler
|
2019-09-02 09:29:06 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
|
|
|
|
featureFlags featureFlags
|
|
|
|
}
|
|
|
|
|
|
|
|
func newMessageProcessor(
|
|
|
|
identity *ecdsa.PrivateKey,
|
|
|
|
database *sql.DB,
|
|
|
|
enc *encryption.Protocol,
|
|
|
|
transport *transport.WhisperServiceTransport,
|
2019-10-14 14:10:48 +00:00
|
|
|
handler messageHandler,
|
2019-09-02 09:29:06 +00:00
|
|
|
logger *zap.Logger,
|
|
|
|
features featureFlags,
|
|
|
|
) (*messageProcessor, error) {
|
|
|
|
dataSyncTransport := datasync.NewDataSyncNodeTransport()
|
|
|
|
dataSyncNode, err := datasyncnode.NewPersistentNode(
|
|
|
|
database,
|
|
|
|
dataSyncTransport,
|
|
|
|
datasyncpeer.PublicKeyToPeerID(identity.PublicKey),
|
|
|
|
datasyncnode.BATCH,
|
|
|
|
datasync.CalculateSendTime,
|
|
|
|
logger,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
ds := datasync.New(dataSyncNode, dataSyncTransport, features.datasync, logger)
|
|
|
|
|
|
|
|
p := &messageProcessor{
|
|
|
|
identity: identity,
|
|
|
|
datasync: ds,
|
|
|
|
protocol: enc,
|
|
|
|
transport: transport,
|
2019-10-14 14:10:48 +00:00
|
|
|
handler: handler,
|
2019-09-02 09:29:06 +00:00
|
|
|
logger: logger,
|
|
|
|
featureFlags: features,
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initializing DataSync is required to encrypt and send messages.
|
|
|
|
// With DataSync enabled, messages are added to the DataSync
|
|
|
|
// but actual encrypt and send calls are postponed.
|
|
|
|
// sendDataSync is responsible for encrypting and sending postponed messages.
|
|
|
|
if features.datasync {
|
|
|
|
ds.Init(p.sendDataSync)
|
|
|
|
ds.Start(300 * time.Millisecond)
|
|
|
|
}
|
|
|
|
|
|
|
|
return p, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *messageProcessor) Stop() {
|
|
|
|
p.datasync.Stop() // idempotent op
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *messageProcessor) SendPrivate(
|
|
|
|
ctx context.Context,
|
2019-10-14 14:10:48 +00:00
|
|
|
recipient *ecdsa.PublicKey,
|
2019-09-02 09:29:06 +00:00
|
|
|
chatID string,
|
|
|
|
data []byte,
|
|
|
|
clock int64,
|
2019-11-21 16:19:22 +00:00
|
|
|
) ([]byte, *v1protocol.Message, error) {
|
|
|
|
message := v1protocol.CreatePrivateTextMessage(data, clock, chatID)
|
2019-09-02 09:29:06 +00:00
|
|
|
encodedMessage, err := p.encodeMessage(message)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "failed to encode message")
|
|
|
|
}
|
2019-10-14 14:10:48 +00:00
|
|
|
messageID, err := p.sendPrivate(ctx, recipient, encodedMessage)
|
2019-09-02 09:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
return messageID, &message, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendPrivateRaw takes encoded data, encrypts it and sends through the wire.
|
|
|
|
func (p *messageProcessor) SendPrivateRaw(
|
|
|
|
ctx context.Context,
|
2019-10-14 14:10:48 +00:00
|
|
|
recipient *ecdsa.PublicKey,
|
2019-09-02 09:29:06 +00:00
|
|
|
data []byte,
|
|
|
|
) ([]byte, error) {
|
|
|
|
p.logger.Debug(
|
|
|
|
"sending a private message",
|
2019-10-14 14:10:48 +00:00
|
|
|
zap.Binary("public-key", crypto.FromECDSAPub(recipient)),
|
2019-09-02 09:29:06 +00:00
|
|
|
zap.String("site", "SendPrivateRaw"),
|
|
|
|
)
|
2019-10-14 14:10:48 +00:00
|
|
|
return p.sendPrivate(ctx, recipient, data)
|
2019-09-26 07:01:17 +00:00
|
|
|
}
|
2019-09-02 09:29:06 +00:00
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
// sendPrivate sends data to the recipient identifying with a given public key.
|
|
|
|
func (p *messageProcessor) sendPrivate(
|
|
|
|
ctx context.Context,
|
2019-10-14 14:10:48 +00:00
|
|
|
recipient *ecdsa.PublicKey,
|
2019-09-26 07:01:17 +00:00
|
|
|
data []byte,
|
|
|
|
) ([]byte, error) {
|
2019-10-14 14:10:48 +00:00
|
|
|
p.logger.Debug("sending private message", zap.Binary("recipient", crypto.FromECDSAPub(recipient)))
|
|
|
|
|
2019-09-02 09:29:06 +00:00
|
|
|
wrappedMessage, err := p.tryWrapMessageV1(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to wrap message")
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
messageID := v1protocol.MessageID(&p.identity.PublicKey, wrappedMessage)
|
2019-09-02 09:29:06 +00:00
|
|
|
|
|
|
|
if p.featureFlags.datasync {
|
2019-10-14 14:10:48 +00:00
|
|
|
if err := p.addToDataSync(recipient, wrappedMessage); err != nil {
|
2019-09-02 09:29:06 +00:00
|
|
|
return nil, errors.Wrap(err, "failed to send message with datasync")
|
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
|
|
|
|
// No need to call transport tracking.
|
|
|
|
// It is done in a data sync dispatch step.
|
2019-09-02 09:29:06 +00:00
|
|
|
} else {
|
2019-10-14 14:10:48 +00:00
|
|
|
messageSpec, err := p.protocol.BuildDirectMessage(p.identity, recipient, wrappedMessage)
|
2019-09-02 09:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to encrypt message")
|
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
hash, newMessage, err := p.sendMessageSpec(ctx, recipient, messageSpec)
|
2019-09-02 09:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to send a message spec")
|
|
|
|
}
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
p.transport.Track([][]byte{messageID}, hash, newMessage)
|
2019-09-02 09:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return messageID, nil
|
|
|
|
}
|
|
|
|
|
2019-10-14 14:10:48 +00:00
|
|
|
func (p *messageProcessor) SendGroup(
|
|
|
|
ctx context.Context,
|
|
|
|
recipients []*ecdsa.PublicKey,
|
|
|
|
chatID string,
|
|
|
|
data []byte,
|
|
|
|
clock int64,
|
2019-11-21 16:19:22 +00:00
|
|
|
) ([][]byte, []*v1protocol.Message, error) {
|
2019-10-14 14:10:48 +00:00
|
|
|
p.logger.Debug("sending a group message", zap.Int("membersCount", len(recipients)))
|
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
message := v1protocol.CreatePrivateGroupTextMessage(data, clock, chatID)
|
2019-10-14 14:10:48 +00:00
|
|
|
encodedMessage, err := p.encodeMessage(message)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, errors.Wrap(err, "failed to encode message")
|
|
|
|
}
|
|
|
|
|
|
|
|
var resultIDs [][]byte
|
|
|
|
for _, recipient := range recipients {
|
|
|
|
messageID, err := p.sendPrivate(ctx, recipient, encodedMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
resultIDs = append(resultIDs, messageID)
|
|
|
|
}
|
|
|
|
return resultIDs, nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *messageProcessor) SendMembershipUpdate(
|
|
|
|
ctx context.Context,
|
|
|
|
recipients []*ecdsa.PublicKey,
|
|
|
|
chatID string,
|
2019-11-21 16:19:22 +00:00
|
|
|
updates []v1protocol.MembershipUpdate,
|
2019-10-14 14:10:48 +00:00
|
|
|
clock int64,
|
|
|
|
) ([][]byte, error) {
|
|
|
|
p.logger.Debug("sending a membership update", zap.Int("membersCount", len(recipients)))
|
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
message := v1protocol.MembershipUpdateMessage{
|
2019-10-14 14:10:48 +00:00
|
|
|
ChatID: chatID,
|
|
|
|
Updates: updates,
|
|
|
|
}
|
2019-11-21 16:19:22 +00:00
|
|
|
encodedMessage, err := v1protocol.EncodeMembershipUpdateMessage(message)
|
2019-10-14 14:10:48 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to encode membership update message")
|
|
|
|
}
|
|
|
|
|
|
|
|
var resultIDs [][]byte
|
|
|
|
for _, recipient := range recipients {
|
|
|
|
messageID, err := p.sendPrivate(ctx, recipient, encodedMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
resultIDs = append(resultIDs, messageID)
|
|
|
|
}
|
|
|
|
return resultIDs, nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
func (p *messageProcessor) SendPublic(ctx context.Context, chatID string, data []byte, clock int64) ([]byte, error) {
|
2019-11-21 16:19:22 +00:00
|
|
|
message := v1protocol.CreatePublicTextMessage(data, clock, chatID)
|
2019-09-02 09:29:06 +00:00
|
|
|
|
|
|
|
encodedMessage, err := p.encodeMessage(message)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to encode message")
|
|
|
|
}
|
|
|
|
|
|
|
|
wrappedMessage, err := p.tryWrapMessageV1(encodedMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to wrap message")
|
|
|
|
}
|
|
|
|
|
|
|
|
messageSpec, err := p.protocol.BuildPublicMessage(p.identity, wrappedMessage)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to build public message")
|
|
|
|
}
|
|
|
|
|
|
|
|
newMessage, err := messageSpecToWhisper(messageSpec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
hash, err := p.transport.SendPublic(ctx, newMessage, chatID)
|
2019-09-02 09:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
messageID := v1protocol.MessageID(&p.identity.PublicKey, wrappedMessage)
|
2019-09-02 09:29:06 +00:00
|
|
|
|
|
|
|
p.transport.Track([][]byte{messageID}, hash, newMessage)
|
|
|
|
|
|
|
|
return messageID, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SendPublicRaw takes encoded data, encrypts it and sends through the wire.
|
|
|
|
func (p *messageProcessor) SendPublicRaw(ctx context.Context, chatName string, data []byte) ([]byte, error) {
|
2019-11-23 17:57:05 +00:00
|
|
|
var newMessage *types.NewMessage
|
2019-09-02 09:29:06 +00:00
|
|
|
|
|
|
|
wrappedMessage, err := p.tryWrapMessageV1(data)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to wrap message")
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
newMessage = &types.NewMessage{
|
2019-09-02 09:29:06 +00:00
|
|
|
TTL: whisperTTL,
|
|
|
|
Payload: wrappedMessage,
|
|
|
|
PowTarget: whisperPoW,
|
|
|
|
PowTime: whisperPoWTime,
|
|
|
|
}
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
hash, err := p.transport.SendPublic(ctx, newMessage, chatName)
|
2019-09-02 09:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
messageID := v1protocol.MessageID(&p.identity.PublicKey, wrappedMessage)
|
2019-09-02 09:29:06 +00:00
|
|
|
|
|
|
|
p.transport.Track([][]byte{messageID}, hash, newMessage)
|
|
|
|
|
|
|
|
return messageID, nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
// Process processes received Whisper messages through all the layers
|
|
|
|
// and returns decoded user messages.
|
|
|
|
// It also handled all non-user messages like PairMessage.
|
2019-11-23 17:57:05 +00:00
|
|
|
func (p *messageProcessor) Process(shhMessage *types.Message) ([]*v1protocol.Message, error) {
|
2019-09-26 07:01:17 +00:00
|
|
|
logger := p.logger.With(zap.String("site", "Process"))
|
2019-09-02 09:29:06 +00:00
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
var decodedMessages []*v1protocol.Message
|
2019-09-02 09:29:06 +00:00
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
hlogger := logger.With(zap.Binary("hash", shhMessage.Hash))
|
|
|
|
hlogger.Debug("handling a received message")
|
|
|
|
|
|
|
|
statusMessages, err := p.handleMessages(shhMessage, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, statusMessage := range statusMessages {
|
|
|
|
switch m := statusMessage.ParsedMessage.(type) {
|
2019-11-21 16:19:22 +00:00
|
|
|
case v1protocol.Message:
|
2019-09-26 07:01:17 +00:00
|
|
|
m.ID = statusMessage.ID
|
|
|
|
m.SigPubKey = statusMessage.SigPubKey()
|
|
|
|
decodedMessages = append(decodedMessages, &m)
|
2019-11-21 16:19:22 +00:00
|
|
|
case v1protocol.MembershipUpdateMessage:
|
2019-10-14 14:10:48 +00:00
|
|
|
// Handle user message that can be attached to the membership update.
|
|
|
|
userMessage := m.Message
|
|
|
|
if userMessage != nil {
|
|
|
|
userMessage.ID = statusMessage.ID
|
|
|
|
userMessage.SigPubKey = statusMessage.SigPubKey()
|
|
|
|
decodedMessages = append(decodedMessages, userMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := p.processMembershipUpdate(m); err != nil {
|
|
|
|
hlogger.Error("failed to process MembershipUpdateMessage", zap.Error(err))
|
|
|
|
}
|
2019-11-21 16:19:22 +00:00
|
|
|
case v1protocol.PairMessage:
|
2019-09-26 07:01:17 +00:00
|
|
|
fromOurDevice := isPubKeyEqual(statusMessage.SigPubKey(), &p.identity.PublicKey)
|
|
|
|
if !fromOurDevice {
|
|
|
|
hlogger.Debug("received PairMessage from not our device, skipping")
|
|
|
|
break
|
|
|
|
}
|
2019-09-02 09:29:06 +00:00
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
if err := p.processPairMessage(m); err != nil {
|
|
|
|
hlogger.Error("failed to process PairMessage", zap.Error(err))
|
2019-09-02 09:29:06 +00:00
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
default:
|
2019-11-04 10:08:22 +00:00
|
|
|
hlogger.Error(
|
|
|
|
"skipped a public message of unsupported type",
|
2019-10-28 13:50:33 +00:00
|
|
|
zap.Any("value", statusMessage.ParsedMessage),
|
2019-11-04 10:08:22 +00:00
|
|
|
)
|
2019-09-02 09:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
2019-09-26 07:01:17 +00:00
|
|
|
|
2019-09-02 09:29:06 +00:00
|
|
|
return decodedMessages, nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
func (p *messageProcessor) processMembershipUpdate(m v1protocol.MembershipUpdateMessage) error {
|
2019-10-14 14:10:48 +00:00
|
|
|
if err := m.Verify(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if p.handler != nil {
|
|
|
|
return p.handler.HandleMembershipUpdate(m)
|
|
|
|
}
|
|
|
|
return errors.New("missing handler")
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
func (p *messageProcessor) processPairMessage(m v1protocol.PairMessage) error {
|
2019-09-26 07:01:17 +00:00
|
|
|
metadata := &multidevice.InstallationMetadata{
|
|
|
|
Name: m.Name,
|
|
|
|
FCMToken: m.FCMToken,
|
|
|
|
DeviceType: m.DeviceType,
|
|
|
|
}
|
|
|
|
return p.protocol.SetInstallationMetadata(&p.identity.PublicKey, m.InstallationID, metadata)
|
|
|
|
}
|
|
|
|
|
2019-09-02 09:29:06 +00:00
|
|
|
// handleMessages expects a whisper message as input, and it will go through
|
|
|
|
// a series of transformations until the message is parsed into an application
|
|
|
|
// layer message, or in case of Raw methods, the processing stops at the layer
|
2019-09-26 07:01:17 +00:00
|
|
|
// before.
|
|
|
|
// It returns an error only if the processing of required steps failed.
|
2019-11-23 17:57:05 +00:00
|
|
|
func (p *messageProcessor) handleMessages(shhMessage *types.Message, applicationLayer bool) ([]*v1protocol.StatusMessage, error) {
|
2019-09-02 09:29:06 +00:00
|
|
|
logger := p.logger.With(zap.String("site", "handleMessages"))
|
|
|
|
hlogger := logger.With(zap.Binary("hash", shhMessage.Hash))
|
2019-11-21 16:19:22 +00:00
|
|
|
var statusMessage v1protocol.StatusMessage
|
2019-09-02 09:29:06 +00:00
|
|
|
|
|
|
|
err := statusMessage.HandleTransport(shhMessage)
|
|
|
|
if err != nil {
|
|
|
|
hlogger.Error("failed to handle transport layer message", zap.Error(err))
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = p.handleEncryptionLayer(context.Background(), &statusMessage)
|
|
|
|
if err != nil {
|
|
|
|
hlogger.Debug("failed to handle an encryption message", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
statusMessages, err := statusMessage.HandleDatasync(p.datasync)
|
|
|
|
if err != nil {
|
|
|
|
hlogger.Debug("failed to handle datasync message", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, statusMessage := range statusMessages {
|
|
|
|
err := statusMessage.HandleApplicationMetadata()
|
|
|
|
if err != nil {
|
|
|
|
hlogger.Error("failed to handle application metadata layer message", zap.Error(err))
|
|
|
|
}
|
|
|
|
|
|
|
|
if applicationLayer {
|
|
|
|
err = statusMessage.HandleApplication()
|
|
|
|
if err != nil {
|
2019-11-04 10:08:22 +00:00
|
|
|
hlogger.Error("failed to handle application layer message", zap.Error(err))
|
2019-09-02 09:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return statusMessages, nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
func (p *messageProcessor) handleEncryptionLayer(ctx context.Context, message *v1protocol.StatusMessage) error {
|
2019-09-02 09:29:06 +00:00
|
|
|
logger := p.logger.With(zap.String("site", "handleEncryptionLayer"))
|
|
|
|
publicKey := message.SigPubKey()
|
|
|
|
|
|
|
|
err := message.HandleEncryption(p.identity, publicKey, p.protocol)
|
|
|
|
if err == encryption.ErrDeviceNotFound {
|
2019-11-04 10:08:22 +00:00
|
|
|
if err := p.handleErrDeviceNotFound(ctx, publicKey); err != nil {
|
|
|
|
logger.Error("failed to handle ErrDeviceNotFound", zap.Error(err))
|
2019-09-02 09:29:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to process an encrypted message")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *messageProcessor) handleErrDeviceNotFound(ctx context.Context, publicKey *ecdsa.PublicKey) error {
|
|
|
|
now := time.Now().Unix()
|
|
|
|
advertise, err := p.protocol.ShouldAdvertiseBundle(publicKey, now)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !advertise {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
messageSpec, err := p.protocol.BuildBundleAdvertiseMessage(p.identity, publicKey)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Second)
|
|
|
|
defer cancel()
|
|
|
|
_, _, err = p.sendMessageSpec(ctx, publicKey, messageSpec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.protocol.ConfirmBundleAdvertisement(publicKey, now)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 16:19:22 +00:00
|
|
|
func (p *messageProcessor) encodeMessage(message v1protocol.Message) ([]byte, error) {
|
|
|
|
encodedMessage, err := v1protocol.EncodeMessage(message)
|
2019-09-02 09:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to encode message")
|
|
|
|
}
|
|
|
|
return encodedMessage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *messageProcessor) tryWrapMessageV1(encodedMessage []byte) ([]byte, error) {
|
|
|
|
if p.featureFlags.sendV1Messages {
|
2019-11-21 16:19:22 +00:00
|
|
|
wrappedMessage, err := v1protocol.WrapMessageV1(encodedMessage, p.identity)
|
2019-09-02 09:29:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to wrap message")
|
|
|
|
}
|
|
|
|
return wrappedMessage, nil
|
|
|
|
}
|
|
|
|
return encodedMessage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *messageProcessor) addToDataSync(publicKey *ecdsa.PublicKey, message []byte) error {
|
|
|
|
groupID := datasync.ToOneToOneGroupID(&p.identity.PublicKey, publicKey)
|
|
|
|
peerID := datasyncpeer.PublicKeyToPeerID(*publicKey)
|
|
|
|
exist, err := p.datasync.IsPeerInGroup(groupID, peerID)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to check if peer is in group")
|
|
|
|
}
|
|
|
|
if !exist {
|
|
|
|
if err := p.datasync.AddPeer(groupID, peerID); err != nil {
|
|
|
|
return errors.Wrap(err, "failed to add peer")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
_, err = p.datasync.AppendMessage(groupID, message)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to append message to datasync")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// sendDataSync sends a message scheduled by the data sync layer.
|
2019-09-26 07:01:17 +00:00
|
|
|
// Data Sync layer calls this method "dispatch" function.
|
2019-09-02 09:29:06 +00:00
|
|
|
func (p *messageProcessor) sendDataSync(ctx context.Context, publicKey *ecdsa.PublicKey, encodedMessage []byte, payload *datasyncproto.Payload) error {
|
|
|
|
messageIDs := make([][]byte, 0, len(payload.Messages))
|
|
|
|
for _, payload := range payload.Messages {
|
2019-11-21 16:19:22 +00:00
|
|
|
messageIDs = append(messageIDs, v1protocol.MessageID(&p.identity.PublicKey, payload.Body))
|
2019-09-02 09:29:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
messageSpec, err := p.protocol.BuildDirectMessage(p.identity, publicKey, encodedMessage)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to encrypt message")
|
|
|
|
}
|
|
|
|
|
|
|
|
hash, newMessage, err := p.sendMessageSpec(ctx, publicKey, messageSpec)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
p.transport.Track(messageIDs, hash, newMessage)
|
2019-09-02 09:29:06 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-26 07:01:17 +00:00
|
|
|
// sendMessageSpec analyses the spec properties and selects a proper transport method.
|
2019-11-23 17:57:05 +00:00
|
|
|
func (p *messageProcessor) sendMessageSpec(ctx context.Context, publicKey *ecdsa.PublicKey, messageSpec *encryption.ProtocolMessageSpec) ([]byte, *types.NewMessage, error) {
|
2019-09-02 09:29:06 +00:00
|
|
|
newMessage, err := messageSpecToWhisper(messageSpec)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
logger := p.logger.With(zap.String("site", "sendMessageSpec"))
|
|
|
|
|
|
|
|
var hash []byte
|
|
|
|
|
|
|
|
switch {
|
|
|
|
case messageSpec.SharedSecret != nil:
|
|
|
|
logger.Debug("sending using shared secret")
|
2019-10-09 14:22:53 +00:00
|
|
|
hash, err = p.transport.SendPrivateWithSharedSecret(ctx, newMessage, publicKey, messageSpec.SharedSecret)
|
2019-09-02 09:29:06 +00:00
|
|
|
case messageSpec.PartitionedTopicMode() == encryption.PartitionTopicV1:
|
|
|
|
logger.Debug("sending partitioned topic")
|
2019-10-09 14:22:53 +00:00
|
|
|
hash, err = p.transport.SendPrivateWithPartitioned(ctx, newMessage, publicKey)
|
2019-09-02 09:29:06 +00:00
|
|
|
case !p.featureFlags.genericDiscoveryTopicEnabled:
|
|
|
|
logger.Debug("sending partitioned topic (generic discovery topic disabled)")
|
2019-10-09 14:22:53 +00:00
|
|
|
hash, err = p.transport.SendPrivateWithPartitioned(ctx, newMessage, publicKey)
|
2019-09-02 09:29:06 +00:00
|
|
|
default:
|
|
|
|
logger.Debug("sending using discovery topic")
|
2019-10-09 14:22:53 +00:00
|
|
|
hash, err = p.transport.SendPrivateOnDiscovery(ctx, newMessage, publicKey)
|
2019-09-02 09:29:06 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2019-10-09 14:22:53 +00:00
|
|
|
return hash, newMessage, nil
|
2019-09-02 09:29:06 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func messageSpecToWhisper(spec *encryption.ProtocolMessageSpec) (*types.NewMessage, error) {
|
|
|
|
var newMessage *types.NewMessage
|
2019-09-02 09:29:06 +00:00
|
|
|
|
|
|
|
payload, err := proto.Marshal(spec.Message)
|
|
|
|
if err != nil {
|
|
|
|
return newMessage, err
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
newMessage = &types.NewMessage{
|
2019-09-02 09:29:06 +00:00
|
|
|
TTL: whisperTTL,
|
|
|
|
Payload: payload,
|
|
|
|
PowTarget: whisperPoW,
|
|
|
|
PowTime: whisperPoWTime,
|
|
|
|
}
|
|
|
|
return newMessage, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// isPubKeyEqual checks that two public keys are equal
|
|
|
|
func isPubKeyEqual(a, b *ecdsa.PublicKey) bool {
|
|
|
|
// the curve is always the same, just compare the points
|
|
|
|
return a.X.Cmp(b.X) == 0 && a.Y.Cmp(b.Y) == 0
|
|
|
|
}
|