2021-04-04 17:06:17 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-10-26 13:43:28 +00:00
|
|
|
"chat2/pb"
|
2021-04-28 20:28:45 +00:00
|
|
|
"context"
|
2022-08-15 18:29:59 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
2021-04-04 17:06:17 +00:00
|
|
|
"time"
|
|
|
|
|
2022-10-19 19:39:32 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-08-15 18:29:59 +00:00
|
|
|
"github.com/multiformats/go-multiaddr"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/dnsdisc"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/node"
|
2022-12-21 18:47:43 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/payload"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/filter"
|
2024-05-03 16:07:03 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/legacy_store"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/lightpush"
|
|
|
|
wpb "github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/relay"
|
2023-11-07 19:48:43 +00:00
|
|
|
wrln "github.com/waku-org/go-waku/waku/v2/protocol/rln"
|
2022-11-09 19:53:01 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/utils"
|
2022-08-15 18:29:59 +00:00
|
|
|
"google.golang.org/protobuf/proto"
|
2021-04-04 17:06:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Chat represents a subscription to a single PubSub topic. Messages
|
|
|
|
// can be published to the topic with Chat.Publish, and received
|
|
|
|
// messages are pushed to the Messages channel.
|
|
|
|
type Chat struct {
|
2022-08-15 18:29:59 +00:00
|
|
|
ctx context.Context
|
|
|
|
wg sync.WaitGroup
|
|
|
|
node *node.WakuNode
|
|
|
|
ui UI
|
|
|
|
uiReady chan struct{}
|
|
|
|
inputChan chan string
|
|
|
|
options Options
|
|
|
|
|
|
|
|
C chan *protocol.Envelope
|
2021-04-04 17:06:17 +00:00
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
nick string
|
|
|
|
}
|
2021-04-04 17:06:17 +00:00
|
|
|
|
2023-05-10 14:13:10 +00:00
|
|
|
func NewChat(ctx context.Context, node *node.WakuNode, connNotifier <-chan node.PeerConnection, options Options) *Chat {
|
2021-06-28 14:14:28 +00:00
|
|
|
chat := &Chat{
|
2022-08-15 18:29:59 +00:00
|
|
|
ctx: ctx,
|
|
|
|
node: node,
|
|
|
|
options: options,
|
|
|
|
nick: options.Nickname,
|
|
|
|
uiReady: make(chan struct{}, 1),
|
|
|
|
inputChan: make(chan string, 100),
|
2021-04-04 17:06:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
chat.ui = NewUIModel(chat.uiReady, chat.inputChan)
|
|
|
|
|
2023-08-18 13:59:37 +00:00
|
|
|
topics := options.Relay.Topics.Value()
|
|
|
|
if len(topics) == 0 {
|
|
|
|
topics = append(topics, relay.DefaultWakuTopic)
|
|
|
|
}
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
if options.Filter.Enable {
|
2023-09-29 05:13:25 +00:00
|
|
|
cf := protocol.ContentFilter{
|
2023-09-19 12:52:11 +00:00
|
|
|
PubsubTopic: relay.DefaultWakuTopic,
|
2023-09-29 05:13:25 +00:00
|
|
|
ContentTopics: protocol.NewContentTopicSet(options.ContentTopic),
|
2023-08-04 13:07:43 +00:00
|
|
|
}
|
|
|
|
var filterOpt filter.FilterSubscribeOption
|
|
|
|
peerID, err := options.Filter.NodePeerID()
|
|
|
|
if err != nil {
|
|
|
|
filterOpt = filter.WithAutomaticPeerSelection()
|
2022-08-15 18:29:59 +00:00
|
|
|
} else {
|
2023-08-04 13:07:43 +00:00
|
|
|
filterOpt = filter.WithPeer(peerID)
|
|
|
|
chat.ui.InfoMessage(fmt.Sprintf("Subscribing to filter node %s", peerID))
|
|
|
|
}
|
2023-09-20 05:56:55 +00:00
|
|
|
theFilters, err := node.FilterLightnode().Subscribe(ctx, cf, filterOpt)
|
2023-08-04 13:07:43 +00:00
|
|
|
if err != nil {
|
|
|
|
chat.ui.ErrorMessage(err)
|
|
|
|
} else {
|
2023-09-20 05:56:55 +00:00
|
|
|
chat.C = theFilters[0].C //Picking first subscription since there is only 1 contentTopic specified.
|
2022-08-15 18:29:59 +00:00
|
|
|
}
|
2021-06-28 14:14:28 +00:00
|
|
|
} else {
|
2023-08-18 13:59:37 +00:00
|
|
|
|
|
|
|
for _, topic := range topics {
|
2023-10-20 19:56:18 +00:00
|
|
|
sub, err := node.Relay().Subscribe(ctx, protocol.NewContentFilter(topic))
|
2023-08-18 13:59:37 +00:00
|
|
|
if err != nil {
|
|
|
|
chat.ui.ErrorMessage(err)
|
|
|
|
} else {
|
|
|
|
chat.C = make(chan *protocol.Envelope)
|
|
|
|
go func() {
|
2023-10-20 19:56:18 +00:00
|
|
|
for e := range sub[0].Ch {
|
2023-08-18 13:59:37 +00:00
|
|
|
chat.C <- e
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2021-06-28 14:14:28 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:13:10 +00:00
|
|
|
chat.wg.Add(7)
|
2022-08-15 18:29:59 +00:00
|
|
|
go chat.parseInput()
|
|
|
|
go chat.receiveMessages()
|
|
|
|
|
|
|
|
connectionWg := sync.WaitGroup{}
|
|
|
|
connectionWg.Add(2)
|
|
|
|
|
|
|
|
go chat.welcomeMessage()
|
2021-04-04 17:06:17 +00:00
|
|
|
|
2023-05-10 14:13:10 +00:00
|
|
|
go chat.connectionWatcher(&connectionWg, connNotifier)
|
2022-08-15 18:29:59 +00:00
|
|
|
go chat.staticNodes(&connectionWg)
|
|
|
|
go chat.discoverNodes(&connectionWg)
|
|
|
|
go chat.retrieveHistory(&connectionWg)
|
2022-07-06 20:29:20 +00:00
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
return chat
|
2021-04-04 17:06:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
func (c *Chat) Stop() {
|
|
|
|
c.wg.Wait()
|
|
|
|
close(c.inputChan)
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:13:10 +00:00
|
|
|
func (c *Chat) connectionWatcher(connectionWg *sync.WaitGroup, connNotifier <-chan node.PeerConnection) {
|
|
|
|
defer c.wg.Done()
|
|
|
|
|
|
|
|
for conn := range connNotifier {
|
|
|
|
if conn.Connected {
|
2024-01-12 17:40:27 +00:00
|
|
|
c.ui.InfoMessage(fmt.Sprintf("Peer %s connected", conn.PeerID.String()))
|
2023-05-10 14:13:10 +00:00
|
|
|
} else {
|
2024-01-12 17:40:27 +00:00
|
|
|
c.ui.InfoMessage(fmt.Sprintf("Peer %s disconnected", conn.PeerID.String()))
|
2023-05-10 14:13:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
func (c *Chat) receiveMessages() {
|
|
|
|
defer c.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.ctx.Done():
|
|
|
|
return
|
|
|
|
case value := <-c.C:
|
|
|
|
|
|
|
|
msgContentTopic := value.Message().ContentTopic
|
2023-08-21 20:54:13 +00:00
|
|
|
if msgContentTopic != c.options.ContentTopic {
|
2022-08-15 18:29:59 +00:00
|
|
|
continue // Discard messages from other topics
|
|
|
|
}
|
|
|
|
|
2023-08-04 13:07:43 +00:00
|
|
|
msg, err := decodeMessage(c.options.ContentTopic, value.Message())
|
2022-08-15 18:29:59 +00:00
|
|
|
if err == nil {
|
|
|
|
// send valid messages to the UI
|
|
|
|
c.ui.ChatMessage(int64(msg.Timestamp), msg.Nick, string(msg.Payload))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (c *Chat) parseInput() {
|
|
|
|
defer c.wg.Done()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-c.ctx.Done():
|
|
|
|
return
|
|
|
|
case line := <-c.inputChan:
|
|
|
|
c.ui.SetSending(true)
|
|
|
|
go func() {
|
|
|
|
defer c.ui.SetSending(false)
|
|
|
|
|
|
|
|
// bail if requested
|
2022-08-15 23:29:19 +00:00
|
|
|
if line == "/exit" {
|
2022-08-15 18:29:59 +00:00
|
|
|
c.ui.Quit()
|
2022-08-15 23:29:19 +00:00
|
|
|
fmt.Println("Bye!")
|
2022-08-15 18:29:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// add peer
|
|
|
|
if strings.HasPrefix(line, "/connect") {
|
|
|
|
peer := strings.TrimPrefix(line, "/connect ")
|
|
|
|
c.wg.Add(1)
|
|
|
|
go func(peer string) {
|
|
|
|
defer c.wg.Done()
|
|
|
|
|
|
|
|
ma, err := multiaddr.NewMultiaddr(peer)
|
|
|
|
if err != nil {
|
|
|
|
c.ui.ErrorMessage(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
peerID, err := ma.ValueForProtocol(multiaddr.P_P2P)
|
|
|
|
if err != nil {
|
|
|
|
c.ui.ErrorMessage(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ui.InfoMessage(fmt.Sprintf("Connecting to peer: %s", peerID))
|
2022-10-05 22:08:01 +00:00
|
|
|
ctx, cancel := context.WithTimeout(c.ctx, time.Duration(10)*time.Second)
|
2022-08-15 18:29:59 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
err = c.node.DialPeerWithMultiAddress(ctx, ma)
|
|
|
|
if err != nil {
|
|
|
|
c.ui.ErrorMessage(err)
|
|
|
|
}
|
|
|
|
}(peer)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// list peers
|
|
|
|
if line == "/peers" {
|
|
|
|
peers := c.node.Host().Network().Peers()
|
|
|
|
if len(peers) == 0 {
|
|
|
|
c.ui.InfoMessage("No peers available")
|
|
|
|
} else {
|
|
|
|
peerInfoMsg := "Peers: \n"
|
|
|
|
for _, p := range peers {
|
|
|
|
peerInfo := c.node.Host().Peerstore().PeerInfo(p)
|
|
|
|
peerProtocols, err := c.node.Host().Peerstore().GetProtocols(p)
|
|
|
|
if err != nil {
|
|
|
|
c.ui.ErrorMessage(err)
|
|
|
|
return
|
|
|
|
}
|
2024-01-12 17:40:27 +00:00
|
|
|
peerInfoMsg += fmt.Sprintf("• %s:\n", p.String())
|
2023-02-16 16:17:52 +00:00
|
|
|
|
|
|
|
var strProtocols []string
|
|
|
|
for _, p := range peerProtocols {
|
|
|
|
strProtocols = append(strProtocols, string(p))
|
|
|
|
}
|
|
|
|
|
|
|
|
peerInfoMsg += fmt.Sprintf(" Protocols: %s\n", strings.Join(strProtocols, ", "))
|
2023-01-03 20:17:55 +00:00
|
|
|
peerInfoMsg += " Addresses:\n"
|
2022-08-15 18:29:59 +00:00
|
|
|
for _, addr := range peerInfo.Addrs {
|
2024-01-12 17:40:27 +00:00
|
|
|
peerInfoMsg += fmt.Sprintf(" - %s/p2p/%s\n", addr.String(), p.String())
|
2022-08-15 18:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
c.ui.InfoMessage(peerInfoMsg)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// change nick
|
|
|
|
if strings.HasPrefix(line, "/nick") {
|
|
|
|
newNick := strings.TrimSpace(strings.TrimPrefix(line, "/nick "))
|
|
|
|
if newNick != "" {
|
|
|
|
c.nick = newNick
|
|
|
|
} else {
|
|
|
|
c.ui.ErrorMessage(errors.New("invalid nickname"))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if line == "/help" {
|
|
|
|
c.ui.InfoMessage(`Available commands:
|
|
|
|
/connect multiaddress - dials a node adding it to the list of connected peers
|
|
|
|
/peers - list of peers connected to this node
|
|
|
|
/nick newNick - change the user's nickname
|
2022-08-15 23:29:19 +00:00
|
|
|
/exit - closes the app`)
|
2022-08-15 18:29:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SendMessage(line)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
}
|
2021-06-10 13:00:16 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
func (c *Chat) SendMessage(line string) {
|
|
|
|
tCtx, cancel := context.WithTimeout(c.ctx, 3*time.Second)
|
|
|
|
defer func() {
|
|
|
|
cancel()
|
|
|
|
}()
|
|
|
|
|
|
|
|
err := c.publish(tCtx, line)
|
|
|
|
if err != nil {
|
2022-09-11 21:08:58 +00:00
|
|
|
if err.Error() == "validation failed" {
|
2023-09-29 05:13:25 +00:00
|
|
|
err = errors.New("message rate violation")
|
2022-09-11 21:08:58 +00:00
|
|
|
}
|
2022-08-15 18:29:59 +00:00
|
|
|
c.ui.ErrorMessage(err)
|
|
|
|
}
|
|
|
|
}
|
2021-04-04 17:06:17 +00:00
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
func (c *Chat) publish(ctx context.Context, message string) error {
|
2021-04-04 17:06:17 +00:00
|
|
|
msg := &pb.Chat2Message{
|
2022-12-09 03:08:04 +00:00
|
|
|
Timestamp: uint64(c.node.Timesource().Now().Unix()),
|
2022-08-15 18:29:59 +00:00
|
|
|
Nick: c.nick,
|
2021-04-04 17:06:17 +00:00
|
|
|
Payload: []byte(message),
|
|
|
|
}
|
|
|
|
|
|
|
|
msgBytes, err := proto.Marshal(msg)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-08-04 13:07:43 +00:00
|
|
|
version := uint32(0)
|
|
|
|
timestamp := utils.GetUnixEpochFrom(c.node.Timesource().Now())
|
|
|
|
keyInfo := &payload.KeyInfo{
|
|
|
|
Kind: payload.None,
|
2021-06-10 13:00:16 +00:00
|
|
|
}
|
2021-04-06 23:27:54 +00:00
|
|
|
|
2022-12-21 18:47:43 +00:00
|
|
|
p := new(payload.Payload)
|
2021-04-06 23:27:54 +00:00
|
|
|
p.Data = msgBytes
|
|
|
|
p.Key = keyInfo
|
2021-04-04 17:06:17 +00:00
|
|
|
|
2021-06-10 13:00:16 +00:00
|
|
|
payload, err := p.Encode(version)
|
2021-04-04 17:06:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-04-22 00:12:51 +00:00
|
|
|
wakuMsg := &wpb.WakuMessage{
|
2021-04-04 17:06:17 +00:00
|
|
|
Payload: payload,
|
2023-11-07 19:48:43 +00:00
|
|
|
Version: proto.Uint32(version),
|
2022-08-15 18:29:59 +00:00
|
|
|
ContentTopic: options.ContentTopic,
|
2021-04-07 21:19:39 +00:00
|
|
|
Timestamp: timestamp,
|
2021-04-04 17:06:17 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
if c.options.RLNRelay.Enable {
|
2022-07-06 20:29:20 +00:00
|
|
|
// for future version when we support more than one rln protected content topic,
|
|
|
|
// we should check the message content topic as well
|
2022-12-09 18:28:08 +00:00
|
|
|
err = c.node.RLNRelay().AppendRLNProof(wakuMsg, c.node.Timesource().Now())
|
2022-07-06 20:29:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-08-15 18:29:59 +00:00
|
|
|
|
2023-11-07 19:48:43 +00:00
|
|
|
rateLimitProof, err := wrln.BytesToRateLimitProof(wakuMsg.RateLimitProof)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
c.ui.InfoMessage(fmt.Sprintf("RLN Epoch: %d", rateLimitProof.Epoch.Uint64()))
|
2022-07-06 20:29:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
if c.options.LightPush.Enable {
|
2024-02-05 12:53:15 +00:00
|
|
|
lightOpt := []lightpush.RequestOption{lightpush.WithDefaultPubsubTopic()}
|
2022-08-15 18:29:59 +00:00
|
|
|
var peerID peer.ID
|
|
|
|
peerID, err = options.LightPush.NodePeerID()
|
|
|
|
if err != nil {
|
2023-10-30 16:30:25 +00:00
|
|
|
lightOpt = append(lightOpt, lightpush.WithAutomaticPeerSelection())
|
2022-08-15 18:29:59 +00:00
|
|
|
} else {
|
2023-10-30 16:30:25 +00:00
|
|
|
lightOpt = append(lightOpt, lightpush.WithPeer(peerID))
|
2022-08-15 18:29:59 +00:00
|
|
|
}
|
2021-11-01 14:42:55 +00:00
|
|
|
|
2023-10-30 16:30:25 +00:00
|
|
|
_, err = c.node.Lightpush().Publish(c.ctx, wakuMsg, lightOpt...)
|
2021-11-01 14:42:55 +00:00
|
|
|
} else {
|
2023-10-30 16:30:25 +00:00
|
|
|
_, err = c.node.Relay().Publish(ctx, wakuMsg, relay.WithDefaultPubsubTopic())
|
2021-11-01 14:42:55 +00:00
|
|
|
}
|
2021-04-11 23:45:42 +00:00
|
|
|
|
|
|
|
return err
|
2021-04-04 17:06:17 +00:00
|
|
|
}
|
|
|
|
|
2023-08-04 13:07:43 +00:00
|
|
|
func decodeMessage(contentTopic string, wakumsg *wpb.WakuMessage) (*pb.Chat2Message, error) {
|
|
|
|
keyInfo := &payload.KeyInfo{
|
|
|
|
Kind: payload.None,
|
2021-06-10 13:00:16 +00:00
|
|
|
}
|
|
|
|
|
2022-12-21 18:47:43 +00:00
|
|
|
payload, err := payload.DecodePayload(wakumsg, keyInfo)
|
2021-04-12 18:03:58 +00:00
|
|
|
if err != nil {
|
2022-07-06 20:29:20 +00:00
|
|
|
return nil, err
|
2021-04-12 18:03:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
msg := &pb.Chat2Message{}
|
|
|
|
if err := proto.Unmarshal(payload.Data, msg); err != nil {
|
2022-07-06 20:29:20 +00:00
|
|
|
return nil, err
|
2021-04-12 18:03:58 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 20:29:20 +00:00
|
|
|
return msg, nil
|
2021-04-12 18:03:58 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
func (c *Chat) retrieveHistory(connectionWg *sync.WaitGroup) {
|
|
|
|
defer c.wg.Done()
|
|
|
|
|
|
|
|
connectionWg.Wait() // Wait until node connection operations are done
|
|
|
|
|
|
|
|
if !c.options.Store.Enable {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-03 16:07:03 +00:00
|
|
|
var storeOpt legacy_store.HistoryRequestOption
|
2022-08-15 18:29:59 +00:00
|
|
|
if c.options.Store.Node == nil {
|
|
|
|
c.ui.InfoMessage("No store node configured. Choosing one at random...")
|
2024-05-03 16:07:03 +00:00
|
|
|
storeOpt = legacy_store.WithAutomaticPeerSelection()
|
2022-08-15 18:29:59 +00:00
|
|
|
} else {
|
|
|
|
peerID, err := (*c.options.Store.Node).ValueForProtocol(multiaddr.P_P2P)
|
|
|
|
if err != nil {
|
|
|
|
c.ui.ErrorMessage(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
pID, err := peer.Decode(peerID)
|
|
|
|
if err != nil {
|
|
|
|
c.ui.ErrorMessage(err)
|
|
|
|
return
|
2022-07-06 20:29:20 +00:00
|
|
|
}
|
2024-05-03 16:07:03 +00:00
|
|
|
storeOpt = legacy_store.WithPeer(pID)
|
2022-08-15 18:29:59 +00:00
|
|
|
c.ui.InfoMessage(fmt.Sprintf("Querying historic messages from %s", peerID))
|
|
|
|
|
2022-07-06 20:29:20 +00:00
|
|
|
}
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
tCtx, cancel := context.WithTimeout(c.ctx, 10*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
|
2024-05-03 16:07:03 +00:00
|
|
|
q := legacy_store.Query{
|
2022-08-15 18:29:59 +00:00
|
|
|
ContentTopics: []string{options.ContentTopic},
|
|
|
|
}
|
|
|
|
|
2024-05-03 16:07:03 +00:00
|
|
|
response, err := c.node.LegacyStore().Query(tCtx, q,
|
|
|
|
legacy_store.WithAutomaticRequestID(),
|
2022-08-15 18:29:59 +00:00
|
|
|
storeOpt,
|
2024-05-03 16:07:03 +00:00
|
|
|
legacy_store.WithPaging(false, 100))
|
2022-08-15 18:29:59 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
c.ui.ErrorMessage(fmt.Errorf("could not query storenode: %w", err))
|
|
|
|
} else {
|
|
|
|
if len(response.Messages) == 0 {
|
|
|
|
c.ui.InfoMessage("0 historic messages available")
|
|
|
|
} else {
|
|
|
|
for _, msg := range response.Messages {
|
2023-11-07 19:48:43 +00:00
|
|
|
c.C <- protocol.NewEnvelope(msg, msg.GetTimestamp(), relay.DefaultWakuTopic)
|
2022-08-15 18:29:59 +00:00
|
|
|
}
|
2022-07-06 20:29:20 +00:00
|
|
|
}
|
2021-04-12 18:03:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
func (c *Chat) staticNodes(connectionWg *sync.WaitGroup) {
|
|
|
|
defer c.wg.Done()
|
|
|
|
defer connectionWg.Done()
|
|
|
|
|
|
|
|
<-c.uiReady // wait until UI is ready
|
|
|
|
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
|
|
|
|
wg.Add(len(options.StaticNodes))
|
|
|
|
for _, n := range options.StaticNodes {
|
|
|
|
go func(addr multiaddr.Multiaddr) {
|
|
|
|
defer wg.Done()
|
2022-10-05 22:08:01 +00:00
|
|
|
ctx, cancel := context.WithTimeout(c.ctx, time.Duration(10)*time.Second)
|
2022-08-15 18:29:59 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
c.ui.InfoMessage(fmt.Sprintf("Connecting to %s", addr.String()))
|
|
|
|
|
2023-05-10 14:13:10 +00:00
|
|
|
err := c.node.DialPeerWithMultiAddress(ctx, addr)
|
2022-08-15 18:29:59 +00:00
|
|
|
if err != nil {
|
|
|
|
c.ui.ErrorMessage(err)
|
|
|
|
}
|
|
|
|
}(n)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chat) welcomeMessage() {
|
|
|
|
defer c.wg.Done()
|
|
|
|
|
|
|
|
<-c.uiReady // wait until UI is ready
|
|
|
|
|
2022-08-15 23:29:19 +00:00
|
|
|
c.ui.InfoMessage("Welcome, " + c.nick + "!")
|
2022-08-15 18:29:59 +00:00
|
|
|
c.ui.InfoMessage("type /help to see available commands \n")
|
|
|
|
|
2022-08-15 23:29:19 +00:00
|
|
|
addrMessage := "Listening on:\n"
|
|
|
|
for _, addr := range c.node.ListenAddresses() {
|
|
|
|
addrMessage += " -" + addr.String() + "\n"
|
|
|
|
}
|
|
|
|
c.ui.InfoMessage(addrMessage)
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
if !c.options.RLNRelay.Enable {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-04-10 15:20:07 +00:00
|
|
|
credential, err := c.node.RLNRelay().IdentityCredential()
|
|
|
|
if err != nil {
|
|
|
|
c.ui.Quit()
|
|
|
|
fmt.Println(err.Error())
|
|
|
|
}
|
|
|
|
|
2023-08-24 18:42:50 +00:00
|
|
|
idx := c.node.RLNRelay().MembershipIndex()
|
2023-04-10 15:20:07 +00:00
|
|
|
|
|
|
|
idTrapdoor := credential.IDTrapdoor
|
|
|
|
idNullifier := credential.IDSecretHash
|
|
|
|
idSecretHash := credential.IDSecretHash
|
|
|
|
idCommitment := credential.IDCommitment
|
2022-08-15 18:29:59 +00:00
|
|
|
|
|
|
|
rlnMessage := "RLN config:\n"
|
2023-04-10 15:20:07 +00:00
|
|
|
rlnMessage += fmt.Sprintf("- Your membership index is: %d\n", idx)
|
|
|
|
rlnMessage += fmt.Sprintf("- Your rln identity trapdoor is: 0x%s\n", hex.EncodeToString(idTrapdoor[:]))
|
|
|
|
rlnMessage += fmt.Sprintf("- Your rln identity nullifier is: 0x%s\n", hex.EncodeToString(idNullifier[:]))
|
|
|
|
rlnMessage += fmt.Sprintf("- Your rln identity secret hash is: 0x%s\n", hex.EncodeToString(idSecretHash[:]))
|
2022-08-15 18:29:59 +00:00
|
|
|
rlnMessage += fmt.Sprintf("- Your rln identity commitment key is: 0x%s\n", hex.EncodeToString(idCommitment[:]))
|
|
|
|
|
|
|
|
c.ui.InfoMessage(rlnMessage)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Chat) discoverNodes(connectionWg *sync.WaitGroup) {
|
|
|
|
defer c.wg.Done()
|
|
|
|
defer connectionWg.Done()
|
|
|
|
|
|
|
|
<-c.uiReady // wait until UI is ready
|
|
|
|
|
|
|
|
var dnsDiscoveryUrl string
|
|
|
|
if options.Fleet != fleetNone {
|
|
|
|
if options.Fleet == fleetTest {
|
2024-03-11 13:22:48 +00:00
|
|
|
dnsDiscoveryUrl = "enrtree://AOGYWMBYOUIMOENHXCHILPKY3ZRFEULMFI4DOM442QSZ73TT2A7VI@test.waku.nodes.status.im"
|
2022-08-15 18:29:59 +00:00
|
|
|
} else {
|
|
|
|
// Connect to prod by default
|
2024-03-11 13:22:48 +00:00
|
|
|
dnsDiscoveryUrl = "enrtree://AIRVQ5DDA4FFWLRBCHJWUWOO6X6S4ZTZ5B667LQ6AJU6PEYDLRD5O@sandbox.waku.nodes.status.im"
|
2022-08-15 18:29:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.DNSDiscovery.Enable && options.DNSDiscovery.URL != "" {
|
|
|
|
dnsDiscoveryUrl = options.DNSDiscovery.URL
|
|
|
|
}
|
|
|
|
|
|
|
|
if dnsDiscoveryUrl != "" {
|
|
|
|
c.ui.InfoMessage(fmt.Sprintf("attempting DNS discovery with %s", dnsDiscoveryUrl))
|
|
|
|
nodes, err := dnsdisc.RetrieveNodes(c.ctx, dnsDiscoveryUrl, dnsdisc.WithNameserver(options.DNSDiscovery.Nameserver))
|
|
|
|
if err != nil {
|
|
|
|
c.ui.ErrorMessage(errors.New(err.Error()))
|
|
|
|
} else {
|
2023-05-10 14:13:10 +00:00
|
|
|
var nodeList []peer.AddrInfo
|
2022-08-15 18:29:59 +00:00
|
|
|
for _, n := range nodes {
|
2023-05-10 14:13:10 +00:00
|
|
|
nodeList = append(nodeList, n.PeerInfo)
|
2022-08-15 18:29:59 +00:00
|
|
|
}
|
|
|
|
c.ui.InfoMessage(fmt.Sprintf("Discovered and connecting to %v ", nodeList))
|
|
|
|
wg := sync.WaitGroup{}
|
|
|
|
wg.Add(len(nodeList))
|
|
|
|
for _, n := range nodeList {
|
2023-05-10 14:13:10 +00:00
|
|
|
go func(ctx context.Context, info peer.AddrInfo) {
|
2022-08-15 18:29:59 +00:00
|
|
|
defer wg.Done()
|
|
|
|
|
2022-10-05 22:08:01 +00:00
|
|
|
ctx, cancel := context.WithTimeout(ctx, time.Duration(10)*time.Second)
|
2022-08-15 18:29:59 +00:00
|
|
|
defer cancel()
|
2023-09-29 05:13:25 +00:00
|
|
|
err = c.node.DialPeerWithInfo(ctx, info)
|
2022-08-15 18:29:59 +00:00
|
|
|
if err != nil {
|
2023-05-10 14:13:10 +00:00
|
|
|
|
2024-01-12 17:40:27 +00:00
|
|
|
c.ui.ErrorMessage(fmt.Errorf("co!!uld not connect to %s: %w", info.ID.String(), err))
|
2022-08-15 18:29:59 +00:00
|
|
|
}
|
|
|
|
}(c.ctx, n)
|
|
|
|
|
|
|
|
}
|
|
|
|
wg.Wait()
|
2022-07-06 20:29:20 +00:00
|
|
|
}
|
2021-04-04 17:06:17 +00:00
|
|
|
}
|
|
|
|
}
|