From 9bb2dcff86cbd7cdce9036aca935ae680701302d Mon Sep 17 00:00:00 2001 From: Pascal Precht <445106+0x-r4bbit@users.noreply.github.com> Date: Tue, 9 May 2023 13:09:38 +0200 Subject: [PATCH] fix: ensure client checks available ENS names in join community view We haven't checked for ENS previously. This is now done in this commit. Depends on: https://github.com/status-im/status-go/pull/3467 Closes #4393 --- .../modules/main/chat_section/controller.nim | 25 ++++++++++++++++++- src/app/modules/main/chat_section/module.nim | 7 +++++- src/app/modules/main/controller.nim | 13 ++++++++-- src/app/modules/main/io_interface.nim | 8 ++++-- src/app/modules/main/module.nim | 18 +++++++++---- src/app_service/service/community/service.nim | 15 +++++------ src/app_service/service/ens/service.nim | 11 ++++++++ src/backend/ens.nim | 4 +++ 8 files changed, 81 insertions(+), 20 deletions(-) diff --git a/src/app/modules/main/chat_section/controller.nim b/src/app/modules/main/chat_section/controller.nim index 390b7bc368..6de44fefe6 100644 --- a/src/app/modules/main/chat_section/controller.nim +++ b/src/app/modules/main/chat_section/controller.nim @@ -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/ens/service as ens_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 @@ -44,6 +45,7 @@ type tokenService: token_service.Service collectibleService: collectible_service.Service communityTokensService: community_tokens_service.Service + ensService: ens_service.Service tmpRequestToJoinCommunityId: string tmpRequestToJoinEnsName: string @@ -55,7 +57,8 @@ proc newController*(delegate: io_interface.AccessInterface, sectionId: string, i walletAccountService: wallet_account_service.Service, tokenService: token_service.Service, collectibleService: collectible_service.Service, - communityTokensService: community_tokens_service.Service): Controller = + communityTokensService: community_tokens_service.Service, + ensService: ens_service.Service): Controller = result = Controller() result.delegate = delegate result.sectionId = sectionId @@ -74,6 +77,7 @@ proc newController*(delegate: io_interface.AccessInterface, sectionId: string, i result.tokenService = tokenService result.collectibleService = collectibleService result.communityTokensService = communityTokensService + result.ensService = ensService result.tmpRequestToJoinCommunityId = "" result.tmpRequestToJoinEnsName = "" @@ -647,6 +651,25 @@ proc ownsCollectible*(self: Controller, chainId: int, contractAddress: string, t return false +proc ownsENS*(self: Controller, ensPattern: string): bool = + let addresses = self.walletAccountService.getWalletAccounts().filter(a => a.walletType != WalletTypeWatch).map(a => a.address) + let ensNames = self.ensService.reverseResolveENS(addresses) + if ensNames.len == 0: + return false + + if not ensPattern.startsWith("*."): + for name in ensNames: + if name == ensPattern: + return true + + else: + let parentName = ensPattern.substr(2, ensPattern.len-1) + for name in ensNames: + if name.endsWith(parentName): + return true + + return false + proc getTokenList*(self: Controller): seq[TokenDto] = return self.tokenService.getTokenList() diff --git a/src/app/modules/main/chat_section/module.nim b/src/app/modules/main/chat_section/module.nim index 97d588fac1..093727bd56 100644 --- a/src/app/modules/main/chat_section/module.nim +++ b/src/app/modules/main/chat_section/module.nim @@ -35,6 +35,7 @@ import ../../../../app_service/service/wallet_account/service as wallet_account_ 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/ens/service as ens_service import ../../../../app_service/service/visual_identity/service as visual_identity import ../../../../app_service/service/contacts/dto/contacts as contacts_dto import ../../../../app_service/service/community/dto/community as community_dto @@ -106,11 +107,12 @@ proc newModule*( tokenService: token_service.Service, collectibleService: collectible_service.Service, communityTokensService: community_tokens_service.Service, + ensService: ens_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, collectibleService, communityTokensService) + contactService, chatService, communityService, messageService, gifService, mailserversService, walletAccountService, tokenService, collectibleService, communityTokensService, ensService) result.view = view.newView(result) result.viewVariant = newQVariant(result.view) result.moduleLoaded = false @@ -1304,6 +1306,9 @@ proc buildTokenPermissionItem*(self: Module, tokenPermission: CommunityTokenPerm if tokenCriteriaMet: break + if tc.`type` == TokenType.ENS: + tokenCriteriaMet = self.controller.ownsENS(tc.ensPattern) + let tokenCriteriaItem = initTokenCriteriaItem( tc.symbol, tc.name, diff --git a/src/app/modules/main/controller.nim b/src/app/modules/main/controller.nim index 2df3fe607d..ab5d64864a 100644 --- a/src/app/modules/main/controller.nim +++ b/src/app/modules/main/controller.nim @@ -18,6 +18,7 @@ import ../../../app_service/service/mailservers/service as mailservers_service import ../../../app_service/service/privacy/service as privacy_service import ../../../app_service/service/node/service as node_service import ../../../app_service/service/community_tokens/service as community_tokens_service +import ../../../app_service/service/ens/service as ens_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/network/service as networks_service @@ -48,6 +49,7 @@ type mailserversService: mailservers_service.Service nodeService: node_service.Service communityTokensService: community_tokens_service.Service + ensService: ens_service.Service activeSectionId: string authenticateUserFlowRequestedBy: string walletAccountService: wallet_account_service.Service @@ -72,6 +74,7 @@ proc newController*(delegate: io_interface.AccessInterface, mailserversService: mailservers_service.Service, nodeService: node_service.Service, communityTokensService: community_tokens_service.Service, + ensService: ens_service.Service, walletAccountService: wallet_account_service.Service, tokenService: token_service.Service, networksService: networks_service.Service, @@ -118,7 +121,8 @@ proc init*(self: Controller) = self.walletAccountService, self.tokenService, self.collectibleService, - self.communityTokensService + self.communityTokensService, + self.ensService ) self.events.on(SIGNAL_COMMUNITY_DATA_LOADED) do(e:Args): @@ -135,7 +139,8 @@ proc init*(self: Controller) = self.walletAccountService, self.tokenService, self.collectibleService, - self.communityTokensService + self.communityTokensService, + self.ensService ) self.events.on(SIGNAL_CHANNEL_GROUPS_LOADING_FAILED) do(e:Args): @@ -172,6 +177,7 @@ proc init*(self: Controller) = self.tokenService, self.collectibleService, self.communityTokensService, + self.ensService, setActive = args.fromUserAction ) @@ -192,6 +198,7 @@ proc init*(self: Controller) = self.tokenService, self.collectibleService, self.communityTokensService, + self.ensService, setActive = args.fromUserAction ) @@ -216,6 +223,7 @@ proc init*(self: Controller) = self.tokenService, self.collectibleService, self.communityTokensService, + self.ensService, setActive = true ) @@ -238,6 +246,7 @@ proc init*(self: Controller) = self.tokenService, self.collectibleService, self.communityTokensService, + self.ensService, setActive = false ) diff --git a/src/app/modules/main/io_interface.nim b/src/app/modules/main/io_interface.nim index 7f9781d4b8..7c448151d1 100644 --- a/src/app/modules/main/io_interface.nim +++ b/src/app/modules/main/io_interface.nim @@ -13,6 +13,7 @@ import ../../../app_service/service/wallet_account/service as wallet_account_ser 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/ens/service as ens_service from ../../../app_service/common/types import StatusType import ../../global/app_signals @@ -88,7 +89,8 @@ method onChannelGroupsLoaded*( walletAccountService: wallet_account_service.Service, tokenService: token_service.Service, collectibleService: collectible_service.Service, - communityTokensService: community_tokens_service.Service) + communityTokensService: community_tokens_service.Service, + ensService: ens_service.Service) {.base.} = raise newException(ValueError, "No implementation available") @@ -106,7 +108,8 @@ method onCommunityDataLoaded*( walletAccountService: wallet_account_service.Service, tokenService: token_service.Service, collectibleService: collectible_service.Service, - communityTokensService: community_tokens_service.Service) + communityTokensService: community_tokens_service.Service, + ensService: ens_service.Service) {.base.} = raise newException(ValueError, "No implementation available") @@ -155,6 +158,7 @@ method communityJoined*(self: AccessInterface, community: CommunityDto, events: tokenService: token_service.Service, collectibleService: collectible_service.Service, communityTokensService: community_tokens_service.Service, + ensService: ens_service.Service, setActive: bool = false,) {.base.} = raise newException(ValueError, "No implementation available") diff --git a/src/app/modules/main/module.nim b/src/app/modules/main/module.nim index adaba1137c..70ae21e10b 100644 --- a/src/app/modules/main/module.nim +++ b/src/app/modules/main/module.nim @@ -54,6 +54,7 @@ import ../../../app_service/service/mailservers/service as mailservers_service import ../../../app_service/service/gif/service as gif_service import ../../../app_service/service/ens/service as ens_service import ../../../app_service/service/community_tokens/service as community_tokens_service +import ../../../app_service/service/ens/service as ens_service import ../../../app_service/service/network/service as network_service import ../../../app_service/service/general/service as general_service import ../../../app_service/service/keycard/service as keycard_service @@ -165,6 +166,7 @@ proc newModule*[T]( mailserversService, nodeService, communityTokensService, + ensService, walletAccountService, tokenService, networkService, @@ -541,7 +543,8 @@ method onChannelGroupsLoaded*[T]( walletAccountService: wallet_account_service.Service, tokenService: token_service.Service, collectibleService: collectible_service.Service, - communityTokensService: community_tokens_service.Service + communityTokensService: community_tokens_service.Service, + ensService: ens_service.Service ) = self.chatsLoaded = true if not self.communityDataLoaded: @@ -568,7 +571,8 @@ method onChannelGroupsLoaded*[T]( walletAccountService, tokenService, collectibleService, - communityTokensService + communityTokensService, + ensService ) let channelGroupItem = self.createChannelGroupItem(channelGroup) self.view.model().addItem(channelGroupItem) @@ -601,7 +605,8 @@ method onCommunityDataLoaded*[T]( walletAccountService: wallet_account_service.Service, tokenService: token_service.Service, collectibleService: collectible_service.Service, - communityTokensService: community_tokens_service.Service + communityTokensService: community_tokens_service.Service, + ensService: ens_service.Service ) = self.communityDataLoaded = true if not self.chatsLoaded: @@ -621,7 +626,8 @@ method onCommunityDataLoaded*[T]( walletAccountService, tokenService, collectibleService, - communityTokensService + communityTokensService, + ensService ) method onChatsLoadingFailed*[T](self: Module[T]) = @@ -844,6 +850,7 @@ method communityJoined*[T]( tokenService: token_service.Service, collectibleService: collectible_service.Service, communityTokensService: community_tokens_service.Service, + ensService: ens_service.Service, setActive: bool = false, ) = var firstCommunityJoined = false @@ -865,7 +872,8 @@ method communityJoined*[T]( walletAccountService, tokenService, collectibleService, - communityTokensService + communityTokensService, + ensService ) let channelGroup = community.toChannelGroupDto() self.channelGroupModules[community.id].load(channelGroup, events, settingsService, nodeConfigurationService, contactsService, diff --git a/src/app_service/service/community/service.nim b/src/app_service/service/community/service.nim index b7e0a704b1..933459aac6 100644 --- a/src/app_service/service/community/service.nim +++ b/src/app_service/service/community/service.nim @@ -572,15 +572,12 @@ QtObject: permissionUpdated = true - for tc in tokenPermission.tokenCriteria: - let index = findIndexBySymbol(tc.symbol, prevTokenPermission.tokenCriteria) - if index == -1: - continue - - let prevTc = prevTokenPermission.tokenCriteria[index] - if tc.amount != prevTc.amount or tc.ensPattern != prevTc.ensPattern: - permissionUpdated = true - break + if not permissionUpdated: + for i, tc in tokenPermission.tokenCriteria: + let prevTc = prevTokenPermission.tokenCriteria[i] + if tc.amount != prevTc.amount or tc.ensPattern != prevTc.ensPattern or tc.`type` != prevTc.`type`: + permissionUpdated = true + break if permissionUpdated: self.events.emit(SIGNAL_COMMUNITY_TOKEN_PERMISSION_UPDATED, diff --git a/src/app_service/service/ens/service.nim b/src/app_service/service/ens/service.nim index 906b1b721a..8152a81613 100644 --- a/src/app_service/service/ens/service.nim +++ b/src/app_service/service/ens/service.nim @@ -411,6 +411,17 @@ QtObject: let balances = status_go_backend.getTokensBalancesForChainIDs(@[self.getChainId()], @[account], @[token.addressAsString()]).result return ens_utils.hex2Token(balances{account}{token.addressAsString()}.getStr, token.decimals) + proc reverseResolveENS*(self: Service, addresses: seq[string]): seq[string] = + try: + let response = status_ens.reverseResolveENS(addresses) + result = @[] + if response.result.kind != JNull: + for ensName in response.result: + result.add(ensName.getStr) + except Exception as e: + error "Error reverse resolving ENS names", exception=e.msg + raise + proc resourceUrl*(self: Service, username: string): (string, string, string) = try: let response = status_ens.resourceURL(self.getChainId(), username) diff --git a/src/backend/ens.nim b/src/backend/ens.nim index 81f7590ab2..f078ec7e91 100644 --- a/src/backend/ens.nim +++ b/src/backend/ens.nim @@ -7,6 +7,10 @@ proc getEnsUsernames*(): RpcResponse[JsonNode] {.raises: [Exception].} = let payload = %* [] return core.callPrivateRPC("ens_getEnsUsernames", payload) +proc reverseResolveENS*(addresses: seq[string]): RpcResponse[JsonNode] {.raises: [Exception].} = + let payload = %* [addresses] + return core.callPrivateRPC("reverseResolveENS".prefix, payload) + proc add*(chainId: int, username: string): RpcResponse[JsonNode] {.raises: [Exception].} = let payload = %* [chainId, username] return core.callPrivateRPC("ens_add", payload)