-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcontact.go
257 lines (206 loc) · 6.61 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
package protocol
import (
"crypto/ecdsa"
"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/settings"
"github.com/status-im/status-go/protocol/common"
"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"
)
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 {
return c.EnsName
}
return c.Alias
}
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"`
SocialLinks identity.SocialLinks `json:"socialLinks"`
Images map[string]images.IdentityImage `json:"images"`
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
}
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() {
c.Blocked = true
c.Added = false
}
func (c *Contact) BlockDesktop() {
c.Blocked = true
}
func (c *Contact) Unblock() {
c.Blocked = false
}
func (c *Contact) Remove() {
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)
}
func BuildContactFromPublicKey(publicKey *ecdsa.PublicKey) (*Contact, error) {
id := common.PubkeyToHex(publicKey)
return buildContact(id, publicKey)
}
func buildContact(publicKeyString string, publicKey *ecdsa.PublicKey) (*Contact, error) {
newIdenticon, err := identicon.GenerateBase64(publicKeyString)
if err != nil {
return nil, err
}
contact := &Contact{
ID: publicKeyString,
Alias: alias.GenerateFromPublicKey(publicKey),
Identicon: newIdenticon,
}
return contact, nil
}
func contactIDFromPublicKey(key *ecdsa.PublicKey) string {
return types.EncodeHex(crypto.FromECDSAPub(key))
}