2023-06-27 12:20:20 +00:00
|
|
|
package protocol
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2023-08-08 15:02:56 +00:00
|
|
|
"sync"
|
|
|
|
"time"
|
2023-06-27 12:20:20 +00:00
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
2023-08-08 15:02:56 +00:00
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
|
|
|
hexutil "github.com/ethereum/go-ethereum/common/hexutil"
|
|
|
|
|
2023-06-27 12:20:20 +00:00
|
|
|
"github.com/status-im/status-go/account"
|
2023-08-08 15:02:56 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/crypto"
|
2023-06-27 12:20:20 +00:00
|
|
|
"github.com/status-im/status-go/eth-node/types"
|
2023-08-08 15:02:56 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/accounts"
|
2023-06-27 12:20:20 +00:00
|
|
|
"github.com/status-im/status-go/multiaccounts/settings"
|
|
|
|
"github.com/status-im/status-go/params"
|
|
|
|
"github.com/status-im/status-go/protocol/common"
|
|
|
|
"github.com/status-im/status-go/protocol/communities"
|
2024-01-16 08:08:56 +00:00
|
|
|
"github.com/status-im/status-go/protocol/communities/token"
|
2023-06-27 12:20:20 +00:00
|
|
|
"github.com/status-im/status-go/protocol/protobuf"
|
|
|
|
"github.com/status-im/status-go/protocol/requests"
|
2023-08-22 09:32:27 +00:00
|
|
|
walletToken "github.com/status-im/status-go/services/wallet/token"
|
2023-07-05 17:35:22 +00:00
|
|
|
"github.com/status-im/status-go/transactions"
|
2023-06-27 12:20:20 +00:00
|
|
|
)
|
|
|
|
|
2023-08-08 15:02:56 +00:00
|
|
|
type AccountManagerMock struct {
|
|
|
|
AccountsMap map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AccountManagerMock) GetVerifiedWalletAccount(db *accounts.Database, address, password string) (*account.SelectedExtKey, error) {
|
|
|
|
return &account.SelectedExtKey{
|
|
|
|
Address: types.HexToAddress(address),
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AccountManagerMock) CanRecover(rpcParams account.RecoverParams, revealedAddress types.Address) (bool, error) {
|
|
|
|
return true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AccountManagerMock) Sign(rpcParams account.SignParams, verifiedAccount *account.SelectedExtKey) (result types.HexBytes, err error) {
|
|
|
|
return types.HexBytes{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AccountManagerMock) DeleteAccount(address types.Address) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type TokenManagerMock struct {
|
|
|
|
Balances *map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TokenManagerMock) GetAllChainIDs() ([]uint64, error) {
|
|
|
|
chainIDs := make([]uint64, 0, len(*m.Balances))
|
|
|
|
for key := range *m.Balances {
|
|
|
|
chainIDs = append(chainIDs, key)
|
|
|
|
}
|
|
|
|
return chainIDs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *TokenManagerMock) GetBalancesByChain(ctx context.Context, accounts, tokenAddresses []gethcommon.Address, chainIDs []uint64) (map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big, error) {
|
|
|
|
time.Sleep(100 * time.Millisecond) // simulate response time
|
|
|
|
return *m.Balances, nil
|
|
|
|
}
|
|
|
|
|
2023-09-11 11:45:30 +00:00
|
|
|
func (m *TokenManagerMock) FindOrCreateTokenByAddress(ctx context.Context, chainID uint64, address gethcommon.Address) *walletToken.Token {
|
2023-08-22 09:32:27 +00:00
|
|
|
time.Sleep(100 * time.Millisecond) // simulate response time
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-08-22 17:48:42 +00:00
|
|
|
type CollectiblesServiceMock struct {
|
2024-03-14 08:39:06 +00:00
|
|
|
Collectibles map[uint64]map[string]*communities.CollectibleContractData
|
|
|
|
Assets map[uint64]map[string]*communities.AssetContractData
|
2023-07-05 17:35:22 +00:00
|
|
|
Signers map[string]string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CollectiblesServiceMock) SetSignerPubkeyForCommunity(communityID []byte, signerPubKey string) {
|
|
|
|
if c.Signers == nil {
|
|
|
|
c.Signers = make(map[string]string)
|
|
|
|
}
|
|
|
|
c.Signers[types.EncodeHex(communityID)] = signerPubKey
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *CollectiblesServiceMock) SetSignerPubKey(ctx context.Context, chainID uint64, contractAddress string, txArgs transactions.SendTxArgs, password string, newSignerPubKey string) (string, error) {
|
|
|
|
return "", nil
|
2023-08-22 17:48:42 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 08:39:06 +00:00
|
|
|
func (c *CollectiblesServiceMock) GetCollectibleContractData(chainID uint64, contractAddress string) (*communities.CollectibleContractData, error) {
|
2023-08-22 17:48:42 +00:00
|
|
|
collectibleContractData, dataExists := c.Collectibles[chainID][contractAddress]
|
|
|
|
if dataExists {
|
|
|
|
return collectibleContractData, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-03-14 08:39:06 +00:00
|
|
|
func (c *CollectiblesServiceMock) GetAssetContractData(chainID uint64, contractAddress string) (*communities.AssetContractData, error) {
|
2023-08-22 17:48:42 +00:00
|
|
|
assetsContractData, dataExists := c.Assets[chainID][contractAddress]
|
|
|
|
if dataExists {
|
|
|
|
return assetsContractData, nil
|
|
|
|
}
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2024-03-14 08:39:06 +00:00
|
|
|
func (c *CollectiblesServiceMock) SetMockCollectibleContractData(chainID uint64, contractAddress string, collectible *communities.CollectibleContractData) {
|
2023-08-22 17:48:42 +00:00
|
|
|
if c.Collectibles == nil {
|
2024-03-14 08:39:06 +00:00
|
|
|
c.Collectibles = make(map[uint64]map[string]*communities.CollectibleContractData)
|
2023-08-22 17:48:42 +00:00
|
|
|
}
|
2024-03-20 18:10:02 +00:00
|
|
|
if _, ok := c.Collectibles[chainID]; !ok {
|
2024-03-14 08:39:06 +00:00
|
|
|
c.Collectibles[chainID] = make(map[string]*communities.CollectibleContractData)
|
2024-03-20 18:10:02 +00:00
|
|
|
}
|
2023-08-22 17:48:42 +00:00
|
|
|
c.Collectibles[chainID][contractAddress] = collectible
|
|
|
|
}
|
|
|
|
|
2024-01-16 08:08:56 +00:00
|
|
|
func (c *CollectiblesServiceMock) SetMockCommunityTokenData(token *token.CommunityToken) {
|
|
|
|
if c.Collectibles == nil {
|
2024-03-14 08:39:06 +00:00
|
|
|
c.Collectibles = make(map[uint64]map[string]*communities.CollectibleContractData)
|
2024-01-16 08:08:56 +00:00
|
|
|
}
|
|
|
|
|
2024-03-14 08:39:06 +00:00
|
|
|
data := &communities.CollectibleContractData{
|
2024-01-16 08:08:56 +00:00
|
|
|
TotalSupply: token.Supply,
|
|
|
|
Transferable: token.Transferable,
|
|
|
|
RemoteBurnable: token.RemoteSelfDestruct,
|
|
|
|
InfiniteSupply: token.InfiniteSupply,
|
|
|
|
}
|
|
|
|
|
|
|
|
c.SetMockCollectibleContractData(uint64(token.ChainID), token.Address, data)
|
|
|
|
}
|
|
|
|
|
2023-07-05 17:35:22 +00:00
|
|
|
func (c *CollectiblesServiceMock) SafeGetSignerPubKey(ctx context.Context, chainID uint64, communityID string) (string, error) {
|
|
|
|
if c.Signers == nil {
|
|
|
|
c.Signers = make(map[string]string)
|
|
|
|
}
|
|
|
|
return c.Signers[communityID], nil
|
|
|
|
}
|
|
|
|
|
2024-03-14 08:39:06 +00:00
|
|
|
func (c *CollectiblesServiceMock) SetMockAssetContractData(chainID uint64, contractAddress string, assetData *communities.AssetContractData) {
|
2023-08-22 17:48:42 +00:00
|
|
|
if c.Assets == nil {
|
2024-03-14 08:39:06 +00:00
|
|
|
c.Assets = make(map[uint64]map[string]*communities.AssetContractData)
|
2023-08-22 17:48:42 +00:00
|
|
|
}
|
2024-03-14 08:39:06 +00:00
|
|
|
c.Assets[chainID] = make(map[string]*communities.AssetContractData)
|
2023-08-22 17:48:42 +00:00
|
|
|
c.Assets[chainID][contractAddress] = assetData
|
|
|
|
}
|
|
|
|
|
2023-08-29 13:17:37 +00:00
|
|
|
func (c *CollectiblesServiceMock) DeploymentSignatureDigest(chainID uint64, addressFrom string, communityID string) ([]byte, error) {
|
|
|
|
return gethcommon.Hex2Bytes("ccbb375343347491706cf4b43796f7b96ccc89c9e191a8b78679daeba1684ec7"), nil
|
|
|
|
}
|
|
|
|
|
2024-01-22 14:47:22 +00:00
|
|
|
type testCommunitiesMessengerConfig struct {
|
|
|
|
testMessengerConfig
|
2023-08-08 15:02:56 +00:00
|
|
|
|
2024-01-22 14:47:22 +00:00
|
|
|
nodeConfig *params.NodeConfig
|
|
|
|
appSettings *settings.Settings
|
2023-08-08 15:02:56 +00:00
|
|
|
|
2024-01-22 14:47:22 +00:00
|
|
|
password string
|
|
|
|
walletAddresses []string
|
|
|
|
mockedBalances *map[uint64]map[gethcommon.Address]map[gethcommon.Address]*hexutil.Big
|
2024-03-14 08:39:06 +00:00
|
|
|
collectiblesService communities.CommunityTokensServiceInterface
|
2024-01-22 14:47:22 +00:00
|
|
|
}
|
2023-08-08 15:02:56 +00:00
|
|
|
|
2024-01-22 14:47:22 +00:00
|
|
|
func (tcmc *testCommunitiesMessengerConfig) complete() error {
|
|
|
|
err := tcmc.testMessengerConfig.complete()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2023-08-08 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-22 14:47:22 +00:00
|
|
|
if tcmc.nodeConfig == nil {
|
|
|
|
tcmc.nodeConfig = defaultTestCommunitiesMessengerNodeConfig()
|
2023-08-08 15:02:56 +00:00
|
|
|
}
|
2024-01-22 14:47:22 +00:00
|
|
|
if tcmc.appSettings == nil {
|
|
|
|
tcmc.appSettings = defaultTestCommunitiesMessengerSettings()
|
2023-08-08 15:02:56 +00:00
|
|
|
}
|
2024-01-22 14:47:22 +00:00
|
|
|
|
|
|
|
return nil
|
2023-08-08 15:02:56 +00:00
|
|
|
}
|
|
|
|
|
2024-01-22 14:47:22 +00:00
|
|
|
func defaultTestCommunitiesMessengerNodeConfig() *params.NodeConfig {
|
|
|
|
return ¶ms.NodeConfig{
|
2023-06-27 12:20:20 +00:00
|
|
|
NetworkID: 10,
|
|
|
|
DataDir: "test",
|
|
|
|
}
|
2024-01-22 14:47:22 +00:00
|
|
|
}
|
2023-06-27 12:20:20 +00:00
|
|
|
|
2024-01-22 14:47:22 +00:00
|
|
|
func defaultTestCommunitiesMessengerSettings() *settings.Settings {
|
2023-06-27 12:20:20 +00:00
|
|
|
networks := json.RawMessage("{}")
|
2024-01-22 14:47:22 +00:00
|
|
|
return &settings.Settings{
|
2023-06-27 12:20:20 +00:00
|
|
|
Address: types.HexToAddress("0x1122334455667788990011223344556677889900"),
|
|
|
|
AnonMetricsShouldSend: false,
|
|
|
|
CurrentNetwork: "mainnet_rpc",
|
|
|
|
DappsAddress: types.HexToAddress("0x1122334455667788990011223344556677889900"),
|
|
|
|
InstallationID: "d3efcff6-cffa-560e-a547-21d3858cbc51",
|
|
|
|
KeyUID: "0x1122334455667788990011223344556677889900",
|
|
|
|
Name: "Test",
|
|
|
|
Networks: &networks,
|
|
|
|
LatestDerivedPath: 0,
|
|
|
|
PhotoPath: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADIAAAAyCAIAAACRXR/mAAAAjklEQVR4nOzXwQmFMBAAUZXUYh32ZB32ZB02sxYQQSZGsod55/91WFgSS0RM+SyjA56ZRZhFmEWYRRT6h+M6G16zrxv6fdJpmUWYRbxsYr13dKfanpN0WmYRZhGzXz6AWYRZRIfbaX26fT9Jk07LLMIsosPt9I/dTDotswizCG+nhFmEWYRZhFnEHQAA///z1CFkYamgfQAAAABJRU5ErkJggg==",
|
|
|
|
PreviewPrivacy: false,
|
|
|
|
PublicKey: "0x04112233445566778899001122334455667788990011223344556677889900112233445566778899001122334455667788990011223344556677889900",
|
|
|
|
SigningPhrase: "yurt joey vibe",
|
|
|
|
SendPushNotifications: true,
|
|
|
|
ProfilePicturesVisibility: 1,
|
|
|
|
DefaultSyncPeriod: 777600,
|
|
|
|
UseMailservers: true,
|
|
|
|
LinkPreviewRequestEnabled: true,
|
|
|
|
SendStatusUpdates: true,
|
|
|
|
WalletRootAddress: types.HexToAddress("0x1122334455667788990011223344556677889900")}
|
2024-01-22 14:47:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func newTestCommunitiesMessenger(s *suite.Suite, waku types.Waku, config testCommunitiesMessengerConfig) *Messenger {
|
|
|
|
err := config.complete()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
accountsManagerMock := &AccountManagerMock{}
|
|
|
|
accountsManagerMock.AccountsMap = make(map[string]string)
|
|
|
|
for _, walletAddress := range config.walletAddresses {
|
|
|
|
accountsManagerMock.AccountsMap[walletAddress] = types.EncodeHex(crypto.Keccak256([]byte(config.password)))
|
|
|
|
}
|
|
|
|
|
|
|
|
tokenManagerMock := &TokenManagerMock{
|
|
|
|
Balances: config.mockedBalances,
|
|
|
|
}
|
2023-06-27 12:20:20 +00:00
|
|
|
|
2024-01-19 15:58:51 +00:00
|
|
|
options := []Option{
|
2024-01-22 14:47:22 +00:00
|
|
|
WithAccountManager(accountsManagerMock),
|
|
|
|
WithTokenManager(tokenManagerMock),
|
|
|
|
WithCommunityTokensService(config.collectiblesService),
|
|
|
|
WithAppSettings(*config.appSettings, *config.nodeConfig),
|
2024-01-19 15:58:51 +00:00
|
|
|
}
|
2023-06-27 12:20:20 +00:00
|
|
|
|
2024-01-30 13:43:34 +00:00
|
|
|
config.extraOptions = append(config.extraOptions, options...)
|
|
|
|
|
|
|
|
messenger, err := newTestMessenger(waku, config.testMessengerConfig)
|
2024-01-22 14:47:22 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
currentDistributorObj, ok := messenger.communitiesKeyDistributor.(*CommunitiesKeyDistributorImpl)
|
|
|
|
s.Require().True(ok)
|
|
|
|
messenger.communitiesKeyDistributor = &TestCommunitiesKeyDistributor{
|
|
|
|
CommunitiesKeyDistributorImpl: *currentDistributorObj,
|
|
|
|
subscriptions: map[chan *CommunityAndKeyActions]bool{},
|
|
|
|
mutex: sync.RWMutex{},
|
2023-12-15 16:16:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-22 14:47:22 +00:00
|
|
|
// add wallet account with keypair
|
|
|
|
for _, walletAddress := range config.walletAddresses {
|
|
|
|
kp := accounts.GetProfileKeypairForTest(false, true, false)
|
|
|
|
kp.Accounts[0].Address = types.HexToAddress(walletAddress)
|
|
|
|
err := messenger.settings.SaveOrUpdateKeypair(kp)
|
|
|
|
s.Require().NoError(err)
|
2023-12-15 16:16:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-22 14:47:22 +00:00
|
|
|
walletAccounts, err := messenger.settings.GetActiveAccounts()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(walletAccounts, len(config.walletAddresses))
|
|
|
|
for i := range config.walletAddresses {
|
|
|
|
s.Require().Equal(walletAccounts[i].Type, accounts.AccountTypeGenerated)
|
|
|
|
}
|
|
|
|
return messenger
|
2023-12-15 16:16:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createEncryptedCommunity(s *suite.Suite, owner *Messenger) (*communities.Community, *Chat) {
|
|
|
|
community, chat := createCommunityConfigurable(s, owner, protobuf.CommunityPermissions_AUTO_ACCEPT)
|
|
|
|
// Add community permission
|
|
|
|
_, err := owner.CreateCommunityTokenPermission(&requests.CreateCommunityTokenPermission{
|
|
|
|
CommunityID: community.ID(),
|
|
|
|
Type: protobuf.CommunityTokenPermission_BECOME_MEMBER,
|
|
|
|
TokenCriteria: []*protobuf.TokenCriteria{{
|
|
|
|
ContractAddresses: map[uint64]string{3: "0x933"},
|
|
|
|
Type: protobuf.CommunityTokenType_ERC20,
|
|
|
|
Symbol: "STT",
|
|
|
|
Name: "Status Test Token",
|
2024-02-13 10:23:11 +00:00
|
|
|
AmountInWei: "10000000000000000000",
|
2023-12-15 16:16:18 +00:00
|
|
|
Decimals: 18,
|
|
|
|
}},
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Add channel permission
|
|
|
|
response, err := owner.CreateCommunityTokenPermission(&requests.CreateCommunityTokenPermission{
|
|
|
|
CommunityID: community.ID(),
|
|
|
|
Type: protobuf.CommunityTokenPermission_CAN_VIEW_CHANNEL,
|
|
|
|
TokenCriteria: []*protobuf.TokenCriteria{
|
|
|
|
&protobuf.TokenCriteria{
|
|
|
|
ContractAddresses: map[uint64]string{3: "0x933"},
|
|
|
|
Type: protobuf.CommunityTokenType_ERC20,
|
|
|
|
Symbol: "STT",
|
|
|
|
Name: "Status Test Token",
|
2024-02-13 10:23:11 +00:00
|
|
|
AmountInWei: "10000000000000000000",
|
2023-12-15 16:16:18 +00:00
|
|
|
Decimals: 18,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ChatIds: []string{chat.ID},
|
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().Len(response.Communities(), 1)
|
|
|
|
community = response.Communities()[0]
|
|
|
|
s.Require().True(community.Encrypted())
|
|
|
|
s.Require().True(community.ChannelEncrypted(chat.CommunityChatID()))
|
|
|
|
|
|
|
|
return community, chat
|
|
|
|
|
2023-06-27 12:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func createCommunity(s *suite.Suite, owner *Messenger) (*communities.Community, *Chat) {
|
2023-10-31 14:20:40 +00:00
|
|
|
return createCommunityConfigurable(s, owner, protobuf.CommunityPermissions_AUTO_ACCEPT)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createOnRequestCommunity(s *suite.Suite, owner *Messenger) (*communities.Community, *Chat) {
|
|
|
|
return createCommunityConfigurable(s, owner, protobuf.CommunityPermissions_MANUAL_ACCEPT)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createCommunityConfigurable(s *suite.Suite, owner *Messenger, permission protobuf.CommunityPermissions_Access) (*communities.Community, *Chat) {
|
2023-06-27 12:20:20 +00:00
|
|
|
description := &requests.CreateCommunity{
|
2023-10-25 13:03:26 +00:00
|
|
|
Membership: protobuf.CommunityPermissions_AUTO_ACCEPT,
|
2023-06-27 12:20:20 +00:00
|
|
|
Name: "status",
|
|
|
|
Color: "#ffffff",
|
|
|
|
Description: "status community description",
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an community chat
|
|
|
|
response, err := owner.CreateCommunity(description, true)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
|
2023-08-03 19:23:38 +00:00
|
|
|
s.Require().Len(response.Communities(), 1)
|
2023-06-27 12:20:20 +00:00
|
|
|
community := response.Communities()[0]
|
2023-08-03 19:23:38 +00:00
|
|
|
s.Require().True(community.Joined())
|
|
|
|
s.Require().True(community.IsControlNode())
|
2024-05-22 13:08:44 +00:00
|
|
|
s.Require().Len(community.Chats(), 1)
|
2023-08-03 19:23:38 +00:00
|
|
|
|
|
|
|
s.Require().Len(response.CommunitiesSettings(), 1)
|
|
|
|
communitySettings := response.CommunitiesSettings()[0]
|
|
|
|
s.Require().Equal(communitySettings.CommunityID, community.IDString())
|
|
|
|
s.Require().Equal(communitySettings.HistoryArchiveSupportEnabled, false)
|
|
|
|
|
2023-06-27 12:20:20 +00:00
|
|
|
return community, response.Chats()[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
func advertiseCommunityTo(s *suite.Suite, community *communities.Community, owner *Messenger, user *Messenger) {
|
2023-12-15 11:55:32 +00:00
|
|
|
// Create wrapped (Signed) community data.
|
|
|
|
wrappedCommunity, err := community.ToProtocolMessageBytes()
|
2023-06-27 12:20:20 +00:00
|
|
|
s.Require().NoError(err)
|
2023-12-15 11:55:32 +00:00
|
|
|
|
|
|
|
// Unwrap signer (Admin) data at user side.
|
|
|
|
signer, description, err := communities.UnwrapCommunityDescriptionMessage(wrappedCommunity)
|
2023-06-27 12:20:20 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2023-12-15 11:55:32 +00:00
|
|
|
// Handle community data state at receiver side
|
|
|
|
messageState := user.buildMessageState()
|
|
|
|
messageState.CurrentMessageState = &CurrentMessageState{}
|
|
|
|
messageState.CurrentMessageState.PublicKey = &user.identity.PublicKey
|
2024-01-08 15:57:57 +00:00
|
|
|
// TODO: handle shards?
|
|
|
|
err = user.handleCommunityDescription(messageState, signer, description, wrappedCommunity, nil, nil)
|
2023-06-27 12:20:20 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
2023-10-20 06:21:41 +00:00
|
|
|
func joinCommunity(s *suite.Suite, community *communities.Community, owner *Messenger, user *Messenger, request *requests.RequestToJoinCommunity, password string) {
|
|
|
|
if password != "" {
|
|
|
|
signingParams, err := user.GenerateJoiningCommunityRequestsForSigning(common.PubkeyToHex(&user.identity.PublicKey), community.ID(), request.AddressesToReveal)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
for i := range signingParams {
|
|
|
|
signingParams[i].Password = password
|
|
|
|
}
|
|
|
|
signatures, err := user.SignData(signingParams)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
updateAddresses := len(request.AddressesToReveal) == 0
|
|
|
|
if updateAddresses {
|
|
|
|
request.AddressesToReveal = make([]string, len(signingParams))
|
|
|
|
}
|
|
|
|
for i := range signingParams {
|
|
|
|
request.AddressesToReveal[i] = signingParams[i].Address
|
|
|
|
request.Signatures = append(request.Signatures, types.FromHex(signatures[i]))
|
|
|
|
}
|
|
|
|
if updateAddresses {
|
|
|
|
request.AirdropAddress = request.AddressesToReveal[0]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-06-27 12:20:20 +00:00
|
|
|
response, err := user.RequestToJoinCommunity(request)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
2024-02-08 09:24:12 +00:00
|
|
|
s.Require().Len(response.RequestsToJoinCommunity(), 1)
|
2023-06-27 12:20:20 +00:00
|
|
|
s.Require().Len(response.ActivityCenterNotifications(), 1)
|
|
|
|
|
|
|
|
notification := response.ActivityCenterNotifications()[0]
|
|
|
|
s.Require().NotNil(notification)
|
|
|
|
s.Require().Equal(notification.Type, ActivityCenterNotificationTypeCommunityRequest)
|
|
|
|
s.Require().Equal(notification.MembershipStatus, ActivityCenterMembershipStatusPending)
|
|
|
|
|
|
|
|
// Retrieve and accept join request
|
2023-09-21 11:16:05 +00:00
|
|
|
_, err = WaitOnMessengerResponse(owner, func(r *MessengerResponse) bool {
|
|
|
|
return len(r.Communities()) > 0 && r.Communities()[0].HasMember(&user.identity.PublicKey)
|
|
|
|
}, "user not accepted")
|
2023-06-27 12:20:20 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
// Retrieve join request response
|
2023-09-21 11:16:05 +00:00
|
|
|
_, err = WaitOnMessengerResponse(user, func(r *MessengerResponse) bool {
|
|
|
|
return len(r.Communities()) > 0 && r.Communities()[0].HasMember(&user.identity.PublicKey)
|
|
|
|
}, "user not accepted")
|
2023-06-27 12:20:20 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
2023-06-29 06:58:47 +00:00
|
|
|
|
2023-09-20 08:37:46 +00:00
|
|
|
func requestToJoinCommunity(s *suite.Suite, controlNode *Messenger, user *Messenger, request *requests.RequestToJoinCommunity) types.HexBytes {
|
2023-07-26 12:16:50 +00:00
|
|
|
response, err := user.RequestToJoinCommunity(request)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
2024-02-08 09:24:12 +00:00
|
|
|
s.Require().Len(response.RequestsToJoinCommunity(), 1)
|
2023-07-26 12:16:50 +00:00
|
|
|
|
2024-02-08 09:24:12 +00:00
|
|
|
requestToJoin := response.RequestsToJoinCommunity()[0]
|
2023-07-26 12:16:50 +00:00
|
|
|
s.Require().Equal(requestToJoin.PublicKey, common.PubkeyToHex(&user.identity.PublicKey))
|
|
|
|
|
2023-09-20 08:37:46 +00:00
|
|
|
_, err = WaitOnMessengerResponse(
|
2023-07-26 12:16:50 +00:00
|
|
|
controlNode,
|
|
|
|
func(r *MessengerResponse) bool {
|
2024-02-08 09:24:12 +00:00
|
|
|
if len(r.RequestsToJoinCommunity()) == 0 {
|
2023-09-20 08:37:46 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2024-02-08 09:24:12 +00:00
|
|
|
for _, resultRequest := range r.RequestsToJoinCommunity() {
|
2023-09-20 08:37:46 +00:00
|
|
|
if resultRequest.PublicKey == common.PubkeyToHex(&user.identity.PublicKey) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
2023-07-26 12:16:50 +00:00
|
|
|
},
|
|
|
|
"control node did not receive community request to join",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2023-09-20 08:37:46 +00:00
|
|
|
return requestToJoin.ID
|
|
|
|
}
|
|
|
|
|
|
|
|
func joinOnRequestCommunity(s *suite.Suite, community *communities.Community, controlNode *Messenger, user *Messenger, request *requests.RequestToJoinCommunity) {
|
|
|
|
// Request to join the community
|
|
|
|
requestToJoinID := requestToJoinCommunity(s, controlNode, user, request)
|
2023-07-26 12:16:50 +00:00
|
|
|
|
|
|
|
// accept join request
|
2023-09-20 08:37:46 +00:00
|
|
|
acceptRequestToJoin := &requests.AcceptRequestToJoinCommunity{ID: requestToJoinID}
|
|
|
|
response, err := controlNode.AcceptRequestToJoinCommunity(acceptRequestToJoin)
|
2023-07-26 12:16:50 +00:00
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NotNil(response)
|
|
|
|
|
|
|
|
updatedCommunity := response.Communities()[0]
|
|
|
|
s.Require().NotNil(updatedCommunity)
|
|
|
|
s.Require().True(updatedCommunity.HasMember(&user.identity.PublicKey))
|
|
|
|
|
|
|
|
// receive request to join response
|
|
|
|
_, err = WaitOnMessengerResponse(
|
|
|
|
user,
|
|
|
|
func(r *MessengerResponse) bool {
|
2023-07-31 15:52:41 +00:00
|
|
|
return len(r.Communities()) > 0 && r.Communities()[0].HasMember(&user.identity.PublicKey)
|
2023-07-26 12:16:50 +00:00
|
|
|
},
|
|
|
|
"user did not receive request to join response",
|
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
2023-07-28 18:18:27 +00:00
|
|
|
|
2023-07-26 12:16:50 +00:00
|
|
|
userCommunity, err := user.GetCommunityByID(community.ID())
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().True(userCommunity.HasMember(&user.identity.PublicKey))
|
2023-07-28 18:18:27 +00:00
|
|
|
|
|
|
|
_, err = WaitOnMessengerResponse(
|
|
|
|
controlNode,
|
|
|
|
func(r *MessengerResponse) bool {
|
2023-07-31 15:52:41 +00:00
|
|
|
return len(r.Communities()) > 0 && r.Communities()[0].HasMember(&user.identity.PublicKey)
|
2023-07-28 18:18:27 +00:00
|
|
|
},
|
2023-07-31 15:52:41 +00:00
|
|
|
"control node did not receive request to join response",
|
2023-07-28 18:18:27 +00:00
|
|
|
)
|
|
|
|
s.Require().NoError(err)
|
2023-07-26 12:16:50 +00:00
|
|
|
}
|
|
|
|
|
2023-06-29 06:58:47 +00:00
|
|
|
func sendChatMessage(s *suite.Suite, sender *Messenger, chatID string, text string) *common.Message {
|
|
|
|
msg := &common.Message{
|
2023-08-18 11:39:59 +00:00
|
|
|
ChatMessage: &protobuf.ChatMessage{
|
2023-06-29 06:58:47 +00:00
|
|
|
ChatId: chatID,
|
|
|
|
ContentType: protobuf.ChatMessage_TEXT_PLAIN,
|
|
|
|
Text: text,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err := sender.SendChatMessage(context.Background(), msg)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
return msg
|
2024-01-30 13:43:34 +00:00
|
|
|
}
|
2023-07-26 12:16:50 +00:00
|
|
|
|
|
|
|
func grantPermission(s *suite.Suite, community *communities.Community, controlNode *Messenger, target *Messenger, role protobuf.CommunityMember_Roles) {
|
|
|
|
responseAddRole, err := controlNode.AddRoleToMember(&requests.AddRoleToMember{
|
|
|
|
CommunityID: community.ID(),
|
|
|
|
User: common.PubkeyToHexBytes(target.IdentityPublicKey()),
|
2023-07-28 18:18:27 +00:00
|
|
|
Role: role,
|
2023-07-26 12:16:50 +00:00
|
|
|
})
|
|
|
|
s.Require().NoError(err)
|
2023-07-31 15:52:41 +00:00
|
|
|
s.Require().NoError(checkRolePermissionInResponse(responseAddRole, target.IdentityPublicKey(), role))
|
2023-07-26 12:16:50 +00:00
|
|
|
|
2023-07-31 15:52:41 +00:00
|
|
|
response, err := WaitOnMessengerResponse(target, func(response *MessengerResponse) bool {
|
2023-07-26 12:16:50 +00:00
|
|
|
if len(response.Communities()) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2023-07-31 15:52:41 +00:00
|
|
|
|
|
|
|
err := checkRolePermissionInResponse(response, target.IdentityPublicKey(), role)
|
|
|
|
|
|
|
|
return err == nil
|
|
|
|
}, "community description changed message not received")
|
|
|
|
s.Require().NoError(err)
|
|
|
|
s.Require().NoError(checkRolePermissionInResponse(response, target.IdentityPublicKey(), role))
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkRolePermissionInResponse(response *MessengerResponse, member *ecdsa.PublicKey, role protobuf.CommunityMember_Roles) error {
|
|
|
|
if len(response.Communities()) == 0 {
|
|
|
|
return errors.New("Response does not contain communities")
|
|
|
|
}
|
|
|
|
rCommunities := response.Communities()
|
|
|
|
switch role {
|
|
|
|
case protobuf.CommunityMember_ROLE_OWNER:
|
|
|
|
if !rCommunities[0].IsMemberOwner(member) {
|
|
|
|
return errors.New("Member without owner role")
|
2023-07-28 18:18:27 +00:00
|
|
|
}
|
2023-07-31 15:52:41 +00:00
|
|
|
case protobuf.CommunityMember_ROLE_ADMIN:
|
|
|
|
if !rCommunities[0].IsMemberAdmin(member) {
|
|
|
|
return errors.New("Member without admin role")
|
|
|
|
}
|
|
|
|
case protobuf.CommunityMember_ROLE_TOKEN_MASTER:
|
|
|
|
if !rCommunities[0].IsMemberTokenMaster(member) {
|
|
|
|
return errors.New("Member without token master role")
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
return errors.New("Can't check unknonw member role")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2023-07-28 18:18:27 +00:00
|
|
|
|
2023-07-31 15:52:41 +00:00
|
|
|
func checkMemberJoinedToTheCommunity(response *MessengerResponse, member *ecdsa.PublicKey) error {
|
|
|
|
if len(response.Communities()) == 0 {
|
|
|
|
return errors.New("No communities in the response")
|
2023-07-26 12:16:50 +00:00
|
|
|
}
|
|
|
|
|
2023-07-31 15:52:41 +00:00
|
|
|
if !response.Communities()[0].HasMember(member) {
|
|
|
|
return errors.New("Member was not added to the community")
|
|
|
|
}
|
2023-07-26 12:16:50 +00:00
|
|
|
|
2023-07-31 15:52:41 +00:00
|
|
|
return nil
|
2023-07-26 12:16:50 +00:00
|
|
|
}
|
2023-08-08 13:16:29 +00:00
|
|
|
|
|
|
|
func waitOnCommunitiesEvent(user *Messenger, condition func(*communities.Subscription) bool) <-chan error {
|
|
|
|
errCh := make(chan error, 1)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer close(errCh)
|
2024-03-01 17:15:38 +00:00
|
|
|
subscription := user.communitiesManager.Subscribe()
|
2023-08-08 13:16:29 +00:00
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
2024-03-01 17:15:38 +00:00
|
|
|
case sub, more := <-subscription:
|
2023-08-08 13:16:29 +00:00
|
|
|
if !more {
|
|
|
|
errCh <- errors.New("channel closed when waiting for communities event")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if condition(sub) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-09 19:59:51 +00:00
|
|
|
case <-time.After(5 * time.Second):
|
2023-08-08 13:16:29 +00:00
|
|
|
errCh <- errors.New("timed out when waiting for communities event")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return errCh
|
|
|
|
}
|