go-waku/examples/chat2/exec.go

152 lines
3.7 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"
2022-09-11 21:08:58 +00:00
"github.com/ethereum/go-ethereum/core/types"
2022-08-15 18:29:59 +00:00
"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/protocol/filter"
"github.com/waku-org/go-waku/waku/v2/protocol/filterv2"
"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
}
}
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),
}
if options.Relay.Enable {
opts = append(opts, node.WithWakuRelay())
}
if options.RLNRelay.Enable {
spamHandler := func(message *pb.WakuMessage) error {
return nil
}
2022-09-11 21:08:58 +00:00
registrationHandler := func(tx *types.Transaction) {
chainID := tx.ChainId().Int64()
url := ""
switch chainID {
case 1:
url = "https://etherscan.io"
case 5:
url = "https://goerli.etherscan.io"
case 11155111:
url = "https://sepolia.etherscan.io"
2022-09-11 21:08:58 +00:00
2022-08-15 18:29:59 +00:00
}
if url != "" {
fmt.Println(fmt.Sprintf("You are registered to the rln membership contract, find details of your registration transaction in %s/tx/%s", url, tx.Hash()))
} else {
fmt.Println(fmt.Sprintf("You are registered to the rln membership contract. Transaction hash: %s", url, tx.Hash()))
}
}
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.PubsubTopic,
options.RLNRelay.ContentTopic,
options.RLNRelay.CredentialsPath,
options.RLNRelay.CredentialsPassword,
options.RLNRelay.MembershipContractAddress,
2022-08-15 18:29:59 +00:00
spamHandler,
options.RLNRelay.ETHClientAddress,
options.RLNRelay.ETHPrivateKey,
2022-09-11 21:08:58 +00:00
registrationHandler,
2022-08-15 18:29:59 +00:00
))
} else {
opts = append(opts, node.WithStaticRLNRelay(
options.RLNRelay.PubsubTopic,
options.RLNRelay.ContentTopic,
uint(options.RLNRelay.MembershipIndex),
spamHandler))
}
}
if options.Filter.Enable {
if options.Filter.UseV2 {
opts = append(opts, node.WithWakuFilterV2LightNode())
} else {
opts = append(opts, node.WithWakuFilter(false))
}
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
}
2023-02-16 16:17:52 +00:00
err = addPeer(wakuNode, options.Store.Node, 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
}
2023-02-16 16:17:52 +00:00
err = addPeer(wakuNode, options.LightPush.Node, 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
}
if options.Filter.UseV2 {
2023-02-16 16:17:52 +00:00
err = addPeer(wakuNode, options.Filter.Node, filterv2.FilterSubscribeID_v20beta1)
} else {
2023-02-16 16:17:52 +00:00
err = addPeer(wakuNode, options.Filter.Node, filter.FilterID_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, options)
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()
}
2023-02-16 16:17:52 +00:00
func addPeer(wakuNode *node.WakuNode, addr *multiaddr.Multiaddr, protocols ...protocol.ID) error {
2022-08-15 18:29:59 +00:00
if addr == nil {
return nil
}
_, err := wakuNode.AddPeer(*addr, protocols...)
return err
}