From 6915c19c84a75e7b0f99e9eb1899464ee0e2acff Mon Sep 17 00:00:00 2001 From: Anthony Laibe Date: Wed, 12 Jan 2022 10:01:49 +0100 Subject: [PATCH] feat: move create accounts to status-go --- .../service/wallet_account/service.nim | 166 ++++++------------ vendor/status-lib | 2 +- 2 files changed, 56 insertions(+), 112 deletions(-) diff --git a/src/app_service/service/wallet_account/service.nim b/src/app_service/service/wallet_account/service.nim index 50d0cfa5f2..c1b892f6f4 100644 --- a/src/app_service/service/wallet_account/service.nim +++ b/src/app_service/service/wallet_account/service.nim @@ -189,130 +189,74 @@ method getWalletAccount*(self: Service, accountIndex: int): WalletAccountDto = method getCurrencyBalance*(self: Service): float64 = return self.getWalletAccounts().map(a => a.getCurrencyBalance()).foldl(a + b, 0.0) -method getDefaultAccount(self: Service): string = - return status_go_eth.getAccounts().result[0].getStr +method addNewAccountToLocalStore(self: Service) = + let accounts = fetchAccounts() + let prices = self.fetchPrices() -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 prices = self.fetchPrices() + var newAccount = accounts[0] + for account in accounts: + if not self.accounts.haskey(account.address): + newAccount = account + break - var newAccount = accounts[0] - for account in accounts: - if not self.accounts.haskey(account.address): - newAccount = account - break - - let balances = self.fetchBalances(@[newAccount.address]) - newAccount.tokens = self.buildTokens(newAccount, prices, balances{newAccount.address}) - self.accounts[newAccount.address] = newAccount - self.events.emit("walletAccount/accountSaved", AccountSaved(account: newAccount)) - except Exception as e: - return fmt"Error adding new account: {e.msg}" + let balances = self.fetchBalances(@[newAccount.address]) + newAccount.tokens = self.buildTokens(newAccount, prices, balances{newAccount.address}) + self.accounts[newAccount.address] = newAccount + self.events.emit("walletAccount/accountSaved", AccountSaved(account: newAccount)) method generateNewAccount*(self: Service, password: string, accountName: string, color: string): string = - let - setting = self.settingService.getSetting() - walletRootAddress = setting.walletRootAddress - walletIndex = setting.latestDerivedPath + 1 - defaultAccount = self.getDefaultAccount() - isPasswordOk = status_go_accounts.verifyAccountPassword(defaultAccount, password, KEYSTOREDIR) + try: + discard status_go_accounts.generateAccount( + password, + accountName, + color, + ) + except Exception as e: + return fmt"Error generating new account: {e.msg}" - 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, - color, - status_go_accounts.GENERATED, - true, - walletIndex, - accountId, - deriveResponse.result[path]{"publicKey"}.getStr - ) - if errMsg != "": - return errMsg - - discard self.settingService.saveSetting("latest-derived-path", walletIndex) + self.addNewAccountToLocalStore() return "" method addAccountsFromPrivateKey*(self: Service, privateKey: string, password: string, accountName: string, color: string): string = - let - accountResponse = status_go_accounts.multiAccountImportPrivateKey(privateKey) - defaultAccount = self.getDefaultAccount() - isPasswordOk = status_go_accounts.verifyAccountPassword(defaultAccount, password, KEYSTOREDIR) + try: + discard status_go_accounts.addAccountWithPrivateKey( + privateKey, + password, + accountName, + color, + ) + except Exception as e: + return fmt"Error adding account with private key: {e.msg}" - if not isPasswordOk: - return "Error generating new account: invalid password" + self.addNewAccountToLocalStore() + return "" - return self.saveAccount( - accountResponse.result{"address"}.getStr, - accountName, - password, - color, - status_go_accounts.KEY, - false, - 0, - accountResponse.result{"accountId"}.getStr, - accountResponse.result{"publicKey"}.getStr, - ) +method addAccountsFromSeed*(self: Service, mnemonic: string, password: string, accountName: string, color: string): string = + try: + discard status_go_accounts.addAccountWithMnemonic( + mnemonic, + password, + accountName, + color, + ) + except Exception as e: + return fmt"Error adding account with mnemonic: {e.msg}" -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 = status_go_accounts.verifyAccountPassword(defaultAccount, password, KEYSTOREDIR) - - if not isPasswordOk: - return "Error generating new account: invalid password" - - return self.saveAccount( - deriveResponse.result[PATH_DEFAULT_WALLET]{"address"}.getStr, - accountName, - password, - color, - status_go_accounts.SEED, - true, - 0, - accountId, - deriveResponse.result[PATH_DEFAULT_WALLET]{"publicKey"}.getStr - ) + self.addNewAccountToLocalStore() + return "" 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_accounts.addAccountWatch( + address, + accountName, + color, + ) + except Exception as e: + return fmt"Error adding account with mnemonic: {e.msg}" + + self.addNewAccountToLocalStore() + return "" method deleteAccount*(self: Service, address: string) = discard status_go_accounts.deleteAccount(address) diff --git a/vendor/status-lib b/vendor/status-lib index 40a908e528..7413bbde00 160000 --- a/vendor/status-lib +++ b/vendor/status-lib @@ -1 +1 @@ -Subproject commit 40a908e528938d2fe156631690c9c1b10a69ab52 +Subproject commit 7413bbde00a015fe5fca37846448135f1b43b2cf