add basic config

This commit is contained in:
Andrea Maria Piana 2020-07-03 12:08:47 +02:00
parent 15a3b710a0
commit 9da64ef251
No known key found for this signature in database
GPG Key ID: AA6CCA6DE0E06424
6 changed files with 151 additions and 95 deletions

View File

@ -523,6 +523,9 @@ type ShhextConfig struct {
VerifyENSContractAddress string
VerifyTransactionChainID int64
// PushNotificationServerEnabled indicates whether a push notification server should be started
PushNotificationServerEnabled bool
}
// Validate validates the ShhextConfig struct and returns an error if inconsistent values are found

View File

@ -3,7 +3,6 @@ package protocol
import (
"context"
"crypto/ecdsa"
"database/sql"
"io/ioutil"
"math/rand"
"os"
@ -26,6 +25,7 @@ import (
"github.com/status-im/status-go/protocol/images"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/push_notification_client"
"github.com/status-im/status-go/protocol/push_notification_server"
"github.com/status-im/status-go/protocol/sqlite"
"github.com/status-im/status-go/protocol/transport"
wakutransp "github.com/status-im/status-go/protocol/transport/waku"
@ -59,6 +59,7 @@ type Messenger struct {
processor *messageProcessor
handler *MessageHandler
pushNotificationClient *push_notification_client.Client
pushNotificationServer *push_notification_server.Server
logger *zap.Logger
verifyTransactionClient EthClient
featureFlags featureFlags
@ -102,99 +103,6 @@ type dbConfig struct {
dbKey string
}
type config struct {
// This needs to be exposed until we move here mailserver logic
// as otherwise the client is not notified of a new filter and
// won't be pulling messages from mailservers until it reloads the chats/filters
onNegotiatedFilters func([]*transport.Filter)
// DEPRECATED: no need to expose it
onSendContactCodeHandler func(*encryption.ProtocolMessageSpec)
// systemMessagesTranslations holds translations for system-messages
systemMessagesTranslations map[protobuf.MembershipUpdateEvent_EventType]string
// Config for the envelopes monitor
envelopesMonitorConfig *transport.EnvelopesMonitorConfig
messagesPersistenceEnabled bool
featureFlags featureFlags
// A path to a database or a database instance is required.
// The database instance has a higher priority.
dbConfig dbConfig
db *sql.DB
verifyTransactionClient EthClient
logger *zap.Logger
}
type Option func(*config) error
// WithSystemMessagesTranslations is required for Group Chats which are currently disabled.
// nolint: unused
func WithSystemMessagesTranslations(t map[protobuf.MembershipUpdateEvent_EventType]string) Option {
return func(c *config) error {
c.systemMessagesTranslations = t
return nil
}
}
func WithOnNegotiatedFilters(h func([]*transport.Filter)) Option {
return func(c *config) error {
c.onNegotiatedFilters = h
return nil
}
}
func WithCustomLogger(logger *zap.Logger) Option {
return func(c *config) error {
c.logger = logger
return nil
}
}
func WithMessagesPersistenceEnabled() Option {
return func(c *config) error {
c.messagesPersistenceEnabled = true
return nil
}
}
func WithDatabaseConfig(dbPath, dbKey string) Option {
return func(c *config) error {
c.dbConfig = dbConfig{dbPath: dbPath, dbKey: dbKey}
return nil
}
}
func WithVerifyTransactionClient(client EthClient) Option {
return func(c *config) error {
c.verifyTransactionClient = client
return nil
}
}
func WithDatabase(db *sql.DB) Option {
return func(c *config) error {
c.db = db
return nil
}
}
func WithDatasync() func(c *config) error {
return func(c *config) error {
c.featureFlags.datasync = true
return nil
}
}
func WithEnvelopesMonitorConfig(emc *transport.EnvelopesMonitorConfig) Option {
return func(c *config) error {
c.envelopesMonitorConfig = emc
return nil
}
}
func NewMessenger(
identity *ecdsa.PrivateKey,
node types.Node,
@ -333,6 +241,12 @@ func NewMessenger(
pushNotificationClientPersistence := push_notification_client.NewPersistence(database)
pushNotificationClient := push_notification_client.New(pushNotificationClientPersistence)
var pushNotificationServer *push_notification_server.Server
if c.pushNotificationServerConfig != nil {
pushNotificationServerPersistence := push_notification_server.NewSQLitePersistence(database)
pushNotificationServer = push_notification_server.New(c.pushNotificationServerConfig, pushNotificationServerPersistence)
}
processor, err := newMessageProcessor(
identity,
database,
@ -357,6 +271,7 @@ func NewMessenger(
processor: processor,
handler: handler,
pushNotificationClient: pushNotificationClient,
pushNotificationServer: pushNotificationServer,
featureFlags: c.featureFlags,
systemMessagesTranslations: c.systemMessagesTranslations,
allChats: make(map[string]*Chat),

View File

@ -0,0 +1,112 @@
package protocol
import (
"database/sql"
"github.com/status-im/status-go/protocol/encryption"
"github.com/status-im/status-go/protocol/protobuf"
"github.com/status-im/status-go/protocol/push_notification_server"
"github.com/status-im/status-go/protocol/transport"
"go.uber.org/zap"
)
type config struct {
// This needs to be exposed until we move here mailserver logic
// as otherwise the client is not notified of a new filter and
// won't be pulling messages from mailservers until it reloads the chats/filters
onNegotiatedFilters func([]*transport.Filter)
// DEPRECATED: no need to expose it
onSendContactCodeHandler func(*encryption.ProtocolMessageSpec)
// systemMessagesTranslations holds translations for system-messages
systemMessagesTranslations map[protobuf.MembershipUpdateEvent_EventType]string
// Config for the envelopes monitor
envelopesMonitorConfig *transport.EnvelopesMonitorConfig
messagesPersistenceEnabled bool
featureFlags featureFlags
// A path to a database or a database instance is required.
// The database instance has a higher priority.
dbConfig dbConfig
db *sql.DB
verifyTransactionClient EthClient
pushNotificationServerConfig *push_notification_server.Config
logger *zap.Logger
}
type Option func(*config) error
// WithSystemMessagesTranslations is required for Group Chats which are currently disabled.
// nolint: unused
func WithSystemMessagesTranslations(t map[protobuf.MembershipUpdateEvent_EventType]string) Option {
return func(c *config) error {
c.systemMessagesTranslations = t
return nil
}
}
func WithOnNegotiatedFilters(h func([]*transport.Filter)) Option {
return func(c *config) error {
c.onNegotiatedFilters = h
return nil
}
}
func WithCustomLogger(logger *zap.Logger) Option {
return func(c *config) error {
c.logger = logger
return nil
}
}
func WithMessagesPersistenceEnabled() Option {
return func(c *config) error {
c.messagesPersistenceEnabled = true
return nil
}
}
func WithDatabaseConfig(dbPath, dbKey string) Option {
return func(c *config) error {
c.dbConfig = dbConfig{dbPath: dbPath, dbKey: dbKey}
return nil
}
}
func WithVerifyTransactionClient(client EthClient) Option {
return func(c *config) error {
c.verifyTransactionClient = client
return nil
}
}
func WithDatabase(db *sql.DB) Option {
return func(c *config) error {
c.db = db
return nil
}
}
func WithPushNotificationServerConfig(pushNotificationServerConfig *push_notification_server.Config) Option {
return func(c *config) error {
c.pushNotificationServerConfig = pushNotificationServerConfig
return nil
}
}
func WithDatasync() func(c *config) error {
return func(c *config) error {
c.featureFlags.datasync = true
return nil
}
}
func WithEnvelopesMonitorConfig(emc *transport.EnvelopesMonitorConfig) Option {
return func(c *config) error {
c.envelopesMonitorConfig = emc
return nil
}
}

View File

@ -9,6 +9,7 @@ import (
"github.com/status-im/status-go/eth-node/crypto/ecies"
"github.com/status-im/status-go/protocol/protobuf"
"go.uber.org/zap"
)
const encryptedPayloadKeyLength = 16
@ -19,6 +20,8 @@ type Config struct {
Identity *ecdsa.PrivateKey
// GorushUrl is the url for the gorush service
GorushURL string
Logger *zap.Logger
}
type Server struct {

View File

@ -37,6 +37,9 @@ type StatusMessage struct {
// Hash is the transport layer hash
Hash []byte `json:"-"`
// Dst is the targeted public key
Dst *ecdsa.PublicKey
// TransportLayerSigPubKey contains the public key provided by the transport layer
TransportLayerSigPubKey *ecdsa.PublicKey `json:"-"`
// ApplicationMetadataLayerPubKey contains the public key provided by the application metadata layer
@ -87,6 +90,14 @@ func (m *StatusMessage) HandleTransport(shhMessage *types.Message) error {
m.TransportLayerSigPubKey = publicKey
m.TransportPayload = shhMessage.Payload
if shhMessage.Dst != nil {
publicKey, err := crypto.UnmarshalPubkey(shhMessage.Dst)
if err != nil {
return err
}
m.Dst = publicKey
}
return nil
}

View File

@ -33,6 +33,7 @@ import (
coretypes "github.com/status-im/status-go/eth-node/core/types"
"github.com/status-im/status-go/eth-node/types"
"github.com/status-im/status-go/protocol"
"github.com/status-im/status-go/protocol/push_notification_server"
"github.com/status-im/status-go/protocol/transport"
)
@ -144,7 +145,7 @@ func (s *Service) InitProtocol(identity *ecdsa.PrivateKey, db *sql.DB, logger *z
EnvelopeEventsHandler: EnvelopeSignalHandler{},
Logger: logger,
}
options := buildMessengerOptions(s.config, db, envelopesMonitorConfig, logger)
options := buildMessengerOptions(s.config, identity, db, envelopesMonitorConfig, logger)
messenger, err := protocol.NewMessenger(
identity,
@ -439,6 +440,7 @@ func onNegotiatedFilters(filters []*transport.Filter) {
func buildMessengerOptions(
config params.ShhextConfig,
identity *ecdsa.PrivateKey,
db *sql.DB,
envelopesMonitorConfig *transport.EnvelopesMonitorConfig,
logger *zap.Logger,
@ -454,6 +456,16 @@ func buildMessengerOptions(
options = append(options, protocol.WithDatasync())
}
// For now build with default/hardcoded options.
if config.PushNotificationServerEnabled {
config := &push_notification_server.Config{
Identity: identity,
Logger: logger,
GorushURL: "https://gorush.status.im",
}
options = append(options, protocol.WithPushNotificationServerConfig(config))
}
if config.VerifyTransactionURL != "" {
client := &verifyTransactionClient{
url: config.VerifyTransactionURL,