2020-01-20 20:56:06 +00:00
|
|
|
package ext
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"database/sql"
|
2021-09-01 12:02:18 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2020-01-20 20:56:06 +00:00
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
|
|
|
|
|
|
|
commongethtypes "github.com/ethereum/go-ethereum/common"
|
|
|
|
gethtypes "github.com/ethereum/go-ethereum/core/types"
|
|
|
|
"github.com/ethereum/go-ethereum/ethclient"
|
|
|
|
"github.com/ethereum/go-ethereum/log"
|
|
|
|
"github.com/ethereum/go-ethereum/node"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p"
|
|
|
|
"github.com/ethereum/go-ethereum/p2p/enode"
|
|
|
|
"github.com/ethereum/go-ethereum/rpc"
|
|
|
|
|
2021-05-14 10:55:42 +00:00
|
|
|
"github.com/status-im/status-go/connection"
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/status-im/status-go/db"
|
|
|
|
coretypes "github.com/status-im/status-go/eth-node/core/types"
|
2021-09-01 12:02:18 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2020-11-25 11:39:01 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts"
|
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
|
|
|
"github.com/status-im/status-go/params"
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/status-im/status-go/protocol"
|
2021-09-01 12:02:18 +00:00
|
|
|
"github.com/status-im/status-go/protocol/anonmetrics"
|
2020-07-22 07:41:40 +00:00
|
|
|
"github.com/status-im/status-go/protocol/pushnotificationclient"
|
|
|
|
"github.com/status-im/status-go/protocol/pushnotificationserver"
|
2020-01-20 20:56:06 +00:00
|
|
|
"github.com/status-im/status-go/protocol/transport"
|
2020-11-25 11:39:01 +00:00
|
|
|
"github.com/status-im/status-go/services/ext/mailservers"
|
2021-01-26 11:49:37 +00:00
|
|
|
localnotifications "github.com/status-im/status-go/services/local-notifications"
|
2021-01-14 22:15:13 +00:00
|
|
|
mailserversDB "github.com/status-im/status-go/services/mailservers"
|
2021-09-09 14:28:54 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/transfer"
|
2020-11-25 11:39:01 +00:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2020-01-20 20:56:06 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// EnvelopeEventsHandler used for two different event types.
|
|
|
|
type EnvelopeEventsHandler interface {
|
|
|
|
EnvelopeSent([][]byte)
|
|
|
|
EnvelopeExpired([][]byte, error)
|
|
|
|
MailServerRequestCompleted(types.Hash, types.Hash, []byte, error)
|
|
|
|
MailServerRequestExpired(types.Hash)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Service is a service that provides some additional API to whisper-based protocols like Whisper or Waku.
|
|
|
|
type Service struct {
|
2021-05-13 13:30:33 +00:00
|
|
|
messenger *protocol.Messenger
|
|
|
|
identity *ecdsa.PrivateKey
|
|
|
|
cancelMessenger chan struct{}
|
|
|
|
storage db.TransactionalStorage
|
|
|
|
n types.Node
|
|
|
|
config params.ShhextConfig
|
|
|
|
mailMonitor *MailRequestMonitor
|
|
|
|
server *p2p.Server
|
|
|
|
peerStore *mailservers.PeerStore
|
|
|
|
accountsDB *accounts.Database
|
|
|
|
multiAccountsDB *multiaccounts.Database
|
|
|
|
account *multiaccounts.Account
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure that Service implements node.Service interface.
|
2021-06-30 11:40:54 +00:00
|
|
|
var _ node.Lifecycle = (*Service)(nil)
|
2020-01-20 20:56:06 +00:00
|
|
|
|
|
|
|
func New(
|
|
|
|
config params.ShhextConfig,
|
|
|
|
n types.Node,
|
|
|
|
ldb *leveldb.DB,
|
|
|
|
mailMonitor *MailRequestMonitor,
|
|
|
|
eventSub mailservers.EnvelopeEventSubscriber,
|
|
|
|
) *Service {
|
|
|
|
cache := mailservers.NewCache(ldb)
|
|
|
|
peerStore := mailservers.NewPeerStore(cache)
|
|
|
|
return &Service{
|
2021-05-13 13:30:33 +00:00
|
|
|
storage: db.NewLevelDBStorage(ldb),
|
|
|
|
n: n,
|
|
|
|
config: config,
|
|
|
|
mailMonitor: mailMonitor,
|
|
|
|
peerStore: peerStore,
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) NodeID() *ecdsa.PrivateKey {
|
|
|
|
if s.server == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return s.server.PrivateKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) GetPeer(rawURL string) (*enode.Node, error) {
|
|
|
|
if len(rawURL) == 0 {
|
|
|
|
return mailservers.GetFirstConnected(s.server, s.peerStore)
|
|
|
|
}
|
|
|
|
return enode.ParseV4(rawURL)
|
|
|
|
}
|
|
|
|
|
2021-11-03 12:38:37 +00:00
|
|
|
func (s *Service) InitProtocol(nodeName string, identity *ecdsa.PrivateKey, db *sql.DB, multiAccountDb *multiaccounts.Database, acc *multiaccounts.Account, logger *zap.Logger) error {
|
2020-01-20 20:56:06 +00:00
|
|
|
if !s.config.PFSEnabled {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// If Messenger has been already set up, we need to shut it down
|
|
|
|
// before we init it again. Otherwise, it will lead to goroutines leakage
|
|
|
|
// due to not stopped filters.
|
|
|
|
if s.messenger != nil {
|
|
|
|
if err := s.messenger.Shutdown(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
s.identity = identity
|
|
|
|
|
|
|
|
dataDir := filepath.Clean(s.config.BackupDisabledDataDir)
|
|
|
|
|
|
|
|
if err := os.MkdirAll(dataDir, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
envelopesMonitorConfig := &transport.EnvelopesMonitorConfig{
|
2021-11-18 17:19:41 +00:00
|
|
|
MaxAttempts: s.config.MaxMessageDeliveryAttempts,
|
|
|
|
MailserverConfirmationsEnabled: s.config.MailServerConfirmations,
|
2020-01-20 20:56:06 +00:00
|
|
|
IsMailserver: func(peer types.EnodeID) bool {
|
|
|
|
return s.peerStore.Exist(peer)
|
|
|
|
},
|
|
|
|
EnvelopeEventsHandler: EnvelopeSignalHandler{},
|
2020-02-18 11:21:01 +00:00
|
|
|
Logger: logger,
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
2020-07-15 12:25:01 +00:00
|
|
|
s.accountsDB = accounts.NewDB(db)
|
2020-11-24 13:13:46 +00:00
|
|
|
s.multiAccountsDB = multiAccountDb
|
2020-12-09 14:03:43 +00:00
|
|
|
s.account = acc
|
2020-07-15 12:25:01 +00:00
|
|
|
|
2021-04-19 12:09:46 +00:00
|
|
|
options, err := buildMessengerOptions(s.config, identity, db, s.multiAccountsDB, acc, envelopesMonitorConfig, s.accountsDB, logger, &MessengerSignalsHandler{})
|
2020-07-15 12:25:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-20 20:56:06 +00:00
|
|
|
|
|
|
|
messenger, err := protocol.NewMessenger(
|
2021-11-03 12:38:37 +00:00
|
|
|
nodeName,
|
2020-01-20 20:56:06 +00:00
|
|
|
identity,
|
|
|
|
s.n,
|
|
|
|
s.config.InstallationID,
|
|
|
|
options...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.messenger = messenger
|
2020-01-29 19:40:06 +00:00
|
|
|
return messenger.Init()
|
|
|
|
}
|
|
|
|
|
2021-01-14 22:15:13 +00:00
|
|
|
func (s *Service) StartMessenger() (*protocol.MessengerResponse, error) {
|
2020-01-20 20:56:06 +00:00
|
|
|
// Start a loop that retrieves all messages and propagates them to status-react.
|
|
|
|
s.cancelMessenger = make(chan struct{})
|
2021-01-14 22:15:13 +00:00
|
|
|
response, err := s.messenger.Start()
|
2020-07-31 09:46:38 +00:00
|
|
|
if err != nil {
|
2021-01-14 22:15:13 +00:00
|
|
|
return nil, err
|
2020-07-31 09:46:38 +00:00
|
|
|
}
|
2020-01-20 20:56:06 +00:00
|
|
|
go s.retrieveMessagesLoop(time.Second, s.cancelMessenger)
|
|
|
|
go s.verifyTransactionLoop(30*time.Second, s.cancelMessenger)
|
2021-09-29 14:52:45 +00:00
|
|
|
|
|
|
|
if s.config.BandwidthStatsEnabled {
|
|
|
|
go s.retrieveStats(5*time.Second, s.cancelMessenger)
|
|
|
|
}
|
|
|
|
|
2021-01-14 22:15:13 +00:00
|
|
|
return response, nil
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
|
|
|
|
2021-01-11 10:32:51 +00:00
|
|
|
func publishMessengerResponse(response *protocol.MessengerResponse) {
|
|
|
|
if !response.IsEmpty() {
|
2021-03-31 16:23:45 +00:00
|
|
|
notifications := response.Notifications()
|
2021-01-11 10:32:51 +00:00
|
|
|
// Clear notifications as not used for now
|
2021-03-31 16:23:45 +00:00
|
|
|
response.ClearNotifications()
|
|
|
|
PublisherSignalHandler{}.NewMessages(response)
|
|
|
|
localnotifications.PushMessages(notifications)
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
func (s *Service) retrieveMessagesLoop(tick time.Duration, cancel <-chan struct{}) {
|
|
|
|
ticker := time.NewTicker(tick)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
response, err := s.messenger.RetrieveAll()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to retrieve raw messages", "err", err)
|
|
|
|
continue
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
publishMessengerResponse(response)
|
2020-01-20 20:56:06 +00:00
|
|
|
case <-cancel:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-03 19:27:15 +00:00
|
|
|
func (s *Service) retrieveStats(tick time.Duration, cancel <-chan struct{}) {
|
|
|
|
ticker := time.NewTicker(tick)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
response := s.messenger.GetStats()
|
|
|
|
PublisherSignalHandler{}.Stats(response)
|
|
|
|
case <-cancel:
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
type verifyTransactionClient struct {
|
|
|
|
chainID *big.Int
|
|
|
|
url string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *verifyTransactionClient) TransactionByHash(ctx context.Context, hash types.Hash) (coretypes.Message, coretypes.TransactionStatus, error) {
|
2021-07-07 06:11:09 +00:00
|
|
|
signer := gethtypes.NewLondonSigner(c.chainID)
|
2020-01-20 20:56:06 +00:00
|
|
|
client, err := ethclient.Dial(c.url)
|
|
|
|
if err != nil {
|
|
|
|
return coretypes.Message{}, coretypes.TransactionStatusPending, err
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction, pending, err := client.TransactionByHash(ctx, commongethtypes.BytesToHash(hash.Bytes()))
|
|
|
|
if err != nil {
|
|
|
|
return coretypes.Message{}, coretypes.TransactionStatusPending, err
|
|
|
|
}
|
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
message, err := transaction.AsMessage(signer, nil)
|
2020-01-20 20:56:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return coretypes.Message{}, coretypes.TransactionStatusPending, err
|
|
|
|
}
|
|
|
|
from := types.BytesToAddress(message.From().Bytes())
|
|
|
|
to := types.BytesToAddress(message.To().Bytes())
|
|
|
|
|
|
|
|
if pending {
|
|
|
|
return coretypes.NewMessage(
|
|
|
|
from,
|
|
|
|
&to,
|
|
|
|
message.Nonce(),
|
|
|
|
message.Value(),
|
|
|
|
message.Gas(),
|
|
|
|
message.GasPrice(),
|
|
|
|
message.Data(),
|
|
|
|
message.CheckNonce(),
|
|
|
|
), coretypes.TransactionStatusPending, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
receipt, err := client.TransactionReceipt(ctx, commongethtypes.BytesToHash(hash.Bytes()))
|
|
|
|
if err != nil {
|
|
|
|
return coretypes.Message{}, coretypes.TransactionStatusPending, err
|
|
|
|
}
|
|
|
|
|
|
|
|
coremessage := coretypes.NewMessage(
|
|
|
|
from,
|
|
|
|
&to,
|
|
|
|
message.Nonce(),
|
|
|
|
message.Value(),
|
|
|
|
message.Gas(),
|
|
|
|
message.GasPrice(),
|
|
|
|
message.Data(),
|
|
|
|
message.CheckNonce(),
|
|
|
|
)
|
|
|
|
|
|
|
|
// Token transfer, check the logs
|
|
|
|
if len(coremessage.Data()) != 0 {
|
2021-09-09 14:28:54 +00:00
|
|
|
if transfer.IsTokenTransfer(receipt.Logs) {
|
2020-01-20 20:56:06 +00:00
|
|
|
return coremessage, coretypes.TransactionStatus(receipt.Status), nil
|
|
|
|
}
|
|
|
|
return coremessage, coretypes.TransactionStatusFailed, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return coremessage, coretypes.TransactionStatus(receipt.Status), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) verifyTransactionLoop(tick time.Duration, cancel <-chan struct{}) {
|
|
|
|
if s.config.VerifyTransactionURL == "" {
|
|
|
|
log.Warn("not starting transaction loop")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(tick)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
ctx, cancelVerifyTransaction := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
|
|
|
accounts, err := s.accountsDB.GetAccounts()
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to retrieve accounts", "err", err)
|
|
|
|
}
|
|
|
|
var wallets []types.Address
|
|
|
|
for _, account := range accounts {
|
2020-12-07 14:03:18 +00:00
|
|
|
if account.IsOwnAccount() {
|
2020-01-20 20:56:06 +00:00
|
|
|
wallets = append(wallets, types.BytesToAddress(account.Address.Bytes()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
response, err := s.messenger.ValidateTransactions(ctx, wallets)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("failed to validate transactions", "err", err)
|
|
|
|
continue
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
publishMessengerResponse(response)
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
case <-cancel:
|
|
|
|
cancelVerifyTransaction()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) EnableInstallation(installationID string) error {
|
|
|
|
return s.messenger.EnableInstallation(installationID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// DisableInstallation disables an installation for multi-device sync.
|
|
|
|
func (s *Service) DisableInstallation(installationID string) error {
|
|
|
|
return s.messenger.DisableInstallation(installationID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// UpdateMailservers updates information about selected mail servers.
|
|
|
|
func (s *Service) UpdateMailservers(nodes []*enode.Node) error {
|
2020-07-07 09:00:04 +00:00
|
|
|
if len(nodes) > 0 && s.messenger != nil {
|
|
|
|
s.messenger.SetMailserver(nodes[0].ID().Bytes())
|
|
|
|
}
|
2021-05-13 13:30:33 +00:00
|
|
|
for _, peer := range nodes {
|
|
|
|
s.server.AddPeer(peer)
|
|
|
|
}
|
2020-01-20 20:56:06 +00:00
|
|
|
if err := s.peerStore.Update(nodes); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-21 19:02:50 +00:00
|
|
|
func (s *Service) SetMailserver(peer []byte) {
|
|
|
|
s.messenger.SetMailserver(peer)
|
|
|
|
}
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
// Protocols returns a new protocols list. In this case, there are none.
|
|
|
|
func (s *Service) Protocols() []p2p.Protocol {
|
|
|
|
return []p2p.Protocol{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// APIs returns a list of new APIs.
|
|
|
|
func (s *Service) APIs() []rpc.API {
|
|
|
|
panic("this is abstract service, use shhext or wakuext implementation")
|
|
|
|
}
|
|
|
|
|
2021-06-30 11:40:54 +00:00
|
|
|
func (s *Service) SetP2PServer(server *p2p.Server) {
|
|
|
|
s.server = server
|
|
|
|
}
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
// Start is run when a service is started.
|
|
|
|
// It does nothing in this case but is required by `node.Service` interface.
|
2021-06-30 11:40:54 +00:00
|
|
|
func (s *Service) Start() error {
|
2020-01-20 20:56:06 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop is run when a service is stopped.
|
|
|
|
func (s *Service) Stop() error {
|
|
|
|
log.Info("Stopping shhext service")
|
|
|
|
if s.cancelMessenger != nil {
|
|
|
|
select {
|
|
|
|
case <-s.cancelMessenger:
|
|
|
|
// channel already closed
|
|
|
|
default:
|
|
|
|
close(s.cancelMessenger)
|
|
|
|
s.cancelMessenger = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if s.messenger != nil {
|
|
|
|
if err := s.messenger.Shutdown(); err != nil {
|
2021-01-14 22:15:13 +00:00
|
|
|
log.Error("failed to stop messenger", "err", err)
|
2020-01-20 20:56:06 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-07 06:11:09 +00:00
|
|
|
s.messenger = nil
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func buildMessengerOptions(
|
|
|
|
config params.ShhextConfig,
|
2020-07-03 10:08:47 +00:00
|
|
|
identity *ecdsa.PrivateKey,
|
2020-01-20 20:56:06 +00:00
|
|
|
db *sql.DB,
|
2020-11-24 13:13:46 +00:00
|
|
|
multiAccounts *multiaccounts.Database,
|
2020-12-09 14:03:43 +00:00
|
|
|
account *multiaccounts.Account,
|
2020-01-20 20:56:06 +00:00
|
|
|
envelopesMonitorConfig *transport.EnvelopesMonitorConfig,
|
2020-07-15 12:25:01 +00:00
|
|
|
accountsDB *accounts.Database,
|
2020-01-20 20:56:06 +00:00
|
|
|
logger *zap.Logger,
|
2021-04-19 12:09:46 +00:00
|
|
|
messengerSignalsHandler protocol.MessengerSignalsHandler,
|
2020-07-15 12:25:01 +00:00
|
|
|
) ([]protocol.Option, error) {
|
2020-01-20 20:56:06 +00:00
|
|
|
options := []protocol.Option{
|
|
|
|
protocol.WithCustomLogger(logger),
|
2020-07-29 06:59:16 +00:00
|
|
|
protocol.WithPushNotifications(),
|
2020-01-20 20:56:06 +00:00
|
|
|
protocol.WithDatabase(db),
|
2020-11-24 13:13:46 +00:00
|
|
|
protocol.WithMultiAccounts(multiAccounts),
|
2021-01-14 22:15:13 +00:00
|
|
|
protocol.WithMailserversDatabase(mailserversDB.NewDB(db)),
|
2020-12-09 14:03:43 +00:00
|
|
|
protocol.WithAccount(account),
|
2020-01-20 20:56:06 +00:00
|
|
|
protocol.WithEnvelopesMonitorConfig(envelopesMonitorConfig),
|
2021-04-19 12:09:46 +00:00
|
|
|
protocol.WithSignalsHandler(messengerSignalsHandler),
|
2021-01-11 10:32:51 +00:00
|
|
|
protocol.WithENSVerificationConfig(publishMessengerResponse, config.VerifyENSURL, config.VerifyENSContractAddress),
|
|
|
|
}
|
2020-01-20 20:56:06 +00:00
|
|
|
|
|
|
|
if config.DataSyncEnabled {
|
|
|
|
options = append(options, protocol.WithDatasync())
|
|
|
|
}
|
2021-01-11 10:32:51 +00:00
|
|
|
|
2020-07-15 12:25:01 +00:00
|
|
|
settings, err := accountsDB.GetSettings()
|
2020-07-22 07:41:40 +00:00
|
|
|
if err != sql.ErrNoRows && err != nil {
|
2020-07-15 12:25:01 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-01-20 20:56:06 +00:00
|
|
|
|
2021-09-01 12:02:18 +00:00
|
|
|
// Generate anon metrics client config
|
|
|
|
if settings.AnonMetricsShouldSend {
|
|
|
|
keyBytes, err := hex.DecodeString(config.AnonMetricsSendID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
key, err := crypto.UnmarshalPubkey(keyBytes)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
amcc := &anonmetrics.ClientConfig{
|
|
|
|
ShouldSend: true,
|
|
|
|
SendAddress: key,
|
|
|
|
}
|
|
|
|
options = append(options, protocol.WithAnonMetricsClientConfig(amcc))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Generate anon metrics server config
|
|
|
|
if config.AnonMetricsServerEnabled {
|
|
|
|
if len(config.AnonMetricsServerPostgresURI) == 0 {
|
|
|
|
return nil, errors.New("AnonMetricsServerPostgresURI must be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
amsc := &anonmetrics.ServerConfig{
|
|
|
|
Enabled: true,
|
|
|
|
PostgresURI: config.AnonMetricsServerPostgresURI,
|
|
|
|
}
|
|
|
|
options = append(options, protocol.WithAnonMetricsServerConfig(amsc))
|
|
|
|
}
|
|
|
|
|
2021-11-03 12:38:37 +00:00
|
|
|
if settings.TelemetryServerURL != "" {
|
|
|
|
options = append(options, protocol.WithTelemetry(settings.TelemetryServerURL))
|
|
|
|
}
|
|
|
|
|
2020-07-17 11:41:49 +00:00
|
|
|
if settings.PushNotificationsServerEnabled {
|
2020-07-22 07:41:40 +00:00
|
|
|
config := &pushnotificationserver.Config{
|
2020-08-20 07:26:00 +00:00
|
|
|
Enabled: true,
|
|
|
|
Logger: logger,
|
2020-07-03 10:08:47 +00:00
|
|
|
}
|
|
|
|
options = append(options, protocol.WithPushNotificationServerConfig(config))
|
|
|
|
}
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
options = append(options, protocol.WithPushNotificationClientConfig(&pushnotificationclient.Config{
|
2020-08-20 09:05:39 +00:00
|
|
|
DefaultServers: config.DefaultPushNotificationsServers,
|
2020-09-03 07:30:03 +00:00
|
|
|
BlockMentions: settings.PushNotificationsBlockMentions,
|
2020-07-15 12:25:01 +00:00
|
|
|
SendEnabled: settings.SendPushNotifications,
|
2020-07-22 07:41:40 +00:00
|
|
|
AllowFromContactsOnly: settings.PushNotificationsFromContactsOnly,
|
2020-07-15 12:25:01 +00:00
|
|
|
RemoteNotificationsEnabled: settings.RemotePushNotificationsEnabled,
|
|
|
|
}))
|
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
if config.VerifyTransactionURL != "" {
|
|
|
|
client := &verifyTransactionClient{
|
|
|
|
url: config.VerifyTransactionURL,
|
|
|
|
chainID: big.NewInt(config.VerifyTransactionChainID),
|
|
|
|
}
|
|
|
|
options = append(options, protocol.WithVerifyTransactionClient(client))
|
|
|
|
}
|
|
|
|
|
2020-07-15 12:25:01 +00:00
|
|
|
return options, nil
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
2021-05-14 10:55:42 +00:00
|
|
|
|
|
|
|
func (s *Service) ConnectionChanged(state connection.State) {
|
|
|
|
if s.messenger != nil {
|
|
|
|
s.messenger.ConnectionChanged(state)
|
|
|
|
}
|
|
|
|
}
|
2021-08-05 13:27:47 +00:00
|
|
|
|
|
|
|
func (s *Service) Messenger() *protocol.Messenger {
|
|
|
|
return s.messenger
|
|
|
|
}
|