/
chat.go
121 lines (103 loc) · 3.31 KB
/
chat.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
package chat
import (
"crypto/rand"
"crypto/sha256"
backend "github.com/Bit-Nation/panthalassa/backend"
preKey "github.com/Bit-Nation/panthalassa/chat/prekey"
db "github.com/Bit-Nation/panthalassa/db"
keyManager "github.com/Bit-Nation/panthalassa/keyManager"
queue "github.com/Bit-Nation/panthalassa/queue"
uiapi "github.com/Bit-Nation/panthalassa/uiapi"
bpb "github.com/Bit-Nation/protobuffers"
x3dh "github.com/Bit-Nation/x3dh"
log "github.com/ipfs/go-log"
dr "github.com/tiabc/doubleratchet"
ed25519 "golang.org/x/crypto/ed25519"
)
var logger = log.Logger("chat")
type Backend interface {
FetchPreKeyBundle(userIDPubKey ed25519.PublicKey) (x3dh.PreKeyBundle, error)
SubmitMessages(messages []*bpb.ChatMessage) error
FetchSignedPreKey(userIdPubKey ed25519.PublicKey) (preKey.PreKey, error)
AddRequestHandler(handler backend.RequestHandler)
Close() error
}
type Chat struct {
chatStorage db.ChatStorage
backend Backend
sharedSecStorage db.SharedSecretStorage
x3dh *x3dh.X3dh
km *keyManager.KeyManager
drKeyStorage dr.KeysStorage
signedPreKeyStorage db.SignedPreKeyStorage
oneTimePreKeyStorage db.OneTimePreKeyStorage
userStorage db.UserStorage
uiApi *uiapi.Api
queue *queue.Queue
}
func (c *Chat) AllChats() ([]db.Chat, error) {
return c.chatStorage.AllChats()
}
func (c *Chat) Messages(partner ed25519.PublicKey, start int64, amount uint) ([]*db.Message, error) {
chat, err := c.chatStorage.GetChatByPartner(partner)
if err != nil {
return nil, err
}
if chat == nil {
return []*db.Message{}, nil
}
return chat.Messages(start, amount)
}
type Config struct {
ChatStorage db.ChatStorage
Backend Backend
SharedSecretDB db.SharedSecretStorage
KM *keyManager.KeyManager
DRKeyStorage dr.KeysStorage
SignedPreKeyStorage db.SignedPreKeyStorage
OneTimePreKeyStorage db.OneTimePreKeyStorage
UserStorage db.UserStorage
UiApi *uiapi.Api
Queue *queue.Queue
}
func (c *Chat) Close() error {
return c.backend.Close()
}
func NewChat(conf Config) (*Chat, error) {
// my chat id key pair
myChatIDKeyPair, err := conf.KM.ChatIdKeyPair()
if err != nil {
return nil, err
}
// curve 25519
c25519 := x3dh.NewCurve25519(rand.Reader)
myX3dh := x3dh.New(&c25519, sha256.New(), "pangea-chat", myChatIDKeyPair)
c := &Chat{
chatStorage: conf.ChatStorage,
backend: conf.Backend,
sharedSecStorage: conf.SharedSecretDB,
x3dh: &myX3dh,
km: conf.KM,
drKeyStorage: conf.DRKeyStorage,
signedPreKeyStorage: conf.SignedPreKeyStorage,
oneTimePreKeyStorage: conf.OneTimePreKeyStorage,
userStorage: conf.UserStorage,
uiApi: conf.UiApi,
queue: conf.Queue,
}
err = c.queue.RegisterProcessor(&SubmitMessagesProcessor{
chat: c,
chatDB: c.chatStorage,
queue: c.queue,
})
if err != nil {
return nil, err
}
// add message handler that will inform the ui about updates
c.chatStorage.AddListener(c.handlePersistedMessage)
// register messages handler
c.backend.AddRequestHandler(c.messagesHandler)
// register now one time pre key handler
c.backend.AddRequestHandler(c.oneTimePreKeysHandler)
return c, nil
}