chore(@desktop/general): accounts service updated, inherits `QtObject` class
This commit is contained in:
parent
662a11a57b
commit
db7769b072
|
@ -1,4 +1,4 @@
|
|||
import os, json, sequtils, strutils, uuids, times
|
||||
import NimQml, os, json, sequtils, strutils, uuids, times
|
||||
import json_serialization, chronicles
|
||||
|
||||
import ../../../app/global/global_singleton
|
||||
|
@ -32,9 +32,8 @@ let TEST_PEER_ENR = getEnv("TEST_PEER_ENR").string
|
|||
|
||||
include utils
|
||||
|
||||
|
||||
type
|
||||
Service* = ref object of RootObj
|
||||
QtObject:
|
||||
type Service* = ref object of QObject
|
||||
fleetConfiguration: FleetConfiguration
|
||||
generatedAccounts: seq[GeneratedAccountDto]
|
||||
accounts: seq[AccountDto]
|
||||
|
@ -44,36 +43,38 @@ type
|
|||
keyStoreDir: string
|
||||
defaultWalletEmoji: string
|
||||
|
||||
proc delete*(self: Service) =
|
||||
discard
|
||||
proc delete*(self: Service) =
|
||||
self.QObject.delete
|
||||
|
||||
proc newService*(fleetConfiguration: FleetConfiguration): Service =
|
||||
proc newService*(fleetConfiguration: FleetConfiguration): Service =
|
||||
result = Service()
|
||||
new(result, delete)
|
||||
result.QObject.setup
|
||||
result.fleetConfiguration = fleetConfiguration
|
||||
result.isFirstTimeAccountLogin = false
|
||||
result.keyStoreDir = main_constants.ROOTKEYSTOREDIR
|
||||
result.defaultWalletEmoji = ""
|
||||
|
||||
proc getLoggedInAccount*(self: Service): AccountDto =
|
||||
proc getLoggedInAccount*(self: Service): AccountDto =
|
||||
return self.loggedInAccount
|
||||
|
||||
proc getImportedAccount*(self: Service): GeneratedAccountDto =
|
||||
proc getImportedAccount*(self: Service): GeneratedAccountDto =
|
||||
return self.importedAccount
|
||||
|
||||
proc isFirstTimeAccountLogin*(self: Service): bool =
|
||||
proc isFirstTimeAccountLogin*(self: Service): bool =
|
||||
return self.isFirstTimeAccountLogin
|
||||
|
||||
proc setKeyStoreDir(self: Service, key: string) =
|
||||
proc setKeyStoreDir(self: Service, key: string) =
|
||||
self.keyStoreDir = joinPath(main_constants.ROOTKEYSTOREDIR, key) & main_constants.sep
|
||||
discard status_general.initKeystore(self.keyStoreDir)
|
||||
|
||||
proc getKeyStoreDir*(self: Service): string =
|
||||
proc getKeyStoreDir*(self: Service): string =
|
||||
return self.keyStoreDir
|
||||
|
||||
proc setDefaultWalletEmoji*(self: Service, emoji: string) =
|
||||
proc setDefaultWalletEmoji*(self: Service, emoji: string) =
|
||||
self.defaultWalletEmoji = emoji
|
||||
|
||||
proc init*(self: Service) =
|
||||
proc init*(self: Service) =
|
||||
try:
|
||||
let response = status_account.generateAddresses(PATHS)
|
||||
|
||||
|
@ -86,13 +87,13 @@ proc init*(self: Service) =
|
|||
except Exception as e:
|
||||
error "error: ", procName="init", errName = e.name, errDesription = e.msg
|
||||
|
||||
proc clear*(self: Service) =
|
||||
proc clear*(self: Service) =
|
||||
self.generatedAccounts = @[]
|
||||
self.loggedInAccount = AccountDto()
|
||||
self.importedAccount = GeneratedAccountDto()
|
||||
self.isFirstTimeAccountLogin = false
|
||||
|
||||
proc validateMnemonic*(self: Service, mnemonic: string): string =
|
||||
proc validateMnemonic*(self: Service, mnemonic: string): string =
|
||||
try:
|
||||
let response = status_general.validateMnemonic(mnemonic)
|
||||
|
||||
|
@ -106,14 +107,14 @@ proc validateMnemonic*(self: Service, mnemonic: string): string =
|
|||
except Exception as e:
|
||||
error "error: ", procName="validateMnemonic", errName = e.name, errDesription = e.msg
|
||||
|
||||
proc generatedAccounts*(self: Service): seq[GeneratedAccountDto] =
|
||||
proc generatedAccounts*(self: Service): seq[GeneratedAccountDto] =
|
||||
if(self.generatedAccounts.len == 0):
|
||||
error "There was some issue initiating account service"
|
||||
return
|
||||
|
||||
result = self.generatedAccounts
|
||||
|
||||
proc openedAccounts*(self: Service): seq[AccountDto] =
|
||||
proc openedAccounts*(self: Service): seq[AccountDto] =
|
||||
try:
|
||||
let response = status_account.openedAccounts(main_constants.STATUSGODIR)
|
||||
|
||||
|
@ -124,7 +125,7 @@ proc openedAccounts*(self: Service): seq[AccountDto] =
|
|||
except Exception as e:
|
||||
error "error: ", procName="openedAccounts", errName = e.name, errDesription = e.msg
|
||||
|
||||
proc storeDerivedAccounts(self: Service, accountId, hashedPassword: string,
|
||||
proc storeDerivedAccounts(self: Service, accountId, hashedPassword: string,
|
||||
paths: seq[string]): DerivedAccounts =
|
||||
let response = status_account.storeDerivedAccounts(accountId, hashedPassword, paths)
|
||||
|
||||
|
@ -133,7 +134,7 @@ proc storeDerivedAccounts(self: Service, accountId, hashedPassword: string,
|
|||
|
||||
result = toDerivedAccounts(response.result)
|
||||
|
||||
proc storeAccount(self: Service, accountId, hashedPassword: string): GeneratedAccountDto =
|
||||
proc storeAccount(self: Service, accountId, hashedPassword: string): GeneratedAccountDto =
|
||||
let response = status_account.storeAccounts(accountId, hashedPassword)
|
||||
|
||||
if response.result.contains("error"):
|
||||
|
@ -141,7 +142,7 @@ proc storeAccount(self: Service, accountId, hashedPassword: string): GeneratedAc
|
|||
|
||||
result = toGeneratedAccountDto(response.result)
|
||||
|
||||
proc saveAccountAndLogin(self: Service, hashedPassword: string, account,
|
||||
proc saveAccountAndLogin(self: Service, hashedPassword: string, account,
|
||||
subaccounts, settings, config: JsonNode): AccountDto =
|
||||
try:
|
||||
let response = status_account.saveAccountAndLogin(hashedPassword, account, subaccounts, settings, config)
|
||||
|
@ -161,7 +162,7 @@ proc saveAccountAndLogin(self: Service, hashedPassword: string, account,
|
|||
except Exception as e:
|
||||
error "error: ", procName="saveAccountAndLogin", errName = e.name, errDesription = e.msg
|
||||
|
||||
proc saveKeycardAccountAndLogin(self: Service, chatKey, password: string, account, subaccounts, settings,
|
||||
proc saveKeycardAccountAndLogin(self: Service, chatKey, password: string, account, subaccounts, settings,
|
||||
config: JsonNode): AccountDto =
|
||||
try:
|
||||
let response = status_account.saveAccountAndLoginWithKeycard(chatKey, password, account, subaccounts, settings, config)
|
||||
|
@ -180,7 +181,7 @@ proc saveKeycardAccountAndLogin(self: Service, chatKey, password: string, accoun
|
|||
except Exception as e:
|
||||
error "error: ", procName="saveKeycardAccountAndLogin", errName = e.name, errDesription = e.msg
|
||||
|
||||
proc prepareAccountJsonObject(self: Service, account: GeneratedAccountDto, displayName: string): JsonNode =
|
||||
proc prepareAccountJsonObject(self: Service, account: GeneratedAccountDto, displayName: string): JsonNode =
|
||||
result = %* {
|
||||
"name": if displayName == "": account.alias else: displayName,
|
||||
"address": account.address,
|
||||
|
@ -189,7 +190,7 @@ proc prepareAccountJsonObject(self: Service, account: GeneratedAccountDto, displ
|
|||
"kdfIterations": KDF_ITERATIONS,
|
||||
}
|
||||
|
||||
proc getAccountDataForAccountId(self: Service, accountId: string, displayName: string): JsonNode =
|
||||
proc getAccountDataForAccountId(self: Service, accountId: string, displayName: string): JsonNode =
|
||||
for acc in self.generatedAccounts:
|
||||
if(acc.id == accountId):
|
||||
return self.prepareAccountJsonObject(acc, displayName)
|
||||
|
@ -198,7 +199,7 @@ proc getAccountDataForAccountId(self: Service, accountId: string, displayName: s
|
|||
if(self.importedAccount.id == accountId):
|
||||
return self.prepareAccountJsonObject(self.importedAccount, displayName)
|
||||
|
||||
proc prepareSubaccountJsonObject(self: Service, account: GeneratedAccountDto, displayName: string):
|
||||
proc prepareSubaccountJsonObject(self: Service, account: GeneratedAccountDto, displayName: string):
|
||||
JsonNode =
|
||||
result = %* [
|
||||
{
|
||||
|
@ -221,7 +222,7 @@ proc prepareSubaccountJsonObject(self: Service, account: GeneratedAccountDto, di
|
|||
}
|
||||
]
|
||||
|
||||
proc getSubaccountDataForAccountId(self: Service, accountId: string, displayName: string): JsonNode =
|
||||
proc getSubaccountDataForAccountId(self: Service, accountId: string, displayName: string): JsonNode =
|
||||
for acc in self.generatedAccounts:
|
||||
if(acc.id == accountId):
|
||||
return self.prepareSubaccountJsonObject(acc, displayName)
|
||||
|
@ -230,7 +231,7 @@ proc getSubaccountDataForAccountId(self: Service, accountId: string, displayName
|
|||
if(self.importedAccount.id == accountId):
|
||||
return self.prepareSubaccountJsonObject(self.importedAccount, displayName)
|
||||
|
||||
proc prepareAccountSettingsJsonObject(self: Service, account: GeneratedAccountDto,
|
||||
proc prepareAccountSettingsJsonObject(self: Service, account: GeneratedAccountDto,
|
||||
installationId: string, displayName: string): JsonNode =
|
||||
result = %* {
|
||||
"key-uid": account.keyUid,
|
||||
|
@ -263,7 +264,7 @@ proc prepareAccountSettingsJsonObject(self: Service, account: GeneratedAccountDt
|
|||
"profile-pictures-visibility": settings.PROFILE_PICTURES_VISIBILITY_EVERYONE
|
||||
}
|
||||
|
||||
proc getAccountSettings(self: Service, accountId: string,
|
||||
proc getAccountSettings(self: Service, accountId: string,
|
||||
installationId: string,
|
||||
displayName: string): JsonNode =
|
||||
for acc in self.generatedAccounts:
|
||||
|
@ -274,7 +275,7 @@ proc getAccountSettings(self: Service, accountId: string,
|
|||
if(self.importedAccount.id == accountId):
|
||||
return self.prepareAccountSettingsJsonObject(self.importedAccount, installationId, displayName)
|
||||
|
||||
proc getDefaultNodeConfig*(self: Service, installationId: string): JsonNode =
|
||||
proc getDefaultNodeConfig*(self: Service, installationId: string): JsonNode =
|
||||
let fleet = Fleet.StatusProd
|
||||
let dnsDiscoveryURL = @["enrtree://AOGECG2SPND25EEFMAJ5WF3KSGJNSGV356DSTL2YVLLZWIV6SAYBM@prod.nodes.status.im"]
|
||||
|
||||
|
@ -328,11 +329,11 @@ proc getDefaultNodeConfig*(self: Service, installationId: string): JsonNode =
|
|||
|
||||
result["KeyStoreDir"] = newJString(self.keyStoreDir.replace(main_constants.STATUSGODIR, ""))
|
||||
|
||||
proc setLocalAccountSettingsFile(self: Service) =
|
||||
proc setLocalAccountSettingsFile(self: Service) =
|
||||
if(defined(macosx) and self.getLoggedInAccount.isValid()):
|
||||
singletonInstance.localAccountSettings.setFileName(self.getLoggedInAccount.name)
|
||||
|
||||
proc addKeycardDetails(self: Service, settingsJson: var JsonNode, accountData: var JsonNode) =
|
||||
proc addKeycardDetails(self: Service, settingsJson: var JsonNode, accountData: var JsonNode) =
|
||||
let keycardPairingJsonString = readFile(main_constants.KEYCARDPAIRINGDATAFILE)
|
||||
let keycardPairingJsonObj = keycardPairingJsonString.parseJSON
|
||||
let now = now().toTime().toUnix()
|
||||
|
@ -344,7 +345,7 @@ proc addKeycardDetails(self: Service, settingsJson: var JsonNode, accountData: v
|
|||
if not accountData.isNil:
|
||||
accountData["keycard-pairing"] = kcDataObj{"key"}
|
||||
|
||||
proc setupAccount*(self: Service, accountId, password, displayName: string): string =
|
||||
proc setupAccount*(self: Service, accountId, password, displayName: string): string =
|
||||
try:
|
||||
let installationId = $genUUID()
|
||||
var accountDataJson = self.getAccountDataForAccountId(accountId, displayName)
|
||||
|
@ -378,7 +379,7 @@ proc setupAccount*(self: Service, accountId, password, displayName: string): str
|
|||
error "error: ", procName="setupAccount", errName = e.name, errDesription = e.msg
|
||||
return e.msg
|
||||
|
||||
proc setupAccountKeycard*(self: Service, keycardData: KeycardEvent, useImportedAcc: bool) =
|
||||
proc setupAccountKeycard*(self: Service, keycardData: KeycardEvent, useImportedAcc: bool) =
|
||||
try:
|
||||
var keyUid = keycardData.keyUid
|
||||
var address = keycardData.masterKey.address
|
||||
|
@ -494,7 +495,7 @@ proc setupAccountKeycard*(self: Service, keycardData: KeycardEvent, useImportedA
|
|||
except Exception as e:
|
||||
error "error: ", procName="setupAccount", errName = e.name, errDesription = e.msg
|
||||
|
||||
proc createAccountFromMnemonic*(self: Service, mnemonic: string, includeEncryption = false, includeWhisper = false,
|
||||
proc createAccountFromMnemonic*(self: Service, mnemonic: string, includeEncryption = false, includeWhisper = false,
|
||||
includeRoot = false, includeDefaultWallet = false, includeEip1581 = false): GeneratedAccountDto =
|
||||
if mnemonic.len == 0:
|
||||
error "empty mnemonic"
|
||||
|
@ -516,7 +517,7 @@ proc createAccountFromMnemonic*(self: Service, mnemonic: string, includeEncrypti
|
|||
except Exception as e:
|
||||
error "error: ", procName="createAccountFromMnemonicAndDeriveAccountsForPaths", errName = e.name, errDesription = e.msg
|
||||
|
||||
proc importMnemonic*(self: Service, mnemonic: string): string =
|
||||
proc importMnemonic*(self: Service, mnemonic: string): string =
|
||||
if mnemonic.len == 0:
|
||||
return "empty mnemonic"
|
||||
try:
|
||||
|
@ -537,7 +538,7 @@ proc importMnemonic*(self: Service, mnemonic: string): string =
|
|||
error "error: ", procName="importMnemonic", errName = e.name, errDesription = e.msg
|
||||
return e.msg
|
||||
|
||||
proc login*(self: Service, account: AccountDto, password: string): string =
|
||||
proc login*(self: Service, account: AccountDto, password: string): string =
|
||||
try:
|
||||
let hashedPassword = hashString(password)
|
||||
var thumbnailImage: string
|
||||
|
@ -626,7 +627,7 @@ proc login*(self: Service, account: AccountDto, password: string): string =
|
|||
error "error: ", procName="login", errName = e.name, errDesription = e.msg
|
||||
return e.msg
|
||||
|
||||
proc loginAccountKeycard*(self: Service, keycardData: KeycardEvent): string =
|
||||
proc loginAccountKeycard*(self: Service, keycardData: KeycardEvent): string =
|
||||
try:
|
||||
self.setKeyStoreDir(keycardData.keyUid)
|
||||
|
||||
|
@ -662,7 +663,7 @@ proc loginAccountKeycard*(self: Service, keycardData: KeycardEvent): string =
|
|||
error "error: ", procName="loginAccountKeycard", errName = e.name, errDesription = e.msg
|
||||
return e.msg
|
||||
|
||||
proc verifyAccountPassword*(self: Service, account: string, password: string): bool =
|
||||
proc verifyAccountPassword*(self: Service, account: string, password: string): bool =
|
||||
try:
|
||||
let response = status_account.verifyAccountPassword(account, password, self.keyStoreDir)
|
||||
if(response.result.contains("error")):
|
||||
|
@ -676,7 +677,7 @@ proc verifyAccountPassword*(self: Service, account: string, password: string): b
|
|||
error "error: ", procName="verifyAccountPassword", errName = e.name, errDesription = e.msg
|
||||
|
||||
|
||||
proc convertToKeycardAccount*(self: Service, keyUid: string, currentPassword: string, newPassword: string): bool =
|
||||
proc convertToKeycardAccount*(self: Service, keyUid: string, currentPassword: string, newPassword: string): bool =
|
||||
try:
|
||||
var accountDataJson = %* {
|
||||
"name": self.getLoggedInAccount().name,
|
||||
|
@ -707,7 +708,7 @@ proc convertToKeycardAccount*(self: Service, keyUid: string, currentPassword: st
|
|||
except Exception as e:
|
||||
error "error: ", procName="convertToKeycardAccount", errName = e.name, errDesription = e.msg
|
||||
|
||||
proc verifyPassword*(self: Service, password: string): bool =
|
||||
proc verifyPassword*(self: Service, password: string): bool =
|
||||
try:
|
||||
let hashedPassword = hashString(password)
|
||||
let response = status_account.verifyPassword(hashedPassword)
|
||||
|
|
Loading…
Reference in New Issue