status-go/protocol/contact.go

287 lines
7.2 KiB
Go
Raw Normal View History

package protocol
2019-08-29 06:33:46 +00:00
import (
"crypto/ecdsa"
"encoding/json"
2019-08-29 06:33:46 +00:00
"github.com/status-im/status-go/api/multiformat"
"github.com/status-im/status-go/eth-node/crypto"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/images"
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
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/protocol/common"
2022-08-02 12:56:26 +00:00
"github.com/status-im/status-go/protocol/identity"
"github.com/status-im/status-go/protocol/identity/alias"
"github.com/status-im/status-go/protocol/identity/identicon"
"github.com/status-im/status-go/protocol/verification"
2019-08-29 06:33:46 +00:00
)
type ContactRequestState int
const (
ContactRequestStateNone ContactRequestState = iota
ContactRequestStateMutual
ContactRequestStateSent
ContactRequestStateReceived
ContactRequestStateDismissed
)
// 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"`
}
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
}
return c.Alias
}
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
func (c *Contact) CanonicalImage(profilePicturesVisibility settings.ProfilePicturesVisibilityType) string {
if profilePicturesVisibility == settings.ProfilePicturesVisibilityNone || (profilePicturesVisibility == settings.ProfilePicturesVisibilityContactsOnly && !c.Added) {
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
}
}
return c.Identicon
}
type VerificationStatus int
const (
VerificationStatusUNVERIFIED VerificationStatus = iota
VerificationStatusVERIFYING
VerificationStatusVERIFIED
)
2021-03-24 08:04:03 +00:00
// Contact has information about a "Contact"
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).
ID string `json:"id"`
// Ethereum address of the contact
Address string `json:"address,omitempty"`
// ENS name of contact
EnsName string `json:"name,omitempty"`
// EnsVerified whether we verified the name of the contact
ENSVerified bool `json:"ensVerified"`
// Generated username name of the contact
Alias string `json:"alias,omitempty"`
// Identicon generated from public key
Identicon string `json:"identicon"`
// 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
LastUpdated uint64 `json:"lastUpdated"`
// 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"`
2022-08-05 11:22:35 +00:00
// Bio - description of the contact (tell us about yourself)
Bio string `json:"bio"`
2022-08-02 12:56:26 +00:00
SocialLinks identity.SocialLinks `json:"socialLinks"`
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"`
ContactRequestState ContactRequestState `json:"contactRequestState"`
ContactRequestClock uint64 `json:"contactRequestClock"`
IsSyncing bool
Removed bool
VerificationStatus VerificationStatus `json:"verificationStatus"`
TrustStatus verification.TrustStatus `json:"trustStatus"`
}
func (c Contact) IsVerified() bool {
return c.VerificationStatus == VerificationStatusVERIFIED
}
func (c Contact) IsVerifying() bool {
return c.VerificationStatus == VerificationStatusVERIFYING
}
func (c Contact) IsUnverified() bool {
return c.VerificationStatus == VerificationStatusUNVERIFIED
}
func (c Contact) IsUntrustworthy() bool {
return c.TrustStatus == verification.TrustStatusUNTRUSTWORTHY
}
func (c Contact) IsTrusted() bool {
return c.TrustStatus == verification.TrustStatusTRUSTED
}
2019-08-29 06:33:46 +00:00
func (c Contact) PublicKey() (*ecdsa.PublicKey, error) {
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) Block() {
2021-03-24 08:04:03 +00:00
c.Blocked = true
2021-11-05 15:11:10 +00:00
c.Added = false
}
func (c *Contact) BlockDesktop() {
c.Blocked = true
}
func (c *Contact) Unblock() {
2021-03-24 08:04:03 +00:00
c.Blocked = false
}
func (c *Contact) Remove() {
2021-03-24 08:04:03 +00:00
c.Added = false
c.Removed = true
}
func (c *Contact) Add() {
c.Added = true
c.Removed = false
}
func (c *Contact) ContactRequestSent() {
switch c.ContactRequestState {
case ContactRequestStateNone, ContactRequestStateDismissed:
c.ContactRequestState = ContactRequestStateSent
case ContactRequestStateReceived:
c.ContactRequestState = ContactRequestStateMutual
}
}
func (c *Contact) ContactRequestReceived() {
switch c.ContactRequestState {
case ContactRequestStateNone:
c.ContactRequestState = ContactRequestStateReceived
case ContactRequestStateSent:
c.ContactRequestState = ContactRequestStateMutual
}
}
func (c *Contact) ContactRequestAccepted() {
switch c.ContactRequestState {
case ContactRequestStateSent:
c.ContactRequestState = ContactRequestStateMutual
}
}
func (c *Contact) AcceptContactRequest() {
switch c.ContactRequestState {
case ContactRequestStateReceived, ContactRequestStateDismissed:
c.ContactRequestState = ContactRequestStateMutual
}
}
func (c *Contact) RetractContactRequest() {
c.ContactRequestState = ContactRequestStateNone
}
func (c *Contact) ContactRequestRetracted() {
c.ContactRequestState = ContactRequestStateNone
}
func (c *Contact) DismissContactRequest() {
c.ContactRequestState = ContactRequestStateDismissed
}
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) {
id := common.PubkeyToHex(publicKey)
return buildContact(id, publicKey)
}
func buildContact(publicKeyString string, publicKey *ecdsa.PublicKey) (*Contact, error) {
2021-03-24 08:04:03 +00:00
newIdenticon, err := identicon.GenerateBase64(publicKeyString)
if err != nil {
return nil, err
}
contact := &Contact{
ID: publicKeyString,
Alias: alias.GenerateFromPublicKey(publicKey),
2021-03-24 08:04:03 +00:00
Identicon: newIdenticon,
}
return contact, nil
}
func contactIDFromPublicKey(key *ecdsa.PublicKey) string {
return types.EncodeHex(crypto.FromECDSAPub(key))
}
func contactIDFromPublicKeyString(key string) (string, error) {
pubKey, err := common.HexToPubkey(key)
if err != nil {
return "", err
}
return contactIDFromPublicKey(pubKey), nil
}
func (c *Contact) MarshalJSON() ([]byte, error) {
type Alias Contact
item := struct {
*Alias
CompressedKey string `json:"compressedKey"`
}{
Alias: (*Alias)(c),
}
compressedKey, err := multiformat.SerializeLegacyKey(item.ID)
if err != nil {
return nil, err
}
item.CompressedKey = compressedKey
return json.Marshal(item)
}