diff --git a/src/app/boot/app_controller.nim b/src/app/boot/app_controller.nim index 8c3bc38f70..2eedc89734 100644 --- a/src/app/boot/app_controller.nim +++ b/src/app/boot/app_controller.nim @@ -15,6 +15,8 @@ import ../../app_service/service/bookmarks/service as bookmark_service import ../../app_service/service/dapp_permissions/service as dapp_permissions_service import ../../app_service/service/mnemonic/service as mnemonic_service import ../../app_service/service/privacy/service as privacy_service +import ../../app_service/service/provider/service as provider_service +import ../../app_service/service/ens/service as ens_service import ../core/local_account_settings import ../../app_service/service/profile/service as profile_service @@ -75,6 +77,8 @@ type settingService: setting_service.Service bookmarkService: bookmark_service.Service dappPermissionsService: dapp_permissions_service.Service + ensService: ens_service.Service + providerService: provider_service.Service profileService: profile_service.Service settingsService: settings_service.Service aboutService: about_service.Service @@ -142,6 +146,8 @@ proc newAppController*(appService: AppService): AppController = result.languageService = language_service.newService() result.mnemonicService = mnemonic_service.newService() result.privacyService = privacy_service.newService() + result.ensService = ens_service.newService() + result.providerService = provider_service.newService(result.dappPermissionsService, result.settingsService, result.ensService) # Core result.localAppSettingsVariant = newQVariant(singletonInstance.localAppSettings) @@ -175,7 +181,8 @@ proc newAppController*(appService: AppService): AppController = result.dappPermissionsService, result.languageService, result.mnemonicService, - result.privacyService + result.privacyService, + result.providerService, ) ################################################# @@ -225,6 +232,8 @@ proc delete*(self: AppController) = self.walletAccountService.delete self.aboutService.delete self.dappPermissionsService.delete + self.providerService.delete + self.ensService.delete proc startupDidLoad*(self: AppController) = ################################################# @@ -269,6 +278,8 @@ proc load*(self: AppController) = self.tokenService.init() self.settingsService.init() self.dappPermissionsService.init() + self.ensService.init() + self.providerService.init() self.walletAccountService.init() self.transactionService.init() self.mainModule.load() diff --git a/src/app/modules/main/browser_section/dapps/view.nim b/src/app/modules/main/browser_section/dapps/view.nim index 774f79d6e3..c15d8e4a25 100644 --- a/src/app/modules/main/browser_section/dapps/view.nim +++ b/src/app/modules/main/browser_section/dapps/view.nim @@ -68,11 +68,6 @@ QtObject: proc revokeAllPermissions(self: View) {.slot.} = self.delegate.revokeAllPermissions() - proc ensResourceURL*(self: View, ens: string, url: string): string {.slot.} = - discard # TODO: - #let (url, base, http_scheme, path_prefix, hasContentHash) = self.status.provider.ensResourceURL(ens, url) - #result = url_replaceHostAndAddPath(url, (if hasContentHash: base else: url_host(base)), http_scheme, path_prefix) - proc clearDapps*(self: View) = self.dappsModel.clear() diff --git a/src/app/modules/main/browser_section/module.nim b/src/app/modules/main/browser_section/module.nim index dbe874af18..b944497dd3 100644 --- a/src/app/modules/main/browser_section/module.nim +++ b/src/app/modules/main/browser_section/module.nim @@ -9,6 +9,7 @@ import dapps/module as dapps_module import ../../../../app_service/service/bookmarks/service as bookmark_service import ../../../../app_service/service/settings/service as settings_service import ../../../../app_service/service/dapp_permissions/service as dapp_permissions_service +import ../../../../app_service/service/provider/service as provider_service export io_interface type @@ -21,13 +22,17 @@ type bookmarkModule: bookmark_module.AccessInterface dappsModule: dapps_module.AccessInterface -proc newModule*(delegate: delegate_interface.AccessInterface, bookmarkService: bookmark_service.ServiceInterface, settingsService: settings_service.ServiceInterface, dappPermissionsService: dapp_permissions_service.ServiceInterface): Module = +proc newModule*(delegate: delegate_interface.AccessInterface, + bookmarkService: bookmark_service.ServiceInterface, + settingsService: settings_service.ServiceInterface, + dappPermissionsService: dapp_permissions_service.ServiceInterface, + providerService: provider_service.ServiceInterface): Module = result = Module() result.delegate = delegate result.view = view.newView(result) result.viewVariant = newQVariant(result.view) result.moduleLoaded = false - result.providerModule = provider_module.newModule(result, settingsService) + result.providerModule = provider_module.newModule(result, settingsService, dappPermissionsService, providerService) result.bookmarkModule = bookmark_module.newModule(result, bookmarkService) result.dappsModule = dapps_module.newModule(result, dappPermissionsService) @@ -72,4 +77,3 @@ method dappsDidLoad*(self: Module) = method viewDidLoad*(self: Module) = self.checkIfModuleDidLoad() - diff --git a/src/app/modules/main/browser_section/provider/controller.nim b/src/app/modules/main/browser_section/provider/controller.nim index 5d327f6a59..7cc2acdb0d 100644 --- a/src/app/modules/main/browser_section/provider/controller.nim +++ b/src/app/modules/main/browser_section/provider/controller.nim @@ -1,23 +1,29 @@ import Tables -import result import controller_interface import io_interface import ../../../../../app_service/service/settings/service as settings_service - +import ../../../../../app_service/service/dapp_permissions/service as dapp_permissions_service +import ../../../../../app_service/service/provider/service as provider_service export controller_interface type Controller* = ref object of controller_interface.AccessInterface delegate: io_interface.AccessInterface settingsService: settings_service.ServiceInterface + dappPermissionsService: dapp_permissions_service.ServiceInterface + providerService: provider_service.ServiceInterface -proc newController*(delegate: io_interface.AccessInterface, - settingsService: settings_service.ServiceInterface): +proc newController*(delegate: io_interface.AccessInterface, + settingsService: settings_service.ServiceInterface, + dappPermissionsService: dapp_permissions_service.ServiceInterface, + providerService: provider_service.ServiceInterface): Controller = result = Controller() result.delegate = delegate result.settingsService = settingsService + result.dappPermissionsService = dappPermissionsService + result.providerService = providerService method delete*(self: Controller) = discard @@ -33,4 +39,16 @@ method setDappsAddress*(self: Controller, address: string) = self.delegate.onDappAddressChanged(address) method getCurrentNetworkDetails*(self: Controller): NetworkDetails = - return self.settingsService.getCurrentNetworkDetails() \ No newline at end of file + return self.settingsService.getCurrentNetworkDetails() + +method disconnect*(self: Controller) = + discard self.dappPermissionsService.revoke("web3".toPermission()) + +method postMessage*(self: Controller, message: string): string = + return self.providerService.postMessage(message) + +method hasPermission*(self: Controller, hostname: string, permission: string): bool = + return self.dappPermissionsService.hasPermission(hostname, permission.toPermission()) + +method ensResourceURL*(self: Controller, ens: string, url: string): (string, string, string, string, bool) = + return self.providerService.ensResourceURL(ens, url) diff --git a/src/app/modules/main/browser_section/provider/controller_interface.nim b/src/app/modules/main/browser_section/provider/controller_interface.nim index 0048b87cc4..9345fd9756 100644 --- a/src/app/modules/main/browser_section/provider/controller_interface.nim +++ b/src/app/modules/main/browser_section/provider/controller_interface.nim @@ -18,3 +18,15 @@ method setDappsAddress*(self: AccessInterface, address: string) {.base.} = method getCurrentNetworkDetails*(self: AccessInterface): NetworkDetails {.base.} = raise newException(ValueError, "No implementation available") + +method disconnect*(self: AccessInterface) {.base.} = + raise newException(ValueError, "No implementation available") + +method postMessage*(self: AccessInterface, message: string): string {.base.} = + raise newException(ValueError, "No implementation available") + +method hasPermission*(self: AccessInterface, hostname: string, permission: string): bool {.base.} = + raise newException(ValueError, "No implementation available") + +method ensResourceURL*(self: AccessInterface, ens: string, url: string): (string, string, string, string, bool) = + raise newException(ValueError, "No implementation available") diff --git a/src/app/modules/main/browser_section/provider/module.nim b/src/app/modules/main/browser_section/provider/module.nim index 61cb67c207..039f1872da 100644 --- a/src/app/modules/main/browser_section/provider/module.nim +++ b/src/app/modules/main/browser_section/provider/module.nim @@ -4,6 +4,8 @@ import view import controller import ../io_interface as delegate_interface import ../../../../../app_service/service/settings/service as settings_service +import ../../../../../app_service/service/dapp_permissions/service as dapp_permissions_service +import ../../../../../app_service/service/provider/service as provider_service import ../../../../core/global_singleton export io_interface @@ -15,13 +17,16 @@ type moduleLoaded: bool controller: controller.AccessInterface -proc newModule*(delegate: delegate_interface.AccessInterface, settingsService: settings_service.ServiceInterface): Module = +proc newModule*(delegate: delegate_interface.AccessInterface, + settingsService: settings_service.ServiceInterface, + dappPermissionsService: dapp_permissions_service.ServiceInterface, + providerService: provider_service.ServiceInterface): Module = result = Module() result.delegate = delegate result.view = newView(result) result.viewVariant = newQVariant(result.view) result.moduleLoaded = false - result.controller = controller.newController(result, settingsService) + result.controller = controller.newController(result, settingsService, dappPermissionsService, providerService) method delete*(self: Module) = self.controller.delete @@ -49,3 +54,15 @@ proc checkIfModuleDidLoad(self: Module) = method viewDidLoad*(self: Module) = self.checkIfModuleDidLoad() + +method disconnect*(self: Module) = + self.controller.disconnect() + +method postMessage*(self: Module, message: string): string = + return self.controller.postMessage(message) + +method hasPermission*(self: Module, hostname: string, permission: string): bool = + return self.controller.hasPermission(hostname, permission) + +method ensResourceURL*(self: Module, ens: string, url: string): (string, string, string, string, bool) = + return self.controller.ensResourceURL(ens, url) diff --git a/src/app/modules/main/browser_section/provider/private_interfaces/module_controller_delegate_interface.nim b/src/app/modules/main/browser_section/provider/private_interfaces/module_controller_delegate_interface.nim index c0442ca943..6faee28bc0 100644 --- a/src/app/modules/main/browser_section/provider/private_interfaces/module_controller_delegate_interface.nim +++ b/src/app/modules/main/browser_section/provider/private_interfaces/module_controller_delegate_interface.nim @@ -1,5 +1,17 @@ -method setDappsAddress*(self: AccessInterface, newDappAddress: string) = +method setDappsAddress*(self: AccessInterface, newDappAddress: string) {.base.} = raise newException(ValueError, "No implementation available") -method onDappAddressChanged*(self: AccessInterface, newDappAddress: string) = +method onDappAddressChanged*(self: AccessInterface, newDappAddress: string) {.base.} = + raise newException(ValueError, "No implementation available") + +method disconnect*(self: AccessInterface) {.base.} = + raise newException(ValueError, "No implementation available") + +method postMessage*(self: AccessInterface, message: string): string {.base.} = + raise newException(ValueError, "No implementation available") + +method hasPermission*(self: AccessInterface, hostname: string, permission: string): bool {.base.} = + raise newException(ValueError, "No implementation available") + +method ensResourceURL*(self: AccessInterface, ens: string, url: string): (string, string, string, string, bool) = raise newException(ValueError, "No implementation available") diff --git a/src/app/modules/main/browser_section/provider/view.nim b/src/app/modules/main/browser_section/provider/view.nim index ffe909dcac..c7f5eb0082 100644 --- a/src/app/modules/main/browser_section/provider/view.nim +++ b/src/app/modules/main/browser_section/provider/view.nim @@ -56,3 +56,16 @@ QtObject: proc getHost*(self: View, url: string): string {.slot.} = result = url_host(url) + proc disconnect*(self: View) {.slot.} = + self.delegate.disconnect() + + proc postMessage*(self: View, message: string): string {.slot.} = + return self.delegate.postMessage(message) + + proc hasPermission(self: View, hostname: string, permission: string): bool {.slot.} = + return self.delegate.hasPermission(hostname, permission) + + proc ensResourceURL*(self: View, ens: string, url: string): string {.slot.} = + let (url, base, http_scheme, path_prefix, hasContentHash) = self.delegate.ensResourceURL(ens, url) + result = url_replaceHostAndAddPath(url, (if hasContentHash: base else: url_host(base)), http_scheme, path_prefix) + diff --git a/src/app/modules/main/controller_interface.nim b/src/app/modules/main/controller_interface.nim index d9c15ef2e3..cd4930623f 100644 --- a/src/app/modules/main/controller_interface.nim +++ b/src/app/modules/main/controller_interface.nim @@ -18,4 +18,4 @@ method checkForStoringPassword*(self: AccessInterface) {.base.} = raise newException(ValueError, "No implementation available") method storePassword*(self: AccessInterface, password: string) {.base.} = - raise newException(ValueError, "No implementation available") \ No newline at end of file + raise newException(ValueError, "No implementation available") diff --git a/src/app/modules/main/module.nim b/src/app/modules/main/module.nim index c9bc14094d..836155504e 100644 --- a/src/app/modules/main/module.nim +++ b/src/app/modules/main/module.nim @@ -19,6 +19,7 @@ import ../../../app_service/service/wallet_account/service as wallet_account_ser import ../../../app_service/service/setting/service as setting_service import ../../../app_service/service/bookmarks/service as bookmark_service import ../../../app_service/service/dapp_permissions/service as dapp_permissions_service +import ../../../app_service/service/provider/service as provider_service import eventemitter import ../../../app_service/service/profile/service as profile_service @@ -75,7 +76,8 @@ proc newModule*[T]( dappPermissionsService: dapp_permissions_service.ServiceInterface, languageService: language_service.ServiceInterface, mnemonicService: mnemonic_service.ServiceInterface, - privacyService: privacy_service.ServiceInterface + privacyService: privacy_service.ServiceInterface, + providerService: provider_service.ServiceInterface ): Module[T] = result = Module[T]() result.delegate = delegate @@ -105,8 +107,8 @@ proc newModule*[T]( settingService ) - result.browserSectionModule = browser_section_module.newModule(result, bookmarkService, settingsService, dappPermissionsService) - result.profileSectionModule = profile_section_module.newModule(result,events, accountsService, settingsService, profileService, contactsService, aboutService, languageService, mnemonicService, privacyService) + result.browserSectionModule = browser_section_module.newModule(result, bookmarkService, settingsService, dappPermissionsService, providerService) + result.profileSectionModule = profile_section_module.newModule(result, events, accountsService, settingsService, profileService, contactsService, aboutService, languageService, mnemonicService, privacyService) method delete*[T](self: Module[T]) = self.chatSectionModule.delete diff --git a/src/app/provider/core.nim b/src/app/provider/core.nim deleted file mode 100644 index 555549c438..0000000000 --- a/src/app/provider/core.nim +++ /dev/null @@ -1,24 +0,0 @@ -import NimQml, chronicles -import status/status -import view - -logScope: - topics = "web3-provider" - -type Web3ProviderController* = ref object - status*: Status - view*: Web3ProviderView - variant*: QVariant - -proc newController*(status: Status): Web3ProviderController = - result = Web3ProviderController() - result.status = status - result.view = newWeb3ProviderView(status) - result.variant = newQVariant(result.view) - -proc delete*(self: Web3ProviderController) = - delete self.variant - delete self.view - -proc init*(self: Web3ProviderController) = - discard \ No newline at end of file diff --git a/src/app/provider/view.nim b/src/app/provider/view.nim deleted file mode 100644 index f2710866e7..0000000000 --- a/src/app/provider/view.nim +++ /dev/null @@ -1,42 +0,0 @@ -import NimQml -import status/[status, ens, chat/stickers, wallet, settings, provider] -import status/types/[setting, permission] -import json, json_serialization, sets, strutils -import chronicles -import stew/byteutils - -logScope: - topics = "provider-view" - -QtObject: - type Web3ProviderView* = ref object of QObject - status*: Status - - proc setup(self: Web3ProviderView) = - self.QObject.setup - - proc delete*(self: Web3ProviderView) = - self.QObject.delete - - proc newWeb3ProviderView*(status: Status): Web3ProviderView = - new(result, delete) - result = Web3ProviderView() - result.status = status - result.setup - - proc disconnect*(self: Web3ProviderView) {.slot.} = - self.status.permissions.revoke("web3".toPermission()) - - proc postMessage*(self: Web3ProviderView, message: string): string {.slot.} = - result = self.status.provider.postMessage(message) - - proc hasPermission*(self: Web3ProviderView, hostname: string, permission: string): bool {.slot.} = - result = self.status.permissions.hasPermission(hostname, permission.toPermission()) - - proc clearPermissions*(self: Web3ProviderView): string {.slot.} = - self.status.permissions.clearPermissions() - - proc ensResourceURL*(self: Web3ProviderView, ens: string, url: string): string {.slot.} = - let (url, base, http_scheme, path_prefix, hasContentHash) = self.status.provider.ensResourceURL(ens, url) - result = url_replaceHostAndAddPath(url, (if hasContentHash: base else: url_host(base)), http_scheme, path_prefix) - diff --git a/src/app_service/service/dapp_permissions/service.nim b/src/app_service/service/dapp_permissions/service.nim index d715e4317d..dbbbcc2760 100644 --- a/src/app_service/service/dapp_permissions/service.nim +++ b/src/app_service/service/dapp_permissions/service.nim @@ -88,8 +88,10 @@ method revoke*(self: Service, dapp: string, permission: Permission): bool = method addPermission*(self: Service, dapp: string, permission: Permission): R = try: if not self.dapps.hasKey(dapp): - result.err "not found" - return + self.dapps[dapp] = Dapp( + name: dapp, + permissions: initHashSet[Permission]() + ) self.dapps[dapp].permissions.incl(permission) discard status_go.addDappPermissions(dapp, self.dapps[dapp].permissions.toSeq().mapIt($it)) diff --git a/src/app_service/service/ens/service.nim b/src/app_service/service/ens/service.nim new file mode 100644 index 0000000000..1f6aea13ca --- /dev/null +++ b/src/app_service/service/ens/service.nim @@ -0,0 +1,75 @@ +import Tables, json, chronicles, strutils +import sets +import options +import chronicles, libp2p/[multihash, multicodec, cid] +import nimcrypto +include ../../common/json_utils +import service_interface +import status/statusgo_backend_new/ens as status_go +export service_interface + +logScope: + topics = "ens-service" + +type + Service* = ref object of ServiceInterface + +method delete*(self: Service) = + discard + +proc newService*(): Service = + result = Service() + +method init*(self: Service) = + discard + +method getContentHash*(self: Service, ens: string): Option[string] = + try: + let contentHash = status_go.contenthash(ens) + if contentHash != "": + return some(contentHash) + except Exception as e: + let errDescription = e.msg + error "error: ", errDescription + return none(string) + +method decodeENSContentHash*(self: Service, value: string): tuple[ensType: ENSType, output: string] = + if value == "": + return (ENSType.UNKNOWN, "") + + if value[0..5] == "e40101": + return (ENSType.SWARM, value.split("1b20")[1]) + + if value[0..7] == "e3010170": + try: + let defaultCodec = parseHexInt("70") #dag-pb + var codec = defaultCodec # no codec specified + var codecStartIdx = 2 # idx of where codec would start if it was specified + # handle the case when starts with 0xe30170 instead of 0xe3010170 + if value[2..5] == "0101": + codecStartIdx = 6 + codec = parseHexInt(value[6..7]) + elif value[2..3] == "01" and value[4..5] != "12": + codecStartIdx = 4 + codec = parseHexInt(value[4..5]) + + # strip the info we no longer need + var multiHashStr = value[codecStartIdx + 2..