mirror of
https://github.com/status-im/status-desktop.git
synced 2025-01-22 04:21:44 +00:00
feat(@desktop/keycard): accounts and wallet services updated
New procs added: - accounts service `createAccountFromMnemonic` - accounts service `convertToKeycardAccount` - accounts service `verifyPassword` - wallet service `fetchBalanceForAddress`
This commit is contained in:
parent
912cbe3b1d
commit
ec7710490d
@ -427,6 +427,16 @@ proc setupAccountKeycard*(self: Service, keycardData: KeycardEvent) =
|
||||
except Exception as e:
|
||||
error "error: ", procName="setupAccount", errName = e.name, errDesription = e.msg
|
||||
|
||||
proc createAccountFromMnemonic*(self: Service, mnemonic: string): GeneratedAccountDto =
|
||||
if mnemonic.len == 0:
|
||||
error "empty mnemonic"
|
||||
return
|
||||
try:
|
||||
let response = status_account.createAccountFromMnemonic(mnemonic)
|
||||
return toGeneratedAccountDto(response.result)
|
||||
except Exception as e:
|
||||
error "error: ", procName="createAccountFromMnemonic", errName = e.name, errDesription = e.msg
|
||||
|
||||
proc importMnemonic*(self: Service, mnemonic: string): string =
|
||||
if mnemonic.len == 0:
|
||||
return "empty mnemonic"
|
||||
@ -538,6 +548,10 @@ proc loginAccountKeycard*(self: Service, keycardData: KeycardEvent): string =
|
||||
error = response.result["error"].getStr
|
||||
if error == "":
|
||||
debug "Account logged in succesfully"
|
||||
# this should be fetched later from waku
|
||||
self.loggedInAccount.name = alias
|
||||
self.loggedInAccount.keyUid = keycardData.keyUid
|
||||
self.loggedInAccount.keycardPairing = accountDataJson{"keycard-pairing"}.getStr
|
||||
return
|
||||
except Exception as e:
|
||||
error "error: ", procName="loginAccountKeycard", errName = e.name, errDesription = e.msg
|
||||
@ -555,3 +569,46 @@ proc verifyAccountPassword*(self: Service, account: string, password: string): b
|
||||
return false
|
||||
except Exception as e:
|
||||
error "error: ", procName="verifyAccountPassword", errName = e.name, errDesription = e.msg
|
||||
|
||||
|
||||
proc convertToKeycardAccount*(self: Service, keyUid: string, password: string): bool =
|
||||
try:
|
||||
self.setKeyStoreDir(keyUid)
|
||||
|
||||
var accountDataJson = %* {
|
||||
"name": self.getLoggedInAccount().name,
|
||||
"key-uid": keyUid
|
||||
}
|
||||
var settingsJson = %* { }
|
||||
|
||||
self.addKeycardDetails(settingsJson, accountDataJson)
|
||||
|
||||
if(accountDataJson.isNil or settingsJson.isNil):
|
||||
let description = "at least one json object is not prepared well"
|
||||
error "error: ", procName="convertToKeycardAccount", errDesription = description
|
||||
return
|
||||
|
||||
let hashedCurrentPassword = hashString(password)
|
||||
let hashedNewPassword = hashString(keyUid)
|
||||
|
||||
let response = status_account.convertToKeycardAccount(self.keyStoreDir, accountDataJson, settingsJson, hashedCurrentPassword,
|
||||
hashedNewPassword)
|
||||
|
||||
if(response.result.contains("error")):
|
||||
let errMsg = response.result["error"].getStr
|
||||
if(errMsg.len == 0):
|
||||
return true
|
||||
else:
|
||||
error "error: ", procName="convertToKeycardAccount", errDesription = errMsg
|
||||
return false
|
||||
except Exception as e:
|
||||
error "error: ", procName="convertToKeycardAccount", errName = e.name, errDesription = e.msg
|
||||
|
||||
proc verifyPassword*(self: Service, password: string): bool =
|
||||
try:
|
||||
let hashedPassword = hashString(password)
|
||||
let response = status_account.verifyPassword(hashedPassword)
|
||||
return response.result.getBool
|
||||
except Exception as e:
|
||||
error "error: ", procName="verifyPassword", errName = e.name, errDesription = e.msg
|
||||
return false
|
@ -61,12 +61,6 @@ proc hex2Balance*(input: string, decimals: int): string =
|
||||
result = $i
|
||||
if(r > 0): result = fmt"{result}.{d}"
|
||||
|
||||
proc fetchAccounts(): seq[WalletAccountDto] =
|
||||
let response = status_go_accounts.getAccounts()
|
||||
return response.result.getElems().map(
|
||||
x => x.toWalletAccountDto()
|
||||
).filter(a => not a.isChat)
|
||||
|
||||
type AccountSaved = ref object of Args
|
||||
account: WalletAccountDto
|
||||
|
||||
@ -156,11 +150,17 @@ QtObject:
|
||||
error "error: ", errDesription
|
||||
return 0.0
|
||||
|
||||
proc fetchAccounts*(self: Service): seq[WalletAccountDto] =
|
||||
let response = status_go_accounts.getAccounts()
|
||||
return response.result.getElems().map(
|
||||
x => x.toWalletAccountDto()
|
||||
).filter(a => not a.isChat)
|
||||
|
||||
proc init*(self: Service) =
|
||||
signalConnect(singletonInstance.localAccountSensitiveSettings, "isWalletEnabledChanged()", self, "onIsWalletEnabledChanged()", 2)
|
||||
|
||||
try:
|
||||
let accounts = fetchAccounts()
|
||||
let accounts = self.fetchAccounts()
|
||||
for account in accounts:
|
||||
account.relatedAccounts = accounts.filter(x => not account.derivedFrom.isEmptyOrWhitespace and (cmpIgnoreCase(x.derivedFrom, account.derivedFrom) == 0))
|
||||
self.walletAccounts[account.address] = account
|
||||
@ -211,7 +211,7 @@ QtObject:
|
||||
return self.getWalletAccounts().map(a => a.getCurrencyBalance()).foldl(a + b, 0.0)
|
||||
|
||||
proc addNewAccountToLocalStore(self: Service) =
|
||||
let accounts = fetchAccounts()
|
||||
let accounts = self.fetchAccounts()
|
||||
var newAccount = accounts[0]
|
||||
for account in accounts:
|
||||
if not self.walletAccounts.haskey(account.address):
|
||||
@ -452,3 +452,24 @@ QtObject:
|
||||
|
||||
proc findTokenSymbolByAddress*(self: Service, address: string): string =
|
||||
return self.tokenService.findTokenSymbolByAddressInAllNetworks(address)
|
||||
|
||||
proc fetchBalanceForAddress*(self: Service, address: string): float64 =
|
||||
let currency = self.settingsService.getCurrency()
|
||||
let networks = self.networkService.getNetworks()
|
||||
var networkSymbols: seq[string]
|
||||
var allTokens: seq[TokenDto]
|
||||
for n in networks:
|
||||
networkSymbols.add(n.nativeCurrencySymbol)
|
||||
allTokens.add(getTokensForChainId(n))
|
||||
allTokens.add(getCustomTokens())
|
||||
allTokens = deduplicate(allTokens)
|
||||
|
||||
var prices = fetchPrices(networkSymbols, allTokens, currency)
|
||||
let tokenBalances = getTokensBalances(@[address], allTokens)
|
||||
|
||||
var totalBalance = 0.0
|
||||
for token in allTokens:
|
||||
let balanceForToken = tokenBalances{address}{token.addressAsString()}.getStr
|
||||
let chainBalanceForToken = parsefloat(hex2Balance(balanceForToken, token.decimals))
|
||||
totalBalance = totalBalance + chainBalanceForToken * prices[token.symbol]
|
||||
return totalBalance
|
@ -114,6 +114,19 @@ proc multiAccountImportMnemonic*(mnemonic: string): RpcResponse[JsonNode] {.rais
|
||||
error "error doing rpc request", methodName = "multiAccountImportMnemonic", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc createAccountFromMnemonic*(mnemonic: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* {
|
||||
"mnemonicPhrase": mnemonic,
|
||||
"Bip39Passphrase": ""
|
||||
}
|
||||
|
||||
try:
|
||||
let response = status_go.createAccountFromMnemonic($payload)
|
||||
result.result = Json.decode(response, JsonNode)
|
||||
except RpcException as e:
|
||||
error "error doing rpc request", methodName = "createAccountFromMnemonic", exception=e.msg
|
||||
raise newException(RpcException, e.msg)
|
||||
|
||||
proc deriveAccounts*(accountId: string, paths: seq[string]): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* {
|
||||
"accountID": accountId,
|
||||
@ -302,3 +315,6 @@ proc getDerivedAddressForPrivateKey*(privateKey: string,): RpcResponse[JsonNode]
|
||||
let payload = %* [privateKey]
|
||||
result = core.callPrivateRPC("wallet_getDerivedAddressForPrivateKey", payload)
|
||||
|
||||
proc verifyPassword*(password: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||
let payload = %* [password]
|
||||
return core.callPrivateRPC("accounts_verifyPassword", payload)
|
2
vendor/nim-status-go
vendored
2
vendor/nim-status-go
vendored
@ -1 +1 @@
|
||||
Subproject commit c8f5bf373771a586400d98832f9652eba9901681
|
||||
Subproject commit 00a352da93cef3010dd6c213b87a66ce1d9cad01
|
Loading…
x
Reference in New Issue
Block a user