2021-04-18 23:41:42 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"net"
|
|
|
|
|
|
|
|
"github.com/libp2p/go-libp2p"
|
|
|
|
connmgr "github.com/libp2p/go-libp2p-connmgr"
|
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
|
|
|
ma "github.com/multiformats/go-multiaddr"
|
|
|
|
manet "github.com/multiformats/go-multiaddr-net"
|
2021-04-22 00:09:37 +00:00
|
|
|
"github.com/status-im/go-waku/waku/v2/protocol/store"
|
2021-04-18 23:41:42 +00:00
|
|
|
wakurelay "github.com/status-im/go-wakurelay-pubsub"
|
|
|
|
)
|
|
|
|
|
|
|
|
type WakuNodeParameters struct {
|
|
|
|
multiAddr []ma.Multiaddr
|
|
|
|
privKey *crypto.PrivKey
|
|
|
|
libP2POpts []libp2p.Option
|
|
|
|
|
|
|
|
enableRelay bool
|
|
|
|
wOpts []wakurelay.Option
|
|
|
|
|
|
|
|
enableStore bool
|
|
|
|
storeMsgs bool
|
|
|
|
store *store.WakuStore
|
|
|
|
}
|
|
|
|
|
|
|
|
type WakuNodeOption func(*WakuNodeParameters) error
|
|
|
|
|
|
|
|
func WithHostAddress(hostAddr []net.Addr) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
var multiAddresses []ma.Multiaddr
|
|
|
|
for _, addr := range hostAddr {
|
|
|
|
hostAddrMA, err := manet.FromNetAddr(addr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
multiAddresses = append(multiAddresses, hostAddrMA)
|
|
|
|
}
|
|
|
|
|
|
|
|
params.multiAddr = multiAddresses
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithPrivateKey(privKey *ecdsa.PrivateKey) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
privk := crypto.PrivKey((*crypto.Secp256k1PrivateKey)(privKey))
|
|
|
|
params.privKey = &privk
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithLibP2POptions(opts ...libp2p.Option) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.libP2POpts = opts
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithWakuRelay(opts ...wakurelay.Option) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableRelay = true
|
|
|
|
params.wOpts = opts
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithWakuStore(shouldStoreMessages bool) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
params.enableStore = true
|
|
|
|
params.storeMsgs = shouldStoreMessages
|
2021-04-22 00:09:37 +00:00
|
|
|
params.store = store.NewWakuStore(shouldStoreMessages, nil)
|
2021-04-18 23:41:42 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithMessageProvider(s store.MessageProvider) WakuNodeOption {
|
|
|
|
return func(params *WakuNodeParameters) error {
|
|
|
|
if params.store != nil {
|
|
|
|
params.store.SetMsgProvider(s)
|
|
|
|
} else {
|
2021-04-22 00:09:37 +00:00
|
|
|
params.store = store.NewWakuStore(true, s)
|
2021-04-18 23:41:42 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var DefaultLibP2POptions = []libp2p.Option{
|
|
|
|
libp2p.DefaultTransports,
|
|
|
|
libp2p.NATPortMap(), // Attempt to open ports using uPNP for NATed hosts.
|
|
|
|
libp2p.EnableNATService(), // TODO: is this needed?)
|
|
|
|
libp2p.ConnectionManager(connmgr.NewConnManager(200, 300, 0)),
|
|
|
|
}
|