Skip to content

Commit

Permalink
fix(permissions): check ownership of collectibles in permissions view
Browse files Browse the repository at this point in the history
Marks collectible token criteria items in JoinCommunityView as we owned
if the account owns wallets that match the criteria
  • Loading branch information
0x-r4bbit committed Mar 30, 2023
1 parent 40e4a36 commit 57467c6
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 9 deletions.
20 changes: 19 additions & 1 deletion src/app/modules/main/chat_section/controller.nim
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Tables
import Tables, sugar, algorithm, sequtils, strutils

import io_interface

Expand All @@ -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

Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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 = ""
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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()

Expand Down
3 changes: 3 additions & 0 deletions src/app/modules/main/chat_section/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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")
23 changes: 19 additions & 4 deletions src/app/modules/main/chat_section/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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,
Expand Down
12 changes: 11 additions & 1 deletion src/app/modules/main/controller.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand All @@ -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()
Expand All @@ -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
Expand All @@ -117,6 +121,7 @@ proc init*(self: Controller) =
self.mailserversService,
self.walletAccountService,
self.tokenService,
self.collectibleService,
self.communityTokensService
)

Expand All @@ -133,6 +138,7 @@ proc init*(self: Controller) =
self.mailserversService,
self.walletAccountService,
self.tokenService,
self.collectibleService,
self.communityTokensService
)

Expand Down Expand Up @@ -168,6 +174,7 @@ proc init*(self: Controller) =
self.mailserversService,
self.walletAccountService,
self.tokenService,
self.collectibleService,
self.communityTokensService,
setActive = args.fromUserAction
)
Expand All @@ -187,6 +194,7 @@ proc init*(self: Controller) =
self.mailserversService,
self.walletAccountService,
self.tokenService,
self.collectibleService,
self.communityTokensService,
setActive = args.fromUserAction
)
Expand All @@ -210,6 +218,7 @@ proc init*(self: Controller) =
self.mailserversService,
self.walletAccountService,
self.tokenService,
self.collectibleService,
self.communityTokensService,
setActive = true
)
Expand All @@ -231,6 +240,7 @@ proc init*(self: Controller) =
self.mailserversService,
self.walletAccountService,
self.tokenService,
self.collectibleService,
self.communityTokensService,
setActive = false
)
Expand Down
4 changes: 4 additions & 0 deletions src/app/modules/main/io_interface.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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")
Expand All @@ -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")
Expand Down Expand Up @@ -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")
Expand Down
9 changes: 8 additions & 1 deletion src/app/modules/main/module.nim
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,8 @@ proc newModule*[T](
communityTokensService,
walletAccountService,
tokenService,
networkService
networkService,
collectibleService
)
result.moduleLoaded = false
result.chatsLoaded = false
Expand Down Expand Up @@ -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
Expand All @@ -572,6 +574,7 @@ method onChannelGroupsLoaded*[T](
mailserversService,
walletAccountService,
tokenService,
collectibleService,
communityTokensService
)
let channelGroupItem = self.createChannelGroupItem(channelGroup)
Expand Down Expand Up @@ -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
Expand All @@ -623,6 +627,7 @@ method onCommunityDataLoaded*[T](
mailserversService,
walletAccountService,
tokenService,
collectibleService,
communityTokensService
)

Expand Down Expand Up @@ -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,
) =
Expand All @@ -869,6 +875,7 @@ method communityJoined*[T](
mailserversService,
walletAccountService,
tokenService,
collectibleService,
communityTokensService
)
let channelGroup = community.toChannelGroupDto()
Expand Down
2 changes: 1 addition & 1 deletion src/app_service/service/community/dto/community.nim
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 57467c6

Please sign in to comment.