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"
|
2021-03-31 16:23:45 +00:00
|
|
|
"encoding/json"
|
2018-12-11 10:23:47 +00:00
|
|
|
|
2022-11-24 21:09:17 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
|
|
|
|
2019-08-09 07:03:10 +00:00
|
|
|
"github.com/ethereum/go-ethereum/common/hexutil"
|
2019-11-23 17:57:05 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
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
|
|
|
|
|
|
|
// 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
|
|
|
|
2019-07-01 10:00:46 +00:00
|
|
|
// EventNewMessages is triggered when we receive new messages
|
|
|
|
EventNewMessages = "messages.new"
|
2021-09-28 18:41:35 +00:00
|
|
|
|
2022-11-24 21:09:17 +00:00
|
|
|
// EventHistoryRequestStarted is triggered before processing a store request
|
2021-09-28 18:41:35 +00:00
|
|
|
EventHistoryRequestStarted = "history.request.started"
|
|
|
|
|
2022-11-24 21:09:17 +00:00
|
|
|
// EventHistoryRequestCompleted is triggered after processing all storenode requests
|
2021-09-28 18:41:35 +00:00
|
|
|
EventHistoryRequestCompleted = "history.request.completed"
|
|
|
|
|
|
|
|
// EventHistoryRequestFailed is triggered when requesting history messages fails
|
|
|
|
EventHistoryRequestFailed = "history.request.failed"
|
2021-10-11 15:39:52 +00:00
|
|
|
|
2022-11-24 21:09:17 +00:00
|
|
|
// EventHistoryRequestFailed is triggered when requesting history messages succeeds
|
|
|
|
EventHistoryRequestSuccess = "history.request.success"
|
|
|
|
|
2021-10-11 15:39:52 +00:00
|
|
|
// EventBackupPerformed is triggered when a backup has been performed
|
|
|
|
EventBackupPerformed = "backup.performed"
|
2022-01-12 16:02:01 +00:00
|
|
|
|
|
|
|
// EventMailserverAvailable is triggered when a mailserver becomes available
|
|
|
|
EventMailserverAvailable = "mailserver.available"
|
|
|
|
|
|
|
|
// EventMailserverChanged is triggered when switching the active mailserver
|
|
|
|
EventMailserverChanged = "mailserver.changed"
|
2022-01-31 10:33:56 +00:00
|
|
|
|
|
|
|
// EventMailserverNotWorking is triggered when the mailserver has failed to connect or failed to respond to requests
|
|
|
|
EventMailserverNotWorking = "mailserver.not.working"
|
2022-06-08 12:38:26 +00:00
|
|
|
|
|
|
|
// EventUpdateAvailable is triggered after a update verification is performed
|
|
|
|
EventUpdateAvailable = "update.available"
|
2018-05-03 07:35:58 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EnvelopeSignal includes hash of the envelope.
|
|
|
|
type EnvelopeSignal struct {
|
2019-11-21 16:19:22 +00:00
|
|
|
IDs []hexutil.Bytes `json:"ids"`
|
2019-11-23 17:57:05 +00:00
|
|
|
Hash types.Hash `json:"hash"`
|
2019-11-21 16:19:22 +00:00
|
|
|
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 {
|
2019-11-23 17:57:05 +00:00
|
|
|
RequestID types.Hash `json:"requestID"`
|
|
|
|
LastEnvelopeHash types.Hash `json:"lastEnvelopeHash"`
|
|
|
|
Cursor string `json:"cursor"`
|
|
|
|
ErrorMsg string `json:"errorMessage"`
|
2018-07-02 07:38:10 +00:00
|
|
|
}
|
|
|
|
|
2021-09-28 18:41:35 +00:00
|
|
|
type HistoryMessagesSignal struct {
|
2021-11-09 21:16:19 +00:00
|
|
|
RequestID string `json:"requestId"`
|
2022-11-24 21:09:17 +00:00
|
|
|
PeerID string `json:"peerId"`
|
2021-11-09 21:16:19 +00:00
|
|
|
BatchIndex int `json:"batchIndex"`
|
|
|
|
NumBatches int `json:"numBatches,omitempty"`
|
|
|
|
ErrorMsg string `json:"errorMessage,omitempty"`
|
2021-09-28 18:41:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 12:38:26 +00:00
|
|
|
type UpdateAvailableSignal struct {
|
|
|
|
Available bool `json:"available"`
|
|
|
|
Version string `json:"version"`
|
|
|
|
URL string `json:"url"`
|
|
|
|
}
|
|
|
|
|
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"`
|
|
|
|
}
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
type MailserverSignal struct {
|
|
|
|
Address string `json:"address"`
|
2022-01-31 10:33:56 +00:00
|
|
|
ID string `json:"id"`
|
2022-01-12 16:02:01 +00:00
|
|
|
}
|
|
|
|
|
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
|
2019-11-23 17:57:05 +00:00
|
|
|
Topic types.TopicType `json:"topic"`
|
2019-05-17 11:06:56 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-11-24 21:09:17 +00:00
|
|
|
func SendHistoricMessagesRequestStarted(numBatches int) {
|
|
|
|
send(EventHistoryRequestStarted, HistoryMessagesSignal{NumBatches: numBatches})
|
2021-11-09 21:16:19 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 21:09:17 +00:00
|
|
|
func SendHistoricMessagesRequestFailed(requestID []byte, peerID peer.ID, err error) {
|
|
|
|
send(EventHistoryRequestFailed, HistoryMessagesSignal{RequestID: hex.EncodeToString(requestID), PeerID: peerID.String(), ErrorMsg: err.Error()})
|
2021-09-28 18:41:35 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 21:09:17 +00:00
|
|
|
func SendHistoricMessagesRequestSuccess(requestID []byte, peerID peer.ID) {
|
|
|
|
send(EventHistoryRequestSuccess, HistoryMessagesSignal{RequestID: hex.EncodeToString(requestID), PeerID: peerID.String()})
|
2021-09-28 18:41:35 +00:00
|
|
|
}
|
|
|
|
|
2022-11-24 21:09:17 +00:00
|
|
|
func SendHistoricMessagesRequestCompleted() {
|
|
|
|
send(EventHistoryRequestCompleted, HistoryMessagesSignal{})
|
2021-09-28 18:41:35 +00:00
|
|
|
}
|
|
|
|
|
2022-06-08 12:38:26 +00:00
|
|
|
func SendUpdateAvailable(available bool, latestVersion string, url string) {
|
|
|
|
send(EventUpdateAvailable, UpdateAvailableSignal{Available: available, Version: latestVersion, URL: url})
|
|
|
|
}
|
|
|
|
|
2018-06-15 15:12:31 +00:00
|
|
|
// SendMailServerRequestCompleted triggered when mail server response has been received
|
2019-11-23 17:57:05 +00:00
|
|
|
func SendMailServerRequestCompleted(requestID types.Hash, lastEnvelopeHash types.Hash, cursor []byte, err error) {
|
2018-10-18 10:25:00 +00:00
|
|
|
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
|
2019-11-23 17:57:05 +00:00
|
|
|
func SendMailServerRequestExpired(hash types.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"`
|
|
|
|
}
|
|
|
|
|
2021-10-11 15:39:52 +00:00
|
|
|
// BackupPerformedSignal signals that a backup has been performed
|
|
|
|
type BackupPerformedSignal struct {
|
|
|
|
LastBackup uint64 `json:"lastBackup"`
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2021-10-11 15:39:52 +00:00
|
|
|
func SendBackupPerformed(lastBackup uint64) {
|
|
|
|
send(EventBackupPerformed, BackupPerformedSignal{lastBackup})
|
|
|
|
}
|
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
|
|
|
|
2021-03-31 16:23:45 +00:00
|
|
|
func SendNewMessages(obj json.Marshaler) {
|
|
|
|
send(EventNewMessages, obj)
|
2019-07-01 10:00:46 +00:00
|
|
|
}
|
2022-01-12 16:02:01 +00:00
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
func SendMailserverAvailable(nodeAddress, id string) {
|
2022-01-12 16:02:01 +00:00
|
|
|
send(EventMailserverAvailable, MailserverSignal{
|
|
|
|
Address: nodeAddress,
|
2022-01-31 10:33:56 +00:00
|
|
|
ID: id,
|
2022-01-12 16:02:01 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-01-31 10:33:56 +00:00
|
|
|
func SendMailserverChanged(nodeAddress, id string) {
|
2022-01-12 16:02:01 +00:00
|
|
|
send(EventMailserverChanged, MailserverSignal{
|
|
|
|
Address: nodeAddress,
|
2022-01-31 10:33:56 +00:00
|
|
|
ID: id,
|
2022-01-12 16:02:01 +00:00
|
|
|
})
|
|
|
|
}
|
2022-01-31 10:33:56 +00:00
|
|
|
|
|
|
|
func SendMailserverNotWorking() {
|
|
|
|
send(EventMailserverNotWorking, MailserverSignal{})
|
|
|
|
}
|