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"
|
2023-01-20 18:51:36 +00:00
|
|
|
"encoding/json"
|
2019-08-29 06:33:46 +00:00
|
|
|
|
2023-03-06 08:51:09 +00:00
|
|
|
accountJson "github.com/status-im/status-go/account/json"
|
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"
|
2022-01-18 16:31:34 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
2022-08-02 12:56:26 +00:00
|
|
|
"github.com/status-im/status-go/protocol/identity"
|
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"
|
2023-02-02 18:12:20 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
2022-07-05 19:49:44 +00:00
|
|
|
"github.com/status-im/status-go/protocol/verification"
|
2019-08-29 06:33:46 +00:00
|
|
|
)
|
|
|
|
|
2022-01-18 16:31:34 +00:00
|
|
|
type ContactRequestState int
|
|
|
|
|
|
|
|
const (
|
|
|
|
ContactRequestStateNone ContactRequestState = iota
|
|
|
|
ContactRequestStateMutual
|
|
|
|
ContactRequestStateSent
|
2023-02-02 18:12:20 +00:00
|
|
|
// Received is a confusing state, we should use
|
|
|
|
// sent for both, since they are now stored in different
|
|
|
|
// states
|
2022-01-18 16:31:34 +00:00
|
|
|
ContactRequestStateReceived
|
|
|
|
ContactRequestStateDismissed
|
|
|
|
)
|
|
|
|
|
2023-06-08 12:00:19 +00:00
|
|
|
type MutualStateUpdateType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
MutualStateUpdateTypeSent MutualStateUpdateType = iota + 1
|
|
|
|
MutualStateUpdateTypeAdded
|
|
|
|
MutualStateUpdateTypeRemoved
|
|
|
|
)
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2022-03-23 18:47:00 +00:00
|
|
|
func (c *Contact) CanonicalImage(profilePicturesVisibility settings.ProfilePicturesVisibilityType) string {
|
2023-01-20 14:28:30 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2022-07-05 19:49:44 +00:00
|
|
|
type VerificationStatus int
|
|
|
|
|
|
|
|
const (
|
|
|
|
VerificationStatusUNVERIFIED VerificationStatus = iota
|
|
|
|
VerificationStatusVERIFYING
|
|
|
|
VerificationStatusVERIFIED
|
|
|
|
)
|
|
|
|
|
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"`
|
|
|
|
|
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"`
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
Blocked bool `json:"blocked"`
|
2020-12-15 15:28:05 +00:00
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
// ContactRequestRemoteState is the state of the contact request
|
|
|
|
// on the contact's end
|
|
|
|
ContactRequestRemoteState ContactRequestState `json:"contactRequestRemoteState"`
|
|
|
|
// ContactRequestRemoteClock is the clock for incoming contact requests
|
|
|
|
ContactRequestRemoteClock uint64 `json:"contactRequestRemoteClock"`
|
|
|
|
|
|
|
|
// ContactRequestLocalState is the state of the contact request
|
|
|
|
// on our end
|
|
|
|
ContactRequestLocalState ContactRequestState `json:"contactRequestLocalState"`
|
|
|
|
// ContactRequestLocalClock is the clock for outgoing contact requests
|
|
|
|
ContactRequestLocalClock uint64 `json:"contactRequestLocalClock"`
|
2022-01-18 16:31:34 +00:00
|
|
|
|
2021-09-03 08:26:05 +00:00
|
|
|
IsSyncing bool
|
|
|
|
Removed bool
|
2022-07-05 19:49:44 +00:00
|
|
|
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
func (c *Contact) Block(clock uint64) {
|
2021-03-24 08:04:03 +00:00
|
|
|
c.Blocked = true
|
2023-01-20 14:28:30 +00:00
|
|
|
c.DismissContactRequest(clock)
|
2023-04-20 10:52:08 +00:00
|
|
|
c.Removed = true
|
2021-09-03 08:26:05 +00:00
|
|
|
}
|
|
|
|
|
2022-04-08 20:14:37 +00:00
|
|
|
func (c *Contact) BlockDesktop() {
|
|
|
|
c.Blocked = true
|
|
|
|
}
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
func (c *Contact) Unblock(clock uint64) {
|
2021-03-24 08:04:03 +00:00
|
|
|
c.Blocked = false
|
2023-01-20 14:28:30 +00:00
|
|
|
// Reset the contact request flow
|
|
|
|
c.RetractContactRequest(clock)
|
2021-09-03 08:26:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
func (c *Contact) added() bool {
|
|
|
|
return c.ContactRequestLocalState == ContactRequestStateSent
|
2021-09-03 08:26:05 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
func (c *Contact) hasAddedUs() bool {
|
|
|
|
return c.ContactRequestRemoteState == ContactRequestStateReceived
|
2022-04-25 09:02:49 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
func (c *Contact) mutual() bool {
|
|
|
|
return c.added() && c.hasAddedUs()
|
2022-01-18 16:31:34 +00:00
|
|
|
}
|
|
|
|
|
2023-02-22 16:57:33 +00:00
|
|
|
func (c *Contact) active() bool {
|
|
|
|
return c.mutual() && !c.Blocked
|
|
|
|
}
|
|
|
|
|
2023-02-02 18:12:20 +00:00
|
|
|
func (c *Contact) dismissed() bool {
|
|
|
|
return c.ContactRequestLocalState == ContactRequestStateDismissed
|
|
|
|
}
|
|
|
|
|
2023-02-22 16:57:33 +00:00
|
|
|
func (c *Contact) names() []string {
|
|
|
|
var names []string
|
|
|
|
|
|
|
|
if c.LocalNickname != "" {
|
|
|
|
names = append(names, c.LocalNickname)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ENSVerified && len(c.EnsName) != 0 {
|
|
|
|
names = append(names, c.EnsName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.DisplayName != "" {
|
|
|
|
names = append(names, c.DisplayName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return append(names, c.Alias)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) PrimaryName() string {
|
|
|
|
return c.names()[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) SecondaryName() string {
|
|
|
|
// Only shown if the user has a nickname
|
|
|
|
if c.LocalNickname == "" {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
names := c.names()
|
|
|
|
if len(names) > 1 {
|
|
|
|
return names[1]
|
|
|
|
}
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
type ContactRequestProcessingResponse struct {
|
|
|
|
processed bool
|
|
|
|
newContactRequestReceived bool
|
2023-02-02 18:12:20 +00:00
|
|
|
sendBackState bool
|
2023-01-20 14:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) ContactRequestSent(clock uint64) ContactRequestProcessingResponse {
|
|
|
|
if clock <= c.ContactRequestLocalClock {
|
|
|
|
return ContactRequestProcessingResponse{}
|
2022-01-18 16:31:34 +00:00
|
|
|
}
|
2023-01-20 14:28:30 +00:00
|
|
|
|
|
|
|
c.ContactRequestLocalClock = clock
|
|
|
|
c.ContactRequestLocalState = ContactRequestStateSent
|
|
|
|
|
|
|
|
c.Removed = false
|
|
|
|
|
|
|
|
return ContactRequestProcessingResponse{processed: true}
|
2022-01-18 16:31:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
func (c *Contact) AcceptContactRequest(clock uint64) ContactRequestProcessingResponse {
|
|
|
|
// We treat accept the same as sent, that's because accepting a contact
|
|
|
|
// request that does not exist is possible if the instruction is coming from
|
|
|
|
// a different device, we'd rather assume that a contact requested existed
|
|
|
|
// and didn't reach our device than being in an inconsistent state
|
|
|
|
return c.ContactRequestSent(clock)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) RetractContactRequest(clock uint64) ContactRequestProcessingResponse {
|
|
|
|
if clock <= c.ContactRequestLocalClock {
|
|
|
|
return ContactRequestProcessingResponse{}
|
2022-01-18 16:31:34 +00:00
|
|
|
}
|
2023-01-20 14:28:30 +00:00
|
|
|
|
|
|
|
// This is a symmetric action, we set both local & remote clock
|
|
|
|
// since we want everything before this point discarded, regardless
|
|
|
|
// the side it was sent from
|
|
|
|
c.ContactRequestLocalClock = clock
|
|
|
|
c.ContactRequestLocalState = ContactRequestStateNone
|
|
|
|
c.ContactRequestRemoteState = ContactRequestStateNone
|
|
|
|
c.ContactRequestRemoteClock = clock
|
|
|
|
c.Removed = true
|
|
|
|
|
|
|
|
return ContactRequestProcessingResponse{processed: true}
|
2022-01-18 16:31:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
func (c *Contact) DismissContactRequest(clock uint64) ContactRequestProcessingResponse {
|
|
|
|
if clock <= c.ContactRequestLocalClock {
|
|
|
|
return ContactRequestProcessingResponse{}
|
2022-01-18 16:31:34 +00:00
|
|
|
}
|
2023-01-20 14:28:30 +00:00
|
|
|
|
|
|
|
c.ContactRequestLocalClock = clock
|
|
|
|
c.ContactRequestLocalState = ContactRequestStateDismissed
|
|
|
|
|
|
|
|
return ContactRequestProcessingResponse{processed: true}
|
2022-01-18 16:31:34 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
// Remote actions
|
|
|
|
|
2023-02-17 10:02:03 +00:00
|
|
|
func (c *Contact) contactRequestRetracted(clock uint64, syncing bool, r ContactRequestProcessingResponse) ContactRequestProcessingResponse {
|
2023-01-20 14:28:30 +00:00
|
|
|
if clock <= c.ContactRequestRemoteClock {
|
2023-02-02 18:12:20 +00:00
|
|
|
return r
|
2023-01-20 14:28:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// This is a symmetric action, we set both local & remote clock
|
|
|
|
// since we want everything before this point discarded, regardless
|
2023-02-02 18:12:20 +00:00
|
|
|
// the side it was sent from. The only exception is when the contact
|
|
|
|
// request has been explicitly dismissed, in which case we don't
|
|
|
|
// change state
|
2023-02-17 10:02:03 +00:00
|
|
|
if c.ContactRequestLocalState != ContactRequestStateDismissed && !syncing {
|
2023-02-02 18:12:20 +00:00
|
|
|
c.ContactRequestLocalClock = clock
|
|
|
|
c.ContactRequestLocalState = ContactRequestStateNone
|
|
|
|
}
|
2023-01-20 14:28:30 +00:00
|
|
|
c.ContactRequestRemoteClock = clock
|
|
|
|
c.ContactRequestRemoteState = ContactRequestStateNone
|
2023-02-02 18:12:20 +00:00
|
|
|
r.processed = true
|
|
|
|
return r
|
|
|
|
}
|
2023-01-20 14:28:30 +00:00
|
|
|
|
2023-02-17 10:02:03 +00:00
|
|
|
func (c *Contact) ContactRequestRetracted(clock uint64, syncing bool) ContactRequestProcessingResponse {
|
|
|
|
return c.contactRequestRetracted(clock, syncing, ContactRequestProcessingResponse{})
|
2022-01-18 16:31:34 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 18:12:20 +00:00
|
|
|
func (c *Contact) contactRequestReceived(clock uint64, r ContactRequestProcessingResponse) ContactRequestProcessingResponse {
|
2023-01-20 14:28:30 +00:00
|
|
|
if clock <= c.ContactRequestRemoteClock {
|
2023-02-02 18:12:20 +00:00
|
|
|
return r
|
2023-01-20 14:28:30 +00:00
|
|
|
}
|
2023-02-02 18:12:20 +00:00
|
|
|
r.processed = true
|
2023-01-20 14:28:30 +00:00
|
|
|
c.ContactRequestRemoteClock = clock
|
|
|
|
switch c.ContactRequestRemoteState {
|
|
|
|
case ContactRequestStateNone:
|
|
|
|
r.newContactRequestReceived = true
|
|
|
|
}
|
|
|
|
c.ContactRequestRemoteState = ContactRequestStateReceived
|
|
|
|
|
|
|
|
return r
|
2022-01-18 16:31:34 +00:00
|
|
|
}
|
|
|
|
|
2023-02-02 18:12:20 +00:00
|
|
|
func (c *Contact) ContactRequestReceived(clock uint64) ContactRequestProcessingResponse {
|
|
|
|
return c.contactRequestReceived(clock, ContactRequestProcessingResponse{})
|
|
|
|
}
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
func (c *Contact) ContactRequestAccepted(clock uint64) ContactRequestProcessingResponse {
|
|
|
|
if clock <= c.ContactRequestRemoteClock {
|
|
|
|
return ContactRequestProcessingResponse{}
|
|
|
|
}
|
|
|
|
// We treat received and accepted in the same way
|
|
|
|
// since the intention is clear on the other side
|
|
|
|
// and there's no difference
|
|
|
|
return c.ContactRequestReceived(clock)
|
2022-01-18 16:31:34 +00:00
|
|
|
}
|
|
|
|
|
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) {
|
2022-01-18 16:31:34 +00:00
|
|
|
id := common.PubkeyToHex(publicKey)
|
2020-12-22 10:49:25 +00:00
|
|
|
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))
|
|
|
|
}
|
2022-08-26 09:25:54 +00:00
|
|
|
|
|
|
|
func contactIDFromPublicKeyString(key string) (string, error) {
|
|
|
|
pubKey, err := common.HexToPubkey(key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return contactIDFromPublicKey(pubKey), nil
|
|
|
|
}
|
2023-01-20 18:51:36 +00:00
|
|
|
|
2023-02-17 10:02:03 +00:00
|
|
|
func (c *Contact) ProcessSyncContactRequestState(remoteState ContactRequestState, remoteClock uint64, localState ContactRequestState, localClock uint64) {
|
2023-01-20 14:28:30 +00:00
|
|
|
// We process the two separately, first local state
|
|
|
|
switch localState {
|
|
|
|
case ContactRequestStateDismissed:
|
|
|
|
c.DismissContactRequest(localClock)
|
|
|
|
case ContactRequestStateNone:
|
|
|
|
c.RetractContactRequest(localClock)
|
|
|
|
case ContactRequestStateSent:
|
|
|
|
c.ContactRequestSent(localClock)
|
|
|
|
}
|
|
|
|
|
|
|
|
// and later remote state
|
|
|
|
switch remoteState {
|
|
|
|
case ContactRequestStateReceived:
|
|
|
|
c.ContactRequestReceived(remoteClock)
|
|
|
|
case ContactRequestStateNone:
|
2023-02-17 10:02:03 +00:00
|
|
|
c.ContactRequestRetracted(remoteClock, true)
|
2023-01-20 14:28:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-20 18:51:36 +00:00
|
|
|
func (c *Contact) MarshalJSON() ([]byte, error) {
|
|
|
|
type Alias Contact
|
2023-03-06 08:51:09 +00:00
|
|
|
type ContactType struct {
|
2023-01-20 18:51:36 +00:00
|
|
|
*Alias
|
2023-01-20 14:28:30 +00:00
|
|
|
Added bool `json:"added"`
|
|
|
|
ContactRequestState ContactRequestState `json:"contactRequestState"`
|
|
|
|
HasAddedUs bool `json:"hasAddedUs"`
|
2023-02-22 16:57:33 +00:00
|
|
|
Mutual bool `json:"mutual"`
|
2023-03-06 08:51:09 +00:00
|
|
|
Active bool `json:"active"`
|
|
|
|
PrimaryName string `json:"primaryName"`
|
|
|
|
SecondaryName string `json:"secondaryName,omitempty"`
|
2023-01-20 18:51:36 +00:00
|
|
|
}
|
|
|
|
|
2023-03-06 08:51:09 +00:00
|
|
|
item := ContactType{
|
|
|
|
Alias: (*Alias)(c),
|
2023-01-20 18:51:36 +00:00
|
|
|
}
|
|
|
|
|
2023-01-20 14:28:30 +00:00
|
|
|
item.Added = c.added()
|
|
|
|
item.HasAddedUs = c.hasAddedUs()
|
2023-02-22 16:57:33 +00:00
|
|
|
item.Mutual = c.mutual()
|
|
|
|
item.Active = c.active()
|
|
|
|
item.PrimaryName = c.PrimaryName()
|
|
|
|
item.SecondaryName = c.SecondaryName()
|
2023-01-20 14:28:30 +00:00
|
|
|
|
|
|
|
if c.mutual() {
|
|
|
|
item.ContactRequestState = ContactRequestStateMutual
|
2023-05-15 07:29:33 +00:00
|
|
|
} else if c.dismissed() {
|
|
|
|
item.ContactRequestState = ContactRequestStateDismissed
|
2023-01-20 14:28:30 +00:00
|
|
|
} else if c.added() {
|
|
|
|
item.ContactRequestState = ContactRequestStateSent
|
|
|
|
} else if c.hasAddedUs() {
|
|
|
|
item.ContactRequestState = ContactRequestStateReceived
|
|
|
|
}
|
2023-03-06 08:51:09 +00:00
|
|
|
ext, err := accountJson.ExtendStructWithPubKeyData(item.ID, item)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-01-20 14:28:30 +00:00
|
|
|
|
2023-03-06 08:51:09 +00:00
|
|
|
return json.Marshal(ext)
|
2023-01-20 18:51:36 +00:00
|
|
|
}
|
2023-02-02 18:12:20 +00:00
|
|
|
|
|
|
|
// ContactRequestPropagatedStateReceived handles the propagation of state from
|
|
|
|
// the other end.
|
|
|
|
func (c *Contact) ContactRequestPropagatedStateReceived(state *protobuf.ContactRequestPropagatedState) ContactRequestProcessingResponse {
|
|
|
|
|
|
|
|
// It's inverted, as their local states is our remote state
|
|
|
|
expectedLocalState := ContactRequestState(state.RemoteState)
|
|
|
|
expectedLocalClock := state.RemoteClock
|
|
|
|
|
|
|
|
remoteState := ContactRequestState(state.LocalState)
|
|
|
|
remoteClock := state.LocalClock
|
|
|
|
|
|
|
|
response := ContactRequestProcessingResponse{}
|
|
|
|
|
|
|
|
// If we notice that the state is not consistent, and their clock is
|
|
|
|
// outdated, we send back the state so they can catch up.
|
|
|
|
if expectedLocalClock < c.ContactRequestLocalClock && expectedLocalState != c.ContactRequestLocalState {
|
|
|
|
response.processed = true
|
|
|
|
response.sendBackState = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// If they expect our state to be more up-to-date, we only
|
|
|
|
// trust it if the state is set to None, in this case we can trust
|
|
|
|
// it, since a retraction can be initiated by both parties
|
|
|
|
if expectedLocalClock > c.ContactRequestLocalClock && c.ContactRequestLocalState != ContactRequestStateDismissed && expectedLocalState == ContactRequestStateNone {
|
|
|
|
response.processed = true
|
|
|
|
c.ContactRequestLocalClock = expectedLocalClock
|
|
|
|
c.ContactRequestLocalState = ContactRequestStateNone
|
2023-02-08 14:58:10 +00:00
|
|
|
// We set the remote state, as this was an implicit retraction
|
|
|
|
// potentially, for example this could happen if they
|
|
|
|
// sent a retraction earier, but we never received it,
|
|
|
|
// or one of our paired devices has retracted the contact request
|
|
|
|
// but we never synced with them.
|
2023-02-02 18:12:20 +00:00
|
|
|
c.ContactRequestRemoteState = ContactRequestStateNone
|
|
|
|
}
|
|
|
|
|
|
|
|
// We always trust this
|
|
|
|
if remoteClock > c.ContactRequestRemoteClock {
|
|
|
|
if remoteState == ContactRequestStateSent {
|
|
|
|
response = c.contactRequestReceived(remoteClock, response)
|
|
|
|
} else if remoteState == ContactRequestStateNone {
|
2023-02-17 10:02:03 +00:00
|
|
|
response = c.contactRequestRetracted(remoteClock, false, response)
|
2023-02-02 18:12:20 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Contact) ContactRequestPropagatedState() *protobuf.ContactRequestPropagatedState {
|
|
|
|
return &protobuf.ContactRequestPropagatedState{
|
|
|
|
LocalClock: c.ContactRequestLocalClock,
|
|
|
|
LocalState: uint64(c.ContactRequestLocalState),
|
|
|
|
RemoteClock: c.ContactRequestRemoteClock,
|
|
|
|
RemoteState: uint64(c.ContactRequestRemoteState),
|
|
|
|
}
|
|
|
|
}
|