-
Notifications
You must be signed in to change notification settings - Fork 376
/
util.go
74 lines (57 loc) · 1.68 KB
/
util.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
package messengerutil
import (
"bytes"
"context"
"encoding/base64"
"fmt"
"time"
ipfscid "github.com/ipfs/go-cid"
"berty.tech/berty/v2/go/pkg/errcode"
"berty.tech/berty/v2/go/pkg/protocoltypes"
)
const (
TyberEventAcknowledgeReceived = "Acknowledge received"
)
func CheckDeviceIsMe(ctx context.Context, client protocoltypes.ProtocolServiceClient, gme *protocoltypes.GroupMessageEvent) (bool, error) {
gpkb := gme.GetEventContext().GetGroupPK()
// TODO: support multiple devices per account
gi, err := client.GroupInfo(ctx, &protocoltypes.GroupInfo_Request{GroupPK: gpkb})
if err != nil {
return false, err
}
dpk := gi.GetDevicePK()
mdpk := gme.GetHeaders().GetDevicePK()
return bytes.Equal(dpk, mdpk), nil
}
func GroupPKFromContactPK(ctx context.Context, client protocoltypes.ProtocolServiceClient, contactPK []byte) ([]byte, error) {
req := &protocoltypes.GroupInfo_Request{ContactPK: contactPK}
groupInfo, err := client.GroupInfo(ctx, req)
if err != nil {
return nil, err
}
groupPK := groupInfo.GetGroup().GetPublicKey()
if groupPK == nil {
return nil, errcode.ErrInternal.Wrap(fmt.Errorf("group pk is empty"))
}
return groupPK, nil
}
func B64EncodeBytes(b []byte) string {
return base64.RawURLEncoding.EncodeToString(b)
}
func B64DecodeBytes(s string) ([]byte, error) {
return base64.RawURLEncoding.DecodeString(s)
}
func TimestampMs(t time.Time) int64 {
return t.UnixNano() / 1000000
}
func EnsureValidBase64CID(str string) error {
cidBytes, err := B64DecodeBytes(str)
if err != nil {
return fmt.Errorf("decode base64: %s", err.Error())
}
_, err = ipfscid.Cast(cidBytes)
if err != nil {
return fmt.Errorf("decode cid: %s", err.Error())
}
return nil
}