From db3722cd4fbf617e274ba2a74c43f78abbb1fdf6 Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Thu, 5 May 2022 15:43:29 +0200 Subject: [PATCH] fix(@general): user status go service to migrate account --- src/app/boot/app_controller.nim | 2 - src/app_service/service/accounts/service.nim | 60 +++----------------- src/backend/core.nim | 14 ++++- src/constants.nim | 2 +- vendor/status-go | 2 +- 5 files changed, 23 insertions(+), 57 deletions(-) diff --git a/src/app/boot/app_controller.nim b/src/app/boot/app_controller.nim index 588ff71acc..77d53f54d0 100644 --- a/src/app/boot/app_controller.nim +++ b/src/app/boot/app_controller.nim @@ -290,8 +290,6 @@ proc start*(self: AppController) = self.startupModule.load() proc load(self: AppController) = - self.accountsService.deleteExtraKeyStoreFile() - self.notificationsManager.init() self.settingsService.init() diff --git a/src/app_service/service/accounts/service.nim b/src/app_service/service/accounts/service.nim index 5b3cdc218b..51e3e66294 100644 --- a/src/app_service/service/accounts/service.nim +++ b/src/app_service/service/accounts/service.nim @@ -1,5 +1,4 @@ import os, json, sequtils, strutils, uuids ->>>>>>> 8b5de1363 (fix(@general): keystore management) import json_serialization, chronicles import times as times @@ -7,6 +6,7 @@ import ./dto/accounts as dto_accounts import ./dto/generated_accounts as dto_generated_accounts import ../../../backend/accounts as status_account import ../../../backend/general as status_general +import ../../../backend/core as status_core import ../../../app/core/fleets/fleet_configuration import ../../common/[account_constants, network_constants, utils, string_utils] @@ -50,49 +50,6 @@ proc getImportedAccount*(self: Service): GeneratedAccountDto = proc isFirstTimeAccountLogin*(self: Service): bool = return self.isFirstTimeAccountLogin -# Remove extra copied keystore that are not needed (can be remove 1st jan 2023) -proc deleteExtraKeyStoreFile*(self: Service) = - let accounts = status_account.getAccounts().result - var deleteCandidates: seq[string] = @[] - for path in walkFiles(self.keyStoreDir & "*"): - var found = false - for account in accounts.getElems(): - let address = account{"address"}.getStr.replace("0x", "") - if path.endsWith(address): - found = true - break - - if not found: - deleteCandidates.add(path) - - if len(deleteCandidates) == 2: - return - - let tz = times.utc() - let tf = times.initTimeFormat("yyyy-mm-dd'T'HH-mm-ss") - proc extractTime(path: string): DateTime = - return os.extractFilename(path).split("--")[1].split(".")[0].parse(tf, tz) - - let interval = times.initDuration(seconds = 2) - var toDelete: seq[string] = @[] - for a in deleteCandidates: - let aTime = extractTime(a) - var found = false - for b in deleteCandidates: - let bTime = extractTime(b) - - if aTime - bTime < interval: - found = true - break - - if found: - continue - - toDelete.add(a) - - for path in toDelete: - os.removeFile(path) - proc setKeyStoreDir(self: Service, key: string) = self.keyStoreDir = joinPath(main_constants.ROOTKEYSTOREDIR, key) & main_constants.sep discard status_general.initKeystore(self.keyStoreDir) @@ -381,12 +338,12 @@ proc login*(self: Service, account: AccountDto, password: string): string = elif(img.imgType == "large"): largeImage = img.uri - # Copy old keystore file to new dir, this code can be remove 1st jan 2023 let keyStoreDir = joinPath(main_constants.ROOTKEYSTOREDIR, account.keyUid) & main_constants.sep - if not dirExists(self.keyStoreDir): - os.createDir(self.keyStoreDir) - for path in walkFiles(main_constants.ROOTKEYSTOREDIR & "*"): - os.copyFile(path, self.keyStoreDir & os.extractFilename(path)) + if not dirExists(keyStoreDir): + os.createDir(keyStoreDir) + status_core.migrateKeyStoreDir($ %* { + "key-uid": account.keyUid + }, password, main_constants.ROOTKEYSTOREDIR, keyStoreDir) self.setKeyStoreDir(account.keyUid) # This is moved from `status-lib` here @@ -420,8 +377,7 @@ proc login*(self: Service, account: AccountDto, password: string): string = } let response = status_account.login(account.name, account.keyUid, hashedPassword, thumbnailImage, - largeImage, $nodeCfg) - + largeImage, $nodeCfg) var error = "response doesn't contain \"error\"" if(response.result.contains("error")): error = response.result["error"].getStr @@ -432,7 +388,7 @@ proc login*(self: Service, account: AccountDto, password: string): string = return error except Exception as e: - error "error: ", procName="setupAccount", errName = e.name, errDesription = e.msg + error "error: ", procName="login", errName = e.name, errDesription = e.msg return e.msg proc verifyAccountPassword*(self: Service, account: string, password: string): bool = diff --git a/src/backend/core.nim b/src/backend/core.nim index fbd26fdc8f..62f22166bd 100644 --- a/src/backend/core.nim +++ b/src/backend/core.nim @@ -68,4 +68,16 @@ proc sendTransaction*(inputJSON: string, password: string): RpcResponse[JsonNode result = Json.decode(rpcResponseRaw, RpcResponse[JsonNode]) except RpcException as e: error "error sending tx", inputJSON, exception=e.msg - raise newException(RpcException, e.msg) \ No newline at end of file + raise newException(RpcException, e.msg) + + +proc migrateKeyStoreDir*(account: string, password: string, oldKeystoreDir: string, multiaccountKeystoreDir: string) + {.raises: [RpcException, ValueError, Defect, SerializationError].} = + try: + var hashed_password = "0x" & $keccak_256.digest(password) + discard status_go.migrateKeyStoreDir(account, hashed_password, oldKeystoreDir, multiaccountKeystoreDir) + except RpcException as e: + error "error migrating keystore dir", account, exception=e.msg + raise newException(RpcException, e.msg) + + \ No newline at end of file diff --git a/src/constants.nim b/src/constants.nim index 40ffdf7f19..889c528f89 100644 --- a/src/constants.nim +++ b/src/constants.nim @@ -56,7 +56,7 @@ let OPENURI* = desktopConfig.uri DATADIR* = baseDir & sep STATUSGODIR* = joinPath(baseDir, "data") & sep - ROOTKEYSTOREDIR* = joinPath(baseDir, "data", "keystore") & sep + ROOTKEYSTOREDIR* = joinPath(baseDir, "data", "keystore") TMPDIR* = joinPath(baseDir, "tmp") & sep LOGDIR* = joinPath(baseDir, "logs") & sep diff --git a/vendor/status-go b/vendor/status-go index 55f2900ec2..7227ae1c8e 160000 --- a/vendor/status-go +++ b/vendor/status-go @@ -1 +1 @@ -Subproject commit 55f2900ec2fcb6384b79bd4f55ddc70cdfb0bff0 +Subproject commit 7227ae1c8ef1f74e180f324185d01f564730af70