feat: generate new accounts and get wallet account correctly

This commit is contained in:
Jonathan Rainville 2020-06-05 16:18:25 -04:00
parent 1cc5d9155f
commit c609a00784
4 changed files with 61 additions and 17 deletions

View File

@ -15,9 +15,9 @@ proc queryAccounts*(): string =
var response = callPrivateRPC("eth_accounts") var response = callPrivateRPC("eth_accounts")
result = parseJson(response)["result"][0].getStr() result = parseJson(response)["result"][0].getStr()
proc generateAddresses*(): seq[GeneratedAccount] = proc generateAddresses*(n = 5): seq[GeneratedAccount] =
let multiAccountConfig = %* { let multiAccountConfig = %* {
"n": 5, "n": n,
"mnemonicPhraseLength": 12, "mnemonicPhraseLength": 12,
"bip39Passphrase": "", "bip39Passphrase": "",
"paths": [PATH_WALLET_ROOT, PATH_EIP_1581, PATH_WHISPER, PATH_DEFAULT_WALLET] "paths": [PATH_WALLET_ROOT, PATH_EIP_1581, PATH_WHISPER, PATH_DEFAULT_WALLET]
@ -174,6 +174,21 @@ proc multiAccountImportMnemonic*(mnemonic: string): GeneratedAccount =
let importResult = $libstatus.multiAccountImportMnemonic($mnemonicJson) let importResult = $libstatus.multiAccountImportMnemonic($mnemonicJson)
result = Json.decode(importResult, GeneratedAccount) result = Json.decode(importResult, GeneratedAccount)
proc saveAccount*(account: GeneratedAccount, password: string, color: string): void =
let storeDerivedResult = storeDerivedAccounts(account, password)
discard callPrivateRPC("accounts_saveAccounts", %* [
[{
"color": color,
"name": account.name,
"address": account.derived.defaultWallet.address,
"public-key": account.derived.defaultWallet.publicKey,
# Do we need to update those?
"type": "generated",
"path": "m/44'/60'/0'/0/1"
}]
])
proc deriveAccounts*(accountId: string): MultiAccounts = proc deriveAccounts*(accountId: string): MultiAccounts =
let deriveJson = %* { let deriveJson = %* {
"accountID": accountId, "accountID": accountId,

View File

@ -4,15 +4,34 @@ import json
import httpclient, json import httpclient, json
import strformat import strformat
import stint import stint
import strutils import strutils, sequtils
import chronicles
proc getAccounts*(): seq[string] = type WalletAccount* = object
var response = callPrivateRPC("eth_accounts") address*, path*, publicKey*, name*, color*: string
result = parseJson(response)["result"].to(seq[string]) wallet*, chat*: bool
proc getWalletAccounts*(): seq[WalletAccount] =
try:
var response = callPrivateRPC("accounts_getAccounts")
let accounts = parseJson(response)["result"]
var walletAccounts:seq[WalletAccount] = @[]
for account in accounts:
if (account["chat"].to(bool) == false): # Might need a better condition
walletAccounts.add(WalletAccount(
address: $account["address"].getStr,
path: $account["path"].getStr,
publicKey: $account["public-key"].getStr,
name: $account["name"].getStr,
color: $account["color"].getStr,
wallet: $account["wallet"].getStr == "true",
chat: $account["chat"].getStr == "false",
))
result = walletAccounts
except:
error "Failed getting wallet accounts"
proc getAccount*(): string =
var accounts = getAccounts()
result = accounts[0]
proc sendTransaction*(from_address: string, to: string, value: string, password: string): string = proc sendTransaction*(from_address: string, to: string, value: string, password: string): string =
var args = %* { var args = %* {

View File

@ -4,6 +4,8 @@ import strformat
import strutils import strutils
import libstatus/wallet as status_wallet import libstatus/wallet as status_wallet
import libstatus/settings as status_settings import libstatus/settings as status_settings
import libstatus/accounts as status_accounts
import chronicles
type CurrencyArgs* = ref object of Args type CurrencyArgs* = ref object of Args
currency*: string currency*: string
@ -88,13 +90,14 @@ proc updateBalance*(self: Account) =
self.balance = fmt"{totalAccountBalance:.2f} {defaultCurrency}" self.balance = fmt"{totalAccountBalance:.2f} {defaultCurrency}"
proc initAccounts*(self: WalletModel) = proc initAccounts*(self: WalletModel) =
let accounts = status_wallet.getAccounts() let accounts = status_wallet.getWalletAccounts()
var totalAccountBalance: float = 0 var totalAccountBalance: float = 0
const symbol = "ETH" const symbol = "ETH"
let defaultCurrency = getDefaultCurrency() let defaultCurrency = getDefaultCurrency()
for address in accounts: for account in accounts:
let address = account.address
let eth_balance = getEthBalance(address) let eth_balance = getEthBalance(address)
let usd_balance = getFiatValue(eth_balance, symbol, defaultCurrency) let usd_balance = getFiatValue(eth_balance, symbol, defaultCurrency)
@ -104,7 +107,7 @@ proc initAccounts*(self: WalletModel) =
var assets: seq[Asset] = @[] var assets: seq[Asset] = @[]
assets.add(asset) assets.add(asset)
var account = Account(name: "Status Account", address: address, iconColor: "", balance: "", assetList: assets, realFiatBalance: totalAccountBalance) var account = Account(name: account.name, address: address, iconColor: account.color, balance: "", assetList: assets, realFiatBalance: totalAccountBalance)
account.updateBalance() account.updateBalance()
self.accounts.add(account) self.accounts.add(account)
@ -113,14 +116,23 @@ proc getTotalFiatBalance*(self: WalletModel): string =
fmt"{newBalance:.2f} {self.defaultCurrency}" fmt"{newBalance:.2f} {self.defaultCurrency}"
proc generateNewAccount*(self: WalletModel, password: string, accountName: string, color: string) = proc generateNewAccount*(self: WalletModel, password: string, accountName: string, color: string) =
# TODO get a real address that we unlock with the password
let accounts = status_accounts.generateAddresses(1)
var generatedAccount = accounts[0]
generatedAccount.name = accountName
try:
status_accounts.saveAccount(generatedAccount, password, color)
except:
error "Error sotring the new account. Bad password?"
return
var symbol = "SNT" var symbol = "SNT"
var asset = Asset(name:"Status", symbol: symbol, value: fmt"0.0", fiatValue: "$" & fmt"0.0", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg") var asset = Asset(name:"Status", symbol: symbol, value: fmt"0.0", fiatValue: "$" & fmt"0.0", image: fmt"../../img/token-icons/{toLowerAscii(symbol)}.svg")
var assets: seq[Asset] = @[] var assets: seq[Asset] = @[]
assets.add(asset) assets.add(asset)
var account = Account(name: accountName, address: "0x0000000000000000000000000000000000000000", iconColor: color, balance: fmt"0.00 {self.defaultCurrency}", assetList: assets, realFiatBalance: 0.0) var account = Account(name: accountName, address: generatedAccount.derived.defaultWallet.address, iconColor: color, balance: fmt"0.00 {self.defaultCurrency}", assetList: assets, realFiatBalance: 0.0)
self.accounts.add(account) self.accounts.add(account)
self.events.emit("newAccountAdded", AccountArgs(account: account)) self.events.emit("newAccountAdded", AccountArgs(account: account))

View File

@ -140,14 +140,12 @@ Item {
ListView { ListView {
id: listView id: listView
height: 160 anchors.bottom: parent.bottom
anchors.top: walletValueTextContainer.bottom anchors.top: walletValueTextContainer.bottom
anchors.topMargin: Theme.padding anchors.topMargin: Theme.padding
spacing: 5 spacing: 5
anchors.right: parent.right anchors.right: parent.right
anchors.rightMargin: 0
anchors.left: parent.left anchors.left: parent.left
anchors.leftMargin: 0
delegate: walletDelegate delegate: walletDelegate