From d5592d0e0ada8cdbcb87c97b590d11919becb93c Mon Sep 17 00:00:00 2001 From: Michele Balistreri Date: Mon, 4 Oct 2021 15:13:05 +0300 Subject: [PATCH] store and login with Keycard accounts --- status/accounts.nim | 12 ++++----- status/statusgo_backend/accounts.nim | 39 +++++++++++++++++++++------- status/types/account.nim | 9 +++++-- 3 files changed, 43 insertions(+), 17 deletions(-) diff --git a/status/accounts.nim b/status/accounts.nim index 6813f11..8ce487e 100644 --- a/status/accounts.nim +++ b/status/accounts.nim @@ -28,16 +28,16 @@ proc generateAddresses*(self: AccountModel): seq[GeneratedAccount] = proc openAccounts*(self: AccountModel, statusGoDir: string): seq[NodeAccount] = result = status_accounts.openAccounts(statusGoDir) -proc login*(self: AccountModel, selectedAccountIndex: int, password: string): NodeAccount = +proc login*(self: AccountModel, selectedAccountIndex: int, password: string, keycardWhisperKey = ""): NodeAccount = let currentNodeAccount = self.nodeAccounts[selectedAccountIndex] - result = status_accounts.login(currentNodeAccount, password) + result = status_accounts.login(currentNodeAccount, password, keycardWhisperKey) -proc storeAccountAndLogin*(self: AccountModel, fleetConfig: FleetConfig, selectedAccountIndex: int, password: string): Account = +proc storeAccountAndLogin*(self: AccountModel, fleetConfig: FleetConfig, selectedAccountIndex: int, password: string, keycardWhisperKey = ""): Account = let generatedAccount: GeneratedAccount = self.generatedAddresses[selectedAccountIndex] - result = status_accounts.setupAccount(fleetConfig, generatedAccount, password) + result = status_accounts.setupAccount(fleetConfig, generatedAccount, password, keycardWhisperKey) -proc storeDerivedAndLogin*(self: AccountModel, fleetConfig: FleetConfig, importedAccount: GeneratedAccount, password: string): Account = - result = status_accounts.setupAccount(fleetConfig, importedAccount, password) +proc storeDerivedAndLogin*(self: AccountModel, fleetConfig: FleetConfig, importedAccount: GeneratedAccount, password: string, keycardWhisperKey = ""): Account = + result = status_accounts.setupAccount(fleetConfig, importedAccount, password, keycardWhisperKey) proc importMnemonic*(self: AccountModel, mnemonic: string): GeneratedAccount = let importedAccount = status_accounts.multiAccountImportMnemonic(mnemonic) diff --git a/status/statusgo_backend/accounts.nim b/status/statusgo_backend/accounts.nim index 7ff214f..0bebf5c 100644 --- a/status/statusgo_backend/accounts.nim +++ b/status/statusgo_backend/accounts.nim @@ -1,6 +1,6 @@ import json, os, nimcrypto, uuids, json_serialization, chronicles, strutils -from status_go import multiAccountGenerateAndDeriveAddresses, generateAlias, identicon, saveAccountAndLogin, login, openAccounts, getNodeConfig +from status_go import multiAccountGenerateAndDeriveAddresses, generateAlias, identicon, saveAccountAndLogin, login, openAccounts, getNodeConfig, saveAccountAndLoginWithKeycard, loginWithKeycard import core import ../utils as utils from ../wallet/account as walletAccount import WalletAccount @@ -117,13 +117,13 @@ proc openAccounts*(STATUSGODIR: string): seq[NodeAccount] = result.add(nodeAccount) - proc saveAccountAndLogin*( account: GeneratedAccount, accountData: string, password: string, configJSON: string, - settingsJSON: string): Account = + settingsJSON: string, + keycardWhisperKey: string): Account = let hashedPassword = hashPassword(password) let subaccountData = %* [ { @@ -144,7 +144,12 @@ proc saveAccountAndLogin*( } ] - var savedResult = $status_go.saveAccountAndLogin(accountData, hashedPassword, settingsJSON, configJSON, $subaccountData) + var savedResult = + if account.isKeycard: + $status_go.saveAccountAndLogin(accountData, hashedPassword, settingsJSON, configJSON, $subaccountData) + else: + $status_go.saveAccountAndLoginWithKeycard(accountData, hashedPassword, settingsJSON, configJSON, $subaccountData, keycardWhisperKey) + let parsedSavedResult = savedResult.parseJson let error = parsedSavedResult["error"].getStr @@ -179,6 +184,9 @@ proc getAccountData*(account: GeneratedAccount): JsonNode = "keycard-pairing": nil } + if account.isKeycard: + result["keycard-pairing"] = %* "yes" + proc getAccountSettings*(account: GeneratedAccount, defaultNetworks: JsonNode, installationId: string): JsonNode = result = %* { "key-uid": account.keyUid, @@ -205,14 +213,22 @@ proc getAccountSettings*(account: GeneratedAccount, defaultNetworks: JsonNode, i "installation-id": installationId } -proc setupAccount*(fleetConfig: FleetConfig, account: GeneratedAccount, password: string): Account = + # actual pairing info has no reason to be in the account, but keycard-pairing is used as a sort of boolean + if account.isKeycard: + result["keycard-instance-uid"] = %* "unused" + result["keycard-paired-on"] = %* 0 + result["keycard-pairing"] = %* "yes" + + +proc setupAccount*(fleetConfig: FleetConfig, account: GeneratedAccount, password: string, keycardWhisperKey: string): Account = try: - let storeDerivedResult = storeDerivedAccounts(account, password) + if not account.isKeycard: + let storeDerivedResult = storeDerivedAccounts(account, password) let accountData = getAccountData(account) let installationId = $genUUID() var settingsJSON = getAccountSettings(account, constants.DEFAULT_NETWORKS, installationId) var nodeConfig = getDefaultNodeConfig(fleetConfig, installationId) - result = saveAccountAndLogin(account, $accountData, password, $nodeConfig, $settingsJSON) + result = saveAccountAndLogin(account, $accountData, password, $nodeConfig, $settingsJSON, keycardWhisperKey) except StatusGoException as e: raise newException(StatusGoException, "Error setting up account: " & e.msg) @@ -222,10 +238,15 @@ proc setupAccount*(fleetConfig: FleetConfig, account: GeneratedAccount, password let peer = "enode://44160e22e8b42bd32a06c1532165fa9e096eebedd7fa6d6e5f8bbef0440bc4a4591fe3651be68193a7ec029021cdb496cfe1d7f9f1dc69eb99226e6f39a7a5d4@35.225.221.245:443" discard status_go.addPeer(peer) -proc login*(nodeAccount: NodeAccount, password: string): NodeAccount = +proc login*(nodeAccount: NodeAccount, password: string, keycardWhisperKey: string): NodeAccount = let hashedPassword = hashPassword(password) let account = nodeAccount.toAccount - let loginResult = $status_go.login($toJson(account), hashedPassword) + let loginResult = + if account.isKeycard: + $status_go.login($toJson(account), hashedPassword) + else: + $status_go.loginWithKeycard($toJson(account), hashedPassword, keycardWhisperKey) + let error = parseJson(loginResult)["error"].getStr if error == "": diff --git a/status/types/account.nim b/status/types/account.nim index d1ae153..816994b 100644 --- a/status/types/account.nim +++ b/status/types/account.nim @@ -15,6 +15,7 @@ type keyUid* {.serializedFieldName("key-uid").}: string identityImage*: IdentityImage identicon*: string + isKeycard* {.dontSerialize.}: bool type NodeAccount* = ref object of Account @@ -35,12 +36,16 @@ type keyUid*: string identicon*: string identityImage*: IdentityImage + isKeycard*: bool + +proc isKeycard*(account: NodeAccount): bool = + result = account.keycardPairing != "" proc toAccount*(account: GeneratedAccount): Account = - result = Account(name: account.name, identityImage: account.identityImage, identicon: account.identicon, keyUid: account.keyUid) + result = Account(name: account.name, identityImage: account.identityImage, identicon: account.identicon, keyUid: account.keyUid, isKeycard: account.isKeycard) proc toAccount*(account: NodeAccount): Account = - result = Account(name: account.name, identityImage: account.identityImage, identicon: account.identicon, keyUid: account.keyUid) + result = Account(name: account.name, identityImage: account.identityImage, identicon: account.identicon, keyUid: account.keyUid, isKeycard: isKeycard(account)) type AccountArgs* = ref object of Args account*: Account