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"
|
2024-08-30 19:28:03 +00:00
|
|
|
"github.com/ethereum/go-ethereum/event"
|
2020-01-20 20:56:06 +00:00
|
|
|
"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"
|
2024-09-26 22:37:32 +00:00
|
|
|
gocommon "github.com/status-im/status-go/common"
|
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"
|
2023-09-21 12:40:58 +00:00
|
|
|
"github.com/status-im/status-go/images"
|
2024-11-06 17:36:49 +00:00
|
|
|
"github.com/status-im/status-go/internal/version"
|
2024-10-28 20:54:17 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
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"
|
2023-09-21 12:40:58 +00:00
|
|
|
"github.com/status-im/status-go/protocol/communities"
|
|
|
|
"github.com/status-im/status-go/protocol/communities/token"
|
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
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"
|
2023-08-25 15:36:39 +00:00
|
|
|
"github.com/status-im/status-go/services/communitytokens"
|
2020-11-25 11:39:01 +00:00
|
|
|
"github.com/status-im/status-go/services/ext/mailservers"
|
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"
|
2024-04-10 09:21:41 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/collectibles"
|
2023-05-19 15:31:45 +00:00
|
|
|
w_common "github.com/status-im/status-go/services/wallet/common"
|
2023-03-21 13:52:14 +00:00
|
|
|
"github.com/status-im/status-go/services/wallet/thirdparty"
|
2023-10-30 14:51:57 +00:00
|
|
|
"github.com/status-im/status-go/wakuv2"
|
2020-01-20 20:56:06 +00:00
|
|
|
)
|
|
|
|
|
2023-09-21 12:40:58 +00:00
|
|
|
const infinityString = "∞"
|
2023-12-13 12:19:25 +00:00
|
|
|
const providerID = "community"
|
2023-09-21 12:40:58 +00:00
|
|
|
|
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
|
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)
|
|
|
|
}
|
|
|
|
|
2024-08-30 19:28:03 +00:00
|
|
|
func (s *Service) InitProtocol(nodeName string, identity *ecdsa.PrivateKey, appDb, walletDb *sql.DB, httpServer *server.MediaServer, multiAccountDb *multiaccounts.Database, acc *multiaccounts.Account, accountManager *account.GethManager, rpcClient *rpc.Client, walletService *wallet.Service, communityTokensService *communitytokens.Service, wakuService *wakuv2.Waku, logger *zap.Logger, accountsFeed *event.Feed) 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
|
|
|
|
|
2024-06-05 13:03:34 +00:00
|
|
|
// This directory should have already been created in loadNodeConfig, keeping this to ensure.
|
|
|
|
dataDir := filepath.Clean(s.config.RootDataDir)
|
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
|
|
|
}
|
2023-08-11 11:25:14 +00:00
|
|
|
s.accountsDB, err = accounts.NewDB(appDb)
|
2022-03-23 18:47:00 +00:00
|
|
|
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
|
|
|
|
2024-08-30 19:28:03 +00:00
|
|
|
options, err := buildMessengerOptions(s.config, identity, appDb, walletDb, httpServer, s.rpcClient, s.multiAccountsDB, acc, envelopesMonitorConfig, s.accountsDB, walletService, communityTokensService, wakuService, logger, &MessengerSignalsHandler{}, accountManager, accountsFeed)
|
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,
|
2024-11-06 17:36:49 +00:00
|
|
|
version.Version(),
|
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)
|
2023-07-20 10:08:57 +00:00
|
|
|
if s.config.ProcessBackedupMessages {
|
|
|
|
s.messenger.EnableBackedupMessagesProcessing()
|
|
|
|
}
|
2024-06-10 15:02:42 +00:00
|
|
|
|
|
|
|
// Be mindful of adding more initialization code, as it can easily
|
|
|
|
// impact login times for mobile users. For example, we avoid calling
|
|
|
|
// messenger.InitFilters here.
|
|
|
|
return s.messenger.InitInstallations()
|
2020-01-29 19:40:06 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2023-11-25 23:24:20 +00:00
|
|
|
s.messenger.StartRetrieveMessagesLoop(time.Second, s.cancelMessenger)
|
2020-01-20 20:56:06 +00:00
|
|
|
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-08-03 19:27:15 +00:00
|
|
|
func (s *Service) retrieveStats(tick time.Duration, cancel <-chan struct{}) {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2021-08-03 19:27:15 +00:00
|
|
|
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 {
|
2023-05-19 15:31:45 +00:00
|
|
|
if w_common.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{}) {
|
2024-09-26 22:37:32 +00:00
|
|
|
defer gocommon.LogOnPanic()
|
2022-01-12 16:02:01 +00:00
|
|
|
if s.config.ShhextConfig.VerifyTransactionURL == "" {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Warn("not starting transaction loop")
|
2020-01-20 20:56:06 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ticker := time.NewTicker(tick)
|
|
|
|
defer ticker.Stop()
|
|
|
|
|
|
|
|
ctx, cancelVerifyTransaction := context.WithCancel(context.Background())
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-ticker.C:
|
2023-07-25 15:17:17 +00:00
|
|
|
accounts, err := s.accountsDB.GetActiveAccounts()
|
2020-01-20 20:56:06 +00:00
|
|
|
if err != nil {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("failed to retrieve accounts", zap.Error(err))
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
|
|
|
var wallets []types.Address
|
|
|
|
for _, account := range accounts {
|
2023-11-17 11:55:10 +00:00
|
|
|
if account.IsWalletNonWatchOnlyAccount() {
|
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 {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("failed to validate transactions", zap.Error(err))
|
2020-01-20 20:56:06 +00:00
|
|
|
continue
|
|
|
|
}
|
2023-11-25 23:24:20 +00:00
|
|
|
s.messenger.PublishMessengerResponse(response)
|
2021-01-11 10:32:51 +00:00
|
|
|
|
2020-01-20 20:56:06 +00:00
|
|
|
case <-cancel:
|
|
|
|
cancelVerifyTransaction()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) EnableInstallation(installationID string) error {
|
2024-10-07 14:05:37 +00:00
|
|
|
_, err := s.messenger.EnableInstallation(installationID)
|
|
|
|
return err
|
2020-01-20 20:56:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Info("Stopping shhext service")
|
2020-01-20 20:56:06 +00:00
|
|
|
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 {
|
2024-10-28 20:54:17 +00:00
|
|
|
logutils.ZapLogger().Error("failed to stop messenger", zap.Error(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,
|
2023-08-11 11:25:14 +00:00
|
|
|
appDb *sql.DB,
|
|
|
|
walletDb *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,
|
2023-08-25 15:36:39 +00:00
|
|
|
communityTokensService *communitytokens.Service,
|
2023-10-30 14:51:57 +00:00
|
|
|
wakuService *wakuv2.Waku,
|
2020-01-20 20:56:06 +00:00
|
|
|
logger *zap.Logger,
|
2021-04-19 12:09:46 +00:00
|
|
|
messengerSignalsHandler protocol.MessengerSignalsHandler,
|
2024-01-19 15:58:51 +00:00
|
|
|
accountManager account.Manager,
|
2024-08-30 19:28:03 +00:00
|
|
|
accountsFeed *event.Feed,
|
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(),
|
2023-08-11 11:25:14 +00:00
|
|
|
protocol.WithDatabase(appDb),
|
|
|
|
protocol.WithWalletDatabase(walletDb),
|
2020-11-24 13:13:46 +00:00
|
|
|
protocol.WithMultiAccounts(multiAccounts),
|
2023-08-11 11:25:14 +00:00
|
|
|
protocol.WithMailserversDatabase(mailserversDB.NewDB(appDb)),
|
2020-12-09 14:03:43 +00:00
|
|
|
protocol.WithAccount(account),
|
2023-08-11 11:25:14 +00:00
|
|
|
protocol.WithBrowserDatabase(browsers.NewDB(appDb)),
|
2020-01-20 20:56:06 +00:00
|
|
|
protocol.WithEnvelopesMonitorConfig(envelopesMonitorConfig),
|
2021-04-19 12:09:46 +00:00
|
|
|
protocol.WithSignalsHandler(messengerSignalsHandler),
|
2023-11-25 23:24:20 +00:00
|
|
|
protocol.WithENSVerificationConfig(config.ShhextConfig.VerifyENSURL, config.ShhextConfig.VerifyENSContractAddress),
|
2022-01-12 16:02:01 +00:00
|
|
|
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),
|
2023-08-25 15:36:39 +00:00
|
|
|
protocol.WithCommunityTokensService(communityTokensService),
|
2023-10-30 14:51:57 +00:00
|
|
|
protocol.WithWakuService(wakuService),
|
2024-01-19 15:58:51 +00:00
|
|
|
protocol.WithAccountManager(accountManager),
|
2024-08-30 19:28:03 +00:00
|
|
|
protocol.WithAccountsFeed(accountsFeed),
|
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 != "" {
|
2024-06-28 10:24:04 +00:00
|
|
|
options = append(options, protocol.WithTelemetry(settings.TelemetryServerURL, time.Duration(settings.TelemetrySendPeriodMs)*time.Millisecond))
|
2021-11-03 12:38:37 +00:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2023-10-26 06:30:18 +00:00
|
|
|
func (s *Service) GetCommunityID(tokenURI string) string {
|
|
|
|
if tokenURI != "" {
|
|
|
|
return tokenURIToCommunityID(tokenURI)
|
|
|
|
}
|
|
|
|
return ""
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 09:21:41 +00:00
|
|
|
func (s *Service) FillCollectiblesMetadata(communityID string, cs []*thirdparty.FullCollectibleData) (bool, error) {
|
|
|
|
if s.messenger == nil {
|
|
|
|
return false, fmt.Errorf("messenger not ready")
|
|
|
|
}
|
|
|
|
|
|
|
|
community, err := s.fetchCommunityInfoForCollectibles(communityID, collectibles.IDsFromAssets(cs))
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
if community == nil {
|
|
|
|
return false, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, collectible := range cs {
|
|
|
|
err := s.FillCollectibleMetadata(community, collectible)
|
|
|
|
if err != nil {
|
|
|
|
return true, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) FillCollectibleMetadata(community *communities.Community, collectible *thirdparty.FullCollectibleData) error {
|
2023-10-12 19:21:49 +00:00
|
|
|
if s.messenger == nil {
|
2023-10-26 06:30:18 +00:00
|
|
|
return fmt.Errorf("messenger not ready")
|
|
|
|
}
|
|
|
|
|
|
|
|
if collectible == nil {
|
|
|
|
return fmt.Errorf("empty collectible")
|
2023-10-12 19:21:49 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 06:30:18 +00:00
|
|
|
id := collectible.CollectibleData.ID
|
|
|
|
communityID := collectible.CollectibleData.CommunityID
|
2023-03-21 13:52:14 +00:00
|
|
|
|
|
|
|
if communityID == "" {
|
2023-10-26 06:30:18 +00:00
|
|
|
return fmt.Errorf("invalid communityID")
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2023-09-21 12:40:58 +00:00
|
|
|
tokenMetadata, err := s.fetchCommunityCollectibleMetadata(community, id.ContractID)
|
|
|
|
|
|
|
|
if err != nil {
|
2023-10-26 06:30:18 +00:00
|
|
|
return err
|
2023-09-21 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if tokenMetadata == nil {
|
2023-10-26 06:30:18 +00:00
|
|
|
return nil
|
2023-09-21 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 06:30:18 +00:00
|
|
|
communityToken, err := s.fetchCommunityToken(communityID, id.ContractID)
|
2023-09-21 12:40:58 +00:00
|
|
|
if err != nil {
|
2023-10-26 06:30:18 +00:00
|
|
|
return err
|
2023-09-21 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
2023-10-26 06:30:18 +00:00
|
|
|
permission := fetchCommunityCollectiblePermission(community, id)
|
|
|
|
|
|
|
|
privilegesLevel := token.CommunityLevel
|
|
|
|
if permission != nil {
|
|
|
|
privilegesLevel = permissionTypeToPrivilegesLevel(permission.GetType())
|
|
|
|
}
|
|
|
|
|
2023-12-15 20:29:39 +00:00
|
|
|
imagePayload, _ := images.GetPayloadFromURI(tokenMetadata.GetImage())
|
|
|
|
|
2024-02-06 15:27:15 +00:00
|
|
|
collectible.CollectibleData.ContractType = w_common.ContractTypeERC721
|
2023-12-13 12:19:25 +00:00
|
|
|
collectible.CollectibleData.Provider = providerID
|
2023-10-26 06:30:18 +00:00
|
|
|
collectible.CollectibleData.Name = tokenMetadata.GetName()
|
|
|
|
collectible.CollectibleData.Description = tokenMetadata.GetDescription()
|
2023-12-15 20:29:39 +00:00
|
|
|
collectible.CollectibleData.ImagePayload = imagePayload
|
2023-10-26 06:30:18 +00:00
|
|
|
collectible.CollectibleData.Traits = getCollectibleCommunityTraits(communityToken)
|
2024-04-09 14:16:20 +00:00
|
|
|
collectible.CollectibleData.Soulbound = !communityToken.Transferable
|
2023-10-26 06:30:18 +00:00
|
|
|
|
|
|
|
if collectible.CollectionData == nil {
|
|
|
|
collectible.CollectionData = &thirdparty.CollectionData{
|
2023-09-21 12:40:58 +00:00
|
|
|
ID: id.ContractID,
|
|
|
|
CommunityID: communityID,
|
2023-10-26 06:30:18 +00:00
|
|
|
}
|
|
|
|
}
|
2024-02-06 15:27:15 +00:00
|
|
|
collectible.CollectionData.ContractType = w_common.ContractTypeERC721
|
2023-12-13 12:19:25 +00:00
|
|
|
collectible.CollectionData.Provider = providerID
|
2023-10-26 06:30:18 +00:00
|
|
|
collectible.CollectionData.Name = tokenMetadata.GetName()
|
2023-12-15 20:29:39 +00:00
|
|
|
collectible.CollectionData.ImagePayload = imagePayload
|
2023-10-26 06:30:18 +00:00
|
|
|
|
2023-12-13 12:19:25 +00:00
|
|
|
collectible.CommunityInfo = communityToInfo(community)
|
|
|
|
|
|
|
|
collectible.CollectibleCommunityInfo = &thirdparty.CollectibleCommunityInfo{
|
2023-10-26 06:30:18 +00:00
|
|
|
PrivilegesLevel: privilegesLevel,
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-09-21 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func permissionTypeToPrivilegesLevel(permissionType protobuf.CommunityTokenPermission_Type) token.PrivilegesLevel {
|
|
|
|
switch permissionType {
|
|
|
|
case protobuf.CommunityTokenPermission_BECOME_TOKEN_OWNER:
|
|
|
|
return token.OwnerLevel
|
|
|
|
case protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER:
|
|
|
|
return token.MasterLevel
|
|
|
|
default:
|
|
|
|
return token.CommunityLevel
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-13 12:19:25 +00:00
|
|
|
func communityToInfo(community *communities.Community) *thirdparty.CommunityInfo {
|
2023-09-21 12:40:58 +00:00
|
|
|
if community == nil {
|
2023-12-13 12:19:25 +00:00
|
|
|
return nil
|
2023-09-21 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 12:19:25 +00:00
|
|
|
return &thirdparty.CommunityInfo{
|
2023-12-15 20:29:39 +00:00
|
|
|
CommunityName: community.Name(),
|
|
|
|
CommunityColor: community.Color(),
|
|
|
|
CommunityImagePayload: fetchCommunityImage(community),
|
2023-09-21 12:40:58 +00:00
|
|
|
}
|
2023-12-13 12:19:25 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 09:21:41 +00:00
|
|
|
func (s *Service) fetchCommunityFromStoreNodes(communityID string) (*communities.Community, error) {
|
2024-04-08 16:48:13 +00:00
|
|
|
community, err := s.messenger.FetchCommunity(&protocol.FetchCommunityRequest{
|
|
|
|
CommunityKey: communityID,
|
|
|
|
TryDatabase: false,
|
|
|
|
WaitForResponse: true,
|
|
|
|
})
|
2023-12-13 12:19:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-10 09:21:41 +00:00
|
|
|
return community, nil
|
2024-04-08 16:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch latest community from store nodes.
|
|
|
|
func (s *Service) FetchCommunityInfo(communityID string) (*thirdparty.CommunityInfo, error) {
|
|
|
|
if s.messenger == nil {
|
|
|
|
return nil, fmt.Errorf("messenger not ready")
|
|
|
|
}
|
|
|
|
|
|
|
|
community, err := s.messenger.FindCommunityInfoFromDB(communityID)
|
|
|
|
if err != nil && err != communities.ErrOrgNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fetch latest version from store nodes
|
|
|
|
if community == nil || !community.IsControlNode() {
|
2024-04-10 09:21:41 +00:00
|
|
|
community, err = s.fetchCommunityFromStoreNodes(communityID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-08 16:48:13 +00:00
|
|
|
}
|
2023-09-21 12:40:58 +00:00
|
|
|
|
2023-12-13 12:19:25 +00:00
|
|
|
return communityToInfo(community), nil
|
2023-09-21 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
2024-04-08 16:48:13 +00:00
|
|
|
// Fetch latest community from store nodes only if any collectibles data is missing.
|
2024-04-10 09:21:41 +00:00
|
|
|
func (s *Service) fetchCommunityInfoForCollectibles(communityID string, ids []thirdparty.CollectibleUniqueID) (*communities.Community, error) {
|
2024-04-08 16:48:13 +00:00
|
|
|
community, err := s.messenger.FindCommunityInfoFromDB(communityID)
|
|
|
|
if err != nil && err != communities.ErrOrgNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
2023-10-12 19:21:49 +00:00
|
|
|
|
2024-04-08 16:48:13 +00:00
|
|
|
if community == nil {
|
|
|
|
return s.fetchCommunityFromStoreNodes(communityID)
|
|
|
|
}
|
2023-10-12 19:21:49 +00:00
|
|
|
|
2024-04-08 16:48:13 +00:00
|
|
|
if community.IsControlNode() {
|
2024-04-10 09:21:41 +00:00
|
|
|
return community, nil
|
2024-04-08 16:48:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
contractIDs := func() map[string]thirdparty.ContractID {
|
|
|
|
result := map[string]thirdparty.ContractID{}
|
|
|
|
for _, id := range ids {
|
|
|
|
result[id.HashKey()] = id.ContractID
|
|
|
|
}
|
|
|
|
return result
|
|
|
|
}()
|
|
|
|
|
|
|
|
hasAllMetadata := true
|
|
|
|
for _, contractID := range contractIDs {
|
|
|
|
tokenMetadata, err := s.fetchCommunityCollectibleMetadata(community, contractID)
|
2024-01-08 20:14:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2024-04-08 16:48:13 +00:00
|
|
|
if tokenMetadata == nil {
|
|
|
|
hasAllMetadata = false
|
|
|
|
break
|
|
|
|
}
|
2024-01-08 20:14:37 +00:00
|
|
|
}
|
2023-03-21 13:52:14 +00:00
|
|
|
|
2024-04-08 16:48:13 +00:00
|
|
|
if !hasAllMetadata {
|
|
|
|
return s.fetchCommunityFromStoreNodes(communityID)
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 09:21:41 +00:00
|
|
|
return community, nil
|
2023-09-21 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) fetchCommunityToken(communityID string, contractID thirdparty.ContractID) (*token.CommunityToken, error) {
|
|
|
|
if s.messenger == nil {
|
|
|
|
return nil, fmt.Errorf("messenger not ready")
|
|
|
|
}
|
|
|
|
|
|
|
|
return s.messenger.GetCommunityToken(communityID, int(contractID.ChainID), contractID.Address.String())
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *Service) fetchCommunityCollectibleMetadata(community *communities.Community, contractID thirdparty.ContractID) (*protobuf.CommunityTokenMetadata, error) {
|
|
|
|
tokensMetadata := community.CommunityTokensMetadata()
|
|
|
|
|
|
|
|
for _, tokenMetadata := range tokensMetadata {
|
|
|
|
contractAddresses := tokenMetadata.GetContractAddresses()
|
|
|
|
if contractAddresses[uint64(contractID.ChainID)] == contractID.Address.Hex() {
|
|
|
|
return tokenMetadata, nil
|
2023-03-21 13:52:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, nil
|
|
|
|
}
|
2023-09-21 12:40:58 +00:00
|
|
|
|
|
|
|
func tokenCriterionContainsCollectible(tokenCriterion *protobuf.TokenCriteria, id thirdparty.CollectibleUniqueID) bool {
|
|
|
|
// Check if token type matches
|
|
|
|
if tokenCriterion.Type != protobuf.CommunityTokenType_ERC721 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
for chainID, contractAddressStr := range tokenCriterion.ContractAddresses {
|
|
|
|
if chainID != uint64(id.ContractID.ChainID) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
contractAddress := commongethtypes.HexToAddress(contractAddressStr)
|
|
|
|
if contractAddress != id.ContractID.Address {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(tokenCriterion.TokenIds) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tokenID := range tokenCriterion.TokenIds {
|
|
|
|
tokenIDBigInt := new(big.Int).SetUint64(tokenID)
|
|
|
|
if id.TokenID.Cmp(tokenIDBigInt) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func permissionContainsCollectible(permission *communities.CommunityTokenPermission, id thirdparty.CollectibleUniqueID) bool {
|
|
|
|
// See if any token criterion contains the collectible we're looking for
|
|
|
|
for _, tokenCriterion := range permission.TokenCriteria {
|
|
|
|
if tokenCriterionContainsCollectible(tokenCriterion, id) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchCommunityCollectiblePermission(community *communities.Community, id thirdparty.CollectibleUniqueID) *communities.CommunityTokenPermission {
|
|
|
|
// Permnission types of interest
|
|
|
|
permissionTypes := []protobuf.CommunityTokenPermission_Type{
|
|
|
|
protobuf.CommunityTokenPermission_BECOME_TOKEN_OWNER,
|
|
|
|
protobuf.CommunityTokenPermission_BECOME_TOKEN_MASTER,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, permissionType := range permissionTypes {
|
|
|
|
permissions := community.TokenPermissionsByType(permissionType)
|
|
|
|
// See if any community permission matches the type we're looking for
|
|
|
|
for _, permission := range permissions {
|
|
|
|
if permissionContainsCollectible(permission, id) {
|
|
|
|
return permission
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-12-15 20:29:39 +00:00
|
|
|
func fetchCommunityImage(community *communities.Community) []byte {
|
2023-09-21 12:40:58 +00:00
|
|
|
imageTypes := []string{
|
|
|
|
images.LargeDimName,
|
|
|
|
images.SmallDimName,
|
|
|
|
}
|
|
|
|
|
|
|
|
communityImages := community.Images()
|
|
|
|
|
|
|
|
for _, imageType := range imageTypes {
|
|
|
|
if pbImage, ok := communityImages[imageType]; ok {
|
2023-12-15 20:29:39 +00:00
|
|
|
return pbImage.Payload
|
2023-09-21 12:40:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-15 20:29:39 +00:00
|
|
|
return nil
|
2023-09-21 12:40:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func boolToString(value bool) string {
|
|
|
|
if value {
|
|
|
|
return "Yes"
|
|
|
|
}
|
|
|
|
return "No"
|
|
|
|
}
|
|
|
|
|
|
|
|
func getCollectibleCommunityTraits(token *token.CommunityToken) []thirdparty.CollectibleTrait {
|
|
|
|
if token == nil {
|
|
|
|
return make([]thirdparty.CollectibleTrait, 0)
|
|
|
|
}
|
|
|
|
|
|
|
|
totalStr := infinityString
|
|
|
|
availableStr := infinityString
|
|
|
|
if !token.InfiniteSupply {
|
|
|
|
totalStr = token.Supply.String()
|
|
|
|
// TODO: calculate available supply. See services/communitytokens/api.go
|
|
|
|
availableStr = totalStr
|
|
|
|
}
|
|
|
|
|
|
|
|
transferableStr := boolToString(token.Transferable)
|
|
|
|
|
|
|
|
destructibleStr := boolToString(token.RemoteSelfDestruct)
|
|
|
|
|
|
|
|
return []thirdparty.CollectibleTrait{
|
|
|
|
{
|
|
|
|
TraitType: "Symbol",
|
|
|
|
Value: token.Symbol,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TraitType: "Total",
|
|
|
|
Value: totalStr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TraitType: "Available",
|
|
|
|
Value: availableStr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TraitType: "Transferable",
|
|
|
|
Value: transferableStr,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
TraitType: "Destructible",
|
|
|
|
Value: destructibleStr,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|