2023-02-02 14:46:20 +01:00
import NimQml , sequtils , sugar , chronicles , os , uuids
2021-10-06 22:05:13 +02:00
2022-01-10 12:03:28 +01:00
import .. / .. / app_service / service / general / service as general_service
2021-10-16 18:14:08 +02:00
import .. / .. / app_service / service / keychain / service as keychain_service
2022-07-21 13:29:18 +02:00
import .. / .. / app_service / service / keycard / service as keycard_service
2021-10-12 15:48:09 +02:00
import .. / .. / app_service / service / accounts / service as accounts_service
2021-10-21 15:13:13 -04:00
import .. / .. / app_service / service / contacts / service as contacts_service
2021-10-21 14:37:21 -04:00
import .. / .. / app_service / service / language / service as language_service
2021-10-06 22:05:13 +02:00
import .. / .. / app_service / service / chat / service as chat_service
import .. / .. / app_service / service / community / service as community_service
2021-10-29 13:10:17 +02:00
import .. / .. / app_service / service / message / service as message_service
2021-10-14 09:58:28 +02:00
import .. / .. / app_service / service / token / service as token_service
2022-12-29 13:44:51 -03:00
import .. / .. / app_service / service / currency / service as currency_service
2021-10-15 10:24:32 +02:00
import .. / .. / app_service / service / transaction / service as transaction_service
2021-10-15 11:12:48 +02:00
import .. / .. / app_service / service / collectible / service as collectible_service
2021-10-18 13:00:56 +02:00
import .. / .. / app_service / service / wallet_account / service as wallet_account_service
2021-10-14 10:29:33 -04:00
import .. / .. / app_service / service / bookmarks / service as bookmark_service
2021-10-21 10:46:24 -04:00
import .. / .. / app_service / service / dapp_permissions / service as dapp_permissions_service
2021-10-22 14:26:11 -04:00
import .. / .. / app_service / service / privacy / service as privacy_service
2021-10-26 10:36:31 -04:00
import .. / .. / app_service / service / provider / service as provider_service
2021-11-15 13:17:41 +03:00
import .. / .. / app_service / service / node / service as node_service
2021-10-13 17:31:04 -04:00
import .. / .. / app_service / service / profile / service as profile_service
import .. / .. / app_service / service / settings / service as settings_service
2021-11-25 12:12:19 -05:00
import .. / .. / app_service / service / stickers / service as stickers_service
2021-10-13 17:31:04 -04:00
import .. / .. / app_service / service / about / service as about_service
2021-11-24 13:09:15 +01:00
import .. / .. / app_service / service / node_configuration / service as node_configuration_service
2021-11-25 12:12:19 -05:00
import .. / .. / app_service / service / network / service as network_service
2021-12-13 14:52:55 -05:00
import .. / .. / app_service / service / activity_center / service as activity_center_service
2021-12-14 09:55:16 +01:00
import .. / .. / app_service / service / saved_address / service as saved_address_service
2021-12-27 12:04:10 +01:00
import .. / .. / app_service / service / devices / service as devices_service
2021-12-27 17:40:46 +01:00
import .. / .. / app_service / service / mailservers / service as mailservers_service
2022-01-13 09:58:42 +01:00
import .. / .. / app_service / service / gif / service as gif_service
2022-01-17 09:56:44 +01:00
import .. / .. / app_service / service / ens / service as ens_service
2023-01-09 13:51:31 +01:00
import .. / .. / app_service / service / community_tokens / service as tokens_service
2022-10-14 10:30:23 +02:00
import .. / .. / app_service / common / account_constants
2021-10-29 13:10:17 +02:00
2021-10-06 22:05:13 +02:00
import .. / modules / startup / module as startup_module
import .. / modules / main / module as main_module
2022-02-25 14:45:52 -05:00
import .. / core / notifications / notifications_manager
2021-10-06 22:05:13 +02:00
2023-02-02 14:46:20 +01:00
import .. / .. / constants as main_constants
2021-11-16 14:49:51 +01:00
import .. / global / global_singleton
2021-10-06 22:05:13 +02:00
2021-11-23 14:44:33 +01:00
import .. / core / [ main ]
2022-05-19 12:24:53 +02:00
logScope :
topics = " app-controller "
2022-02-09 10:43:23 +01:00
type
AppController * = ref object of RootObj
2023-01-23 12:41:59 +01:00
storeDefaultKeyPair : bool
syncKeycardBasedOnAppWalletState : bool
changedKeycardUids : seq [ tuple [ oldKcUid : string , newKcUid : string ] ] # used in case user unlocked keycard during onboarding using seed phrase
2021-11-17 14:13:20 +01:00
statusFoundation : StatusFoundation
2022-02-25 14:45:52 -05:00
notificationsManager * : NotificationsManager
2023-02-02 14:46:20 +01:00
keychainConnectionIds : seq [ UUID ]
2022-02-09 10:43:23 +01:00
2021-11-17 13:57:19 +01:00
# Global
2022-05-10 15:15:56 +02:00
appSettingsVariant : QVariant
2021-11-17 13:57:19 +01:00
localAppSettingsVariant : QVariant
localAccountSettingsVariant : QVariant
localAccountSensitiveSettingsVariant : QVariant
userProfileVariant : QVariant
2021-12-14 17:11:31 +01:00
globalUtilsVariant : QVariant
2021-11-17 13:57:19 +01:00
2021-10-06 22:05:13 +02:00
# Services
2022-01-10 12:03:28 +01:00
generalService : general_service . Service
2022-07-21 13:29:18 +02:00
keycardService * : keycard_service . Service
2021-10-16 18:14:08 +02:00
keychainService : keychain_service . Service
2021-10-12 15:48:09 +02:00
accountsService : accounts_service . Service
2021-10-21 15:13:13 -04:00
contactsService : contacts_service . Service
2021-10-06 22:05:13 +02:00
chatService : chat_service . Service
communityService : community_service . Service
2021-10-29 13:10:17 +02:00
messageService : message_service . Service
2021-10-14 09:58:28 +02:00
tokenService : token_service . Service
2022-12-29 13:44:51 -03:00
currencyService : currency_service . Service
2021-10-15 10:24:32 +02:00
transactionService : transaction_service . Service
2021-10-15 11:12:48 +02:00
collectibleService : collectible_service . Service
2021-10-18 13:00:56 +02:00
walletAccountService : wallet_account_service . Service
2021-10-14 10:29:33 -04:00
bookmarkService : bookmark_service . Service
2021-10-21 10:46:24 -04:00
dappPermissionsService : dapp_permissions_service . Service
2021-10-26 10:36:31 -04:00
providerService : provider_service . Service
2021-10-13 17:31:04 -04:00
profileService : profile_service . Service
settingsService : settings_service . Service
2021-11-25 12:12:19 -05:00
stickersService : stickers_service . Service
2021-10-13 17:31:04 -04:00
aboutService : about_service . Service
2021-11-25 12:12:19 -05:00
networkService : network_service . Service
2021-12-13 14:52:55 -05:00
activityCenterService : activity_center_service . Service
2021-10-21 14:37:21 -04:00
languageService : language_service . Service
2021-12-30 13:39:47 +01:00
# mnemonicService: mnemonic_service.Service
2021-10-22 14:26:11 -04:00
privacyService : privacy_service . Service
2021-11-24 13:09:15 +01:00
nodeConfigurationService : node_configuration_service . Service
2021-12-14 09:55:16 +01:00
savedAddressService : saved_address_service . Service
2021-12-27 12:04:10 +01:00
devicesService : devices_service . Service
2021-12-27 17:40:46 +01:00
mailserversService : mailservers_service . Service
2021-11-15 13:17:41 +03:00
nodeService : node_service . Service
2022-01-13 09:58:42 +01:00
gifService : gif_service . Service
2022-01-17 09:56:44 +01:00
ensService : ens_service . Service
2023-01-09 13:51:31 +01:00
tokensService : tokens_service . Service
2021-10-20 11:50:50 +02:00
2021-10-13 17:31:04 -04:00
# Modules
2021-10-19 09:19:03 +02:00
startupModule : startup_module . AccessInterface
2021-10-13 17:31:04 -04:00
mainModule : main_module . AccessInterface
2021-10-06 22:05:13 +02:00
#################################################
# Forward declaration section
2021-10-29 19:27:14 +02:00
proc load ( self : AppController )
proc buildAndRegisterLocalAccountSensitiveSettings ( self : AppController )
proc buildAndRegisterUserProfile ( self : AppController )
2021-10-06 22:05:13 +02:00
# Startup Module Delegate Interface
proc startupDidLoad * ( self : AppController )
2022-11-30 10:25:47 +01:00
proc userLoggedIn * ( self : AppController ) : string
2022-12-01 18:26:16 -04:00
proc logout * ( self : AppController )
2022-11-30 10:25:47 +01:00
proc finishAppLoading * ( self : AppController )
2023-01-23 12:41:59 +01:00
proc storeDefaultKeyPairForNewKeycardUser * ( self : AppController )
proc syncKeycardBasedOnAppWalletStateAfterLogin * ( self : AppController )
proc addToKeycardUidPairsToCheckForAChangeAfterLogin * ( self : AppController , oldKeycardUid : string , newKeycardUid : string )
proc removeAllKeycardUidPairsForCheckingForAChangeAfterLogin * ( self : AppController )
2021-10-06 22:05:13 +02:00
# Main Module Delegate Interface
proc mainDidLoad * ( self : AppController )
#################################################
2021-10-16 16:42:40 +02:00
proc connect ( self : AppController ) =
2022-01-10 12:03:28 +01:00
self . statusFoundation . events . once ( " nodeStopped " ) do ( a : Args ) :
# not sure, but maybe we should take some actions when node stops
discard
2021-10-06 22:05:13 +02:00
2023-01-03 21:17:13 +01:00
# Handle runtime log level settings changes
if not existsEnv ( " LOG_LEVEL " ) :
self . statusFoundation . events . on ( node_configuration_service . SIGNAL_NODE_LOG_LEVEL_UPDATE ) do ( a : Args ) :
let args = NodeLogLevelUpdatedArgs ( a )
if args . logLevel = = LogLevel . DEBUG :
setLogLevel ( LogLevel . DEBUG )
elif defined ( production ) :
setLogLevel ( LogLevel . INFO )
2021-11-17 14:13:20 +01:00
proc newAppController * ( statusFoundation : StatusFoundation ) : AppController =
2021-10-06 22:05:13 +02:00
result = AppController ( )
2023-01-23 12:41:59 +01:00
result . storeDefaultKeyPair = false
result . syncKeycardBasedOnAppWalletState = false
2021-11-17 14:13:20 +01:00
result . statusFoundation = statusFoundation
2022-05-10 15:15:56 +02:00
# Preparing settings service to be exposed later as global QObject
2022-06-14 13:27:23 +02:00
result . settingsService = settings_service . newService ( statusFoundation . events )
2022-05-10 15:15:56 +02:00
result . appSettingsVariant = newQVariant ( result . settingsService )
result . notificationsManager = newNotificationsManager ( statusFoundation . events , result . settingsService )
2021-11-17 13:57:19 +01:00
# Global
result . localAppSettingsVariant = newQVariant ( singletonInstance . localAppSettings )
result . localAccountSettingsVariant = newQVariant ( singletonInstance . localAccountSettings )
result . localAccountSensitiveSettingsVariant = newQVariant ( singletonInstance . localAccountSensitiveSettings )
result . userProfileVariant = newQVariant ( singletonInstance . userProfile )
2022-05-10 15:15:56 +02:00
result . globalUtilsVariant = newQVariant ( singletonInstance . utils )
2021-11-17 13:57:19 +01:00
2021-10-06 22:05:13 +02:00
# Services
2022-12-22 12:49:27 +01:00
result . generalService = general_service . newService ( statusFoundation . events , statusFoundation . threadpool )
2022-09-30 20:49:54 +04:00
result . activityCenterService = activity_center_service . newService ( statusFoundation . events , statusFoundation . threadpool )
2022-07-21 13:29:18 +02:00
result . keycardService = keycard_service . newService ( statusFoundation . events , statusFoundation . threadpool )
2022-02-09 10:43:23 +01:00
result . nodeConfigurationService = node_configuration_service . newService ( statusFoundation . fleetConfiguration ,
2022-11-21 13:51:39 +01:00
result . settingsService , statusFoundation . events )
2022-01-10 12:03:28 +01:00
result . keychainService = keychain_service . newService ( statusFoundation . events )
2022-11-14 11:24:16 +01:00
result . accountsService = accounts_service . newService ( statusFoundation . events , statusFoundation . threadpool ,
statusFoundation . fleetConfiguration )
2022-02-17 10:15:37 +01:00
result . networkService = network_service . newService ( statusFoundation . events , result . settingsService )
2022-02-08 14:32:21 +01:00
result . contactsService = contacts_service . newService (
2022-09-30 20:49:54 +04:00
statusFoundation . events , statusFoundation . threadpool , result . networkService , result . settingsService ,
result . activityCenterService
2022-02-08 14:32:21 +01:00
)
2023-01-30 16:05:34 -05:00
result . chatService = chat_service . newService ( statusFoundation . events , statusFoundation . threadpool , result . contactsService )
2022-02-08 14:32:21 +01:00
result . tokenService = token_service . newService (
2022-04-04 14:11:39 +02:00
statusFoundation . events , statusFoundation . threadpool , result . networkService
2022-02-08 14:32:21 +01:00
)
2022-12-29 13:44:51 -03:00
result . currencyService = currency_service . newService ( result . tokenService , result . settingsService )
2022-11-15 10:13:08 +01:00
result . collectibleService = collectible_service . newService ( statusFoundation . events , statusFoundation . threadpool , result . networkService )
2022-02-10 13:10:56 +01:00
result . walletAccountService = wallet_account_service . newService (
2022-05-12 17:24:03 +02:00
statusFoundation . events , statusFoundation . threadpool , result . settingsService , result . accountsService ,
result . tokenService , result . networkService ,
2022-02-10 13:10:56 +01:00
)
2022-03-21 11:32:53 +01:00
result . messageService = message_service . newService (
statusFoundation . events , statusFoundation . threadpool , result . contactsService , result . tokenService , result . walletAccountService , result . networkService
)
2022-12-06 15:18:42 -05:00
result . communityService = community_service . newService ( statusFoundation . events ,
statusFoundation . threadpool , result . chatService , result . activityCenterService , result . messageService )
2022-06-07 15:57:09 +02:00
result . transactionService = transaction_service . newService ( statusFoundation . events , statusFoundation . threadpool , result . networkService , result . settingsService , result . tokenService )
2022-06-08 21:40:02 +02:00
result . bookmarkService = bookmark_service . newService ( statusFoundation . events )
2022-11-30 10:25:47 +01:00
result . profileService = profile_service . newService ( statusFoundation . events , result . settingsService )
2021-11-25 12:12:19 -05:00
result . stickersService = stickers_service . newService (
2022-01-10 12:03:28 +01:00
statusFoundation . events ,
2021-11-25 12:12:19 -05:00
statusFoundation . threadpool ,
result . settingsService ,
result . walletAccountService ,
result . transactionService ,
result . networkService ,
2022-02-14 19:27:23 -04:00
result . chatService ,
result . tokenService
2021-11-25 12:12:19 -05:00
)
2022-05-31 11:36:05 +02:00
result . aboutService = about_service . newService ( statusFoundation . events , statusFoundation . threadpool )
2021-10-21 10:46:24 -04:00
result . dappPermissionsService = dapp_permissions_service . newService ( )
2022-07-22 14:23:11 +02:00
result . languageService = language_service . newService ( statusFoundation . events )
2021-12-30 13:39:47 +01:00
# result.mnemonicService = mnemonic_service.newService()
2022-02-09 10:43:23 +01:00
result . privacyService = privacy_service . newService ( statusFoundation . events , result . settingsService ,
2021-12-30 13:39:47 +01:00
result . accountsService )
2022-10-20 15:33:23 +02:00
result . savedAddressService = saved_address_service . newService ( statusFoundation . events , result . networkService )
2022-01-10 12:03:28 +01:00
result . devicesService = devices_service . newService ( statusFoundation . events , result . settingsService )
2022-02-09 10:43:23 +01:00
result . mailserversService = mailservers_service . newService ( statusFoundation . events , statusFoundation . threadpool ,
2022-01-28 19:18:30 -04:00
result . settingsService , result . nodeConfigurationService , statusFoundation . fleetConfiguration )
2023-01-24 19:37:27 -04:00
result . nodeService = node_service . newService ( statusFoundation . events , result . settingsService , result . nodeConfigurationService )
2022-01-13 09:58:42 +01:00
result . gifService = gif_service . newService ( result . settingsService )
2022-03-09 11:27:32 +01:00
result . ensService = ens_service . newService ( statusFoundation . events , statusFoundation . threadpool ,
2022-03-21 11:32:53 +01:00
result . settingsService , result . walletAccountService , result . transactionService ,
2022-02-09 11:39:10 -05:00
result . networkService , result . tokenService )
2023-01-09 13:51:31 +01:00
result . tokensService = tokens_service . newService ( statusFoundation . events , statusFoundation . threadpool ,
result . networkService , result . transactionService )
2022-03-29 12:21:12 +02:00
result . providerService = provider_service . newService ( statusFoundation . events , statusFoundation . threadpool , result . ensService )
2021-10-14 09:58:28 +02:00
2021-10-06 22:05:13 +02:00
# Modules
2021-10-19 09:19:03 +02:00
result . startupModule = startup_module . newModule [ AppController ] (
result ,
2022-01-10 12:03:28 +01:00
statusFoundation . events ,
2022-02-09 10:43:23 +01:00
result . keychainService ,
2022-03-22 10:29:59 +01:00
result . accountsService ,
2022-07-20 14:34:44 +02:00
result . generalService ,
2022-07-21 13:29:18 +02:00
result . profileService ,
result . keycardService
2021-10-19 09:19:03 +02:00
)
2021-10-15 10:24:32 +02:00
result . mainModule = main_module . newModule [ AppController ] (
2021-12-14 09:55:16 +01:00
result ,
2022-01-10 12:03:28 +01:00
statusFoundation . events ,
2022-10-20 11:05:10 +02:00
statusFoundation . urlsManager ,
2021-10-15 10:24:32 +02:00
result . keychainService ,
2021-12-14 09:55:16 +01:00
result . accountsService ,
2021-10-15 10:24:32 +02:00
result . chatService ,
result . communityService ,
2021-10-29 13:10:17 +02:00
result . messageService ,
2021-10-15 10:24:32 +02:00
result . tokenService ,
2022-12-29 13:44:51 -03:00
result . currencyService ,
2021-10-15 11:12:48 +02:00
result . transactionService ,
2021-10-18 13:00:56 +02:00
result . collectibleService ,
2021-10-19 11:03:52 +02:00
result . walletAccountService ,
2021-10-14 10:29:33 -04:00
result . bookmarkService ,
2021-10-13 17:31:04 -04:00
result . profileService ,
result . settingsService ,
2021-10-21 15:13:13 -04:00
result . contactsService ,
2021-10-13 17:31:04 -04:00
result . aboutService ,
2021-10-21 10:46:24 -04:00
result . dappPermissionsService ,
2021-10-21 18:31:41 -04:00
result . languageService ,
2021-12-30 13:39:47 +01:00
# result.mnemonicService,
2021-10-26 10:36:31 -04:00
result . privacyService ,
result . providerService ,
2021-12-13 14:52:55 -05:00
result . stickersService ,
2021-12-14 09:55:16 +01:00
result . activityCenterService ,
2021-12-14 19:47:32 +01:00
result . savedAddressService ,
2021-12-27 12:04:10 +01:00
result . nodeConfigurationService ,
2021-12-27 17:40:46 +01:00
result . devicesService ,
2021-11-15 13:17:41 +03:00
result . mailserversService ,
2022-01-13 09:58:42 +01:00
result . nodeService ,
result . gifService ,
2022-02-11 10:44:49 +01:00
result . ensService ,
2023-01-09 13:51:31 +01:00
result . tokensService ,
2022-02-11 10:44:49 +01:00
result . networkService ,
2022-08-16 11:19:15 +02:00
result . generalService ,
result . keycardService
2021-10-15 10:24:32 +02:00
)
2021-10-06 22:05:13 +02:00
2021-12-13 11:49:54 +01:00
# Do connections
2021-10-16 16:42:40 +02:00
result . connect ( )
2021-10-06 22:05:13 +02:00
proc delete * ( self : AppController ) =
2023-01-24 16:10:04 -04:00
info " logging out... "
self . generalService . logout ( )
2022-01-12 00:16:17 +01:00
singletonInstance . delete
2022-02-25 14:45:52 -05:00
self . notificationsManager . delete
2021-12-30 13:39:47 +01:00
self . keychainService . delete
2021-10-21 15:13:13 -04:00
self . contactsService . delete
2021-10-14 10:29:33 -04:00
self . bookmarkService . delete
2022-01-13 09:58:42 +01:00
self . gifService . delete
2022-09-02 13:03:40 +02:00
if not self . startupModule . isNil :
self . startupModule . delete
2021-10-06 22:05:13 +02:00
self . mainModule . delete
2021-12-29 10:53:56 +01:00
self . languageService . delete
2022-02-09 10:43:23 +01:00
2022-05-10 15:15:56 +02:00
self . appSettingsVariant . delete
2021-10-20 12:55:10 +02:00
self . localAppSettingsVariant . delete
2021-10-16 21:54:00 +02:00
self . localAccountSettingsVariant . delete
2021-10-20 11:50:50 +02:00
self . localAccountSensitiveSettingsVariant . delete
2021-10-29 19:27:14 +02:00
self . userProfileVariant . delete
2021-12-14 17:11:31 +01:00
self . globalUtilsVariant . delete
2021-10-16 21:54:00 +02:00
2021-10-12 15:48:09 +02:00
self . accountsService . delete
self . chatService . delete
self . communityService . delete
2022-12-29 13:44:51 -03:00
self . currencyService . delete
2021-10-19 09:19:03 +02:00
self . tokenService . delete
self . transactionService . delete
self . collectibleService . delete
self . walletAccountService . delete
2021-10-13 17:31:04 -04:00
self . aboutService . delete
2021-11-25 12:12:19 -05:00
self . networkService . delete
2021-12-13 14:52:55 -05:00
self . activityCenterService . delete
2021-10-21 10:46:24 -04:00
self . dappPermissionsService . delete
2021-10-26 10:36:31 -04:00
self . providerService . delete
2021-11-24 13:09:15 +01:00
self . nodeConfigurationService . delete
2021-11-15 13:17:41 +03:00
self . nodeService . delete
2021-11-25 12:12:19 -05:00
self . settingsService . delete
self . stickersService . delete
2021-12-14 09:55:16 +01:00
self . savedAddressService . delete
2021-12-27 12:04:10 +01:00
self . devicesService . delete
2021-12-27 17:40:46 +01:00
self . mailserversService . delete
2021-12-30 13:39:47 +01:00
self . messageService . delete
self . privacyService . delete
self . profileService . delete
2022-01-10 12:03:28 +01:00
self . generalService . delete
2022-01-17 09:56:44 +01:00
self . ensService . delete
2023-01-09 13:51:31 +01:00
self . tokensService . delete
2022-01-26 20:31:45 +01:00
self . gifService . delete
2022-07-21 13:29:18 +02:00
self . keycardService . delete
2021-10-06 22:05:13 +02:00
2023-02-02 14:46:20 +01:00
proc disconnectKeychain ( self : AppController ) =
for id in self . keychainConnectionIds :
self . statusFoundation . events . disconnect ( id )
self . keychainConnectionIds = @ [ ]
proc connectKeychain ( self : AppController ) =
var handlerId = self . statusFoundation . events . onWithUUID ( SIGNAL_KEYCHAIN_SERVICE_SUCCESS ) do ( e : Args ) :
let args = KeyChainServiceArg ( e )
self . disconnectKeychain ( )
## we need to set local `storeToKeychain` prop to `store` value since in this context means pass/pin is stored well
singletonInstance . localAccountSettings . setStoreToKeychainValue ( LS_VALUE_STORE )
self . keychainConnectionIds . add ( handlerId )
handlerId = self . statusFoundation . events . onWithUUID ( SIGNAL_KEYCHAIN_SERVICE_ERROR ) do ( e : Args ) :
let args = KeyChainServiceArg ( e )
self . disconnectKeychain ( )
## no need for any other activity in this context, local `storeToKeychain` prop remains as it was
## maybe in some point in future we add a popup letting user know about this
info " unable to store the data to keychain " , errCode = args . errCode , errType = args . errType , errDesc = args . errDescription
self . keychainConnectionIds . add ( handlerId )
proc checkForStoringPasswordToKeychain ( self : AppController ) =
## This proc is used to store pass/pin depends on user's selection during onboarding flow.
let account = self . accountsService . getLoggedInAccount ( )
let value = singletonInstance . localAccountSettings . getStoreToKeychainValue ( )
if not main_constants . IS_MACOS or # This is MacOS only feature
value = = LS_VALUE_STORE or # means pass is already stored, no need to store it again
value = = LS_VALUE_NEVER or # means pass doesn't need to be stored at all
account . name . len = = 0 :
return
# We are here if stored "storeToKeychain" property for the logged in user is either empty or set to "NotNow".
#TODO: we should store PubKey of this account instead of display name (display name is not unique)
# and we may run into a problem if 2 accounts with the same display name are generated.
self . connectKeychain ( )
let pass = self . startupModule . getPassword ( )
if pass . len > 0 :
self . keychainService . storeData ( account . name , pass )
else :
self . keychainService . storeData ( account . name , self . startupModule . getPin ( ) )
2021-10-06 22:05:13 +02:00
proc startupDidLoad * ( self : AppController ) =
2021-10-20 12:55:10 +02:00
singletonInstance . engine . setRootContextProperty ( " localAppSettings " , self . localAppSettingsVariant )
2021-10-20 11:50:50 +02:00
singletonInstance . engine . setRootContextProperty ( " localAccountSettings " , self . localAccountSettingsVariant )
2022-03-01 20:14:20 -04:00
singletonInstance . engine . setRootContextProperty ( " globalUtils " , self . globalUtilsVariant )
2021-10-16 16:42:40 +02:00
singletonInstance . engine . load ( newQUrl ( " qrc:///main.qml " ) )
2021-10-06 22:05:13 +02:00
2021-12-29 10:53:56 +01:00
# We need to init a language service once qml is loaded
self . languageService . init ( )
2022-01-27 16:29:17 +01:00
# We need this to set app width/height appropriatelly on the app start.
self . startupModule . startUpUIRaised ( )
2021-10-20 12:55:10 +02:00
2021-10-06 22:05:13 +02:00
proc mainDidLoad * ( self : AppController ) =
2021-10-14 10:04:15 +02:00
self . startupModule . moveToAppState ( )
2023-02-02 14:46:20 +01:00
self . checkForStoringPasswordToKeychain ( )
2021-10-16 21:03:01 +02:00
2021-10-06 22:05:13 +02:00
proc start * ( self : AppController ) =
2022-07-21 13:29:18 +02:00
self . keycardService . init ( )
2022-01-27 16:29:17 +01:00
self . keychainService . init ( )
2022-01-10 12:03:28 +01:00
self . generalService . init ( )
2021-10-12 15:48:09 +02:00
self . accountsService . init ( )
2022-02-09 10:43:23 +01:00
2021-10-12 15:48:09 +02:00
self . startupModule . load ( )
2021-10-06 22:05:13 +02:00
2021-11-22 16:31:10 +01:00
proc load ( self : AppController ) =
2022-02-25 14:45:52 -05:00
self . notificationsManager . init ( )
2021-11-22 19:40:03 +01:00
self . settingsService . init ( )
2022-11-30 10:25:47 +01:00
self . profileService . init ( )
2021-11-24 13:09:15 +01:00
self . nodeConfigurationService . init ( )
2022-11-03 09:03:10 -04:00
self . mailserversService . init ( )
2021-10-21 15:13:13 -04:00
self . contactsService . init ( )
2021-10-06 22:05:13 +02:00
self . chatService . init ( )
2022-01-05 13:50:01 -05:00
self . messageService . init ( )
2021-10-06 22:05:13 +02:00
self . communityService . init ( )
2021-10-14 10:29:33 -04:00
self . bookmarkService . init ( )
2021-10-21 10:46:24 -04:00
self . dappPermissionsService . init ( )
2021-10-26 10:36:31 -04:00
self . providerService . init ( )
2021-10-20 10:02:17 +02:00
self . transactionService . init ( )
2021-11-25 12:12:19 -05:00
self . stickersService . init ( )
2021-12-13 14:52:55 -05:00
self . activityCenterService . init ( )
2021-12-14 09:55:16 +01:00
self . savedAddressService . init ( )
2021-12-20 13:21:21 -05:00
self . aboutService . init ( )
2021-12-27 12:04:10 +01:00
self . devicesService . init ( )
2022-01-17 09:56:44 +01:00
self . ensService . init ( )
2023-01-09 13:51:31 +01:00
self . tokensService . init ( )
2022-01-26 20:31:45 +01:00
self . gifService . init ( )
2021-10-29 19:27:14 +02:00
2022-05-10 15:15:56 +02:00
# Accessible after user login
singletonInstance . engine . setRootContextProperty ( " appSettings " , self . appSettingsVariant )
2021-12-14 17:11:31 +01:00
singletonInstance . engine . setRootContextProperty ( " globalUtils " , self . globalUtilsVariant )
2022-03-17 11:28:38 +01:00
2022-02-09 10:43:23 +01:00
self . buildAndRegisterLocalAccountSensitiveSettings ( )
2021-10-29 19:27:14 +02:00
self . buildAndRegisterUserProfile ( )
2022-03-17 11:28:38 +01:00
2022-03-10 10:51:12 +01:00
self . networkService . init ( )
self . tokenService . init ( )
2022-12-29 13:44:51 -03:00
self . currencyService . init ( )
2022-03-10 10:51:12 +01:00
self . walletAccountService . init ( )
2021-10-29 19:27:14 +02:00
2023-01-03 21:17:13 +01:00
# Apply runtime log level settings
if not existsEnv ( " LOG_LEVEL " ) :
if self . nodeConfigurationService . isDebugEnabled ( ) :
setLogLevel ( LogLevel . DEBUG )
2021-10-29 19:27:14 +02:00
# load main module
2021-11-25 12:12:19 -05:00
self . mainModule . load (
2022-01-10 12:03:28 +01:00
self . statusFoundation . events ,
2021-12-21 10:26:13 +01:00
self . settingsService ,
2022-11-30 14:47:57 -04:00
self . nodeConfigurationService ,
2021-11-30 15:49:45 +01:00
self . contactsService ,
2021-11-25 12:12:19 -05:00
self . chatService ,
self . communityService ,
2022-01-13 09:58:42 +01:00
self . messageService ,
self . gifService ,
2022-03-09 11:27:32 +01:00
self . mailserversService ,
2021-11-25 12:12:19 -05:00
)
2021-10-14 10:04:15 +02:00
2022-11-30 10:25:47 +01:00
proc userLoggedIn * ( self : AppController ) : string =
try :
self . generalService . startMessenger ( )
self . statusFoundation . userLoggedIn ( )
return " "
except Exception as e :
let errDescription = e . msg
error " error: " , errDescription
return errDescription
proc finishAppLoading * ( self : AppController ) =
2021-10-14 10:29:33 -04:00
self . load ( )
2021-10-29 19:27:14 +02:00
2021-12-30 13:39:47 +01:00
# Once user is logged in and main module is loaded we need to check if it gets here importing mnemonic or not
# and delete mnemonic in the first case.
let importedAccount = self . accountsService . getImportedAccount ( )
if ( importedAccount . isValid ( ) ) :
self . privacyService . removeMnemonic ( )
2022-09-02 13:03:40 +02:00
if not self . startupModule . isNil :
self . startupModule . delete
2022-12-01 18:26:16 -04:00
proc logout * ( self : AppController ) =
self . generalService . logout ( )
2022-02-09 10:43:23 +01:00
proc buildAndRegisterLocalAccountSensitiveSettings ( self : AppController ) =
2021-11-22 19:40:03 +01:00
var pubKey = self . settingsService . getPublicKey ( )
2021-10-29 19:27:14 +02:00
singletonInstance . localAccountSensitiveSettings . setFileName ( pubKey )
singletonInstance . engine . setRootContextProperty ( " localAccountSensitiveSettings " , self . localAccountSensitiveSettingsVariant )
2022-02-09 10:43:23 +01:00
proc buildAndRegisterUserProfile ( self : AppController ) =
2021-11-22 19:40:03 +01:00
let pubKey = self . settingsService . getPublicKey ( )
2022-05-19 12:24:53 +02:00
let alias = self . settingsService . getName ( )
2022-12-13 12:37:27 +03:00
var preferredName = self . settingsService . getPreferredName ( )
let displayName = self . settingsService . getDisplayName ( )
2022-06-10 11:01:31 +02:00
let currentUserStatus = self . settingsService . getCurrentUserStatus ( )
2021-11-22 19:40:03 +01:00
let loggedInAccount = self . accountsService . getLoggedInAccount ( )
var thumbnail , large : string
for img in loggedInAccount . images :
if ( img . imgType = = " large " ) :
large = img . uri
elif ( img . imgType = = " thumbnail " ) :
thumbnail = img . uri
2022-09-26 13:01:26 +02:00
singletonInstance . userProfile . setFixedData ( alias , loggedInAccount . keyUid , pubKey , loggedInAccount . keycardPairing . len > 0 )
2022-03-01 20:14:20 -04:00
singletonInstance . userProfile . setDisplayName ( displayName )
2021-12-29 16:09:29 +01:00
singletonInstance . userProfile . setPreferredName ( preferredName )
2021-11-22 19:40:03 +01:00
singletonInstance . userProfile . setThumbnailImage ( thumbnail )
singletonInstance . userProfile . setLargeImage ( large )
2022-06-10 11:01:31 +02:00
singletonInstance . userProfile . setCurrentUserStatus ( currentUserStatus . statusType . int )
2021-10-29 19:27:14 +02:00
2022-01-26 20:31:45 +01:00
singletonInstance . engine . setRootContextProperty ( " userProfile " , self . userProfileVariant )
2022-10-14 10:30:23 +02:00
2023-01-23 12:41:59 +01:00
############################################################################## store def kc sync with app kc uid
## Onboarding flows sync keycard state after login keypair | (inc. kp store) | update
## `I’ m new to Status` -> `Generate new keys` -> na | na | na
## `I’ m new to Status` -> `Generate keys for a new Keycard` -> yes | no | no
## `I’ m new to Status` -> `Import a seed phrase` -> `Import a seed phrase` -> na | na | na
## `I’ m new to Status` -> `Import a seed phrase` -> `Import a seed phrase into a new Keycard` -> yes | no | no
##
## `I already use Status` -> `Scan sync code` -> flow not developed yet
## `I already use Status` -> `I don’ t have other device` -> `Login with Keycard` (fetched) -> no | yes | no
## `I already use Status` -> `I don’ t have other device` -> `Login with Keycard` (unlock via puk, fetched) -> no | yes | no
## `I already use Status` -> `I don’ t have other device` -> `Login with Keycard` (unlock via seed phrase, fetched) -> no | yes | yes (kc details should be fetched and set to db while recovering, that's the reason why)
## `I already use Status` -> `I don’ t have other device` -> `Login with Keycard` (not fetched) -> no | yes | no
## `I already use Status` -> `I don’ t have other device` -> `Login with Keycard` (unlock via puk, not fetched) -> no | yes | no
## `I already use Status` -> `I don’ t have other device` -> `Login with Keycard` (unlock via seed phrase, not fetched) -> no | yes | no
## `I already use Status` -> `I don’ t have other device` -> `Enter a seed phrase` -> na | na | na
##
## `Login` -> na | na | na
## `Login` -> if card was unlocked via puk -> na | na | na
## `Login` -> if card was unlocked via seed phrase -> no | no | yes
## `Login` -> `Create replacement Keycard with seed phrase` -> no | yes | no (we don't know which kc is replaced if user has more kc for the same kp)
##############################################################################
2023-01-18 12:53:38 +01:00
if singletonInstance . userProfile . getIsKeycardUser ( ) :
2023-01-23 12:41:59 +01:00
if self . storeDefaultKeyPair :
2023-01-18 12:53:38 +01:00
let allAccounts = self . walletAccountService . fetchAccounts ( )
let defaultWalletAccounts = allAccounts . filter ( a = >
a . walletType = = WalletTypeDefaultStatusAccount and
a . path = = account_constants . PATH_DEFAULT_WALLET and
not a . isChat and
a . isWallet
)
if defaultWalletAccounts . len = = 0 :
error " default wallet account was not generated "
return
let defaultWalletAddress = defaultWalletAccounts [ 0 ] . address
let keyPair = KeyPairDto ( keycardUid : self . keycardService . getLastReceivedKeycardData ( ) . flowEvent . instanceUID ,
keycardName : displayName ,
keycardLocked : false ,
accountsAddresses : @ [ defaultWalletAddress ] ,
keyUid : loggedInAccount . keyUid )
2023-01-31 20:33:56 +01:00
discard self . walletAccountService . addMigratedKeyPair ( keyPair )
2023-01-23 12:41:59 +01:00
if self . syncKeycardBasedOnAppWalletState :
2023-01-18 12:53:38 +01:00
let allAccounts = self . walletAccountService . fetchAccounts ( )
let accountsForLoggedInUser = allAccounts . filter ( a = > a . keyUid = = loggedInAccount . keyUid )
2023-01-23 12:41:59 +01:00
var keyPair = KeyPairDto ( keycardUid : " " ,
2023-01-18 12:53:38 +01:00
keycardName : displayName ,
keycardLocked : false ,
accountsAddresses : @ [ ] ,
keyUid : loggedInAccount . keyUid )
var activeValidPathsToStoreToAKeycard : seq [ string ]
for acc in accountsForLoggedInUser :
activeValidPathsToStoreToAKeycard . add ( acc . path )
2023-01-23 12:41:59 +01:00
keyPair . accountsAddresses . add ( acc . address )
2023-01-18 12:53:38 +01:00
self . keycardService . startStoreMetadataFlow ( displayName , self . startupModule . getPin ( ) , activeValidPathsToStoreToAKeycard )
## sleep for 3 seconds, since that is more than enough to store metadata to a keycard, if the reader is still plugged in
## and the card is still inserted, otherwise we just skip that.
## At the moment we're not able to sync later keycard without metadata, cause such card doesn't return instance uid for
## loaded seed phrase, that's in the notes I am taking for discussion with keycard team. If they are able to provide
## instance uid for GetMetadata flow we will be able to use SIGNAL_SHARED_KEYCARD_MODULE_TRY_KEYCARD_SYNC signal for syncing
## otherwise we need to handle that way separatelly in `handleKeycardSyncing` of shared module
sleep ( 3000 )
2023-01-23 12:41:59 +01:00
self . keycardService . cancelCurrentFlow ( )
2023-01-18 12:53:38 +01:00
let ( _ , kcEvent ) = self . keycardService . getLastReceivedKeycardData ( )
if kcEvent . instanceUID . len > 0 :
2023-01-23 12:41:59 +01:00
keyPair . keycardUid = kcEvent . instanceUID
2023-01-31 20:33:56 +01:00
discard self . walletAccountService . addMigratedKeyPair ( keyPair )
2022-10-14 10:30:23 +02:00
2023-01-23 12:41:59 +01:00
if self . changedKeycardUids . len > 0 :
let oldUid = self . changedKeycardUids [ 0 ] . oldKcUid
let newUid = self . changedKeycardUids [ ^ 1 ] . newKcUid
discard self . walletAccountService . updateKeycardUid ( oldUid , newUid )
2023-01-23 18:07:56 +01:00
discard self . walletAccountService . setKeycardUnlocked ( loggedInAccount . keyUid , newUid )
2023-01-18 12:53:38 +01:00
2023-01-23 12:41:59 +01:00
proc storeDefaultKeyPairForNewKeycardUser * ( self : AppController ) =
self . storeDefaultKeyPair = true
proc syncKeycardBasedOnAppWalletStateAfterLogin * ( self : AppController ) =
self . syncKeycardBasedOnAppWalletState = true
proc addToKeycardUidPairsToCheckForAChangeAfterLogin * ( self : AppController , oldKeycardUid : string , newKeycardUid : string ) =
self . changedKeycardUids . add ( ( oldKcUid : oldKeycardUid , newKcUid : newKeycardUid ) )
proc removeAllKeycardUidPairsForCheckingForAChangeAfterLogin * ( self : AppController ) =
self . changedKeycardUids = @ [ ]