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"
"github.com/multiformats/go-multiaddr"
2022-11-09 19:53:01 +00:00
"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/lightpush"
"github.com/waku-org/go-waku/waku/v2/protocol/pb"
"github.com/waku-org/go-waku/waku/v2/protocol/store"
"github.com/waku-org/go-waku/waku/v2/utils"
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 ) {
fmt . Println ( fmt . Sprintf ( "You are registered to the rln membership contract, find details of your registration transaction in https://goerli.etherscan.io/tx/%s" , tx . Hash ( ) ) )
}
2022-08-15 18:29:59 +00:00
if options . RLNRelay . Dynamic {
2022-11-08 20:20:08 +00:00
membershipCredentials , err := node . GetMembershipCredentials (
utils . Logger ( ) ,
options . RLNRelay . CredentialsPath ,
options . RLNRelay . CredentialsPassword ,
options . RLNRelay . MembershipContractAddress ,
uint ( options . RLNRelay . MembershipIndex ) ,
)
2022-08-15 18:29:59 +00:00
if err != nil {
2022-08-15 23:29:19 +00:00
fmt . Println ( err )
return
2022-08-15 18:29:59 +00:00
}
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 (
options . RLNRelay . PubsubTopic ,
options . RLNRelay . ContentTopic ,
2022-10-10 22:08:35 +00:00
membershipCredentials ,
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 {
opts = append ( opts , node . WithWakuFilter ( false ) )
}
if options . LightPush . Enable {
opts = append ( opts , node . WithLightPush ( ) )
}
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
}
err = addPeer ( wakuNode , options . Store . Node , string ( store . StoreID_v20beta4 ) )
if err != nil {
2022-08-15 23:29:19 +00:00
fmt . Println ( err . Error ( ) )
2022-08-15 18:29:59 +00:00
return
}
err = addPeer ( wakuNode , options . LightPush . Node , string ( lightpush . LightPushID_v20beta1 ) )
if err != nil {
2022-08-15 23:29:19 +00:00
fmt . Println ( err . Error ( ) )
2022-08-15 18:29:59 +00:00
return
}
err = addPeer ( wakuNode , options . Filter . Node , string ( filter . FilterID_v20beta1 ) )
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
}
if options . RLNRelay . Enable && options . RLNRelay . Dynamic {
2022-11-08 20:20:08 +00:00
err := node . WriteRLNMembershipCredentialsToFile ( wakuNode . RLNRelay ( ) . MembershipKeyPair ( ) , wakuNode . RLNRelay ( ) . MembershipIndex ( ) , wakuNode . RLNRelay ( ) . MembershipContractAddress ( ) , options . RLNRelay . CredentialsPath , [ ] byte ( options . RLNRelay . CredentialsPassword ) )
2022-10-20 21:49:45 +00:00
if 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 {
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 ( )
}
func addPeer ( wakuNode * node . WakuNode , addr * multiaddr . Multiaddr , protocols ... string ) error {
if addr == nil {
return nil
}
_ , err := wakuNode . AddPeer ( * addr , protocols ... )
return err
}