feat: generate new accounts and get wallet account correctly
This commit is contained in:
parent
1cc5d9155f
commit
c609a00784
|
@ -15,9 +15,9 @@ proc queryAccounts*(): string =
|
|||
var response = callPrivateRPC("eth_accounts")
|
||||
result = parseJson(response)["result"][0].getStr()
|
||||
|
||||
proc generateAddresses*(): seq[GeneratedAccount] =
|
||||
proc generateAddresses*(n = 5): seq[GeneratedAccount] =
|
||||
let multiAccountConfig = %* {
|
||||
"n": 5,
|
||||
"n": n,
|
||||
"mnemonicPhraseLength": 12,
|
||||
"bip39Passphrase": "",
|
||||
"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)
|
||||
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 =
|
||||
let deriveJson = %* {
|
||||
"accountID": accountId,
|
||||
|
|
|
@ -4,15 +4,34 @@ import json
|
|||
import httpclient, json
|
||||
import strformat
|
||||
import stint
|
||||
import strutils
|
||||
import strutils, sequtils
|
||||
import chronicles
|
||||
|
||||
proc getAccounts*(): seq[string] =
|
||||
var response = callPrivateRPC("eth_accounts")
|
||||
result = parseJson(response)["result"].to(seq[string])
|
||||
type WalletAccount* = object
|
||||
address*, path*, publicKey*, name*, color*: 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 =
|
||||
var args = %* {
|
||||
|
|
|
@ -4,6 +4,8 @@ import strformat
|
|||
import strutils
|
||||
import libstatus/wallet as status_wallet
|
||||
import libstatus/settings as status_settings
|
||||
import libstatus/accounts as status_accounts
|
||||
import chronicles
|
||||
|
||||
type CurrencyArgs* = ref object of Args
|
||||
currency*: string
|
||||
|
@ -88,13 +90,14 @@ proc updateBalance*(self: Account) =
|
|||
self.balance = fmt"{totalAccountBalance:.2f} {defaultCurrency}"
|
||||
|
||||
proc initAccounts*(self: WalletModel) =
|
||||
let accounts = status_wallet.getAccounts()
|
||||
let accounts = status_wallet.getWalletAccounts()
|
||||
|
||||
var totalAccountBalance: float = 0
|
||||
const symbol = "ETH"
|
||||
let defaultCurrency = getDefaultCurrency()
|
||||
|
||||
for address in accounts:
|
||||
for account in accounts:
|
||||
let address = account.address
|
||||
let eth_balance = getEthBalance(address)
|
||||
let usd_balance = getFiatValue(eth_balance, symbol, defaultCurrency)
|
||||
|
||||
|
@ -104,7 +107,7 @@ proc initAccounts*(self: WalletModel) =
|
|||
var assets: seq[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()
|
||||
self.accounts.add(account)
|
||||
|
||||
|
@ -113,14 +116,23 @@ proc getTotalFiatBalance*(self: WalletModel): string =
|
|||
fmt"{newBalance:.2f} {self.defaultCurrency}"
|
||||
|
||||
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 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] = @[]
|
||||
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.events.emit("newAccountAdded", AccountArgs(account: account))
|
||||
|
|
|
@ -140,14 +140,12 @@ Item {
|
|||
|
||||
ListView {
|
||||
id: listView
|
||||
height: 160
|
||||
anchors.bottom: parent.bottom
|
||||
anchors.top: walletValueTextContainer.bottom
|
||||
anchors.topMargin: Theme.padding
|
||||
spacing: 5
|
||||
anchors.right: parent.right
|
||||
anchors.rightMargin: 0
|
||||
anchors.left: parent.left
|
||||
anchors.leftMargin: 0
|
||||
|
||||
delegate: walletDelegate
|
||||
|
||||
|
|
Loading…
Reference in New Issue