2021-06-16 20:19:45 +00:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"sync"
|
2023-11-02 14:16:59 +00:00
|
|
|
"sync/atomic"
|
2022-03-02 14:33:00 +00:00
|
|
|
"time"
|
2021-06-16 20:19:45 +00:00
|
|
|
|
2023-01-07 15:25:55 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/payload"
|
2022-11-22 22:05:54 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
)
|
|
|
|
|
2021-12-01 15:15:18 +00:00
|
|
|
// MessageType represents where this message comes from
|
|
|
|
type MessageType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
RelayedMessageType MessageType = iota
|
|
|
|
StoreMessageType
|
2024-06-11 07:45:01 +00:00
|
|
|
SendMessageType
|
2021-12-01 15:15:18 +00:00
|
|
|
)
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
// MessageParams specifies the exact way a message should be wrapped
|
|
|
|
// into an Envelope.
|
|
|
|
type MessageParams struct {
|
|
|
|
Src *ecdsa.PrivateKey
|
|
|
|
Dst *ecdsa.PublicKey
|
|
|
|
KeySym []byte
|
|
|
|
Topic TopicType
|
|
|
|
Payload []byte
|
|
|
|
Padding []byte
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReceivedMessage represents a data packet to be received through the
|
|
|
|
// WakuV2 protocol and successfully decrypted.
|
|
|
|
type ReceivedMessage struct {
|
|
|
|
Envelope *protocol.Envelope // Wrapped Waku Message
|
|
|
|
|
2021-12-01 15:15:18 +00:00
|
|
|
MsgType MessageType
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
Data []byte
|
|
|
|
Padding []byte
|
|
|
|
Signature []byte
|
|
|
|
|
2024-06-11 07:45:01 +00:00
|
|
|
Sent uint32 // Time when the message was posted into the network in seconds
|
2023-05-22 21:38:02 +00:00
|
|
|
Src *ecdsa.PublicKey // Message recipient (identity used to decode the message)
|
|
|
|
Dst *ecdsa.PublicKey // Message recipient (identity used to decode the message)
|
|
|
|
|
|
|
|
PubsubTopic string
|
|
|
|
ContentTopic TopicType
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
SymKeyHash common.Hash // The Keccak256Hash of the key
|
|
|
|
|
|
|
|
hash common.Hash
|
2023-11-02 14:16:59 +00:00
|
|
|
|
|
|
|
Processed atomic.Bool
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// MessagesRequest contains details of a request for historic messages.
|
|
|
|
type MessagesRequest struct {
|
|
|
|
// ID of the request. The current implementation requires ID to be 32-byte array,
|
|
|
|
// however, it's not enforced for future implementation.
|
|
|
|
ID []byte `json:"id"`
|
|
|
|
|
|
|
|
// From is a lower bound of time range.
|
|
|
|
From uint32 `json:"from"`
|
|
|
|
|
|
|
|
// To is a upper bound of time range.
|
|
|
|
To uint32 `json:"to"`
|
|
|
|
|
|
|
|
// Limit determines the number of messages sent by the mail server
|
|
|
|
// for the current paginated request.
|
|
|
|
Limit uint32 `json:"limit"`
|
|
|
|
|
|
|
|
// Cursor is used as starting point for paginated requests.
|
|
|
|
Cursor []byte `json:"cursor"`
|
|
|
|
|
|
|
|
// Topics is a list of topics. A returned message should
|
|
|
|
// belong to one of the topics from the list.
|
|
|
|
Topics [][]byte `json:"topics"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r MessagesRequest) Validate() error {
|
|
|
|
if len(r.ID) != common.HashLength {
|
|
|
|
return errors.New("invalid 'ID', expected a 32-byte slice")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.From > r.To {
|
|
|
|
return errors.New("invalid 'From' value which is greater than To")
|
|
|
|
}
|
|
|
|
|
|
|
|
if r.Limit > MaxLimitInMessagesRequest {
|
|
|
|
return fmt.Errorf("invalid 'Limit' value, expected value lower than %d", MaxLimitInMessagesRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// EnvelopeError code and optional description of the error.
|
|
|
|
type EnvelopeError struct {
|
|
|
|
Hash common.Hash
|
|
|
|
Code uint
|
|
|
|
Description string
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrorToEnvelopeError converts common golang error into EnvelopeError with a code.
|
|
|
|
func ErrorToEnvelopeError(hash common.Hash, err error) EnvelopeError {
|
|
|
|
code := EnvelopeOtherError
|
|
|
|
switch err.(type) {
|
|
|
|
case TimeSyncError:
|
|
|
|
code = EnvelopeTimeNotSynced
|
|
|
|
}
|
|
|
|
return EnvelopeError{
|
|
|
|
Hash: hash,
|
|
|
|
Code: code,
|
|
|
|
Description: err.Error(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MessagesResponse sent as a response after processing batch of envelopes.
|
|
|
|
type MessagesResponse struct {
|
|
|
|
// Hash is a hash of all envelopes sent in the single batch.
|
|
|
|
Hash common.Hash
|
|
|
|
// Per envelope error.
|
|
|
|
Errors []EnvelopeError
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *ReceivedMessage) isSymmetricEncryption() bool {
|
|
|
|
return msg.SymKeyHash != common.Hash{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (msg *ReceivedMessage) isAsymmetricEncryption() bool {
|
|
|
|
return msg.Dst != nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MessageStore defines interface for temporary message store.
|
|
|
|
type MessageStore interface {
|
|
|
|
Add(*ReceivedMessage) error
|
|
|
|
Pop() ([]*ReceivedMessage, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMemoryMessageStore returns pointer to an instance of the MemoryMessageStore.
|
|
|
|
func NewMemoryMessageStore() *MemoryMessageStore {
|
|
|
|
return &MemoryMessageStore{
|
|
|
|
messages: map[common.Hash]*ReceivedMessage{},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MemoryMessageStore represents messages stored in a memory hash table.
|
|
|
|
type MemoryMessageStore struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
messages map[common.Hash]*ReceivedMessage
|
|
|
|
}
|
|
|
|
|
2021-12-01 15:15:18 +00:00
|
|
|
func NewReceivedMessage(env *protocol.Envelope, msgType MessageType) *ReceivedMessage {
|
2022-10-10 15:21:27 +00:00
|
|
|
ct, err := ExtractTopicFromContentTopic(env.Message().ContentTopic)
|
|
|
|
if err != nil {
|
2023-02-24 13:32:13 +00:00
|
|
|
log.Debug("failed to extract content topic from message", "topic", env.Message().ContentTopic, "err", err)
|
|
|
|
return nil
|
2022-10-10 15:21:27 +00:00
|
|
|
}
|
|
|
|
|
2021-06-16 20:19:45 +00:00
|
|
|
return &ReceivedMessage{
|
2023-05-22 21:38:02 +00:00
|
|
|
Envelope: env,
|
|
|
|
MsgType: msgType,
|
2023-12-05 04:29:27 +00:00
|
|
|
Sent: uint32(env.Message().GetTimestamp() / int64(time.Second)),
|
2023-05-22 21:38:02 +00:00
|
|
|
ContentTopic: ct,
|
|
|
|
PubsubTopic: env.PubsubTopic(),
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Hash returns the SHA3 hash of the envelope, calculating it if not yet done.
|
|
|
|
func (msg *ReceivedMessage) Hash() common.Hash {
|
|
|
|
if (msg.hash == common.Hash{}) {
|
2024-05-15 23:15:00 +00:00
|
|
|
msg.hash = common.BytesToHash(msg.Envelope.Hash().Bytes())
|
2021-06-16 20:19:45 +00:00
|
|
|
}
|
|
|
|
return msg.hash
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add adds message to store.
|
|
|
|
func (store *MemoryMessageStore) Add(msg *ReceivedMessage) error {
|
|
|
|
store.mu.Lock()
|
|
|
|
defer store.mu.Unlock()
|
|
|
|
if _, exist := store.messages[msg.Hash()]; !exist {
|
|
|
|
store.messages[msg.Hash()] = msg
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Pop returns all available messages and cleans the store.
|
|
|
|
func (store *MemoryMessageStore) Pop() ([]*ReceivedMessage, error) {
|
|
|
|
store.mu.Lock()
|
|
|
|
defer store.mu.Unlock()
|
|
|
|
all := make([]*ReceivedMessage, 0, len(store.messages))
|
|
|
|
for hash, msg := range store.messages {
|
|
|
|
delete(store.messages, hash)
|
|
|
|
all = append(all, msg)
|
|
|
|
}
|
|
|
|
return all, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open tries to decrypt an message, and populates the message fields in case of success.
|
|
|
|
func (msg *ReceivedMessage) Open(watcher *Filter) (result *ReceivedMessage) {
|
|
|
|
if watcher == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// The API interface forbids filters doing both symmetric and asymmetric encryption.
|
|
|
|
if watcher.expectsAsymmetricEncryption() && watcher.expectsSymmetricEncryption() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: should we update msg instead of creating a new received message?
|
|
|
|
result = new(ReceivedMessage)
|
|
|
|
|
2023-01-07 15:25:55 +00:00
|
|
|
keyInfo := new(payload.KeyInfo)
|
2021-06-16 20:19:45 +00:00
|
|
|
if watcher.expectsAsymmetricEncryption() {
|
2023-01-07 15:25:55 +00:00
|
|
|
keyInfo.Kind = payload.Asymmetric
|
2021-06-16 20:19:45 +00:00
|
|
|
keyInfo.PrivKey = watcher.KeyAsym
|
|
|
|
msg.Dst = &watcher.KeyAsym.PublicKey
|
|
|
|
} else if watcher.expectsSymmetricEncryption() {
|
2023-01-07 15:25:55 +00:00
|
|
|
keyInfo.Kind = payload.Symmetric
|
2021-06-16 20:19:45 +00:00
|
|
|
keyInfo.SymKey = watcher.KeySym
|
|
|
|
msg.SymKeyHash = crypto.Keccak256Hash(watcher.KeySym)
|
|
|
|
}
|
|
|
|
|
2023-01-07 15:25:55 +00:00
|
|
|
raw, err := payload.DecodePayload(msg.Envelope.Message(), keyInfo)
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to decode message", "err", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
result.Envelope = msg.Envelope
|
|
|
|
result.Data = raw.Data
|
|
|
|
result.Padding = raw.Padding
|
|
|
|
result.Signature = raw.Signature
|
|
|
|
result.Src = raw.PubKey
|
|
|
|
|
2023-12-05 04:29:27 +00:00
|
|
|
result.Sent = uint32(msg.Envelope.Message().GetTimestamp() / int64(time.Second))
|
2022-10-10 15:21:27 +00:00
|
|
|
|
|
|
|
ct, err := ExtractTopicFromContentTopic(msg.Envelope.Message().ContentTopic)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to decode message", "err", err)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-05-22 21:38:02 +00:00
|
|
|
result.PubsubTopic = watcher.PubsubTopic
|
|
|
|
result.ContentTopic = ct
|
2021-06-16 20:19:45 +00:00
|
|
|
|
|
|
|
return result
|
|
|
|
}
|