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-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2020-12-15 15:28:05 +00:00
|
|
|
"github.com/status-im/status-go/images"
|
2022-03-23 18:47:00 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
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
|
|
|
)
|
|
|
|
|
2021-10-07 04:42:27 +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"`
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
func (c *Contact) CanonicalName() string {
|
|
|
|
if c.LocalNickname != "" {
|
|
|
|
return c.LocalNickname
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ENSVerified {
|
2022-02-17 15:13:10 +00:00
|
|
|
return c.EnsName
|
2021-03-31 16:23:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return c.Alias
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
func (c *Contact) CanonicalImage(profilePicturesVisibility settings.ProfilePicturesVisibilityType) string {
|
|
|
|
if profilePicturesVisibility == settings.ProfilePicturesVisibilityNone || (profilePicturesVisibility == settings.ProfilePicturesVisibilityContactsOnly && !c.Added) {
|
2021-10-07 04:42:27 +00:00
|
|
|
return c.Identicon
|
|
|
|
}
|
|
|
|
|
|
|
|
if largeImage, ok := c.Images[images.LargeDimName]; ok {
|
|
|
|
imageBase64, err := largeImage.GetDataURI()
|
|
|
|
if err == nil {
|
|
|
|
return imageBase64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if thumbImage, ok := c.Images[images.SmallDimName]; ok {
|
|
|
|
imageBase64, err := thumbImage.GetDataURI()
|
|
|
|
if err == nil {
|
|
|
|
return imageBase64
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
return c.Identicon
|
|
|
|
}
|
|
|
|
|
2021-03-24 08:04:03 +00:00
|
|
|
// Contact has information about a "Contact"
|
2019-07-30 18:39:16 +00:00
|
|
|
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
|
2022-03-17 17:28:42 +00:00
|
|
|
EnsName 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"`
|
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
|
|
|
// 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
|
|
|
|
2021-10-22 14:20:42 +00:00
|
|
|
// LastUpdatedLocally is the last time we updated the contact locally
|
|
|
|
LastUpdatedLocally uint64 `json:"lastUpdatedLocally"`
|
|
|
|
|
2021-03-24 08:04:03 +00:00
|
|
|
LocalNickname string `json:"localNickname,omitempty"`
|
|
|
|
|
2022-02-17 15:13:10 +00:00
|
|
|
// Display name of the contact
|
|
|
|
DisplayName string `json:"displayName"`
|
|
|
|
|
2021-03-24 08:04:03 +00:00
|
|
|
Images map[string]images.IdentityImage `json:"images"`
|
|
|
|
|
2021-10-01 14:50:16 +00:00
|
|
|
Added bool `json:"added"`
|
|
|
|
Blocked bool `json:"blocked"`
|
|
|
|
HasAddedUs bool `json:"hasAddedUs"`
|
2020-12-15 15:28:05 +00:00
|
|
|
|
2021-09-03 08:26:05 +00:00
|
|
|
IsSyncing bool
|
|
|
|
Removed bool
|
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)
|
|
|
|
}
|
|
|
|
|
2021-09-03 08:26:05 +00:00
|
|
|
func (c *Contact) Block() {
|
2021-03-24 08:04:03 +00:00
|
|
|
c.Blocked = true
|
2021-11-05 15:11:10 +00:00
|
|
|
c.Added = false
|
2021-09-03 08:26:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 20:14:37 +00:00
|
|
|
func (c *Contact) BlockDesktop() {
|
|
|
|
c.Blocked = true
|
|
|
|
}
|
|
|
|
|
2021-09-03 08:26:05 +00:00
|
|
|
func (c *Contact) Unblock() {
|
2021-03-24 08:04:03 +00:00
|
|
|
c.Blocked = false
|
2021-09-03 08:26:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) Remove() {
|
2021-03-24 08:04:03 +00:00
|
|
|
c.Added = false
|
2021-09-03 08:26:05 +00:00
|
|
|
c.Removed = true
|
|
|
|
}
|
|
|
|
|
2022-04-25 09:02:49 +00:00
|
|
|
func (c *Contact) Add() {
|
|
|
|
c.Added = true
|
|
|
|
c.Removed = false
|
|
|
|
}
|
|
|
|
|
2020-12-22 10:49:25 +00:00
|
|
|
func buildContactFromPkString(pkString string) (*Contact, error) {
|
|
|
|
publicKeyBytes, err := types.DecodeHex(pkString)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
publicKey, err := crypto.UnmarshalPubkey(publicKeyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return buildContact(pkString, publicKey)
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:15:22 +00:00
|
|
|
func BuildContactFromPublicKey(publicKey *ecdsa.PublicKey) (*Contact, error) {
|
2020-12-22 10:49:25 +00:00
|
|
|
id := types.EncodeHex(crypto.FromECDSAPub(publicKey))
|
|
|
|
return buildContact(id, publicKey)
|
|
|
|
}
|
2019-11-04 10:08:22 +00:00
|
|
|
|
2020-12-22 10:49:25 +00:00
|
|
|
func buildContact(publicKeyString string, publicKey *ecdsa.PublicKey) (*Contact, error) {
|
2021-03-24 08:04:03 +00:00
|
|
|
newIdenticon, err := identicon.GenerateBase64(publicKeyString)
|
2019-11-04 10:08:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
contact := &Contact{
|
2020-12-22 10:49:25 +00:00
|
|
|
ID: publicKeyString,
|
2019-11-04 10:08:22 +00:00
|
|
|
Alias: alias.GenerateFromPublicKey(publicKey),
|
2021-03-24 08:04:03 +00:00
|
|
|
Identicon: newIdenticon,
|
2019-11-04 10:08:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return contact, nil
|
|
|
|
}
|
2020-01-15 07:25:09 +00:00
|
|
|
|
|
|
|
func contactIDFromPublicKey(key *ecdsa.PublicKey) string {
|
|
|
|
return types.EncodeHex(crypto.FromECDSAPub(key))
|
|
|
|
}
|