-
Notifications
You must be signed in to change notification settings - Fork 243
/
messenger_share_urls.go
577 lines (479 loc) · 14.9 KB
/
messenger_share_urls.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
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package protocol
import (
"fmt"
"regexp"
"strings"
"github.com/golang/protobuf/proto"
"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/protocol/common"
"github.com/status-im/status-go/protocol/common/shard"
"github.com/status-im/status-go/protocol/communities"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/requests"
"github.com/status-im/status-go/protocol/urls"
"github.com/status-im/status-go/services/utils"
)
type CommunityURLData struct {
DisplayName string `json:"displayName"`
Description string `json:"description"`
MembersCount uint32 `json:"membersCount"`
Color string `json:"color"`
TagIndices []uint32 `json:"tagIndices"`
CommunityID string `json:"communityId"`
}
type CommunityChannelURLData struct {
Emoji string `json:"emoji"`
DisplayName string `json:"displayName"`
Description string `json:"description"`
Color string `json:"color"`
ChannelUUID string `json:"channelUuid"`
}
type ContactURLData struct {
DisplayName string `json:"displayName"`
Description string `json:"description"`
PublicKey string `json:"publicKey"`
}
type URLDataResponse struct {
Community *CommunityURLData `json:"community"`
Channel *CommunityChannelURLData `json:"channel"`
Contact *ContactURLData `json:"contact"`
Shard *shard.Shard `json:"shard,omitempty"`
}
const baseShareURL = "https://status.app"
const userPath = "u#"
const userWithDataPath = "u/"
const communityPath = "c#"
const communityWithDataPath = "c/"
const channelPath = "cc/"
const sharedURLUserPrefix = baseShareURL + "/" + userPath
const sharedURLUserPrefixWithData = baseShareURL + "/" + userWithDataPath
const sharedURLCommunityPrefix = baseShareURL + "/" + communityPath
const sharedURLCommunityPrefixWithData = baseShareURL + "/" + communityWithDataPath
const sharedURLChannelPrefixWithData = baseShareURL + "/" + channelPath
const channelUUIDRegExp = "^[0-9a-f]{8}-[0-9a-f]{4}-[0-5][0-9a-f]{3}-[089ab][0-9a-f]{3}-[0-9a-f]{12}$"
var channelRegExp = regexp.MustCompile(channelUUIDRegExp)
func decodeCommunityID(serialisedPublicKey string) (string, error) {
deserializedCommunityID, err := multiformat.DeserializeCompressedKey(serialisedPublicKey)
if err != nil {
return "", err
}
communityID, err := common.HexToPubkey(deserializedCommunityID)
if err != nil {
return "", err
}
return types.EncodeHex(crypto.CompressPubkey(communityID)), nil
}
func serializePublicKey(compressedKey types.HexBytes) (string, error) {
return utils.SerializePublicKey(compressedKey)
}
func deserializePublicKey(compressedKey string) (types.HexBytes, error) {
return utils.DeserializePublicKey(compressedKey)
}
func (m *Messenger) ShareCommunityURLWithChatKey(communityID types.HexBytes) (string, error) {
shortKey, err := serializePublicKey(communityID)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/c#%s", baseShareURL, shortKey), nil
}
func parseCommunityURLWithChatKey(urlData string) (*URLDataResponse, error) {
communityID, err := decodeCommunityID(urlData)
if err != nil {
return nil, err
}
return &URLDataResponse{
Community: &CommunityURLData{
CommunityID: communityID,
TagIndices: []uint32{},
},
Shard: nil,
}, nil
}
func (m *Messenger) prepareEncodedCommunityData(community *communities.Community) (string, string, error) {
communityProto := &protobuf.Community{
DisplayName: community.Identity().DisplayName,
Description: community.DescriptionText(),
MembersCount: uint32(community.MembersCount()),
Color: community.Identity().GetColor(),
TagIndices: community.TagsIndices(),
}
communityData, err := proto.Marshal(communityProto)
if err != nil {
return "", "", err
}
urlDataProto := &protobuf.URLData{
Content: communityData,
Shard: community.Shard().Protobuffer(),
}
urlData, err := proto.Marshal(urlDataProto)
if err != nil {
return "", "", err
}
shortKey, err := serializePublicKey(community.ID())
if err != nil {
return "", "", err
}
encodedData, err := urls.EncodeDataURL(urlData)
if err != nil {
return "", "", err
}
return encodedData, shortKey, nil
}
func (m *Messenger) ShareCommunityURLWithData(communityID types.HexBytes) (string, error) {
community, err := m.GetCommunityByID(communityID)
if err != nil {
return "", err
}
data, shortKey, err := m.prepareEncodedCommunityData(community)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/c/%s#%s", baseShareURL, data, shortKey), nil
}
func parseCommunityURLWithData(data string, chatKey string) (*URLDataResponse, error) {
communityID, err := deserializePublicKey(chatKey)
if err != nil {
return nil, err
}
urlData, err := urls.DecodeDataURL(data)
if err != nil {
return nil, err
}
var urlDataProto protobuf.URLData
err = proto.Unmarshal(urlData, &urlDataProto)
if err != nil {
return nil, err
}
var communityProto protobuf.Community
err = proto.Unmarshal(urlDataProto.Content, &communityProto)
if err != nil {
return nil, err
}
var tagIndices []uint32
if communityProto.TagIndices != nil {
tagIndices = communityProto.TagIndices
} else {
tagIndices = []uint32{}
}
return &URLDataResponse{
Community: &CommunityURLData{
DisplayName: communityProto.DisplayName,
Description: communityProto.Description,
MembersCount: communityProto.MembersCount,
Color: communityProto.Color,
TagIndices: tagIndices,
CommunityID: types.EncodeHex(communityID),
},
Shard: shard.FromProtobuff(urlDataProto.Shard),
}, nil
}
func (m *Messenger) ShareCommunityChannelURLWithChatKey(request *requests.CommunityChannelShareURL) (string, error) {
if err := request.Validate(); err != nil {
return "", err
}
shortKey, err := serializePublicKey(request.CommunityID)
if err != nil {
return "", err
}
valid, err := regexp.MatchString(channelUUIDRegExp, request.ChannelID)
if err != nil {
return "", err
}
if !valid {
return "", fmt.Errorf("channelID should be UUID, got %s", request.ChannelID)
}
return fmt.Sprintf("%s/cc/%s#%s", baseShareURL, request.ChannelID, shortKey), nil
}
func parseCommunityChannelURLWithChatKey(channelID string, publicKey string) (*URLDataResponse, error) {
valid, err := regexp.MatchString(channelUUIDRegExp, channelID)
if err != nil {
return nil, err
}
if !valid {
return nil, fmt.Errorf("channelID should be UUID, got %s", channelID)
}
communityID, err := decodeCommunityID(publicKey)
if err != nil {
return nil, err
}
return &URLDataResponse{
Community: &CommunityURLData{
CommunityID: communityID,
TagIndices: []uint32{},
},
Channel: &CommunityChannelURLData{
ChannelUUID: channelID,
},
Shard: nil,
}, nil
}
func (m *Messenger) prepareEncodedCommunityChannelData(community *communities.Community, channel *protobuf.CommunityChat, channelID string) (string, string, error) {
communityProto := &protobuf.Community{
DisplayName: community.Identity().DisplayName,
Description: community.DescriptionText(),
MembersCount: uint32(community.MembersCount()),
Color: community.Identity().GetColor(),
TagIndices: community.TagsIndices(),
}
channelProto := &protobuf.Channel{
DisplayName: channel.Identity.DisplayName,
Description: channel.Identity.Description,
Emoji: channel.Identity.Emoji,
Color: channel.GetIdentity().Color,
Community: communityProto,
Uuid: channelID,
}
channelData, err := proto.Marshal(channelProto)
if err != nil {
return "", "", err
}
urlDataProto := &protobuf.URLData{
Content: channelData,
Shard: community.Shard().Protobuffer(),
}
urlData, err := proto.Marshal(urlDataProto)
if err != nil {
return "", "", err
}
shortKey, err := serializePublicKey(community.ID())
if err != nil {
return "", "", err
}
encodedData, err := urls.EncodeDataURL(urlData)
if err != nil {
return "", "", err
}
return encodedData, shortKey, nil
}
func (m *Messenger) ShareCommunityChannelURLWithData(request *requests.CommunityChannelShareURL) (string, error) {
if err := request.Validate(); err != nil {
return "", err
}
valid, err := regexp.MatchString(channelUUIDRegExp, request.ChannelID)
if err != nil {
return "", err
}
if !valid {
return "nil", fmt.Errorf("channelID should be UUID, got %s", request.ChannelID)
}
community, err := m.GetCommunityByID(request.CommunityID)
if err != nil {
return "", err
}
channel := community.Chats()[request.ChannelID]
if channel == nil {
return "", fmt.Errorf("channel with channelID %s not found", request.ChannelID)
}
data, shortKey, err := m.prepareEncodedCommunityChannelData(community, channel, request.ChannelID)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/cc/%s#%s", baseShareURL, data, shortKey), nil
}
func parseCommunityChannelURLWithData(data string, chatKey string) (*URLDataResponse, error) {
communityID, err := deserializePublicKey(chatKey)
if err != nil {
return nil, err
}
urlData, err := urls.DecodeDataURL(data)
if err != nil {
return nil, err
}
var urlDataProto protobuf.URLData
err = proto.Unmarshal(urlData, &urlDataProto)
if err != nil {
return nil, err
}
var channelProto protobuf.Channel
err = proto.Unmarshal(urlDataProto.Content, &channelProto)
if err != nil {
return nil, err
}
var tagIndices []uint32
if channelProto.Community.TagIndices != nil {
tagIndices = channelProto.Community.TagIndices
} else {
tagIndices = []uint32{}
}
return &URLDataResponse{
Community: &CommunityURLData{
DisplayName: channelProto.Community.DisplayName,
Description: channelProto.Community.Description,
MembersCount: channelProto.Community.MembersCount,
Color: channelProto.Community.Color,
TagIndices: tagIndices,
CommunityID: types.EncodeHex(communityID),
},
Channel: &CommunityChannelURLData{
Emoji: channelProto.Emoji,
DisplayName: channelProto.DisplayName,
Description: channelProto.Description,
Color: channelProto.Color,
ChannelUUID: channelProto.Uuid,
},
Shard: shard.FromProtobuff(urlDataProto.Shard),
}, nil
}
func (m *Messenger) ShareUserURLWithChatKey(contactID string) (string, error) {
publicKey, err := common.HexToPubkey(contactID)
if err != nil {
return "", err
}
shortKey, err := serializePublicKey(crypto.CompressPubkey(publicKey))
if err != nil {
return "", err
}
return fmt.Sprintf("%s/u#%s", baseShareURL, shortKey), nil
}
func parseUserURLWithChatKey(urlData string) (*URLDataResponse, error) {
pubKeyBytes, err := deserializePublicKey(urlData)
if err != nil {
return nil, err
}
pubKey, err := crypto.DecompressPubkey(pubKeyBytes)
if err != nil {
return nil, err
}
serializedPublicKey, err := multiformat.SerializeLegacyKey(common.PubkeyToHex(pubKey))
if err != nil {
return nil, err
}
return &URLDataResponse{
Contact: &ContactURLData{
PublicKey: serializedPublicKey,
},
}, nil
}
func (m *Messenger) ShareUserURLWithENS(contactID string) (string, error) {
contact := m.GetContactByID(contactID)
if contact == nil {
return "", ErrContactNotFound
}
return fmt.Sprintf("%s/u#%s", baseShareURL, contact.EnsName), nil
}
func parseUserURLWithENS(ensName string) (*URLDataResponse, error) {
// TODO: fetch contact by ens name
return nil, fmt.Errorf("not implemented yet")
}
func (m *Messenger) prepareEncodedUserData(contact *Contact) (string, string, error) {
pk, err := contact.PublicKey()
if err != nil {
return "", "", err
}
shortKey, err := serializePublicKey(crypto.CompressPubkey(pk))
if err != nil {
return "", "", err
}
userProto := &protobuf.User{
DisplayName: contact.DisplayName,
Description: contact.Bio,
}
userData, err := proto.Marshal(userProto)
if err != nil {
return "", "", err
}
urlDataProto := &protobuf.URLData{
Content: userData,
}
urlData, err := proto.Marshal(urlDataProto)
if err != nil {
return "", "", err
}
encodedData, err := urls.EncodeDataURL(urlData)
if err != nil {
return "", "", err
}
return encodedData, shortKey, nil
}
func (m *Messenger) ShareUserURLWithData(contactID string) (string, error) {
contact := m.GetContactByID(contactID)
if contact == nil {
return "", ErrContactNotFound
}
data, shortKey, err := m.prepareEncodedUserData(contact)
if err != nil {
return "", err
}
return fmt.Sprintf("%s/u/%s#%s", baseShareURL, data, shortKey), nil
}
func parseUserURLWithData(data string, chatKey string) (*URLDataResponse, error) {
urlData, err := urls.DecodeDataURL(data)
if err != nil {
return nil, err
}
var urlDataProto protobuf.URLData
err = proto.Unmarshal(urlData, &urlDataProto)
if err != nil {
return nil, err
}
var userProto protobuf.User
err = proto.Unmarshal(urlDataProto.Content, &userProto)
if err != nil {
return nil, err
}
return &URLDataResponse{
Contact: &ContactURLData{
DisplayName: userProto.DisplayName,
Description: userProto.Description,
PublicKey: chatKey,
},
}, nil
}
func IsStatusSharedURL(url string) bool {
return strings.HasPrefix(url, sharedURLUserPrefix) ||
strings.HasPrefix(url, sharedURLUserPrefixWithData) ||
strings.HasPrefix(url, sharedURLCommunityPrefix) ||
strings.HasPrefix(url, sharedURLCommunityPrefixWithData) ||
strings.HasPrefix(url, sharedURLChannelPrefixWithData)
}
func splitSharedURLData(data string) (string, string, error) {
const count = 2
contents := strings.SplitN(data, "#", count)
if len(contents) != count {
return "", "", fmt.Errorf("url should contain at least one `#` separator")
}
return contents[0], contents[1], nil
}
func ParseSharedURL(url string) (*URLDataResponse, error) {
if strings.HasPrefix(url, sharedURLUserPrefix) {
chatKey := strings.TrimPrefix(url, sharedURLUserPrefix)
if strings.HasPrefix(chatKey, "zQ3sh") {
return parseUserURLWithChatKey(chatKey)
}
return parseUserURLWithENS(chatKey)
}
if strings.HasPrefix(url, sharedURLUserPrefixWithData) {
trimmedURL := strings.TrimPrefix(url, sharedURLUserPrefixWithData)
encodedData, chatKey, err := splitSharedURLData(trimmedURL)
if err != nil {
return nil, err
}
return parseUserURLWithData(encodedData, chatKey)
}
if strings.HasPrefix(url, sharedURLCommunityPrefix) {
chatKey := strings.TrimPrefix(url, sharedURLCommunityPrefix)
return parseCommunityURLWithChatKey(chatKey)
}
if strings.HasPrefix(url, sharedURLCommunityPrefixWithData) {
trimmedURL := strings.TrimPrefix(url, sharedURLCommunityPrefixWithData)
encodedData, chatKey, err := splitSharedURLData(trimmedURL)
if err != nil {
return nil, err
}
return parseCommunityURLWithData(encodedData, chatKey)
}
if strings.HasPrefix(url, sharedURLChannelPrefixWithData) {
trimmedURL := strings.TrimPrefix(url, sharedURLChannelPrefixWithData)
encodedData, chatKey, err := splitSharedURLData(trimmedURL)
if err != nil {
return nil, err
}
if channelRegExp.MatchString(encodedData) {
return parseCommunityChannelURLWithChatKey(encodedData, chatKey)
}
return parseCommunityChannelURLWithData(encodedData, chatKey)
}
return nil, fmt.Errorf("not a status shared url")
}