-
Notifications
You must be signed in to change notification settings - Fork 245
/
contact.go
558 lines (457 loc) · 16.6 KB
/
contact.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
package protocol
import (
"crypto/ecdsa"
"encoding/json"
"fmt"
accountJson "github.com/status-im/status-go/account/json"
"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"
"github.com/status-im/status-go/multiaccounts"
"github.com/status-im/status-go/multiaccounts/accounts"
"github.com/status-im/status-go/multiaccounts/settings"
"github.com/status-im/status-go/protocol/common"
"github.com/status-im/status-go/protocol/identity"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/verification"
)
type ContactRequestState int
const (
ContactRequestStateNone ContactRequestState = iota
ContactRequestStateMutual
ContactRequestStateSent
// Received is a confusing state, we should use
// sent for both, since they are now stored in different
// states
ContactRequestStateReceived
ContactRequestStateDismissed
)
type MutualStateUpdateType int
const (
MutualStateUpdateTypeSent MutualStateUpdateType = iota + 1
MutualStateUpdateTypeAdded
MutualStateUpdateTypeRemoved
)
// 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) 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
)
// Contact has information about a "Contact"
type Contact struct {
// 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"`
LocalNickname string `json:"localNickname,omitempty"`
// Display name of the contact
DisplayName string `json:"displayName"`
// Bio - description of the contact (tell us about yourself)
Bio string `json:"bio"`
// Deprecated: use social links from ProfileShowcasePreferences
SocialLinks identity.SocialLinks `json:"socialLinks"`
Images map[string]images.IdentityImage `json:"images"`
Blocked bool `json:"blocked"`
// 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"`
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
}
func (c Contact) PublicKey() (*ecdsa.PublicKey, error) {
b, err := types.DecodeHex(c.ID)
if err != nil {
return nil, err
}
return crypto.UnmarshalPubkey(b)
}
func (c *Contact) Block(clock uint64) {
c.Blocked = true
c.DismissContactRequest(clock)
c.Removed = true
}
func (c *Contact) BlockDesktop() {
c.Blocked = true
}
func (c *Contact) Unblock(clock uint64) {
c.Blocked = false
// Reset the contact request flow
c.RetractContactRequest(clock)
}
func (c *Contact) added() bool {
return c.ContactRequestLocalState == ContactRequestStateSent
}
func (c *Contact) hasAddedUs() bool {
return c.ContactRequestRemoteState == ContactRequestStateReceived
}
func (c *Contact) mutual() bool {
return c.added() && c.hasAddedUs()
}
func (c *Contact) active() bool {
return c.mutual() && !c.Blocked
}
func (c *Contact) dismissed() bool {
return c.ContactRequestLocalState == ContactRequestStateDismissed
}
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 ""
}
type ContactRequestProcessingResponse struct {
processed bool
newContactRequestReceived bool
sendBackState bool
}
func (c *Contact) ContactRequestSent(clock uint64) ContactRequestProcessingResponse {
if clock <= c.ContactRequestLocalClock {
return ContactRequestProcessingResponse{}
}
c.ContactRequestLocalClock = clock
c.ContactRequestLocalState = ContactRequestStateSent
c.Removed = false
return ContactRequestProcessingResponse{processed: true}
}
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{}
}
// 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}
}
func (c *Contact) DismissContactRequest(clock uint64) ContactRequestProcessingResponse {
if clock <= c.ContactRequestLocalClock {
return ContactRequestProcessingResponse{}
}
c.ContactRequestLocalClock = clock
c.ContactRequestLocalState = ContactRequestStateDismissed
return ContactRequestProcessingResponse{processed: true}
}
// Remote actions
func (c *Contact) contactRequestRetracted(clock uint64, fromSyncing bool, r ContactRequestProcessingResponse) ContactRequestProcessingResponse {
if clock <= c.ContactRequestRemoteClock {
return r
}
// 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. The only exception is when the contact
// request has been explicitly dismissed, in which case we don't
// change state
if c.ContactRequestLocalState != ContactRequestStateDismissed && !fromSyncing {
c.ContactRequestLocalClock = clock
c.ContactRequestLocalState = ContactRequestStateNone
}
c.ContactRequestRemoteClock = clock
c.ContactRequestRemoteState = ContactRequestStateNone
r.processed = true
return r
}
func (c *Contact) ContactRequestRetracted(clock uint64, fromSyncing bool) ContactRequestProcessingResponse {
return c.contactRequestRetracted(clock, fromSyncing, ContactRequestProcessingResponse{})
}
func (c *Contact) contactRequestReceived(clock uint64, r ContactRequestProcessingResponse) ContactRequestProcessingResponse {
if clock <= c.ContactRequestRemoteClock {
return r
}
r.processed = true
c.ContactRequestRemoteClock = clock
switch c.ContactRequestRemoteState {
case ContactRequestStateNone:
r.newContactRequestReceived = true
}
c.ContactRequestRemoteState = ContactRequestStateReceived
return r
}
func (c *Contact) ContactRequestReceived(clock uint64) ContactRequestProcessingResponse {
return c.contactRequestReceived(clock, ContactRequestProcessingResponse{})
}
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)
}
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)
}
func BuildContactFromPublicKey(publicKey *ecdsa.PublicKey) (*Contact, error) {
id := common.PubkeyToHex(publicKey)
return buildContact(id, publicKey)
}
func getShortenedCompressedKey(publicKey string) string {
if len(publicKey) > 9 {
firstPart := publicKey[0:3]
ellipsis := "..."
publicKeySize := len(publicKey)
lastPart := publicKey[publicKeySize-6 : publicKeySize]
abbreviatedKey := fmt.Sprintf("%s%s%s", firstPart, ellipsis, lastPart)
return abbreviatedKey
}
return ""
}
func buildContact(publicKeyString string, publicKey *ecdsa.PublicKey) (*Contact, error) {
compressedKey, err := multiformat.SerializeLegacyKey(common.PubkeyToHex(publicKey))
if err != nil {
return nil, err
}
address := crypto.PubkeyToAddress(*publicKey)
contact := &Contact{
ID: publicKeyString,
Alias: getShortenedCompressedKey(compressedKey),
Address: types.EncodeHex(address[:]),
}
return contact, nil
}
func buildSelfContact(identity *ecdsa.PrivateKey, settings *accounts.Database, multiAccounts *multiaccounts.Database, account *multiaccounts.Account) (*Contact, error) {
myPublicKeyString := types.EncodeHex(crypto.FromECDSAPub(&identity.PublicKey))
c, err := buildContact(myPublicKeyString, &identity.PublicKey)
if err != nil {
return nil, fmt.Errorf("failed to build contact: %w", err)
}
if settings != nil {
if s, err := settings.GetSettings(); err == nil {
c.DisplayName = s.DisplayName
c.Bio = s.Bio
if s.PreferredName != nil {
c.EnsName = *s.PreferredName
}
}
if socialLinks, err := settings.GetSocialLinks(); err != nil {
c.SocialLinks = socialLinks
}
}
if multiAccounts != nil && account != nil {
if identityImages, err := multiAccounts.GetIdentityImages(account.KeyUID); err != nil {
imagesMap := make(map[string]images.IdentityImage)
for _, img := range identityImages {
imagesMap[img.Name] = *img
}
c.Images = imagesMap
}
}
return c, 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) ProcessSyncContactRequestState(remoteState ContactRequestState, remoteClock uint64, localState ContactRequestState, localClock uint64) {
// 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:
c.ContactRequestRetracted(remoteClock, true)
}
}
func (c *Contact) MarshalJSON() ([]byte, error) {
type Alias Contact
type ContactType struct {
*Alias
Added bool `json:"added"`
ContactRequestState ContactRequestState `json:"contactRequestState"`
HasAddedUs bool `json:"hasAddedUs"`
Mutual bool `json:"mutual"`
Active bool `json:"active"`
PrimaryName string `json:"primaryName"`
SecondaryName string `json:"secondaryName,omitempty"`
}
item := ContactType{
Alias: (*Alias)(c),
}
item.Added = c.added()
item.HasAddedUs = c.hasAddedUs()
item.Mutual = c.mutual()
item.Active = c.active()
item.PrimaryName = c.PrimaryName()
item.SecondaryName = c.SecondaryName()
if c.mutual() {
item.ContactRequestState = ContactRequestStateMutual
} else if c.dismissed() {
item.ContactRequestState = ContactRequestStateDismissed
} else if c.added() {
item.ContactRequestState = ContactRequestStateSent
} else if c.hasAddedUs() {
item.ContactRequestState = ContactRequestStateReceived
}
ext, err := accountJson.ExtendStructWithPubKeyData(item.ID, item)
if err != nil {
return nil, err
}
return json.Marshal(ext)
}
// 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
// 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.
c.ContactRequestRemoteState = ContactRequestStateNone
}
// We always trust this
if remoteClock > c.ContactRequestRemoteClock {
if remoteState == ContactRequestStateSent {
response = c.contactRequestReceived(remoteClock, response)
} else if remoteState == ContactRequestStateNone {
response = c.contactRequestRetracted(remoteClock, false, response)
}
}
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),
}
}