2018-05-03 07:35:58 +00:00
|
|
|
package signal
|
|
|
|
|
2018-06-15 15:12:31 +00:00
|
|
|
import (
|
2018-12-11 10:23:47 +00:00
|
|
|
"encoding/hex"
|
|
|
|
|
2018-06-15 15:12:31 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common"
|
2019-08-09 07:03:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-07-01 10:00:46 +00:00
|
|
|
"github.com/status-im/status-go/services/shhext/dedup"
|
2019-05-17 11:06:56 +00:00
|
|
|
whisper "github.com/status-im/whisper/whisperv6"
|
2019-07-17 22:25:42 +00:00
|
|
|
|
2019-08-09 07:03:10 +00:00
|
|
|
statustransp "github.com/status-im/status-protocol-go/transport/whisper"
|
2018-06-15 15:12:31 +00:00
|
|
|
)
|
2018-05-03 07:35:58 +00:00
|
|
|
|
|
|
|
const (
|
|
|
|
// EventEnvelopeSent is triggered when envelope was sent at least to a one peer.
|
|
|
|
EventEnvelopeSent = "envelope.sent"
|
|
|
|
|
|
|
|
// EventEnvelopeExpired is triggered when envelop was dropped by a whisper without being sent
|
|
|
|
// to any peer
|
|
|
|
EventEnvelopeExpired = "envelope.expired"
|
2018-06-15 15:12:31 +00:00
|
|
|
|
2019-04-30 06:46:12 +00:00
|
|
|
// EventEnvelopeDiscarded is triggerd when envelope was discarded by a peer for some reason.
|
|
|
|
EventEnvelopeDiscarded = "envelope.discarded"
|
|
|
|
|
2018-06-15 15:12:31 +00:00
|
|
|
// EventMailServerRequestCompleted is triggered when whisper receives a message ack from the mailserver
|
|
|
|
EventMailServerRequestCompleted = "mailserver.request.completed"
|
|
|
|
|
|
|
|
// EventMailServerRequestExpired is triggered when request TTL ends
|
|
|
|
EventMailServerRequestExpired = "mailserver.request.expired"
|
2018-07-16 07:40:40 +00:00
|
|
|
|
|
|
|
// EventEnodeDiscovered is tiggered when enode has been discovered.
|
|
|
|
EventEnodeDiscovered = "enode.discovered"
|
2018-09-24 18:07:34 +00:00
|
|
|
|
|
|
|
// EventDecryptMessageFailed is triggered when we receive a message from a bundle we don't have
|
|
|
|
EventDecryptMessageFailed = "messages.decrypt.failed"
|
2018-10-16 10:31:05 +00:00
|
|
|
|
|
|
|
// EventBundleAdded is triggered when we receive a bundle
|
|
|
|
EventBundleAdded = "bundles.added"
|
2019-05-17 11:06:56 +00:00
|
|
|
|
|
|
|
// EventWhisperFilterAdded is triggered when we setup a new filter or restore existing ones
|
|
|
|
EventWhisperFilterAdded = "whisper.filter.added"
|
2019-07-01 10:00:46 +00:00
|
|
|
|
|
|
|
// EventNewMessages is triggered when we receive new messages
|
|
|
|
EventNewMessages = "messages.new"
|
2018-05-03 07:35:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EnvelopeSignal includes hash of the envelope.
|
|
|
|
type EnvelopeSignal struct {
|
2019-08-09 07:03:10 +00:00
|
|
|
IDs []hexutil.Bytes `json:"ids"`
|
|
|
|
Hash common.Hash `json:"hash"`
|
|
|
|
Message string `json:"message"`
|
2018-05-03 07:35:58 +00:00
|
|
|
}
|
|
|
|
|
2018-07-02 07:38:10 +00:00
|
|
|
// MailServerResponseSignal holds the data received in the response from the mailserver.
|
|
|
|
type MailServerResponseSignal struct {
|
|
|
|
RequestID common.Hash `json:"requestID"`
|
|
|
|
LastEnvelopeHash common.Hash `json:"lastEnvelopeHash"`
|
|
|
|
Cursor string `json:"cursor"`
|
2018-10-18 10:25:00 +00:00
|
|
|
ErrorMsg string `json:"errorMessage"`
|
2018-07-02 07:38:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-24 18:07:34 +00:00
|
|
|
// DecryptMessageFailedSignal holds the sender of the message that could not be decrypted
|
|
|
|
type DecryptMessageFailedSignal struct {
|
|
|
|
Sender string `json:"sender"`
|
|
|
|
}
|
|
|
|
|
2018-10-16 10:31:05 +00:00
|
|
|
// BundleAddedSignal holds the identity and installation id of the user
|
|
|
|
type BundleAddedSignal struct {
|
|
|
|
Identity string `json:"identity"`
|
|
|
|
InstallationID string `json:"installationID"`
|
|
|
|
}
|
|
|
|
|
2019-05-17 11:06:56 +00:00
|
|
|
type Filter struct {
|
2019-05-23 08:47:20 +00:00
|
|
|
// ChatID is the identifier of the chat
|
|
|
|
ChatID string `json:"chatId"`
|
|
|
|
// SymKeyID is the symmetric key id used for symmetric chats
|
|
|
|
SymKeyID string `json:"symKeyId"`
|
|
|
|
// OneToOne tells us if we need to use asymmetric encryption for this chat
|
|
|
|
Listen bool `json:"listen"`
|
|
|
|
// FilterID the whisper filter id generated
|
|
|
|
FilterID string `json:"filterId"`
|
|
|
|
// Identity is the public key of the other recipient for non-public chats
|
|
|
|
Identity string `json:"identity"`
|
|
|
|
// Topic is the whisper topic
|
|
|
|
Topic whisper.TopicType `json:"topic"`
|
2019-05-17 11:06:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type WhisperFilterAddedSignal struct {
|
|
|
|
Filters []*Filter `json:"filters"`
|
|
|
|
}
|
|
|
|
|
2019-07-01 10:00:46 +00:00
|
|
|
// NewMessagesSignal notifies clients of new messages
|
|
|
|
type NewMessagesSignal struct {
|
|
|
|
Messages []*Messages `json:"messages"`
|
|
|
|
}
|
|
|
|
|
2018-05-03 07:35:58 +00:00
|
|
|
// SendEnvelopeSent triggered when envelope delivered at least to 1 peer.
|
2019-08-09 07:03:10 +00:00
|
|
|
func SendEnvelopeSent(identifiers [][]byte) {
|
|
|
|
var hexIdentifiers []hexutil.Bytes
|
|
|
|
for _, i := range identifiers {
|
|
|
|
hexIdentifiers = append(hexIdentifiers, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
send(EventEnvelopeSent, EnvelopeSignal{
|
|
|
|
IDs: hexIdentifiers,
|
|
|
|
})
|
2018-05-03 07:35:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendEnvelopeExpired triggered when envelope delivered at least to 1 peer.
|
2019-08-09 07:03:10 +00:00
|
|
|
func SendEnvelopeExpired(identifiers [][]byte, err error) {
|
2019-04-02 10:40:45 +00:00
|
|
|
var message string
|
|
|
|
if err != nil {
|
|
|
|
message = err.Error()
|
|
|
|
}
|
2019-08-09 07:03:10 +00:00
|
|
|
var hexIdentifiers []hexutil.Bytes
|
|
|
|
for _, i := range identifiers {
|
|
|
|
hexIdentifiers = append(hexIdentifiers, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
send(EventEnvelopeExpired, EnvelopeSignal{IDs: hexIdentifiers, Message: message})
|
2018-05-03 07:35:58 +00:00
|
|
|
}
|
2018-06-15 15:12:31 +00:00
|
|
|
|
|
|
|
// SendMailServerRequestCompleted triggered when mail server response has been received
|
2018-10-18 10:25:00 +00:00
|
|
|
func SendMailServerRequestCompleted(requestID common.Hash, lastEnvelopeHash common.Hash, cursor []byte, err error) {
|
|
|
|
errorMsg := ""
|
|
|
|
if err != nil {
|
|
|
|
errorMsg = err.Error()
|
|
|
|
}
|
2018-07-02 07:38:10 +00:00
|
|
|
sig := MailServerResponseSignal{
|
|
|
|
RequestID: requestID,
|
|
|
|
LastEnvelopeHash: lastEnvelopeHash,
|
2018-12-11 10:23:47 +00:00
|
|
|
Cursor: hex.EncodeToString(cursor),
|
2018-10-18 10:25:00 +00:00
|
|
|
ErrorMsg: errorMsg,
|
2018-07-02 07:38:10 +00:00
|
|
|
}
|
|
|
|
send(EventMailServerRequestCompleted, sig)
|
2018-06-15 15:12:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SendMailServerRequestExpired triggered when mail server request expires
|
|
|
|
func SendMailServerRequestExpired(hash common.Hash) {
|
2019-04-02 10:40:45 +00:00
|
|
|
send(EventMailServerRequestExpired, EnvelopeSignal{Hash: hash})
|
2018-06-15 15:12:31 +00:00
|
|
|
}
|
2018-07-16 07:40:40 +00:00
|
|
|
|
|
|
|
// EnodeDiscoveredSignal includes enode address and topic
|
|
|
|
type EnodeDiscoveredSignal struct {
|
|
|
|
Enode string `json:"enode"`
|
|
|
|
Topic string `json:"topic"`
|
|
|
|
}
|
|
|
|
|
2019-07-01 10:00:46 +00:00
|
|
|
type Messages struct {
|
2019-08-20 11:20:25 +00:00
|
|
|
Error error `json:"error"`
|
|
|
|
Messages []*dedup.DeduplicateMessage `json:"messages"`
|
|
|
|
Chat statustransp.Filter `json:"chat"` // not a mistake, it's called chat in status-react
|
2019-07-01 10:00:46 +00:00
|
|
|
}
|
|
|
|
|
2018-07-16 07:40:40 +00:00
|
|
|
// SendEnodeDiscovered tiggered when an enode is discovered.
|
|
|
|
// finds a new enode.
|
|
|
|
func SendEnodeDiscovered(enode, topic string) {
|
|
|
|
send(EventEnodeDiscovered, EnodeDiscoveredSignal{
|
|
|
|
Enode: enode,
|
|
|
|
Topic: topic,
|
|
|
|
})
|
|
|
|
}
|
2018-09-24 18:07:34 +00:00
|
|
|
|
|
|
|
func SendDecryptMessageFailed(sender string) {
|
|
|
|
send(EventDecryptMessageFailed, DecryptMessageFailedSignal{sender})
|
|
|
|
}
|
2018-10-16 10:31:05 +00:00
|
|
|
|
|
|
|
func SendBundleAdded(identity string, installationID string) {
|
|
|
|
send(EventBundleAdded, BundleAddedSignal{Identity: identity, InstallationID: installationID})
|
|
|
|
}
|
2019-05-17 11:06:56 +00:00
|
|
|
|
|
|
|
func SendWhisperFilterAdded(filters []*Filter) {
|
|
|
|
send(EventWhisperFilterAdded, WhisperFilterAddedSignal{Filters: filters})
|
|
|
|
}
|
2019-07-01 10:00:46 +00:00
|
|
|
|
|
|
|
func SendNewMessages(messages []*Messages) {
|
|
|
|
send(EventNewMessages, NewMessagesSignal{Messages: messages})
|
|
|
|
}
|