status-go/protocol/v1/status_message.go

351 lines
14 KiB
Go
Raw Normal View History

package protocol
import (
"crypto/ecdsa"
"encoding/json"
"reflect"
"github.com/golang/protobuf/proto"
"github.com/jinzhu/copier"
"github.com/pkg/errors"
2020-01-02 09:10:19 +00:00
"github.com/ethereum/go-ethereum/common/hexutil"
"github.com/ethereum/go-ethereum/log"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol/encryption"
2020-07-31 12:22:05 +00:00
"github.com/status-im/status-go/protocol/encryption/multidevice"
"github.com/status-im/status-go/protocol/encryption/sharedsecret"
"github.com/status-im/status-go/protocol/protobuf"
)
type StatusMessageT int
// StatusMessage is any Status Protocol message.
type StatusMessage struct {
// TransportMessage is the parsed message received from the transport layer, i.e the input
TransportMessage *types.Message `json:"transportMessage"`
// Type is the type of application message contained
Type protobuf.ApplicationMetadataMessage_Type `json:"-"`
// ParsedMessage is the parsed message by the application layer, i.e the output
ParsedMessage *reflect.Value `json:"-"`
// TransportPayload is the payload as received from the transport layer
TransportPayload []byte `json:"-"`
// DecryptedPayload is the payload after having been processed by the encryption layer
DecryptedPayload []byte `json:"decryptedPayload"`
// UnwrappedPayload is the payload after having been unwrapped from the applicaition metadata layer
UnwrappedPayload []byte `json:"unwrappedPayload"`
// ID is the canonical ID of the message
ID types.HexBytes `json:"id"`
// Hash is the transport layer hash
Hash []byte `json:"-"`
2020-07-03 10:08:47 +00:00
// Dst is the targeted public key
Dst *ecdsa.PublicKey
// TransportLayerSigPubKey contains the public key provided by the transport layer
TransportLayerSigPubKey *ecdsa.PublicKey `json:"-"`
// ApplicationMetadataLayerPubKey contains the public key provided by the application metadata layer
ApplicationMetadataLayerSigPubKey *ecdsa.PublicKey `json:"-"`
2020-07-31 12:22:05 +00:00
// Installations is the new installations returned by the encryption layer
Installations []*multidevice.Installation
// SharedSecret is the shared secret returned by the encryption layer
SharedSecrets []*sharedsecret.Secret
2022-09-21 16:05:29 +00:00
// HashRatchetInfo is the information about a new hash ratchet group/key pair
HashRatchetInfo []*encryption.HashRatchetInfo
}
// Temporary JSON marshaling for those messages that are not yet processed
// by the go code
func (m *StatusMessage) MarshalJSON() ([]byte, error) {
item := struct {
ID types.HexBytes `json:"id"`
Payload string `json:"payload"`
From types.HexBytes `json:"from"`
Timestamp uint32 `json:"timestamp"`
}{
ID: m.ID,
Payload: string(m.UnwrappedPayload),
Timestamp: m.TransportMessage.Timestamp,
From: m.TransportMessage.Sig,
}
return json.Marshal(item)
}
// SigPubKey returns the most important signature, from the application layer to transport
func (m *StatusMessage) SigPubKey() *ecdsa.PublicKey {
if m.ApplicationMetadataLayerSigPubKey != nil {
return m.ApplicationMetadataLayerSigPubKey
}
return m.TransportLayerSigPubKey
}
func (m *StatusMessage) Clone() (*StatusMessage, error) {
copy := &StatusMessage{}
err := copier.Copy(&copy, m)
return copy, err
}
func (m *StatusMessage) HandleTransport(shhMessage *types.Message) error {
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
2020-07-03 10:08:47 +00:00
if shhMessage.Dst != nil {
publicKey, err := crypto.UnmarshalPubkey(shhMessage.Dst)
if err != nil {
return err
}
m.Dst = publicKey
}
return nil
}
func (m *StatusMessage) HandleEncryption(myKey *ecdsa.PrivateKey, senderKey *ecdsa.PublicKey, enc *encryption.Protocol, skipNegotiation bool) 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
// Nothing to do
if skipNegotiation {
return nil
}
var protocolMessage encryption.ProtocolMessage
err := proto.Unmarshal(m.TransportPayload, &protocolMessage)
if err != nil {
return errors.Wrap(err, "failed to unmarshal ProtocolMessage")
}
2020-07-31 12:22:05 +00:00
response, err := enc.HandleMessage(
myKey,
senderKey,
&protocolMessage,
m.Hash,
)
2022-09-21 16:05:29 +00:00
if err == encryption.ErrHashRatchetGroupIDNotFound {
if response != nil {
m.HashRatchetInfo = response.HashRatchetInfo
}
return err
}
if err != nil {
return errors.Wrap(err, "failed to handle Encryption message")
}
2020-07-31 12:22:05 +00:00
m.DecryptedPayload = response.DecryptedMessage
m.Installations = response.Installations
m.SharedSecrets = response.SharedSecrets
2022-09-21 16:05:29 +00:00
m.HashRatchetInfo = response.HashRatchetInfo
return nil
}
func (m *StatusMessage) HandleApplicationMetadata() error {
message, err := protobuf.Unmarshal(m.DecryptedPayload)
if err != nil {
return err
}
recoveredKey, err := message.RecoverKey()
if err != nil {
return err
}
m.ApplicationMetadataLayerSigPubKey = recoveredKey
// Calculate ID using the wrapped record
m.ID = MessageID(recoveredKey, m.DecryptedPayload)
log.Debug("calculated ID for envelope", "envelopeHash", hexutil.Encode(m.Hash), "messageId", hexutil.Encode(m.ID))
m.UnwrappedPayload = message.Payload
m.Type = message.Type
return nil
}
func (m *StatusMessage) HandleApplication() error {
switch m.Type {
case protobuf.ApplicationMetadataMessage_CHAT_MESSAGE:
return m.unmarshalProtobufData(new(protobuf.ChatMessage))
case protobuf.ApplicationMetadataMessage_MEMBERSHIP_UPDATE_MESSAGE:
return m.unmarshalProtobufData(new(protobuf.MembershipUpdateMessage))
case protobuf.ApplicationMetadataMessage_ACCEPT_REQUEST_ADDRESS_FOR_TRANSACTION:
return m.unmarshalProtobufData(new(protobuf.AcceptRequestAddressForTransaction))
case protobuf.ApplicationMetadataMessage_SEND_TRANSACTION:
return m.unmarshalProtobufData(new(protobuf.SendTransaction))
case protobuf.ApplicationMetadataMessage_REQUEST_TRANSACTION:
return m.unmarshalProtobufData(new(protobuf.RequestTransaction))
case protobuf.ApplicationMetadataMessage_DECLINE_REQUEST_ADDRESS_FOR_TRANSACTION:
return m.unmarshalProtobufData(new(protobuf.DeclineRequestAddressForTransaction))
case protobuf.ApplicationMetadataMessage_DECLINE_REQUEST_TRANSACTION:
return m.unmarshalProtobufData(new(protobuf.DeclineRequestTransaction))
case protobuf.ApplicationMetadataMessage_REQUEST_ADDRESS_FOR_TRANSACTION:
return m.unmarshalProtobufData(new(protobuf.RequestAddressForTransaction))
case protobuf.ApplicationMetadataMessage_CONTACT_UPDATE:
return m.unmarshalProtobufData(new(protobuf.ContactUpdate))
case protobuf.ApplicationMetadataMessage_PIN_MESSAGE:
return m.unmarshalProtobufData(new(protobuf.PinMessage))
case protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION:
return m.unmarshalProtobufData(new(protobuf.SyncInstallation))
case protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION_CONTACT:
log.Debug("Sync installation contact")
return m.unmarshalProtobufData(new(protobuf.SyncInstallationContactV2))
case protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION_PUBLIC_CHAT:
return m.unmarshalProtobufData(new(protobuf.SyncInstallationPublicChat))
case protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION_ACCOUNT:
return m.unmarshalProtobufData(new(protobuf.SyncInstallationAccount))
2022-03-24 09:35:56 +00:00
case protobuf.ApplicationMetadataMessage_SYNC_PROFILE_PICTURE:
return m.unmarshalProtobufData(new(protobuf.SyncProfilePictures))
case protobuf.ApplicationMetadataMessage_PAIR_INSTALLATION:
return m.unmarshalProtobufData(new(protobuf.PairInstallation))
case protobuf.ApplicationMetadataMessage_SYNC_INSTALLATION_COMMUNITY:
return m.unmarshalProtobufData(new(protobuf.SyncCommunity))
2020-07-03 08:26:35 +00:00
case protobuf.ApplicationMetadataMessage_CONTACT_CODE_ADVERTISEMENT:
return m.unmarshalProtobufData(new(protobuf.ContactCodeAdvertisement))
2020-07-03 08:26:35 +00:00
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_REQUEST:
return m.unmarshalProtobufData(new(protobuf.PushNotificationRequest))
2020-07-03 08:26:35 +00:00
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_REGISTRATION_RESPONSE:
return m.unmarshalProtobufData(new(protobuf.PushNotificationRegistrationResponse))
2020-07-03 08:26:35 +00:00
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_QUERY:
return m.unmarshalProtobufData(new(protobuf.PushNotificationQuery))
2020-07-03 08:26:35 +00:00
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_QUERY_RESPONSE:
return m.unmarshalProtobufData(new(protobuf.PushNotificationQueryResponse))
2020-07-03 08:26:35 +00:00
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_RESPONSE:
return m.unmarshalProtobufData(new(protobuf.PushNotificationResponse))
case protobuf.ApplicationMetadataMessage_EMOJI_REACTION:
return m.unmarshalProtobufData(new(protobuf.EmojiReaction))
2020-08-07 13:49:37 +00:00
case protobuf.ApplicationMetadataMessage_GROUP_CHAT_INVITATION:
return m.unmarshalProtobufData(new(protobuf.GroupChatInvitation))
case protobuf.ApplicationMetadataMessage_COMMUNITY_DESCRIPTION:
return m.unmarshalProtobufData(new(protobuf.CommunityDescription))
case protobuf.ApplicationMetadataMessage_COMMUNITY_INVITATION:
return m.unmarshalProtobufData(new(protobuf.CommunityInvitation))
2021-01-11 10:32:51 +00:00
case protobuf.ApplicationMetadataMessage_COMMUNITY_REQUEST_TO_JOIN:
return m.unmarshalProtobufData(new(protobuf.CommunityRequestToJoin))
case protobuf.ApplicationMetadataMessage_COMMUNITY_REQUEST_TO_JOIN_RESPONSE:
return m.unmarshalProtobufData(new(protobuf.CommunityRequestToJoinResponse))
case protobuf.ApplicationMetadataMessage_COMMUNITY_CANCEL_REQUEST_TO_JOIN:
return m.unmarshalProtobufData(new(protobuf.CommunityCancelRequestToJoin))
case protobuf.ApplicationMetadataMessage_COMMUNITY_REQUEST_TO_LEAVE:
return m.unmarshalProtobufData(new(protobuf.CommunityRequestToLeave))
2021-06-07 08:31:27 +00:00
case protobuf.ApplicationMetadataMessage_EDIT_MESSAGE:
return m.unmarshalProtobufData(new(protobuf.EditMessage))
case protobuf.ApplicationMetadataMessage_DELETE_MESSAGE:
return m.unmarshalProtobufData(new(protobuf.DeleteMessage))
2022-09-28 11:42:17 +00:00
case protobuf.ApplicationMetadataMessage_SYNC_DELETE_FOR_ME_MESSAGE:
return m.unmarshalProtobufData(new(protobuf.DeleteForMeMessage))
case protobuf.ApplicationMetadataMessage_STATUS_UPDATE:
return m.unmarshalProtobufData(new(protobuf.StatusUpdate))
case protobuf.ApplicationMetadataMessage_PUSH_NOTIFICATION_REGISTRATION:
// This message is a bit different as it's encrypted, so we pass it straight through
v := reflect.ValueOf(m.UnwrappedPayload)
m.ParsedMessage = &v
return nil
case protobuf.ApplicationMetadataMessage_CHAT_IDENTITY:
return m.unmarshalProtobufData(new(protobuf.ChatIdentity))
Anon Metrics Broadcast (#2198) * Protobufs and adapters * Added basic anon metric service and config init * Added fibonacci interval incrementer * Added basic Client.Start func and integrated interval incrementer * Added new processed field to app metrics table * Added id column to app metrics table * Added migration clean up * Added appmetrics GetUnprocessed and SetToProcessedByIDs and tests There was a wierd bug where metrics in the db that did not explicitly insert a value would be NULL, so could not be found by . In addition I've added a new primary id field to the app_metrics table so that updates could be done against very specific metric rows. * Updated adaptors and db to handle proto_id I need a way to distinguish individual metric items from each other so that I can ignore the ones that have been seen before. * Moved incrementer into dedicated file * Resolve incrementer test fail * Finalised the main loop functionality * Implemented delete loop framework * Updated adaptors file name * Added delete loop delay and quit, and tweak on RawMessage gen * Completed delete loop logic * Added DBLock to prevent deletion during mainLoop * Added postgres DB connection, integrated into anonmetrics.Server * Removed proto_id from SQL migration and model * Integrated postgres with Server and updated adaptors * Function name update * Added sample config files for client and server * Fixes and testing for low level e2e * make generate * Fix lint * Fix for receiving an anonMetricBatch not in server mode * Postgres test fixes * Tidy up, make vendor and make generate * delinting * Fixing database tests * Attempted fix of does: cannot open `does' (No such file or directory) not: cannot open `not' (No such file or directory) exist: cannot open `exist' (No such file or directory) error on sql resource loas * Moved all anon metric postgres migration logic and sources into a the protocol/anonmetrics package or sub packages. I don't know if this will fix the does: cannot open `does' (No such file or directory) not: cannot open `not' (No such file or directory) exist: cannot open `exist' (No such file or directory) error that happens in Jenkins but this could work * Lint for the lint god * Why doesn't the linter list all its problems at once? * test tweaks * Fix for wakuV2 change * DB reset change * Fix for postgres db migrations fails * More robust implementation of postgres test setup and teardown * Added block for anon metrics functionality * Version Bump to 0.84.0 * Added test to check anon metrics broadcast is deactivated * Protobufs and adapters * Added basic anon metric service and config init * Added new processed field to app metrics table * Added id column to app metrics table * Added migration clean up * Added appmetrics GetUnprocessed and SetToProcessedByIDs and tests There was a wierd bug where metrics in the db that did not explicitly insert a value would be NULL, so could not be found by . In addition I've added a new primary id field to the app_metrics table so that updates could be done against very specific metric rows. * Updated adaptors and db to handle proto_id I need a way to distinguish individual metric items from each other so that I can ignore the ones that have been seen before. * Added postgres DB connection, integrated into anonmetrics.Server * Removed proto_id from SQL migration and model * Integrated postgres with Server and updated adaptors * Added sample config files for client and server * Fix lint * Fix for receiving an anonMetricBatch not in server mode * Postgres test fixes * Tidy up, make vendor and make generate * Moved all anon metric postgres migration logic and sources into a the protocol/anonmetrics package or sub packages. I don't know if this will fix the does: cannot open `does' (No such file or directory) not: cannot open `not' (No such file or directory) exist: cannot open `exist' (No such file or directory) error that happens in Jenkins but this could work
2021-09-01 12:02:18 +00:00
case protobuf.ApplicationMetadataMessage_ANONYMOUS_METRIC_BATCH:
return m.unmarshalProtobufData(new(protobuf.AnonymousMetricBatch))
2021-10-05 17:26:02 +00:00
case protobuf.ApplicationMetadataMessage_SYNC_CHAT_REMOVED:
return m.unmarshalProtobufData(new(protobuf.SyncChatRemoved))
2021-10-12 10:33:32 +00:00
case protobuf.ApplicationMetadataMessage_SYNC_CHAT_MESSAGES_READ:
return m.unmarshalProtobufData(new(protobuf.SyncChatMessagesRead))
case protobuf.ApplicationMetadataMessage_BACKUP:
return m.unmarshalProtobufData(new(protobuf.Backup))
case protobuf.ApplicationMetadataMessage_SYNC_ACTIVITY_CENTER_READ:
return m.unmarshalProtobufData(new(protobuf.SyncActivityCenterRead))
case protobuf.ApplicationMetadataMessage_SYNC_ACTIVITY_CENTER_ACCEPTED:
return m.unmarshalProtobufData(new(protobuf.SyncActivityCenterAccepted))
case protobuf.ApplicationMetadataMessage_SYNC_ACTIVITY_CENTER_DISMISSED:
return m.unmarshalProtobufData(new(protobuf.SyncActivityCenterDismissed))
2022-01-17 03:42:11 +00:00
case protobuf.ApplicationMetadataMessage_SYNC_BOOKMARK:
return m.unmarshalProtobufData(new(protobuf.SyncBookmark))
2022-02-10 10:00:59 +00:00
case protobuf.ApplicationMetadataMessage_SYNC_CLEAR_HISTORY:
return m.unmarshalProtobufData(new(protobuf.SyncClearHistory))
Sync Settings (#2478) * Sync Settings * Added valueHandlers and Database singleton Some issues remain, need a way to comparing incoming sql.DB to check if the connection is to a different file or not. Maybe make singleton instance per filename * Added functionality to check the sqlite filename * Refactor of Database.SaveSyncSettings to be used as a handler * Implemented inteface for setting sync protobuf factories * Refactored and completed adhoc send setting sync * Tidying up * Immutability refactor * Refactor settings into dedicated package * Breakout structs * Tidy up * Refactor of bulk settings sync * Bug fixes * Addressing feedback * Fix code dropped during rebase * Fix for db closed * Fix for node config related crashes * Provisional fix for type assertion - issue 2 * Adding robust type assertion checks * Partial fix for null literal db storage and json encoding * Fix for passively handling nil sql.DB, and checking if elem has len and if len is 0 * Added test for preferred name behaviour * Adding saved sync settings to MessengerResponse * Completed granular initial sync and clock from network on save * add Settings to isEmpty * Refactor of protobufs, partially done * Added syncSetting receiver handling, some bug fixes * Fix for sticker packs * Implement inactive flag on sync protobuf factory * Refactor of types and structs * Added SettingField.CanSync functionality * Addressing rebase artifact * Refactor of Setting SELECT queries * Refactor of string return queries * VERSION bump and migration index bump * Deactiveate Sync Settings * Deactiveated preferred_name and send_status_updates Co-authored-by: Andrea Maria Piana <andrea.maria.piana@gmail.com>
2022-03-23 18:47:00 +00:00
case protobuf.ApplicationMetadataMessage_SYNC_SETTING:
return m.unmarshalProtobufData(new(protobuf.SyncSetting))
case protobuf.ApplicationMetadataMessage_COMMUNITY_ARCHIVE_MAGNETLINK:
return m.unmarshalProtobufData(new(protobuf.CommunityMessageArchiveMagnetlink))
2022-05-18 10:42:51 +00:00
case protobuf.ApplicationMetadataMessage_SYNC_WALLET_ACCOUNT:
return m.unmarshalProtobufData(new(protobuf.SyncWalletAccounts))
case protobuf.ApplicationMetadataMessage_ACCEPT_CONTACT_REQUEST:
return m.unmarshalProtobufData(new(protobuf.AcceptContactRequest))
case protobuf.ApplicationMetadataMessage_RETRACT_CONTACT_REQUEST:
return m.unmarshalProtobufData(new(protobuf.RetractContactRequest))
case protobuf.ApplicationMetadataMessage_SYNC_COMMUNITY_SETTINGS:
return m.unmarshalProtobufData(new(protobuf.SyncCommunitySettings))
case protobuf.ApplicationMetadataMessage_REQUEST_CONTACT_VERIFICATION:
return m.unmarshalProtobufData(new(protobuf.RequestContactVerification))
case protobuf.ApplicationMetadataMessage_ACCEPT_CONTACT_VERIFICATION:
return m.unmarshalProtobufData(new(protobuf.AcceptContactVerification))
case protobuf.ApplicationMetadataMessage_CANCEL_CONTACT_VERIFICATION:
return m.unmarshalProtobufData(new(protobuf.CancelContactVerification))
case protobuf.ApplicationMetadataMessage_DECLINE_CONTACT_VERIFICATION:
return m.unmarshalProtobufData(new(protobuf.DeclineContactVerification))
case protobuf.ApplicationMetadataMessage_SYNC_TRUSTED_USER:
return m.unmarshalProtobufData((new(protobuf.SyncTrustedUser)))
case protobuf.ApplicationMetadataMessage_SYNC_VERIFICATION_REQUEST:
return m.unmarshalProtobufData((new(protobuf.SyncVerificationRequest)))
2022-08-07 14:25:03 +00:00
case protobuf.ApplicationMetadataMessage_SYNC_CONTACT_REQUEST_DECISION:
return m.unmarshalProtobufData((new(protobuf.SyncContactRequestDecision)))
case protobuf.ApplicationMetadataMessage_SYNC_SAVED_ADDRESS:
return m.unmarshalProtobufData(new(protobuf.SyncSavedAddress))
case protobuf.ApplicationMetadataMessage_SYNC_ALL_KEYCARDS:
return m.unmarshalProtobufData(new(protobuf.SyncAllKeycards))
case protobuf.ApplicationMetadataMessage_SYNC_KEYCARD_ACTION:
return m.unmarshalProtobufData(new(protobuf.SyncKeycardAction))
}
return nil
}
func (m *StatusMessage) unmarshalProtobufData(pb proto.Message) error {
var ptr proto.Message
rv := reflect.ValueOf(pb)
if rv.Kind() == reflect.Ptr {
ptr = pb
} else {
ptr = rv.Addr().Interface().(proto.Message)
}
err := proto.Unmarshal(m.UnwrappedPayload, ptr)
if err != nil {
m.ParsedMessage = nil
log.Error("[message::DecodeMessage] could not decode %T: %#x, err: %v", pb, m.Hash, err.Error())
} else {
rv = reflect.ValueOf(ptr)
elem := rv.Elem()
m.ParsedMessage = &elem
return nil
}
return nil
}