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"
"github.com/status-im/go-waku/waku/v2/node"
"github.com/status-im/go-waku/waku/v2/protocol/filter"
"github.com/status-im/go-waku/waku/v2/protocol/lightpush"
"github.com/status-im/go-waku/waku/v2/protocol/pb"
"github.com/status-im/go-waku/waku/v2/protocol/store"
)
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 ) ,
node . WithHostAddress ( hostAddr ) ,
node . WithWakuStore ( false , false ) ,
}
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-10-10 22:08:35 +00:00
membershipCredentials , err := getMembershipCredentials ( options . RLNRelay )
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 ( )
wakuNode , err := node . New ( ctx , opts ... )
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
}
if err := wakuNode . Start ( ) ; 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-10-20 21:49:45 +00:00
err := writeRLNMembershipCredentialsToFile ( options . RLNRelay . CredentialsPath , wakuNode . RLNRelay ( ) . MembershipKeyPair ( ) , wakuNode . RLNRelay ( ) . MembershipIndex ( ) , wakuNode . RLNRelay ( ) . MembershipContractAddress ( ) )
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
}