-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathexec.go
122 lines (102 loc) · 2.96 KB
/
exec.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package main
import (
"context"
"fmt"
"net"
tea "github.com/charmbracelet/bubbletea"
"github.com/ethereum/go-ethereum/crypto"
"github.com/libp2p/go-libp2p/core/protocol"
"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/legacy_store"
"github.com/waku-org/go-waku/waku/v2/protocol/lightpush"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
)
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)
opts := []node.WakuNodeOption{
node.WithPrivateKey(options.NodeKey),
node.WithNTP(),
node.WithHostAddress(hostAddr),
node.WithConnectionNotification(connNotifier),
}
if options.Relay.Enable {
opts = append(opts, node.WithWakuRelay())
}
if options.RLNRelay.Enable {
spamHandler := func(message *pb.WakuMessage, topic string) error {
return nil
}
if options.RLNRelay.Dynamic {
fmt.Println("Setting up dynamic rln...")
opts = append(opts, node.WithDynamicRLNRelay(
options.RLNRelay.CredentialsPath,
options.RLNRelay.CredentialsPassword,
"", // Will use default tree path
options.RLNRelay.MembershipContractAddress,
options.RLNRelay.MembershipIndex,
spamHandler,
options.RLNRelay.ETHClientAddress,
))
} else {
opts = append(opts, node.WithStaticRLNRelay(
options.RLNRelay.MembershipIndex,
spamHandler))
}
}
if options.Filter.Enable {
opts = append(opts, node.WithWakuFilterLightNode())
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
wakuNode, err := node.New(opts...)
if err != nil {
fmt.Println(err.Error())
return
}
err = addPeer(wakuNode, options.Store.Node, options.Relay.Topics.Value(), legacy_store.StoreID_v20beta4)
if err != nil {
fmt.Println(err.Error())
return
}
err = addPeer(wakuNode, options.LightPush.Node, options.Relay.Topics.Value(), lightpush.LightPushID_v20beta1)
if err != nil {
fmt.Println(err.Error())
return
}
err = addPeer(wakuNode, options.Filter.Node, options.Relay.Topics.Value(), filter.FilterSubscribeID_v20beta1)
if err != nil {
fmt.Println(err.Error())
return
}
if err := wakuNode.Start(ctx); err != nil {
fmt.Println(err.Error())
return
}
chat := NewChat(ctx, wakuNode, connNotifier, options)
p := tea.NewProgram(chat.ui)
if err := p.Start(); err != nil {
fmt.Println(err.Error())
}
cancel()
wakuNode.Stop()
chat.Stop()
}
func addPeer(wakuNode *node.WakuNode, addr *multiaddr.Multiaddr, topics []string, protocols ...protocol.ID) error {
if addr == nil {
return nil
}
_, err := wakuNode.AddPeer(*addr, peerstore.Static, topics, protocols...)
return err
}