2019-11-21 16:19:22 +00:00
|
|
|
package protocol
|
2019-07-30 18:39:16 +00:00
|
|
|
|
2019-08-29 06:33:46 +00:00
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
2019-11-04 10:08:22 +00:00
|
|
|
"encoding/hex"
|
2019-08-29 06:33:46 +00:00
|
|
|
|
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/identity/alias"
|
|
|
|
"github.com/status-im/status-go/protocol/identity/identicon"
|
2019-08-29 06:33:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
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
|
|
|
contactBlocked = ":contact/blocked"
|
|
|
|
contactAdded = ":contact/added"
|
|
|
|
contactRequestReceived = ":contact/request-received"
|
2019-08-29 06:33:46 +00:00
|
|
|
)
|
|
|
|
|
2019-07-30 18:39:16 +00:00
|
|
|
// ContactDeviceInfo is a struct containing information about a particular device owned by a contact
|
|
|
|
type ContactDeviceInfo struct {
|
|
|
|
// The installation id of the device
|
|
|
|
InstallationID string `json:"id"`
|
|
|
|
// Timestamp represents the last time we received this info
|
|
|
|
Timestamp int64 `json:"timestamp"`
|
|
|
|
// FCMToken is to be used for push notifications
|
|
|
|
FCMToken string `json:"fcmToken"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// Contact has information about a "Contact". A contact is not necessarily one
|
|
|
|
// that we added or added us, that's based on SystemTags.
|
|
|
|
type Contact struct {
|
2019-08-29 06:33:46 +00:00
|
|
|
// ID of the contact. It's a hex-encoded public key (prefixed with 0x).
|
2019-07-30 18:39:16 +00:00
|
|
|
ID string `json:"id"`
|
|
|
|
// Ethereum address of the contact
|
2020-01-20 16:44:32 +00:00
|
|
|
Address string `json:"address,omitempty"`
|
2019-11-04 10:08:22 +00:00
|
|
|
// ENS name of contact
|
2019-09-26 07:01:17 +00:00
|
|
|
Name string `json:"name,omitempty"`
|
2019-11-04 10:08:22 +00:00
|
|
|
// EnsVerified whether we verified the name of the contact
|
|
|
|
ENSVerified bool `json:"ensVerified"`
|
|
|
|
// EnsVerifiedAt the time we last verified the name
|
2020-02-05 10:09:33 +00:00
|
|
|
ENSVerifiedAt uint64 `json:"ensVerifiedAt"`
|
|
|
|
// LastENSClockValue is the last clock value of when we
|
|
|
|
// received an ENS name for the user
|
|
|
|
LastENSClockValue uint64 `json:"lastENSClockValue"`
|
|
|
|
// ENSVerificationRetries is how many times we retried the ENS
|
|
|
|
ENSVerificationRetries uint64 `json:"ensVerificationRetries"`
|
2019-09-26 07:01:17 +00:00
|
|
|
// Generated username name of the contact
|
|
|
|
Alias string `json:"alias,omitempty"`
|
|
|
|
// Identicon generated from public key
|
|
|
|
Identicon string `json:"identicon"`
|
2019-07-30 18:39:16 +00:00
|
|
|
// Photo is the base64 encoded photo
|
2019-09-26 07:01:17 +00:00
|
|
|
Photo string `json:"photoPath,omitempty"`
|
2019-07-30 18:39:16 +00:00
|
|
|
// LastUpdated is the last time we received an update from the contact
|
|
|
|
// updates should be discarded if last updated is less than the one stored
|
2020-01-10 18:59:01 +00:00
|
|
|
LastUpdated uint64 `json:"lastUpdated"`
|
2019-07-30 18:39:16 +00:00
|
|
|
// SystemTags contains information about whether we blocked/added/have been
|
|
|
|
// added.
|
|
|
|
SystemTags []string `json:"systemTags"`
|
|
|
|
|
|
|
|
DeviceInfo []ContactDeviceInfo `json:"deviceInfo"`
|
2020-02-10 11:22:37 +00:00
|
|
|
TributeToTalk string `json:"tributeToTalk,omitempty"`
|
2019-07-30 18:39:16 +00:00
|
|
|
}
|
2019-08-29 06:33:46 +00:00
|
|
|
|
|
|
|
func (c Contact) PublicKey() (*ecdsa.PublicKey, error) {
|
2019-11-23 17:57:05 +00:00
|
|
|
b, err := types.DecodeHex(c.ID)
|
2019-08-29 06:33:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return crypto.UnmarshalPubkey(b)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Contact) IsAdded() bool {
|
|
|
|
return existsInStringSlice(c.SystemTags, contactAdded)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Contact) HasBeenAdded() bool {
|
|
|
|
return existsInStringSlice(c.SystemTags, contactRequestReceived)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c Contact) IsBlocked() bool {
|
|
|
|
return existsInStringSlice(c.SystemTags, contactBlocked)
|
|
|
|
}
|
|
|
|
|
2020-02-05 10:09:33 +00:00
|
|
|
func (c *Contact) ResetENSVerification(clock uint64, name string) {
|
|
|
|
c.ENSVerifiedAt = 0
|
|
|
|
c.ENSVerified = false
|
|
|
|
c.ENSVerificationRetries = 0
|
|
|
|
c.LastENSClockValue = clock
|
|
|
|
c.Name = name
|
|
|
|
}
|
|
|
|
|
2019-08-29 06:33:46 +00:00
|
|
|
// existsInStringSlice checks if a string is in a set.
|
|
|
|
func existsInStringSlice(set []string, find string) bool {
|
|
|
|
for _, s := range set {
|
|
|
|
if s == find {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
2019-11-04 10:08:22 +00:00
|
|
|
|
|
|
|
func buildContact(publicKey *ecdsa.PublicKey) (*Contact, error) {
|
|
|
|
id := "0x" + hex.EncodeToString(crypto.FromECDSAPub(publicKey))
|
|
|
|
|
|
|
|
identicon, err := identicon.GenerateBase64(id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contact := &Contact{
|
|
|
|
ID: id,
|
|
|
|
Alias: alias.GenerateFromPublicKey(publicKey),
|
|
|
|
Identicon: identicon,
|
|
|
|
}
|
|
|
|
|
|
|
|
return contact, nil
|
|
|
|
}
|
2020-01-15 07:25:09 +00:00
|
|
|
|
2020-04-17 11:22:38 +00:00
|
|
|
// HasCustomFields returns whether the the contact has any field that is valuable
|
|
|
|
// to the client other than the computed name/image
|
|
|
|
func (c Contact) HasCustomFields() bool {
|
|
|
|
return c.IsAdded() || c.HasBeenAdded() || c.IsBlocked() || c.ENSVerified
|
|
|
|
}
|
|
|
|
|
2020-01-15 07:25:09 +00:00
|
|
|
func contactIDFromPublicKey(key *ecdsa.PublicKey) string {
|
|
|
|
return types.EncodeHex(crypto.FromECDSAPub(key))
|
|
|
|
|
|
|
|
}
|