2022-08-15 18:29:59 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
"github.com/ethereum/go-ethereum/crypto"
|
2023-02-16 16:17:52 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/protocol"
|
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/node"
|
2023-08-10 21:55:43 +00:00
|
|
|
"github.com/waku-org/go-waku/waku/v2/peerstore"
|
2022-11-09 19:53:01 +00:00
|
|
|
"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"
|
|
|
|
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
|
2022-08-15 18:29:59 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func execute(options Options) {
|
|
|
|
var err error
|
|
|
|
hostAddr, _ := net.ResolveTCPAddr("tcp", fmt.Sprintf("0.0.0.0:%d", options.Port))
|
|
|
|
|
|
|
|
if options.NodeKey == nil {
|
|
|
|
options.NodeKey, err = crypto.GenerateKey()
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Could not generate random key")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-10 14:13:10 +00:00
|
|
|
connNotifier := make(chan node.PeerConnection)
|
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
opts := []node.WakuNodeOption{
|
|
|
|
node.WithPrivateKey(options.NodeKey),
|
2022-12-09 03:08:04 +00:00
|
|
|
node.WithNTP(),
|
2022-08-15 18:29:59 +00:00
|
|
|
node.WithHostAddress(hostAddr),
|
2023-05-10 14:13:10 +00:00
|
|
|
node.WithConnectionNotification(connNotifier),
|
2022-08-15 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if options.Relay.Enable {
|
|
|
|
opts = append(opts, node.WithWakuRelay())
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.RLNRelay.Enable {
|
2023-09-07 21:39:10 +00:00
|
|
|
spamHandler := func(message *pb.WakuMessage, topic string) error {
|
2022-08-15 18:29:59 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-10 15:20:07 +00:00
|
|
|
if options.RLNRelay.Dynamic {
|
2022-08-15 23:29:19 +00:00
|
|
|
fmt.Println("Setting up dynamic rln...")
|
2022-08-15 18:29:59 +00:00
|
|
|
opts = append(opts, node.WithDynamicRLNRelay(
|
2023-04-10 15:20:07 +00:00
|
|
|
options.RLNRelay.CredentialsPath,
|
|
|
|
options.RLNRelay.CredentialsPassword,
|
2023-08-10 21:55:43 +00:00
|
|
|
"", // Will use default tree path
|
2023-04-10 15:20:07 +00:00
|
|
|
options.RLNRelay.MembershipContractAddress,
|
2023-09-04 21:44:41 +00:00
|
|
|
options.RLNRelay.MembershipIndex,
|
2022-08-15 18:29:59 +00:00
|
|
|
spamHandler,
|
|
|
|
options.RLNRelay.ETHClientAddress,
|
|
|
|
))
|
|
|
|
} else {
|
|
|
|
opts = append(opts, node.WithStaticRLNRelay(
|
2023-09-04 21:44:41 +00:00
|
|
|
options.RLNRelay.MembershipIndex,
|
2022-08-15 18:29:59 +00:00
|
|
|
spamHandler))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if options.Filter.Enable {
|
2023-08-04 13:07:43 +00:00
|
|
|
opts = append(opts, node.WithWakuFilterLightNode())
|
2022-08-15 18:29:59 +00:00
|
|
|
}
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
2023-01-06 22:37:57 +00:00
|
|
|
wakuNode, err := node.New(opts...)
|
2022-08-15 18:29:59 +00:00
|
|
|
if err != nil {
|
2022-08-15 23:29:19 +00:00
|
|
|
fmt.Println(err.Error())
|
2022-08-15 18:29:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-03 16:07:03 +00:00
|
|
|
err = addPeer(wakuNode, options.Store.Node, options.Relay.Topics.Value(), legacy_store.StoreID_v20beta4)
|
2022-08-15 18:29:59 +00:00
|
|
|
if err != nil {
|
2022-08-15 23:29:19 +00:00
|
|
|
fmt.Println(err.Error())
|
2022-08-15 18:29:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-14 15:00:06 +00:00
|
|
|
err = addPeer(wakuNode, options.LightPush.Node, options.Relay.Topics.Value(), lightpush.LightPushID_v20beta1)
|
2022-08-15 18:29:59 +00:00
|
|
|
if err != nil {
|
2022-08-15 23:29:19 +00:00
|
|
|
fmt.Println(err.Error())
|
2022-08-15 18:29:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-09-14 15:00:06 +00:00
|
|
|
err = addPeer(wakuNode, options.Filter.Node, options.Relay.Topics.Value(), filter.FilterSubscribeID_v20beta1)
|
2023-08-04 13:07:43 +00:00
|
|
|
|
2022-08-15 18:29:59 +00:00
|
|
|
if err != nil {
|
2022-08-15 23:29:19 +00:00
|
|
|
fmt.Println(err.Error())
|
2022-08-15 18:29:59 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-01-06 22:37:57 +00:00
|
|
|
if err := wakuNode.Start(ctx); err != nil {
|
2022-08-15 23:29:19 +00:00
|
|
|
fmt.Println(err.Error())
|
|
|
|
return
|
2022-08-15 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
2023-05-10 14:13:10 +00:00
|
|
|
chat := NewChat(ctx, wakuNode, connNotifier, options)
|
2022-08-15 18:29:59 +00:00
|
|
|
p := tea.NewProgram(chat.ui)
|
|
|
|
if err := p.Start(); err != nil {
|
2022-08-15 23:29:19 +00:00
|
|
|
fmt.Println(err.Error())
|
2022-08-15 18:29:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
wakuNode.Stop()
|
|
|
|
chat.Stop()
|
|
|
|
}
|
|
|
|
|
2023-09-14 15:00:06 +00:00
|
|
|
func addPeer(wakuNode *node.WakuNode, addr *multiaddr.Multiaddr, topics []string, protocols ...protocol.ID) error {
|
2022-08-15 18:29:59 +00:00
|
|
|
if addr == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2023-09-14 15:00:06 +00:00
|
|
|
_, err := wakuNode.AddPeer(*addr, peerstore.Static, topics, protocols...)
|
2022-08-15 18:29:59 +00:00
|
|
|
return err
|
|
|
|
}
|