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"
|
2023-03-21 13:52:14 +00:00
|
|
|
"fmt"
|
2020-01-20 20:56:06 +00:00
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2023-03-21 13:52:14 +00:00
|
|
|
"strings"
|
2020-01-20 20:56:06 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/syndtr/goleveldb/leveldb"
|
2022-06-15 14:49:31 +00:00
|
|
|
"go.uber.org/zap"
|
2020-01-20 20:56:06 +00:00
|
|
|
|
|
|
|
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"
|
2022-06-02 12:17:52 +00:00
|
|
|
gethrpc "github.com/ethereum/go-ethereum/rpc"
|
2020-01-20 20:56:06 +00:00
|
|
|
|
2023-03-10 12:53:40 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2023-03-21 13:52:14 +00:00
|
|
|
"github.com/status-im/status-go/api/multiformat"
|
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"
|
2023-03-21 13:52:14 +00:00
|
|
|
"github.com/status-im/status-go/protocol/common"
|
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"
|
2022-06-15 14:49:31 +00:00
|
|
|
"github.com/status-im/status-go/rpc"
|
|
|
|
"github.com/status-im/status-go/server"
|
|
|
|
"github.com/status-im/status-go/services/browsers"
|
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"
|
2023-04-25 12:00:17 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet"
|
2023-03-21 13:52:14 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
2021-09-09 14:28:54 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/transfer"
|
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 {
|
2023-03-21 13:52:14 +00:00
|
|
|
thirdparty.NFTMetadataProvider
|
2021-05-13 13:30:33 +00:00
|
|
|
messenger *protocol.Messenger
|
|
|
|
identity *ecdsa.PrivateKey
|
|
|
|
cancelMessenger chan struct{}
|
|
|
|
storage db.TransactionalStorage
|
|
|
|
n types.Node
|
2022-06-02 12:17:52 +00:00
|
|
|
rpcClient *rpc.Client
|
2022-01-12 16:02:01 +00:00
|
|
|
config params.NodeConfig
|
2021-05-13 13:30:33 +00:00
|
|
|
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(
|
2022-01-12 16:02:01 +00:00
|
|
|
config params.NodeConfig,
|
2020-01-20 20:56:06 +00:00
|
|
|
n types.Node,
|
2022-06-02 12:17:52 +00:00
|
|
|
rpcClient *rpc.Client,
|
2020-01-20 20:56:06 +00:00
|
|
|
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,
|
2022-06-02 12:17:52 +00:00
|
|
|
rpcClient: rpcClient,
|
2021-05-13 13:30:33 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2023-04-25 12:00:17 +00:00
|
|
|
func (s *Service) InitProtocol(nodeName string, identity *ecdsa.PrivateKey, db *sql.DB, httpServer *server.MediaServer, multiAccountDb *multiaccounts.Database, acc *multiaccounts.Account, accountManager *account.GethManager, rpcClient *rpc.Client, walletService *wallet.Service, logger *zap.Logger) error {
|
2022-03-23 18:47:00 +00:00
|
|
|
var err error
|
2022-01-12 16:02:01 +00:00
|
|
|
if !s.config.ShhextConfig.PFSEnabled {
|
2020-01-20 20:56:06 +00:00
|
|
|
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
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
dataDir := filepath.Clean(s.config.ShhextConfig.BackupDisabledDataDir)
|
2020-01-20 20:56:06 +00:00
|
|
|
|
|
|
|
if err := os.MkdirAll(dataDir, os.ModePerm); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
envelopesMonitorConfig := &transport.EnvelopesMonitorConfig{
|
2022-01-12 16:02:01 +00:00
|
|
|
MaxAttempts: s.config.ShhextConfig.MaxMessageDeliveryAttempts,
|
|
|
|
AwaitOnlyMailServerConfirmations: s.config.ShhextConfig.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
|
|
|
}
|
2022-03-23 18:47:00 +00:00
|
|
|
s.accountsDB, err = accounts.NewDB(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
2023-04-25 12:00:17 +00:00
|
|
|
options, err := buildMessengerOptions(s.config, identity, db, httpServer, s.rpcClient, s.multiAccountsDB, acc, envelopesMonitorConfig, s.accountsDB, walletService, 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,
|
2022-01-12 16:02:01 +00:00
|
|
|
s.config.ShhextConfig.InstallationID,
|
|
|
|
s.peerStore,
|
2023-03-10 12:53:40 +00:00
|
|
|
accountManager,
|
2020-01-20 20:56:06 +00:00
|
|
|
options...,
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
s.messenger = messenger
|
2022-01-12 16:02:01 +00:00
|
|
|
s.messenger.SetP2PServer(s.server)
|
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) {
|
2022-07-17 13:37:14 +00:00
|
|
|
// Start a loop that retrieves all messages and propagates them to status-mobile.
|
2020-01-20 20:56:06 +00:00
|
|
|
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
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
if s.config.ShhextConfig.BandwidthStatsEnabled {
|
2021-09-29 14:52:45 +00:00
|
|
|
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:
|
2022-01-31 10:33:56 +00:00
|
|
|
// We might be shutting down here
|
|
|
|
if s.messenger == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-01-20 20:56:06 +00:00
|
|
|
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{}) {
|
2022-01-12 16:02:01 +00:00
|
|
|
if s.config.ShhextConfig.VerifyTransactionURL == "" {
|
2020-01-20 20:56:06 +00:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
2022-06-02 12:17:52 +00:00
|
|
|
func (s *Service) APIs() []gethrpc.API {
|
2020-01-20 20:56:06 +00:00
|
|
|
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(
|
2022-01-12 16:02:01 +00:00
|
|
|
config params.NodeConfig,
|
2020-07-03 10:08:47 +00:00
|
|
|
identity *ecdsa.PrivateKey,
|
2020-01-20 20:56:06 +00:00
|
|
|
db *sql.DB,
|
2022-06-15 14:49:31 +00:00
|
|
|
httpServer *server.MediaServer,
|
2022-06-02 12:17:52 +00:00
|
|
|
rpcClient *rpc.Client,
|
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,
|
2023-04-25 12:00:17 +00:00
|
|
|
walletService *wallet.Service,
|
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),
|
2022-01-17 03:42:11 +00:00
|
|
|
protocol.WithBrowserDatabase(browsers.NewDB(db)),
|
2020-01-20 20:56:06 +00:00
|
|
|
protocol.WithEnvelopesMonitorConfig(envelopesMonitorConfig),
|
2021-04-19 12:09:46 +00:00
|
|
|
protocol.WithSignalsHandler(messengerSignalsHandler),
|
2022-01-12 16:02:01 +00:00
|
|
|
protocol.WithENSVerificationConfig(publishMessengerResponse, config.ShhextConfig.VerifyENSURL, config.ShhextConfig.VerifyENSContractAddress),
|
|
|
|
protocol.WithClusterConfig(config.ClusterConfig),
|
2022-03-08 13:17:26 +00:00
|
|
|
protocol.WithTorrentConfig(&config.TorrentConfig),
|
2022-05-09 13:07:57 +00:00
|
|
|
protocol.WithHTTPServer(httpServer),
|
2022-06-02 12:17:52 +00:00
|
|
|
protocol.WithRPCClient(rpcClient),
|
2022-08-24 12:06:48 +00:00
|
|
|
protocol.WithMessageCSV(config.OutputMessageCSVEnabled),
|
2023-03-27 09:35:03 +00:00
|
|
|
protocol.WithWalletConfig(&config.WalletConfig),
|
2023-04-25 12:00:17 +00:00
|
|
|
protocol.WithWalletService(walletService),
|
2021-01-11 10:32:51 +00:00
|
|
|
}
|
2020-01-20 20:56:06 +00:00
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
if config.ShhextConfig.DataSyncEnabled {
|
2020-01-20 20:56:06 +00:00
|
|
|
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 {
|
2022-01-12 16:02:01 +00:00
|
|
|
keyBytes, err := hex.DecodeString(config.ShhextConfig.AnonMetricsSendID)
|
2021-09-01 12:02:18 +00:00
|
|
|
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
|
2022-01-12 16:02:01 +00:00
|
|
|
if config.ShhextConfig.AnonMetricsServerEnabled {
|
|
|
|
if len(config.ShhextConfig.AnonMetricsServerPostgresURI) == 0 {
|
2021-09-01 12:02:18 +00:00
|
|
|
return nil, errors.New("AnonMetricsServerPostgresURI must be set")
|
|
|
|
}
|
|
|
|
|
|
|
|
amsc := &anonmetrics.ServerConfig{
|
|
|
|
Enabled: true,
|
2022-01-12 16:02:01 +00:00
|
|
|
PostgresURI: config.ShhextConfig.AnonMetricsServerPostgresURI,
|
2021-09-01 12:02:18 +00:00
|
|
|
}
|
|
|
|
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))
|
|
|
|
}
|
|
|
|
|
2022-04-25 11:27:40 +00:00
|
|
|
var pushNotifServKey []*ecdsa.PublicKey
|
|
|
|
for _, d := range config.ShhextConfig.DefaultPushNotificationsServers {
|
|
|
|
pushNotifServKey = append(pushNotifServKey, d.PublicKey)
|
|
|
|
}
|
|
|
|
|
2020-07-22 07:41:40 +00:00
|
|
|
options = append(options, protocol.WithPushNotificationClientConfig(&pushnotificationclient.Config{
|
2022-04-25 11:27:40 +00:00
|
|
|
DefaultServers: pushNotifServKey,
|
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,
|
|
|
|
}))
|
|
|
|
|
2022-01-12 16:02:01 +00:00
|
|
|
if config.ShhextConfig.VerifyTransactionURL != "" {
|
2020-01-20 20:56:06 +00:00
|
|
|
client := &verifyTransactionClient{
|
2022-01-12 16:02:01 +00:00
|
|
|
url: config.ShhextConfig.VerifyTransactionURL,
|
|
|
|
chainID: big.NewInt(config.ShhextConfig.VerifyTransactionChainID),
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
2023-03-21 13:52:14 +00:00
|
|
|
|
|
|
|
func tokenURIToCommunityID(tokenURI string) string {
|
|
|
|
tmpStr := strings.Split(tokenURI, "/")
|
|
|
|
|
|
|
|
// Community NFTs have a tokenURI of the form "compressedCommunityID/tokenID"
|
|
|
|
if len(tmpStr) != 2 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
compressedCommunityID := tmpStr[0]
|
|
|
|
|
|
|
|
hexCommunityID, err := multiformat.DeserializeCompressedKey(compressedCommunityID)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
pubKey, err := common.HexToPubkey(hexCommunityID)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
communityID := types.EncodeHex(crypto.CompressPubkey(pubKey))
|
|
|
|
|
|
|
|
return communityID
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) CanProvideNFTMetadata(chainID uint64, id thirdparty.NFTUniqueID, tokenURI string) (bool, error) {
|
|
|
|
ret := tokenURI != "" && tokenURIToCommunityID(tokenURI) != ""
|
|
|
|
return ret, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) FetchNFTMetadata(chainID uint64, id thirdparty.NFTUniqueID, tokenURI string) (*thirdparty.NFTMetadata, error) {
|
|
|
|
if s.messenger == nil {
|
|
|
|
return nil, fmt.Errorf("messenger not ready")
|
|
|
|
}
|
|
|
|
|
|
|
|
communityID := tokenURIToCommunityID(tokenURI)
|
|
|
|
|
|
|
|
if communityID == "" {
|
|
|
|
return nil, fmt.Errorf("invalid tokenURI")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Try to fetch metadata from Messenger communities
|
|
|
|
community, err := s.messenger.RequestCommunityInfoFromMailserver(communityID, true)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if community != nil {
|
|
|
|
tokensMetadata := community.CommunityTokensMetadata()
|
|
|
|
|
|
|
|
for _, tokenMetadata := range tokensMetadata {
|
|
|
|
contractAddresses := tokenMetadata.GetContractAddresses()
|
|
|
|
if contractAddresses[chainID] == id.ContractAddress.Hex() {
|
|
|
|
return &thirdparty.NFTMetadata{
|
2023-03-29 16:36:23 +00:00
|
|
|
Name: tokenMetadata.GetName(),
|
|
|
|
Description: tokenMetadata.GetDescription(),
|
|
|
|
CollectionImageURL: tokenMetadata.GetImage(),
|
|
|
|
ImageURL: tokenMetadata.GetImage(),
|
2023-03-21 13:52:14 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|