github.com/status-im/status-go@v1.1.0/signal/events_shhext.go (about)

     1  package signal
     2  
     3  import (
     4  	"encoding/hex"
     5  	"encoding/json"
     6  
     7  	"github.com/libp2p/go-libp2p/core/peer"
     8  	"github.com/multiformats/go-multiaddr"
     9  
    10  	"github.com/ethereum/go-ethereum/common/hexutil"
    11  	"github.com/status-im/status-go/eth-node/types"
    12  	"github.com/status-im/status-go/services/mailservers"
    13  )
    14  
    15  const (
    16  	// EventEnvelopeSent is triggered when envelope was sent at least to a one peer.
    17  	EventEnvelopeSent = "envelope.sent"
    18  
    19  	// EventEnvelopeExpired is triggered when envelop was dropped by a whisper without being sent
    20  	// to any peer
    21  	EventEnvelopeExpired = "envelope.expired"
    22  
    23  	// EventMailServerRequestCompleted is triggered when whisper receives a message ack from the mailserver
    24  	EventMailServerRequestCompleted = "mailserver.request.completed"
    25  
    26  	// EventMailServerRequestExpired is triggered when request TTL ends
    27  	EventMailServerRequestExpired = "mailserver.request.expired"
    28  
    29  	// EventEnodeDiscovered is tiggered when enode has been discovered.
    30  	EventEnodeDiscovered = "enode.discovered"
    31  
    32  	// EventDecryptMessageFailed is triggered when we receive a message from a bundle we don't have
    33  	EventDecryptMessageFailed = "messages.decrypt.failed"
    34  
    35  	// EventBundleAdded is triggered when we receive a bundle
    36  	EventBundleAdded = "bundles.added"
    37  
    38  	// EventNewMessages is triggered when we receive new messages
    39  	EventNewMessages = "messages.new"
    40  
    41  	// EventHistoryRequestStarted is triggered before processing a store request
    42  	EventHistoryRequestStarted = "history.request.started"
    43  
    44  	// EventHistoryRequestCompleted is triggered after processing all storenode requests
    45  	EventHistoryRequestCompleted = "history.request.completed"
    46  
    47  	// EventHistoryRequestFailed is triggered when requesting history messages fails
    48  	EventHistoryRequestFailed = "history.request.failed"
    49  
    50  	// EventBackupPerformed is triggered when a backup has been performed
    51  	EventBackupPerformed = "backup.performed"
    52  
    53  	// EventMailserverAvailable is triggered when a mailserver becomes available
    54  	EventMailserverAvailable = "mailserver.available"
    55  
    56  	// EventMailserverChanged is triggered when switching the active mailserver
    57  	EventMailserverChanged = "mailserver.changed"
    58  
    59  	// EventMailserverNotWorking is triggered when the mailserver has failed to connect or failed to respond to requests
    60  	EventMailserverNotWorking = "mailserver.not.working"
    61  
    62  	// EventUpdateAvailable is triggered after a update verification is performed
    63  	EventUpdateAvailable = "update.available"
    64  )
    65  
    66  // EnvelopeSignal includes hash of the envelope.
    67  type EnvelopeSignal struct {
    68  	IDs     []hexutil.Bytes `json:"ids"`
    69  	Hash    types.Hash      `json:"hash"`
    70  	Message string          `json:"message"`
    71  }
    72  
    73  // MailServerResponseSignal holds the data received in the response from the mailserver.
    74  type MailServerResponseSignal struct {
    75  	RequestID        types.Hash `json:"requestID"`
    76  	LastEnvelopeHash types.Hash `json:"lastEnvelopeHash"`
    77  	Cursor           string     `json:"cursor"`
    78  	ErrorMsg         string     `json:"errorMessage"`
    79  }
    80  
    81  type HistoryMessagesSignal struct {
    82  	RequestID  string `json:"requestId"`
    83  	PeerID     string `json:"peerId"`
    84  	BatchIndex int    `json:"batchIndex"`
    85  	NumBatches int    `json:"numBatches,omitempty"`
    86  	ErrorMsg   string `json:"errorMessage,omitempty"`
    87  }
    88  
    89  type UpdateAvailableSignal struct {
    90  	Available bool   `json:"available"`
    91  	Version   string `json:"version"`
    92  	URL       string `json:"url"`
    93  }
    94  
    95  // DecryptMessageFailedSignal holds the sender of the message that could not be decrypted
    96  type DecryptMessageFailedSignal struct {
    97  	Sender string `json:"sender"`
    98  }
    99  
   100  // BundleAddedSignal holds the identity and installation id of the user
   101  type BundleAddedSignal struct {
   102  	Identity       string `json:"identity"`
   103  	InstallationID string `json:"installationID"`
   104  }
   105  
   106  type MailserverSignal struct {
   107  	Address *multiaddr.Multiaddr `json:"address"`
   108  	ID      string               `json:"id"`
   109  }
   110  
   111  type Filter struct {
   112  	// ChatID is the identifier of the chat
   113  	ChatID string `json:"chatId"`
   114  	// SymKeyID is the symmetric key id used for symmetric chats
   115  	SymKeyID string `json:"symKeyId"`
   116  	// OneToOne tells us if we need to use asymmetric encryption for this chat
   117  	Listen bool `json:"listen"`
   118  	// FilterID the whisper filter id generated
   119  	FilterID string `json:"filterId"`
   120  	// Identity is the public key of the other recipient for non-public chats
   121  	Identity string `json:"identity"`
   122  	// Topic is the whisper topic
   123  	Topic types.TopicType `json:"topic"`
   124  }
   125  
   126  // SendEnvelopeSent triggered when envelope delivered at least to 1 peer.
   127  func SendEnvelopeSent(identifiers [][]byte) {
   128  	var hexIdentifiers []hexutil.Bytes
   129  	for _, i := range identifiers {
   130  		hexIdentifiers = append(hexIdentifiers, i)
   131  	}
   132  
   133  	send(EventEnvelopeSent, EnvelopeSignal{
   134  		IDs: hexIdentifiers,
   135  	})
   136  }
   137  
   138  // SendEnvelopeExpired triggered when envelope delivered at least to 1 peer.
   139  func SendEnvelopeExpired(identifiers [][]byte, err error) {
   140  	var message string
   141  	if err != nil {
   142  		message = err.Error()
   143  	}
   144  	var hexIdentifiers []hexutil.Bytes
   145  	for _, i := range identifiers {
   146  		hexIdentifiers = append(hexIdentifiers, i)
   147  	}
   148  
   149  	send(EventEnvelopeExpired, EnvelopeSignal{IDs: hexIdentifiers, Message: message})
   150  }
   151  
   152  func SendHistoricMessagesRequestStarted(numBatches int) {
   153  	send(EventHistoryRequestStarted, HistoryMessagesSignal{NumBatches: numBatches})
   154  }
   155  
   156  func SendHistoricMessagesRequestFailed(requestID []byte, peerID peer.ID, err error) {
   157  	send(EventHistoryRequestFailed, HistoryMessagesSignal{RequestID: hex.EncodeToString(requestID), PeerID: peerID.String(), ErrorMsg: err.Error()})
   158  }
   159  
   160  func SendHistoricMessagesRequestCompleted() {
   161  	send(EventHistoryRequestCompleted, HistoryMessagesSignal{})
   162  }
   163  
   164  func SendUpdateAvailable(available bool, latestVersion string, url string) {
   165  	send(EventUpdateAvailable, UpdateAvailableSignal{Available: available, Version: latestVersion, URL: url})
   166  }
   167  
   168  // SendMailServerRequestCompleted triggered when mail server response has been received
   169  func SendMailServerRequestCompleted(requestID types.Hash, lastEnvelopeHash types.Hash, cursor []byte, err error) {
   170  	errorMsg := ""
   171  	if err != nil {
   172  		errorMsg = err.Error()
   173  	}
   174  	sig := MailServerResponseSignal{
   175  		RequestID:        requestID,
   176  		LastEnvelopeHash: lastEnvelopeHash,
   177  		Cursor:           hex.EncodeToString(cursor),
   178  		ErrorMsg:         errorMsg,
   179  	}
   180  	send(EventMailServerRequestCompleted, sig)
   181  }
   182  
   183  // SendMailServerRequestExpired triggered when mail server request expires
   184  func SendMailServerRequestExpired(hash types.Hash) {
   185  	send(EventMailServerRequestExpired, EnvelopeSignal{Hash: hash})
   186  }
   187  
   188  // EnodeDiscoveredSignal includes enode address and topic
   189  type EnodeDiscoveredSignal struct {
   190  	Enode string `json:"enode"`
   191  	Topic string `json:"topic"`
   192  }
   193  
   194  // BackupPerformedSignal signals that a backup has been performed
   195  type BackupPerformedSignal struct {
   196  	LastBackup uint64 `json:"lastBackup"`
   197  }
   198  
   199  // SendEnodeDiscovered tiggered when an enode is discovered.
   200  // finds a new enode.
   201  func SendEnodeDiscovered(enode, topic string) {
   202  	send(EventEnodeDiscovered, EnodeDiscoveredSignal{
   203  		Enode: enode,
   204  		Topic: topic,
   205  	})
   206  }
   207  
   208  func SendBackupPerformed(lastBackup uint64) {
   209  	send(EventBackupPerformed, BackupPerformedSignal{lastBackup})
   210  }
   211  func SendDecryptMessageFailed(sender string) {
   212  	send(EventDecryptMessageFailed, DecryptMessageFailedSignal{sender})
   213  }
   214  
   215  func SendBundleAdded(identity string, installationID string) {
   216  	send(EventBundleAdded, BundleAddedSignal{Identity: identity, InstallationID: installationID})
   217  }
   218  
   219  func SendNewMessages(obj json.Marshaler) {
   220  	send(EventNewMessages, obj)
   221  }
   222  
   223  func sendMailserverSignal(ms *mailservers.Mailserver, event string) {
   224  	msSignal := MailserverSignal{}
   225  	if ms != nil {
   226  		msSignal.Address = ms.Addr
   227  		msSignal.ID = ms.ID
   228  	}
   229  	send(event, msSignal)
   230  }
   231  
   232  func SendMailserverAvailable(ms *mailservers.Mailserver) {
   233  	sendMailserverSignal(ms, EventMailserverAvailable)
   234  }
   235  
   236  func SendMailserverChanged(ms *mailservers.Mailserver) {
   237  	sendMailserverSignal(ms, EventMailserverChanged)
   238  }
   239  
   240  func SendMailserverNotWorking() {
   241  	sendMailserverSignal(nil, EventMailserverNotWorking)
   242  }