2021-09-08 18:05:39 +00:00
|
|
|
import json, strformat, options, chronicles, sugar, sequtils, strutils
|
|
|
|
|
2021-09-11 21:50:36 +00:00
|
|
|
import statusgo_backend/accounts as status_accounts
|
|
|
|
import statusgo_backend/accounts/constants as constants
|
|
|
|
import statusgo_backend/wallet as status_wallet
|
|
|
|
import statusgo_backend/network as status_network
|
|
|
|
import statusgo_backend/settings as status_settings
|
2021-09-22 17:49:45 +00:00
|
|
|
import eth/contracts
|
|
|
|
import wallet2/balance_manager
|
|
|
|
import eth/tokens as tokens_backend
|
2021-09-08 18:05:39 +00:00
|
|
|
import wallet2/account as wallet_account
|
2021-09-22 17:49:45 +00:00
|
|
|
import ./types/[account, transaction, network, network_type, setting, gas_prediction, rpc_response]
|
2021-09-08 18:05:39 +00:00
|
|
|
import ../eventemitter
|
|
|
|
from web3/ethtypes import Address
|
|
|
|
from web3/conversions import `$`
|
|
|
|
|
2021-09-20 16:24:36 +00:00
|
|
|
export wallet_account
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
logScope:
|
|
|
|
topics = "status-wallet2"
|
|
|
|
|
|
|
|
type
|
|
|
|
CryptoServicesArg* = ref object of Args
|
|
|
|
services*: JsonNode # an array
|
|
|
|
|
|
|
|
type
|
|
|
|
StatusWalletController* = ref object
|
|
|
|
events: EventEmitter
|
|
|
|
accounts: seq[WalletAccount]
|
2021-09-16 21:30:29 +00:00
|
|
|
networks*: seq[Network]
|
2021-09-08 18:05:39 +00:00
|
|
|
tokens: seq[Erc20Contract]
|
|
|
|
totalBalance*: float
|
|
|
|
|
|
|
|
# Forward declarations
|
|
|
|
proc initEvents*(self: StatusWalletController)
|
|
|
|
proc generateAccountConfiguredAssets*(self: StatusWalletController,
|
|
|
|
accountAddress: string): seq[Asset]
|
|
|
|
proc calculateTotalFiatBalance*(self: StatusWalletController)
|
|
|
|
|
|
|
|
proc setup(self: StatusWalletController, events: EventEmitter) =
|
|
|
|
self.events = events
|
|
|
|
self.accounts = @[]
|
|
|
|
self.tokens = @[]
|
2021-09-09 21:36:28 +00:00
|
|
|
self.networks = @[]
|
2021-09-08 18:05:39 +00:00
|
|
|
self.totalBalance = 0.0
|
|
|
|
self.initEvents()
|
|
|
|
|
|
|
|
proc delete*(self: StatusWalletController) =
|
|
|
|
discard
|
|
|
|
|
2021-09-22 17:49:45 +00:00
|
|
|
proc newStatusWalletController*(events: EventEmitter): StatusWalletController =
|
2021-09-08 18:05:39 +00:00
|
|
|
result = StatusWalletController()
|
|
|
|
result.setup(events)
|
|
|
|
|
|
|
|
proc initTokens(self: StatusWalletController) =
|
2021-09-22 17:49:45 +00:00
|
|
|
let network = status_settings.getCurrentNetwork().toNetwork()
|
|
|
|
self.tokens = tokens_backend.getVisibleTokens(network)
|
2021-09-08 18:05:39 +00:00
|
|
|
|
2021-09-09 21:36:28 +00:00
|
|
|
proc initNetworks(self: StatusWalletController) =
|
|
|
|
self.networks = status_network.getNetworks()
|
|
|
|
|
2021-09-08 18:05:39 +00:00
|
|
|
proc initAccounts(self: StatusWalletController) =
|
|
|
|
let accounts = status_wallet.getWalletAccounts()
|
|
|
|
for acc in accounts:
|
|
|
|
var assets: seq[Asset] = self.generateAccountConfiguredAssets(acc.address)
|
|
|
|
var walletAccount = newWalletAccount(acc.name, acc.address, acc.iconColor,
|
|
|
|
acc.path, acc.walletType, acc.publicKey, acc.wallet, acc.chat, assets)
|
|
|
|
self.accounts.add(walletAccount)
|
|
|
|
|
|
|
|
proc init*(self: StatusWalletController) =
|
|
|
|
self.initTokens()
|
2021-09-09 21:36:28 +00:00
|
|
|
self.initNetworks()
|
2021-09-08 18:05:39 +00:00
|
|
|
self.initAccounts()
|
|
|
|
|
|
|
|
proc initEvents*(self: StatusWalletController) =
|
|
|
|
self.events.on("currencyChanged") do(e: Args):
|
|
|
|
self.events.emit("accountsUpdated", Args())
|
|
|
|
|
|
|
|
self.events.on("newAccountAdded") do(e: Args):
|
|
|
|
self.calculateTotalFiatBalance()
|
|
|
|
|
|
|
|
proc getAccounts*(self: StatusWalletController): seq[WalletAccount] =
|
|
|
|
self.accounts
|
|
|
|
|
|
|
|
proc getDefaultCurrency*(self: StatusWalletController): string =
|
|
|
|
# TODO: this should come from a model? It is going to be used too in the
|
|
|
|
# profile section and ideally we should not call the settings more than once
|
|
|
|
status_settings.getSetting[string](Setting.Currency, "usd")
|
|
|
|
|
|
|
|
proc generateAccountConfiguredAssets*(self: StatusWalletController,
|
|
|
|
accountAddress: string): seq[Asset] =
|
|
|
|
var assets: seq[Asset] = @[]
|
|
|
|
var asset = Asset(name:"Ethereum", symbol: "ETH", value: "0.0",
|
|
|
|
fiatBalanceDisplay: "0.0", accountAddress: accountAddress)
|
|
|
|
assets.add(asset)
|
|
|
|
for token in self.tokens:
|
|
|
|
var symbol = token.symbol
|
|
|
|
var existingToken = Asset(name: token.name, symbol: symbol,
|
|
|
|
value: fmt"0.0", fiatBalanceDisplay: "$0.0", accountAddress: accountAddress,
|
|
|
|
address: $token.address)
|
|
|
|
assets.add(existingToken)
|
|
|
|
assets
|
|
|
|
|
|
|
|
proc calculateTotalFiatBalance*(self: StatusWalletController) =
|
|
|
|
self.totalBalance = 0.0
|
|
|
|
for account in self.accounts:
|
|
|
|
if account.realFiatBalance.isSome:
|
|
|
|
self.totalBalance += account.realFiatBalance.get()
|
|
|
|
|
|
|
|
proc newAccount*(self: StatusWalletController, walletType: string, derivationPath: string,
|
|
|
|
name: string, address: string, iconColor: string, balance: string,
|
|
|
|
publicKey: string): WalletAccount =
|
|
|
|
var assets: seq[Asset] = self.generateAccountConfiguredAssets(address)
|
|
|
|
var account = WalletAccount(name: name, path: derivationPath, walletType: walletType,
|
|
|
|
address: address, iconColor: iconColor, balance: none[string](), assetList: assets,
|
|
|
|
realFiatBalance: none[float](), publicKey: publicKey)
|
|
|
|
updateBalance(account, self.getDefaultCurrency())
|
|
|
|
account
|
|
|
|
|
|
|
|
proc addNewGeneratedAccount(self: StatusWalletController, generatedAccount: GeneratedAccount,
|
|
|
|
password: string, accountName: string, color: string, accountType: string,
|
|
|
|
isADerivedAccount = true, walletIndex: int = 0) =
|
|
|
|
try:
|
|
|
|
generatedAccount.name = accountName
|
|
|
|
var derivedAccount: DerivedAccount = status_accounts.saveAccount(generatedAccount,
|
|
|
|
password, color, accountType, isADerivedAccount, walletIndex)
|
|
|
|
var account = self.newAccount(accountType, derivedAccount.derivationPath,
|
|
|
|
accountName, derivedAccount.address, color, fmt"0.00 {self.getDefaultCurrency()}",
|
|
|
|
derivedAccount.publicKey)
|
|
|
|
|
|
|
|
self.accounts.add(account)
|
|
|
|
# wallet_checkRecentHistory is required to be called when a new account is
|
|
|
|
# added before wallet_getTransfersByAddress can be called. This is because
|
|
|
|
# wallet_checkRecentHistory populates the status-go db that
|
|
|
|
# wallet_getTransfersByAddress reads from
|
|
|
|
discard status_wallet.checkRecentHistory(self.accounts.map(account => account.address))
|
|
|
|
self.events.emit("newAccountAdded", wallet_account.AccountArgs(account: account))
|
|
|
|
except Exception as e:
|
|
|
|
raise newException(StatusGoException, fmt"Error adding new account: {e.msg}")
|
|
|
|
|
|
|
|
proc generateNewAccount*(self: StatusWalletController, password: string, accountName: string, color: string) =
|
|
|
|
let
|
|
|
|
walletRootAddress = status_settings.getSetting[string](Setting.WalletRootAddress, "")
|
|
|
|
walletIndex = status_settings.getSetting[int](Setting.LatestDerivedPath) + 1
|
|
|
|
loadedAccount = status_accounts.loadAccount(walletRootAddress, password)
|
|
|
|
derivedAccount = status_accounts.deriveWallet(loadedAccount.id, walletIndex)
|
|
|
|
generatedAccount = GeneratedAccount(
|
|
|
|
id: loadedAccount.id,
|
|
|
|
publicKey: derivedAccount.publicKey,
|
|
|
|
address: derivedAccount.address
|
|
|
|
)
|
|
|
|
|
|
|
|
# if we've gotten here, the password is ok (loadAccount requires a valid password)
|
|
|
|
# so no need to check for a valid password
|
|
|
|
self.addNewGeneratedAccount(generatedAccount, password, accountName, color, constants.GENERATED, true, walletIndex)
|
|
|
|
|
|
|
|
let statusGoResult = status_settings.saveSetting(Setting.LatestDerivedPath, $walletIndex)
|
|
|
|
if statusGoResult.error != "":
|
|
|
|
error "Error storing the latest wallet index", msg=statusGoResult.error
|
|
|
|
|
2021-09-10 15:29:10 +00:00
|
|
|
proc addAccountsFromSeed*(self: StatusWalletController, seed: string, password: string, accountName: string, color: string, keystoreDir: string) =
|
2021-09-08 18:05:39 +00:00
|
|
|
let mnemonic = replace(seed, ',', ' ')
|
|
|
|
var generatedAccount = status_accounts.multiAccountImportMnemonic(mnemonic)
|
|
|
|
generatedAccount.derived = status_accounts.deriveAccounts(generatedAccount.id)
|
|
|
|
|
|
|
|
let
|
|
|
|
defaultAccount = status_accounts.getDefaultAccount()
|
2021-09-10 15:29:10 +00:00
|
|
|
isPasswordOk = status_accounts.verifyAccountPassword(defaultAccount, password, keystoreDir)
|
2021-09-08 18:05:39 +00:00
|
|
|
if not isPasswordOk:
|
|
|
|
raise newException(StatusGoException, "Error generating new account: invalid password")
|
|
|
|
|
|
|
|
self.addNewGeneratedAccount(generatedAccount, password, accountName, color, constants.SEED)
|
|
|
|
|
2021-09-10 15:29:10 +00:00
|
|
|
proc addAccountsFromPrivateKey*(self: StatusWalletController, privateKey: string, password: string, accountName: string, color: string, keystoreDir: string) =
|
2021-09-08 18:05:39 +00:00
|
|
|
let
|
|
|
|
generatedAccount = status_accounts.MultiAccountImportPrivateKey(privateKey)
|
|
|
|
defaultAccount = status_accounts.getDefaultAccount()
|
2021-09-10 15:29:10 +00:00
|
|
|
isPasswordOk = status_accounts.verifyAccountPassword(defaultAccount, password, keystoreDir)
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
if not isPasswordOk:
|
|
|
|
raise newException(StatusGoException, "Error generating new account: invalid password")
|
|
|
|
|
|
|
|
self.addNewGeneratedAccount(generatedAccount, password, accountName, color, constants.KEY, false)
|
|
|
|
|
|
|
|
proc addWatchOnlyAccount*(self: StatusWalletController, address: string, accountName: string, color: string) =
|
|
|
|
let account = GeneratedAccount(address: address)
|
|
|
|
self.addNewGeneratedAccount(account, "", accountName, color, constants.WATCH, false)
|
|
|
|
|
|
|
|
proc changeAccountSettings*(self: StatusWalletController, address: string, accountName: string, color: string): string =
|
|
|
|
var selectedAccount: WalletAccount
|
|
|
|
for account in self.accounts:
|
|
|
|
if (account.address == address):
|
|
|
|
selectedAccount = account
|
|
|
|
break
|
|
|
|
if (isNil(selectedAccount)):
|
|
|
|
result = "No account found with that address"
|
|
|
|
error "No account found with that address", address
|
|
|
|
selectedAccount.name = accountName
|
|
|
|
selectedAccount.iconColor = color
|
|
|
|
result = status_accounts.changeAccount(selectedAccount.name, selectedAccount.address,
|
|
|
|
selectedAccount.publicKey, selectedAccount.walletType, selectedAccount.iconColor)
|
|
|
|
|
|
|
|
proc deleteAccount*(self: StatusWalletController, address: string): string =
|
|
|
|
result = status_accounts.deleteAccount(address)
|
|
|
|
self.accounts = self.accounts.filter(acc => acc.address.toLowerAscii != address.toLowerAscii)
|
|
|
|
|
|
|
|
proc getOpenseaCollections*(address: string): string =
|
2021-09-20 16:24:36 +00:00
|
|
|
let networkId = status_settings.getCurrentNetworkDetails().config.networkId
|
|
|
|
result = status_wallet.getOpenseaCollections(networkId, address)
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
proc getOpenseaAssets*(address: string, collectionSlug: string, limit: int): string =
|
2021-09-20 16:24:36 +00:00
|
|
|
let networkId = status_settings.getCurrentNetworkDetails().config.networkId
|
|
|
|
result = status_wallet.getOpenseaAssets(networkId, address, collectionSlug, limit)
|
2021-09-08 18:05:39 +00:00
|
|
|
|
|
|
|
proc onAsyncFetchCryptoServices*(self: StatusWalletController, response: string) =
|
|
|
|
let responseArray = response.parseJson
|
|
|
|
if (responseArray.kind != JArray):
|
|
|
|
info "received crypto services is not a json array"
|
|
|
|
self.events.emit("cryptoServicesFetched", CryptoServicesArg())
|
|
|
|
return
|
|
|
|
|
|
|
|
self.events.emit("cryptoServicesFetched", CryptoServicesArg(services: responseArray))
|