2019-11-21 16:19:22 +00:00
|
|
|
package protocol
|
2019-07-17 22:25:42 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2019-07-26 07:17:29 +00:00
|
|
|
"time"
|
|
|
|
|
2019-07-17 22:25:42 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
2019-08-06 21:50:13 +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"
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2019-07-17 22:25:42 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
// ErrInvalidDecodedValue means that the decoded message is of wrong type.
|
|
|
|
// This might mean that the status message serialization tag changed.
|
|
|
|
ErrInvalidDecodedValue = errors.New("invalid decoded value type")
|
|
|
|
)
|
|
|
|
|
|
|
|
// TimestampInMsFromTime returns a TimestampInMs from a time.Time instance.
|
2019-12-02 15:34:05 +00:00
|
|
|
func TimestampInMsFromTime(t time.Time) uint64 {
|
|
|
|
return uint64(t.UnixNano() / int64(time.Millisecond))
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
2019-08-06 21:50:13 +00:00
|
|
|
// MessageID calculates the messageID from author's compressed public key
|
|
|
|
// and not encrypted but encoded payload.
|
2019-11-23 17:57:05 +00:00
|
|
|
func MessageID(author *ecdsa.PublicKey, data []byte) types.HexBytes {
|
2019-08-09 07:03:10 +00:00
|
|
|
keyBytes := crypto.FromECDSAPub(author)
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
return types.HexBytes(crypto.Keccak256(append(keyBytes, data...)))
|
2019-07-17 22:25:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// WrapMessageV1 wraps a payload into a protobuf message and signs it if an identity is provided
|
2019-12-02 15:34:05 +00:00
|
|
|
func WrapMessageV1(payload []byte, messageType protobuf.ApplicationMetadataMessage_Type, identity *ecdsa.PrivateKey) ([]byte, error) {
|
2019-07-17 22:25:42 +00:00
|
|
|
var signature []byte
|
|
|
|
if identity != nil {
|
|
|
|
var err error
|
|
|
|
signature, err = crypto.Sign(crypto.Keccak256(payload), identity)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Move to protobuf for Message type (#1706)
* Use a single Message type `v1/message.go` and `message.go` are the same now, and they embed `protobuf.ChatMessage`
* Use `SendChatMessage` for sending chat messages, this is basically the old `Send` but a bit more flexible so we can send different message types (stickers,commands), and not just text.
* Remove dedup from services/shhext. Because now we process in status-protocol, dedup makes less sense, as those messages are going to be processed anyway, so removing for now, we can re-evaluate if bringing it to status-go or not.
* Change the various retrieveX method to a single one:
`RetrieveAll` will be processing those messages that it can process (Currently only `Message`), and return the rest in `RawMessages` (still transit). The format for the response is:
`Chats`: -> The chats updated by receiving the message
`Messages`: -> The messages retrieved (already matched to a chat)
`Contacts`: -> The contacts updated by the messages
`RawMessages` -> Anything else that can't be parsed, eventually as we move everything to status-protocol-go this will go away.
2019-12-05 16:25:34 +00:00
|
|
|
message := &protobuf.ApplicationMetadataMessage{
|
2019-07-17 22:25:42 +00:00
|
|
|
Signature: signature,
|
2019-12-02 15:34:05 +00:00
|
|
|
Type: messageType,
|
2019-07-17 22:25:42 +00:00
|
|
|
Payload: payload,
|
|
|
|
}
|
|
|
|
return proto.Marshal(message)
|
|
|
|
}
|