parent
20a2d48eea
commit
cd8bd4147c
|
@ -11,6 +11,7 @@ import ./service_interface, ./dto
|
||||||
import ../../../app/core/eventemitter
|
import ../../../app/core/eventemitter
|
||||||
import ../../../backend/accounts as status_go_accounts
|
import ../../../backend/accounts as status_go_accounts
|
||||||
import ../../../backend/tokens as status_go_tokens
|
import ../../../backend/tokens as status_go_tokens
|
||||||
|
import ../../../backend/wallet as status_go_wallet
|
||||||
import ../../../backend/eth as status_go_eth
|
import ../../../backend/eth as status_go_eth
|
||||||
|
|
||||||
export service_interface
|
export service_interface
|
||||||
|
@ -165,7 +166,6 @@ method fetchPrices(self: Service): Table[string, float64] =
|
||||||
method fetchBalances(self: Service, accounts: seq[string]): JsonNode =
|
method fetchBalances(self: Service, accounts: seq[string]): JsonNode =
|
||||||
let chainId = self.settingsService.getCurrentNetworkId()
|
let chainId = self.settingsService.getCurrentNetworkId()
|
||||||
let tokens = self.getVisibleTokens().map(t => t.addressAsString())
|
let tokens = self.getVisibleTokens().map(t => t.addressAsString())
|
||||||
|
|
||||||
return status_go_tokens.getBalances(chainId, accounts, tokens).result
|
return status_go_tokens.getBalances(chainId, accounts, tokens).result
|
||||||
|
|
||||||
method refreshBalances(self: Service) =
|
method refreshBalances(self: Service) =
|
||||||
|
@ -203,33 +203,7 @@ method getWalletAccount*(self: Service, accountIndex: int): WalletAccountDto =
|
||||||
method getCurrencyBalance*(self: Service): float64 =
|
method getCurrencyBalance*(self: Service): float64 =
|
||||||
return self.getWalletAccounts().map(a => a.getCurrencyBalance()).foldl(a + b, 0.0)
|
return self.getWalletAccounts().map(a => a.getCurrencyBalance()).foldl(a + b, 0.0)
|
||||||
|
|
||||||
method getDefaultAccount(self: Service): string =
|
method addNewAccountToLocalStore(self: Service) =
|
||||||
return status_go_eth.getAccounts().result[0].getStr
|
|
||||||
|
|
||||||
method saveAccount(
|
|
||||||
self: Service,
|
|
||||||
address: string,
|
|
||||||
name: string,
|
|
||||||
password: string,
|
|
||||||
color: string,
|
|
||||||
accountType: string,
|
|
||||||
isADerivedAccount = true,
|
|
||||||
walletIndex: int = 0,
|
|
||||||
id: string = "",
|
|
||||||
publicKey: string = "",
|
|
||||||
): string =
|
|
||||||
try:
|
|
||||||
status_go_accounts.saveAccount(
|
|
||||||
address,
|
|
||||||
name,
|
|
||||||
password,
|
|
||||||
color,
|
|
||||||
accountType,
|
|
||||||
isADerivedAccount = true,
|
|
||||||
walletIndex,
|
|
||||||
id,
|
|
||||||
publicKey,
|
|
||||||
)
|
|
||||||
let accounts = fetchAccounts()
|
let accounts = fetchAccounts()
|
||||||
let prices = self.fetchPrices()
|
let prices = self.fetchPrices()
|
||||||
|
|
||||||
|
@ -241,92 +215,57 @@ method saveAccount(
|
||||||
|
|
||||||
let balances = self.fetchBalances(@[newAccount.address])
|
let balances = self.fetchBalances(@[newAccount.address])
|
||||||
newAccount.tokens = self.buildTokens(newAccount, prices, balances{newAccount.address})
|
newAccount.tokens = self.buildTokens(newAccount, prices, balances{newAccount.address})
|
||||||
|
|
||||||
self.accounts[newAccount.address] = newAccount
|
self.accounts[newAccount.address] = newAccount
|
||||||
self.events.emit("walletAccount/accountSaved", AccountSaved(account: newAccount))
|
self.events.emit("walletAccount/accountSaved", AccountSaved(account: newAccount))
|
||||||
except Exception as e:
|
|
||||||
return fmt"Error adding new account: {e.msg}"
|
|
||||||
|
|
||||||
method generateNewAccount*(self: Service, password: string, accountName: string, color: string): string =
|
method generateNewAccount*(self: Service, password: string, accountName: string, color: string): string =
|
||||||
let
|
try:
|
||||||
walletRootAddress = self.settingsService.getWalletRootAddress()
|
discard status_go_wallet.generateAccount(
|
||||||
walletIndex = self.settingsService.getLatestDerivedPath() + 1
|
|
||||||
defaultAccount = self.getDefaultAccount()
|
|
||||||
isPasswordOk = self.accountsService.verifyAccountPassword(defaultAccount, password)
|
|
||||||
|
|
||||||
if not isPasswordOk:
|
|
||||||
return "Error generating new account: invalid password"
|
|
||||||
|
|
||||||
let accountResponse = status_go_accounts.loadAccount(walletRootAddress, password)
|
|
||||||
let accountId = accountResponse.result{"id"}.getStr
|
|
||||||
let path = "m/" & $walletIndex
|
|
||||||
let deriveResponse = status_go_accounts.deriveAccounts(accountId, @[path])
|
|
||||||
let errMsg = self.saveAccount(
|
|
||||||
deriveResponse.result[path]{"address"}.getStr,
|
|
||||||
accountName,
|
|
||||||
password,
|
password,
|
||||||
color,
|
accountName,
|
||||||
status_go_accounts.GENERATED,
|
color)
|
||||||
true,
|
except Exception as e:
|
||||||
walletIndex,
|
return fmt"Error generating new account: {e.msg}"
|
||||||
accountId,
|
|
||||||
deriveResponse.result[path]{"publicKey"}.getStr
|
|
||||||
)
|
|
||||||
if errMsg != "":
|
|
||||||
return errMsg
|
|
||||||
|
|
||||||
discard self.settingsService.saveLatestDerivedPath(walletIndex)
|
self.addNewAccountToLocalStore()
|
||||||
return ""
|
|
||||||
|
|
||||||
method addAccountsFromPrivateKey*(self: Service, privateKey: string, password: string, accountName: string, color: string): string =
|
method addAccountsFromPrivateKey*(self: Service, privateKey: string, password: string, accountName: string, color: string): string =
|
||||||
let
|
try:
|
||||||
accountResponse = status_go_accounts.multiAccountImportPrivateKey(privateKey)
|
discard status_go_wallet.addAccountWithPrivateKey(
|
||||||
defaultAccount = self.getDefaultAccount()
|
privateKey,
|
||||||
isPasswordOk = self.accountsService.verifyAccountPassword(defaultAccount, password)
|
|
||||||
|
|
||||||
if not isPasswordOk:
|
|
||||||
return "Error generating new account: invalid password"
|
|
||||||
|
|
||||||
return self.saveAccount(
|
|
||||||
accountResponse.result{"address"}.getStr,
|
|
||||||
accountName,
|
|
||||||
password,
|
password,
|
||||||
color,
|
|
||||||
status_go_accounts.KEY,
|
|
||||||
false,
|
|
||||||
0,
|
|
||||||
accountResponse.result{"accountId"}.getStr,
|
|
||||||
accountResponse.result{"publicKey"}.getStr,
|
|
||||||
)
|
|
||||||
|
|
||||||
method addAccountsFromSeed*(self: Service, seedPhrase: string, password: string, accountName: string, color: string): string =
|
|
||||||
let mnemonic = replace(seedPhrase, ',', ' ')
|
|
||||||
let paths = @[PATH_WALLET_ROOT, PATH_EIP_1581, PATH_WHISPER, PATH_DEFAULT_WALLET]
|
|
||||||
let accountResponse = status_go_accounts.multiAccountImportMnemonic(mnemonic)
|
|
||||||
let accountId = accountResponse.result{"id"}.getStr
|
|
||||||
let deriveResponse = status_go_accounts.deriveAccounts(accountId, paths)
|
|
||||||
|
|
||||||
let
|
|
||||||
defaultAccount = self.getDefaultAccount()
|
|
||||||
isPasswordOk = self.accountsService.verifyAccountPassword(defaultAccount, password)
|
|
||||||
|
|
||||||
if not isPasswordOk:
|
|
||||||
return "Error generating new account: invalid password"
|
|
||||||
|
|
||||||
return self.saveAccount(
|
|
||||||
deriveResponse.result[PATH_DEFAULT_WALLET]{"address"}.getStr,
|
|
||||||
accountName,
|
accountName,
|
||||||
password,
|
|
||||||
color,
|
color,
|
||||||
status_go_accounts.SEED,
|
|
||||||
true,
|
|
||||||
0,
|
|
||||||
accountId,
|
|
||||||
deriveResponse.result[PATH_DEFAULT_WALLET]{"publicKey"}.getStr
|
|
||||||
)
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return fmt"Error adding account with private key: {e.msg}"
|
||||||
|
|
||||||
|
self.addNewAccountToLocalStore()
|
||||||
|
|
||||||
|
method addAccountsFromSeed*(self: Service, mnemonic: string, password: string, accountName: string, color: string): string =
|
||||||
|
try:
|
||||||
|
discard status_go_wallet.addAccountWithMnemonic(
|
||||||
|
mnemonic,
|
||||||
|
password,
|
||||||
|
accountName,
|
||||||
|
color,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return fmt"Error adding account with mnemonic: {e.msg}"
|
||||||
|
|
||||||
|
self.addNewAccountToLocalStore()
|
||||||
|
|
||||||
method addWatchOnlyAccount*(self: Service, address: string, accountName: string, color: string): string =
|
method addWatchOnlyAccount*(self: Service, address: string, accountName: string, color: string): string =
|
||||||
return self.saveAccount(address, accountName, "", color, status_go_accounts.WATCH, false)
|
try:
|
||||||
|
discard status_go_wallet.addAccountWatch(
|
||||||
|
address,
|
||||||
|
accountName,
|
||||||
|
color,
|
||||||
|
)
|
||||||
|
except Exception as e:
|
||||||
|
return fmt"Error adding account with mnemonic: {e.msg}"
|
||||||
|
|
||||||
|
self.addNewAccountToLocalStore()
|
||||||
|
|
||||||
method deleteAccount*(self: Service, address: string) =
|
method deleteAccount*(self: Service, address: string) =
|
||||||
discard status_go_accounts.deleteAccount(address)
|
discard status_go_accounts.deleteAccount(address)
|
||||||
|
|
|
@ -10,3 +10,19 @@ logScope:
|
||||||
proc getPendingTransactions*(): RpcResponse[JsonNode] {.raises: [Exception].} =
|
proc getPendingTransactions*(): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||||
let payload = %* []
|
let payload = %* []
|
||||||
result = core.callPrivateRPC("wallet_getPendingTransactions", payload)
|
result = core.callPrivateRPC("wallet_getPendingTransactions", payload)
|
||||||
|
|
||||||
|
proc generateAccount*(password, name, color: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||||
|
let payload = %* [hashPassword(password), name, color]
|
||||||
|
return core.callPrivateRPC("accounts_generateAccount", payload)
|
||||||
|
|
||||||
|
proc addAccountWithMnemonic*(mnemonic, password, name, color: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||||
|
let payload = %* [mnemonic, hashPassword(password), name, color]
|
||||||
|
return core.callPrivateRPC("accounts_addAccountWithMnemonic", payload)
|
||||||
|
|
||||||
|
proc addAccountWithPrivateKey*(privateKey, password, name, color: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||||
|
let payload = %* [privateKey, hashPassword(password), name, color]
|
||||||
|
return core.callPrivateRPC("accounts_addAccountWithPrivateKey", payload)
|
||||||
|
|
||||||
|
proc addAccountWatch*(address, name, color: string): RpcResponse[JsonNode] {.raises: [Exception].} =
|
||||||
|
let payload = %* [address, name, color]
|
||||||
|
return core.callPrivateRPC("accounts_addAccountWatch", payload)
|
Loading…
Reference in New Issue