github.com/status-im/status-go@v1.1.0/eth-node/types/waku.go (about)

     1  package types
     2  
     3  import (
     4  	"context"
     5  	"crypto/ecdsa"
     6  	"encoding/json"
     7  	"sync"
     8  	"time"
     9  
    10  	"github.com/libp2p/go-libp2p/core/peer"
    11  	"github.com/libp2p/go-libp2p/core/protocol"
    12  	"github.com/multiformats/go-multiaddr"
    13  	"github.com/pborman/uuid"
    14  
    15  	"github.com/ethereum/go-ethereum/common"
    16  	"github.com/ethereum/go-ethereum/p2p/enode"
    17  	"github.com/status-im/status-go/connection"
    18  )
    19  
    20  type ConnStatus struct {
    21  	IsOnline bool      `json:"isOnline"`
    22  	Peers    PeerStats `json:"peers"`
    23  }
    24  
    25  type PeerStats map[peer.ID]WakuV2Peer
    26  
    27  func (m PeerStats) MarshalJSON() ([]byte, error) {
    28  	tmpMap := make(map[string]WakuV2Peer)
    29  	for k, v := range m {
    30  		tmpMap[k.String()] = v
    31  	}
    32  	return json.Marshal(tmpMap)
    33  }
    34  
    35  type WakuV2Peer struct {
    36  	Protocols []protocol.ID         `json:"protocols"`
    37  	Addresses []multiaddr.Multiaddr `json:"addresses"`
    38  }
    39  
    40  type PeerList struct {
    41  	FullMeshPeers peer.IDSlice `json:"fullMesh"`
    42  	AllPeers      peer.IDSlice `json:"all"`
    43  }
    44  
    45  type ConnStatusSubscription struct {
    46  	sync.RWMutex
    47  
    48  	ID     string
    49  	C      chan ConnStatus
    50  	active bool
    51  }
    52  
    53  func NewConnStatusSubscription() *ConnStatusSubscription {
    54  	return &ConnStatusSubscription{
    55  		ID:     uuid.NewRandom().String(),
    56  		C:      make(chan ConnStatus, 100),
    57  		active: true,
    58  	}
    59  }
    60  
    61  func (u *ConnStatusSubscription) Active() bool {
    62  	u.RLock()
    63  	defer u.RUnlock()
    64  	return u.active
    65  }
    66  
    67  func (u *ConnStatusSubscription) Unsubscribe() {
    68  	u.Lock()
    69  	defer u.Unlock()
    70  	close(u.C)
    71  	u.active = false
    72  }
    73  
    74  func (u *ConnStatusSubscription) Send(s ConnStatus) bool {
    75  	u.RLock()
    76  	defer u.RUnlock()
    77  	if !u.active {
    78  		return false
    79  	}
    80  	u.C <- s
    81  	return true
    82  }
    83  
    84  type WakuKeyManager interface {
    85  	// GetPrivateKey retrieves the private key of the specified identity.
    86  	GetPrivateKey(id string) (*ecdsa.PrivateKey, error)
    87  	// AddKeyPair imports a asymmetric private key and returns a deterministic identifier.
    88  	AddKeyPair(key *ecdsa.PrivateKey) (string, error)
    89  	// DeleteKeyPair deletes the key with the specified ID if it exists.
    90  	DeleteKeyPair(keyID string) bool
    91  	// DeleteKeyPairs deletes all the keys
    92  	DeleteKeyPairs() error
    93  	AddSymKeyDirect(key []byte) (string, error)
    94  	AddSymKeyFromPassword(password string) (string, error)
    95  	DeleteSymKey(id string) bool
    96  	GetSymKey(id string) ([]byte, error)
    97  }
    98  
    99  // Whisper represents a dark communication interface through the Ethereum
   100  // network, using its very own P2P communication layer.
   101  type Waku interface {
   102  	PublicWakuAPI() PublicWakuAPI
   103  
   104  	// Waku protocol version
   105  	Version() uint
   106  
   107  	// PeerCount
   108  	PeerCount() int
   109  
   110  	ListenAddresses() ([]multiaddr.Multiaddr, error)
   111  
   112  	RelayPeersByTopic(topic string) (*PeerList, error)
   113  
   114  	ENR() (*enode.Node, error)
   115  
   116  	Peers() PeerStats
   117  
   118  	StartDiscV5() error
   119  
   120  	StopDiscV5() error
   121  
   122  	SubscribeToPubsubTopic(topic string, optPublicKey *ecdsa.PublicKey) error
   123  
   124  	UnsubscribeFromPubsubTopic(topic string) error
   125  
   126  	StorePubsubTopicKey(topic string, privKey *ecdsa.PrivateKey) error
   127  
   128  	RetrievePubsubTopicKey(topic string) (*ecdsa.PrivateKey, error)
   129  
   130  	RemovePubsubTopicKey(topic string) error
   131  
   132  	AddStorePeer(address multiaddr.Multiaddr) (peer.ID, error)
   133  
   134  	AddRelayPeer(address multiaddr.Multiaddr) (peer.ID, error)
   135  
   136  	DialPeer(address multiaddr.Multiaddr) error
   137  
   138  	DialPeerByID(peerID peer.ID) error
   139  
   140  	DropPeer(peerID peer.ID) error
   141  
   142  	SubscribeToConnStatusChanges() (*ConnStatusSubscription, error)
   143  
   144  	SetCriteriaForMissingMessageVerification(peerID peer.ID, pubsubTopic string, contentTopics []TopicType) error
   145  
   146  	// MinPow returns the PoW value required by this node.
   147  	MinPow() float64
   148  	// BloomFilter returns the aggregated bloom filter for all the topics of interest.
   149  	// The nodes are required to send only messages that match the advertised bloom filter.
   150  	// If a message does not match the bloom, it will tantamount to spam, and the peer will
   151  	// be disconnected.
   152  	BloomFilter() []byte
   153  
   154  	// GetCurrentTime returns current time.
   155  	GetCurrentTime() time.Time
   156  
   157  	// GetPrivateKey retrieves the private key of the specified identity.
   158  	GetPrivateKey(id string) (*ecdsa.PrivateKey, error)
   159  
   160  	SubscribeEnvelopeEvents(events chan<- EnvelopeEvent) Subscription
   161  
   162  	// AddKeyPair imports a asymmetric private key and returns a deterministic identifier.
   163  	AddKeyPair(key *ecdsa.PrivateKey) (string, error)
   164  	// DeleteKeyPair deletes the key with the specified ID if it exists.
   165  	DeleteKeyPair(keyID string) bool
   166  	AddSymKeyDirect(key []byte) (string, error)
   167  	AddSymKeyFromPassword(password string) (string, error)
   168  	DeleteSymKey(id string) bool
   169  	GetSymKey(id string) ([]byte, error)
   170  	MaxMessageSize() uint32
   171  
   172  	GetStats() StatsSummary
   173  
   174  	Subscribe(opts *SubscriptionOptions) (string, error)
   175  	GetFilter(id string) Filter
   176  	Unsubscribe(ctx context.Context, id string) error
   177  	UnsubscribeMany(ids []string) error
   178  
   179  	// RequestStoreMessages uses the WAKU2-STORE protocol to request historic messages
   180  	RequestStoreMessages(ctx context.Context, peerID peer.ID, request MessagesRequest, processEnvelopes bool) (StoreRequestCursor, int, error)
   181  
   182  	// ProcessingP2PMessages indicates whether there are in-flight p2p messages
   183  	ProcessingP2PMessages() bool
   184  
   185  	// MarkP2PMessageAsProcessed tells the waku layer that a P2P message has been processed
   186  	MarkP2PMessageAsProcessed(common.Hash)
   187  
   188  	// ConnectionChanged is called whenever the client knows its connection status has changed
   189  	ConnectionChanged(connection.State)
   190  
   191  	// ClearEnvelopesCache clears waku envelopes cache
   192  	ClearEnvelopesCache()
   193  
   194  	// ConfirmMessageDelivered updates a message has been delivered in waku
   195  	ConfirmMessageDelivered(hash []common.Hash)
   196  
   197  	// SetStorePeerID updates the peer id of store node
   198  	SetStorePeerID(peerID peer.ID)
   199  
   200  	// PeerID returns node's PeerID
   201  	PeerID() peer.ID
   202  
   203  	// PingPeer returns the reply time
   204  	PingPeer(ctx context.Context, peerID peer.ID) (time.Duration, error)
   205  }