go-waku/examples/chat2/exec.go

123 lines
2.9 KiB
Go
Raw Normal View History

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"
"github.com/waku-org/go-waku/waku/v2/node"
"github.com/waku-org/go-waku/waku/v2/peerstore"
"github.com/waku-org/go-waku/waku/v2/protocol/filter"
"github.com/waku-org/go-waku/waku/v2/protocol/lightpush"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/protocol/store"
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
}
}
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),
node.WithConnectionNotification(connNotifier),
2022-08-15 18:29:59 +00:00
}
if options.Relay.Enable {
opts = append(opts, node.WithWakuRelay())
}
if options.RLNRelay.Enable {
spamHandler := func(message *pb.WakuMessage, topic string) error {
2022-08-15 18:29:59 +00:00
return nil
}
if options.RLNRelay.Dynamic {
fmt.Println("Setting up dynamic rln...")
2022-08-15 18:29:59 +00:00
opts = append(opts, node.WithDynamicRLNRelay(
options.RLNRelay.CredentialsPath,
options.RLNRelay.CredentialsPassword,
"", // Will use default tree path
options.RLNRelay.MembershipContractAddress,
options.RLNRelay.MembershipIndex,
2022-08-15 18:29:59 +00:00
spamHandler,
options.RLNRelay.ETHClientAddress,
))
} else {
opts = append(opts, node.WithStaticRLNRelay(
options.RLNRelay.MembershipIndex,
2022-08-15 18:29:59 +00:00
spamHandler))
}
}
if options.Filter.Enable {
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 {
fmt.Println(err.Error())
2022-08-15 18:29:59 +00:00
return
}
err = addPeer(wakuNode, options.Store.Node, options.Relay.Topics.Value(), store.StoreID_v20beta4)
2022-08-15 18:29:59 +00:00
if err != nil {
fmt.Println(err.Error())
2022-08-15 18:29:59 +00:00
return
}
err = addPeer(wakuNode, options.LightPush.Node, options.Relay.Topics.Value(), lightpush.LightPushID_v20beta1)
2022-08-15 18:29:59 +00:00
if err != nil {
fmt.Println(err.Error())
2022-08-15 18:29:59 +00:00
return
}
err = addPeer(wakuNode, options.Filter.Node, options.Relay.Topics.Value(), filter.FilterSubscribeID_v20beta1)
2022-08-15 18:29:59 +00:00
if err != nil {
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 {
fmt.Println(err.Error())
return
2022-08-15 18:29:59 +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 {
fmt.Println(err.Error())
2022-08-15 18:29:59 +00:00
}
cancel()
wakuNode.Stop()
chat.Stop()
}
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
}
_, err := wakuNode.AddPeer(*addr, peerstore.Static, topics, protocols...)
2022-08-15 18:29:59 +00:00
return err
}