fix(permissions): check ownership of collectibles in permissions view

Marks collectible token criteria items in JoinCommunityView as we owned
if the account owns wallets that match the criteria
This commit is contained in:
Pascal Precht 2023-03-28 12:52:15 +02:00 committed by Follow the white rabbit
parent 40e4a3670f
commit 57467c65b8
8 changed files with 66 additions and 9 deletions

View File

@ -1,4 +1,4 @@
import Tables
import Tables, sugar, algorithm, sequtils, strutils
import io_interface
@ -13,6 +13,7 @@ import ../../../../app_service/service/mailservers/service as mailservers_servic
import ../../../../app_service/service/wallet_account/service as wallet_account_service
import ../../../../app_service/service/token/service as token_service
import ../../../../app_service/service/community_tokens/service as community_tokens_service
import ../../../../app_service/service/collectible/service as collectible_service
import ../../../../app_service/service/visual_identity/service as procs_from_visual_identity_service
import ../../shared_modules/keycard_popup/io_interface as keycard_shared_module
@ -41,6 +42,7 @@ type
mailserversService: mailservers_service.Service
walletAccountService: wallet_account_service.Service
tokenService: token_service.Service
collectibleService: collectible_service.Service
communityTokensService: community_tokens_service.Service
tmpRequestToJoinCommunityId: string
tmpRequestToJoinEnsName: string
@ -52,6 +54,7 @@ proc newController*(delegate: io_interface.AccessInterface, sectionId: string, i
mailserversService: mailservers_service.Service,
walletAccountService: wallet_account_service.Service,
tokenService: token_service.Service,
collectibleService: collectible_service.Service,
communityTokensService: community_tokens_service.Service): Controller =
result = Controller()
result.delegate = delegate
@ -69,6 +72,7 @@ proc newController*(delegate: io_interface.AccessInterface, sectionId: string, i
result.mailserversService = mailserversService
result.walletAccountService = walletAccountService
result.tokenService = tokenService
result.collectibleService = collectibleService
result.communityTokensService = communityTokensService
result.tmpRequestToJoinCommunityId = ""
result.tmpRequestToJoinEnsName = ""
@ -298,6 +302,9 @@ proc init*(self: Controller) =
if (args.communityId == self.sectionId):
self.delegate.onCommunityTokenMetadataAdded(args.communityId, args.tokenMetadata)
self.events.on(SIGNAL_OWNED_COLLECTIBLES_UPDATE_FINISHED) do(e: Args):
self.delegate.onOwnedCollectiblesUpdated()
self.events.on(SIGNAL_WALLET_ACCOUNT_TOKENS_REBUILT) do(e: Args):
self.delegate.onWalletAccountTokensRebuilt()
@ -627,6 +634,17 @@ proc deleteCommunityTokenPermission*(self: Controller, communityId: string, perm
proc allAccountsTokenBalance*(self: Controller, symbol: string): float64 =
return self.walletAccountService.allAccountsTokenBalance(symbol)
proc ownsCollectible*(self: Controller, chainId: int, contractAddress: string, tokenIds: seq[string]): bool =
let addresses = self.walletAccountService.getWalletAccounts().filter(a => a.walletType != WalletTypeWatch).map(a => a.address)
for address in addresses:
let data = self.collectibleService.getOwnedCollectibles(chainId, address)
for collectible in data.collectibles:
if collectible.id.contractAddress == contractAddress.toLowerAscii:
return true
return false
proc getTokenList*(self: Controller): seq[TokenDto] =
return self.tokenService.getTokenList()

View File

@ -393,3 +393,6 @@ method onDeactivateChatLoader*(self: AccessInterface, chatId: string) =
method requestToJoinCommunityWithAuthentication*(self: AccessInterface, communityId: string, ensName: string) =
raise newException(ValueError, "No implementation available")
method onOwnedcollectiblesUpdated*(self: AccessInterface) =
raise newException(ValueError, "No implementation available")

View File

@ -33,6 +33,7 @@ import ../../../../app_service/service/mailservers/service as mailservers_servic
import ../../../../app_service/service/gif/service as gif_service
import ../../../../app_service/service/wallet_account/service as wallet_account_service
import ../../../../app_service/service/token/service as token_service
import ../../../../app_service/service/collectible/service as collectible_service
import ../../../../app_service/service/community_tokens/service as community_tokens_service
import ../../../../app_service/service/visual_identity/service as visual_identity
import ../../../../app_service/service/contacts/dto/contacts as contacts_dto
@ -101,12 +102,13 @@ proc newModule*(
mailserversService: mailservers_service.Service,
walletAccountService: wallet_account_service.Service,
tokenService: token_service.Service,
communityTokensService: community_tokens_service.Service
collectibleService: collectible_service.Service,
communityTokensService: community_tokens_service.Service,
): Module =
result = Module()
result.delegate = delegate
result.controller = controller.newController(result, sectionId, isCommunity, events, settingsService, nodeConfigurationService,
contactService, chatService, communityService, messageService, gifService, mailserversService, walletAccountService, tokenService, communityTokensService)
contactService, chatService, communityService, messageService, gifService, mailserversService, walletAccountService, tokenService, collectibleService, communityTokensService)
result.view = view.newView(result)
result.viewVariant = newQVariant(result.view)
result.moduleLoaded = false
@ -327,6 +329,9 @@ proc buildTokenList(self: Module) =
method onWalletAccountTokensRebuilt*(self: Module) =
self.rebuildCommunityTokenPermissionsModel()
method onOwnedcollectiblesUpdated*(self: Module) =
self.rebuildCommunityTokenPermissionsModel()
proc convertPubKeysToJson(self: Module, pubKeys: string): seq[string] =
return map(parseJson(pubKeys).getElems(), proc(x:JsonNode):string = x.getStr)
@ -1245,9 +1250,19 @@ proc buildTokenPermissionItem*(self: Module, tokenPermission: CommunityTokenPerm
for tc in tokenPermission.tokenCriteria:
let balance = self.controller.allAccountsTokenBalance(tc.symbol)
var tokenCriteriaMet = false
let amount = tc.amount.parseFloat
let tokenCriteriaMet = balance >= amount
if tc.`type` == TokenType.ERC20:
let balance = self.controller.allAccountsTokenBalance(tc.symbol)
tokenCriteriaMet = balance >= amount
if tc.`type` == TokenType.ERC721:
for chainId, address in tc.contractAddresses:
tokenCriteriaMet = self.controller.ownsCollectible(chainId, address, tc.tokenIds)
if tokenCriteriaMet:
break
let tokenCriteriaItem = initTokenCriteriaItem(
tc.symbol,
tc.name,

View File

@ -21,6 +21,7 @@ import ../../../app_service/service/community_tokens/service as community_tokens
import ../../../app_service/service/wallet_account/service as wallet_account_service
import ../../../app_service/service/token/service as token_service
import ../../../app_service/service/network/service as networks_service
import ../../../app_service/service/collectible/service as collectible_service
import ../shared_models/section_item, io_interface
import ../shared_modules/keycard_popup/io_interface as keycard_shared_module
@ -52,6 +53,7 @@ type
walletAccountService: wallet_account_service.Service
tokenService: token_service.Service
networksService: networks_service.Service
collectibleService: collectible_service.Service
# Forward declaration
proc setActiveSection*(self: Controller, sectionId: string, skipSavingInSettings: bool = false)
@ -72,7 +74,8 @@ proc newController*(delegate: io_interface.AccessInterface,
communityTokensService: community_tokens_service.Service,
walletAccountService: wallet_account_service.Service,
tokenService: token_service.Service,
networksService: networks_service.Service
networksService: networks_service.Service,
collectibleService: collectible_service.Service
):
Controller =
result = Controller()
@ -93,6 +96,7 @@ proc newController*(delegate: io_interface.AccessInterface,
result.walletAccountService = walletAccountService
result.tokenService = tokenService
result.networksService = networksService
result.collectibleService = collectibleService
proc delete*(self: Controller) =
discard
@ -117,6 +121,7 @@ proc init*(self: Controller) =
self.mailserversService,
self.walletAccountService,
self.tokenService,
self.collectibleService,
self.communityTokensService
)
@ -133,6 +138,7 @@ proc init*(self: Controller) =
self.mailserversService,
self.walletAccountService,
self.tokenService,
self.collectibleService,
self.communityTokensService
)
@ -168,6 +174,7 @@ proc init*(self: Controller) =
self.mailserversService,
self.walletAccountService,
self.tokenService,
self.collectibleService,
self.communityTokensService,
setActive = args.fromUserAction
)
@ -187,6 +194,7 @@ proc init*(self: Controller) =
self.mailserversService,
self.walletAccountService,
self.tokenService,
self.collectibleService,
self.communityTokensService,
setActive = args.fromUserAction
)
@ -210,6 +218,7 @@ proc init*(self: Controller) =
self.mailserversService,
self.walletAccountService,
self.tokenService,
self.collectibleService,
self.communityTokensService,
setActive = true
)
@ -231,6 +240,7 @@ proc init*(self: Controller) =
self.mailserversService,
self.walletAccountService,
self.tokenService,
self.collectibleService,
self.communityTokensService,
setActive = false
)

View File

@ -11,6 +11,7 @@ import ../../../app_service/service/mailservers/service as mailservers_service
import ../../../app_service/service/community_tokens/service as community_token_service
import ../../../app_service/service/wallet_account/service as wallet_account_service
import ../../../app_service/service/token/service as token_service
import ../../../app_service/service/collectible/service as collectible_service
import ../../../app_service/service/community_tokens/service as community_tokens_service
from ../../../app_service/common/types import StatusType
@ -89,6 +90,7 @@ method onChannelGroupsLoaded*(
mailserversService: mailservers_service.Service,
walletAccountService: wallet_account_service.Service,
tokenService: token_service.Service,
collectibleService: collectible_service.Service,
communityTokensService: community_tokens_service.Service)
{.base.} =
raise newException(ValueError, "No implementation available")
@ -106,6 +108,7 @@ method onCommunityDataLoaded*(
mailserversService: mailservers_service.Service,
walletAccountService: wallet_account_service.Service,
tokenService: token_service.Service,
collectibleService: collectible_service.Service,
communityTokensService: community_tokens_service.Service)
{.base.} =
raise newException(ValueError, "No implementation available")
@ -153,6 +156,7 @@ method communityJoined*(self: AccessInterface, community: CommunityDto, events:
mailserversService: mailservers_service.Service,
walletAccountService: wallet_account_service.Service,
tokenService: token_service.Service,
collectibleService: collectible_service.Service,
communityTokensService: community_tokens_service.Service,
setActive: bool = false,) {.base.} =
raise newException(ValueError, "No implementation available")

View File

@ -169,7 +169,8 @@ proc newModule*[T](
communityTokensService,
walletAccountService,
tokenService,
networkService
networkService,
collectibleService
)
result.moduleLoaded = false
result.chatsLoaded = false
@ -546,6 +547,7 @@ method onChannelGroupsLoaded*[T](
mailserversService: mailservers_service.Service,
walletAccountService: wallet_account_service.Service,
tokenService: token_service.Service,
collectibleService: collectible_service.Service,
communityTokensService: community_tokens_service.Service
) =
self.chatsLoaded = true
@ -572,6 +574,7 @@ method onChannelGroupsLoaded*[T](
mailserversService,
walletAccountService,
tokenService,
collectibleService,
communityTokensService
)
let channelGroupItem = self.createChannelGroupItem(channelGroup)
@ -604,6 +607,7 @@ method onCommunityDataLoaded*[T](
mailserversService: mailservers_service.Service,
walletAccountService: wallet_account_service.Service,
tokenService: token_service.Service,
collectibleService: collectible_service.Service,
communityTokensService: community_tokens_service.Service
) =
self.communityDataLoaded = true
@ -623,6 +627,7 @@ method onCommunityDataLoaded*[T](
mailserversService,
walletAccountService,
tokenService,
collectibleService,
communityTokensService
)
@ -848,6 +853,7 @@ method communityJoined*[T](
mailserversService: mailservers_service.Service,
walletAccountService: wallet_account_service.Service,
tokenService: token_service.Service,
collectibleService: collectible_service.Service,
communityTokensService: community_tokens_service.Service,
setActive: bool = false,
) =
@ -869,6 +875,7 @@ method communityJoined*[T](
mailserversService,
walletAccountService,
tokenService,
collectibleService,
communityTokensService
)
let channelGroup = community.toChannelGroupDto()

View File

@ -202,7 +202,7 @@ proc toTokenCriteriaDto*(jsonObj: JsonNode): TokenCriteriaDto =
result.`type` = TokenType(typeInt)
var contractAddressesObj: JsonNode
if(jsonObj.getProp("contractAddresses", contractAddressesObj) and contractAddressesObj.kind == JObject):
if(jsonObj.getProp("contract_addresses", contractAddressesObj) and contractAddressesObj.kind == JObject):
result.contractAddresses = initTable[int, string]()
for chainId, contractAddress in contractAddressesObj:
result.contractAddresses[parseInt(chainId)] = contractAddress.getStr

2
vendor/status-go vendored

@ -1 +1 @@
Subproject commit 6eb39eca7533ba730194cf440d5c617b693bba14
Subproject commit 9267ad46c51acf57f1ef62bfabcce6daeed0e790