2019-11-21 16:19:22 +00:00
|
|
|
package protocol
|
2019-07-26 07:17:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"log"
|
|
|
|
|
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/jinzhu/copier"
|
2019-09-26 07:01:17 +00:00
|
|
|
"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/applicationmetadata"
|
|
|
|
"github.com/status-im/status-go/protocol/datasync"
|
|
|
|
"github.com/status-im/status-go/protocol/encryption"
|
2019-07-26 07:17:29 +00:00
|
|
|
)
|
|
|
|
|
2019-11-06 16:23:11 +00:00
|
|
|
type StatusMessageT int
|
|
|
|
|
|
|
|
const (
|
|
|
|
MessageT StatusMessageT = iota + 1
|
|
|
|
MembershipUpdateMessageT
|
|
|
|
PairMessageT
|
|
|
|
)
|
|
|
|
|
2019-07-26 07:17:29 +00:00
|
|
|
// StatusMessage is any Status Protocol message.
|
|
|
|
type StatusMessage struct {
|
2019-09-26 07:01:17 +00:00
|
|
|
// TransportMessage is the parsed message received from the transport layer, i.e the input
|
2019-11-23 17:57:05 +00:00
|
|
|
TransportMessage *types.Message
|
2019-11-06 16:23:11 +00:00
|
|
|
// MessageType is the type of application message contained
|
|
|
|
MessageType StatusMessageT
|
2019-07-26 07:17:29 +00:00
|
|
|
// ParsedMessage is the parsed message by the application layer, i.e the output
|
|
|
|
ParsedMessage interface{}
|
|
|
|
|
|
|
|
// TransportPayload is the payload as received from the transport layer
|
|
|
|
TransportPayload []byte
|
|
|
|
// DecryptedPayload is the payload after having been processed by the encryption layer
|
|
|
|
DecryptedPayload []byte
|
|
|
|
|
|
|
|
// ID is the canonical ID of the message
|
2019-11-23 17:57:05 +00:00
|
|
|
ID types.HexBytes
|
2019-07-26 07:17:29 +00:00
|
|
|
// Hash is the transport layer hash
|
|
|
|
Hash []byte
|
|
|
|
|
|
|
|
// TransportLayerSigPubKey contains the public key provided by the transport layer
|
|
|
|
TransportLayerSigPubKey *ecdsa.PublicKey
|
|
|
|
// ApplicationMetadataLayerPubKey contains the public key provided by the application metadata layer
|
|
|
|
ApplicationMetadataLayerSigPubKey *ecdsa.PublicKey
|
|
|
|
}
|
|
|
|
|
|
|
|
// SigPubKey returns the most important signature, from the application layer to transport
|
2019-11-23 17:57:05 +00:00
|
|
|
func (m *StatusMessage) SigPubKey() *ecdsa.PublicKey {
|
|
|
|
if m.ApplicationMetadataLayerSigPubKey != nil {
|
|
|
|
return m.ApplicationMetadataLayerSigPubKey
|
2019-07-26 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
return m.TransportLayerSigPubKey
|
2019-07-26 07:17:29 +00:00
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (m *StatusMessage) Clone() (*StatusMessage, error) {
|
2019-07-26 07:17:29 +00:00
|
|
|
copy := &StatusMessage{}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
err := copier.Copy(©, m)
|
2019-07-26 07:17:29 +00:00
|
|
|
return copy, err
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:57:05 +00:00
|
|
|
func (m *StatusMessage) HandleTransport(shhMessage *types.Message) error {
|
2019-07-26 07:17:29 +00:00
|
|
|
publicKey, err := crypto.UnmarshalPubkey(shhMessage.Sig)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to get signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.TransportMessage = shhMessage
|
|
|
|
m.Hash = shhMessage.Hash
|
|
|
|
m.TransportLayerSigPubKey = publicKey
|
|
|
|
m.TransportPayload = shhMessage.Payload
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StatusMessage) HandleEncryption(myKey *ecdsa.PrivateKey, senderKey *ecdsa.PublicKey, enc *encryption.Protocol) error {
|
|
|
|
// As we handle non-encrypted messages, we make sure that DecryptPayload
|
|
|
|
// is set regardless of whether this step is successful
|
|
|
|
m.DecryptedPayload = m.TransportPayload
|
|
|
|
|
|
|
|
var protocolMessage encryption.ProtocolMessage
|
|
|
|
err := proto.Unmarshal(m.TransportPayload, &protocolMessage)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to unmarshal ProtocolMessage")
|
|
|
|
}
|
|
|
|
|
|
|
|
payload, err := enc.HandleMessage(
|
|
|
|
myKey,
|
|
|
|
senderKey,
|
|
|
|
&protocolMessage,
|
|
|
|
m.Hash,
|
|
|
|
)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "failed to handle Encryption message")
|
|
|
|
}
|
|
|
|
|
|
|
|
m.DecryptedPayload = payload
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-09-02 09:29:06 +00:00
|
|
|
// HandleDatasync processes StatusMessage through data sync layer.
|
|
|
|
// This is optional and DataSync might be nil. In such a case,
|
|
|
|
// only one payload will be returned equal to DecryptedPayload.
|
2019-07-26 07:17:29 +00:00
|
|
|
func (m *StatusMessage) HandleDatasync(datasync *datasync.DataSync) ([]*StatusMessage, error) {
|
|
|
|
var statusMessages []*StatusMessage
|
2019-09-02 09:29:06 +00:00
|
|
|
|
2019-07-26 07:17:29 +00:00
|
|
|
payloads := datasync.Handle(
|
|
|
|
m.SigPubKey(),
|
|
|
|
m.DecryptedPayload,
|
|
|
|
)
|
2019-09-02 09:29:06 +00:00
|
|
|
|
2019-07-26 07:17:29 +00:00
|
|
|
for _, payload := range payloads {
|
|
|
|
message, err := m.Clone()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
message.DecryptedPayload = payload
|
|
|
|
statusMessages = append(statusMessages, message)
|
|
|
|
}
|
|
|
|
return statusMessages, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StatusMessage) HandleApplicationMetadata() error {
|
|
|
|
message, err := applicationmetadata.Unmarshal(m.DecryptedPayload)
|
|
|
|
// Not an applicationmetadata message, calculate ID using the previous
|
|
|
|
// signature
|
|
|
|
if err != nil {
|
|
|
|
m.ID = MessageID(m.SigPubKey(), m.DecryptedPayload)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
recoveredKey, err := message.RecoverKey()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.ApplicationMetadataLayerSigPubKey = recoveredKey
|
2019-08-27 12:04:15 +00:00
|
|
|
// Calculate ID using the wrapped record
|
|
|
|
m.ID = MessageID(recoveredKey, m.DecryptedPayload)
|
2019-07-26 07:17:29 +00:00
|
|
|
m.DecryptedPayload = message.Payload
|
|
|
|
return nil
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *StatusMessage) HandleApplication() error {
|
|
|
|
value, err := decodeTransitMessage(m.DecryptedPayload)
|
|
|
|
if err != nil {
|
2019-10-28 13:50:33 +00:00
|
|
|
log.Printf("[message::DecodeMessage] could not decode message: %#x, err: %v", m.Hash, err.Error())
|
2019-07-26 07:17:29 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
m.ParsedMessage = value
|
2019-11-06 16:23:11 +00:00
|
|
|
switch m.ParsedMessage.(type) {
|
|
|
|
case Message:
|
|
|
|
m.MessageType = MessageT
|
|
|
|
case MembershipUpdateMessage:
|
|
|
|
m.MessageType = MembershipUpdateMessageT
|
|
|
|
case PairMessage:
|
|
|
|
m.MessageType = PairMessageT
|
|
|
|
// By default we null the parsed message field, as
|
|
|
|
// otherwise is populated with the raw transit and we are
|
|
|
|
// unable to marshal in case it contains maps
|
|
|
|
// as they have type map[interface{}]interface{}
|
|
|
|
default:
|
|
|
|
m.ParsedMessage = nil
|
2019-07-26 07:17:29 +00:00
|
|
|
|
2019-11-06 16:23:11 +00:00
|
|
|
}
|
2019-07-26 07:17:29 +00:00
|
|
|
return nil
|
|
|
|
}
|